text:
# @author : Abhishek R S

import os
import h5py
import numpy as np
import tensorflow as tf

"""
SegNet
# Reference
- [Very Deep Convolutional Networks for Large-Scale Image Recognition]
  (https://arxiv.org/abs/1409.1556)
- [SegNet](https://arxiv.org/pdf/1511.00561.pdf)
- [Bayesian SegNet](https://arxiv.org/pdf/1511.02680.pdf)
- [SegNet Project](http://mi.eng.cam.ac.uk/projects/segnet/)

# Pretrained model weights
- [Download pretrained vgg-16 model]
  (https://github.com/fchollet/deep-learning-models/releases/)
"""

class SegNet:
    def __init__(self, pretrained_weights, is_training, data_format="channels_first", num_classes=15):
        self._weights_h5 = h5py.File(pretrained_weights, "r")
        self._is_training = is_training
        self._data_format = data_format
        self._num_classes = num_classes
        self._padding = "SAME"
        self._encoder_conv_strides = [1, 1, 1, 1]
        self._feature_map_axis = None
        self._encoder_data_format = None
        self._encoder_pool_kernel = None
        self._encoder_pool_strides = None
        self._initializer = tf.contrib.layers.xavier_initializer_conv2d()

        """
        based on the data format set appropriate pool_kernel and pool_strides
        always use channels_first i.e. NCHW as the data format on a GPU
        """

        if data_format == "channels_first":
            self._encoder_data_format = "NCHW"
            self._encoder_pool_kernel = [1, 1, 2, 2]
            self._encoder_pool_strides = [1, 1, 2, 2]
            self._feature_map_axis = 1
        else:
            self._encoder_data_format = "NHWC"
            self._encoder_pool_kernel = [1, 2, 2, 1]
            self._encoder_pool_strides = [1, 2, 2, 1]
            self._feature_map_axis = -1

    # build vgg-16 encoder
    def vgg16_encoder(self, features):
        # input : BGR format with image_net mean subtracted
        # bgr mean : [103.939, 116.779, 123.68]

        if self._data_format == "channels_last":
            features = tf.transpose(features, perm=[0, 2, 3, 1])

        # Stage 1
        self.conv1_1 = self._conv_block(features, "block1_conv1")
        self.conv1_2 = self._conv_block(self.conv1_1, "block1_conv2")
        self.pool1 = self._maxpool_layer(self.conv1_2, name="pool1")

        # Stage 2
        self.conv2_1 = self._conv_block(self.pool1, "block2_conv1")
        self.conv2_2 = self._conv_block(self.conv2_1, "block2_conv2")
        self.pool2 = self._maxpool_layer(self.conv2_2, name="pool2")

        # Stage 3
        self.conv3_1 = self._conv_block(self.pool2, "block3_conv1")
        self.conv3_2 = self._conv_block(self.conv3_1, "block3_conv2")
        self.conv3_3 = self._conv_block(self.conv3_2, "block3_conv3")
        self.pool3 = self._maxpool_layer(self.conv3_3, name="pool3")

        # Stage 4
        self.conv4_1 = self._conv_block(self.pool3, "block4_conv1")
        self.conv4_2 = self._conv_block(self.conv4_1, "block4_conv2")
        self.conv4_3 = self._conv_block(self.conv4_2, "block4_conv3")
        self.pool4 = self._maxpool_layer(self.conv4_3, name="pool4")

        # Stage 5
        self.conv5_1 = self._conv_block(self.pool4, "block5_conv1")
        self.conv5_2 = self._conv_block(self.conv5_1, "block5_conv2")
        self.conv5_3 = self._conv_block(self.conv5_2, "block5_conv3")
        self.pool5 = self._maxpool_layer(self.conv5_3, name="pool5")

    # define the bayesian decoder with transposed convolution upsampling
    def segnet_bayesian(self):
        self.decoder1 = self._get_decoder_block_tr_conv(
            self.pool5, 512, 3, name="decoder1_")
        self.dropout1 = self._get_dropout_layer(
            self.decoder1, name="decoder1_dropout")
        self.decoder2 = self._get_decoder_block_tr_conv(
            self.dropout1, 512, 3, name="decoder2_")
        self.dropout2 = self._get_dropout_layer(
            self.decoder2, name="decoder2_dropout")
        self.decoder3 = self._get_decoder_block_tr_conv(
            self.dropout2, 256, 3, name="decoder3_")
        self.dropout3 = self._get_dropout_layer(
            self.decoder3, name="decoder3_dropout")
        self.decoder4 = self._get_decoder_block_tr_conv(
            self.dropout3, 128, 2, name="decoder4_")
        self.dropout4 = self._get_dropout_layer(
            self.decoder4, name="decoder4_dropout")
        self.decoder5 = self._get_decoder_block_tr_conv(
            self.dropout4, 64, 2, name="decoder5_")

        self.logits = self._get_conv2d_layer(
            self.decoder5, self._num_classes, [1, 1], [1, 1], name="logits"
        )

    # define the decoder with transposed convolution upsampling
    def segnet_tr_conv(self):
        self.decoder1 = self._get_decoder_block_tr_conv(
            self.pool5, 512, 3, name="decoder1_")
        self.decoder2 = self._get_decoder_block_tr_conv(
            self.decoder1, 512, 3, name="decoder2_")
        self.decoder3 = self._get_decoder_block_tr_conv(
            self.decoder2, 256, 3, name="decoder3_")
        self.decoder4 = self._get_decoder_block_tr_conv(
            self.decoder3, 128, 2, name="decoder4_")
        self.decoder5 = self._get_decoder_block_tr_conv(
            self.decoder4, 64, 2, name="decoder5_")

        self.logits = self._get_conv2d_layer(
            self.decoder5, self._num_classes, [1, 1], [1, 1], name="logits"
        )

    # define the decoder with bilinear upsampling
    def segnet_bilinear(self):
        self.decoder1 = self._get_decoder_block_bilinear(
            self.pool5, 512, 3, name="decoder1_")
        self.decoder2 = self._get_decoder_block_bilinear(
            self.decoder1, 512, 3, name="decoder2_")
        self.decoder3 = self._get_decoder_block_bilinear(
            self.decoder2, 256, 3, name="decoder3_")
        self.decoder4 = self._get_decoder_block_bilinear(
            self.decoder3, 128, 2, name="decoder4_")
        self.decoder5 = self._get_decoder_block_bilinear(
            self.decoder4, 64, 2, name="decoder5_")

        self.logits = self._get_conv2d_layer(
            self.decoder5, self._num_classes, [1, 1], [1, 1], name="logits"
        )

    # define the decoder with nearest neighbor upsampling
    def segnet_nn(self):
        self.decoder1 = self._get_decoder_block_nn(
            self.pool5, 512, 3, name="decoder1_")
        self.decoder2 = self._get_decoder_block_nn(
            self.decoder1, 512, 3, name="decoder2_")
        self.decoder3 = self._get_decoder_block_nn(
            self.decoder2, 256, 3, name="decoder3_")
        self.decoder4 = self._get_decoder_block_nn(
            self.decoder3, 128, 2, name="decoder4_")
        self.decoder5 = self._get_decoder_block_nn(
            self.decoder4, 64, 2, name="decoder5_")

        self.logits = self._get_conv2d_layer(
            self.decoder5, self._num_classes, [1, 1], [1, 1], name="logits"
        )

    # return decoder block for transposed convolution upsampling
    def _get_decoder_block_tr_conv(self, features, num_out_features, num_intermediate_layers, name="decoder_"):
        _up = self._get_conv2d_transpose_layer(
            features, num_out_features, [2, 2], [2, 2], name=name + "tr_conv"
        )
        inputs = _up

        for i in range(num_intermediate_layers):
            _conv = self._get_conv2d_layer(
                inputs, num_out_features, [3, 3], [1, 1], name=name + "conv" + str(i + 1)
            )
            _bn = self._get_batchnorm_layer(_conv, name=name + "bn" + str(i + 1))
            _relu = self._get_relu_activation(_bn, name=name + "relu" + str(i + 1))
            inputs = _relu

        return _relu

    # return decoder block for bilinear upsampling
    def _get_decoder_block_bilinear(self, features, num_out_features, num_intermediate_layers, name="decoder_"):
        if self._data_format == "channels_first":
            features = tf.transpose(features, perm=[0, 2, 3, 1])

        up_size = 2 * tf.shape(features)[1:3]
        _up = tf.image.resize_bilinear(features, size=up_size, name=name + "up_bilinear")

        if self._data_format == "channels_first":
            _up = tf.transpose(_up, perm=[0, 3, 1, 2])

        inputs = _up

        for i in range(num_intermediate_layers):
            _conv = self._get_conv2d_layer(
                inputs, num_out_features, [3, 3], [1, 1], name=name + "conv" + str(i + 1)
            )
            _bn = self._get_batchnorm_layer(_conv, name=name + "bn" + str(i + 1))
            _relu = self._get_relu_activation(_bn, name=name + "relu" + str(i + 1))
            inputs = _relu

        return _relu

    # return decoder block for nearest neighbor upsampling
    def _get_decoder_block_nn(self, features, num_out_features, num_intermediate_layers, name="decoder_"):
        if self._data_format == "channels_first":
            features = tf.transpose(features, perm=[0, 2, 3, 1])

        up_size = 2 * tf.shape(features)[1:3]
        _up = tf.image.resize_nearest_neighbor(features, size=up_size, name=name + "up_nearest_neighbour")

        if self._data_format == "channels_first":
            _up = tf.transpose(_up, perm=[0, 3, 1, 2])

        inputs = _up

        for i in range(num_intermediate_layers):
            _conv = self._get_conv2d_layer(
                inputs, num_out_features, [3, 3], [1, 1], name=name + "conv" + str(i + 1)
            )
            _bn = self._get_batchnorm_layer(_conv, name=name + "bn" + str(i + 1))
            _relu = self._get_relu_activation(_bn, name=name + "relu" + str(i + 1))
            inputs = _relu

        return _relu

    # return convolution2d layer
    def _get_conv2d_layer(self, inputs, num_filters, kernel_size, strides, name="conv"):
        conv_2d_layer = tf.layers.conv2d(
            inputs=inputs, filters=num_filters, kernel_size=kernel_size, strides=strides, padding=self._padding,
            data_format=self._data_format, kernel_initializer=self._initializer, name=name
        )
        return conv_2d_layer

    # return convolution2d_transpose layer
    def _get_conv2d_transpose_layer(self, inputs, num_filters, kernel_size, strides, name="conv_tr"):
        conv_2d_tr_layer = tf.layers.conv2d_transpose(
            inputs=inputs, filters=num_filters, kernel_size=kernel_size, strides=strides, padding=self._padding,
            data_format=self._data_format, kernel_initializer=self._initializer, name=name
        )
        return conv_2d_tr_layer

    # return relu activation function
    def _get_relu_activation(self, inputs, name="relu"):
        relu_layer = tf.nn.relu(inputs, name=name)
        return relu_layer

    # return dropout layer
    def _get_dropout_layer(self, inputs, rate=0.5, name="dropout"):
        dropout_layer = tf.layers.dropout(inputs=inputs, rate=rate, training=self._is_training, name=name)
        return dropout_layer

    # return batch normalization layer
    def _get_batchnorm_layer(self, inputs, name="bn"):
        bn_layer = tf.layers.batch_normalization(inputs, axis=self._feature_map_axis, training=self._is_training, name=name)
        return bn_layer

    #-------------------------------------#
    # pretrained vgg-16 encoder functions #
    #-------------------------------------#
    #-----------------------#
    # convolution2d layer   #
    #-----------------------#
    def _conv_block(self, input_layer, name):
        W = tf.constant(self._weights_h5[name][name + "_W_1:0"])
        b = self._weights_h5[name][name + "_b_1:0"]
        b = tf.constant(np.reshape(b, (b.shape[0])))

        x = tf.nn.conv2d(
            input_layer, filter=W, strides=self._encoder_conv_strides,
            padding=self._padding, data_format=self._encoder_data_format, name=name + "_conv"
        )
        x = tf.nn.bias_add(
            x, b, data_format=self._encoder_data_format, name=name + "_bias"
        )
        x = tf.nn.relu(x, name=name + "_relu")

        return x

    #-----------------------#
    # maxpool2d layer       #
    #-----------------------#
    def _maxpool_layer(self, input_layer, name):
        pool_layer = tf.nn.max_pool(
            input_layer, ksize=self._encoder_pool_kernel, strides=self._encoder_pool_strides,
            padding=self._padding, data_format=self._encoder_data_format, name=name
        )

        return pool_layer


