text:
import math
import os
import random

import numpy as np
import torch

def seed_torch(seed=42):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

class EarlyStopping:
    """Early stops the training if validation loss doesn't improve after a given patience."""
    def __init__(self, patience=7, eps=0, verbose=False, save_path='checkpoint.pt',
                 counter=0, best_score=None, val_loss_history=[], save_latest_path=None):
        """
        Args:
            patience (int): How long to wait after last time validation loss improved.
                            Default: 7
            verbose (bool): If True, prints a message for each validation loss improvement.
                            Default: False
            save_path (str): Directory for saving a model.
                             Default: "'checkpoint.pt'"
        """
        self.patience = patience
        self.eps = eps
        self.verbose = verbose
        self.save_path = save_path
        self.counter = counter
        self.best_score = best_score
        self.save_latest_path = save_latest_path
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.val_loss_history = val_loss_history

    def __call__(self, val_loss, model, preds, epoch):

        score = -val_loss
        self.val_loss_history.append(val_loss)

        if self.best_score is None:
            self.best_score = score
            self.save_checkpoint(val_loss, model, preds, epoch)
            self.save_latest(val_loss, model, preds, epoch, score)
        elif score >= self.best_score + self.eps:
            self.counter = 0
            self.best_score = score
            self.save_checkpoint(val_loss, model, preds, epoch)
            self.save_latest(val_loss, model, preds, epoch, score)
        # nanになったら学習ストップ
        elif math.isnan(score):
            self.early_stop = True
        else:
            self.counter += 1
            if self.save_latest_path is not None:
                self.save_latest(val_loss, model, preds, epoch, score)
            if self.verbose:
                print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
            if self.counter >= self.patience:
                self.early_stop = True

    def save_checkpoint(self, val_loss, model, preds, epoch):
        '''Saves model when validation loss decrease.'''
        if self.verbose:
            print(f'Validation loss decreased ({self.val_loss_min:.10f} --> {val_loss:.10f}).  Saving model ...')
        torch.save({'model': model.state_dict(), 'preds': preds, 'epoch' : epoch,
                    'best_score' : self.best_score, 'counter' : self.counter,
                    'val_loss_history': self.val_loss_history},
                   self.save_path)
        self.val_loss_min = val_loss

    def save_latest(self, val_loss, model, preds, epoch, score):
        '''Saves latest model.'''
        torch.save({'model': model.state_dict(), 'preds': preds, 'epoch' : epoch,
                    'score' : score, 'counter' : self.counter,
                    'val_loss_history': self.val_loss_history},
                   self.save_latest_path)
        self.val_loss_min = val_loss


text:
import math
import numpy as np
import cv2
import lib.config as globals
from lib.config import *

mouse_pressed = False
lengths = np.array([])
temp_data = []

class DrawLineWidget(object):
    def __init__(self, img, scaling_factor):
        self.original_image = img
        self.clone = self.original_image.copy()
        self.scaling_factor = scaling_factor
        cv2.namedWindow('Pixel-Meter')
        cv2.setWindowProperty('Pixel-Meter', cv2.WND_PROP_TOPMOST, 2)  # set window always on top
        w_x, w_y, w_w, w_h = cv2.getWindowImageRect(globals.project)
        #font_scale = img.shape[1] / w_w
        bottom_left_corner_of_text = (10, img.shape[0] - 10)
        cv2.rectangle(img, (0, img.shape[0] - 30), (int(1 * 1e4), img.shape[0]), (0, 0, 0),
                      cv2.FILLED)
        cv2.addWeighted(img, alpha, self.clone, 1 - alpha, gamma=0)
        cv2.putText(img, "Mark Object Of Reference and then specify length!", bottom_left_corner_of_text, cv2.FONT_ITALIC, 1/2, stats_color, 1)

        cv2.setMouseCallback('Pixel-Meter', self.extract_coordinates)
        self.dist = 0
        self.pixel_as_cm = 0
        # List to store start/end points
        self.image_coordinates = []

    def is_positive_numeric(self, str):
        """ Returns True is string is a number. """
        return str.replace('.', '', 1).isdigit() and float(str) > 0

    def extract_coordinates(self, event, x, y, flags, parameters):
        global mouse_pressed
        # Record ending (x,y) coordinates on left mouse bottom release
        if event == cv2.EVENT_LBUTTONUP:
            mouse_pressed = False
            end = len(self.image_coordinates) - 1
            # print('Starting: {}, Ending: {}'.format(self.image_coordinates[0], self.image_coordinates[end]))
            x1 = self.image_coordinates[0][0]
            x2 = self.image_coordinates[end][0]
            y1 = self.image_coordinates[0][1]
            y2 = self.image_coordinates[end][1]
            dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
            self.dist = dist
            # print('Distance: ' + str(dist))
            print(Cyan + "SETUP\nEnter Object of Reference size in CM (greater than 0):" + Bold + White)
            size_in_cm = input()
            while not self.is_positive_numeric(size_in_cm):
                print(Cyan + "SETUP\nInvalid size was inserted. Please enter size in CM greater than 0:" + Bold + White)
                size_in_cm = input()
            try:
                if self.pixel_as_cm == 0:
                    self.pixel_as_cm = float(size_in_cm) / float(self.dist)
                # print('one pixel as cm: {:0.3f}'.format(self.pixel_as_cm))
            except ZeroDivisionError:
                self.pixel_as_cm = 100

            # Draw line
            self.clone = self.original_image.copy()
            cv2.line(self.clone, self.image_coordinates[0], self.image_coordinates[end], (107, 209, 67), 2, cv2.LINE_AA)
            cv2.imshow("Pixel-Meter", self.clone)

            pixels_in_meter = (dist / float(size_in_cm)) * 100 / self.scaling_factor

            global lengths
            global temp_data

            lengths = np.append(lengths, pixels_in_meter)
            temp_data.append((x1, y1))
            print(RESET+40*'=')
            print(f'\t\t{Magenta}{pixels_in_meter:.1f}{Bold} pixel/meter{RESET}')
            print(40*'=')

        if mouse_pressed:
            if event == cv2.EVENT_MOUSEMOVE:
                self.image_coordinates.append((x, y))
                # Draw line
                end = len(self.image_coordinates) - 1
                self.clone = self.original_image.copy()
                cv2.line(self.clone, self.image_coordinates[0], self.image_coordinates[end], (67, 76, 209), 2, cv2.LINE_AA)
                cv2.imshow("Pixel-Meter", self.clone)

        # Record starting (x,y) coordinates on left mouse button click
        elif event == cv2.EVENT_LBUTTONDOWN:
            self.image_coordinates = [(x, y)]
            self.image_coordinates.append((x, y))
            mouse_pressed = True

        # Clear drawing boxes on right mouse button click
        elif event == cv2.EVENT_RBUTTONDOWN:
            self.clone = self.original_image.copy()

    def show_image(self):
        return self.clone

def convert(frame):
    # if temp_data:
    #     data = np.array([*temp_data])
    #     return lengths, data

    img = frame.img
    height, width = img.shape[:2]
    max_height = 900
    max_width = 900
    scaling_factor = 1
    # only shrink if img is bigger than required
    if max_height < height or max_width < width:
        # get scaling factor
        scaling_factor = max_height / float(height)
        if max_width / float(width) < scaling_factor:
            scaling_factor = max_width / float(width)
        # resize image
        img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
    draw_line_widget = DrawLineWidget(img, scaling_factor)
    while True:
        cv2.imshow('Pixel-Meter', draw_line_widget.show_image())
        key = cv2.waitKey(10)
        # Close program with keyboard 'q'
        if key == 27 or key == ord('q'):
            cv2.destroyWindow("Pixel-Meter")
            data = np.array([*temp_data])
            return lengths, data

ext: py

max_stars_repo_name: Serfati/opencovid

max_issues_repo_name: Serfati/opencovid

max_forks_repo_name: Serfati/opencovid

reason: import numpy


text:
#THIS CODE IS AN EXAMPLE HOW TO COMPUTE VARIOUS PROPERTIES

import numpy as np
import wemrr

milestones = [2.45,2.7,3.5,4.5,5.5,7.0,9.0]

#==================================================
#Compute steady state K and mean first passage time
#===================================================
K,t,Nhit = wemrr.compute_kernel(milestones)

#MFPT from r=2.7 to r=7.0
print(wemrr.MFPT(K,t,1,5))

#==================================================
#Compute equilibrium K and free energy profile
#===================================================

G = wemrr.free_energy(K,t,milestones,radial=True)

print(G)

#===================================================
#Compute MFPT with error bars
#===================================================

N_total = 300
interval = 10

K_list = wemrr.Monte_Carlo_bootstrapping(N_total,K,t,Nhit,interval)

print(K_list)
mfpt_list = []
for i in range(len(K_list)):
    mfpt_list.append(wemrr.MFPT(K_list[i],t,1,5))

mfpt_list = np.array(mfpt_list)

mfpt_mean = np.mean(mfpt_list)
mfpt_std = np.std(mfpt_list)
mfpt_err = 1.96*mfpt_std #95% confidence interval

print("Mean First Passage Time = ",mfpt_mean," +/- ",mfpt_err)

max_stars_repo_name: dhimanray/WEMRR

max_issues_repo_name: dhimanray/WEMRR

max_forks_repo_name: dhimanray/WEMRR


text:
# Instructions for subclassing numpy.ndarray are taken from (9/8/2017)
#   https://docs.scipy.org/doc/numpy-1.13.0/user/basics.subclassing.html
import numpy as np
from spacepy import pycdf
from matplotlib import pyplot as plt
from matplotlib import image as img
import pytz
import re
import pdb

class MrArray(np.ndarray):

    _count = 0
    _cache = []

    def __new__(cls, x, t=[], cache=False, name='MrArray'):
        # Convert input to ndarray (if it is not one already)
        # Cast it as an instance of MrArray
        obj = np.asarray(x).view(cls)

        # Add new attributes
        obj.x = np.array(x)
        obj.t = np.array(t, dtype='datetime64[s]')

        # Name and cache the object
        obj.name = name
        if cache:
            obj.__class__.cache(obj)

        # Finally, we must return the newly created object:
        return obj

    def __array_finalize__(self, obj):
        # ``self`` is a new object resulting from
        # ndarray.__new__(InfoArray, ...), therefore it only has
        # attributes that the ndarray.__new__ constructor gave it -
        # i.e. those of a standard ndarray.
        #
        # We could have got to the ndarray.__new__ call in 3 ways:
        # From an explicit constructor - e.g. InfoArray():
        #    obj is None
        #    (we're in the middle of the InfoArray.__new__
        #    constructor, and self.info will be set when we return to
        #    InfoArray.__new__)

        if obj is None: return
        # From view casting - e.g arr.view(InfoArray):
        #    obj is arr
        #    (type(obj) can be InfoArray)
        # From new-from-template - e.g infoarr[:3]
        #    type(obj) is InfoArray
        #
        # Note that it is here, rather than in the __new__ method,
        # that we set the default value for 'info', because this
        # method sees all creation of default objects - with the
        # InfoArray.__new__ constructor, but also with
        # arr.view(InfoArray).
        self.x = getattr(obj, 'x', None)
        self.t = getattr(obj, 't', None)
        self.name  = getattr(obj, 'name', 'MrArray')
        # We do not need to return anything

    def __del__(self):
        try:
            self.remove()
        except ValueError:
            pass

    def __getitem__(self, dt):
        # use duck typing
        # is it an integer or sequence?
        try:
            obj = super(MrArray, self).__getitem__(dt)

            # Scalar index
            # If scalar indices were given, obj will be a scalar (no attributes)
            if np.isscalar(dt):
                pass

            # Slice multiple dimensions
            # Use the first dimensional slice to slice the time array
            elif isinstance(dt, tuple):
                try:
                    obj.t = self.t[dt[-1]]
                except AttributeError:
                    pass

            # Index or slice single dimension
            else:
                obj.t = self.t[dt]

            return obj

        except (TypeError, IndexError) as e:
            raise

        # Assume a time was given
        s = self.t.get_index_time[dt]
        obj = super(MrArray, self).__getitem(s)
        obj.t = self.t[s]

        return obj

        # it must be datetime
        # make sure it is a numpy datetime
        dt = np.datetime64(dt,'s')

        # Return exact match
        idx = dt == self.t
        if np.any(idx):
            return self[idx][0]

        # Nearest neighbor interpolation
        idx = np.argwhere(dt < self.t)
        idx = idx[0][0]
        x_lo = self[idx - 1, ...]
        x_hi = self[idx, ...]
        t_lo = self.t[idx - 1]
        t_hi = self.t[idx]

        return x_lo + (x_hi - x_lo) * ((dt - t_lo) / (t_hi - t_lo))

    def get_item_time(self, idx):
        pass

    def __check_time(self, t):
        return np.all(self.t == t)

