text:
"""
    PSPNet for image segmentation, implemented in Gluon.
    Original paper: 'Pyramid Scene Parsing Network,' https://arxiv.org/abs/1612.01105.
"""

__all__ = ['PSPNet', 'pspnet_resnetd50b_voc', 'pspnet_resnetd101b_voc', 'pspnet_resnetd50b_coco',
           'pspnet_resnetd101b_coco', 'pspnet_resnetd50b_ade20k', 'pspnet_resnetd101b_ade20k',
           'pspnet_resnetd50b_cityscapes', 'pspnet_resnetd101b_cityscapes']

import os
from mxnet import cpu
from mxnet.gluon import nn, HybridBlock
from mxnet.gluon.contrib.nn import HybridConcurrent, Identity
from .common import conv1x1, conv1x1_block, conv3x3_block
from .resnetd import resnetd50b, resnetd101b

class PSPFinalBlock(HybridBlock):
    """
    PSPNet final block.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    bottleneck_factor : int, default 4
        Bottleneck factor.
    """
    def __init__(self,
                 in_channels,
                 out_channels,
                 bottleneck_factor=4,
                 **kwargs):
        super(PSPFinalBlock, self).__init__(**kwargs)
        assert (in_channels % bottleneck_factor == 0)
        mid_channels = in_channels // bottleneck_factor

        with self.name_scope():
            self.conv1 = conv3x3_block(
                in_channels=in_channels,
                out_channels=mid_channels)
            self.dropout = nn.Dropout(rate=0.1)
            self.conv2 = conv1x1(
                in_channels=mid_channels,
                out_channels=out_channels,
                use_bias=True)

    def hybrid_forward(self, F, x, out_size):
        x = self.conv1(x)
        x = self.dropout(x)
        x = self.conv2(x)
        x = F.contrib.BilinearResize2D(x, height=out_size[0], width=out_size[1])
        return x

class PyramidPoolingBranch(HybridBlock):
    """
    Pyramid Pooling branch.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    pool_out_size : int
        Target output size of the image.
    upscale_out_size : tuple of 2 int or None
        Spatial size of output image for the bilinear upsampling operation.
    """
    def __init__(self,
                 in_channels,
                 out_channels,
                 pool_out_size,
                 upscale_out_size,
                 **kwargs):
        super(PyramidPoolingBranch, self).__init__(**kwargs)
        self.pool_out_size = pool_out_size
        self.upscale_out_size = upscale_out_size

        with self.name_scope():
            self.conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels)

    def hybrid_forward(self, F, x):
        in_size = self.upscale_out_size if self.upscale_out_size is not None else x.shape[2:]
        x = F.contrib.AdaptiveAvgPooling2D(x, output_size=self.pool_out_size)
        x = self.conv(x)
        x = F.contrib.BilinearResize2D(x, height=in_size[0], width=in_size[1])
        return x

class PyramidPooling(HybridBlock):
    """
    Pyramid Pooling module.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    upscale_out_size : tuple of 2 int
        Spatial size of the input tensor for the bilinear upsampling operation.
    """
    def __init__(self,
                 in_channels,
                 upscale_out_size,
                 **kwargs):
        super(PyramidPooling, self).__init__(**kwargs)
        pool_out_sizes = [1, 2, 3, 6]
        assert (len(pool_out_sizes) == 4)
        assert (in_channels % 4 == 0)
        mid_channels = in_channels // 4

        with self.name_scope():
            self.branches = HybridConcurrent(axis=1, prefix="")
            self.branches.add(Identity())
            for pool_out_size in pool_out_sizes:
                self.branches.add(PyramidPoolingBranch(
                    in_channels=in_channels,
                    out_channels=mid_channels,
                    pool_out_size=pool_out_size,
                    upscale_out_size=upscale_out_size))

    def hybrid_forward(self, F, x):
        x = self.branches(x)
        return x