text:
'''
cba_data from http://www.worldmrio.com/footprints/carbon/

wdi_data from https://datacatalog.worldbank.org/dataset/world-development-indicators

gender_data from https://datacatalog.worldbank.org/dataset/gender-statistics
'''

import pandas
import numpy
from class_framework import clean_col_headers

# loading data files

cba_data = pandas.read_csv('national_cba_report_1970-2015.txt', sep = None).replace(0, numpy.nan)
clean_col_headers(cba_data)
cba_data.set_index('country', inplace=True)

wdi_data = pandas.read_csv('WDIData.csv', sep = None).replace(0, numpy.nan)
clean_col_headers(wdi_data)
wdi_data.set_index('country_name', inplace=True)

country_data = pandas.read_csv('WDI_StatsCountry.csv', sep = None).replace(0, numpy.nan)
clean_col_headers(country_data)
country_data.set_index('country_code', inplace=True)

# making filter indecies (to ensure that countries are the same for cba and wdi, and to assign regions)

country_filter = country_data[(country_data.currency_unit.notnull())][['currency_unit', 'region', 'short_name']]

region_index = country_data[(country_data.short_name == 'East Asia & Pacific') |
            (country_data.short_name == 'Europe & Central Asia') |
            (country_data.short_name == 'Latin America & Caribbean') |
            (country_data.short_name == 'Middle East & North Africa') |
            (country_data.short_name == 'North America') |
            (country_data.short_name == 'South Asia') |
            (country_data.short_name == 'Sub-Saharan Africa')][('short_name')].reset_index().set_index('country_code')

country_index = country_filter.set_index('region').join(
        region_index.reset_index().set_index('short_name'), lsuffix='_country', rsuffix='_region').drop(['currency_unit'], axis = 1
                                ).reset_index().rename(columns = {'country_code':'region_code', 'index':'region'}).set_index('short_name')
country_index.to_csv('country_index.csv', sep = '\t')

# wdi (gdp) and cba by country

cba_country = cba_data[(cba_data.record == "CBA_MtCO2perCap")].drop(['record', 'unnamed:_48'], axis = 1)
cba_country['code'] = 'cba'

wdi_country = country_index.drop('region_code', axis = 1).join(wdi_data, lsuffix='_country', rsuffix = '_wdi')
wdi_country = wdi_country[(wdi_country.indicator_name == "GDP per capita (current US$)")].drop(
        ['indicator_name', 'country_code', 'indicator_code', 'region'], axis = 1).T.reset_index()
wdi_country = wdi_country.rename(index=str, columns={"index": "year"})
wdi_country['year'] = wdi_country['year'].astype(int)
wdi_country = wdi_country[(wdi_country.year >= 1970) & (wdi_country.year <= 2015)]
wdi_country['year'] = wdi_country['year'].astype(str)
wdi_country = wdi_country.set_index('year').T
wdi_country['code'] = 'wdi'

country_filter = cba_country['code'].reset_index().set_index('country').join(
        wdi_country['code'].reset_index().set_index('index'), lsuffix = '_cba', rsuffix = '_wdi')
country_filter = country_filter[(country_filter.code_cba == 'cba') & (country_filter.code_wdi == 'wdi')].drop(['code_cba', 'code_wdi'], axis = 1)
country_filter['country_list'] = country_filter.index.astype(str)

country_data = country_filter.join(cba_country.append(wdi_country)).join(
        country_index.drop(['region'], axis = 1)).sort_index(axis=1, ascending=True).sort_values(by = ['code', 'country_list'], ascending=True)

country_data = country_data[country_data.code == 'wdi'].drop(
        ['code', 'region_code', 'country_list'], axis = 1).add_prefix('wdi_').join(
                country_data[country_data.code == 'cba'].drop(
                        ['code', 'region_code', 'country_list'], axis = 1).add_prefix('cba_'), rsuffix = '_cba', lsuffix = '_wdi').dropna(
                        ).sort_index(axis = 1).T