#    def __mul__(self, value):
#        a = 1

    def cache(self, index=None, clobber=False):
        # Store an instance in the array cache. Do not allow
        # arrays to have the same name.
        names = []
        names = [arr[1].name for arr in enumerate(self.__class__._cache)]

        # Check if the name already exists. When clobbering,
        # delete the existing value. Otherwise, if the name
        # exists, find the highest number in <name>_# that
        # makes <name> unique.
        if self.name in names:

            if clobber:
                idx = names.index(self.name)
                del self.__class__._cache[idx]

            else:
                rex = re.compile(self.name + '(_(\d+))?$')
                num = [re.match(rex, x).group(2) for x in names if re.match(rex, x)]
                num = max([0 if i is None else int(i) for i in num])
                self.name += '_' + str(num+1)

        # Add to the variable cache
        if index is None:
            self.__class__._cache.append(self)
        else:
            self.__class__._cache.insert(index, self)

    def image(self, axes=[], colorbar=True, show=True):
        # Create the figure
        if not axes:
            fig, axes = plt.subplots(nrows=1, ncols=1)

        # Convert time to seconds and reshape to 2D arrays
        dt2sec = np.vectorize(lambda x: x.total_seconds())
        x0 = dt2sec(self.x0.x - self.x0.x[0])
        x1 = self.x1
        if x0.ndim == 1:
            x0 = np.repeat(x0[:, np.newaxis], self.shape[1], axis=1)
        if x1.ndim == 1:
            x1 = np.repeat(x1[np.newaxis, :], self.shape[0], axis=0)

        # Format the image
        #   TODO: Use the delta_plus and delta_minus attributes to create
        #         x0 and x1 arrays so that x does not lose an element
        data = self.x[0:-1,0:-1]
        if hasattr(self, 'scale'):
            data = np.ma.log(data)

        # Create the image
        im = axes.pcolorfast(x0, x1, data, cmap='nipy_spectral')
        axes.images.append(im)

        # Set plot attributes
        self.__plot_apply_xattrs(axes, self.x0)
        self.__plot_apply_yattrs(axes, x1)

        # TODO: Add a colorbar
        if colorbar:
            cb = plt.colorbar(im)
            try:
                cb.set_label(self.title)
            except AttributeError:
                pass

        # Display the plot
        if show:
            plt.ion()
            plt.show()

    def iscached(self):
        # Determine if the instance has been cached
        return self in self.__class__._cache

    def plot(self, axes=[], legend=True, show=True):
        if not axes:
            axes = plt.axes()

        # Plot the data
        axes.plot(self.x0.x, self.x)

        # Set plot attributes
        self.__plot_apply_xattrs(axes, self.x0)
        self.__plot_apply_yattrs(axes, self)

        if legend:
            try:
                axes.legend(self.label)
            except AttributeError:
                pass

        # Display the plot
        if show:
            plt.ion()
            plt.show()

    def remove(self):
        self.__class__._cache.remove(self)

    @staticmethod
    def __plot_apply_xattrs(ax, x):
        try:
            ax.XLim = x.lim
        except AttributeError:
            pass

        try:
            ax.set_title(x.plot_title)
        except AttributeError:
            pass

        try:
            ax.set_xscale(x.scale)
        except AttributeError:
            pass

        try:
            ax.set_xlabel(x.title.replace('\n', ' '))
        except AttributeError:
            pass

    @staticmethod
    def __plot_apply_yattrs(ax, y):
        try:
            ax.YLim = y.lim
        except AttributeError:
            pass

        try:
            ax.set_title(y.plot_title)
        except AttributeError:
            pass

        try:
            ax.set_yscale(y.scale)
        except AttributeError:
            pass

        try:
            ax.set_ylabel(y.title)
        except AttributeError:
            pass

    @classmethod
    def get(cls, arrlist=[]):
        # Return all variables
        if not arrlist:
            return cls._cache

        # Output array
        arrs = []

        # Get an array from the cache
        for item in arrlist:
            if isinstance(item, int):
                arrs.append(cls._cache[item])

            elif isinstance(item, str):
                names = cls.names()
                arrs.append(cls._cache[names.index(item)])

            elif item is MrArray:
                arrs.apend(item)

            else:
                raise TypeError('arrlist must contain integers, strings, or MrArray objects.')

        # Return a single object if only one input was given
        if not isinstance(item, list):
            arrs = arrs[0]

        return arrs

    @classmethod
    def names(cls):
        names = [arr.name for arr in cls._cache]
        return names

        # Print the index and name of each item in the cache.
        # Use a space to pad between index and name; index is
        # right-justified and padded on the left while name
        # would be left-justified and padded on the right. How
        # then to pad between them?
        if len(cls._cache) == 0:
            print('The cache is empty.')
        else:
            print('{:4}{:3}{}'.format('Index', '', 'Name'))
            for idx, item in enumerate(cls._cache):
                print('{0:>4d}{1:<4}{2}'.format(idx, '', item.name))

def from_cdf(files, variable, cache=False, clobber=False, name=''):
    """
    Read variable data from a CDF file.

    Parameters
    ==========
    filenames : str, list
        Name of the CDF file(s) to be read.
    variable : str
        Name of the variable to be read.

    Returns
    =======
    vars : mrarry
        A mrarray object.
    """
    global cdf_vars
    global file_vars

    if len(files) == 0:
        raise ValueError('at least one valid file name must be given.')
    if isinstance(files, str):
        files = [files]

    # Read variables from files
    cdf_vars = {}
    for file in files:
        file_vars = {}
        with pycdf.CDF(file) as f:
            var = __from_cdf_read_var(f, variable)

    # Cache
    if cache:
        var.cache(clobber=clobber)

    return var

def __from_cdf_read_var(cdf, varname):
    """
    Read data and collect metadata from a CDF variable.

    Parameters
    ==========
    cdf: : object
        A spacepy.pycdf.CDF object of the CDF file being read.
    varname : str
        The name of the CDF variable to be read
    """
    global cdf_vars
    global file_vars

    # Data has already been read from this CDF file
    if varname in file_vars:
        var = file_vars[varname]

    else:
        # TODO
        #   - Create a time object that inherits from numpy.datetime64 that can
        #   - be cached the same way as MrArray
#        if cdf_var.type() in (pycdf.const.CDF_EPOCH.value, pycdf.const.CDF_EPOCH16.value,
#                              pycdf.const.CDF_TIME_TT2000.value):
#            var = np.array(data, dtype='datetime64')
#        else:
#            var = MrArray(data)

        # Read the data
        var = MrArray(cdf[varname][...])
        var.name = varname
        var.rec_vary = cdf[varname].rv

        # Append to existing data
        if varname in cdf_vars and cdf[varname].rv:
            d0 = cdf_vars[varname]
            np.append(var.y, d0, 0)

        # Mark as read
        #  - Prevent infinite loop. Must save the variable in the registry
        #  so that variable attributes do not try to read the same variable
        #  again.
        cdf_vars[varname] = var
        file_vars[varname] = var

        # Read the metadata
        var = __from_cdf_read_var_attrs(cdf, var)
        var = __from_cdf_attrs2gfxkeywords(cdf, var)

    return var

def __from_cdf_read_var_attrs(cdf, var):
    """
    Read metadata from a CDF variable.

    Parameters
    ==========
    cdf: : object
        A spacepy.pycdf.CDF object of the CDF file being read.
    var : object
        The MrArray object that will take on CDF variable attributes
        as object attributes.

    Returns
    ==========
    var : object
        The input tsdata object with new attributes.
    """

    # CDF variable and properties
    cdf_var = cdf[var.name]
    var.cdf_name = cdf_var.name()
    var.cdf_type = cdf_var.type()

    # Variable attributes
    for vattr in cdf_var.attrs:

        # Follow pointers
        attrvalue = cdf_var.attrs[vattr]
        if isinstance(attrvalue, str) and attrvalue in cdf:
            varname = attrvalue
            attrvalue = __from_cdf_read_var(cdf, varname)

        # Set the attribute value
        if vattr == 'DELTA_PLUS_VAR':
            var.delta_plus = attrvalue
        elif vattr == 'DELTA_MINUS_VAR':
            var.delta_minus = attrvalue
        elif vattr == 'DEPEND_0':
            var.x0 = attrvalue
        elif vattr == 'DEPEND_1':
            var.x1 = attrvalue
        elif vattr == 'DEPEND_2':
            var.x2 = attrvalue
        elif vattr == 'DEPEND_3':
            var.x3 = attrvalue
#        elif vattr == 'LABL_PTR_1':
#            var.label = attrvalue
        elif vattr == 'LABL_PTR_2':
            var.label2 = attrvalue
        elif vattr == 'LABL_PTR_3':
            var.label3 = attrvalue
        else:
            setattr(var, vattr, attrvalue)

    return var

def __from_cdf_attrs2gfxkeywords(cdf, var):
    """
    Set plotting attributes for the variable based on CDF metadata.

    Parameters
    ==========
    cdf: : object
        A spacepy.pycdf.CDF object of the CDF file being read.
    var : object
        The tsdata object that will take on CDF variable attributes
        as object attributes.

    Returns
    ==========
    var : object
        The input MrArray object with new attributes.
    """

    # Extract the attributes for ease of access
    cdf_attrs = cdf[var.name].attrs

    # Plot title
    if 'FIELDNAM' in cdf_attrs:
        var.plot_title = cdf_attrs['FIELDNAM']
    elif 'CATDESC' in cdf_attrs:
        var.plot_title = cdf_attrs['CATDESC']

    # Axis label
    title = ''
    if 'LABLAXIS' in cdf_attrs:
        title = cdf_attrs['LABLAXIS']
    elif 'FIELDNAM' in cdf_attrs:
        title = cdf_attrs['FIELDNAM']
    if 'UNITS' in cdf_attrs:
        title = title + '\n(' + cdf_attrs['UNITS'] + ')'
    var.title = title

    # Legend label
    if 'LABL_PTR_1' in cdf_attrs:
        var.label = cdf[cdf_attrs['LABL_PTR_1']][...]

    # Axis scaling
    if 'SCALETYP' in cdf_attrs:
        var.scale = cdf_attrs['SCALETYP']

    return var

def plot(variables=[], layout=[]):
    # Get the object references
    vars = MrArray.get(variables)

    # Plot layout
    if not layout:
        layout = [len(vars), 1]

    # Setup plot
    fig, axes = plt.subplots(nrows=layout[0], ncols=layout[1])

    # Plot each variable
    for idx, var in enumerate(vars):
        if hasattr(var, 'x1'):
            var.image(axes=axes[idx], show=False)
        elif hasattr(var, 'x0'):
            var.plot(axes=axes[idx], show=False)

    # Display the figure
    plt.show()
    return axes

def main1():
    x = [1,2,3,4]
    PST = pytz.timezone('America/Los_Angeles')
    dt = [datetime(2013,1,1,12,30,0,tzinfo=PST),
          datetime(2013,1,1,13,30,0,tzinfo=PST),
          datetime(2013,1,1,14,30,0,tzinfo=PST),
          datetime(2013,1,1,15,30,0,tzinfo=PST)]
    ts = MrArray(x,dt)
    ts2 = ts + 1
    print( ts2, ts2.x, ts2.t, ts2.__class__, ts2.__class__.__bases__)
    print( ts)
    print( ts[0], ts[1], ts[2], ts[3])
    print( ts[datetime(2013,1,1,15,30,0,tzinfo=PST)])
    print( ts[datetime(2013,1,1,14,45,0,tzinfo=PST)])
    print( ts.__class__)
    print( ts.__class__.__bases__)