class PSPNet(HybridBlock):
    """
    PSPNet model from 'Pyramid Scene Parsing Network,' https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    backbone : nn.Sequential
        Feature extractor.
    backbone_out_channels : int, default 2048
        Number of output channels form feature extractor.
    aux : bool, default False
        Whether to output an auxiliary result.
    fixed_size : bool, default True
        Whether to expect fixed spatial size of input image.
    in_channels : int, default 3
        Number of input channels.
    in_size : tuple of two ints, default (480, 480)
        Spatial size of the expected input image.
    classes : int, default 21
        Number of segmentation classes.
    """
    def __init__(self,
                 backbone,
                 backbone_out_channels=2048,
                 aux=False,
                 fixed_size=True,
                 in_channels=3,
                 in_size=(480, 480),
                 classes=21,
                 **kwargs):
        super(PSPNet, self).__init__(**kwargs)
        assert (in_channels > 0)
        assert ((in_size[0] % 8 == 0) and (in_size[1] % 8 == 0))
        self.in_size = in_size
        self.classes = classes
        self.aux = aux
        self.fixed_size = fixed_size

        with self.name_scope():
            self.backbone = backbone
            pool_out_size = (self.in_size[0] // 8, self.in_size[1] // 8) if fixed_size else None
            self.pool = PyramidPooling(
                in_channels=backbone_out_channels,
                upscale_out_size=pool_out_size)
            pool_out_channels = 2 * backbone_out_channels
            self.final_block = PSPFinalBlock(
                in_channels=pool_out_channels,
                out_channels=classes,
                bottleneck_factor=8)
            if self.aux:
                aux_out_channels = backbone_out_channels // 2
                self.aux_block = PSPFinalBlock(
                    in_channels=aux_out_channels,
                    out_channels=classes,
                    bottleneck_factor=4)

    def hybrid_forward(self, F, x):
        in_size = self.in_size if self.fixed_size else x.shape[2:]
        x, y = self.backbone(x)
        x = self.pool(x)
        x = self.final_block(x, in_size)
        if self.aux:
            y = self.aux_block(y, in_size)
            return x, y
        else:
            return x

def get_pspnet(backbone,
               classes,
               aux=False,
               model_name=None,
               pretrained=False,
               ctx=cpu(),
               root=os.path.join("~", ".mxnet", "models"),
               **kwargs):
    """
    Create PSPNet model with specific parameters.

    Parameters:
    ----------
    backbone : nn.Sequential
        Feature extractor.
    classes : int
        Number of segmentation classes.
    aux : bool, default False
        Whether to output an auxiliary result.
    model_name : str or None, default None
        Model name for loading pretrained model.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """

    net = PSPNet(
        backbone=backbone,
        classes=classes,
        aux=aux,
        **kwargs)

    if pretrained:
        if (model_name is None) or (not model_name):
            raise ValueError("Parameter `model_name` should be properly initialized for loading pretrained model.")
        from .model_store import get_model_file
        net.load_parameters(
            filename=get_model_file(
                model_name=model_name,
                local_model_store_dir_path=root),
            ctx=ctx,
            ignore_extra=True)

    return net

def pspnet_resnetd50b_voc(pretrained_backbone=False, classes=21, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-50b for Pascal VOC from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 21
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd50b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd50b_voc", **kwargs)

def pspnet_resnetd101b_voc(pretrained_backbone=False, classes=21, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-101b for Pascal VOC from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 21
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd101b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd101b_voc", **kwargs)

def pspnet_resnetd50b_coco(pretrained_backbone=False, classes=21, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-50b for COCO from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 21
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd50b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd50b_coco", **kwargs)

def pspnet_resnetd101b_coco(pretrained_backbone=False, classes=21, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-101b for COCO from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 21
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd101b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd101b_coco", **kwargs)

def pspnet_resnetd50b_ade20k(pretrained_backbone=False, classes=150, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-50b for ADE20K from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 150
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd50b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd50b_ade20k", **kwargs)

def pspnet_resnetd101b_ade20k(pretrained_backbone=False, classes=150, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-101b for ADE20K from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 150
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd101b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd101b_ade20k", **kwargs)

def pspnet_resnetd50b_cityscapes(pretrained_backbone=False, classes=19, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-50b for Cityscapes from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 19
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd50b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd50b_cityscapes", **kwargs)

def pspnet_resnetd101b_cityscapes(pretrained_backbone=False, classes=19, aux=True, **kwargs):
    """
    PSPNet model on the base of ResNet(D)-101b for Cityscapes from 'Pyramid Scene Parsing Network,'
    https://arxiv.org/abs/1612.01105.

    Parameters:
    ----------
    pretrained_backbone : bool, default False
        Whether to load the pretrained weights for feature extractor.
    classes : int, default 19
        Number of segmentation classes.
    aux : bool, default True
        Whether to output an auxiliary result.
    pretrained : bool, default False
        Whether to load the pretrained weights for model.
    ctx : Context, default CPU
        The context in which to load the pretrained weights.
    root : str, default '~/.mxnet/models'
        Location for keeping the model parameters.
    """
    backbone = resnetd101b(pretrained=pretrained_backbone, ordinary_init=False, multi_output=True).features[:-1]
    return get_pspnet(backbone=backbone, classes=classes, aux=aux, model_name="pspnet_resnetd101b_cityscapes", **kwargs)

def _test():
    import numpy as np
    import mxnet as mx

    in_size = (480, 480)
    aux = False
    pretrained = False

    models = [
        (pspnet_resnetd50b_voc, 21),
        (pspnet_resnetd101b_voc, 21),
        (pspnet_resnetd50b_coco, 21),
        (pspnet_resnetd101b_coco, 21),
        (pspnet_resnetd50b_ade20k, 150),
        (pspnet_resnetd101b_ade20k, 150),
        (pspnet_resnetd50b_cityscapes, 19),
        (pspnet_resnetd101b_cityscapes, 19),
    ]

    for model, classes in models:

        net = model(pretrained=pretrained, in_size=in_size, aux=aux)

        ctx = mx.cpu()
        if not pretrained:
            net.initialize(ctx=ctx)

        # net.hybridize()
        net_params = net.collect_params()
        weight_count = 0
        for param in net_params.values():
            if (param.shape is None) or (not param._differentiable):
                continue
            weight_count += np.prod(param.shape)
        print("m={}, {}".format(model.__name__, weight_count))
        if aux:
            assert (model != pspnet_resnetd50b_voc or weight_count == 49081578)
            assert (model != pspnet_resnetd101b_voc or weight_count == 68073706)
            assert (model != pspnet_resnetd50b_coco or weight_count == 49081578)
            assert (model != pspnet_resnetd101b_coco or weight_count == 68073706)
            assert (model != pspnet_resnetd50b_ade20k or weight_count == 49180908)
            assert (model != pspnet_resnetd101b_ade20k or weight_count == 68173036)
            assert (model != pspnet_resnetd50b_cityscapes or weight_count == 49080038)
            assert (model != pspnet_resnetd101b_cityscapes or weight_count == 68072166)
        else:
            assert (model != pspnet_resnetd50b_voc or weight_count == 46716373)
            assert (model != pspnet_resnetd101b_voc or weight_count == 65708501)
            assert (model != pspnet_resnetd50b_coco or weight_count == 46716373)
            assert (model != pspnet_resnetd101b_coco or weight_count == 65708501)
            assert (model != pspnet_resnetd50b_ade20k or weight_count == 46782550)
            assert (model != pspnet_resnetd101b_ade20k or weight_count == 65774678)
            assert (model != pspnet_resnetd50b_cityscapes or weight_count == 46715347)
            assert (model != pspnet_resnetd101b_cityscapes or weight_count == 65707475)

        x = mx.nd.zeros((1, 3, in_size[0], in_size[1]), ctx=ctx)
        ys = net(x)
        y = ys[0] if aux else ys
        assert ((y.shape[0] == x.shape[0]) and (y.shape[1] == classes) and (y.shape[2] == x.shape[2]) and
                (y.shape[3] == x.shape[3]))

if __name__ == "__main__":
    _test()

max_issues_repo_name: fireoil/imgclsmob

max_forks_repo_name: fireoil/imgclsmob


text:
from matplotlib import pyplot as plt
import numpy as np
import scipy.stats as st

def kde(x, y, xlabel='x', ylabel='y', ax=None, contours=True):
    """
    Create a kernel density estimation visualization of a 2d point pattern

    Parameters
    ---------

    x : ndarray
        of x-axis values

    y : ndarray
        of y-axis values

    xlabel : str
             The label for the x-axis

    ylabel : str
             The lable for the y-axis

    ax : object
         A Matplotlib axis object to insert the plot into. If
         none provided, the current axis is grabbed

    contours : bool
               If true (default) add contours.
    """
    if ax == None:
        ax = plt.gca()

    xmin = x.min()
    xmax = x.max()
    ymin = y.min()
    ymax = y.max()
    xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions = np.vstack([xx.ravel(), yy.ravel()])
    values = np.vstack([x, y])
    kernel = st.gaussian_kde(values)
    f = np.reshape(kernel(positions).T, xx.shape)

    ax.set_xlim(xmin, xmax)
    ax.set_ylim(ymin, ymax)

    # Contour plot
    if contours:
        cfset = ax.contourf(xx, yy, f, cmap='Blues')
        cset = ax.contour(xx, yy, f, colors='k')
        ax.clabel(cset, inline=1, fontsize=10)
    else:
        ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax])

    # Contour plot
    # Label plot
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)

    return ax

ext: py

reason: import numpy,import scipy


text:
# in case this is run outside of conda environment with python2
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import  unicode_literals

import mlflow
from mlflow import pyfunc
import pandas as pd
import shutil
import tempfile
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
import mlflow.tensorflow
import numpy as np
import os
import pandas as pd
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'

# Enable auto-logging to MLflow to capture TensorBoard metrics.
mlflow.tensorflow.autolog()

def main(argv):

    zip_path = tf.keras.utils.get_file(
    origin='https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip',
    fname='jena_climate_2009_2016.csv.zip',
    extract=True)

    csv_path, _ = os.path.splitext(zip_path)
    df = pd.read_csv(csv_path)

    def univariate_data(dataset, start_index, end_index, history_size, target_size):
        data = []
        labels = []
        start_index = start_index + history_size
        if end_index is None:
            end_index = len(dataset) - target_size

        for i in range(start_index, end_index):
            indices = range(i-history_size, i)
            # Reshape data from (history_size,) to (history_size, 1)
            data.append(np.reshape(dataset[indices], (history_size, 1)))
            labels.append(dataset[i+target_size])
        return np.array(data), np.array(labels)

    TRAIN_SPLIT = 300000

    uni_data = df['T (degC)']
    uni_data.index = df['Date Time']
    uni_data = uni_data.values
    uni_train_mean = uni_data[:TRAIN_SPLIT].mean()
    uni_train_std = uni_data[:TRAIN_SPLIT].std()
    uni_data = (uni_data-uni_train_mean)/uni_train_std

    univariate_past_history = 20
    univariate_future_target = 0

    x_train_uni, y_train_uni = univariate_data(uni_data, 0, TRAIN_SPLIT,univariate_past_history,univariate_future_target)
    x_val_uni, y_val_uni = univariate_data(uni_data, TRAIN_SPLIT, None,univariate_past_history,univariate_future_target)

    BATCH_SIZE = 256
    BUFFER_SIZE = 10000

    train_univariate = tf.data.Dataset.from_tensor_slices((x_train_uni, y_train_uni))
    train_univariate = train_univariate.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()

    val_univariate = tf.data.Dataset.from_tensor_slices((x_val_uni, y_val_uni))
    val_univariate = val_univariate.batch(BATCH_SIZE).repeat()

    simple_lstm_model = tf.keras.models.Sequential([tf.keras.layers.LSTM(8, input_shape=x_train_uni.shape[-2:]),tf.keras.layers.Dense(1)])
    simple_lstm_model.compile(optimizer='adam', loss='mae')

    with mlflow.start_run():
        mlflow.log_param("Model Summary", simple_lstm_model.summary())

        EVALUATION_INTERVAL = 200
        EPOCHS = 10

        simple_lstm_model.fit(train_univariate, epochs=EPOCHS,steps_per_epoch=EVALUATION_INTERVAL,verbose=1)

        loss=simple_lstm_model.evaluate(val_univariate,steps=100,verbose=1)

        mlflow.log_metric("Error", loss)

if __name__ == "__main__":
    # The Estimator periodically generates "INFO" logs; make these logs visible.
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.app.run(main=main)

max_stars_repo_name: mananparasher/ai-platform

max_issues_repo_name: mananparasher/ai-platform

max_forks_repo_name: mananparasher/ai-platform

reason: import numpy


text:
import pandas as pd
import FixedEffectModelPyHDFE.api as FEM
from FixedEffectModelPyHDFE.DemeanDataframe import get_np_columns
#import FixedEffectModel.api as FEM
import numpy as np
from patsy import dmatrices
import statsmodels.formula.api as smf
import statsmodels.api as sm

df = pd.read_stata('/home/abom/Desktop/FixedEffectModel/data/test.dta')
consist_input = ['wks_ue','tenure']
output_input = ['ttl_exp']
category_input = ['idcode']
cluster_input = ['idcode', 'birth_yr', 'fifty_clusts', 'sixty_clusts']
#cluster_input = ['idcode', 'sixty_clusts', 'fifty_clusts']

print("absorbed", category_input)
print("clustered", cluster_input)
result1 = FEM.ols_high_d_category(df,consist_input,output_input,category_input,cluster_input,formula=None,c_method='cgm2',robust=False,epsilon = 1e-8,max_iter = 1e6)

result1.summary()

y, X = dmatrices('ttl_exp ~ wks_ue + tenure', data=df, return_type='dataframe')

model = sm.OLS(y, X)

res = model.fit(cov_type='cluster', cov_kwds={'df_correction':True, 'groups':get_np_columns(df, columns=['birth_yr'])})
res = model.fit()

#print(res.summary())
#print(res.get_robustcov_results(df_correction = False, cov_type='cluster', groups=get_np_columns(df, columns=['fifty_clusts']) ).summary())

#import pandas as pd
#import econtools.metrics as mt
#
## Load a data file with columns 'ln_wage', 'educ', and 'state'
#
#y = 'ttl_exp'
#X = ['wks_ue', 'tenure']
#fe_var = ''
#cluster_var = 'birth_yr'
#
#results = mt.reg(
#    df,                     # DataFrame
#    y,                      # Dependent var (string)
#    X,                      # Independent var(s) (string or list of strings)
#    addcons=True,
#    cluster=cluster_var     # Cluster var (string)
#)
#print(results)
#print(results.Ftest(["wks_ue", "tenure"]))

reason: import numpy,import statsmodels


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 14:40:04 2015

@author: ryuhei
"""

import numpy as np
from basic_distributions import (
    ProbabilityDistribution,  Multinomial, Gaussian, Poisson
)

class MixtureDistributionBase(ProbabilityDistribution):
    def __init__(self, K, weights):
        assert K == len(weights)

        self.K = K
        self.weights = weights
        self.multinomial = Multinomial(weights)
        self.mixture_components = None

    def __call__(self, num_examples=10000, complete_data=False):
        z = []
        x = []
        for i in range(num_examples):
            z_i = self.multinomial(1)[0]
            x_i = self.mixture_components[z_i](1)[0]
            z.append(z_i)
            x.append(x_i)

        if complete_data:
            return (np.array(z), np.array(x))
        else:
            return np.array(x)

class MixtureOfGaussians(MixtureDistributionBase):
    def __init__(self, K=3, weights=[0.1, 0.7, 0.2],
                 means=[-15, 0, 30], stds=[1, 10, 2]):
        assert K == len(means) == len(stds)
        super(MixtureOfGaussians, self).__init__(K, weights)

        self.means = means
        self.stds = stds
        self.mixture_components = [
            Gaussian(means[k], stds[k]) for k in range(K)]

class MixtureOfPoissons(MixtureDistributionBase):
    def __init__(self, K=3, weights=[0.3, 0.3, 0.4],
                 means=[2, 20, 50]):
        assert K == len(means)
        super(MixtureOfPoissons, self).__init__(K, weights)

        self.means = means
        self.mixture_components = [Poisson(means[k]) for k in range(K)]

if __name__ == '__main__':
    distributions = [MixtureOfGaussians(),  # 0
                     MixtureOfPoissons(),   # 1
                     ]
    dist_type = 1
    complete_data = True

    sampler = distributions[dist_type]

    data = sampler(10000, complete_data)
    if complete_data:
        z, x = data
    else:
        x = data

    sampler.visualize(x)
    print("Distribution: ", sampler.get_name())
    print("Parameters: ", sampler.get_params())


text:
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""BERT finetuning runner."""

from __future__ import absolute_import, division, print_function

import argparse
import csv
import logging
import os
import random
import sys

import numpy as np
import torch
from torch.utils.data import (DataLoader, RandomSampler, SequentialSampler,
                              TensorDataset)
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm, trange

from torch.nn import CrossEntropyLoss, MSELoss
from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import matthews_corrcoef, f1_score

from pytorch_pretrained_bert.file_utils import PYTORCH_PRETRAINED_BERT_CACHE, WEIGHTS_NAME, CONFIG_NAME
from pytorch_pretrained_bert.modeling import BertForSequenceClassification, BertConfig
from pytorch_pretrained_bert.tokenization import BertTokenizer
from pytorch_pretrained_bert.optimization import BertAdam, WarmupLinearSchedule

logger = logging.getLogger(__name__)

class InputExample(object):
    """A single training/test example for simple sequence classification."""

    def __init__(self, guid, text_a, text_b=None, label=None):
        """Constructs a InputExample.

        Args:
            guid: Unique id for the example.
            text_a: string. The untokenized text of the first sequence. For single
            sequence tasks, only this sequence must be specified.
            text_b: (Optional) string. The untokenized text of the second sequence.
            Only must be specified for sequence pair tasks.
            label: (Optional) string. The label of the example. This should be
            specified for train and dev examples, but not for test examples.
        """
        self.guid = guid
        self.text_a = text_a
        self.text_b = text_b
        self.label = label

class InputFeatures(object):
    """A single set of features of data."""

    def __init__(self, input_ids, input_mask, segment_ids, label_id):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.segment_ids = segment_ids
        self.label_id = label_id

class DataProcessor(object):
    """Base class for data converters for sequence classification data sets."""

    def get_train_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the train set."""
        raise NotImplementedError()

    def get_dev_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the dev set."""
        raise NotImplementedError()

    def get_labels(self):
        """Gets the list of labels for this data set."""
        raise NotImplementedError()

    @classmethod
    def _read_tsv(cls, input_file, quotechar=None):
        """Reads a tab separated value file."""
        with open(input_file, "r", encoding="utf-8") as f:
            reader = csv.reader(f, delimiter="\t", quotechar=quotechar)
            lines = []
            for line in reader:
                if sys.version_info[0] == 2:
                    line = list(unicode(cell, 'utf-8') for cell in line)
                lines.append(line)
            return lines

class ColaProcessor(DataProcessor):
    """Processor for the CoLA data set (GLUE version)."""

    def get_train_examples(self, data_dir):
        """See base class."""
        return self._create_examples(
            self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")

    def get_dev_examples(self, data_dir):
        """See base class."""
        return self._create_examples(
            self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")

    def get_pred_examples(self, data_dir):
        """See base class."""
        lines = self._read_tsv(os.path.join(data_dir, "pred.tsv"))
        examples = []
        for (i, line) in enumerate(lines):
            if i == 0:
                continue
            guid = "%s-%s" % ('pred', i)
            text_a = line[0]
            text_b = None
            label = str(i%2)
            examples.append(
                InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
        return examples

    def get_labels(self):
        """See base class."""
        return ["0", "1"]

    def _create_examples(self, lines, set_type):
        """Creates examples for the training and dev sets."""
        examples = []
        for (i, line) in enumerate(lines):
            guid = "%s-%s" % (set_type, i)
            text_a = line[3]
            label = line[1]
            examples.append(
                InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
        return examples

def convert_examples_to_features(examples, label_list, max_seq_length,
                                 tokenizer, output_mode='classification'):
    """Loads a data file into a list of `InputBatch`s."""

    label_map = {label : i for i, label in enumerate(label_list)}

    features = []
    for (ex_index, example) in enumerate(examples):
        if ex_index % 10000 == 0:
            logger.info("Writing example %d of %d" % (ex_index, len(examples)))

        tokens_a = tokenizer.tokenize(example.text_a)

        tokens_b = None
        if example.text_b:
            tokens_b = tokenizer.tokenize(example.text_b)
            # Modifies `tokens_a` and `tokens_b` in place so that the total
            # length is less than the specified length.
            # Account for [CLS], [SEP], [SEP] with "- 3"
            _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
        else:
            # Account for [CLS] and [SEP] with "- 2"
            if len(tokens_a) > max_seq_length - 2:
                tokens_a = tokens_a[:(max_seq_length - 2)]

        # The convention in BERT is:
        # (a) For sequence pairs:
        #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
        #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
        # (b) For single sequences:
        #  tokens:   [CLS] the dog is hairy . [SEP]
        #  type_ids: 0   0   0   0  0     0 0
        #
        # Where "type_ids" are used to indicate whether this is the first
        # sequence or the second sequence. The embedding vectors for `type=0` and
        # `type=1` were learned during pre-training and are added to the wordpiece
        # embedding vector (and position vector). This is not *strictly* necessary
        # since the [SEP] token unambiguously separates the sequences, but it makes
        # it easier for the model to learn the concept of sequences.
        #
        # For classification tasks, the first vector (corresponding to [CLS]) is
        # used as as the "sentence vector". Note that this only makes sense because
        # the entire model is fine-tuned.
        tokens = ["[CLS]"] + tokens_a + ["[SEP]"]
        segment_ids = [0] * len(tokens)

        if tokens_b:
            tokens += tokens_b + ["[SEP]"]
            segment_ids += [1] * (len(tokens_b) + 1)

        input_ids = tokenizer.convert_tokens_to_ids(tokens)

        # The mask has 1 for real tokens and 0 for padding tokens. Only real
        # tokens are attended to.
        input_mask = [1] * len(input_ids)

        # Zero-pad up to the sequence length.
        padding = [0] * (max_seq_length - len(input_ids))
        input_ids += padding
        input_mask += padding
        segment_ids += padding

        assert len(input_ids) == max_seq_length
        assert len(input_mask) == max_seq_length
        assert len(segment_ids) == max_seq_length

        if output_mode == "classification":
            label_id = label_map[example.label]
        elif output_mode == "regression":
            label_id = float(example.label)
        else:
            raise KeyError(output_mode)

        if ex_index < 5:
            logger.info("*** Example ***")
            logger.info("guid: %s" % (example.guid))
            logger.info("tokens: %s" % " ".join(
                    [str(x) for x in tokens]))
            logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
            logger.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
            logger.info(
                    "segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
            logger.info("label: %s (id = %d)" % (example.label, label_id))

        features.append(
                InputFeatures(input_ids=input_ids,
                              input_mask=input_mask,
                              segment_ids=segment_ids,
                              label_id=label_id))
    return features

def _truncate_seq_pair(tokens_a, tokens_b, max_length):
    """Truncates a sequence pair in place to the maximum length."""

    # This is a simple heuristic which will always truncate the longer sequence
    # one token at a time. This makes more sense than truncating an equal percent
    # of tokens from each, since if one sequence is very short then each token
    # that's truncated likely contains more information than a longer sequence.
    while True:
        total_length = len(tokens_a) + len(tokens_b)
        if total_length <= max_length:
            break
        if len(tokens_a) > len(tokens_b):
            tokens_a.pop()
        else:
            tokens_b.pop()

def simple_accuracy(preds, labels):
    return (preds == labels).mean()

def compute_metrics(task_name, preds, labels):
    assert len(preds) == len(labels)
    return {"mcc": matthews_corrcoef(labels, preds),"acc": simple_accuracy(preds, labels)}

def main():
    parser = argparse.ArgumentParser()

    ## Required parameters
    parser.add_argument("--data_dir",
                        default=None,
                        type=str,
                        required=True,
                        help="The input data dir. Should contain the .tsv files (or other data files) for the task.")
    parser.add_argument("--bert_model", default=None, type=str, required=True,
                        help="Bert pre-trained model selected in the list: bert-base-uncased, "
                        "bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, "
                        "bert-base-multilingual-cased, bert-base-chinese.")
    parser.add_argument("--output_dir",
                        default=None,
                        type=str,
                        required=True,
                        help="The output directory where the model predictions and checkpoints will be written.")

    ## Other parameters
    parser.add_argument("--cache_dir",
                        default="",
                        type=str,
                        help="Where do you want to store the pre-trained models downloaded from s3")
    parser.add_argument("--max_seq_length",
                        default=128,
                        type=int,
                        help="The maximum total input sequence length after WordPiece tokenization. \n"
                             "Sequences longer than this will be truncated, and sequences shorter \n"
                             "than this will be padded.")
    parser.add_argument("--do_eval",
                        action='store_true',
                        help="Whether to run eval on the dev set.")
    parser.add_argument("--do_lower_case",
                        action='store_true',
                        help="Set this flag if you are using an uncased model.")
    parser.add_argument("--eval_batch_size",
                        default=8,
                        type=int,
                        help="Total batch size for eval.")
    parser.add_argument("--no_cuda",
                        action='store_true',
                        help="Whether not to use CUDA when available")
    parser.add_argument("--local_rank",
                        type=int,
                        default=-1,
                        help="local_rank for distributed training on gpus")
    parser.add_argument('--seed',
                        type=int,
                        default=42,
                        help="random seed for initialization")

    args = parser.parse_args()

    device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
    n_gpu = torch.cuda.device_count()

    logging.basicConfig(format = '%(asctime)s - %(levelname)s - %(name)s -   %(message)s',
                        datefmt = '%m/%d/%Y %H:%M:%S',
                        level = logging.INFO if args.local_rank in [-1, 0] else logging.WARN)

    logger.info("device: {} n_gpu: {}, distributed training: {}, 16-bits training: -".format(
        device, n_gpu, bool(args.local_rank != -1) ))

    random.seed(args.seed)
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if n_gpu > 0:
        torch.cuda.manual_seed_all(args.seed)

    if not os.path.exists(args.output_dir):
        # os.makedirs(args.output_dir)
        raise ValueError("No model output dir found.")

    processor = ColaProcessor() # processors[task_name]()

    label_list = processor.get_labels()
    num_labels = len(label_list)

    global_step = 0

    model = BertForSequenceClassification.from_pretrained(args.output_dir, num_labels=num_labels)
    tokenizer = BertTokenizer.from_pretrained(args.output_dir, do_lower_case=args.do_lower_case)
    model.to(device)

    # if args.do_eval and (args.local_rank == -1 or torch.distributed.get_rank() == 0):
    eval_examples = processor.get_pred_examples(args.data_dir)
    eval_features = convert_examples_to_features(
            eval_examples, label_list, args.max_seq_length, tokenizer) # , output_mode)
    logger.info("***** Running evaluation *****")
    logger.info("  Num examples = %d", len(eval_examples))
    logger.info("  Batch size = %d", args.eval_batch_size)
    # print(eval_examples[:10])
    all_input_idxs = torch.tensor([idx for idx,f in enumerate(eval_features)], dtype=torch.long)
    all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
    all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
    all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)

    all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.long)

    eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids, all_input_idxs)
    eval_sampler = SequentialSampler(eval_data)
    eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)

    model.eval()
    # model.predict()
    eval_loss = 0
    nb_eval_steps = 0
    preds = []

    for input_ids, input_mask, segment_ids, label_ids, text_idxs in tqdm(eval_dataloader, desc="Evaluating", disable=True):
        input_ids = input_ids.to(device)
        input_mask = input_mask.to(device)
        segment_ids = segment_ids.to(device)
        label_ids = label_ids.to(device)

        with torch.no_grad():
            logits = model(input_ids, segment_ids, input_mask, labels=None)
            for idx,logit in zip(list(text_idxs.data),list(logits.data)):
                # print(idx,logit)
                print("%s\t%f\t%f" % ( eval_examples[idx.item()].text_a,logit[0].item(),logit[1].item()) )

if __name__ == "__main__":
    main()

reason: import numpy,from scipy


text:
import sys, os
sys.path.append(os.path.abspath(__file__).split('test')[0])

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from pyml.supervised.linear_regression.LinearRegression import LinearRegression

"""

------------------------------------------------------------------------------------------------------------------------

                                                    CASAS

------------------------------------------------------------------------------------------------------------------------

"""

#-----------------------------------------------------------------------------------------------------------------------
#---------------------------------------------- OBTENCION DATOS --------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

data = pd.read_csv('../../../data/ex1data2.txt', sep=",", header=None)         #Leer datos
data.columns = ["size", "n_bedrooms", "price"]

print(data.head())                                                          #Imprimimos los datos
print(data.describe())

num_col = data.shape[0]
num_filas = data.shape[1]

X = np.matrix([np.ones(num_col), data['size'], data['n_bedrooms']])
y = np.matrix(data['price'])

#-----------------------------------------------------------------------------------------------------------------------
#------------------------------------------- FEATURE NORMALIZATION -----------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

multi_linear_regression = LinearRegression(X, y)
multi_linear_regression.aplicar_feature_normalization(include_y=True)

#-----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------- VISUALIZACION ---------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

fig = plt.figure()
ax = fig.gca(projection='3d')

xline = multi_linear_regression.X[1]
yline = multi_linear_regression.X[2]
zline = multi_linear_regression.y

ax.scatter(xline, yline, zline, 'gray')
plt.title("Dataset")
plt.xlabel("Tamano")
plt.ylabel("Numero camas")
plt.show()

#-----------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------- TEST -------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

print("\n--------------------------------------------")
print("    RESULTADOS CON FEATURE NORMALIZATION          ")
print("--------------------------------------------\n")

multi_linear_regression = LinearRegression(X, y)
multi_linear_regression.aplicar_feature_normalization()

print("Coste inicial: ", multi_linear_regression.calcular_coste())
multi_linear_regression.minimizacion()
print("Coste final: ", multi_linear_regression.calcular_coste())

print("\n--------------------------------------------")
print("    RESULTADOS SIN FEATURE NORMALIZATION          ")
print("--------------------------------------------\n")

multi_linear_regression = LinearRegression(X, y)

print("Coste inicial: ", multi_linear_regression.calcular_coste())
multi_linear_regression.minimizacion()
print("Coste final: ", multi_linear_regression.calcular_coste())

"""

------------------------------------------------------------------------------------------------------------------------

                                                EJEMPLO REAL STATE

------------------------------------------------------------------------------------------------------------------------

"""

#-----------------------------------------------------------------------------------------------------------------------
#---------------------------------------------- OBTENCION DATOS --------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

data = pd.read_csv("../../../data/50_Startups.csv", sep=",")

print("\n--------------------------------------------")
print("          REAL STATE        ")
print("--------------------------------------------\n")

print(data.head())
print(data.describe())

data_encoded = pd.get_dummies(data)
num_filas = data_encoded.values.shape[0]
num_col = data_encoded.values.shape[1]

X = np.matrix([np.ones(num_filas)])
X = np.concatenate((X, data_encoded.loc[:, data_encoded.columns != 'Profit'].values.T))
y = np.matrix(data_encoded['Profit'].values)

#-----------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------- TEST -------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

print("\n--------------------------------------------")
print("    RESULTADOS SIN FEATURE NORMALIZATION          ")
print("--------------------------------------------\n")

multi_linear_regression = LinearRegression(X, y)

print("Coste inicial: ", multi_linear_regression.calcular_coste())
multi_linear_regression.minimizacion()
print("Coste final: ", multi_linear_regression.calcular_coste())

print("\n--------------------------------------------")
print("    RESULTADOS CON FEATURE NORMALIZATION          ")
print("--------------------------------------------\n")

multi_linear_regression = LinearRegression(X, y)
multi_linear_regression.aplicar_feature_normalization(exclude=[4, 6], include_y=True)

print("Coste inicial: ", multi_linear_regression.calcular_coste())
multi_linear_regression.minimizacion()
print("Coste final: ", multi_linear_regression.calcular_coste())


text:
from PIL import Image
import numpy as np

DPI = 600  # printer DPI
WHEEL_ID = 67.9  # wheel inner diameter (mm)
WHEEL_WIDTH = 26
TICKS_PER_REV = 10

wheel_id_px = int(np.round(WHEEL_ID * np.pi * DPI / 25.4))
wheel_w_px = int(np.round(WHEEL_WIDTH * DPI / 25.4))

print wheel_w_px, wheel_id_px

img = np.zeros((wheel_id_px, wheel_w_px*4 + 3), np.uint8)

for i in range(TICKS_PER_REV):
    # make the top half of each period white
    y0 = int(i * float(wheel_id_px) / TICKS_PER_REV)
    y1 = int((i+0.5) * float(wheel_id_px) / TICKS_PER_REV)
    img[y0:y1] = 255

img[:, wheel_w_px:wheel_w_px+4] = 255
img[:, wheel_w_px*2:wheel_w_px*2+4] = 255
img[:, wheel_w_px*3:wheel_w_px*3+4] = 255

Image.fromarray(img).save("we.png")

max_stars_repo_name: ParikhKadam/cycloid

max_issues_repo_name: ParikhKadam/cycloid

max_forks_repo_name: ParikhKadam/cycloid


text:
import numpy as np
import matplotlib.pyplot as plt
import scipy
from scipy.fftpack import fftshift, ifftshift, fft2, ifft2
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
from scipy.signal import correlate2d as correlate
from scipy.signal import general_gaussian
from astropy.io import fits
from scipy import ndimage
from functools import partial
import time
import imreg_dft
import pyfits
import tools
import aperture
import PD
import noise
import wavefront
import deconvolution
from scipy.optimize import minimize, minimize_scalar

#from . import detector
#from .detector import *
from telescope import *

from tools import imreg, apo2d
from aperture import *
from PD import *
from noise import *
from wavefront import *
from deconvolution import *

class minimization(object):

   def __init__(self,foc,defoc,size,lam, diameter,focal_length,platescale,cut_off,reg,ap,x1,x2,y1,y2,co_num,del_z):
      self.foc = foc
      self.defoc = defoc
      self.cut_off = cut_off
      self.reg = reg
      self.size = size
      self.ap = ap
      self.co_num = co_num
      self.x1 = x1
      self.x2 = x2
      self.y1 = y1
      self.y2 = y2
      self.del_z = del_z
      self.lam = lam
      self.diameter = diameter
      self.focal_length = focal_length
      self.platescale = platescale
      self.reg = reg
      self.telescope = Telescope(self.lam,self.diameter,self.focal_length,'HRT',self.platescale,self.size)

   def fit(self,m = 'L-BFGS-B'):
      im0 = self.foc[self.y1:self.y2,self.x1:self.x2]
      imk = self.defoc[self.y1:self.y2,self.x1:self.x2]
      imk = tools.imreg(im0,imk)
      im0 = tools.apo2d(im0,self.ap)
      imk = tools.apo2d(imk, self.ap)
      rpupil = self.telescope.pupil_size()
      Mask = aperture.mask_pupil(rpupil,self.size)

      d0,dk = PD.FT(im0,imk)

      noise_filter = fftshift(noise.noise_mask_high(self.size,self.cut_off))
      M_gamma = noise.noise_mask(self.size,self.cut_off)
      gam = noise.Gamma(d0,dk,M_gamma)
      p0 =   np.zeros(self.co_num)

      def Minimise(coefficients):
       A_f = wavefront.pupil_foc(coefficients,self.size,self.telescope.pupil_size(),self.co_num)
       A_def = wavefront.pupil_defocus(coefficients,self.size,self.del_z,self.telescope.pupil_size(),self.co_num)
       psf_foc = wavefront.PSF(Mask,A_f,False)
       psf_defoc = wavefront.PSF(Mask,A_def,False)
       t0 = wavefront.OTF(psf_foc)
       tk = wavefront.OTF(psf_defoc)

       q,q2 = PD.Q_matrix(t0,tk,self.reg,gam)

       #noise_filter = sch_filter(N0, t0, tk, d0, dk,reg)
       F_m = PD.F_M(q2,d0, dk,t0,tk,noise_filter,gam)

       E_metric = PD.Error_metric(t0,tk,d0,dk,q,noise_filter)
       L_m = PD.L_M(E_metric,self.size)
       return L_m

      Minimise_partial = partial(Minimise)
      mini = scipy.optimize.minimize(Minimise_partial,p0,method=m)
      return mini.x

   def plot_results(self,Z,wav,modulation,azimuthal):

    if len(Z)!=self.co_num:
      self.co_num = len(Z)

      A_f = wavefront.pupil_foc(Z,self.size,self.telescope.pupil_size(),self.co_num)
      rpupil = self.telescope.pupil_size()
      Mask = aperture.mask_pupil(rpupil,self.size)
      psf_foc = wavefront.PSF(Mask,A_f,False)
      t0 = wavefront.OTF(psf_foc)
      ph =wavefront.phase(Z, self.telescope.pupil_size(),self.co_num)

      if wav=='True':
      #def plot_wavefront(self):
        fig = plt.figure(figsize=(10,10))
	ax = fig.add_subplot(1,1,1)

	im = ax.imshow(ph/(2*np.pi), origin='lower',cmap='gray')
        ax.set_xlabel('[Pixels]',fontsize=18)
	ax.set_ylabel('[Pixels]',fontsize=18)
	divider = make_axes_locatable(ax)
	cax = divider.append_axes("right", size=0.15, pad=0.05)

	cbar = plt.colorbar(im, cax=cax,orientation='vertical')#,ticks=np.arange(0.4,0.9,0.1))
        cbar.set_label('WF error HRT [$\lambda$]',fontsize=20)
	plt.show()
      #else:
        #pass

      #def plot_mtf(self):
      if modulation=='True':
        fig = plt.figure(figsize=(10,10))
	ax = fig.add_subplot(1,1,1)
        im = ax.imshow(wavefront.MTF(fftshift(t0)), origin='lower',cmap='gray')
        ax.set_xlabel('[Pixels]',fontsize=18)
	ax.set_ylabel('[Pixels]',fontsize=18)
	divider = make_axes_locatable(ax)
	cax = divider.append_axes("right", size=0.15, pad=0.05)

	cbar = plt.colorbar(im, cax=cax,orientation='vertical')#,ticks=np.arange(0.4,0.9,0.1))
        cbar.set_label('MTF',fontsize=20)

        plt.show()
      #else:
        #pass

      if azimuthal =='True':
      #def plot_azimuthal_mtf(self):
        az = tools.GetPSD1D(wavefront.MTF(fftshift(t0)))
        freq=np.linspace(0,0.5,int(self.size/2))
        freq_c_hrt = self.diameter/(self.lam*self.focal_length*100)
        phi_hrt = np.arccos(freq/freq_c_hrt)
        MTF_p_hrt = (2/np.pi)*(phi_hrt - (np.cos(phi_hrt))*np.sin(phi_hrt))
        fig = plt.figure(figsize=(10,10))
        plt.xlabel('Freq (1/pixel)',fontsize=22)
        plt.ylabel('MTF',fontsize=22)
        plt.plot(freq,MTF_p_hrt,label='Theoretical MTF')
        plt.plot(freq,az,label='Observed MTF')
        plt.xlim(0,0.5)
        plt.legend()
        plt.show()

   def restored_scene(self,Z, to_clean,filter,iterations_RL):

      A_f = wavefront.pupil_foc(Z,self.size,self.telescope.pupil_size(),self.co_num)
      rpupil = self.telescope.pupil_size()
      Mask = aperture.mask_pupil(rpupil,self.size)
      psf_foc = wavefront.PSF(Mask,A_f,False)
      t0 = wavefront.OTF(psf_foc)
      if to_clean.shape != t0.shape:
         raise ValueError('Image and PSF do not have the same dimensions')
      elif to_clean.shape == t0.shape:

        if filter == 'Wiener':
         restored = deconvolution.Wienerfilter(to_clean,t0,self.reg,self.cut_off,self.size)
         return restored
        if filter == 'richardsonLucy':
         restored = deconvolution.richardsonLucy(to_clean, psf_foc, iterations_RL)
         return restored

max_stars_repo_name: fakahil/PyPD

max_issues_repo_name: fakahil/PyPD

max_forks_repo_name: fakahil/PyPD

reason: import numpy,import scipy,from scipy,from astropy


text:
import numpy as np
from .base import Base

class Flatten(Base):
    """
    Flattens a contiguous range of dims into a vector.

    Example
    -------
    >>> input numpy as np
    >>> from dnetworks.layers import Flatten

    >>> input = np.zeros((2, 2, 1, 4))
    >>> flat = Flatten()
    >>> vector = flat.forward(input)
    """
    def __init__(self):
        self.type = 'Flatten'

    def forward(self, A):
        """
        Reshapes a n-dim representation into a vector, keeping all
        the values.

        Parameters
        ----------
        A : numpy.array
            Array that will be reshaped.

        Returns
        -------
        Z : numpy.array
            Reshaped array.
        """
        self.A = A
        self.Z = A.reshape(-1, self.A.shape[-1])

        return self.Z

    def backward(self, dA):
        """
        Computes the backward propagation for the flatten layer.

        Parameters
        ----------
        dA : numpy.array
            Array containing the gradientes to be backpropagated.

        Returns
        -------
        dZ : numpy.array
            Array containing the  new gradients.
        """
        dZ = dA.reshape(self.A.shape)

        return dZ

max_stars_repo_name: Javicadserres/dnetworks

max_issues_repo_name: Javicadserres/dnetworks

max_forks_repo_name: Javicadserres/DNet


text:
#!/usr/bin/env python
# Copyright (c) 2009, South African Astronomical Observatory (SAAO)        #
# All rights reserved.                                                     #

"""
specreduce is a program to reduce a set of RSS spectroscopic data.
The task will calculate the wavelength calibration for any
arc images, rectify all science images, sky subtract the data,
and produce 1-D spectra assuming the data is longslit.

There are three main methods for reduction which are None, rss,
and line.  If None, it will do no wavelength calibration.  If the
method is rss, then it will only use the RSS model to calculate
and determine the wavelength calibrate.   If it is line, then it
will go through and create the wavelength solution first,
and then correct for that.

Author                 Version      Date
-----------------------------------------------
 S. M. Crawford (SAAO)    0.1       1 April 2011

"""

from pyraf import iraf
from pyraf.iraf import pysalt
import saltsafekey as saltkey
import saltsafeio as saltio
from saltsafelog import logging
import os
import string
import sys
import glob
import pyfits
import time
import numpy

from spectools import SALTSpecError
from specprepare import prepare
from specrectify import rectify
from specsky import skysubtract
from specextract import extract, write_extract
from spectools import makesection

debug = True

# -----------------------------------------------------------
# core routine

def specreduce(images, badpixelimage=None, caltype='rss',
               function='polynomial', order=3,
               skysub=True, skysection=None, findobj=False,
               objsection=None, thresh=3.0,
               clobber=True, logfile='salt.log', verbose=True):

    with logging(logfile, debug) as log:

        # Check the input images
        infiles = saltio.argunpack('Input', images)

        # open the badpixelstruct
        if saltio.checkfornone(badpixelimage):
            badpixelstruct = saltio.openfits(badpixelimage)
        else:
            badpixelstruct = None

        # set up the section for sky estimate
        if skysection is not None:
            skysection = makesection(skysection)
        else:
            skysub = False

        # set up the section for sky estimate
        section = saltio.checkfornone(objsection)
        if section is not None:
            sections = saltio.getSection(section, iraf_format=False)
            objsection = []
            for i in range(0, len(sections), 2):
                objsection.append((sections[i], sections[i + 1]))

        # determine the wavelength solutions
        if caltype == 'line':
            calc_wavesol(infiles)

        # correct the images
        for img in infiles:
            # open the fits file
            struct = saltio.openfits(img)

            # prepare filep
            log.message('Preparing %s' % img)
            struct = prepare(struct, badpixelstruct)

            # rectify the spectrum
            log.message('Rectifying %s using %s' % (img, caltype))
            struct = rectify(
                struct,
                None,
                caltype=caltype,
                function=function,
                order=order)

            # sky subtract the spectrum
            # assumes the data is long slit and in the middle of the field
            if skysub:
                log.message('Subtracting the sky from %s' % img)

                struct = skysubtract(
                    struct,
                    method='normal',
                    section=skysection)

            # extract the spectrum
            log.message('Extracting the spectrum from %s' % img)
            print(objsection)
            aplist = extract(
                struct,
                method='normal',
                section=objsection,
                thresh=thresh)
            oimg = os.path.dirname(
                os.path.abspath(img)) + '/s' + os.path.basename(img.strip())
            ofile = oimg[:-5] + '.txt'
            write_extract(ofile, aplist, clobber=clobber)

            # write FITS file
            log.message('Writing 2-D corrected image as %s' % oimg)
            saltio.writefits(struct, oimg, clobber=clobber)
            saltio.closefits(struct)

def calc_wavesol(infiles):
    """For each file in infiles, calculate whether the file
       is an arc frame and if so, calculate the wavlength
       solution
    """
    return

# -----------------------------------------------------------
# main code

parfile = iraf.osfn("saltspec$specreduce.par")
t = iraf.IrafTaskFactory(
    taskname="specreduce",
    value=parfile,
    function=specreduce,
    pkgname='saltspec')


text:
from __future__ import absolute_import, division, print_function

from collections import Iterator
import datetime
from functools import reduce
import itertools
import operator
import warnings

import datashape
from datashape import discover, Tuple, Record, DataShape, var
from datashape.predicates import iscollection, isscalar, isrecord, istabular
import numpy as np
from odo import resource, odo
from odo.utils import ignoring, copydoc
from odo.compatibility import unicode
from pandas import DataFrame, Series

from .expr import Expr, Symbol, ndim
from .dispatch import dispatch
from .compatibility import _strtypes

__all__ = ['Data', 'Table', 'into', 'to_html']

names = ('_%d' % i for i in itertools.count(1))
not_an_iterator = []

with ignoring(ImportError):
    import bcolz
    not_an_iterator.append(bcolz.carray)

with ignoring(ImportError):
    import pymongo
    not_an_iterator.append(pymongo.collection.Collection)
    not_an_iterator.append(pymongo.database.Database)

class InteractiveSymbol(Symbol):
    """Interactive data.

    The ``Data`` object presents a familiar view onto a variety of forms of
    data.  This user-level object provides an interactive experience to using
    Blaze's abstract expressions.

    Parameters
    ----------
    data : object
        Any type with ``discover`` and ``compute`` implementations
    fields : list, optional
        Field or column names, will be inferred from datasource if possible
    dshape : str or DataShape, optional
        DataShape describing input data
    name : str, optional
        A name for the data.

    Examples
    --------
    >>> t = Data([(1, 'Alice', 100),
    ...           (2, 'Bob', -200),
    ...           (3, 'Charlie', 300),
    ...           (4, 'Denis', 400),
    ...           (5, 'Edith', -500)],
    ...          fields=['id', 'name', 'balance'])
    >>> t[t.balance < 0].name
        name
    0    Bob
    1  Edith
    """
    __slots__ = 'data', 'dshape', '_name'

    def __init__(self, data, dshape, name=None):
        self.data = data
        self.dshape = dshape
        self._name = name or (next(names)
                              if isrecord(dshape.measure)
                              else None)

    def _resources(self):
        return {self: self.data}

    @property
    def _args(self):
        return id(self.data), self.dshape, self._name

    def __setstate__(self, state):
        for slot, arg in zip(self.__slots__, state):
            setattr(self, slot, arg)

@copydoc(InteractiveSymbol)
def Data(data, dshape=None, name=None, fields=None, columns=None, schema=None,
         **kwargs):
    if columns:
        raise ValueError("columns argument deprecated, use fields instead")
    if schema and dshape:
        raise ValueError("Please specify one of schema= or dshape= keyword"
                         " arguments")

    if isinstance(data, InteractiveSymbol):
        return Data(data.data, dshape, name, fields, columns, schema, **kwargs)

    if isinstance(data, _strtypes):
        data = resource(data, schema=schema, dshape=dshape, columns=columns,
                        **kwargs)
    if (isinstance(data, Iterator) and
            not isinstance(data, tuple(not_an_iterator))):
        data = tuple(data)
    if schema and not dshape:
        dshape = var * schema
    if dshape and isinstance(dshape, _strtypes):
        dshape = datashape.dshape(dshape)
    if not dshape:
        dshape = discover(data)
        types = None
        if isinstance(dshape.measure, Tuple) and fields:
            types = dshape[1].dshapes
            schema = Record(list(zip(fields, types)))
            dshape = DataShape(*(dshape.shape + (schema,)))
        elif isscalar(dshape.measure) and fields:
            types = (dshape.measure,) * int(dshape[-2])
            schema = Record(list(zip(fields, types)))
            dshape = DataShape(*(dshape.shape[:-1] + (schema,)))
        elif isrecord(dshape.measure) and fields:
            ds = discover(data)
            assert isrecord(ds.measure)
            names = ds.measure.names
            if names != fields:
                raise ValueError('data column names %s\n'
                                 '\tnot equal to fields parameter %s,\n'
                                 '\tuse Data(data).relabel(%s) to rename '
                                 'fields' % (names,
                                             fields,
                                             ', '.join('%s=%r' % (k, v)
                                                       for k, v in
                                                       zip(names, fields))))
            types = dshape.measure.types
            schema = Record(list(zip(fields, types)))
            dshape = DataShape(*(dshape.shape + (schema,)))

    ds = datashape.dshape(dshape)
    return InteractiveSymbol(data, ds, name)

def Table(*args, **kwargs):
    """ Deprecated, see Data instead """
    warnings.warn("Table is deprecated, use Data instead",
                  DeprecationWarning)
    return Data(*args, **kwargs)

@dispatch(InteractiveSymbol, dict)
def _subs(o, d):
    return o

@dispatch(Expr)
def compute(expr, **kwargs):
    resources = expr._resources()
    if not resources:
        raise ValueError("No data resources found")
    else:
        return compute(expr, resources, **kwargs)

def concrete_head(expr, n=10):
    """ Return head of computed expression """
    if not expr._resources():
        raise ValueError("Expression does not contain data resources")
    if not iscollection(expr.dshape):
        return compute(expr)

    head = expr.head(n + 1)

    if not iscollection(expr.dshape):
        return odo(head, object)
    elif isrecord(expr.dshape.measure):
        return odo(head, DataFrame)
    else:
        df = odo(head, DataFrame)
        df.columns = [expr._name]
        return df
    result = compute(head)

    if len(result) == 0:
        return DataFrame(columns=expr.fields)
    if isrecord(expr.dshape.measure):
        return odo(result, DataFrame, dshape=expr.dshape)
    else:
        df = odo(result, DataFrame, dshape=expr.dshape)
        df.columns = [expr._name]
        return df

def repr_tables(expr, n=10):
    result = concrete_head(expr, n).rename(columns={None: ''})

    if isinstance(result, (DataFrame, Series)):
        s = repr(result)
        if len(result) > 10:
            s = '\n'.join(s.split('\n')[:-1]) + '\n...'
        return s
    else:
        return repr(result)  # pragma: no cover

def numel(shape):
    if var in shape:
        return None
    if not shape:
        return 1
    return reduce(operator.mul, shape, 1)

def short_dshape(ds, nlines=5):
    s = datashape.coretypes.pprint(ds)
    lines = s.split('\n')
    if len(lines) > 5:
        s = '\n'.join(lines[:nlines]) + '\n  ...'
    return s

def coerce_to(typ, x):
    try:
        return typ(x)
    except TypeError:
        return odo(x, typ)

def coerce_scalar(result, dshape):
    if 'float' in dshape:
        return coerce_to(float, result)
    elif 'int' in dshape:
        return coerce_to(int, result)
    elif 'bool' in dshape:
        return coerce_to(bool, result)
    elif 'datetime' in dshape:
        return coerce_to(datetime.datetime, result)
    elif 'date' in dshape:
        return coerce_to(datetime.date, result)
    else:
        return result

def expr_repr(expr, n=10):
    # Pure Expressions, not interactive
    if not expr._resources():
        return str(expr)

    # Scalars
    if ndim(expr) == 0 and isscalar(expr.dshape):
        return repr(coerce_scalar(compute(expr), str(expr.dshape)))

    # Tables
    if (ndim(expr) == 1 and (istabular(expr.dshape) or
                             isscalar(expr.dshape.measure))):
        return repr_tables(expr, 10)

    # Smallish arrays
    if ndim(expr) >= 2 and numel(expr.shape) and numel(expr.shape) < 1000000:
        return repr(compute(expr))

    # Other
    dat = expr._resources().values()
    if len(dat) == 1:
        dat = list(dat)[0]  # may be dict_values

    s = 'Data:       %s' % dat
    if not isinstance(expr, Symbol):
        s += '\nExpr:       %s' % str(expr)
    s += '\nDataShape:  %s' % short_dshape(expr.dshape, nlines=7)

    return s

@dispatch(DataFrame)
def to_html(df):
    return df.to_html()

@dispatch(Expr)
def to_html(expr):
    # Tables
    if not expr._resources() or ndim(expr) != 1:
        return to_html(repr(expr))
    return to_html(concrete_head(expr))

@dispatch(object)
def to_html(o):
    return repr(o)

@dispatch(_strtypes)
def to_html(o):
    return o.replace('\n', '<br>')

@dispatch((object, type, str, unicode), Expr)
def into(a, b, **kwargs):
    result = compute(b, **kwargs)
    kwargs['dshape'] = b.dshape
    return into(a, result, **kwargs)

def table_length(expr):
    try:
        return expr._len()
    except ValueError:
        return compute(expr.count())

Expr.__repr__ = expr_repr
Expr._repr_html_ = lambda x: to_html(x)
Expr.__len__ = table_length

def intonumpy(data, dtype=None, **kwargs):
    # TODO: Don't ignore other kwargs like copy
    result = odo(data, np.ndarray)
    if dtype and result.dtype != dtype:
        result = result.astype(dtype)
    return result

def convert_base(typ, x):
    x = compute(x)
    try:
        return typ(x)
    except:
        return typ(odo(x, typ))

Expr.__array__ = intonumpy
Expr.__int__ = lambda x: convert_base(int, x)
Expr.__float__ = lambda x: convert_base(float, x)
Expr.__complex__ = lambda x: convert_base(complex, x)
Expr.__bool__ = lambda x: convert_base(bool, x)
Expr.__nonzero__ = lambda x: convert_base(bool, x)
Expr.__iter__ = into(Iterator)

max_stars_repo_name: jdmcbr/blaze

max_issues_repo_name: jdmcbr/blaze

max_forks_repo_name: jdmcbr/blaze


text:
#By WhitemuTeam(Github)
#安装库：pip install opencv-python/pip install PySimpleGUI

import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
import PySimpleGUI as sg

#GUI
layout = [[sg.Text('请输入新人的用户名:')],
          [sg.Input()],
          [sg.Button('提交')]]

window = sg.Window('欢迎新人表情包生成', layout, icon=r'LOGO.ico') #设置窗口
event, value = window.Read() #读取输入值
name = value[0]
word = '欢迎'+name+'进群' #生成第一行句子
namehigh = len(name) #判断用户名长度

# 编辑图片路径
bk_img= cv2.imread("first.jpg")
#设置需要显示的字体
fontpath= "HarmonyOS_Sans_SC_Black.ttf"
if namehigh < 8:
	font= ImageFont.truetype(fontpath,32) # 32为字体大小
else:
	font= ImageFont.truetype(fontpath,25)
img_pil= Image.fromarray(bk_img)
draw= ImageDraw.Draw(img_pil)
#绘制文字信息# (100,300)为字体的位置，(255,255,255)为颜色RGB码
draw.text((70,230), word, font= font, fill= (0,0,0))
bk_img= np.array(img_pil)

cv2.imshow("finally",bk_img) #展示图片
cv2.waitKey()# 保存图片路径
cv2.imwrite("finally.jpg",bk_img)#输出图片


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
==================
Network Generators
==================

:Author:
    Moritz Emanuel Beber
:Date:
    2011-03-10
:Copyright:
    Copyright(c) 2011 Jacobs University of Bremen. All rights reserved.
:File:
    generators.py
"""

from . import classes
import warnings
import numpy

def rbp_network(top, bottom, p, directed=False, seed=None):
    """
    Creates a random bipartite graph with a link probability according to the
    principles of an Erdos-Renyi-like random graph.

    Parameters
    ----------
    top: int or iter
        The number of or a sequence of one population of nodes.
    bottom: int or iter
        The number of or a sequence of the other population of nodes.
    p: float
        The probability of a link being present.
    directed: bool (optional)
        Whether the generated network should be directed or undirected.
    seed: int (optional)
        Define a fixed seed for the random number generator, for repeatable
        experiments.

    Returns
    -------
    A networkx.Graph or networkx.DiGraph object. The two populations of nodes
    are recorded in the graph attributes under the keys: "top" and "bottom".
    """
    if seed:
        numpy.random.seed(seed)
    if hasattr(top, "__iter__"):
        top = set(top)
    else:
        top = range(int(top))
    if hasattr(bottom, "__iter__"):
        bottom = set(bottom)
    else:
        bottom = range(len(top), len(top) + int(bottom))
    if directed:
        network = classes.BipartiteDiGraph(name="rbp directed graph")
    else:
        network = classes.BipartiteGraph(name="rbp undirected graph")
    network.add_nodes_from(top, pop=1)
    network.add_nodes_from(bottom, pop=-1)
    uniform = numpy.random.random_sample
    for tar in top:
        for src in bottom:
            if uniform() < p:
                network.add_edge(src, tar)
    if directed:
        for tar in bottom:
            for src in top:
                if uniform() < p:
                    network.add_edge(src, tar)
    return network

max_stars_repo_name: Midnighter/Everyday-Utilities

max_issues_repo_name: Midnighter/Everyday-Utilities

max_forks_repo_name: Midnighter/Everyday-Utilities


text:
"""
Deep Q Network
"""

from collections import deque
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.autograd as autograd
import math, random

Variable = lambda *args, **kwargs: autograd.Variable(*args, **kwargs).cuda() if USE_CUDA else autograd.Variable(*args, **kwargs)
USE_CUDA = torch.cuda.is_available()

class QLearner(nn.Module):
    def __init__(self, env, args, replay_buffer):
        super(QLearner, self).__init__()

        self.batch_size = args.batch_size
        self.gamma = args.gamma
        self.num_frames = args.num_frames
        self.replay_buffer = replay_buffer
        self.env = env
        self.input_shape = self.env.observation_space.shape
        self.num_actions = self.env.action_space.n
        self.N = args.N

        self.features = nn.Sequential(
            nn.Conv2d(self.input_shape[0], 32, kernel_size=8, stride=4),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=4, stride=2),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=1),
            nn.ReLU()
        )

        self.fc = nn.Sequential(
            nn.Linear(self.feature_size(), 512),
            nn.ReLU(),
            nn.Linear(512, self.num_actions)
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

    def feature_size(self):
            return self.features(autograd.Variable(torch.zeros(1, *self.input_shape))).view(1, -1).size(1)

    def act(self, state, epsilon):
        action = []

        if random.random() > epsilon:
            state   = Variable(torch.FloatTensor(np.float32(state)).unsqueeze(0), \
                               requires_grad=True)
            ######## YOUR CODE HERE! ########
            # TODO: Given state, you should write code to get the Q value and chosen action
            # Complete the R.H.S. of the following 2 lines and uncomment them
            q_value = self.forward(state)
            action = torch.argmax(q_value).item()
            ######## YOUR CODE HERE! ########
        else:
            action = random.randrange(self.env.action_space.n)
        return action

def compute_td_loss(model_Q, model_target_Q, batch_size, gamma, replay_buffer, N):

    state, action, reward, next_state, done = replay_buffer.sample(batch_size)

    state = Variable(torch.FloatTensor(np.float32(state)))
    next_state = Variable(torch.FloatTensor(np.float32(next_state)))
    action = Variable(torch.LongTensor(action))
    reward = Variable(torch.FloatTensor(reward))
    done = Variable(torch.FloatTensor(done))

    ######## YOUR CODE HERE! ########
    # TODO: Implement the Temporal Difference Loss

    # Compute current Q value, q_func takes only state and output value for every state-action pair
    # We choose Q based on action taken.
    q_value = model_Q.forward(state)
    current_q_value = q_value.gather(1, action.view(-1,1))
    # Compute next Q value based on which action gives max Q values
    # Detach variable from the current graph since we don't want gradients for next Q to propagated
    with torch.no_grad():
        next_q_value = model_target_Q.forward(next_state).detach()
        target_q_value = reward + (1-done) * gamma * torch.max(next_q_value.detach(),dim=1)[0]
        target_q_value = target_q_value.view(-1,1)

    # loss = torch.mean((target_q_val - current_q_value)**2))
    loss = F.smooth_l1_loss(current_q_value, target_q_value)
    ######## YOUR CODE HERE! ########
    return loss

class ReplayBuffer(object):
    def __init__(self, capacity):
        #Returns a new deque object initialized left-to-right
        self.buffer = deque(maxlen=capacity)

    def push(self, state, action, reward, next_state, done):
        # state = np.expand_dims(state, 0)
        # next_state = np.expand_dims(next_state, 0)

        self.buffer.append((state, action, reward, next_state, done))

    def sample(self, batch_size):
        ######## YOUR CODE HERE! ########
        # TODO: Randomly sampling data with specific batch size from the buffer
        # Hint: you may use the python library "random".

        batch = random.sample(self.buffer, batch_size)
        state,action,reward,next_state,done = zip(*batch)

        # If you are not familiar with the "deque" python library, please google it.
        ######## YOUR CODE HERE! ########
        return np.array(state), np.array(action), np.array(reward), np.array(next_state), np.array(done)

    def __len__(self):
        return len(self.buffer)

max_stars_repo_name: MaryZolfaghar/DQNPong

max_issues_repo_name: MaryZolfaghar/DQNPong

max_forks_repo_name: MaryZolfaghar/DQNPong


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# License: BSD-3 (https://tldrlegal.com/license/bsd-3-clause-license-(revised))
# Copyright (c) 2016-2021, Cabral, Juan; Luczywo, Nadia
# Copyright (c) 2022, QuatroPe
# All rights reserved.

# =============================================================================
# DOCS
# =============================================================================

"""test for skcriteria.preprocessing.filters.

"""

# =============================================================================
# IMPORTS
# =============================================================================

import numpy as np

import pytest

import skcriteria as skc
from skcriteria.preprocessing import filters

# =============================================================================
# TEST CLASSES
# =============================================================================

def test_SKCByCriteriaFilterABC_not_provide_filters():
    class FooFilter(filters.SKCByCriteriaFilterABC):
        def _make_mask(
            self, matrix, criteria, criteria_to_use, criteria_filters
        ):
            pass

        def _coerce_filters(self, filters):
            return list(filters.keys()), list(filters.values())

    with pytest.raises(ValueError):
        FooFilter({})

def test_SKCByCriteriaFilterABC_not_implemented_make_mask():
    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    class FooFilter(filters.SKCByCriteriaFilterABC):
        def _make_mask(
            self, matrix, criteria, criteria_to_use, criteria_filters
        ):
            return super()._make_mask(
                matrix, criteria, criteria_to_use, criteria_filters
            )

        def _coerce_filters(self, filters):
            return list(filters.keys()), list(filters.values())

    tfm = FooFilter({"ROE": 1})

    with pytest.raises(NotImplementedError):
        tfm.transform(dm)

def test_SKCByCriteriaFilterABC_not_implemented_coerce_filters():
    class FooFilter(filters.SKCByCriteriaFilterABC):
        def _make_mask(
            self, matrix, criteria, criteria_to_use, criteria_filters
        ):
            pass

        def _coerce_filters(self, filters):
            return super()._coerce_filters(filters)

    with pytest.raises(NotImplementedError):
        FooFilter({"ROE": 1})

def test_SKCByCriteriaFilterABC_missing_criteria():
    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    class FooFilter(filters.SKCByCriteriaFilterABC):
        def _make_mask(
            self, matrix, criteria, criteria_to_use, criteria_filters
        ):
            pass

        def _coerce_filters(self, filters):
            return list(filters.keys()), list(filters.values())

    tfm = FooFilter({"ZARAZA": 1})

    with pytest.raises(ValueError):
        tfm.transform(dm)

    tfm = FooFilter({"ZARAZA": 1}, ignore_missing_criteria=True)

    result = tfm.transform(dm)
    assert result.equals(dm) and result is not dm

# =============================================================================
# FILTER
# =============================================================================

def test_Filter_criteria_is_not_str():

    with pytest.raises(ValueError):
        filters.Filter({1: lambda e: e > 1})

def test_Filter_filter_is_not_callable():

    with pytest.raises(ValueError):
        filters.Filter({"foo": 2})

def test_Filter():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 6, 28],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "AA", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.Filter(
        {
            "ROE": lambda e: e > 1,
            "RI": lambda e: e >= 28,
        }
    )

    result = tfm.transform(dm)
    assert result.equals(expected)

# =============================================================================
# ARITHMETIC FILTER
# =============================================================================

def test_SKCArithmeticFilterABC_not_implemented__filter():
    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    class FooFilter(filters.SKCArithmeticFilterABC):
        def _filter(self, arr, cond):
            return super()._filter(arr, cond)

    tfm = FooFilter({"ROE": 1})

    with pytest.raises(NotImplementedError):
        tfm.transform(dm)

def test_SKCArithmeticFilterABC_criteria_is_not_str():
    class FooFilter(filters.SKCArithmeticFilterABC):
        _filter = np.greater

    with pytest.raises(ValueError):
        FooFilter({1: 1})

def test_SKCArithmeticFilterABC_filter_is_not_a_number():
    class FooFilter(filters.SKCArithmeticFilterABC):
        _filter = np.greater

    with pytest.raises(ValueError):
        FooFilter({"foo": "nope"})

def test_FilterGT():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 6, 28],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "AA", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterGT({"ROE": 1, "RI": 27})

    result = tfm.transform(dm)
    assert result.equals(expected)

def test_FilterGE():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 6, 28],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "AA", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterGE({"ROE": 2, "RI": 28})

    result = tfm.transform(dm)
    assert result.equals(expected)

def test_FilterLT():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [5, 4, 26],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["JN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterLT({"ROE": 7, "CAP": 5})

    result = tfm.transform(dm)
    assert result.equals(expected)

def test_FilterLE():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [5, 4, 26],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["JN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterLE({"ROE": 5, "CAP": 4})

    result = tfm.transform(dm)
    assert result.equals(expected)

def test_FilterEQ():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [5, 4, 26],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["JN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterEQ({"ROE": 5, "CAP": 4})

    result = tfm.transform(dm)
    assert result.equals(expected)

def test_FilterNE():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [1, 7, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "MM"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterNE({"ROE": 5, "CAP": 4})

    result = tfm.transform(dm)

    assert result.equals(expected)

# =============================================================================
# SET FILTER
# =============================================================================

def test_SKCSetFilterABC_not_implemented__set_filter():
    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    class FooFilter(filters.SKCSetFilterABC):
        def _set_filter(self, arr, cond):
            return super()._set_filter(arr, cond)

    tfm = FooFilter({"ROE": [1]})

    with pytest.raises(NotImplementedError):
        tfm.transform(dm)

def test_SKCSetFilterABC_criteria_is_not_str():
    class FooFilter(filters.SKCSetFilterABC):
        def _set_filter(self, arr, cond):
            pass

    with pytest.raises(ValueError):
        FooFilter({1: [1]})

def test_SKCSetFilterABC_filter_is_not_a_number():
    class FooFilter(filters.SKCSetFilterABC):
        def _set_filter(self, arr, cond):
            pass

    with pytest.raises(ValueError):
        FooFilter({"foo": 1})

def test_FilterIn():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [5, 4, 26],
            [1, 7, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["JN", "MM"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterIn({"ROE": [5, 1], "CAP": [4, 7]})

    result = tfm.transform(dm)

    assert result.equals(expected)

def test_FilterNotIn():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterNotIn({"ROE": [5, 1], "CAP": [4, 7]})

    result = tfm.transform(dm)

    assert result.equals(expected)

# =============================================================================
# DOMINANCE
# =============================================================================

def test_FilterNonDominated():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterNonDominated(strict=False)

    result = tfm.transform(dm)

    assert result.equals(expected)

def test_FilterNonDominated_strict():

    dm = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    expected = skc.mkdm(
        matrix=[
            [7, 5, 35],
            [5, 4, 26],
            [5, 6, 28],
            [1, 7, 30],
            [5, 8, 30],
        ],
        objectives=[max, max, min],
        weights=[2, 4, 1],
        alternatives=["PE", "JN", "AA", "MM", "FN"],
        criteria=["ROE", "CAP", "RI"],
    )

    tfm = filters.FilterNonDominated(strict=True)

    result = tfm.transform(dm)

    assert result.equals(expected)


text:
from CGAL.CGAL_Triangulation_2 import Constrained_Delaunay_triangulation_2
from CGAL.CGAL_Kernel import Point_2

from stompy.grid import cgal_line_walk

import numpy as np

def test_cgal_link_walk():
    DT=Constrained_Delaunay_triangulation_2()

    xys=np.array( [ [0,0],
                    [1,0],
                    [0,1],
                    [1,2] ],'f8' )
    # in some versions, Point_2 is picky that it gets doubles,
    # not ints.
    pnts=[Point_2(xy[0],xy[1])
          for xy in xys]

    vhs=[DT.insert(p) for p in pnts]

    DT.insert_constraint(vhs[0],vhs[2])

    ##

    res0=cgal_line_walk.line_walk(DT,vhs[0],vhs[1])

    assert not DT.is_constrained(res0[0][1])
    res1=cgal_line_walk.line_walk(DT,vhs[0],vhs[2])

    assert DT.is_constrained(res1[0][1])

    assert len(cgal_line_walk.line_conflicts(DT,p1=[5,5],p2=[5,6]))==0

    assert len(cgal_line_walk.line_conflicts(DT,p1=[0.5,-0.5],p2=[0.5,0.5]))==0

    assert len(cgal_line_walk.line_conflicts(DT,p1=[-0.5,0.5],p2=[0.5,0.5]))>0

    res3=cgal_line_walk.line_conflicts(DT,p1=[0,-1],p2=[2,1])
    assert len(res3)>0
    assert res3[0][0]=='v'

max_stars_repo_name: oneconcern/stompy

max_issues_repo_name: oneconcern/stompy

max_forks_repo_name: rustychris/stompy