country_data.to_csv('country_data.csv', sep = '\t')

ext: py

reason: import numpy


text:
"""
Convert frame level (FPS1) features for videos to clip level (FPS2) features, by pooling across multiple frames.

FeaturePerSecond (FPS): FPS1 > FPS2.
"""
import os
import h5py
import numpy as np
from tqdm import tqdm

def convert_for_single_h5(frm_h5, clip_h5, clip_boundaries_in_frm_idx, pool_type="max", debug=False):
    """
    Args:
        frm_h5: h5py.File object, containing the frame level features
        clip_h5: h5py.File object, containing the clip level features
        clip_boundaries_in_frm_idx: list, features belong to clip `clip_idx` should be indexed as
            features[clip_boundaries_in_frm_idx[clip_idx]:clip_boundaries_in_frm_idx[clip_idx+1]]
        pool_type: max or avg
        debug:
    Returns:

    """
    assert pool_type in ["max", "avg"]
    np_pool_func = np.max if pool_type == "max" else np.mean
    for k in tqdm(frm_h5.keys()):
        frm_features = frm_h5[k]
        clip_features = []
        for idx in range(len(clip_boundaries_in_frm_idx)):
            cur_clip_feat = frm_features[clip_boundaries_in_frm_idx[idx]:clip_boundaries_in_frm_idx[idx+1]]
            if len(cur_clip_feat) == 0:
                break
            cur_clip_feat = np_pool_func(cur_clip_feat, axis=0, keepdims=True)
            clip_features.append(cur_clip_feat)
        clip_h5.create_dataset(k, data=np.concatenate(clip_features, axis=0), dtype=np.float32)
        if debug:
            break

def get_clip2frm_idx_mapping(clip_length=1.5, max_video_length=300):
    """ This function depends on how the features are extracted.
    original features are extract from frames (video fps=30):
    [3, 13, 23] frame in a second.
    Args:
        clip_length: float,
        max_video_length: int,

    Returns:
        {clip_idx1 (int): [frm_idx0, frm_idx1, ...],
         ...
        }
    """
    # frame 0 in the feature is actually the frame 3 in the original video, so its
    # corresponding time is 3 / 30 = 0.1s. More generally ==> [0.1, 0.43, 0.77] + n.
    frm2seconds = np.concatenate([
        np.array([3, 13, 23]) / 30. + offset for offset in np.arange(0, max_video_length)], axis=0)

    clip_boundaries = np.arange(0, max_video_length, clip_length)
    # no need to worry about search boundary.
    # indexed as clip_boundaries_in_frm_idx[idx]:clip_boundaries_in_frm_idx[idx+1]
    clip_boundaries_in_frm_idx = np.searchsorted(frm2seconds, clip_boundaries)
    return clip_boundaries_in_frm_idx

def main_convert():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--src_h5_files", type=str, nargs='+', help="frm .h5 file paths")
    parser.add_argument("--tgt_h5_file", type=str, help=".h5 path to stores the converted data")
    parser.add_argument("--pool_type", type=str, default="max",
                        choices=["max", "avg"], help="how to aggreate frame features")
    parser.add_argument("--clip_length", type=float, default=1.5)
    parser.add_argument("--debug", action="store_true")
    args = parser.parse_args()

    clip_boundaries_in_frm_idx = get_clip2frm_idx_mapping(clip_length=args.clip_length)
    assert not os.path.exists(args.tgt_h5_file)
    with h5py.File(args.tgt_h5_file, "a") as tgt_h5:
        for src_f in args.src_h5_files:
            with h5py.File(src_f, "r") as src_h5:
                convert_for_single_h5(src_h5, tgt_h5, clip_boundaries_in_frm_idx,
                                      pool_type=args.pool_type, debug=args.debug)

if __name__ == '__main__':
    main_convert()


text:
from osgeo import gdal
import numpy as np
import rasterio as rio
import matplotlib.pyplot as plt
from rasterio.plot import show
from pynhd import NLDI
from rasterio.mask import mask
from shapely import geometry
import geopandas as gpd
import json

def getFeatures(gdf):
    """Function to parse features from GeoDataFrame in such a manner that rasterio wants them"""
    import json
    return [json.loads(gdf.to_json())['features'][0]['geometry']]

band_of_interest = 1 # DEM
with rio.open('Mads_1_COG.tif') as dataset:
    elev = dataset.read(1)

    basin = NLDI().getfeature_byid("comid", "13294318", basin=True).to_crs('epsg:4269')

    ax = plt.subplot(111)
    show(dataset, ax=ax)
    basin.plot(ax=ax, facecolor="none",
              edgecolor='black', lw=0.7)
    plt.show()

#Subset to bounding box of basin
demdata = rio.open('Mads_1.tif')
show((demdata,1),cmap='terrain')
print(demdata.crs, basin.crs)
# print(basin.boundary.to_json())
basin_coords = getFeatures(basin)
minx,miny,maxx,maxy = basin.total_bounds
p1 = geometry.Point(minx,miny)
p2 = geometry.Point(minx,maxy)
p3 = geometry.Point(maxx, maxy)
p4 = geometry.Point(maxx,miny)

pointlist = [p1,p2,p3,p4,p1]
clipBnd = geometry.Polygon(pointlist)
clip = gpd.GeoSeries(clipBnd)

out_img, out_transform = mask(dataset=demdata, shapes=clip, crop=True)
out_meta = demdata.meta
out_meta.update({"driver": "GTiff",
                 "height": out_img.shape[1],
                 "width": out_img.shape[2],
                 "transform": out_transform})
out_raster = rio.open('Mads_clip_1.tif', "w", **out_meta)
out_raster.write(out_img)

out_raster.close()
demdata_clip = rio.open('Mads_clip_1.tif')
with rio.open('Mads_clip_1.tif') as dataset1:
    elev1 = dataset1.read(1)
show(demdata_clip,cmap='terrain')

tmp = 0

max_stars_repo_name: rmcd-mscb/COGTest

max_issues_repo_name: rmcd-mscb/COGTest

max_forks_repo_name: rmcd-mscb/COGTest


text:
import numpy as np
from typing import List, Union

from autoarray.plot.abstract_plotters import Plotter
from autoarray.plot.mat_wrap.visuals import Visuals1D
from autoarray.plot.mat_wrap.visuals import Visuals2D
from autoarray.plot.mat_wrap.include import Include1D
from autoarray.plot.mat_wrap.include import Include2D
from autoarray.plot.mat_wrap.mat_plot import MatPlot1D
from autoarray.plot.mat_wrap.mat_plot import MatPlot2D
from autoarray.plot.mat_wrap.mat_plot import AutoLabels
from autoarray.structures.arrays.one_d.array_1d import Array1D
from autoarray.structures.arrays.two_d.array_2d import Array2D
from autoarray.structures.grids.two_d.grid_2d import Grid2D

class Array2DPlotter(Plotter):
    def __init__(
        self,
        array: Array2D,
        mat_plot_2d: MatPlot2D = MatPlot2D(),
        visuals_2d: Visuals2D = Visuals2D(),
        include_2d: Include2D = Include2D(),
    ):
        """
        Plots `Array2D` objects using the matplotlib method `imshow()` and many other matplotlib functions which
        customize the plot's appearance.

        The `mat_plot_2d` attribute wraps matplotlib function calls to make the figure. By default, the settings
        passed to every matplotlib function called are those specified in the `config/visualize/mat_wrap/*.ini` files,
        but a user can manually input values into `MatPlot2d` to customize the figure's appearance.

        Overlaid on the figure are visuals, contained in the `Visuals2D` object. Attributes may be extracted from
        the `Array2D` and plotted via the visuals object, if the corresponding entry is `True` in the `Include2D`
        object or the `config/visualize/include.ini` file.

        Parameters
        ----------
        array
            The 2D array the plotter plot.
        mat_plot_2d
            Contains objects which wrap the matplotlib function calls that make 2D plots.
        visuals_2d
            Contains 2D visuals that can be overlaid on 2D plots.
        include_2d
            Specifies which attributes of the `Array2D` are extracted and plotted as visuals for 2D plots.
        """
        super().__init__(
            visuals_2d=visuals_2d, include_2d=include_2d, mat_plot_2d=mat_plot_2d
        )

        self.array = array

    def get_visuals_2d(self) -> Visuals2D:
        return self.get_2d.via_mask_from(mask=self.array.mask)

    def figure_2d(self):
        """
        Plots the plotter's `Array2D` object in 2D.
        """
        self.mat_plot_2d.plot_array(
            array=self.array,
            visuals_2d=self.get_visuals_2d(),
            auto_labels=AutoLabels(title="Array2D", filename="array"),
        )