def test_plot_cached_vars():
    from pymms.pymms import mrmms_sdc_api as api

    # Get the data file
    sdc = api.MrMMS_SDC_API('mms1', 'fpi', 'brst', 'l2',
                            optdesc='des-moms',
                            start_date='2015-12-06T00:23:04',
                            end_date='2015-12-06T00:25:34')
    file = sdc.Download()

    # Variable name
    n_vname = 'mms1_des_numberdensity_brst'
    v_vname = 'mms1_des_bulkv_gse_brst'
    espec_vname = 'mms1_des_energyspectr_omni_brst'

    # Read data
    n = from_cdf(file, n_vname, cache=True)
    V = from_cdf(file, v_vname, cache=True)
    ESPec = from_cdf(file, espec_vname, cache=True)

    # Plot data
    axes = plot()

    return file

if __name__ == '__main__':
    main_mms_fpi()

max_stars_repo_name: argallmr/pyarray

max_issues_repo_name: argallmr/pyarray

max_forks_repo_name: argallmr/pyarray

reason: import numpy,from numpy


text:
from __future__ import print_function
import os
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from data import cfg_mnetv2, cfg_re50
from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
import cv2
from models.retinaface import RetinaFace
from utils.box_utils import decode, decode_landm
from utils.timer import Timer
from tqdm import tqdm

def check_keys(model, pretrained_state_dict):
    ckpt_keys = set(pretrained_state_dict.keys())
    model_keys = set(model.state_dict().keys())
    used_pretrained_keys = model_keys & ckpt_keys
    unused_pretrained_keys = ckpt_keys - model_keys
    missing_keys = model_keys - ckpt_keys
    print('Missing keys:{}'.format(len(missing_keys)))
    print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
    print('Used keys:{}'.format(len(used_pretrained_keys)))
    assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
    return True

def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()}

def load_model(model, pretrained_path, load_to_cpu):
    print('Loading pretrained model from {}'.format(pretrained_path))
    if load_to_cpu:
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
    else:
        device = torch.cuda.current_device()
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
    if "state_dict" in pretrained_dict.keys():
        pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
    else:
        pretrained_dict = remove_prefix(pretrained_dict, 'module.')
    check_keys(model, pretrained_dict)
    model.load_state_dict(pretrained_dict, strict=False)
    return model

model = "mobileNet"
cpu = False

if __name__ == '__main__':
    torch.set_grad_enabled(False)
    _t = {'forward_pass': Timer(), 'misc': Timer()}
    confidence_threshold = .02
    nms_threshold = 0.04

    origin_size = True
    if model == "resnet50":
        cfg = cfg_re50
        if cfg['gender']:
            weight_path = './weights/Resnet50_Gender_Final.pth'
        else:
            weight_path = './weights/Resnet50_Final.pth'
    else:
        cfg = cfg_mnetv2
        weight_path = './weights/MobileNet_v2_Final.pth'
    # net and model
    net = RetinaFace(cfg=cfg)
    net = load_model(net, weight_path, cpu)

    net.eval()
    print('Finished loading model!')

    cudnn.benchmark = True
    device = torch.device("cpu" if cpu else "cuda")
    net = net.to(device)
    ori_path = "/data/datasets/FDDB/originalPics"
    file_path = "/data/datasets/FDDB/FDDB-folds"
    for i in range(1, 11):
        with open(os.path.join(file_path, "FDDB-fold-{:02d}.txt".format(i)), "r") as f, \
                open("FDDB_result/FDDB-fold-{:02d}_{}.txt".format(i, model), "w") as fw:
            lines = f.readlines()
            for line in tqdm(lines):
                line = line.rstrip('\n')
                fw.write(line + "\n")
                image_path = os.path.join(ori_path, line) + ".jpg"
                img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR)
                img = np.float32(img_raw)

                im_height, im_width, _ = img.shape
                scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
                img -= (104, 117, 123)
                img /= (57, 57, 58)
                img = img.transpose(2, 0, 1)
                img = torch.from_numpy(img).unsqueeze(0)
                img = img.to(device)
                scale = scale.to(device)

                _t['forward_pass'].tic()
                if cfg['gender']:
                    loc, conf, landms, gender = net(img)  # forward pass
                else:
                    loc, conf, landms = net(img)  # forward pass

                _t['forward_pass'].toc()
                _t['misc'].tic()
                priorbox = PriorBox(cfg, image_size=(im_height, im_width))
                priors = priorbox.forward()
                priors = priors.to(device)
                prior_data = priors.data
                boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
                boxes = boxes * scale
                boxes = boxes.cpu().numpy()
                scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
                if cfg['gender']:
                    genders = gender.squeeze(0).data.cpu().numpy()

                landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
                scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                                       img.shape[3], img.shape[2], img.shape[3], img.shape[2],
                                       img.shape[3], img.shape[2]])
                scale1 = scale1.to(device)
                landms = landms * scale1
                landms = landms.cpu().numpy()

                # ignore low scores
                inds = np.where(scores > confidence_threshold)[0]
                boxes = boxes[inds]
                landms = landms[inds]
                scores = scores[inds]
                if cfg['gender']:
                    genders = genders[inds]
                # keep top-K before NMS
                order = scores.argsort()[::-1]
                # order = scores.argsort()[::-1][:args.top_k]
                boxes = boxes[order]
                landms = landms[order]
                scores = scores[order]
                if cfg['gender']:
                    genders = genders[order]
                # do NMS
                dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
                keep = py_cpu_nms(dets, nms_threshold)
                # keep = nms(dets, args.nms_threshold,force_cpu=args.cpu)
                dets = dets[keep, :]
                landms = landms[keep]
                if cfg['gender']:
                    genders = genders[keep]
                # keep top-K faster NMS
                # dets = dets[:args.keep_top_k, :]
                # landms = landms[:args.keep_top_k, :]
                if cfg['gender']:
                    dets = np.concatenate((dets, landms, genders * 10000), axis=1).astype(np.float32)
                else:
                    dets = np.concatenate((dets, landms), axis=1).astype(np.float32)

                _t['misc'].toc()

                vis_thres = 0.5
                fw.write(str(len(dets)) + "\n")
                for b in dets:
                    x = int(b[0])
                    y = int(b[1])
                    w = int(b[2]) - int(b[0])
                    h = int(b[3]) - int(b[1])
                    confidence = str(b[4])

                    fw.write("{} {} {} {} {}\n".format(x, y, w, h, confidence))
                    fw.flush()

max_stars_repo_name: TalkUHulk/RetinaFace-with-Gender

max_issues_repo_name: TalkUHulk/RetinaFace-with-Gender

max_forks_repo_name: TalkUHulk/RetinaFace-with-Gender


text:
import mmcv
import numpy as np
import torch

from .flops_hook import profile
from collections.abc import Sequence

def params_count(model):
    """
    Compute the number of parameters.
    Args:
        model (model): model to count the number of parameters.
    """
    return np.sum([p.numel() for p in model.parameters()]).item()

def gpu_mem_usage():
    """
    Compute the GPU memory usage for the current device (MB).
    """
    mem_usage_bytes = torch.cuda.max_memory_allocated()
    return mem_usage_bytes / (1024 * 1024)

def get_flop_stats(model, input):
    """
    Compute the gflops for the current model given the config.
    Args:
        model (model): model to compute the flop counts.
        cfg (CfgNode): configs. Details can be found in
            slowfast/config/defaults.py
        is_train (bool): if True, compute flops for training. Otherwise,
            compute flops for testing.
    Returns:
        float: the total number of gflops of the given model.
    """
    input['img_group'] = input['img_group'].cuda()
    if 'num_crops' in input:
        input['num_crops'] = input['num_crops'].unsqueeze(0).cuda()

    if 'num_clips' in input:
        input['num_clips'] = input['num_clips'].unsqueeze(0).cuda()

    total_flops, total_params = profile(model, input, verbose=False)
    return total_flops, total_params

def to_tensor(data):
    """Convert objects of various python types to :obj:`torch.Tensor`.

    Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
    :class:`Sequence`, :class:`int` and :class:`float`.
    """
    if isinstance(data, torch.Tensor):
        return data
    elif isinstance(data, np.ndarray):
        return torch.from_numpy(data)
    elif isinstance(data, Sequence) and not mmcv.is_str(data):
        return torch.tensor(data)
    elif isinstance(data, int):
        return torch.LongTensor([data])
    elif isinstance(data, float):
        return torch.FloatTensor([data])
    else:
        raise TypeError('type {} cannot be converted to tensor.'.format(
            type(data)))


text:
"""
Example: Compute DDK3 filtered grids for ocean applications

The test data required is available on the ITSG FTP server:
ftp://ftp.tugraz.at/outgoing/ITSG/GRACE/ITSG-Grace2018/monthly

Download the potential coefficients to a directory of your choice and change the variables `model_dir` and
`solution_dir` accordingly.

The example script performs the following steps for a time series of GRACE monthly solutions:

- replace c20 with an SLR estimate
- add degree one coefficients
- remove GIA signal
- restored AOD1B ocean bottom pressure to get the full ocean mass

- reduce the mean of the resulting time series
- compute gridded equivalent water height on a 0.5x0.5 degree geographic grid
- write the grid time series to a netCDF file

"""

import l3py
import numpy as np
import datetime as dt

time_start = dt.datetime(2002, 1, 1)
time_end = dt.datetime(2017, 1, 1)

epochs = [t for t in l3py.time.month_iterator(time_start, time_end, use_middle=True)]

model_dir = 'monthly_background'
solution_dir = 'monthly_n96'

monthly_solutions = []
for t in epochs:

    try:
        grace_monthly = l3py.io.loadgfc(t.strftime(solution_dir + '/ITSG-Grace2018_n96_%Y-%m.gfc'))

        slr_c20 = l3py.io.loadgfc(t.strftime(model_dir + '/model_c20_%Y-%m.gfc'))
        degree1 = l3py.io.loadgfc(t.strftime(model_dir + '/model_degree1_%Y-%m.gfc'))
        gia = l3py.io.loadgfc(t.strftime(model_dir + '/model_glacialIsostaticAdjustment_%Y-%m.gfc'))
        obp = l3py.io.loadgfc(t.strftime(model_dir + '/model_oceanBottomPressure_%Y-%m.gfc'))

        gia.truncate(grace_monthly.nmax())
        obp.truncate(grace_monthly.nmax())

        grace_monthly.replace_c20(slr_c20)
        grace_monthly += degree1

        grace_monthly -= gia
        grace_monthly += obp

        grace_monthly.epoch = t
        monthly_solutions.append(grace_monthly)

    except FileNotFoundError as e:
        print(e)
        continue

monthly_solutions = np.array(monthly_solutions)
monthly_solutions -= np.mean(monthly_solutions)

shc_filter = l3py.filter.DDK(3)

base_grid = l3py.grid.GeographicGrid(dlon=0.5, dlat=0.5)

grids = []
for grace_monthly in monthly_solutions:
    print(grace_monthly.epoch.strftime('Compute filtered grid for %Y-%m.'))

    filtered_solution = shc_filter.filter(grace_monthly)
    filtered_grid = filtered_solution.to_grid(base_grid, kernel='ewh')
    grids.append(filtered_grid)

l3py.io.savegrids('output.nc', grids)


text:
###########################
### IMPORT DEPENDENCIES ###
###########################
from typing import Sequence
import cv2
import numpy as np
import os
from matplotlib import pyplot as plt
import time
import mediapipe as mp
import tensorflow as tf

mp_holistic = mp.solutions.holistic
mp_drawing = mp.solutions.drawing_utils

actions = np.array(["xin_chao","tam_biet","cam_on","vui","khoe","bo","me","toi","ban","gap","ten","kcg","co","khong","i_love_you","ong","hoc","vuii","ban_be","sach","doc"]) # Actions that we try to detect
no_sequences = 100                                  # Number of videos that we want to collect for our dataset
sequence_length = 30                                # Number of frames per video

model_path = "model_training/MODEL_LSTM_5.h5"
model = tf.keras.models.load_model(model_path)
########################
### DEFINE FUNCTIONS ###
########################
def mediapipe_detection(image,model):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # color conversion BGR to RGB
    image.flags.writeable = False                  # image is not writeable
    results = model.process(image)                 # Make prediction
    image.flags.writeable = True                   # Image is writeable again
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # color conversion RGB to BGR
    return image, results