class Grid2DPlotter(Plotter):
    def __init__(
        self,
        grid: Grid2D,
        mat_plot_2d: MatPlot2D = MatPlot2D(),
        visuals_2d: Visuals2D = Visuals2D(),
        include_2d: Include2D = Include2D(),
    ):
        """
        Plots `Grid2D` objects using the matplotlib method `scatter()` and many other matplotlib functions which
        customize the plot's appearance.

        The `mat_plot_2d` attribute wraps matplotlib function calls to make the figure. By default, the settings
        passed to every matplotlib function called are those specified in the `config/visualize/mat_wrap/*.ini` files,
        but a user can manually input values into `MatPlot2d` to customize the figure's appearance.

        Overlaid on the figure are visuals, contained in the `Visuals2D` object. Attributes may be extracted from
        the `Grid2D` and plotted via the visuals object, if the corresponding entry is `True` in the `Include2D`
        object or the `config/visualize/include.ini` file.

        Parameters
        ----------
        grid
            The 2D grid the plotter plot.
        mat_plot_2d
            Contains objects which wrap the matplotlib function calls that make 2D plots.
        visuals_2d
            Contains 2D visuals that can be overlaid on 2D plots.
        include_2d
            Specifies which attributes of the `Grid2D` are extracted and plotted as visuals for 2D plots.
        """
        super().__init__(
            visuals_2d=visuals_2d, include_2d=include_2d, mat_plot_2d=mat_plot_2d
        )

        self.grid = grid

    def get_visuals_2d(self) -> Visuals2D:
        return self.get_2d.via_grid_from(grid=self.grid)

    def figure_2d(self, color_array: np.ndarray = None):
        """
        Plots the plotter's `Grid2D` object in 2D.

        Parameters
        ----------
        color_array
            An array of RGB color values which can be used to give the plotted 2D grid a colorscale (w/ colorbar).
        """
        self.mat_plot_2d.plot_grid(
            grid=self.grid,
            visuals_2d=self.get_visuals_2d(),
            auto_labels=AutoLabels(title="Grid2D", filename="grid"),
            color_array=color_array,
        )

class YX1DPlotter(Plotter):
    def __init__(
        self,
        y: Union[np.ndarray, List, Array1D],
        x: Union[np.ndarray, List, Array1D],
        mat_plot_1d: MatPlot1D = MatPlot1D(),
        visuals_1d: Visuals1D = Visuals1D(),
        include_1d: Include1D = Include1D(),
    ):
        """
        Plots two 1D objects using the matplotlib method `plot()` (or a similar method) and many other matplotlib
        functions which customize the plot's appearance.

        The `mat_plot_1d` attribute wraps matplotlib function calls to make the figure. By default, the settings
        passed to every matplotlib function called are those specified in the `config/visualize/mat_wrap/*.ini` files,
        but a user can manually input values into `MatPlot1d` to customize the figure's appearance.

        Overlaid on the figure are visuals, contained in the `Visuals1D` object. Attributes may be extracted from
        the `Array1D` and plotted via the visuals object, if the corresponding entry is `True` in the `Include1D`
        object or the `config/visualize/include.ini` file.

        Parameters
        ----------
        y
            The 1D y values the plotter plot.
        x
            The 1D x values the plotter plot.
        mat_plot_1d
            Contains objects which wrap the matplotlib function calls that make 1D plots.
        visuals_1d
            Contains 1D visuals that can be overlaid on 1D plots.
        include_1d
            Specifies which attributes of the `Array1D` are extracted and plotted as visuals for 1D plots.
        """
        super().__init__(
            visuals_1d=visuals_1d, include_1d=include_1d, mat_plot_1d=mat_plot_1d
        )

        self.y = y
        self.x = x

    def get_visuals_1d(self) -> Visuals1D:
        return self.get_1d.via_array_1d_from(array_1d=self.x)

    def figure_1d(self):
        """
        Plots the plotter's y and x values in 1D.
        """
        self.mat_plot_1d.plot_yx(
            y=self.y,
            x=self.x,
            visuals_1d=self.get_visuals_1d(),
            auto_labels=AutoLabels(),
        )

max_stars_repo_name: caoxiaoyue/PyAutoArray

max_issues_repo_name: caoxiaoyue/PyAutoArray

max_forks_repo_name: caoxiaoyue/PyAutoArray


text:
import json
import numpy as np
import pandas as pd
from cyberpy import address_to_address
from telebot.apihelper import ApiTelegramException

from src.bash_utils import transfer_tokens, create_account
from config import BASE_AFTER_SIGN_UP_KEYBOARD, db_worker, bot, TOKEN_NAME, CYBERPAGE_URL, CYBERPAGE_BASE_URL, \
    CYBER_CHAIN_ID

TRANSFER_VALUE_NEW_USERS = 10_000_000
TRANSFER_VALUE_LEADERS = 100_000_000
MILLIAMPERE_TRANSFER_VALUE = 1_000
MILLIVOLT_TRANSFER_VALUE = 1_000
MILLIAMPERE_TOKEN_NAME = 'MILLIAMPERE'
MILLIVOLT_TOKEN_NAME = 'MILLIVOLT'
TOTAL_GOL_GIFT = 1_178_234_463
LOAD_NEW_DATA = True
CREATE_NEW_ADDRESS_COLUMN = False
CREATE_NEW_ADDRESSES = False
NEW_USER_MESSAGE = f'If you create 10 links with the bot, another 90 M{TOKEN_NAME} will be transferred.'

def message_genesis(cyber_address: str, bostrom_address: str, genesis_balance: str) -> str:
    return f'''
Also your bostrom address {bostrom_address} was included in the {CYBER_CHAIN_ID} Genesis.
Your genesis balance is {genesis_balance}.
{CYBERPAGE_URL}/contract/{bostrom_address}/wallet
@cyberdBot sent the mnemonic phrase for the cyber address {cyber_address} during sign up.new_bostrom_address
You can use this mnemonic phrase to access the bostrom address by any wallet (e.g. Keplr).'''

def message_transfer(transfer_value: float, address: str = '', links_amount: int = 0, token_for_links_amount: float = 0,
                     users_message: str = '', token_name: str = TOKEN_NAME) -> str:
    if token_name == TOKEN_NAME and links_amount >= 1:
        return f'''
@cyberdBot received 1,178 M{TOKEN_NAME} from Game of Link and distributes this prize between accounts in proportion to the number of created cyberLinks.
You created {int(links_amount):>,} cyberLinks.
{round(transfer_value / 1e6, 1)} M{TOKEN_NAME} has been transferred to your address {address}, including {token_for_links_amount} M{TOKEN_NAME} reward for cyberLink creation.
Remember, these tokens shall not be migrated to the production network.
Let's delegate, investmint to Volt and Ampere by cyb.ai/mint and Keplr wallet.
{users_message}'''
    elif token_name == TOKEN_NAME:
        return f'''
@cyberdBot transferred {int(transfer_value // 1e6)} M{token_name} to your address {address}.
Remember, these tokens shall not be migrated to the production network.
Let's delegate, investmint to Volt and Ampere by cyb.ai/mint.
{NEW_USER_MESSAGE}
Go for it!'''
    elif token_name == MILLIAMPERE_TOKEN_NAME:
        return f'''
Also @cyberdBot transferred {int(transfer_value // 1e3)} {token_name[5:]} to you.
These tokens shall not be migrated to the production network too.
AMPERE token will give weight to your cyberLinks.'''
    elif token_name == MILLIVOLT_TOKEN_NAME:
        return f'''
@cyberdBot transferred {int(transfer_value // 1e3)} {token_name[5:]} to you.
These tokens shall not be migrated to the production network.
Your bandwidth can be enough to generate at least 1 link per day.
Let's tweet and create cyberLinks by @cyberdBot or cyb.ai.
Go for it!'''

def sign_up_user(user_id: int, account_name: str):
    account_data, create_account_error = create_account(account_name)
    if account_data:
        print(f'Account created {account_name} {user_id} {account_data["address"]}')
        try:
            bot.send_message(
                user_id,
                f'@cyberdBot created new account for you:\n'
                f'Account: {account_data["name"]}\n'
                f'Address: {account_data["address"]}\n'
                f'Link: {CYBERPAGE_URL}/contract/{account_data["address"]}\n\n'
                f'Mnemonic phrase: {account_data["mnemonic_phrase"]}\n'
                f'**Important**Please write down your mnemonic phrase and keep it safe. '
                f'The mnemonic is the only way to recover your account. '
                f'There is no way of recovering any funds if you lose it.\n'
                f'Storing tokens on this account is not secure and is intended for experimentation.',
                parse_mode="HTML",
                reply_markup=BASE_AFTER_SIGN_UP_KEYBOARD)
            return account_data["address"]
        except ApiTelegramException:
            print('Chat not found')
    else:
        print(f'Account not created, user id {user_id}. Error: {create_account_error}')
    return

def send_message_genesis(row: pd.Series) -> None:
    if len(row.genesis) > 0:
        print(f'\nSend genesis message to {row.user_id} with {row.genesis_str} genesis bostrom address {row.bostrom_address}')
        try:
            bot.send_message(
                chat_id=row.user_id,
                text=message_genesis(row.address, row.bostrom_address, row.genesis_str),
                parse_mode='HTML',
                reply_markup=BASE_AFTER_SIGN_UP_KEYBOARD)
        except ApiTelegramException:
            print('Chat not found')

def send_message_transfer(row: pd.Series, transfer_value: int, gift_per_link: float,
                          new_bostrom_address: str = '', token_name: str = TOKEN_NAME) -> None:
    user_message = '' if row.number_of_cyberlinks >= 10 else NEW_USER_MESSAGE
    if token_name in (MILLIAMPERE_TOKEN_NAME, MILLIVOLT_TOKEN_NAME):
        message_text = \
             message_transfer(transfer_value=transfer_value, token_name=token_name)
    elif row.number_of_cyberlinks > 0:
        message_text = \
            message_transfer(
                transfer_value=transfer_value,
                links_amount=row.number_of_cyberlinks,
                address=row.address,
                token_for_links_amount=round(row.number_of_cyberlinks * gift_per_link / 1e6, 1),
                users_message=user_message)
    else:
        message_text = \
            message_transfer(transfer_value=transfer_value, address=row.address)
    print(f'\nSend transfer message to user_id {row.user_id}, link number {row.number_of_cyberlinks}, cyber address {row.address}, '
          f'transfer value {transfer_value:>,}{token_name.lower()} {"new bostrom address " + new_bostrom_address if new_bostrom_address else ""}')
    try:
        bot.send_message(
            chat_id=row.user_id,
            text=message_text,
            parse_mode='HTML',
            reply_markup=BASE_AFTER_SIGN_UP_KEYBOARD)
    except ApiTelegramException:
        print('Chat not found')

def get_users_and_links() -> pd.DataFrame:
    return db_worker.get_df(
        query='''
            SELECT
                acc.user_id,
                acc.account_name,
                links.cyberlink_count,
                acc.account_address
            FROM (
                SELECT
                    user_id,
                    account_name,
                    account_address
                FROM accounts
                WHERE account_address != ''
                ) as acc
            LEFT JOIN (
                SELECT
                    user_id,
                    count() as cyberlink_count
                FROM (
                    SELECT distinct
                        user_id,
                        from_ipfs_hash,
                        to_ipfs_hash
                    FROM cyberlinks
                    WHERE ts < '2021-06-12 00:00:00')
                GROUP BY user_id
                ORDER BY cyberlink_count DESC
                ) as links
            ON links.user_id = acc.user_id
            ORDER BY links.cyberlink_count DESC''',
        columns=['user_id', 'account_name', 'number_of_cyberlinks', 'address']).fillna(0)

def compute_users_and_links(load_new_data: bool = LOAD_NEW_DATA) -> pd.DataFrame:
    if load_new_data:
        _users_and_links_df = get_users_and_links()
        with open("data/genesis_bostrom_testnet_5.json") as _jsonFile:
            _genesis_json = json.load(_jsonFile)
            _jsonFile.close()

        _genesis_balances_dict = {item['address']: [f"{int(coin['amount']):>,} {coin['denom']}"
                                                    for coin in item['coins']]
                                  for item in _genesis_json['app_state']['bank']['balances']}
        _users_and_links_df['bostrom_address'] = _users_and_links_df['address'].map(
            lambda x: address_to_address(x, 'bostrom'))
        _users_and_links_df['genesis'] = _users_and_links_df.bostrom_address.map(
            lambda x: _genesis_balances_dict[x] if x in _genesis_balances_dict.keys() else [])
        _users_and_links_df['genesis_str'] = _users_and_links_df.genesis.map(lambda x: ', '.join(x))
        _users_and_links_df.to_csv('users_and_links.csv')
    return pd.read_csv(
                'users_and_links.csv',
                index_col=0,
                dtype={'user_id': np.int64, 'account_name': str, 'number_of_cyberlinks': 'Int64'},
                converters={"genesis": lambda x: x.strip("[]").split(", ") if x != '[]' else []})

def transfer_tokens_handler(row: pd.Series, transfer_value: int,
                            gift_per_link: float = 0, token_name: str = TOKEN_NAME) -> bool:
    _transfer_success, _transfer_error = \
        transfer_tokens(
            account_address=row.address,
            value=transfer_value,
            token_name=token_name)
    if _transfer_success:
        send_message_transfer(
            row=row,
            transfer_value=transfer_value,
            gift_per_link=gift_per_link,
            token_name=token_name)
        return True
    else:
        print(f'\nUnsuccessful transfer {transfer_value} {token_name} to {row.address}, user id {row.user_id}')
        return False

def run():
    if CREATE_NEW_ADDRESS_COLUMN:
        db_worker.rename_column(new_column_name='account_address_euler')
        db_worker.add_column()

    users_and_links_df = compute_users_and_links()
    total_cyberlinks = int(sum(users_and_links_df.number_of_cyberlinks))
    gift_per_link = round(TOTAL_GOL_GIFT/sum(users_and_links_df.number_of_cyberlinks))
    print(f'Total cyberLinks created by cyberdBot {total_cyberlinks:>,}\n'
          f'Gift per created cyberLink {gift_per_link:>,} {TOKEN_NAME}')

    for df_index in users_and_links_df.index:

        row = users_and_links_df.iloc[df_index].copy()
        token_amount = TRANSFER_VALUE_LEADERS if row.number_of_cyberlinks >= 10 else TRANSFER_VALUE_NEW_USERS
        transfer_value = token_amount + row.number_of_cyberlinks * gift_per_link

        if CREATE_NEW_ADDRESSES:
            row.address = sign_up_user(user_id=row.user_id, account_name=row.account_name)
            if row.address is None:
                continue
            db_worker.update_account_address(user_id=row.user_id, new_address=row.address)

        transfer_tokens_handler(row=row, transfer_value=transfer_value, gift_per_link=gift_per_link)
        transfer_tokens_handler(row=row, transfer_value=MILLIAMPERE_TRANSFER_VALUE, token_name=MILLIAMPERE_TOKEN_NAME)
        transfer_tokens_handler(row=row, transfer_value=MILLIVOLT_TRANSFER_VALUE, token_name=MILLIVOLT_TOKEN_NAME)

        # send_message_genesis(row)

    print('Transferred Successfully!')

if __name__ == '__main__':
    run()

max_stars_repo_name: Snedashkovsky/cyberdBot

max_issues_repo_name: Snedashkovsky/cyberdBot

max_forks_repo_name: Snedashkovsky/cyberdBot


text:
"""
Tracks posts and add displacements to database
"""
import logging

import concurrent.futures
import cv2
import pandas as pd
import numpy as np
from scipy.spatial import distance
from crud import crud_tissue_tracking, crud_video

log = logging.getLogger("tracking_logger")