def draw_landmarks(image, results):
    mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_TESSELATION)      # draw the connection map of face landmarks
    mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)          # draw the connection map of pose landmarks
    mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)     # draw the connection map of left-hand landmarks
    mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)    # draw the connection map of right-hand landmarks

def draw_landmarks_styled(image, results):
    # draw the connection map of face landmarks
    mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_TESSELATION,
                                    mp_drawing.DrawingSpec(color=(80,120,10), thickness=1, circle_radius=1),    # dot color
                                    mp_drawing.DrawingSpec(color=(80,255,120), thickness=1, circle_radius=1))   # line color

    # draw the connection map of pose landmarks
    mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS,
                                    mp_drawing.DrawingSpec(color=(120,30,10), thickness=2, circle_radius=4),
                                    mp_drawing.DrawingSpec(color=(120,70,110), thickness=2, circle_radius=2))

    # draw the connection map of left-hand landmarks
    mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
                                    mp_drawing.DrawingSpec(color=(122,160,255), thickness=2, circle_radius=4),
                                    mp_drawing.DrawingSpec(color=(114,128,250), thickness=2, circle_radius=2))

    # draw the connection map of right-hand landmarks
    mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS,
                                    mp_drawing.DrawingSpec(color=(154,250,0), thickness=2, circle_radius=4),
                                    mp_drawing.DrawingSpec(color=(87,139,46), thickness=2, circle_radius=2))