class TissueTracker:
    """Tissue class for pointfinding"""

    def __init__(self, database, un_points, calib_factor, video_object):
        video_file_path = video_object.save_location
        tissue_object_list = video_object.tissues

        self.tissue_ids = [tissue_object.id for tissue_object in tissue_object_list]
        self.calib_factor = calib_factor
        self.database = database
        self.times = []

        videostream = cv2.VideoCapture(video_file_path)
        self.total_frames = int(videostream.get(cv2.CAP_PROP_FRAME_COUNT))
        first_image = videostream.read()[1]

        boxes = [
            format_box(un_points[i - 1], un_points[i])
            for i in range(len(un_points))
            if i % 2 == 1
        ]

        tracks = list(
            map(lambda box: trackers_init(box, video_file_path, first_image), boxes)
        )
        lists = np.array(tracks).T.tolist()

        log.info("Start Tracking")
        with concurrent.futures.ThreadPoolExecutor() as executor:
            post_locs = list(executor.map(self.threaded_centroid, *lists))
            executor.shutdown(wait=True)

        for index, post_info in enumerate(post_locs):
            if index % 2 == 1:
                rel_index = int((index - 1) / 2)
                self.add_to_database(post_info, post_locs[index - 1], rel_index)
        crud_video.update_tracked_status(database, video_object.id, True)
        log.info("Done Tracking")

    def add_to_database(self, post_locs_one, post_locs_two, rel_tiss_id):
        """Adds information to database"""
        disps = list(map(distance.euclidean, post_locs_one, post_locs_two))

        transpose_one = np.array(post_locs_one).T.tolist()
        transpose_two = np.array(post_locs_two).T.tolist()

        odd_x = transpose_one[0]
        odd_y = transpose_one[1]
        even_x = transpose_two[0]
        even_y = transpose_two[1]

        id_repeated = [self.tissue_ids[rel_tiss_id]] * len(disps)

        zipped = zip(self.times[0], disps, id_repeated, odd_x, odd_y, even_x, even_y)
        dataframe = pd.DataFrame(
            list(zipped),
            columns=[
                "time",
                "displacement",
                "tissue_id",
                "odd_x",
                "odd_y",
                "even_x",
                "even_y",
            ],
        )

        crud_tissue_tracking.create_tissue_tracking(
            self.database, self.tissue_ids[rel_tiss_id], dataframe
        )

    def threaded_centroid(self, tracker, videostream):
        """Tracks a single post through all frames"""
        frame = 0
        centroid_list = []
        timer = []
        while True:
            successful, image = videostream.read()
            if not successful:
                break
            successful, post = tracker.update(image)
            frame += 1
            print(frame / self.total_frames)

            (x_dist, y_dist, width, height) = post
            centroid = (float(x_dist + width / 2), float(y_dist + height / 2))

            if (time := videostream.get(cv2.CAP_PROP_POS_MSEC)) != 0:
                centroid_list.append(self.calibrate(centroid))
                timer.append(time / 1000)

        self.times.append(timer)
        return centroid_list

    def calibrate(self, centroids):
        """Converts the locations from pixels to mms"""
        return (centroids[0] / self.calib_factor, centroids[1] / self.calib_factor)

def trackers_init(box, vid_path, image):
    """Initialize a single tracker"""
    tracker = cv2.TrackerCSRT_create()
    tracker.init(image, box)
    return tracker, cv2.VideoCapture(vid_path)

def format_box(first_point, second_point):
    """Format boxes for use with openCV"""
    x_dist = first_point[0] * 2
    y_dist = first_point[1] * 2
    width = second_point[0] * 2 - x_dist
    height = second_point[1] * 2 - y_dist
    return int(x_dist), int(y_dist), int(width), int(height)

max_stars_repo_name: Jack-alope/tempers

max_issues_repo_name: Jack-alope/tempers

max_forks_repo_name: Jack-alope/tempers

reason: import numpy,from scipy


text:
import numpy as np
import tensorflow as tf

from tfsnippet.distributions import Distribution, reduce_group_ndims

class DistributionTestCase(tf.test.TestCase):

    def test_basic(self):
        class _Distribution(Distribution):
            def log_prob(self, given, group_ndims=0, name=None):
                return reduce_group_ndims(
                    tf.reduce_sum,
                    tf.convert_to_tensor(given) - 1.,
                    group_ndims
                )

        with self.test_session() as sess:
            distrib = _Distribution(
                dtype=tf.float32,
                is_reparameterized=True,
                is_continuous=True,
                batch_shape=tf.constant([]),
                batch_static_shape=tf.TensorShape([]),
                value_ndims=0,
            )
            self.assertIs(distrib.base_distribution, distrib)
            x = np.asarray([0., 1., 2.])
            np.testing.assert_allclose(
                sess.run(distrib.prob(x, group_ndims=0)),
                np.exp(x - 1.)
            )
            np.testing.assert_allclose(
                sess.run(distrib.prob(x, group_ndims=1)),
                np.exp(np.sum(x - 1., -1))
            )


text:
import numpy as np

from jdaviz.core.helpers import ConfigHelper
from jdaviz.core.events import SnackbarMessage
from astropy.table import QTable
import astropy.units as u

class Specviz2d(ConfigHelper):
    """SpecViz2D Helper class"""

    _default_configuration = "specviz2d"

    def __init__(self):
        super().__init__()

        spec1d = self.app.get_viewer("spectrum-viewer")
        spec1d.scales['x'].observe(self._update_spec2d_x_axis)

        spec2d = self.app.get_viewer("spectrum-2d-viewer")
        spec2d.scales['x'].observe(self._update_spec1d_x_axis)

    def _extend_world(self, spec1d, ext):
        # Extend 1D spectrum world axis to enable panning (within reason) past
        # the bounds of data
        world = self.app.data_collection[spec1d]["World 0"].copy()
        dw = world[1]-world[0]
        prepend = np.linspace(world[0]-dw*ext, world[0]-dw, ext)
        dw = world[-1]-world[-2]
        append = np.linspace(world[-1]+dw, world[-1]+dw*ext, ext)
        world = np.hstack((prepend, world, append))
        return world

    def _update_spec2d_x_axis(self, change):
        # This assumes the two spectrum viewers have the same x-axis shape and
        # wavelength solution, which should always hold
        if change['old'] is None:
            pass
        else:
            name = change['name']
            if name not in ['min', 'max']:
                return
            new_val = change['new']
            spec1d = self.app.get_viewer('spectrum-viewer').state.reference_data.label
            extend_by = int(self.app.data_collection[spec1d]["World 0"].shape[0])
            world = self._extend_world(spec1d, extend_by)

            # Warn the user if they've panned far enough away from the data
            # that the viewers will desync
            if new_val > world[-1] or new_val < world[0]:
                msg = "Warning: panning too far away from the data may desync\
                      the 1D and 2D spectrum viewers"
                msg = SnackbarMessage(msg, color='warning', sender=self)
                self.app.hub.broadcast(msg)

            idx = float((np.abs(world - new_val)).argmin()) - extend_by
            scales = self.app.get_viewer('spectrum-2d-viewer').scales
            old_idx = getattr(scales['x'], name)
            if idx != old_idx:
                setattr(scales['x'], name, idx)

    def _update_spec1d_x_axis(self, change):
        # This assumes the two spectrum viewers have the same x-axis shape and
        # wavelength solution, which should always hold
        if change['old'] is None:
            pass
        else:
            name = change['name']
            if name not in ['min', 'max']:
                return
            new_idx = int(np.around(change['new']))
            spec1d = self.app.get_viewer('spectrum-viewer').state.reference_data.label
            extend_by = int(self.app.data_collection[spec1d]["World 0"].shape[0])
            world = self._extend_world(spec1d, extend_by)

            scales = self.app.get_viewer('spectrum-viewer').scales
            old_val = getattr(scales['x'], name)

            # Warn the user if they've panned far enough away from the data
            # that the viewers will desync
            try:
                val = world[new_idx+extend_by]
            except IndexError:
                val=old_val
                msg = "Warning: panning too far away from the data may desync \
                       the 1D and 2D spectrum viewers"
                msg = SnackbarMessage(msg, color='warning', sender=self)
                self.app.hub.broadcast(msg)
            if val != old_val:
                setattr(scales['x'], name, val)

    def load_data(self, spectrum_2d=None, spectrum_1d=None, spectrum_1d_label=None,
                  spectrum_2d_label=None, show_in_viewer=True):
        """
        Load and parse a pair of corresponding 1D and 2D spectra

        Parameters
        ----------
        spectrum_1d: str or Spectrum1D
            A spectrum as translatable container objects (e.g.
            ``Spectrum1D``) that can be read by glue-jupyter. Alternatively,
            can be a string file path.

        spectrum_2d: str
            A spectrum as translatable container objects (e.g.
            ``Spectrum1D``) that can be read by glue-jupyter. Alternatively,
            can be a string file path.

        spectrum_1d_label : str
            String representing the label for the data item loaded via
            ``onedspectra``. Can be a list of strings representing data labels
            for each item in ``data_obj`` if  ``data_obj`` is a list.

        spectrum_2d_label : str
            String representing the label for the data item loaded via
            ``twodspectra``. Can be a list of strings representing data labels
            for each item in ``data_obj`` if  ``data_obj`` is a list.

        """
        if spectrum_2d_label is None:
            spectrum_2d_label = "Spectrum 2D"
        elif spectrum_2d_label[-2:] != "2D":
            spectrum_2d_label = "{} 2D".format(spectrum_2d_label)

        if spectrum_1d_label is None:
            spectrum_1d_label = spectrum_2d_label.replace("2D", "1D")

        if spectrum_2d is not None:
            self.app.load_data(spectrum_2d, parser_reference="mosviz-spec2d-parser",
                               data_labels=spectrum_2d_label,
                               show_in_viewer=show_in_viewer,
                               add_to_table=False)
            # Collapse the 2D spectrum to 1D if no 1D spectrum provided
            if spectrum_1d is None:
                self.app.load_data(spectrum_2d,
                                  parser_reference="spec2d-1d-parser",
                                  data_label=spectrum_1d_label,
                                  show_in_viewer=show_in_viewer)

                # Warn that this shouldn't be used for science
                msg = "Warning: The collapsed 1D spectrum is for quicklook" +\
                    " purposes only. A robust extraction should be used for" +\
                    " scientific use cases."
                msg = SnackbarMessage(msg, color='warning', sender=self,
                                      timeout = 10000)
                self.app.hub.broadcast(msg)

                return

        self.app.load_data(spectrum_1d, data_label = spectrum_1d_label,
                           parser_reference="specviz-spectrum1d-parser",
                           show_in_viewer=show_in_viewer)

max_stars_repo_name: javerbukh/jdaviz

max_issues_repo_name: javerbukh/jdaviz

max_forks_repo_name: javerbukh/jdaviz

reason: import numpy,import astropy,from astropy


text:
#!/usr/bin/env python

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                   Simulation with reactive streams                    #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

"""Transition graph representation of mixing problem
onenote:https://d.docs.live.net/8e55450f976ac566/Notes/AI/Статьи.one#SWRS%20v0.2.3%20Graph%20modeling&
    section-id={40964F6B-F1E6-40E5-93D6-D7D464B4D57F}&page-id={BB7332E8-6019-4F25-8EB8-970FBE79BC2F}&
    object-id={AB21A1CE-24D1-09BB-1213-C2C15F1AAB66}&22
Created 13.06.2018 author CAB
"""

import numpy as np
import matplotlib.pyplot as plt
import math as m
from tools.graph_visualisation import NodeLike, GraphLike, GraphVisualisation

# Script init
print("""
#### Transition graph representation of mixing problem ####
X = [t]
Y = [ω_1, ω_2̂]
G = [v_1, v_2, q_1, q_2, q_3, q_4, ω_3]
""")

# Value definitions
class b𝔛_:
    def __init__(self, t):
        self.t = t
    def __repr__(self):
        return f"𝔛 = [t={self.t}]"
class h𝔜_:
    def __init__(self, ω_1, ω_2):
        self.ω_1 = ω_1
        self.ω_2 = ω_2
    def __repr__(self):
        return f"𝔜 = [ω_1={self.ω_1}, ω_2={self.ω_2}]"
class 𝔈_:
    def __init__(self, v_1, v_2, q_1, q_2, q_3, q_4, ω_3):
        self.v_1 = v_1
        self.v_2 = v_2
        self.q_1 = q_1
        self.q_2 = q_2
        self.q_3 = q_3
        self.q_4 = q_4
        self.ω_3 = ω_3
    def __repr__(self):
        return f"𝔈 = [v_1={self.v_1}, v_2={self.v_2}, q_1={self.q_1}, q_2={self.q_2}, " \
             + f"q_3={self.q_3}, q_4={self.q_4}, ω_3={self.ω_3}]"
class 𝔓_h_:
    def __init__(self, 𝔈, h):
        if h == 1:
            self.v_1 = 𝔈.v_1
            self.q_1 = 𝔈.q_1
            self.q_2 = 𝔈.q_2
            self.q_3 = 𝔈.q_3
            self.ω_3 = 𝔈.ω_3
        elif h == 2:
            self.v_2 = 𝔈.v_2
            self.q_2 = 𝔈.q_2
            self.q_3 = 𝔈.q_3
            self.q_4 = 𝔈.q_4
        else:
            assert False, "index 'h' can be 1 or 2"
        self.h = h
    def __repr__(self):
        if self.h == 1:
            return f"𝔓_h = [v_1={self.v_1}, q_1={self.q_1}, q_2={self.q_2}, q_3={self.q_3}, ω_3={self.ω_3}]_h=1"
        else:
            return f"𝔓_h = [v_2={self.v_2}, q_2={self.q_2}, q_3={self.q_3}, q_4={self.q_4}]_h=2"
class 𝔖𝔛_q_: # Sub-state, 𝔖 ⊆ 𝔜 is sub-stat value, 𝔛 ∈ 𝕏 is key values set, q ∈ ℕ is sub-sate index
    def __init__(self, t, ω, q):
        self.t = t
        if q == 1:
            self.ω_1 = ω
        elif q == 2:
            self.ω_2 = ω
        else:
            assert False, "index q can be only  1 or 2"
        self.q = q
    def __repr__(self):
        if self.q == 1:
            return f"𝔖^𝔛_q = 𝔖=[ω_1={self.ω_1}]^𝔛=[t={self.t}]_q=1"
        else:
            return f"𝔖^𝔛_q = 𝔖=[ω_2={self.ω_2}]^𝔛=[t={self.t}]_q=2"

# S node definitions
class S_(NodeLike): # Represent S (variable) node that holds sub-state 𝔖𝔛q, d, w is index of S_node in the Γ graph
    def __init__(self, Θ𝔓, d, w):
        self.Θ𝔓 = Θ𝔓
        self.d = d
        self.w = w
        self.S = None
        if Θ𝔓 is not None:
            Θ𝔓.set_S(self)
    def is_defined(self):
        return self.S is not None
    def assign(self, 𝔖𝔛q):
        assert 𝔖𝔛q is not None
        assert 𝔖𝔛q.q == self.w
        self.S = 𝔖𝔛q
    def get(self):
        return self.S
    def graph_repr(self):
        label = f"S_{self.d},{self.w}"
        pos = (self.d, self.w)
        color = "k" if self.is_defined() else "m"
        edges = [] if self.Θ𝔓 is None else self.Θ𝔓.graph_repr_edges()
        return label, pos, color, edges
    def __repr__(self):
        if self.S is None:
            return f"S_d,w = (∅)_d={self.d},w={self.w}"
        else:
            return f"S_d,w = ({self.S})_d={self.d},w={self.w}"
class Θ𝔓_: # Represent set of S node income edges (transaction function)
    def __init__(self, S_1, S_2, 𝔓_h, f_t, f_ω):
        assert S_1.w == 1
        assert S_2.w == 2
        self.S_1 = S_1
        self.S_2 = S_2
        self.𝔓_h  = 𝔓_h
        self.f_t = f_t
        self.f_ω = f_ω
        self.gS = None
    def set_S(self, gS):
        assert self.S_1.d == (gS.d - 1)
        assert self.S_2.d == (gS.d - 1)
        assert gS.d == gS.d
        assert gS.w == gS.w
        assert self.𝔓_h.h == gS.w
        self.gS = gS
    def eval(self):
        assert self.gS is not None
        if self.S_1.is_defined() and self.S_2.is_defined() and not self.gS.is_defined():
            assert self.S_1.get().t == self.S_2.get().t
            t = self.f_t(self.S_1.get().t)
            ω = self.f_ω(self.S_1.get().ω_1, self.S_2.get().ω_2, self.S_1.get().t, t, self.𝔓_h)
            self.gS.assign(𝔖𝔛_q_(t, ω, q=self.gS.w))
            return True
        else:
            return False
    def graph_repr_edges(self):
        return [(self.S_1, self.gS), (self.S_2, self.gS)]
    def __repr__(self):
        return f"{self.S_1}, {self.S_2} -- Θ^|𝔓 --> {self.gS}"