def extract_keypoints(results):
    face = np.array([[res.x, res.y, res.z] for res in results.face_landmarks.landmark]).flatten() if results.face_landmarks else np.zeros(468*3)
    pose = np.array([[res.x, res.y, res.z, res.visibility] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33*4)
    lefthand = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
    righthand = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
    return np.concatenate([pose,face,lefthand,righthand])

def prob_visualization(res, actions, input_frame, colors):
    output_frame = input_frame.copy()
    for num, prob in enumerate(res):
        cv2.rectangle(output_frame, (0,60+num*40), (int(prob*100),90+num*40), colors[num], -1)
        cv2.putText(output_frame, actions[num], (0, 85+num*40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)

    return output_frame

####################
### MAIN PROCESS ###
####################
sequence = []
sentence = []
predictions = []
threshold = 0.8
# colors = [(245,117,16), (117,245,16), (16,117,245)]

cap = cv2.VideoCapture(0)
# Set mediapipe model
with mp_holistic.Holistic(min_detection_confidence=0.5,
                          min_tracking_confidence=0.5) as holistic:
    while cap.isOpened():

        # Read frame
        ret, frame = cap.read()

        # Make detections
        image, results = mediapipe_detection(frame, holistic)
        # print(results)

        # Draw landmarks
        # draw_landmarks_styled(image, results)

        # Prediction logic
        keypoints = extract_keypoints(results)
        sequence.append(keypoints)
        sequence = sequence[-30:]

        if len(sequence) == 30:
            res = model.predict(np.expand_dims(sequence, axis=0))[0]
            print(actions[np.argmax(res)])
            predictions.append(np.argmax(res))

        # Visualization logic
            if np.unique(predictions[-10:])[0] == np.argmax(res) and np.argmax(res)!=11:
                if res[np.argmax(res)] > threshold:

                    if len(sentence) > 0:
                        if actions[np.argmax(res)] != sentence[-1]:
                            sentence.append(actions[np.argmax(res)])
                    else:
                        sentence.append(actions[np.argmax(res)])

            if len(sentence) > 5:
                sentence = sentence[-5:]

            # image = prob_visualization(res, actions, image, colors)

        cv2.rectangle(image, (0,0), (640,40), (120,70,110), -1)
        # print(sentence[-5:])
        cv2.putText(image, ' '.join(sentence), (3,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)

        # Show
        cv2.imshow("MediaPipe Holistic", image)

        # Break
        if cv2.waitKey(27) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

max_stars_repo_name: kristrg/VSL-detection

max_issues_repo_name: kristrg/VSL-detection

max_forks_repo_name: kristrg/VSL-detection


text:
import numpy as np
import os
from os import path
import shutil

def materials_het():
    '''
    Cross-sections from Brantley and Larsen. The Simplified P3
    Approximation. 2000.

    Returns:
    --------
    materials: [dictionary]
        dictionary that contains the materials and their respective
        cross-sections.
        * main keys: materials (fuel, moderator)
        * secondary keys: constants (totxs, nsf, scxs)
    '''

    constants = {
        'fuel': {
                'TOT': np.array([1.5]),
                'NSF': np.array([0.24]),
                'SP0': np.array([1.35]),
                },
        'moderator': {
                'TOT': np.array([1.0]),
                'NSF': np.array([0.0]),
                'SP0': np.array([0.93]),
                     },
    }

    constants2 = prepare_xs(constants)

    return constants2

def prepare_xs(constants):
    '''
    This function prepares the cross-sections and outputs a dictionary
    with all the information required by Cerberus.

    Parameters:
    ----------
    constants: [dictionary]
        cross-section data
        primary keys: name of the material
        secondary keys: constants
    Returns:
    --------
    constants2: [dictionary]
        primary keys: name of the material
        secondary keys: constants required by Cerberus
    '''

    constants2 = {}
    for mat in constants.keys():
        G = len(constants[mat]['TOT'])
        totxs = constants[mat]['TOT']
        try:
            s0xs = constants[mat]['SP0']
        except KeyError:
            s0xs = np.zeros(G*G)
        try:
            s1xs = constants[mat]['SP1']
        except KeyError:
            s1xs = np.zeros(G*G)
        try:
            s2xs = constants[mat]['SP2']
        except KeyError:
            s2xs = np.zeros(G*G)
        try:
            s3xs = constants[mat]['SP3']
        except KeyError:
            s3xs = np.zeros(G*G)

        rem0xs = totxs - s0xs.reshape(G, G).diagonal()
        rem1xs = totxs - s1xs.reshape(G, G).diagonal()
        rem2xs = totxs - s2xs.reshape(G, G).diagonal()
        rem3xs = totxs - s3xs.reshape(G, G).diagonal()

        constants2[mat] = {}
        constants2[mat]['BETA_EFF'] = np.zeros(8)
        constants2[mat]['CHID'] = np.zeros(G)
        constants2[mat]['CHIP'] = np.zeros(G)
        constants2[mat]['CHIT'] = np.zeros(G)
        constants2[mat]['CHIT'][0] = 1.
        constants2[mat]['COUPLEXSA'] = 2*rem0xs
        constants2[mat]['COUPLEXSB'] = 2./5*rem0xs
        constants2[mat]['DIFFCOEFA'] = 1./3/rem1xs
        constants2[mat]['DIFFCOEFB'] = 9./35/rem3xs
        try:
            constants2[mat]['FISS'] = constants[mat]['FISS']
        except KeyError:
            constants2[mat]['FISS'] = constants[mat]['NSF']/2.4
        constants2[mat]['INVV'] = np.zeros(G)
        constants2[mat]['KAPPA'] = 200*np.ones(G)
        constants2[mat]['LAMBDA'] = np.zeros(8)
        try:
            constants2[mat]['NSF'] = constants[mat]['NSF']
        except KeyError:
            constants2[mat]['NSF'] = np.zeros(G)
        constants2[mat]['REMXSA'] = rem0xs
        constants2[mat]['REMXSB'] = rem2xs + 4./5*rem0xs
        constants2[mat]['SP0'] = s0xs

    return constants2

def output_xs(outdir, temp, materials):
    '''
    This function outputs the dictionary with the material cross-sections
    into the Cerberus and moltres readable text files.

    Parameters:
    -----------
    outdir: [string]
        directory that will hold the cross-section files
    temp: [float]
        temperature at which the cross-sections were obtained
    materials: [dictionary]
        contains the cross-section informations
        primary keys: name of the material
        secondary keys: constants
    Return:
    -------
    None
    '''

    for currentMat in materials.keys():
        for data in materials[currentMat].keys():
            with open(outdir + '/' + currentMat +
                      '_' + data + '.txt', 'a') as fh:

                strData = materials[currentMat][data]
                strData = ' '.join(
                    [str(dat) for dat in strData]) if isinstance(
                    strData, np.ndarray) else str(strData)
                fh.write(str(temp) + ' ' + strData)
                fh.write('\n')
    return None

if __name__ == "__main__":
    temp = 300

    # produces cross-sections for the heterogeneous case
    outdir = 'xs'
    if os.path.exists(outdir):
        shutil.rmtree(outdir)
    os.mkdir(outdir)
    materials = materials_het()
    output_xs(outdir, temp, materials)


text:
"""Demo example file that shows how to add or duplicate data channels in a
NixIO Glitter2 h5 file.
"""

import nixio
from glitter2.storage.data_file import DataFile
from os.path import join, dirname
import shutil
from kivy_garden.painter import PaintCircle
import numpy as np
import math

# first copy the file before editing
src_data_file = join(dirname(__file__), 'video_data.h5')
data_file = join(dirname(__file__), 'video.h5')
shutil.copy(src_data_file, data_file)

# open the nix data file for editing
nix_file = nixio.File.open(data_file, nixio.FileMode.ReadWrite)
# create our data controller
data_file = DataFile(nix_file=nix_file)
# load the data from the file
data_file.open_file()

# check whether we have seen all frame. If we haven't then the timestamps and
# channel data may be discontinuous, otherwise we can just read the data from
# the single timestamps array. For this demo file it should be True
assert data_file.saw_all_timestamps

# get video metadata
video_metadata = data_file.video_metadata_dict
width, height = video_metadata['src_vid_size']

# get the timestamps
timestamps = np.array(data_file.timestamps)

# Duplicate the event channel. There should only be one event channel
# in the demo file so get that one
event = list(data_file.event_channels.values())[0]
new_event = data_file.duplicate_channel(event)
# fix the name of the duplicated channel because all the channels should have
# unique names
metadata = event.channel_config_dict
metadata['name'] += ' copy'
event.channel_config_dict = metadata

# create the channels
event = data_file.create_channel('event')
pos = data_file.create_channel('pos')
zone = data_file.create_channel('zone')

# set channel metadata
event.channel_config_dict = {'name': 'A new event'}
pos.channel_config_dict = {'name': 'A new spiral'}

center_x = width / 2. + 10
center_y = height / 2. + 10
circle = PaintCircle.create_shape(
    [center_x, center_y], 5 / 12 * min(width, height))
zone.channel_config_dict = {
    'name': 'A new circle', 'shape_config': circle.get_state()}

# add the new channels' data
# set the channel data and timestamps as we "read" the timestamps
angle = 20 * math.pi / len(timestamps)
extent = 1 / 3 * min(width, height)
for i, t in enumerate(timestamps):
    event.set_timestamp_value(t, bool((i // 10) % 2))

    current_angle = i * angle
    pos.set_timestamp_value(t, (
        center_x + i / len(timestamps) * extent * math.cos(current_angle),
        center_y + i / len(timestamps) * extent * math.sin(current_angle)
    ))

# finally close the nix file
nix_file.close()


text:
# This code runs the macro-weighted F1-computation for the UKP  Sentential Argument Mining Corpus (Table 1 in the Paper Classification and Clustering of Arguments with Contextualized Word Embeddings)
# Usage: python ukp_evaluation.py bert_output/ukp/bert-base-topic-sentence/*/test_predictions.txt
# Expected output:
#   ...
#   ===================== Overall results ===================="
#   Average F1 score over all topics
#   test_predictions.txt (8) 61.69%
#
# Note: output values can change due to the non-determinism of GPU computations

from __future__ import print_function
from sklearn.metrics import recall_score, precision_score, f1_score
from sklearn.utils.multiclass import unique_labels
import numpy as np
import sys
import os

def analyze_predictions(filepath):
    total_sent = 0
    correct_sent = 0
    count = {}

    y_true = []
    y_pred = []

    for line in open(filepath, encoding='utf8'):
        splits = line.strip().split("\t")
        gold = splits[0]
        pred = splits[1]

        total_sent += 1
        if gold == pred:
            correct_sent += 1

        if gold not in count:
            count[gold] = {}

        if pred not in count[gold]:
            count[gold][pred] = 0

        count[gold][pred] += 1

        y_true.append(gold)
        y_pred.append(pred)

    print("gold - pred - Confusion Matrix")
    for gold_label in sorted(count.keys()):
        for pred_label in sorted(count[gold_label].keys()):
            print("%s - %s: %d" % (gold_label, pred_label, count[gold_label][pred_label]))

    print(":: BERT ::")
    print("Acc: %.2f%%" % (correct_sent/total_sent*100) )
    labels = unique_labels(y_true, y_pred)
    prec = precision_score(y_true, y_pred, average=None)
    rec = recall_score(y_true, y_pred, average=None)
    f1 = f1_score(y_true, y_pred, average=None)

    arg_f1 = []
    for idx, label in enumerate(labels):
        print("\n:: F1 for "+label+" ::")
        print("Prec: %.2f%%" % (prec[idx]*100))
        print("Recall: %.2f%%" % (rec[idx]*100))
        print("F1: %.2f%%" % (f1[idx]*100))

        if label in labels:
            if label != 'NoArgument':
                arg_f1.append(f1[idx])

    print("\n:: Macro Weighted for all  ::")
    print("F1: %.2f%%" % (np.mean(f1)*100))

    prec_mapping = {key:value for key, value in zip(labels, prec)}
    rec_mapping = {key:value for key, value in zip(labels, rec)}
    return np.mean(f1), prec_mapping, rec_mapping

results = {}
prec_results = {}
rec_results = {}
for filepath in sys.argv[1:]:
    print("\n\n===================== "+filepath+" ====================")
    f1, prec, rec = analyze_predictions(filepath)

    folder = filepath.split('/')[-2]
    topic = folder.split('_')[0].split('(')[0]
    filename = os.path.basename(filepath)

    if topic not in results:
        results[topic] = {}
        prec_results[topic] = {}
        rec_results[topic] = {}

    if filename not in results[topic]:
        results[topic][filename] = []
        prec_results[topic][filename] = []
        rec_results[topic][filename] = []

    results[topic][filename].append(f1)
    prec_results[topic][filename].append(prec)
    rec_results[topic][filename].append(rec)

print("\n\n===================== Overall results ====================")
model_f1 = {}
model_prec = {}
model_rec = {}

for topic in sorted(results.keys()):
    print(topic)
    for filename in sorted(results[topic].keys()):
        topic_f1_mean = np.mean(results[topic][filename])
        print("%s (%d): %.4f" % (filename, len(results[topic][filename]), topic_f1_mean))

        if filename not in model_f1:
            model_f1[filename] = []
            model_prec[filename] = []
            model_rec[filename] = []

        model_f1[filename].append(results[topic][filename])
        for prec in prec_results[topic][filename]:
            model_prec[filename].append(prec)

        for rec in rec_results[topic][filename]:
            model_rec[filename].append(rec)
    print("")

print("\n\n==========================================")
print("Average F1 score over all topics")
for filename in model_f1:
    print("%s (%d) %.2f%%" % (filename, len(model_f1[filename]), np.mean(model_f1[filename])*100))

print("\n\n==========================================")
print("P_arg score over all topics")
for filename in model_prec:
    prec_pos = [prec_result['Argument_for'] for prec_result in model_prec[filename]]
    print("P_arg+ %s (%d): %.4f" % (filename, len(prec_pos), np.mean(prec_pos)))

    prec_neg = [prec_result['Argument_against'] for prec_result in model_prec[filename]]
    print("P_arg- %s (%d): %.4f" % (filename, len(prec_neg), np.mean(prec_neg)))

print("\n\n==========================================")
print("R_arg score over all topics")
for filename in model_rec:
    rec_pos = [rec_result['Argument_for'] for rec_result in model_rec[filename]]
    print("R_arg+ %s (%d): %.4f" % (filename, len(rec_pos), np.mean(rec_pos)))

    rec_neg = [rec_result['Argument_against'] for rec_result in model_rec[filename]]
    print("R_arg- %s (%d): %.4f" % (filename, len(rec_neg), np.mean(rec_neg)))


text:
import numpy as np
import cv2
from os import listdir
from os.path import isfile, join

# Extract all images from the file path
path = 'trainingData/'
files = [image for image in listdir(path) if isfile(join(path, image))]

inputs = []
targets = []

for i, file in enumerate(files):
	image_path = path + files[i]
	print (image_path)
	img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
	inputs.append(np.asarray(img, dtype=np.uint8))
	targets.append(i)

targets = np.asarray(targets, dtype=np.int32)

classifier = cv2.face.createLBPHFaceRecognizer()

classifier.train(np.asarray(inputs), np.asarray(targets))

print ('Done.')


text:
from bricks.types.angle import Angle
from bricks.types.angle import Quadrant
from bricks.types.angle import _calc_quadrant
from bricks.types.angle import _is_in_quadrant_I
from bricks.types.angle import _is_in_quadrant_II
from bricks.types.angle import _is_in_quadrant_III
from bricks.types.angle import _is_in_quadrant_IV
from bricks.types.angle import _calc_angle_if_out_of_rangle

import pytest
from pytest import approx
from numpy import deg2rad

class TestAngle:
    def test_init(self):
        obj = Angle()
        assert obj.value == 0.0
        assert obj.quadrant == Quadrant.I
        assert obj.quadrant_angle == 0.0

    def test_value(self):
        obj = Angle(deg2rad(30.0))
        assert obj.value == deg2rad(30.0)

        obj.value = deg2rad(65.0)
        assert obj.value == deg2rad(65.0)

    def test_quadrant_angle(self):
        obj = Angle(deg2rad(30.0))

        assert obj.value == deg2rad(30.0)
        assert obj.quadrant_angle == deg2rad(30.0)

        obj.quadrant_angle = deg2rad(60.0)

        assert obj.value == deg2rad(60.0)
        assert obj.quadrant_angle == deg2rad(60.0)

    def test_quadrant(self):
        obj = Angle(deg2rad(30.0))
        assert obj.quadrant == Quadrant.I

        obj.quadrant = Quadrant.III
        assert obj.quadrant == Quadrant.III

    @pytest.mark.parametrize(
        "quadrant_before, quadrant_after, angle_before, angle_after",
        [
            (Quadrant.I, Quadrant.IV, 30.0, 330.0),
            (Quadrant.II, Quadrant.III, 120.0, 240.0),
            (Quadrant.III, Quadrant.II, 240.0, 120.0),
            (Quadrant.IV, Quadrant.I, 330.0, 30.0),
        ],
    )
    def test_mirror_horizontal(
        self,
        quadrant_before: Quadrant,
        quadrant_after: Quadrant,
        angle_before: float,
        angle_after: float,
    ):
        obj = Angle(deg2rad(angle_before))
        assert obj.quadrant == quadrant_before

        obj.mirror_horizontal()
        assert obj.quadrant == quadrant_after
        assert obj.value == approx(deg2rad(angle_after))

    @pytest.mark.parametrize(
        "quadrant_before, quadrant_after, angle_before, angle_after",
        [
            (Quadrant.I, Quadrant.II, 30.0, 150.0),
            (Quadrant.II, Quadrant.I, 150.0, 30.0),
            (Quadrant.III, Quadrant.IV, 210.0, 330.0),
            (Quadrant.IV, Quadrant.III, 330.0, 210.0),
        ],
    )
    def test_mirror_vertical(
        self,
        quadrant_before: Quadrant,
        quadrant_after: Quadrant,
        angle_before: float,
        angle_after: float,
    ):
        obj = Angle(deg2rad(angle_before))
        assert obj.quadrant == quadrant_before

        obj.mirror_vertical()
        assert obj.quadrant == quadrant_after
        assert obj.value == approx(deg2rad(angle_after))

@pytest.mark.parametrize(
    "angle, quadrant",
    [
        (0.0, Quadrant.I),
        (45.0, Quadrant.I),
        (90.0, Quadrant.I),
        (90.1, Quadrant.II),
        (135.0, Quadrant.II),
        (180.0, Quadrant.II),
        (180.1, Quadrant.III),
        (225.0, Quadrant.III),
        (270.0, Quadrant.III),
        (270.1, Quadrant.IV),
        (315.0, Quadrant.IV),
        (360.0, Quadrant.IV),
    ],
)
def test_calc_quadrant_true(angle, quadrant):
    assert _calc_quadrant(deg2rad(angle)) == quadrant

@pytest.mark.parametrize(
    "angle, result",
    [
        (0.0, True),
        (45.0, True),
        (90.0, True),
        (90.1, False),
        (135.0, False),
        (180.0, False),
        (180.1, False),
        (225.0, False),
        (270.0, False),
        (270.1, False),
        (315.0, False),
        (360.0, False),
    ],
)
def test_is_in_quarant_I(angle: Angle, result: bool):
    assert _is_in_quadrant_I(deg2rad(angle)) == result

@pytest.mark.parametrize(
    "angle, result",
    [
        (0.0, False),
        (45.0, False),
        (90.0, False),
        (90.1, True),
        (135.0, True),
        (180.0, True),
        (180.1, False),
        (225.0, False),
        (270.0, False),
        (270.1, False),
        (315.0, False),
        (360.0, False),
    ],
)
def test_is_in_quarant_II(angle: Angle, result: bool):
    assert _is_in_quadrant_II(deg2rad(angle)) == result

@pytest.mark.parametrize(
    "angle, result",
    [
        (0.0, False),
        (45.0, False),
        (90.0, False),
        (90.1, False),
        (135.0, False),
        (180.0, False),
        (180.1, True),
        (225.0, True),
        (270.0, True),
        (270.1, False),
        (315.0, False),
        (360.0, False),
    ],
)
def test_is_in_quarant_III(angle: Angle, result: bool):
    assert _is_in_quadrant_III(deg2rad(angle)) == result

@pytest.mark.parametrize(
    "angle, result",
    [
        (0.0, False),
        (45.0, False),
        (90.0, False),
        (90.1, False),
        (135.0, False),
        (180.0, False),
        (180.1, False),
        (225.0, False),
        (270.0, False),
        (270.1, True),
        (315.0, True),
        (360.0, True),
    ],
)
def test_is_in_quarant_IV_true(angle: Angle, result: bool):
    assert _is_in_quadrant_IV(deg2rad(angle)) == result

@pytest.mark.parametrize(
    "angle, result_angle",
    [
        (0.0, 0.0),
        (360.0, 360.0),
        (360.1, 0.1),
        (540.0, 180.0),
        (-0.1, 359.9),
        (-360.0, 0.0),
        (-540.0, 180.0),
    ],
)
def test_calc_angle_if_out_of_range(angle, result_angle):
    assert _calc_angle_if_out_of_rangle(deg2rad(angle)) == approx(
        deg2rad(result_angle)
    )

max_stars_repo_name: SandroWissmann/Bricks-Py

max_issues_repo_name: SandroWissmann/Bricks-Py

max_forks_repo_name: SandroWissmann/Bricks-Py

reason: from numpy


text:
import signal, time, random, itertools, copy, math, sys
import pickle as pkl
# numpy imports
import numpy as np
# torch imports
import torch
import torch.autograd as ta
import torch.nn.functional as F
import torch.nn as nn
# code imports
from graph_encoder import *
from nn_classes import *
from acc_networks import *
from matcher_network import *
from candidate_inference_network import *
import parse_input_forms as pf

class SMN(nn.Module):

    def __init__(self, dataset_params):
        super().__init__()
        self.node_ct = dataset_params['node_ct']
        self.edge_ct = dataset_params['edge_ct']
        self.device = dataset_params['device']
        self.aug_ct = dataset_params['aug_ct']
        self.node_state_dim = dataset_params['state_dim']
        self.lr = dataset_params['lr']
        self.tiered_ident_thresh = dataset_params['ti_thresh']
        self.graph_encoder = dataset_params['graph_encoder']

        self.use_cos_sim = dataset_params['cos_sim']

        self.repr_nn = DagLSTMACC(dataset_params)
        self.sig_nn = DagLSTMACC(dataset_params)

        dataset_params['ptr_comp_dim'] = self.node_state_dim * 4
        self.matcher_nn = MatcherNetwork(dataset_params)
        self.cand_inf_nn = CandidateInferenceNetwork(dataset_params)

        sparse_param_ids = []
        for m in self.modules():
            if type(m) == torch.nn.modules.sparse.Embedding:
                sparse_param_ids.extend([id(p) for p in m.parameters()])
        dense_params = [param for param in self.parameters()
                        if not id(param) in sparse_param_ids]
        sparse_params = [param for param in self.parameters()
                         if id(param) in sparse_param_ids]
        self.dense_optimizer = torch.optim.Adam(dense_params, lr=self.lr)
        self.sparse_optimizer = torch.optim.SparseAdam(sparse_params, lr=self.lr)

        self.cr_loss_wt = 1.
        self.ci_loss_wt = 0.1

    #########
    # Training stuff
    #########

    def train_batch(self, node_inds, sig_inds, edge_inds,
                    upd_layers, func_sets, gr_ranges, in_edges, cand_infs):
        self.dense_optimizer.zero_grad()
        self.sparse_optimizer.zero_grad()
        loss = torch.tensor(0., device=self.device)

        bm_res = self.batch_matrs(node_inds, sig_inds, edge_inds, upd_layers,
                                  func_sets, gr_ranges, in_edges=in_edges)

        cr_loss, cr_prec, cr_rec, cr_f1 = self.get_correspondence_loss(bm_res,
                                                                       in_edges)
        #cr_loss, cr_prec, cr_rec, cr_f1 = 0., 0., 0., 0.
        base_incl = [[b for b, _ in in_e] for in_e in in_edges]
        ci_loss, ci_prec, ci_rec, ci_f1 = self.get_candidate_inf_loss(bm_res,
                                                                      base_incl,
                                                                      cand_infs)

        loss = self.cr_loss_wt * cr_loss + self.ci_loss_wt * ci_loss
        loss.backward()
        self.dense_optimizer.step()
        self.sparse_optimizer.step()

        return float(loss), [(cr_prec, cr_rec, cr_f1),
                             (ci_prec, ci_rec, ci_f1)]

    def get_correspondence_loss(self, bm_res, in_edges):
        repr_matrs, sig_matrs, norm_matrs, idents = bm_res
        # now computing performance loss
        forests, in_pos, pos_dicts = [], [], []
        z_iter = zip(repr_matrs, sig_matrs, norm_matrs, in_edges, idents)
        for repr_pr, sig_pr, norm_pr, in_e, ident in z_iter:
            match_forest, pos_d, rev_p = self.build_match_forest(repr_pr, sig_pr,
                                                                 norm_pr, ident)
            forests.append(match_forest)
            in_p = [pos_d[e] for e in in_e]
            in_pos.append(in_p)

        false_pos, false_neg, true_pos = 0, 0, 0
        out_probs, metrics = self.matcher_nn.train_matcher(forests, in_pos)

        targets = torch.ones(len(out_probs), device=self.device, dtype=torch.float)
        loss = nn.BCELoss()(torch.stack(out_probs), targets)

        true_pos, false_pos, false_neg = metrics
        precision, recall, f1 = calc_metrics(true_pos, false_pos, false_neg)

        return loss, precision, recall, f1

    def get_candidate_inf_loss(self, bm_res, incl_nodes, cand_infs):
        _, sig_matrs, _, _ = bm_res
        # now computing performance loss
        sel_nodes, cand_nodes, in_pos = [], [], []
        z_iter = zip(sig_matrs, incl_nodes, cand_infs)
        for (base_sig_pr, _), i_ns, ci_ns in z_iter:
            b_ns = [i for i in range(len(base_sig_pr)) if not i in i_ns]
            if not i_ns: continue
            if not b_ns: continue
            i_ns_tensor = torch.tensor(i_ns, device=self.device)
            b_ns_tensor = torch.tensor(b_ns, device=self.device)
            sel_nodes.append(base_sig_pr.index_select(0, i_ns_tensor))
            cand_nodes.append(base_sig_pr.index_select(0, b_ns_tensor))
            in_p = [position(i, b_ns) for i in ci_ns]
            in_pos.append(in_p)

        if not sel_nodes: return 0., 0., 0., 0.

        false_pos, false_neg, true_pos = 0, 0, 0
        out_probs, metrics = self.cand_inf_nn.train_selector(sel_nodes, cand_nodes,
                                                             in_pos)

        targets = torch.ones(len(out_probs), device=self.device, dtype=torch.float)
        loss = nn.BCELoss()(torch.stack(out_probs), targets)

        true_pos, false_pos, false_neg = metrics
        precision, recall, f1 = calc_metrics(true_pos, false_pos, false_neg)

        return loss, precision, recall, f1

    #########
    # Testing stuff
    #########

    def batch_structural_match(self, node_inds, sig_inds, edge_inds, upd_layers,
                               func_sets, gr_ranges, req_corrs=None):
        bm_res = self.batch_matrs(node_inds, sig_inds, edge_inds, upd_layers,
                                  func_sets, gr_ranges, req_corrs=req_corrs)
        selected_corrs, base_preds = self.get_correspondences(bm_res)
        selected_cis = self.get_candidate_inferences(bm_res, base_preds)

        return selected_corrs, selected_cis

    def get_correspondences(self, bm_res):
        repr_matrs, sig_matrs, norm_matrs, idents = bm_res
        forests, in_pos, rev_dicts = [], [], []
        z_iter = zip(repr_matrs, sig_matrs, norm_matrs, idents)
        for repr_pr, sig_pr, norm_pr, ident in z_iter:
            match_forest, pos_d, rev_p = self.build_match_forest(repr_pr, sig_pr,
                                                                 norm_pr, ident)
            forests.append(match_forest)
            rev_dicts.append(rev_p)
        #
        # get base correspondence predictions
        #
        selected_corrs, base_preds = [], []
        out_probs, selected_inds = self.matcher_nn.predict(forests)
        for probs, sel_inds, rev_p in zip(out_probs, selected_inds, rev_dicts):
            base_preds.append([rev_p[ind][0] for ind in sel_inds])
            sel_lst = [(float(prob), rev_p[ind])
                       for prob, ind in zip(probs, sel_inds)]
            selected_corrs.append(sel_lst)

        return selected_corrs, base_preds

    def get_candidate_inferences(self, bm_res, base_preds):
        _, sig_matrs, _, _ = bm_res
        #
        # get candidate inference predictions
        #
        sel_nodes, cand_nodes, rev_dicts = [], [], []
        z_iter = zip(sig_matrs, base_preds)
        for (base_sig_pr, _), i_ns in z_iter:
            b_ns = [i for i in range(len(base_sig_pr)) if not i in i_ns]
            if not (i_ns and b_ns): continue
            i_ns_tensor = torch.tensor(i_ns, device=self.device)
            b_ns_tensor = torch.tensor(b_ns, device=self.device)
            sel_nodes.append(base_sig_pr.index_select(0, i_ns_tensor))
            cand_nodes.append(base_sig_pr.index_select(0, b_ns_tensor))
            rev_dicts.append(dict(list(enumerate(b_ns))))
        if not sel_nodes: return [[]]
        selected_cis = []
        out_probs, selected_inds = self.cand_inf_nn.predict(sel_nodes, cand_nodes)
        for probs, sel_inds, rev_p in zip(out_probs, selected_inds, rev_dicts):
            sel_lst = [(float(prob), rev_p[ind])
                       for prob, ind in zip(probs, sel_inds)]
            selected_cis.append(sel_lst)
        return selected_cis

    #####
    # Formatting
    #####

    def structural_match(self, b_node_lst, t_node_lst, k_encs=32, req_corrs=None,
                         score_by_prob=False, output_k=False, hashable_syms=False, use_iou=True):
        best_mapping = (float('-inf'), float('-inf'), [], [], (0, 0))
        all_mappings = []
        for _ in range(k_encs):
            lst_res = self.listify_input_pair(b_node_lst, t_node_lst,
                                              hashable_syms=hashable_syms)
            ( node_ranges, node_inds, sig_inds, edge_inds,
              offset_upd, offset_funcs, asgns ) = lst_res
            ind_req_corrs = []
            if req_corrs:
                ind_req_corrs = [(asgns[b], asgns[t]) for b, t in req_corrs]
            mapping, cis = self.batch_structural_match(node_inds, sig_inds,
                                                       edge_inds, offset_upd,
                                                       [offset_funcs],[node_ranges],
                                                       req_corrs=ind_req_corrs)
            mapping, cis = mapping[0], cis[0]
            if not mapping: continue
            mapping_prob = np.prod([prob for prob, _ in mapping])
            mapping_edges = [(b_node_lst[b], t_node_lst[t])
                             for _, (b, t) in mapping]
            valid_edges, sm_score, violations = get_sm_from_mapping(mapping_edges)
            mapping_val = (mapping_prob, sm_score, mapping, cis,
                           valid_edges, violations)
            all_mappings.append(mapping_val)
            comp_score = mapping_prob if score_by_prob else sm_score
            best_score = best_mapping[0] if score_by_prob else best_mapping[1]
            v_ct, bv_ct = sum(violations), sum(best_mapping[len(best_mapping)-1])
            # take best score, otherwise break ties with violation count
            if (comp_score == best_score and v_ct < bv_ct) or \
               (comp_score > best_score):
                best_mapping = mapping_val
        iou_rankings = []
        for m1 in all_mappings:
            m1_prob = m1[0]
            edges1 = set([e for (p, e) in m1[2]])
            iou = 0
            for m2 in all_mappings:
                edges2 = [e for (p, e) in m2[2]]
                iou += (len(edges1.intersection(edges2)) / len(edges1.union(edges2)))
            iou_rankings.append((iou, m1_prob, m1))
        iou_rankings = sorted(iou_rankings, key=lambda x : x[0], reverse=True)
        if use_iou: best_mapping = iou_rankings[0][2]
        if output_k: return all_mappings
        return best_mapping

    def listify_input_pair(self, b_node_lst, t_node_lst, hashable_syms=False):
        node_ranges, node_inds, sig_inds = [], [], []
        edge_inds, offset_upd, offset_funcs, asgn_dict = [], [], [], {}
        self.graph_encoder.reset_assignments()
        base_info = self.graph_encoder.get_update_lsts(b_node_lst,
                                                       hashable_syms=hashable_syms)
        targ_info = self.graph_encoder.get_update_lsts(t_node_lst,
                                                       hashable_syms=hashable_syms)
        for i, n in enumerate(b_node_lst): asgn_dict[n] = i
        for i, n in enumerate(t_node_lst): asgn_dict[n] = i + len(b_node_lst)
        for (n_inds, s_inds, e_inds, updates, funcs) in [base_info, targ_info]:
            n_offset, e_offset = len(node_inds), len(edge_inds)
            node_ranges.append([n_offset, n_offset + len(n_inds)])
            # embedding indices dont get offset
            node_inds.extend(n_inds)
            sig_inds.extend(s_inds)
            edge_inds.extend(e_inds)
            offset_funcs.append([f + n_offset for f in funcs])
            # get offset updates and merge them across batch
            for i, upd_layer in enumerate(updates):
                offset_layer = []
                for (n_i, n_j, e_ij) in upd_layer:
                    if n_j != None:
                        off_ex = (n_i + n_offset, n_j + n_offset, e_ij + e_offset)
                    else:
                        off_ex = (n_i + n_offset, None, None)
                    offset_layer.append(off_ex)
                if len(offset_upd) > i:
                    offset_upd[i].extend(offset_layer)
                else:
                    offset_upd.append(offset_layer)

        return node_ranges, node_inds, sig_inds, edge_inds, offset_upd, offset_funcs, asgn_dict

    def batch_matrs(self, node_inds, sig_inds, edge_inds, upd_layers, func_sets,
                    gr_ranges, req_corrs=None, in_edges=None):
        if in_edges == None: in_edges = [[] for _ in gr_ranges]
        l_reqs, r_reqs = {}, {}
        if req_corrs:
            l_reqs, r_reqs = dict(req_corrs), dict([(t, b) for b, t in req_corrs])
        sig_matr = self.sig_nn.compute_node_reprs(sig_inds,edge_inds, upd_layers)
        #sig_matr = self.sig_nn.compute_node_reprs(sig_inds, upd_layers)
        if self.use_cos_sim:
            norm_sig_matr = F.normalize(sig_matr, dim=1, p=2)
        else:
            norm_sig_matr = sig_matr

        repr_matr, emb_matr = self.repr_nn.compute_node_reprs(node_inds, edge_inds,
                                                              upd_layers,
                                                              ret_init_embs=True)
        batch_reprs, batch_sigs, batch_norm_sigs, batch_idents = [], [], [], []
        z_iter = zip(func_sets, gr_ranges, in_edges)
        for ex_num, ((l_funcs, r_funcs), (l, r), in_e) in enumerate(z_iter):
            l_span = torch.tensor(range(l[0], l[1]), dtype=torch.long,
                                  device=self.device)
            r_span = torch.tensor(range(r[0], r[1]), dtype=torch.long,
                                  device=self.device)

            l_rm = repr_matr.index_select(0, l_span)
            r_rm = repr_matr.index_select(0, r_span)
            batch_reprs.append((l_rm, r_rm))

            l_sm = norm_sig_matr.index_select(0, l_span)
            r_sm = norm_sig_matr.index_select(0, r_span)
            batch_sigs.append((l_sm, r_sm))

            l_sm = norm_sig_matr.index_select(0, l_span)
            r_sm = norm_sig_matr.index_select(0, r_span)
            batch_norm_sigs.append((l_sm, r_sm))

            l_em = emb_matr.index_select(0, l_span)
            r_em = emb_matr.index_select(0, r_span)

            mf_inds = set()
            for l_orig_ind in range(len(l_rm)):
                l_ind = l[0] + l_orig_ind
                for r_orig_ind in range(len(r_rm)):
                    r_ind = r[0] + r_orig_ind
                    # computes node embedding similarity
                    dist = torch.sum((l_rm[l_orig_ind] - r_rm[r_orig_ind]).pow(2))
                    # last conjunct in clause specifies functors
                    # to match regardless of labels
                    if (l_orig_ind, r_orig_ind) in in_e or \
                       (dist <= self.tiered_ident_thresh and \
                        not (l_ind in l_reqs or r_ind in r_reqs)) or \
                        (l_ind in l_reqs and l_reqs[l_ind] == r_ind):
                        #(l_ind in l_funcs and r_ind in r_funcs):
                        mf_inds.add((l_orig_ind, r_orig_ind))
            # this will restrict it to only training
            if in_edges and self.aug_ct > 0:
                aug_edges = self.augment_edges(l[0], r[0], len(l_rm), len(r_rm),
                                               mf_inds)
                mf_inds = mf_inds.union(aug_edges)
            batch_idents.append(mf_inds)
        return batch_reprs, batch_sigs, batch_norm_sigs, batch_idents

    def augment_edges(self, l_0, r_0, l_max, r_max, curr_edges):
        new_edges = set()
        for l in set([l for l, _ in curr_edges]):
            if r_max > self.aug_ct:
                aug = np.random.choice(list(range(r_max)), self.aug_ct,
                                       replace=False)
            else: aug = list(range(r_max))
            for a in aug: new_edges.add((l_0 + l, r_0 + a))
        for r in set([r for _, r in curr_edges]):
            if l_max > self.aug_ct:
                aug = np.random.choice(list(range(l_max)), self.aug_ct,
                                       replace=False)
            else: aug = list(range(l_max))
            for a in aug: new_edges.add((l_0 + a, r_0 + r))
        return new_edges

    #################
    # Pointer input
    #################

    def build_pointer_inputs(self, l_len, r_len, allowable_matches):
        pos_d, sd_ct, l_c, r_c = {}, 0, [], []
        for i in range(l_len):
            for j in range(r_len):
                if not (i, j) in allowable_matches: continue
                pos_d[(i, j)] = sd_ct
                sd_ct += 1
                l_c.append(i)
                r_c.append(j)
        l_c = torch.tensor(l_c, device=self.device)
        r_c = torch.tensor(r_c, device=self.device)
        return sd_ct, l_c, r_c, pos_d

    def build_match_forest(self, repr_pair, sig_pair, norm_pair, ident):
        l_repr_matr, r_repr_matr = repr_pair
        l_sig_matr, r_sig_matr = sig_pair
        l_norm_matr, r_norm_matr = norm_pair
        l_len, r_len = len(l_repr_matr), len(r_repr_matr)
        sd_inds, l_c, r_c, pos_d = self.build_pointer_inputs(l_len, r_len, ident)
        rev_p = dict([(v, k) for k, v in pos_d.items()])
        l_rm = l_repr_matr.index_select(0, l_c)
        r_rm = r_repr_matr.index_select(0, r_c)
        l_sm = l_sig_matr.index_select(0, l_c)
        r_sm = r_sig_matr.index_select(0, r_c)
        l_nm = l_norm_matr.index_select(0, l_c)
        r_nm = r_norm_matr.index_select(0, r_c)
        match_forest = [torch.cat((l_rm, r_rm, l_nm, r_nm), 1), l_sm, r_sm]
        return match_forest, pos_d, rev_p

    #################

max_stars_repo_name: ibrahimabdelaziz/NeuralAnalogy

max_issues_repo_name: ibrahimabdelaziz/NeuralAnalogy

max_forks_repo_name: ibrahimabdelaziz/NeuralAnalogy


text:
import numpy as np
import cv2

class CoverMatcher(object):
    def __init__(self, descriptor, coverPaths, ratio=0.7,
        minMatches=40, useHamming=True):
        self.descriptor=descriptor
        self.coverPaths=coverPaths
        self.ratio=ratio
        self.minMatches=minMatches
        self.distanceMethod='BruteForce'
        if useHamming:
            self.distanceMethod+="-Hamming"

    def search(self, queryKps, queryDescs):
        results=[]
        for coverPath in self.coverPaths:
            cover=cv2.imread(coverPath)
            gray=cv2.cvtColor(cover,cvt.COLOR_BGR2GRAY)
            (kps, descs)=self.descriptor.describe(gray)
            score=self.match(queryKps,queryDescs, kps, descs)

            results[coverPath]=score
        if len(results)>0:
            results=sorted([(v,k) for (k,v) in results.items() if v>0],
                reverse=True)
        return results

    def match(self,kpsA, featuresA, kpsB, featuresB):
        matcher=cv2.DescriptorMatcher_create(self.distanceMethod)
        rawMatches=matcher.knnMatch(featuresB, featuresA, 2)
        matches=[]

        for m in rawMatches:
            if len(m) ==2 and m[0].distance < m[1].distance*self.ratio:
                matches.append((m[0].trainIdx, m[0].queryIdx))

        if len(matches)>self.minMatches:
            ptsA=np.float32([kpsA[i] for (i,_) in matches])
            ptsB=np.float32([kpsB[j] for (_,j) in matches])
            (_, status)=cv2.findHomography(ptsA, ptsB, cv2.RANSAC, 4.0)
            return float(status.sum())/status.size
        return -1.0


text:
from django.core.management.base import BaseCommand, CommandError
from marketgrab.models import Data, MovingAvg, Movements, Sigma
import numpy as np
from datetime import date
from django.db.models import Avg

class Command(BaseCommand):
    help = 'Compute and store up to date sigma for movements.'

    def add_arguments(self, parser):
        parser.add_argument('-t', '--tickers', nargs='+', required=True)

    def handle(self, **options):
        for t in options['tickers']:
            try:
                latest_s = Sigma.objects.filter(ticker=t, series='market').latest('date').date
            except:
                latest_s = date.min

            latest_m = Movements.objects.filter(ticker=t, series='market').latest('date').date
            if latest_s < latest_m:
                move = Movements.objects.filter(ticker=t, series='market')
                sigma = np.std(np.array([i.percent for i in move]))
                d = move.latest('date').date
                s = Sigma(ticker=t, date=d, value=sigma, series='market')
                s.save()

                movement = move.latest('date')
                zscore = (movement.percent - move.aggregate(avg = Avg('percent'))['avg'])/sigma
                movement.zvalue = zscore
                movement.save()

                for s in [3, 5, 50, 200]:
                    move = Movements.objects.filter(ticker=t, series=s)
                    sigma = np.std(np.array([i.percent for i in move]))
                    d = move.latest('date').date
                    s = Sigma(ticker=t, date=d, value=sigma, series=s)
                    s.save()

                    movement = move.latest('date')
                    zscore = (movement.percent - move.aggregate(avg = Avg('percent'))['avg'])/sigma
                    movement.zvalue = zscore
                    movement.save()

max_stars_repo_name: colinmcglone/window-time

max_issues_repo_name: colinmcglone/window-time

max_forks_repo_name: colinmcglone/window-time


text:
#from sklearn.feature_extraction import
from pathlib import Path
import numpy as np
import re

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import scikitplot as skplt

pos_directory = Path('data/raw/aclImdb/train/pos')
neg_directory = Path('data/raw/aclImdb/train/neg')

pos_examples = []
neg_examples = []

for fname in list(pos_directory.iterdir())[:1000]:
    pos_examples.append(fname.read_text())
for fname in list(neg_directory.iterdir())[:1000]:
    neg_examples.append(fname.read_text())

def normalize(s :str) -> str:
    s = s.replace('<br />', '')
    s = re.sub('[^a-zA-Z]', ' ', s)
    s = re.sub(r'\s\s+', ' ', s)
    s = s.lower()

    return s

pos_examples = [normalize(x) for x in pos_examples]
neg_examples = [normalize(x) for x in neg_examples]

y = np.concatenate((np.ones(len(pos_examples)), np.zeros(len(neg_examples))))

vectorizer = CountVectorizer(stop_words='english', ngram_range=(1,3), max_features=2000)
X = vectorizer.fit_transform((pos_examples + neg_examples)).toarray()

clf = GaussianNB()
clf.fit(X, y)
y_pred = clf.predict(X)
y_prob = clf.predict_proba(X)

print(classification_report(y, y_pred))

# skplt.metrics.plot_confusion_matrix(y, y_pred)
# plt.savefig('cm.png')

# plt.clf()
skplt.metrics.plot_lift_curve(y, y_prob)
plt.savefig('cal.png')

max_stars_repo_name: mbednarski/ml-prod-example

max_issues_repo_name: mbednarski/ml-prod-example

max_forks_repo_name: mbednarski/ml-prod-example


text:
import symlens as s
from pixell import enmap, utils as putils, powspec
import os,sys
from scipy.interpolate import interp1d
import numpy as np
import pytest

"""
Only tests marked @pytest.mark.webtest
will be run on Travis
"""

@pytest.mark.webtest
def test_trivial():
    assert 1==1

# The following tests need orphics, and so are not meant to be run on Travis
def test_hdv_huok_planck():
    from orphics import lensing,io,cosmology,maps

    shape,wcs = enmap.geometry(shape=(512,512),res=2.0*putils.arcmin,pos=(0,0))
    modlmap = enmap.modlmap(shape,wcs)
    theory = cosmology.default_theory()
    ells = np.arange(0,3000,1)
    ctt = theory.lCl('TT',ells)
    # ps,_ = powspec.read_camb_scalar("tests/Aug6_highAcc_CDM_scalCls.dat")
    # ells = range(ps.shape[-1])

    ## Build HuOk TT estimator
    f = s.Ldl1 * s.e('uC_T_T_l1') + s.Ldl2 * s.e('uC_T_T_l2')
    F = f / 2 / s.e('tC_T_T_l1') / s.e('tC_T_T_l2')
    expr1 = f * F
    feed_dict = {}
    feed_dict['uC_T_T'] = s.interp(ells,ctt)(modlmap)
    feed_dict['tC_T_T'] = s.interp(ells,ctt)(modlmap)+(33.*np.pi/180./60.)**2./s.gauss_beam(modlmap,7.0)**2.
    tellmin = 10 ; tellmax = 3000
    xmask = s.mask_kspace(shape,wcs,lmin=tellmin,lmax=tellmax)
    integral = s.integrate(shape,wcs,feed_dict,expr1,xmask=xmask,ymask=xmask).real
    Nl = modlmap**4./integral/4.
    bin_edges = np.arange(10,3000,40)
    binner = s.bin2D(modlmap,bin_edges)
    cents,nl1d = binner.bin(Nl)

    ## Build HDV TT estimator
    F = s.Ldl1 * s.e('uC_T_T_l1') / s.e('tC_T_T_l1') / s.e('tC_T_T_l2')
    expr1 = f*F
    integral = s.integrate(shape,wcs,feed_dict,expr1,xmask=xmask,ymask=xmask).real
    Nl = modlmap**4./integral/4.

    cents,nl1d2 = binner.bin(Nl)

    cents,nl1d3 = binner.bin(s.N_l_cross(shape,wcs,feed_dict,"hu_ok","TT","hu_ok","TT",
                                         xmask=xmask,ymask=xmask))
    cents,nl1d4 = binner.bin(s.N_l_cross(shape,wcs,feed_dict,"hdv","TT","hdv","TT",
                                       xmask=xmask,ymask=xmask))
    cents,nl1d5 = binner.bin(s.N_l(shape,wcs,feed_dict,"hu_ok","TT",xmask=xmask,ymask=xmask))
    cents,nl1d6 = binner.bin(s.N_l(shape,wcs,feed_dict,"hdv","TT",xmask=xmask,ymask=xmask))

    clkk = theory.gCl('kk',ells)
    pl = io.Plotter(xyscale='linlog',xlabel='L',ylabel='C')
    pl.add(cents,nl1d)
    pl.add(cents,nl1d2)
    # pl.add(cents,nl1d3)
    pl.add(cents,nl1d4)
    pl.add(cents,nl1d5)
    pl.add(cents,nl1d6)
    pl.add(ells,clkk)
    pl.done("plcomp.png")

def test_lens_recon():
    from orphics import lensing,io,cosmology,maps
    from enlib import bench

    deg = 10.
    px = 2.0
    tellmin = 100
    tellmax = 3000
    kellmin = 40
    kellmax = 3000
    grad_cut = None
    bin_width = 80
    beam_arcmin = 0.01
    noise_uk_arcmin = 0.01

    theory = cosmology.default_theory(lpad=30000)
    shape,wcs = s.rect_geometry(width_deg=deg,px_res_arcmin=px)
    flsims = lensing.FlatLensingSims(shape,wcs,theory,beam_arcmin,noise_uk_arcmin)
    kbeam = flsims.kbeam
    modlmap = enmap.modlmap(shape,wcs)
    fc = maps.FourierCalc(shape,wcs)
    n2d = (noise_uk_arcmin*np.pi/180./60.)**2./flsims.kbeam**2.
    tmask = s.mask_kspace(shape,wcs,lmin=tellmin,lmax=tellmax)
    kmask = s.mask_kspace(shape,wcs,lmin=kellmin,lmax=kellmax)
    with bench.show("orphics init"):
        qest = lensing.qest(shape,wcs,theory,
                            noise2d=n2d,kmask=tmask,kmask_K=kmask,
                            pol=False,grad_cut=grad_cut,
                            unlensed_equals_lensed=True,bigell=30000)
    bin_edges = np.arange(kellmin,kellmax,bin_width)
    binner = s.bin2D(modlmap,bin_edges)
    i = 0
    unlensed,kappa,lensed,beamed,noise_map,observed = flsims.get_sim(seed_cmb=(i,1),
                                                                     seed_kappa=(i,2),
                                                                     seed_noise=(i,3),
                                                                     lens_order=5,
                                                                     return_intermediate=True)

    kmap = enmap.fft(observed,normalize="phys")
    # _,kmap,_ = fc.power2d(observed)
    with bench.show("orphics"):
        kkappa = qest.kappa_from_map("TT",kmap/kbeam,alreadyFTed=True,returnFt=True)
    pir2d,kinput = fc.f1power(kappa,kkappa)
    pii2d = fc.f2power(kinput,kinput)
    prr2d = fc.f2power(kkappa,kkappa)
    cents,pir1d = binner.bin(pir2d)
    cents,pii1d = binner.bin(pii2d)
    cents,prr1d = binner.bin(prr2d)

    feed_dict = {}
    cltt = theory.lCl('TT',modlmap)
    feed_dict['uC_T_T'] = theory.lCl('TT',modlmap)
    feed_dict['tC_T_T'] = cltt+n2d
    feed_dict['X'] = kmap/kbeam
    feed_dict['Y'] = kmap/kbeam

    with bench.show("symlens init"):
        Al = s.A_l(shape,wcs,feed_dict,"hdv","TT",xmask=tmask,ymask=tmask)
    Nl = s.N_l_from_A_l_optimal(shape,wcs,Al)
    with bench.show("symlens"):
        ukappa = s.unnormalized_quadratic_estimator(shape,wcs,feed_dict,"hdv","TT",xmask=tmask,ymask=tmask)
    nkappa = Al * ukappa

    pir2d2 = fc.f2power(nkappa,kinput)
    cents,pir1d2 = binner.bin(pir2d2)

    cents,Nlkk = binner.bin(qest.N.Nlkk['TT'])
    cents,Nlkk2 = binner.bin(Nl)

    pl = io.Plotter(xyscale='linlog',xlabel='L',ylabel='C')
    pl.add(cents,pii1d,color='k',lw=3)
    pl.add(cents,pir1d,label='orphics')
    pl.add(cents,pir1d2,label='hdv symlens')
    pl.add(cents,Nlkk,ls="--",label='orphics')
    pl.add(cents,Nlkk2,ls="-.",label='symlens')
    pl.done("ncomp.png")

def test_shear():
    from orphics import lensing,io,cosmology,maps

    deg = 20.
    px = 2.0
    tellmin = 30
    tellmax = 3500
    kellmin = 10
    kellmax = 3000
    bin_width = 20
    beam_arcmin = 1.4
    noise_uk_arcmin = 7.0

    theory = cosmology.default_theory(lpad=30000)
    shape,wcs = s.rect_geometry(width_deg=deg,px_res_arcmin=px)
    flsims = lensing.FlatLensingSims(shape,wcs,theory,beam_arcmin,noise_uk_arcmin)
    kbeam = flsims.kbeam
    modlmap = enmap.modlmap(shape,wcs)
    fc = maps.FourierCalc(shape,wcs)
    n2d = (noise_uk_arcmin*np.pi/180./60.)**2./flsims.kbeam**2.
    tmask = s.mask_kspace(shape,wcs,lmin=tellmin,lmax=tellmax)
    kmask = s.mask_kspace(shape,wcs,lmin=kellmin,lmax=kellmax)
    bin_edges = np.arange(kellmin,kellmax,bin_width)
    binner = s.bin2D(modlmap,bin_edges)
    i = 0
    unlensed,kappa,lensed,beamed,noise_map,observed = flsims.get_sim(seed_cmb=(i,1),
                                                                     seed_kappa=(i,2),
                                                                     seed_noise=(i,3),
                                                                     lens_order=5,
                                                                     return_intermediate=True)
    _,kmap,_ = fc.power2d(observed)
    pii2d,kinput,_ = fc.power2d(kappa)

    feed_dict = {}
    cltt = theory.lCl('TT',modlmap)
    feed_dict['uC_T_T'] = theory.lCl('TT',modlmap)
    feed_dict['tC_T_T'] = (cltt+n2d)
    feed_dict['X'] = kmap/kbeam
    feed_dict['Y'] = kmap/kbeam

    ells = np.arange(0,10000,1)
    ucltt = theory.lCl('TT',ells)
    feed_dict['duC_T_T'] = s.interp(ells,np.gradient(np.log(ucltt),np.log(ells)))(modlmap)
    sAl = s.A_l(shape,wcs,feed_dict,"shear","TT",xmask=tmask,ymask=tmask)
    sNl = s.N_l(shape,wcs,feed_dict,"shear","TT",
              xmask=tmask,ymask=tmask,
              Al=sAl)
    sukappa = s.unnormalized_quadratic_estimator(shape,wcs,feed_dict,"shear","TT",xmask=tmask,ymask=tmask)
    snkappa = sAl * sukappa

    pir2d3 = fc.f2power(snkappa,kinput)
    cents,pir1d3 = binner.bin(pir2d3)
    cents,pii1d = binner.bin(pii2d)
    cents,prr1d = binner.bin(fc.f2power(snkappa,snkappa))

    cents,Nlkk3 = binner.bin(sNl)

    pl = io.Plotter(xyscale='loglog',xlabel='L',ylabel='C')
    pl.add(ells,theory.gCl('kk',ells))
    pl.add(cents,pii1d,color='k',lw=3)
    pl.add(cents,pir1d3,label='shear')
    pl.add(cents,prr1d)
    pl.add(cents,Nlkk3,ls=":")
    pl._ax.set_xlim(10,3500)
    pl.done("ncomp.png")

def test_pol():
    from orphics import lensing,io,cosmology,maps

    est = "hu_ok"
    pols = ['TT','EE','TE','EB','TB']
    # est = "hdv"
    # pols = ['TT','EE','TE','ET','EB','TB']

    deg = 5.
    px = 2.0
    tellmin = 30
    tellmax = 3000
    pellmin = 30
    pellmax = 5000
    kellmin = 10
    kellmax = 5000
    bin_width = 40

    beam_arcmin = 1.5
    noise_uk_arcmin = 10.0

    theory = cosmology.default_theory(lpad=30000)
    shape,wcs = s.rect_geometry(width_deg=deg,px_res_arcmin=px)
    modlmap = enmap.modlmap(shape,wcs)
    kbeam = s.gauss_beam(modlmap,beam_arcmin)
    n2d = (noise_uk_arcmin*np.pi/180./60.)**2./kbeam**2.
    tmask = s.mask_kspace(shape,wcs,lmin=tellmin,lmax=tellmax)
    pmask = s.mask_kspace(shape,wcs,lmin=pellmin,lmax=pellmax)
    kmask = s.mask_kspace(shape,wcs,lmin=kellmin,lmax=kellmax)
    bin_edges = np.arange(kellmin,kellmax,bin_width)
    binner = s.bin2D(modlmap,bin_edges)

    feed_dict = {}
    cltt = theory.lCl('TT',modlmap)
    clee = theory.lCl('EE',modlmap)
    clbb = theory.lCl('BB',modlmap)
    clte = theory.lCl('TE',modlmap)
    feed_dict['uC_T_T'] = cltt
    feed_dict['tC_T_T'] = (cltt+n2d)
    feed_dict['uC_E_E'] = clee
    feed_dict['tC_E_E'] = (clee+n2d*2.)
    feed_dict['uC_B_B'] = clbb
    feed_dict['tC_B_B'] = (clbb+n2d*2.)
    feed_dict['uC_T_E'] = clte
    feed_dict['tC_T_E'] = clte

    ells = np.arange(0,10000,1)
    pl = io.Plotter(xyscale='loglog',xlabel='L',ylabel='C')
    pl.add(ells,theory.gCl('kk',ells))
    imask = {'T':tmask, 'E':pmask,'B': pmask}
    for pol in pols:
        print(pol)
        X,Y = pol
        cents,Nl = binner.bin(s.N_l(shape,wcs,feed_dict,est,pol,xmask=imask[X],ymask=imask[Y]))
        pl.add(cents,Nl,label=pol)
    pl._ax.set_xlim(10,kellmax)
    pl.done("nls.png")

max_stars_repo_name: EEmGuzman/symlens

max_issues_repo_name: EEmGuzman/symlens

max_forks_repo_name: EEmGuzman/symlens

reason: import numpy,from scipy


text:
""" transform label to groundtruth(density map)"""
from scipy import integrate
import math
import numpy as np

def PDF(x, u, sig):
    # f(x)
    return np.exp(-(x - u) ** 2 / (2 * sig ** 2)) / (math.sqrt(2 * math.pi) * sig)

# integral f(x)
def get_integrate(x_1, x_2, avg, sig):
    y, err = integrate.quad(PDF, x_1, x_2, args=(avg, sig))
    return y

def normalize_label(y_frame, y_length):
    # y_length: total frames
    # return: normalize_label  size:nparray(y_length,)
    y_label = [0 for i in range(y_length)]  # 坐标轴长度，即帧数
    for i in range(0, len(y_frame), 2):
        x_a = y_frame[i]
        x_b = y_frame[i + 1]
        avg = (x_b + x_a) / 2
        sig = (x_b - x_a) / 6
        num = x_b - x_a + 1  # 帧数量 update 1104
        if num != 1:
            for j in range(num):
                x_1 = x_a - 0.5 + j
                x_2 = x_a + 0.5 + j
                y_ing = get_integrate(x_1, x_2, avg, sig)
                y_label[x_a + j] = y_ing
        else:
            y_label[x_a] = 1
    return y_label

max_stars_repo_name: SvipRepetitionCounting/TransRAC

max_issues_repo_name: SvipRepetitionCounting/TransRAC

max_forks_repo_name: SvipRepetitionCounting/TransRAC