# Graph definitions
class Γ𝔈_graph(GraphLike):
    def __init__(self, setS, setΘ_𝔓):
        for S in setS:
            assert not S.is_defined()
        self.setS = setS
        self.setΘ_𝔓 = setΘ_𝔓
    def γ(self, p𝔖):
        for (d, w), 𝔖𝔛_q in p𝔖.items():
            for S in self.setS:
                if S.d == d and S.w == w:
                    S.assign(𝔖𝔛_q)
        while False in [S.is_defined() for S in self.setS]:
            for Θ_𝔓 in self.setΘ_𝔓:
                if Θ_𝔓.eval():
                    self.redraw()
        return γ𝔈_graph(self.setS, self.setΘ_𝔓)
    def graph_repr(self):
        return self.setS
    def __repr__(self):
        rs = "Γ^|𝔈: \n"
        for Θ_𝔓 in self.setΘ_𝔓:
            rs = rs + f"    {str(Θ_𝔓)}\n"
        return rs
class γ𝔈_graph:
    def __init__(self, setS, setΘ_𝔓):
        for S in setS:
            assert S.is_defined()
        self.setS = setS
        self.setΘ_𝔓 = setΘ_𝔓
    def 𝔖(self):
        set𝔖X𝔈 = []
        for S in self.setS:
            set𝔖X𝔈.append(S.get())
        return set𝔖X𝔈
    def __repr__(self):
        rs = "γ^|𝔈: \n"
        for Θ_𝔓 in self.setΘ_𝔓:
            rs = rs + f"    {str(Θ_𝔓)}\n"
        return rs

# Γ^|𝔈 builder
def build_Γ𝔈(n, Δt, 𝔈, X_transition, S_transition):
    _𝔓_1 = 𝔓_h_(𝔈, h=1)
    _𝔓_2 = 𝔓_h_(𝔈, h=2)
    f_t = X_transition(Δt)
    f_ω_1, f_ω_2 = S_transition()
    S_1 = S_(Θ𝔓=None, d=0, w=1)
    S_2 = S_(Θ𝔓=None, d=0, w=2)
    setS = [S_1, S_2]
    setΘ_𝔓 = []
    for i in range(1, n + 1):
        Θ_𝔓_1 = Θ𝔓_(S_1, S_2, _𝔓_1, f_t=f_t, f_ω=f_ω_1)
        Θ_𝔓_2 = Θ𝔓_(S_1, S_2, _𝔓_2, f_t=f_t, f_ω=f_ω_2)
        gS_1 = S_(Θ_𝔓_1, d=i, w=1)
        gS_2 = S_(Θ_𝔓_2, d=i, w=2)
        setS.extend([gS_1, gS_2])
        setΘ_𝔓.extend([Θ_𝔓_1, Θ_𝔓_2])
        S_1 = gS_1
        S_2 = gS_2
    set𝔓 = [_𝔓_1, _𝔓_2]
    return Γ𝔈_graph(setS, setΘ_𝔓)

# Parameters
𝔈 = 𝔈_(
     v_1 = 4,  # L
     v_2 = 8,  # L
     q_1 = 3,  # L/m
     q_2 = 2,  # L/m
     q_3 = 5,  # L/m
     q_4 = 3,  # L/m
     ω_3 = 10) # g/l
Δt = .1
n = 10
p𝔖 = {
    (0,1): 𝔖𝔛_q_(t=.0, ω=0, q=1),    #State for S_d=0,w=1
    (0,2): 𝔖𝔛_q_(t=.0, ω=20, q=2)}   #State for S_d=0,w=2
use_earlier_transition_function = True

# Transition implementation
def X_transition(Δt):
    def f_t(t):
        return np.round(t + Δt, 4)
    return f_t
def S_functional_transition():
    q = m.sqrt(105.0)
    def em(t):
        return m.exp(((q - 15.0) * t) / 16.0)
    def ep(t):
        return m.exp(-(((q + 15.0) * t) / 16.0))
    def f_ω_1(ω_1, ω_2, t_prev, t, 𝔓_1):
        return ((13.0 * em(t) * q) / 21.0) - ((13.0 * ep(t) * q) / 21.0) - (5.0 * em(t)) - (5.0 * ep(t)) + 10.0
    def f_ω_2(ω_1, ω_2, t_prev, t, 𝔓_2):
        return -((5.0 * em(t) * q) / 21.0) + ((5.0 * ep(t) * q) / 21.0) + (5.0 * em(t)) + (5.0 * ep(t)) + 10.0
    return f_ω_1, f_ω_2
def S_earlier_transition():
    def f_ω_1(ω_1, ω_2, t, t_next, 𝔓_1):
        return ω_1 + (t_next - t) * (((𝔓_1.q_1 * 𝔓_1.ω_3) + (𝔓_1.q_2 * ω_2) - (𝔓_1.q_3 * ω_1)) / 𝔓_1.v_1)
    def f_ω_2(ω_1, ω_2, t, t_next, 𝔓_2):
        return ω_2 + (t_next - t) * (((𝔓_2.q_3 * ω_1) - (𝔓_2.q_2 * ω_2) - (𝔓_2.q_4 * ω_2)) / 𝔓_2.v_2)
    return f_ω_1, f_ω_2

# Build Γ^|𝔈
if use_earlier_transition_function:
    Γ𝔈 = build_Γ𝔈(n, Δt, 𝔈, X_transition, S_earlier_transition)
else:
    Γ𝔈 = build_Γ𝔈(n, Δt, 𝔈, X_transition, S_functional_transition)
print(Γ𝔈)
graph_viz = GraphVisualisation("Γ_graph", Γ𝔈, pause=.05)

# Eval γ^|𝔈 for given 𝔖' and get 𝔖^X|𝔈 set
γ𝔈 = Γ𝔈.γ(p𝔖)
print(γ𝔈)
set𝔖X𝔈 = γ𝔈.𝔖()
print("Set 𝔖^X|𝔈 gotten from γ^|𝔈:")
for 𝔖X𝔈 in set𝔖X𝔈: print("    " + str(𝔖X𝔈))

# Simulation function
def simulation(set𝔛):
    set𝔜 = []
    for 𝔛 in set𝔛:
        𝔖𝔛_1 = None
        𝔖𝔛_2 = None
        for 𝔖𝔛_q in set𝔖X𝔈:
            if 𝔖𝔛_q.t == 𝔛.t and 𝔖𝔛_q.q == 1:
                𝔖𝔛_1 = 𝔖𝔛_q
            if 𝔖𝔛_q.t == 𝔛.t and 𝔖𝔛_q.q == 2:
                𝔖𝔛_2 = 𝔖𝔛_q
        set𝔜.append(h𝔜_(𝔖𝔛_1.ω_1, 𝔖𝔛_2.ω_2))
    return set𝔜

# Run simulation
set𝔛 = np.vectorize(lambda t: b𝔛_(t))(np.round(np.arange(0.0, (n + 1) * Δt, Δt), 4))
set𝔜 = simulation(set𝔛)

# Print result
print("Simulation result (𝔛 -> 𝔜): ")
for [𝔛, 𝔜] in np.column_stack((set𝔛, set𝔜)):
    print(f"    {str(𝔛):30} --> {𝔜}")

# Plot result
plt.figure("Simulation of function set representation")
plt.grid(color="gray")
plt.plot([𝔛.t for 𝔛 in set𝔛], [𝔜.ω_1 for 𝔜 in set𝔜], "g", label="ω_1")
plt.plot([𝔛.t for 𝔛 in set𝔛], [𝔜.ω_2 for 𝔜 in set𝔜], "r", label="ω_2")
plt.show()

max_stars_repo_name: AlexCAB/SWRS

max_issues_repo_name: AlexCAB/SWRS

max_forks_repo_name: AlexCAB/SWRS