text:
import numpy as np
import pandas as pd
from os import path

def g(inpu):
    return (1/(1+np.exp(-inpu)))

def cost(inpu, actual):
    return (actual*np.log(inpu)) + ((1-actual)*(np.log(1-inpu)))

class LogisticRegressor():
    def __init__(self, lr=0.05):
        # store learning rate and weights
        self.lr = lr
        self.weights = None

    def fit(self, train_data_feature, train_data_label):
        # get shape of the training data
        m,n = train_data_feature.shape

        # creating numpy vector with feature data
        X = np.zeros(shape=(m,n+1))
        for i in range(m):
            for j in range(n+1):
                if j==0:
                    X[i][j] = 1
                else:
                    X[i][j] = train_data_feature[i][j-1]
        # setting initial value of the parameters as 1
        coeff_vals = np.ones(n+1)
        # stores previous cost
        prev_jtheta = None
        # calculate loss
        h_theta = g(np.dot(X, coeff_vals))
        loss = (h_theta - train_data_label)
        # current cost
        cur_jtheta = np.sum(cost(h_theta, train_data_label)) * (-1/m)
        # setting convergence when the difference between consecutive error is less than 0.0000001
        while (prev_jtheta is None or abs(prev_jtheta - cur_jtheta) > 0.0000001):
            # gradient descent with vector notation, simultaneous calculation
            descent_vals = (np.dot(X.transpose(), loss) * self.lr) / m
            # update all coefficients with descent
            coeff_vals -= descent_vals
            prev_jtheta = cur_jtheta
            # calculate new cost
            h_theta = g(np.dot(X, coeff_vals))
            loss = (h_theta - train_data_label)
            cur_jtheta = np.sum(cost(h_theta, train_data_label)) * (-1/m)
            print(f"Difference between consecutive costs: {abs(prev_jtheta - cur_jtheta)}\t", end="\r", flush=True)
        # print(f"Parameters: {list(coeff_vals)}\t\t")
        # print(f"Error on Data: {cur_jtheta}\n")

        self.weights = coeff_vals

    # function for predicting results
    def predict(self, data_feature):
        m,n = data_feature.shape
        # creating numpy vector with feature data
        X = np.zeros(shape=(m,n+1))
        for i in range(m):
            for j in range(n+1):
                if j==0:
                    X[i][j] = 1
                else:
                    X[i][j] = data_feature[i][j-1]
        h_theta = g(np.dot(X, self.weights))
        # threshold is chosen as 0.5
        h_theta[h_theta>=0.5] = 1
        h_theta[h_theta<0.5] = 0
        return h_theta

    def get_params(self, deep = False):
        return {'lr':self.lr}

max_stars_repo_name: thealphadollar/ML-Assignments

max_issues_repo_name: thealphadollar/ML-Assignments


text:
"""
pyart.core.radar
================

A general central radial scanning (or dwelling) instrument class.

.. autosummary::
    :toctree: generated/

    join_radar
    is_vpt
    to_vpt

.. autosummary::
    :toctree: generated/
    :template: dev_template.rst

    Radar

"""
from __future__ import print_function

import copy
import sys

import numpy as np

from ..config import get_metadata

class Radar(object):
    """
    A class for storing antenna coordinate radar data.

    The structure of the Radar class is based on the CF/Radial Data file
    format.  Global attributes and variables (section 4.1 and 4.3) are
    represented as a dictionary in the metadata attribute.  Other required and
    optional variables are represented as dictionaries in a attribute with the
    same name as the variable in the CF/Radial standard.  When a optional
    attribute not present the attribute has a value of None.  The data for a
    given variable is stored in the dictionary under the 'data' key.  Moment
    field data is stored as a dictionary of dictionaries in the fields
    attribute.  Sub-convention variables are stored as a dictionary of
    dictionaries under the meta_group attribute.

    Refer to the attribute section for information on the parameters.

    Attributes
    ----------
    time : dict
        Time at the center of each ray.
    range : dict
        Range to the center of each gate (bin).
    fields : dict of dicts
        Moment fields.
    metadata : dict
        Metadata describing the instrument and data.
    scan_type : str
        Type of scan, one of 'ppi', 'rhi', 'sector' or 'other'.  If the scan
        volume contains multiple sweep modes this should be 'other'.
    latitude : dict
        Latitude of the instrument.
    longitude : dict
        Longitude of the instrument.
    altitude : dict
        Altitude of the instrument, above sea level.
    altitude_agl : dict or None
        Altitude of the instrument above ground level.  If not provided this
        attribute is set to None, indicating this parameter not available.
    sweep_number : dict
        The number of the sweep in the volume scan, 0-based.
    sweep_mode : dict
        Sweep mode for each mode in the volume scan.
    fixed_angle : dict
        Target angle for thr sweep.  Azimuth angle in RHI modes, elevation
        angle in all other modes.
    sweep_start_ray_index : dict
        Index of the first ray in each sweep relative to the start of the
        volume, 0-based.
    sweep_end_ray_index : dict
        Index of the last ray in each sweep relative to the start of the
        volume, 0-based.
    rays_per_sweep : dict
        Number of rays in each sweep.  This is a read only attribute,
        attempting to set the attribute will raise a AttributeError and any
        changes to the dictionary keys will be lost when the attribute is
        accessed again.
    target_scan_rate : dict or None
        Intended scan rate for each sweep.  If not provided this attribute is
        set to None, indicating this parameter is not available.
    rays_are_indexed : dict or None
        Indication of whether ray angles are indexed to a regular grid in
        each sweep.  If not provided this attribute is set to None, indicating
        ray angle spacing is not determined.
    ray_angle_res : dict or None
        If rays_are_indexed is not None, this provides the angular resolution
        of the grid.  If not provided or available this attribute is set to
        None.
    azimuth : dict
        Azimuth of antenna, relative to true North.
    elevation : dict
        Elevation of antenna, relative to the horizontal plane.
    scan_rate : dict or None
        Actual antenna scan rate.  If not provided this attribute is set to
        None, indicating this parameter is not available.
    antenna_transition : dict or None
        Flag indicating if the antenna is in transition, 1 = yes, 0 = no.
        If not provided this attribute is set to None, indicating this
        parameter is not available.
    rotation : dict or None
        The rotation angle of the antenna.  The angle about the aircraft
        longitudinal axis for a vertically scanning radar.
    tilt : dict or None
        The tilt angle with respect to the plane orthogonal (Z-axis) to
        aircraft longitudinal axis.
    roll : dict or None
        The roll angle of platform, for aircraft right wing down is positive.
    drift : dict or None
        Drift angle of antenna, the angle between heading and track.
    heading : dict or None
        Heading (compass) angle, clockwise from north.
    pitch : dict or None
        Pitch angle of antenna, for aircraft nose up is positive.
    georefs_applied : dict or None
        Indicates whether the variables have had georeference calculation
        applied.  Leading to Earth-centric azimuth and elevation angles.
    instrument_parameters : dict of dicts or None
        Instrument parameters, if not provided this attribute is set to None,
        indicating these parameters are not avaiable.  This dictionary also
        includes variables in the radar_parameters CF/Radial subconvention.
    radar_calibration : dict of dicts or None
        Instrument calibration parameters.  If not provided this attribute is
        set to None, indicating these parameters are not available
    ngates : int
        Number of gates (bins) in the volume.
    nrays : int
        Number of rays in the volume.
    nsweeps : int
        Number of sweep in the volume.

    """

    def __init__(self, time, _range, fields, metadata, scan_type,
                 latitude, longitude, altitude,

                 sweep_number, sweep_mode, fixed_angle, sweep_start_ray_index,
                 sweep_end_ray_index,

                 azimuth, elevation,

                 altitude_agl=None,
                 target_scan_rate=None, rays_are_indexed=None,
                 ray_angle_res=None,

                 scan_rate=None, antenna_transition=None,

                 instrument_parameters=None,
                 radar_calibration=None,

                 rotation=None, tilt=None, roll=None, drift=None, heading=None,
                 pitch=None, georefs_applied=None,

                 ):

        if 'calendar' not in time:
            time['calendar'] = 'gregorian'
        self.time = time
        self.range = _range

        self.fields = fields
        self.metadata = metadata
        self.scan_type = scan_type

        self.latitude = latitude
        self.longitude = longitude
        self.altitude = altitude
        self.altitude_agl = altitude_agl  # optional

        self.sweep_number = sweep_number
        self.sweep_mode = sweep_mode
        self.fixed_angle = fixed_angle
        self.sweep_start_ray_index = sweep_start_ray_index
        self.sweep_end_ray_index = sweep_end_ray_index
        self.target_scan_rate = target_scan_rate  # optional
        self.rays_are_indexed = rays_are_indexed    # optional
        self.ray_angle_res = ray_angle_res  # optional

        self.azimuth = azimuth
        self.elevation = elevation
        self.scan_rate = scan_rate  # optional
        self.antenna_transition = antenna_transition  # optional
        self.rotation = rotation  # optional
        self.tilt = tilt  # optional
        self.roll = roll  # optional
        self.drift = drift  # optional
        self.heading = heading  # optional
        self.pitch = pitch  # optional
        self.georefs_applied = georefs_applied  # optional

        self.instrument_parameters = instrument_parameters  # optional
        self.radar_calibration = radar_calibration  # optional

        self.ngates = len(_range['data'])
        self.nrays = len(time['data'])
        self.nsweeps = len(sweep_number['data'])

    @property
    def rays_per_sweep(self):
        dic = get_metadata('rays_per_sweep')
        dic['data'] = (self.sweep_end_ray_index['data'] -
                       self.sweep_start_ray_index['data'] + 1)
        return dic

    # private functions for checking limits, etc.
    def _check_sweep_in_range(self, sweep):
        """ Check that a sweep number is in range. """
        if sweep < 0 or sweep >= self.nsweeps:
            raise IndexError('Sweep out of range: ', sweep)
        return

    # public check functions
    def check_field_exists(self, field_name):
        """
        Check that a field exists in the fields dictionary.

        If the field does not exist raise a KeyError.

        Parameters
        ----------
        field_name : str
            Name of field to check.

        """
        if field_name not in self.fields:
            raise KeyError('Field not available: ' + field_name)
        return

    # Iterators

    def iter_start(self):
        """ Return an iterator over the sweep start indices. """
        return (s for s in self.sweep_start_ray_index['data'])

    def iter_end(self):
        """ Return an iterator over the sweep end indices. """
        return (s for s in self.sweep_end_ray_index['data'])

    def iter_start_end(self):
        """ Return an iterator over the sweep start and end indices. """
        return ((s, e) for s, e in zip(self.iter_start(), self.iter_end()))

    def iter_slice(self):
        """ Return an iterator which returns sweep slice objects. """
        return (slice(s, e+1) for s, e in self.iter_start_end())

    def iter_field(self, field_name):
        """ Return an iterator which returns sweep field data. """
        self.check_field_exists(field_name)
        return (self.fields[field_name]['data'][s] for s in self.iter_slice())

    def iter_azimuth(self):
        """ Return an iterator which returns sweep azimuth data. """
        return (self.azimuth['data'][s] for s in self.iter_slice())

    def iter_elevation(self):
        """ Return an iterator which returns sweep elevation data. """
        return (self.elevation['data'][s] for s in self.iter_slice())

    # get methods

    def get_start(self, sweep):
        """ Return the starting ray index for a given sweep.  """
        self._check_sweep_in_range(sweep)
        return self.sweep_start_ray_index['data'][sweep]

    def get_end(self, sweep):
        """ Return the ending ray for a given sweep. """
        self._check_sweep_in_range(sweep)
        return self.sweep_end_ray_index['data'][sweep]

    def get_start_end(self, sweep):
        """ Return the starting and ending ray for a given sweep. """
        return self.get_start(sweep), self.get_end(sweep)

    def get_slice(self, sweep):
        """ Return a slice for selecting rays for a given sweep. """
        start, end = self.get_start_end(sweep)
        return slice(start, end+1)

    def get_field(self, sweep, field_name, copy=False):
        """
        Return the field data for a given sweep.

        Parameters
        ----------
        sweep : int
            Sweep number to retrieve data for, 0 based.
        field_name : str
            Name of the field from which data should be retrieved.
        copy : bool, optional
            True to return a copy of the data. False, the default, returns
            a view of the data (when possible), changing this data will
            change the data in the underlying Radar object.

        Returns
        -------
        data : array
            Array containing data for the requested sweep and field.

        """
        self.check_field_exists(field_name)
        s = self.get_slice(sweep)
        data = self.fields[field_name]['data'][s]
        if copy:
            return data.copy()
        else:
            return data

    def get_azimuth(self, sweep, copy=False):
        """
        Return an array of azimuth angles for a given sweep.

        Parameters
        ----------
        sweep : int
            Sweep number to retrieve data for, 0 based.
        copy : bool, optional
            True to return a copy of the azimuths. False, the default, returns
            a view of the azimuths (when possible), changing this data will
            change the data in the underlying Radar object.

        Returns
        -------
        azimuths : array
            Array containing the azimuth angles for a given sweep.

        """
        s = self.get_slice(sweep)
        azimuths = self.azimuth['data'][s]
        if copy:
            return azimuths.copy()
        else:
            return azimuths

    def get_elevation(self, sweep, copy=False):
        """
        Return an array of elevation angles for a given sweep.

        Parameters
        ----------
        sweep : int
            Sweep number to retrieve data for, 0 based.
        copy : bool, optional
            True to return a copy of the elevations. False, the default,
            returns a view of the elevations (when possible), changing this
            data will change the data in the underlying Radar object.

        Returns
        -------
        azimuths : array
            Array containing the elevation angles for a given sweep.

        """
        s = self.get_slice(sweep)
        elevation = self.elevation['data'][s]
        if copy:
            return elevation.copy()
        else:
            return elevation

    def get_nyquist_vel(self, sweep, check_uniform=True):
        """
        Return the Nyquist velocity in meters per second for a given sweep.

        Raises a LookupError if the Nyquist velocity is not available, an
        Exception is raised if the velocities are not uniform in the sweep
        unless check_uniform is set to False.

        Parameters
        ----------
        sweep : int
            Sweep number to retrieve data for, 0 based.
        check_uniform : bool
            True to check to perform a check on the Nyquist velocities that
            they are uniform in the sweep, False will skip this check and
            return the velocity of the first ray in the sweep.

        Returns
        -------
        nyquist_velocity : float
            Array containing the Nyquist velocity in m/s for a given sweep.

        """
        s = self.get_slice(sweep)
        try:
            nyq_vel = self.instrument_parameters['nyquist_velocity']['data'][s]
        except:
            raise LookupError('Nyquist velocity unavailable')
        if check_uniform:
            if np.any(nyq_vel != nyq_vel[0]):
                raise Exception('Nyquist velocities are not uniform in sweep')
        return float(nyq_vel[0])

    # Methods

    def info(self, level='standard', out=sys.stdout):
        """
        Print information on radar.

        Parameters
        ----------
        level : {'compact', 'standard', 'full', 'c', 's', 'f'}
            Level of information on radar object to print, compact is
            minimal information, standard more and full everything.
        out : file-like
            Stream to direct output to, default is to print information
            to standard out (the screen).

        """
        if level == 'c':
            level = 'compact'
        elif level == 's':
            level = 'standard'
        elif level == 'f':
            level = 'full'

        if level not in ['standard', 'compact', 'full']:
            raise ValueError('invalid level parameter')

        self._dic_info('altitude', level, out)
        self._dic_info('altitude_agl', level, out)
        self._dic_info('antenna_transition', level, out)
        self._dic_info('azimuth', level, out)
        self._dic_info('elevation', level, out)

        print('fields:', file=out)
        for field_name, field_dic in self.fields.items():
            self._dic_info(field_name, level, out, field_dic, 1)

        self._dic_info('fixed_angle', level, out)

        if self.instrument_parameters is None:
            print('instrument_parameters: None', file=out)
        else:
            print('instrument_parameters:', file=out)
            for name, dic in self.instrument_parameters.items():
                self._dic_info(name, level, out, dic, 1)

        self._dic_info('latitude', level, out)
        self._dic_info('longitude', level, out)

        print('nsweeps:', self.nsweeps, file=out)
        print('ngates:', self.ngates, file=out)
        print('nrays:', self.nrays, file=out)

        if self.radar_calibration is None:
            print('radar_calibration: None', file=out)
        else:
            print('radar_calibration:', file=out)
            for name, dic in self.radar_calibration.items():
                self._dic_info(name, level, out, dic, 1)

        self._dic_info('range', level, out)
        self._dic_info('scan_rate', level, out)
        print('scan_type:', self.scan_type, file=out)
        self._dic_info('sweep_end_ray_index', level, out)
        self._dic_info('sweep_mode', level, out)
        self._dic_info('sweep_number', level, out)
        self._dic_info('sweep_start_ray_index', level, out)
        self._dic_info('target_scan_rate', level, out)
        self._dic_info('time', level, out)

        # Airborne radar parameters
        if self.rotation is not None:
            self._dic_info('rotation', level, out)
        if self.tilt is not None:
            self._dic_info('tilt', level, out)
        if self.roll is not None:
            self._dic_info('roll', level, out)
        if self.drift is not None:
            self._dic_info('drift', level, out)
        if self.heading is not None:
            self._dic_info('heading', level, out)
        if self.pitch is not None:
            self._dic_info('pitch', level, out)
        if self.georefs_applied is not None:
            self._dic_info('georefs_applied', level, out)

        # always print out all metadata last
        self._dic_info('metadata', 'full', out)

    def _dic_info(self, attr, level, out, dic=None, ident_level=0):
        """ Print information on a dictionary attribute. """
        if dic is None:
            dic = getattr(self, attr)

        ilvl0 = '\t' * ident_level
        ilvl1 = '\t' * (ident_level + 1)

        if dic is None:
            print(attr + ': None', file=out)
            return

        # make a string summary of the data key if it exists.
        if 'data' not in dic:
            d_str = 'Missing'
        elif not isinstance(dic['data'], np.ndarray):
            d_str = '<not a ndarray>'
        else:
            data = dic['data']
            t = (data.dtype, data.shape)
            d_str = '<ndarray of type: %s and shape: %s>' % t

        # compact, only data summary
        if level == 'compact':
            print(ilvl0 + attr + ':', d_str, file=out)

        # standard, all keys, only summary for data
        elif level == 'standard':
            print(ilvl0 + attr + ':', file=out)
            print(ilvl1 + 'data:', d_str, file=out)
            for key, val in dic.items():
                if key == 'data':
                    continue
                print(ilvl1 + key + ':', val, file=out)

        # full, all keys, full data
        elif level == 'full':
            print(attr + ':', file=out)
            if 'data' in dic:
                print(ilvl1 + 'data:', dic['data'], file=out)
            for key, val in dic.items():
                if key == 'data':
                    continue
                print(ilvl1 + key + ':', val, file=out)

        return

    def add_field(self, field_name, dic, replace_existing=False):
        """
        Add a field to the object.

        Parameters
        ----------
        field_name : str
            Name of the field to add to the dictionary of fields.
        dic : dict
            Dictionary contain field data and metadata.
        replace_existing : bool
            True to replace the existing field with key field_name if it
            exists, loosing any existing data.  False will raise a ValueError
            when the field already exists.

        """
        # check that the field dictionary to add is valid
        if field_name in self.fields and replace_existing is False:
            err = 'A field with name: %s already exists' % (field_name)
            raise ValueError(err)
        if 'data' not in dic:
            raise KeyError("dic must contain a 'data' key")
        if dic['data'].shape != (self.nrays, self.ngates):
            t = (self.nrays, self.ngates)
            err = "'data' has invalid shape, should be (%i, %i)" % t
            raise ValueError(err)
        # add the field
        self.fields[field_name] = dic
        return

    def add_field_like(self, existing_field_name, field_name, data,
                       replace_existing=False):
        """
        Add a field to the object with metadata from a existing field.

        Note that the data parameter is not copied by this method.
        If data refers to a 'data' array from an existing field dictionary, a
        copy should be made within or prior to using this method.  If this is
        not done the 'data' key in both field dictionaries will point to the
        same NumPy array and modification of one will change the second.  To
        copy NumPy arrays use the copy() method.  See the Examples section
        for how to create a copy of the 'reflectivity' field as a field named
        'reflectivity_copy'.

        Parameters
        ----------
        existing_field_name : str
            Name of an existing field to take metadata from when adding
            the new field to the object.
        field_name : str
            Name of the field to add to the dictionary of fields.
        data : array
            Field data. A copy of this data is not made, see the note above.
        replace_existing : bool
            True to replace the existing field with key field_name if it
            exists, loosing any existing data.  False will raise a ValueError
            when the field already exists.

        Examples
        --------
        >>> radar.add_field_like('reflectivity', 'reflectivity_copy',
        ...                      radar.fields['reflectivity']['data'].copy())

        """
        if existing_field_name not in self.fields:
            err = 'field %s does not exist in object' % (existing_field_name)
            raise ValueError(err)
        dic = {}
        for k, v in self.fields[existing_field_name].items():
            if k != 'data':
                dic[k] = v
        dic['data'] = data
        return self.add_field(field_name, dic,
                              replace_existing=replace_existing)

    def extract_sweeps(self, sweeps):
        """
        Create a new radar contains only the data from select sweeps.

        Parameters
        ----------
        sweeps : array_like
            Sweeps (0-based) to include in new Radar object.

        Returns
        -------
        radar : Radar
            Radar object which contains a copy of data from the selected
            sweeps.

        """

        # parse and verify parameters
        sweeps = np.array(sweeps, dtype='int32')
        if np.any(sweeps > (self.nsweeps - 1)):
            raise ValueError('invalid sweeps indices in sweeps parameter')
        if np.any(sweeps < 0):
            raise ValueError('only positive sweeps can be extracted')

        def mkdic(dic, select):
            """ Make a dictionary, selecting out select from data key """
            if dic is None:
                return None
            d = dic.copy()
            if 'data' in d and select is not None:
                d['data'] = d['data'][select].copy()
            return d

        # create array of rays which select the sweeps selected and
        # the number of rays per sweep.
        ray_count = (self.sweep_end_ray_index['data'] -
                     self.sweep_start_ray_index['data'] + 1)[sweeps]
        ssri = self.sweep_start_ray_index['data'][sweeps]
        rays = np.concatenate(
            [range(s, s+e) for s, e in zip(ssri, ray_count)]).astype('int32')

        # radar location attribute dictionary selector
        if len(self.altitude['data']) == 1:
            loc_select = None
        else:
            loc_select = sweeps

        # create new dictionaries
        time = mkdic(self.time, rays)
        _range = mkdic(self.range, None)

        fields = {}
        for field_name, dic in self.fields.items():
            fields[field_name] = mkdic(dic, rays)
        metadata = mkdic(self.metadata, None)
        scan_type = str(self.scan_type)

        latitude = mkdic(self.latitude, loc_select)
        longitude = mkdic(self.longitude, loc_select)
        altitude = mkdic(self.altitude, loc_select)
        altitude_agl = mkdic(self.altitude_agl, loc_select)

        sweep_number = mkdic(self.sweep_number, sweeps)
        sweep_mode = mkdic(self.sweep_mode, sweeps)
        fixed_angle = mkdic(self.fixed_angle, sweeps)
        sweep_start_ray_index = mkdic(self.sweep_start_ray_index, None)
        sweep_start_ray_index['data'] = np.cumsum(np.append([0],
                                                  ray_count[:-1]))
        sweep_end_ray_index = mkdic(self.sweep_end_ray_index, None)
        sweep_end_ray_index['data'] = np.cumsum(ray_count) - 1
        target_scan_rate = mkdic(self.target_scan_rate, sweeps)

        azimuth = mkdic(self.azimuth, rays)
        elevation = mkdic(self.elevation, rays)
        scan_rate = mkdic(self.scan_rate, rays)
        antenna_transition = mkdic(self.antenna_transition, rays)

        # instrument_parameters
        # Filter the instrument_parameter dictionary based size of leading
        # dimension, this might not always be correct.
        if self.instrument_parameters is None:
            instrument_parameters = None
        else:
            instrument_parameters = {}
            for key, dic in self.instrument_parameters.items():
                if dic['data'].ndim != 0:
                    dim0_size = dic['data'].shape[0]
                else:
                    dim0_size = -1
                if dim0_size == self.nsweeps:
                    fdic = mkdic(dic, sweeps)
                elif dim0_size == self.nrays:
                    fdic = mkdic(dic, rays)
                else:   # keep everything
                    fdic = mkdic(dic, None)
                instrument_parameters[key] = fdic

        # radar_calibration
        # copy all field in radar_calibration as is except for
        # r_calib_index which we filter based upon time.  This might
        # leave some indices in the "r_calib" dimension not referenced in
        # the r_calib_index array.
        if self.radar_calibration is None:
            radar_calibration = None
        else:
            radar_calibration = {}
            for key, dic in self.radar_calibration.items():
                if key == 'r_calib_index':
                    radar_calibration[key] = mkdic(dic, rays)
                else:
                    radar_calibration[key] = mkdic(dic, None)

        return Radar(time, _range, fields, metadata, scan_type,
                     latitude, longitude, altitude,
                     sweep_number, sweep_mode, fixed_angle,
                     sweep_start_ray_index, sweep_end_ray_index,
                     azimuth, elevation,
                     altitude_agl=altitude_agl,
                     target_scan_rate=target_scan_rate,
                     scan_rate=scan_rate,
                     antenna_transition=antenna_transition,
                     instrument_parameters=instrument_parameters,
                     radar_calibration=radar_calibration)

def is_vpt(radar, offset=0.5):
    """
    Determine if a Radar appears to be a vertical pointing scan.

    This function only verifies that the object is a vertical pointing scan,
    use the :py:func:`to_vpt` function to convert the radar to a vpt scan
    if this function returns True.

    Parameters
    ----------
    radar : Radar
        Radar object to determine if
    offset : float
        Maximum offset of the elevation from 90 degrees to still consider
        to be vertically pointing.

    Returns
    -------
    flag : bool
        True if the radar appear to be verticle pointing, False if not.

    """
    # check that the elevation is within offset of 90 degrees.
    elev = radar.elevation['data']
    return np.all((elev < 90.0 + offset) & (elev > 90.0 - offset))

def to_vpt(radar, single_scan=True):
    """
    Convert an existing Radar object to represent a vertical pointing scan.

    This function does not verify that the Radar object contains a vertical
    pointing scan.  To perform such a check use :py:func:`is_vpt`.

    Parameters
    ----------
    radar : Radar
        Mislabeled vertical pointing scan Radar object to convert to be
        properly labeled.  This object is converted in place, no copy of
        the existing data is made.
    single_scan : bool, optional
        True to convert the volume to a single scan, any azimuth angle data
        is lost.  False will convert the scan to contain the same number of
        scans as rays, azimuth angles are retained.

    """
    if single_scan:
        nsweeps = 1
        radar.azimuth['data'][:] = 0.0
        seri = np.array([radar.nrays - 1], dtype='int32')
        radar.sweep_end_ray_index['data'] = seri
    else:
        nsweeps = radar.nrays
        # radar.azimuth not adjusted
        radar.sweep_end_ray_index['data'] = np.arange(nsweeps, dtype='int32')

    radar.scan_type = 'vpt'
    radar.nsweeps = nsweeps
    radar.target_scan_rate = None       # no scanning
    radar.elevation['data'][:] = 90.0

    radar.sweep_number['data'] = np.arange(nsweeps, dtype='int32')
    radar.sweep_mode['data'] = np.array(['vertical_pointing'] * nsweeps)
    radar.fixed_angle['data'] = np.ones(nsweeps, dtype='float32') * 90.0
    radar.sweep_start_ray_index['data'] = np.arange(nsweeps, dtype='int32')

    if radar.instrument_parameters is not None:
        for key in ['prt_mode', 'follow_mode', 'polarization_mode']:
            if key in radar.instrument_parameters:
                ip_dic = radar.instrument_parameters[key]
                ip_dic['data'] = np.array([ip_dic['data'][0]] * nsweeps)

    # Attributes that do not need any changes
    # radar.altitude
    # radar.altitude_agl
    # radar.latitude
    # radar.longitude

    # radar.range
    # radar.ngates
    # radar.nrays

    # radar.metadata
    # radar.radar_calibration

    # radar.time
    # radar.fields
    # radar.antenna_transition
    # radar.scan_rate
    return

def join_radar(radar1, radar2):

    # must have same gate spacing
    new_radar = copy.deepcopy(radar1)
    new_radar.azimuth['data'] = np.append(radar1.azimuth['data'],
                                          radar2.azimuth['data'])
    new_radar.elevation['data'] = np.append(radar1.elevation['data'],
                                            radar2.elevation['data'])

    if len(radar1.range['data']) >= len(radar2.range['data']):
        new_radar.range['data'] = radar1.range['data']
    else:
        new_radar.range['data'] = radar2.range['data']
    new_radar.time['data'] = np.append(radar1.time['data'],
                                       radar2.time['data'])

    for var in new_radar.fields.keys():
        sh1 = radar1.fields[var]['data'].shape
        sh2 = radar2.fields[var]['data'].shape
        print(sh1, sh2)
        new_field = np.ma.zeros([sh1[0] + sh2[0],
                                max([sh1[1], sh2[1]])]) - 9999.0
        new_field[0:sh1[0], 0:sh1[1]] = radar1.fields[var]['data']
        new_field[sh1[0]:, 0:sh2[1]] = radar2.fields[var]['data']
        new_radar.fields[var]['data'] = new_field

    # radar locations
    # TODO moving platforms
    lat1 = float(radar1.latitude['data'])
    lon1 = float(radar1.longitude['data'])
    alt1 = float(radar1.altitude['data'])
    lat2 = float(radar2.latitude['data'])
    lon2 = float(radar2.longitude['data'])
    alt2 = float(radar2.altitude['data'])

    if (lat1 != lat2) or (lon1 != lon2) or (alt1 != alt2):
        ones1 = np.ones(len(radar1.time['data']), dtype='float32')
        ones2 = np.ones(len(radar2.time['data']), dtype='float32')
        new_radar.latitude['data'] = np.append(ones1 * lat1, ones2 * lat2)
        new_radar.longitude['data'] = np.append(ones1 * lon1, ones2 * lon2)
        new_radar.latitude['data'] = np.append(ones1 * alt1, ones2 * alt2)
    else:
        new_radar.latitude = radar1.latitude['data']
        new_radar.longitude = radar1.latitude['data']
        new_radar.altitude = radar1.altitude['data']
    return new_radar

ext: py

max_stars_repo_name: josephhardinee/pyart

max_issues_repo_name: josephhardinee/pyart

max_forks_repo_name: josephhardinee/pyart

reason: import numpy


text:
'''
# 此程序为简易版完整训练程序

'''

# 读取 DIV2K 数据集
# 其中数据集又分为采用 bicubic 双三次采样和未知方式降采样缩小图像分辨率
# 且数据集已按照两种降低方式提前准备高清图 HR、2倍缩小图、3倍缩小图、4倍缩小图
# 故本次采用 HR 和 bicubic 降采样的 2(/3/4) 倍缩小图作为训练集和验证集

# 先采用 X2 倍缩小的图片进行训练

# ''''''''''''''''''''''''''''''cfg''''''''''''''''''''''''''''''
# 配置函数，包含各种训练参数的配置

BATCH_SIZE = 4
EPOCH_NUMBER = 5
TRAIN_ROOT = "/home/jichao/gitRes/Datasets/DIV2K/train"
TRAIN_LABEL = "/home/jichao/gitRes/Datasets/DIV2K/label"
VAL_ROOT = ""
VAL_LABEL = ""
TEST_ROOT = ""
TEST_LABEL = ""
crop_size_img = (768, 768)
crop_size_label = (1523, 1523)
# ''''''''''''''''''''''''''''''cfg''''''''''''''''''''''''''''''

# 1. 导入所需的包
import os
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms, datasets, utils
import torchvision.transforms.functional as ff
from datetime import datetime
from tensorboardX import SummaryWriter

# ''''''''''''''''''''''''''''''dataset.py''''''''''''''''''''''''''''''
# 数据预处理文件，重中之中，需要手敲一遍
# 1. 标签处理
# 2. 标签编码
# 3. 可视化编码过程
# 4. 定义预处理类

# 图片数据集处理
# return：img，label
class DIV2KDataset(Dataset):
    def __init__(self, file_path=[], crop_size_img=None, crop_size_label=None):
        """para:
            file_path(list): 数据和标签路径,列表元素第一个为图片路径，第二个为标签路径
        """
        # 1 正确读入图片和标签路径
        if len(file_path) != 2:
            raise ValueError("同时需要图片和标签文件夹的路径，图片路径在前")
        self.img_path = file_path[0]
        self.label_path = file_path[1]
        # 2 从路径中取出图片和标签数据的文件名保持到两个列表当中（程序中的数据来源）
        self.imgs = self.read_file(self.img_path)
        self.labels = self.read_file(self.label_path)
        # 3 初始化数据处理函数设置
        self.crop_size_img = crop_size_img
        self.crop_size_label = crop_size_label

    def __getitem__(self, index):
        img = self.imgs[index]
        label = self.labels[index]
        # 从文件名中读取数据（图片和标签都是png格式的图像数据）
        img = Image.open(img)
        label = Image.open(label)

        img, label = self.center_crop(img, label, crop_size_img, crop_size_label)

        img, label = self.img_transform(img, label)
        # print('处理后的图片和标签大小：',img.shape, label.shape)
        sample = {'img': img, 'label': label}

        return sample

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

    def read_file(self, path):
        """从文件夹中读取数据"""
        files_list = os.listdir(path)
        file_path_list = [os.path.join(path, img) for img in files_list]
        file_path_list.sort()
        return file_path_list

    def center_crop(self, data, label, crop_size_img, crop_size_label):
        """裁剪输入的图片和标签大小"""
        data = ff.center_crop(data, crop_size_img)
        label = ff.center_crop(label, crop_size_label)
        return data, label

    def img_transform(self, img, label):
        """对图片和标签做一些数值处理"""
        transform = transforms.Compose(
            [
                transforms.ToTensor(),
                # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
            ]
        )
        img = transform(img)
        label = transform(label)

        return img, label
# ''''''''''''''''''''''''''''''dataset.py''''''''''''''''''''''''''''''

# 2. 构建网络结构
# 网络结构，将图像变大一倍
class AnNet(nn.Module):
    def __init__(self):
        super(AnNet, self).__init__()
        self.conv1 = nn.Conv2d(3,  64, 5, 1, 0)
        self.conv2 = nn.Conv2d(64, 32, 3, 1, 0)
        self.conv3 = nn.ConvTranspose2d(32, 3,  9, 2, 4)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = self.conv3(x)
        return x

# 3. 读取数据
AnNet = AnNet()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
AnNet = AnNet.to(device=device)

optimizer = optim.Adam(AnNet.parameters(), lr=0.001)
criterion = nn.MSELoss()

train_Data = DIV2KDataset([TRAIN_ROOT, TRAIN_LABEL], crop_size_img, crop_size_label)
train_Loader = DataLoader(train_Data, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)

# 4. 训练模型
starttime = datetime.now() # 开始计时

for epoch in range(EPOCH_NUMBER):
    print("开始第 %d 轮训练"%(epoch+1))

    train_loss = 0.0

    for i_batch, sample in enumerate(train_Loader):

        img = sample["img"].to(device)
        label = sample["label"].to(device)

        pred = AnNet(img)

        loss = criterion(pred, label)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        train_loss += loss.item()

        print('epoch{}|batch[{}/{}]|batch_loss {:.8f}|'.format(epoch+1, i_batch+1, len(train_Data)/BATCH_SIZE, loss.item()))

        # 使用 TensorboardX 记录损失图像
        with SummaryWriter() as w:
            w.add_scalar('scalar/test', loss.item(), (i_batch+1+200*epoch) )
            w.add_scalar('scalar/epoch', loss.item(), (i_batch+1+200*epoch) )

        if i_batch%100 == 99:
            # 保存原图结果
            img_Save_Name = "img_Epoch" + str(epoch+1) + "_Batch" + str(i_batch+1) + ".jpg"
            utils.save_image(img, img_Save_Name)
            # 保存预测结果
            pred_Save_Name = "pred_Epoch" + str(epoch+1) + "_Batch" + str(i_batch+1) + ".jpg"
            utils.save_image(pred, pred_Save_Name)
            # 保存标签结果
            label_Save_Name = "label_Epoch" + str(epoch+1) + "_Batch" + str(i_batch+1) + ".jpg"
            utils.save_image(label, label_Save_Name)

    endtime = datetime.now() # 结束计时
    # 打印运行时间
    print("RunTime: {}h-{}m-{}s".format(endtime.hour-starttime.hour, endtime.minute-starttime.minute, endtime.second-starttime.second))

# torch.save(AnNet, "AnNet.pth")
torch.save(AnNet.state_dict(), "Epoch" + str(EPOCH_NUMBER) + ".pth")


text:
"""
========================================
Plotting topographic maps of evoked data
========================================

Load evoked data and plot topomaps for selected time points.

"""
# Authors: Christian Brodbeck <christianbrodbeck@nyu.edu>
#          Tal Linzen <linzen@nyu.edu>
#
# License: BSD (3-clause)

print(__doc__)

import numpy as np
import mne

path = mne.datasets.sample.data_path()
fname = path + '/MEG/sample/sample_audvis-ave.fif'

# load evoked and subtract baseline
evoked = mne.fiff.read_evoked(fname, 'Left Auditory', baseline=(None, 0))

# plot magnetometer data as topomap at 1 time point : 100ms
evoked.plot_topomap(0.1, ch_type='mag', size=3, colorbar=False)

# set time instants in seconds (from 50 to 150ms in a step of 10ms)
times = np.arange(0.05, 0.15, 0.01)
# If times is set to None only 10 regularly spaced topographies will be shown

# plot magnetometer data as topomaps
evoked.plot_topomap(times, ch_type='mag')

# plot gradiometer data (plots the RMS for each pair of gradiometers)
evoked.plot_topomap(times, ch_type='grad')

# add channel labels and title
evoked.plot_topomap(0.1, ch_type='mag', show_names=True, colorbar=False,
                    size=8, title='Auditory response')

max_stars_repo_name: Anevar/mne-python

max_issues_repo_name: Anevar/mne-python

max_forks_repo_name: Anevar/mne-python


text:
# 0406.py
import cv2
import numpy as np

src = cv2.imread('./data/lena.jpg', cv2.IMREAD_GRAYSCALE)
##src = cv2.imread('./data/lena.jpg', cv2.IMREAD_COLOR)
dst = np.zeros(src.shape, dtype = src.dtype)

N = 4       #8, 32, 64
height, width = src.shape       #그레이스케일 영상
##height, width, _  = src.shape #컬러영상

h = height // N
w = width // N
for i in range(N):
    for j in range(N):
        y = i*h
        x = j*w
        roi = src[y:y+h, x:x+w]
        dst[y:y+h, x:x+w] = cv2.mean(roi)[0]    #그레이스케일 영상
##        dst[y:y+h, x:x+w] = cv2.mean(roi)[0:3]  #컬러영상

cv2.imshow('dst', dst)
cv2.waitKey()
cv2.destroyAllWindows()


text:
from typing import Any, List

import numpy as np
import torch

from catalyst.metrics._metric import IMetric
from catalyst.utils.distributed import all_gather, get_rank

class ConfusionMatrixMetric(IMetric):
    """Constructs a confusion matrix for a multiclass classification problems.

    Args:
        num_classes: number of classes in the classification problem
        normalized: determines whether or not the confusion matrix is normalized or not
        compute_on_call: Boolean flag to computes and return confusion matrix during __call__.
            default: True
    """

    def __init__(self, num_classes: int, normalized: bool = False, compute_on_call: bool = True):
        """Constructs a confusion matrix for a multiclass classification problems."""
        super().__init__(compute_on_call=compute_on_call)
        self.num_classes = num_classes
        self.normalized = normalized
        self.conf = np.ndarray((num_classes, num_classes), dtype=np.int32)
        self._is_ddp = False
        self.reset()

    def reset(self) -> None:
        """Reset confusion matrix, filling it with zeros."""
        self.conf.fill(0)
        self._is_ddp = get_rank() > -1

    def update(self, predictions: torch.Tensor, targets: torch.Tensor) -> None:
        """Computes the confusion matrix of ``K x K`` size where ``K`` is no of classes.

        Args:
            predictions: Can be an N x K tensor of predicted scores
                obtained from the model for N examples and K classes
                or an N-tensor of integer values between 0 and K-1
            targets: Can be a N-tensor of integer values assumed
                to be integer values between 0 and K-1 or N x K tensor, where
                targets are assumed to be provided as one-hot vectors
        """
        predictions = predictions.cpu().numpy()
        targets = targets.cpu().numpy()

        assert (
            predictions.shape[0] == targets.shape[0]
        ), "number of targets and predicted outputs do not match"

        if np.ndim(predictions) != 1:
            assert (
                predictions.shape[1] == self.num_classes
            ), "number of predictions does not match size of confusion matrix"
            predictions = np.argmax(predictions, 1)
        else:
            assert (predictions.max() < self.num_classes) and (
                predictions.min() >= 0
            ), "predicted values are not between 1 and k"

        onehot_target = np.ndim(targets) != 1
        if onehot_target:
            assert (
                targets.shape[1] == self.num_classes
            ), "Onehot target does not match size of confusion matrix"
            assert (targets >= 0).all() and (
                targets <= 1
            ).all(), "in one-hot encoding, target values should be 0 or 1"
            assert (targets.sum(1) == 1).all(), "multilabel setting is not supported"
            targets = np.argmax(targets, 1)
        else:
            assert (predictions.max() < self.num_classes) and (
                predictions.min() >= 0
            ), "predicted values are not between 0 and k-1"

        # hack for bincounting 2 arrays together
        x = predictions + self.num_classes * targets
        bincount_2d = np.bincount(
            x.astype(np.int32), minlength=self.num_classes ** 2
        )  # noqa: WPS114
        assert bincount_2d.size == self.num_classes ** 2
        conf = bincount_2d.reshape((self.num_classes, self.num_classes))

        self.conf += conf

    def compute(self) -> Any:
        """
        Returns:
            Confusion matrix of K rows and K columns, where rows corresponds
            to ground-truth targets and columns corresponds to predicted
            targets.
        """
        if self._is_ddp:
            value: List[np.ndarray] = all_gather(self.conf)
            value: np.ndarray = np.sum(np.stack(value, axis=0), axis=0)
            self.conf = value
        if self.normalized:
            conf = self.conf.astype(np.float32)
            return conf / conf.sum(1).clip(min=1e-12)[:, None]
        else:
            return self.conf

__all__ = ["ConfusionMatrixMetric"]

max_stars_repo_name: ssktotoro/catalyst

max_issues_repo_name: ssktotoro/catalyst

max_forks_repo_name: ssktotoro/catalyst


text:
# -*- coding: utf-8 -*-
# @Author: Bao
# @Date:   2022-01-03 08:51:16
# @Last Modified by:   ADMIN
# @Last Modified time: 2022-01-19 09:02:13

'''
PTZ controller put here
Reference: https://github.com/smartsenselab/sensecam-control
'''

import cv2
import math
import numpy as np
from onvif import ONVIFCamera

class CameraControl:
    """
    Module for control cameras using Onvif
    """

    def __init__(self, ip, user, password, calibrate=True):
        self.__cam_ip = ip
        self.__cam_user = user
        self.__cam_password = password

        self.calibrate = calibrate
        # For undistord point
        if calibrate:
            parameters = np.load("app/ptz/parameters.npz")
            self.mtx   = parameters["mtx"]
            self.dist  = parameters["dist"]
            self.rvecs = parameters["rvecs"]
            self.tvecs = parameters["tvecs"]

            self.newcameramtx, _ = cv2.getOptimalNewCameraMatrix(self.mtx, self.dist, (640, 480), 1, (640, 480))

        self.h_pov = 55 / 640
        self.v_pov = 33 / 480 * (180 / 105)

    def camera_start(self):
        """
        Creates the connection to the camera using the onvif protocol

        Returns:
            Return the ptz service object and media service object
        """
        mycam = ONVIFCamera(self.__cam_ip, 80, self.__cam_user, self.__cam_password)
        print('Create media service object')
        media = mycam.create_media_service()
        print('Create ptz service object')
        ptz = mycam.create_ptz_service()
        print('Get target profile')
        media_profile = media.GetProfiles()[0]
        print('Camera working!')

        self.mycam = mycam
        self.camera_ptz = ptz
        self.camera_media_profile = media_profile
        self.camera_media = media

        return self.camera_ptz, self.camera_media_profile

    def absolute_move(self, pan: float, tilt: float, zoom: float):
        """
        Operation to move pan, tilt or zoom to a absolute destination.

        Args:
            pan: Pans the device relative to the (0,0) position.
            tilt: Tilts the device relative to the (0,0) position.
            zoom: Zooms the device n steps.

        Returns:
            Return onvif's response
        """
        request = self.camera_ptz.create_type('AbsoluteMove')
        request.ProfileToken = self.camera_media_profile.token
        request.Position = {'PanTilt': {'x': pan, 'y': tilt}, 'Zoom': zoom}
        resp = self.camera_ptz.AbsoluteMove(request)
        print('camera_command( aboslute_move(%f, %f, %f) )' %(pan, tilt, zoom))
        return resp

    def continuous_move(self, direction):
        """
        Operation for continuous Pan/Tilt and Zoom movements.

        Args:
            pan: speed of movement of Pan.
            tilt: speed of movement of Tilt.
            zoom: speed of movement of Zoom.

        Returns:
            Return onvif's response.
        """
        config_req = self.camera_ptz.create_type('GetConfigurationOptions')
        ptz_configuration_options = self.camera_ptz.GetConfigurationOptions(config_req)

        XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max
        XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min
        YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max
        YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min

        if direction.lower() in ["u","up"]:
            x, y = 0, YMAX
        elif direction.lower() in ["d","do","dow","down"]:
            x, y = 0, YMIN
        elif direction.lower() in ["l","le","lef","left"]:
            x, y = XMIN, 0
        elif direction.lower() in ["r","ri","rig","righ","right"]:
            x, y = XMAX, 0
        elif direction.lower() in ["ul"]:
            x, y = XMIN, YMAX
        elif direction.lower() in ["ur"]:
            x, y = XMAX, YMAX
        elif direction.lower() in ["dl"]:
            x, y = XMIN, YMIN
        elif direction.lower() in ["dr"]:
            x, y = XMAX, YMIN
        elif direction.lower() in ["s","st","sto","stop"]:
            self.stop_move()

        request = self.camera_ptz.create_type('ContinuousMove')
        request.ProfileToken = self.camera_media_profile.token
        request.Velocity = {'PanTilt': {'x': x, 'y': y}, 'Zoom': 0}
        resp = self.camera_ptz.ContinuousMove(request)
        print('camera_command( continuous_move(%f, %f, %f) )' %(x, y, 0))
        return resp

    def relative_move(self, pan: float, tilt: float, zoom: float):
        """
        Operation for Relative Pan/Tilt and Zoom Move.

        Args:
            pan: A positional Translation relative to the pan current position.
            tilt: A positional Translation relative to the tilt current position.
            zoom:

        Returns:
            Return onvif's response
        """
        request = self.camera_ptz.create_type('RelativeMove')
        request.ProfileToken = self.camera_media_profile.token
        request.Translation = {'PanTilt': {'x': pan, 'y': tilt}, 'Zoom': zoom}
        resp = self.camera_ptz.RelativeMove(request)
        print('camera_command( relative_move(%f, %f, %f) )' %(pan, tilt, zoom))
        return resp

    def point_move(self, x, y):
        x, y = round(x), round(y)
        """ Provide x, y coordinate on image, center image to this point """
        if self.calibrate:
            in_point = np.asarray((x, y), dtype=np.float).reshape(1, 2)
            out_point = cv2.undistortPoints(in_point, self.mtx, self.dist, P=self.newcameramtx)
            x, y = out_point[0][0]

        move_p = x  - 320 # No problem
        move_t = - (y  - 240)

        move_p *= self.h_pov
        move_t *= self.v_pov

        move_p = math.radians(move_p)
        move_t = math.radians(move_t)
        print("move_p, move_t: ", move_p, move_t)

        # move_p = round(move_p / math.pi, 5)
        # move_t = round(move_t / math.pi, 5)
        move_p = round(move_p * 0.34632, 5)
        move_t = round(move_t * 0.31831, 5)

        print("Move params: %d - %d - %.5f - %.5f" %(x, y, move_p, move_t))
        self.relative_move(move_p, move_t, 0)

    def get_cur_position(self):
        ptz_status = self.camera_ptz.GetStatus({'ProfileToken': self.camera_media_profile.token}).Position
        print('x: %.5f \ty: %.5f' %(ptz_status.PanTilt.x, ptz_status.PanTilt.y))
        current_location = dict({'p': 0, 't': 0, 'z': 0})
        current_location['p'] = ptz_status.PanTilt.x
        current_location['t'] = ptz_status.PanTilt.y
        current_location['z'] = ptz_status.Zoom.x
        return current_location

    def stop_move(self):
        """
        Operation to stop ongoing pan, tilt and zoom movements of absolute relative and continuous type.

        Returns:
            Return onvif's response
        """
        request = self.camera_ptz.create_type('Stop')
        request.ProfileToken = self.camera_media_profile.token
        resp = self.camera_ptz.Stop(request)
        print('camera_command( stop_move() )')
        return resp

    def set_home_position(self):
        """
        Operation to save current position as the home position.

        Returns:
            Return onvif's response
        """
        request = self.camera_ptz.create_type('SetHomePosition')
        request.ProfileToken = self.camera_media_profile.token
        resp = self.camera_ptz.SetHomePosition(request)
        self.camera_ptz.Stop({'ProfileToken': self.camera_media_profile.token})
        print('camera_command( set_home_position() )')
        return resp

    def go_home_position(self):
        """
        Operation to move the PTZ device to it's "home" position.

        Returns:
            Return onvif's response
        """
        request = self.camera_ptz.create_type('GotoHomePosition')
        request.ProfileToken = self.camera_media_profile.token
        resp = self.camera_ptz.GotoHomePosition(request)
        print('camera_command( go_home_position() )')
        return resp

    def get_ptz(self):
        """
        Operation to request PTZ status.

        Returns:
            Returns a list with the values of Pan, Tilt and Zoom
        """
        request = self.camera_ptz.create_type('GetStatus')
        request.ProfileToken = self.camera_media_profile.token
        ptz_status = self.camera_ptz.GetStatus(request)
        pan = ptz_status.Position.PanTilt.x
        tilt = ptz_status.Position.PanTilt.y
        zoom = ptz_status.Position.Zoom.x
        ptz_list = (pan, tilt, zoom)
        print('camera_command( get_ptz() )')
        return ptz_list

    def set_preset(self, preset_name: str):
        """
        The command saves the current device position parameters.
        Args:
            preset_name: Name for preset.

        Returns:
            Return onvif's response.
        """
        presets = CameraControl.get_preset_complete(self)
        request = self.camera_ptz.create_type('SetPreset')
        request.ProfileToken = self.camera_media_profile.token
        request.PresetName = preset_name
        print('camera_command( set_preset%s) )' %preset_name)

        for i, _ in enumerate(presets):
            if str(presets[i].Name) == preset_name:
                print(
                    'Preset (\'%s\') not created. Preset already exists!' %preset_name)
                return None

        ptz_set_preset = self.camera_ptz.SetPreset(request)
        print('Preset (\'%s\') created!' %preset_name)
        return ptz_set_preset

    def get_preset(self):
        """
        Operation to request all PTZ presets.

        Returns:
            Returns a list of tuples with the presets.
        """
        ptz_get_presets = CameraControl.get_preset_complete(self)
        print('camera_command( get_preset() )')

        presets = []
        for i, _ in enumerate(ptz_get_presets):
            presets.append((i, ptz_get_presets[i].Name))
        return presets

    def get_preset_complete(self):
        """
        Operation to request all PTZ presets.

        Returns:
            Returns the complete presets Onvif.
        """
        request = self.camera_ptz.create_type('GetPresets')
        request.ProfileToken = self.camera_media_profile.token
        ptz_get_presets = self.camera_ptz.GetPresets(request)
        return ptz_get_presets

    def remove_preset(self, preset_name: str):
        """
        Operation to remove a PTZ preset.

        Args:
            preset_name: Preset name.

        Returns:
            Return onvif's response.
        """
        presets = CameraControl.get_preset_complete(self)
        request = self.camera_ptz.create_type('RemovePreset')
        request.ProfileToken = self.camera_media_profile.token
        print('camera_command( remove_preset(%s) )', preset_name)
        for i, _ in enumerate(presets):
            if str(presets[i].Name) == preset_name:
                request.PresetToken = presets[i].token
                ptz_remove_preset = self.camera_ptz.RemovePreset(request)
                print('Preset (\'%s\') removed!' %preset_name)
                return ptz_remove_preset
        print("Preset (\'%s\') not found!" %preset_name)
        return None

    def go_to_preset(self, preset_position: str):
        """
        Operation to go to a saved preset position.

        Args:
            preset_position: preset name.

        Returns:
            Return onvif's response.
        """
        presets = CameraControl.get_preset_complete(self)
        request = self.camera_ptz.create_type('GotoPreset')
        request.ProfileToken = self.camera_media_profile.token
        print('camera_command( go_to_preset(%s) )' %preset_position)
        for i, _ in enumerate(presets):
            str1 = str(presets[i].Name)
            if str1 == preset_position:
                request.PresetToken = presets[i].token
                resp = self.camera_ptz.GotoPreset(request)
                print("Goes to (\'%s\')" %preset_position)
                return resp
        print("Preset (\'%s\') not found!" %preset_position)
        return None

max_stars_repo_name: DoriHp/PTZ-controller

max_issues_repo_name: DoriHp/PTZ-controller

max_forks_repo_name: DoriHp/PTZ-controller


text:
"""
Contains various visualization utilities.
"""
import torch
import numpy as np
import matplotlib.pyplot as plt

from typing import List,Union

import moviepy.editor as mpy

def plot_images(images, figsize=(16, 16)):
  fig=plt.figure(figsize=figsize)
  columns = len(images)

  for i, img in enumerate(images):
    img = img[:, :, :3]
    fig.add_subplot(1, columns, i+1)
    plt.grid(False)
    plt.imshow(img)
  plt.show()

def animate_frames(frames, video_path):
  def frame(t):
    t = int(t * 10.)
    if t >= len(frames):
      t = len(frames) - 1
    return frames[t]

  clip = mpy.VideoClip(frame, duration=len(frames) // 10)
  clip.write_videofile(video_path, fps=10.)

def validate_neural_painter(strokes, actions, neural_painter, checkpoints_to_test):
  for ckpt in checkpoints_to_test:
    neural_painter.load_from_train_checkpoint(ckpt)
    with torch.no_grad():
      pred_strokes = neural_painter(actions)

    plot_images(np.transpose(strokes.numpy(), [0, 2, 3, 1]))
    plot_images(np.transpose(pred_strokes.numpy(), [0, 2, 3, 1]))

def neural_painter_stroke_animation(neural_painter_fn,
                                    action_size,
                                    checkpoints_to_test,
                                    video_path,
                                    num_acs=8,
                                    duration=10.0,
                                    fps=30.0,
                                    real_env=None):
  if real_env:
    real_env.reset()
  acs = np.random.uniform(size=[num_acs, action_size])

  neural_painters = []
  for ckpt in checkpoints_to_test:
    x = neural_painter_fn()
    x.load_from_train_checkpoint(ckpt)
    neural_painters.append(x)

  def frame(t):
    t_ = t / duration
    t = np.abs((1.0 - np.cos(num_acs * np.pi * np.mod(t_, 1. / num_acs))) / 2.0)

    new_ac = (1 - t) * acs[int(np.floor(t_ * num_acs))] + t * acs[int((np.floor(t_ * num_acs) + 1) % num_acs)]
    if real_env:
      real_env.draw(new_ac)
      im = real_env.image
      im = im[:, :, :3]
    stack_these = []
    for neural_painter in neural_painters:
      with torch.no_grad():
        decoded = neural_painter(torch.FloatTensor([new_ac]))
      decoded = np.transpose(decoded.numpy(), [0, 2, 3, 1])[0]
      decoded = (decoded * 255).astype(np.uint8)
      if real_env:
        decoded = np.concatenate([im, decoded], 1)
      stack_these.append(decoded)
    return np.concatenate(stack_these, axis=0)

  clip = mpy.VideoClip(frame, duration=duration)
  clip.write_videofile(video_path, fps=fps)
  print("written to {}".format(video_path))

def animate_strokes_on_canvas(intermediate_canvases: List[torch.Tensor],
                              target_image: Union[torch.tensor, None],
                              video_path: str, skip_every_n: int = 1,
                              batch_idx: int = 0):
  _, _, h, w = intermediate_canvases[0].shape

  # We take only one sample of the batch for each step and concatenate everything into a numpy array
  intermediate_canvases = [(x.detach().cpu().numpy() * 255).astype(np.uint8).transpose(0, 2, 3, 1)[batch_idx]
                           for x in intermediate_canvases]
  intermediate_canvases = np.stack(intermediate_canvases)
  intermediate_canvases = intermediate_canvases[::skip_every_n]

  to_plot = intermediate_canvases

  if target_image is not None:
    target_images = (target_image.detach().cpu().numpy() * 255).astype(np.uint8).reshape(1, 3, h, w).transpose(0, 2, 3, 1)
    target_images = np.tile(target_images, [len(intermediate_canvases), 1, 1, 1])
    to_plot = np.concatenate([target_images, to_plot], axis=(2 if h >= w else 1))

  to_plot = np.concatenate([to_plot, np.tile(to_plot[-1:, :, :, :], [50, 1, 1, 1])], axis=0)

  animate_frames(to_plot, video_path)

max_stars_repo_name: GSSJacky/neural-painters-pytorch

max_issues_repo_name: GSSJacky/neural-painters-pytorch

max_forks_repo_name: GSSJacky/neural-painters-pytorch


text:
#! /usr/bin/env python

import os
os.environ["CUDA_VISIBLE_DEVICES"]="0,-1"
import numpy as np
import tensorflow as tf

#from tensorflow.python.client import device_lib
#print(device_lib.list_local_devices())

from datetime import datetime
import h5py
from scipy.io import loadmat,savemat

from cnn_1d_sleep import CNN1DSleep
from cnn_1d_sleep_config import Config

from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import cohen_kappa_score

from datagenerator_from_list_v2 import DataGenerator

# Parameters
# ==================================================

#tf.app.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement")
tf.app.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement")
tf.app.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices")

# My Parameters
tf.app.flags.DEFINE_string("eeg_train_data", "../train_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("eeg_test_data", "../test_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("eog_train_data", "../train_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("eog_test_data", "../test_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("emg_train_data", "../train_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("emg_test_data", "../test_data.mat", "Point to directory of input data")
tf.app.flags.DEFINE_string("out_dir", "./output/", "Point to output directory")
tf.app.flags.DEFINE_string("checkpoint_dir", "./checkpoint/", "Point to checkpoint directory")

tf.app.flags.DEFINE_float("dropout_keep_prob", 0.5, "Dropout keep probability (default: 0.5)")
tf.flags.DEFINE_integer("num_filter", 500, "Number of filters per filter size (default: 400)")

tf.app.flags.DEFINE_string("eeg_pretrainedfb_path", "./output/filterbank.mat", "Point to the pretrainedfb mat file")
tf.app.flags.DEFINE_string("eog_pretrainedfb_path", "./output/filterbank.mat", "Point to the pretrainedfb mat file")
tf.app.flags.DEFINE_string("emg_pretrainedfb_path", "./output/filterbank.mat", "Point to the pretrainedfb mat file")

FLAGS = tf.app.flags.FLAGS
print("\nParameters:")
for attr, value in sorted(FLAGS.__flags.items()): # python3
    print("{}={}".format(attr.upper(), value))
print("")

out_path = os.path.abspath(os.path.join(os.path.curdir,FLAGS.out_dir))
checkpoint_path = os.path.abspath(os.path.join(out_path,FLAGS.checkpoint_dir))

config = Config()
config.dropout_keep_prob = FLAGS.dropout_keep_prob
config.num_filters = FLAGS.num_filter

eeg_active = ((FLAGS.eeg_train_data != "") & (FLAGS.eeg_test_data != ""))
eog_active = ((FLAGS.eog_train_data != "") & (FLAGS.eog_test_data != ""))
emg_active = ((FLAGS.emg_train_data != "") & (FLAGS.emg_test_data != ""))

if (eeg_active):
    print("eeg active")
    # Initalize the data generator seperately for the training, test sets
    # We load the training data here just for normalization purpose
    eeg_train_gen = DataGenerator(os.path.abspath(FLAGS.eeg_train_data), data_shape=[config.time_length, config.freq_length], shuffle = False)
    eeg_test_gen = DataGenerator(os.path.abspath(FLAGS.eeg_test_data), data_shape=[config.time_length, config.freq_length], shuffle = False, test_mode=True)

    # load pretrained filterbank and do filtering first
    eeg_filter = loadmat(FLAGS.eeg_pretrainedfb_path)
    Wfb = eeg_filter['Wfb']
    eeg_train_gen.filter_with_filterbank(Wfb)
    eeg_test_gen.filter_with_filterbank(Wfb)
    del Wfb

if (eog_active):
    print("eog active")
    # Initalize the data generator seperately for the training, test sets
    eog_train_gen = DataGenerator(os.path.abspath(FLAGS.eog_train_data), data_shape=[config.time_length, config.freq_length], shuffle = False)
    eog_test_gen = DataGenerator(os.path.abspath(FLAGS.eog_test_data), data_shape=[config.time_length, config.freq_length], shuffle = False, test_mode=True)

    # do filtering first
    eog_filter = loadmat(FLAGS.eog_pretrainedfb_path)
    Wfb = eog_filter['Wfb']
    eog_train_gen.filter_with_filterbank(Wfb)
    eog_test_gen.filter_with_filterbank(Wfb)
    del Wfb

if (emg_active):
    print("emg active")
    # Initalize the data generator seperately for the training, test sets
    emg_train_gen = DataGenerator(os.path.abspath(FLAGS.emg_train_data), data_shape=[config.time_length, config.freq_length], shuffle = False)
    emg_test_gen = DataGenerator(os.path.abspath(FLAGS.emg_test_data), data_shape=[config.time_length, config.freq_length], shuffle = False, test_mode=True)

    # do filtering first
    emg_filter = loadmat(FLAGS.emg_pretrainedfb_path)
    Wfb = emg_filter['Wfb']
    emg_train_gen.filter_with_filterbank(Wfb)
    emg_test_gen.filter_with_filterbank(Wfb)
    del Wfb

# eeg always active
train_generator = eeg_train_gen
test_generator = eeg_test_gen

# concatenate different channels
if (eog_active):
    train_generator.X = np.concatenate((train_generator.X, eog_train_gen.X), axis=-1)
    train_generator.data_shape = train_generator.X.shape[1:]
    test_generator.X = np.concatenate((test_generator.X, eog_test_gen.X), axis=-1)
    test_generator.data_shape = test_generator.X.shape[1:]

if (emg_active):
    train_generator.X = np.concatenate((train_generator.X, emg_train_gen.X), axis=-1)
    train_generator.data_shape = train_generator.X.shape[1:]
    test_generator.X = np.concatenate((test_generator.X, emg_test_gen.X), axis=-1)
    test_generator.data_shape = test_generator.X.shape[1:]

del eeg_train_gen
del eeg_test_gen
if (eog_active):
    del eog_train_gen
    del eog_test_gen
if (emg_active):
    del emg_train_gen
    del emg_test_gen

# data normalization here
X = train_generator.X
X = np.reshape(X,(train_generator.data_size*train_generator.data_shape[0], train_generator.data_shape[1]))
meanX = X.mean(axis=0)
stdX = X.std(axis=0)
X = (X - meanX) / stdX
train_generator.X = np.reshape(X, (train_generator.data_size, train_generator.data_shape[0], train_generator.data_shape[1]))

X = test_generator.X
X = np.reshape(X,(test_generator.data_size*test_generator.data_shape[0], test_generator.data_shape[1]))
X = (X - meanX) / stdX
test_generator.X = np.reshape(X, (test_generator.data_size, test_generator.data_shape[0], test_generator.data_shape[1]))

# update the frequency dimension after filtering
config.freq_length = train_generator.data_shape[1]

test_batches_per_epoch = np.floor(len(test_generator.data_index) / config.batch_size).astype(np.int16)

with tf.Graph().as_default():
    session_conf = tf.ConfigProto(
      allow_soft_placement=FLAGS.allow_soft_placement,
      log_device_placement=FLAGS.log_device_placement)
    session_conf.gpu_options.allow_growth = True
    sess = tf.Session(config=session_conf)
    with sess.as_default():
        cnn = CNN1DSleep(
            time_length=config.time_length,
            freq_length=config.freq_length,
            num_classes=config.num_classes,
            filter_sizes=list(map(int, config.filter_sizes.split(","))),
            num_filters=config.num_filters,
            l2_reg_lambda=config.l2_reg_lambda)

        # Define Training procedure
        global_step = tf.Variable(0, name="global_step", trainable=False)
        optimizer = tf.train.AdamOptimizer(1e-4)
        grads_and_vars = optimizer.compute_gradients(cnn.loss)
        train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

        def dev_step(x_batch, y_batch1, y_batch2, y_batch3):
            feed_dict = {
                  cnn.input_x: x_batch,
                  cnn.input_y1: y_batch1,
                  cnn.input_y2: y_batch2,
                  cnn.input_y3: y_batch3,
                  cnn.dropout_keep_prob: 1.0
                }
            loss, acc1, pred_Y1, score1_, \
                acc2, pred_Y2, score2_,\
                acc3, pred_Y3, score3_ = sess.run(
                   [cnn.loss, cnn.accuracy1, cnn.predictions1, cnn.scores1,
                    cnn.accuracy2, cnn.predictions2, cnn.scores2,
                    cnn.accuracy3, cnn.predictions3, cnn.scores3],
                   feed_dict)
            return acc1, acc2, acc3, pred_Y1, pred_Y2, pred_Y3, score1_, score2_, score3_

        saver = tf.train.Saver(tf.all_variables())

        # Load saved model to continue training or initialize all variables
        best_dir = os.path.join(checkpoint_path, "best_model_acc")
        saver.restore(sess, best_dir)
        print("Model loaded")

        test_yhat1 = np.zeros_like(test_generator.data_index)
        score1 = np.zeros([len(test_generator.data_index),config.num_classes])
        test_yhat2 = np.zeros_like(test_generator.data_index)
        score2 = np.zeros([len(test_generator.data_index),config.num_classes])
        test_yhat3 = np.zeros_like(test_generator.data_index)
        score3 = np.zeros([len(test_generator.data_index),config.num_classes])
        test_step = 1
        while test_step < test_batches_per_epoch:
            x_batch, y_batch1, label_batch1_,\
                y_batch2, label_batch2_,\
                y_batch3, label_batch3_ = test_generator.next_batch(config.batch_size)
            x_batch = np.expand_dims(x_batch,axis=3)
            _, _, _, test_yhat1_, test_yhat2_, test_yhat3_,\
                        score1_, score2_, score3_ = dev_step(x_batch, y_batch1, y_batch2, y_batch3)
            test_yhat1[(test_step-1)*config.batch_size : test_step*config.batch_size] = test_yhat1_
            score1[(test_step-1)*config.batch_size : test_step*config.batch_size,:] = score1_
            test_yhat2[(test_step-1)*config.batch_size : test_step*config.batch_size] = test_yhat2_
            score2[(test_step-1)*config.batch_size : test_step*config.batch_size,:] = score2_
            test_yhat3[(test_step-1)*config.batch_size : test_step*config.batch_size] = test_yhat3_
            score3[(test_step-1)*config.batch_size : test_step*config.batch_size,:] = score3_
            test_step += 1
        if(test_generator.pointer < len(test_generator.data_index)):
            actual_len, x_batch, y_batch1, label_batch1_,\
                y_batch2, label_batch2_,\
                y_batch3, label_batch3_ = test_generator.rest_batch(config.batch_size)
            x_batch = np.expand_dims(x_batch,axis=3)
            _, _, _, test_yhat1_, test_yhat2_, test_yhat3_,\
                        score1_, score2_, score3_ = dev_step(x_batch, y_batch1, y_batch2, y_batch3)
            test_yhat1[(test_step-1)*config.batch_size : len(test_generator.data_index)] = test_yhat1_
            score1[(test_step-1)*config.batch_size : len(test_generator.data_index),:] = score1_
            test_yhat2[(test_step-1)*config.batch_size : len(test_generator.data_index)] = test_yhat2_
            score2[(test_step-1)*config.batch_size : len(test_generator.data_index),:] = score2_
            test_yhat3[(test_step-1)*config.batch_size : len(test_generator.data_index)] = test_yhat3_
            score3[(test_step-1)*config.batch_size : len(test_generator.data_index),:] = score3_
        test_yhat1 = test_yhat1 + 1
        test_yhat2 = test_yhat2 + 1
        test_yhat3 = test_yhat3 + 1
        test_fscore1 = f1_score(test_generator.label[:-1], test_yhat1[1:], average='macro')
        test_acc1 = accuracy_score(test_generator.label[:-1], test_yhat1[1:])
        test_kappa1 = cohen_kappa_score(test_generator.label[:-1], test_yhat1[1:])
        test_fscore2 = f1_score(test_generator.label, test_yhat2, average='macro')
        test_acc2 = accuracy_score(test_generator.label, test_yhat2)
        test_kappa2 = cohen_kappa_score(test_generator.label, test_yhat2)
        test_fscore3 = f1_score(test_generator.label[1:], test_yhat3[:-1], average='macro')
        test_acc3 = accuracy_score(test_generator.label[1:], test_yhat3[:-1])
        test_kappa3 = cohen_kappa_score(test_generator.label[1:], test_yhat3[:-1])
        savemat(os.path.join(out_path, "test_ret_model_acc.mat"), dict(acc1=test_acc1,
                                                                       fscore1=test_fscore1,
                                                                       kappa1=test_kappa1,
                                                                       acc2=test_acc2,
                                                                       fscore2=test_fscore2,
                                                                       kappa2=test_kappa2,
                                                                       acc3=test_acc3,
                                                                       fscore3=test_fscore3,
                                                                       kappa3=test_kappa3,
                                                                       yhat1=test_yhat1,
                                                                       yhat2=test_yhat2,
                                                                       yhat3=test_yhat3,
                                                                       score1 = score1,
                                                                       score2 = score2,
                                                                       score3 = score3))
        test_generator.reset_pointer()

reason: import numpy,from scipy


text:
import cv2
import numpy as np

import keras

from ..preprocessing.image_transform import resize_image_1
from ..utils.anchors import compute_overlap
from ..utils.visualization import draw_detections

def detection_inference_on_single_image(model, image, min_side=800, max_side=1333):
    """ Performs inference on a single image using a detection model

    Args
        model    : Detection model with predict method
        image    : Image in RGB format
        min_side : Minimum side length of model input
        max_side : Maximum side length of model input
    Returns
        boxes    : The bounding box axis for each detection in the format [x1, y1, x2, y2]
        scores   : The scores for each detection
        labels   : The labels for each detection
    """
    assert len(image.shape) == 3, 'Image must be of dimension 3'

    # Make copy of image
    image = image.copy()

    # Resize Image
    image, scale = resize_image_1(image, min_side=min_side, max_side=max_side)

    # Perform prediction on image
    boxes, scores, labels = model.predict(np.expand_dims(image, 0))

    # Correct boxes for scale
    boxes /= scale

    return boxes[0], scores[0], labels[0]

def detection_inference_on_batch(model, image_group, min_side=800, max_side=1333):
    """ Performs inference on a batch of images using a detection model

    Args
        model       : Detection model with predict method
        image_group : List (not array) of images in RGB format
        min_side    : Minimum side length of model input
        max_side    : Maximum side length of model input
    Returns
        boxes  : The bounding box axis for each detection in the format [x1, y1, x2, y2]
        scores : The scores for each detection
        labels : The labels for each detection
    """
    batch_size = len(image_group)
    scales = np.zeros((batch_size, 1))

    for image_index, image in enumerate(image_group):
        assert len(image == 3), 'Each image must be of dimension 3'

        # Make copy of image
        image = image.copy()

        # Resize image
        image, scale = resize_image_1(image, min_side=min_side, max_side=max_side)

        # Store image back into list
        image_group[image_index] = image
        scales[image_index] = scale

    # Find max shape
    max_shape = tuple(max(image.shape[x] for image in image_group) for x in range(3))

    # Create placeholder
    image_batch = np.zeros((batch_size,) + max_shape, dtype=keras.backend.floatx())

    # Store all images into placeholder
    for image_index, image in enumerate(image_group):
        image_batch[image_index, :image.shape[0], :image.shape[1], :image.shape[2]] = image

    # Perform prediction on batch
    boxes, scores, labels = model.predict_on_batch(image_batch)

    # Correct boxes against scales
    boxes /= scales

    return boxes, scores, labels

max_stars_repo_name: mingruimingrui/keras-FRCNN

max_issues_repo_name: mingruimingrui/keras-FRCNN

max_forks_repo_name: mingruimingrui/keras-FRCNN


text:
from __future__ import division  #brings in Python 3.0 mixed type calculation rules
import logging
import numpy as np
import pandas as pd

class ScreenipFunctions(object):
    """
    Function class for screenip.
    """

    def __init__(self):
        """Class representing the functions for screenip"""
        super(ScreenipFunctions, self).__init__()

    def fw_bird(self):
        """
        For birds, the daily water intake rate is calculated using the equation below.
        This equation is representative of passerine birds, which represent the majority
        of bird species visiting agricultural areas and which have higher daily water flux
        requirements than other birds.  As a result, the equations represent the most
        conservative estimate of pesticide concentrations in water. The resulting daily
        water intake rate for the 20 g bird is 0.0162 L.

            Flux(water) = (1.180 * BW^0.874) / 1000

            where: BW = 20 g
        """
        """
        Using fixed value to correctly handle floating point decimals as compared to spreadsheet implementation
        """
        fw_bird = 0.0162
        self.out_fw_bird = pd.Series([fw_bird for x in range(self.no_of_runs)])
        return self.out_fw_bird

    # Daily water intake rate for mammals
    def fw_mamm(self):
        """
        For mammals, the daily water intake rate is calculated using the equation below.
        This equation is representative of eutherian herbivore mammals, which have higher
        daily water flux requirements compared to other mammals that visit agricultural areas.
        The only equation that would generate higher estimates of daily water flux corresponds
        to marsupial carnivores, which are not considered to be representative of the majority
        of mammals that visit agricultural areas.  The resulting daily water intake rate for a
        1000 g mammal is 0.172 L.

            Flux(water) = (0.708 * BW^0.795) / 1000

            where: BW = 1000 g
        """
        """
        Using fixed value to correctly handle floating point decimals as compared to spreadsheet implementation
        """
        fw_mamm = 0.172
        self.out_fw_mamm = pd.Series([fw_mamm for x in range(self.no_of_runs)])
        return self.out_fw_mamm

    # Upper bound estimate of exposure for birds
    def dose_bird(self):
        """
        The model calculates the upper bound estimate of exposure in drinking water
        (dose-based; units in mg/kg-bw) by multiplying the daily water intake rate (L)
        by the chemical solubility (mg/L) and then dividing by the body weight (in kg)
        of the assessed animal (See equation below). In cases where water characteristics
        (e.g., pH) influence the solubility of a chemical in water, the user should select
        the highest available water solubility for use in SIP.

            Dose = (Flux(water) * solubility) / BW

            where: BW = body weight (kg) of the assessed bird (e.g. mallard duck, bobtail quail, other)
        """
        conv = 1000.0
        self.out_dose_bird = (self.out_fw_bird * self.solubility) / (self.bodyweight_assessed_bird / conv)
        return self.out_dose_bird

    # Upper bound estimate of exposure for mammals
    def dose_mamm(self):
        """
        The model calculates the upper bound estimate of exposure in drinking water
        (dose-based; units in mg/kg-bw) by multiplying the daily water intake rate (L)
        by the chemical solubility (mg/L) and then dividing by the body weight (in kg)
        of the assessed animal (See equation below). In cases where water characteristics
        (e.g., pH) influence the solubility of a chemical in water, the user should select
        the highest available water solubility for use in SIP.

            Dose = (Flux(water) * solubility) / BW

            where: BW = body weight (kg) of the assessed animal (e.g. laboratory rat, other)
        """
        conv = 1000.0
        self.out_dose_mamm = (self.out_fw_mamm * self.solubility) / (self.bodyweight_assessed_mammal / conv)
        return self.out_dose_mamm

    # Acute adjusted toxicity value for birds
    def at_bird(self):
        """
        LD50 values for mammals and birds are adjusted using the same approach employed
        by T-REX (USEPA 2008). These equations are provided below. In these equations,
        AT = adjusted toxicity value (mg/kg-bw); LD50 = endpoint reported by toxicity study
        (mg/kg-bw); TW = body weight of tested animal (350g rat, 1580g mallard duck, 178 g
        Northern bobwhite quail or weight defined by the model user for an alternative species);

        AT = LD50* (AW / TW)^(x-1)

        where:
            AW = body weight of assessed animal (g)
            x = Mineau scaling factor.  Chemical specific values for x may be located in the
            worksheet titled "Mineau scaling factors." If no chemical specific data are available,
            the default value of 1.15 should be used for this parameter.
        """
        self.out_at_bird = self.ld50_avian_water * (
            (self.bodyweight_assessed_bird / self.ld50_bodyweight_tested_bird) ** (self.mineau_scaling_factor - 1.))
        return self.out_at_bird

    # Acute adjusted toxicity value for mammals
    def at_mamm(self):
        """
        LD50 values for mammals and birds are adjusted using the same approach employed
        by T-REX (USEPA 2008). These equations are provided below. In these equations,
        AT = adjusted toxicity value (mg/kg-bw); LD50 = endpoint reported by toxicity study
        (mg/kg-bw); TW = body weight of tested animal (350g rat, 1580g mallard duck, 178 g
        Northern bobwhite quail or weight defined by the model user for an alternative species);

        AT = LD50* (TW / AW)^0.25

        where:
            AW = body weight of assessed animal (g)
            x = Mineau scaling factor.  Chemical specific values for x may be located in the
            worksheet titled "Mineau scaling factors." If no chemical specific data are available,
            the default value of 1.15 should be used for this parameter.
        """
        self.out_at_mamm = self.ld50_mammal_water * (
            (self.ld50_bodyweight_tested_mammal / self.bodyweight_assessed_mammal) ** 0.25)
        return self.out_at_mamm

    # Adjusted chronic toxicity values for birds
    # FI = Food Intake Rate
    def fi_bird(self, bw_grams):
        """
        Daily Food Intake Rate:

        Chronic avian toxicity studies produce endpoints based on concentration in food, not dose.
        The endpoint is a No Observed Adverse Effects Concentration (NOAEC) that is assumed to be
        relevant to all birds, regardless of body weight.  In order to convert a reported avian
        NOAEC (mg/kg-diet) value to a dose equivalent toxicity value for the assessed animal,
        the daily food (dry) intake of the test bird is considered. The daily food intake rate
        (FI; units in kg-food) of the test bird is calculated using the equation below.

        FI = 0.0582 * BW^0.651

        where:
            BW = body weight in kg (USEPA 1993). This equation corresponds to a daily food intake
            rate for all birds, which generates a lower food intake rate compared to passerines.
            The equation is more conservative because it results in a lower dose-equivalent toxicity value.
        """
        #bw_grams is the bodyweight of test bird (it's a series with a value per model simulation run)
        fi_bird = 0.0582 * ((bw_grams / 1000.) ** 0.651)
        return fi_bird

    # Dose-equivalent chronic toxicity value for birds
    def det(self):
        """
        Dose Equiv. Toxicity:

        The FI value (kg-diet) is multiplied by the reported NOAEC (mg/kg-diet) and then divided by
        the test animal's body weight to derive the dose-equivalent chronic toxicity value (mg/kg-bw):

        Dose Equiv. Toxicity = (NOAEC * FI) / BW

        NOTE: The user enters the lowest available NOAEC for the mallard duck, for the bobwhite quail,
        and for any other test species. The model calculates the dose equivalent toxicity values for
        all of the modeled values (Cells F20-24 and results worksheet) and then selects the lowest dose
        equivalent toxicity value to represent the chronic toxicity of the chemical to birds.
        """

        try:
            # Body weight of bobtail quail is 178 g (assigned across all model simulation runs)
            bw_quail_series = pd.Series([self.bodyweight_bobwhite_quail for x in range(self.no_of_runs)])
            self.out_det_quail = (self.noaec_quail * self.fi_bird(bw_quail_series)) / (bw_quail_series / 1000.)
        except Exception:
            pass
        try:
            # Body weight of mallard duck is 1580 g (assigned across all model simulation runs)
            bw_duck_series = pd.Series([self.bodyweight_mallard_duck for x in range(self.no_of_runs)])
            self.out_det_duck = (self.noaec_duck * self.fi_bird(bw_duck_series)) / (bw_duck_series / 1000.)
        except Exception:
            pass

        try:
            self.out_det_other_1 = (self.noaec_bird_other_1 * self.fi_bird(self.noaec_bodyweight_bird_other_1)) / (
                self.noaec_bodyweight_bird_other_1 / 1000.)
        except Exception:
            pass
            # self.out_det_other_1 = pd.Series(None, list(range(self.chemical_name.size)))

        try:
            self.out_det_other_2 = (self.noaec_bird_other_2 * self.fi_bird(self.noaec_bodyweight_bird_other_2)) / (
                self.noaec_bodyweight_bird_other_2 / 1000.)
        except Exception:
            pass
            # self.out_det_other_2 = pd.Series(None, list(range(self.chemical_name.size)))

        # Create DataFrame containing method Series created above
        df_noaec = pd.DataFrame({
            'out_det_quail': self.out_det_quail,
            'out_det_duck': self.out_det_duck,
            'out_det_other_1': self.out_det_other_1,
            'out_det_other_2': self.out_det_other_2
        })

        # Create a Series of the minimum values for each row/model run of the above DataFrame
        self.out_det = df_noaec.min(axis=1, numeric_only=True)

        return self.out_det

    # Adjusted chronic toxicty value for mammals
    def act(self):
        """
        SIP relies upon the No Observed Adverse Effects Level (NOAEL; mg/kg-bw) from a chronic mammalian study.
        If only a NOAEC value (in mg/kg-diet) is available, the model user should divide the NOAEC by 20 to
        determine the equivalent chronic daily dose. This approach is consistent with that of T-REX, which
        relies upon the standard FDA lab rat conversion. (USEPA 2008). Mammalian NOAEL values are adjusted
        using the same approach employed by T-REX (USEPA 2008). The equation for mammals is provided below
        (variables are defined above).

        AT = NOAEL * (TW / AW)^0.25
        """
        self.out_act = self.noael_mammal_water * (
            (self.noael_bodyweight_tested_mammal / self.bodyweight_assessed_mammal) ** 0.25)

        # MAMMILIAN:  If only a NOAEC value (in mg/kg-diet) is available, the model user should divide the NOAEC
        # by 20 to determine the equivalent chronic daily dose (NOAEL)

        return self.out_act

    # Acute exposures for birds
    def acute_bird(self):
        """
        For acute exposures, if the ratio of the upper bound dose to the adjusted LD50 value is <0.1,
        the risk assessor can conclude that pesticide exposure to mammals or birds through drinking
        water by itself is not an exposure route of concern. If the ratio of the upper bound dose to
        the adjusted LD50 value is  >=0.1, the risk assessor can conclude that pesticide exposure to
        mammals or birds through drinking water by itself is an exposure route of concern.
        """
        self.out_acute_bird = self.out_dose_bird / self.out_at_bird
        return self.out_acute_bird

    def acuconb(self):
        """
        Message stating whether or not a risk is present
        """
        msg_pass = 'Drinking water exposure alone is NOT a potential concern for birds'
        msg_fail = 'Exposure through drinking water alone is a potential concern for birds'
        boo_ratios = [ratio < 0.1 for ratio in self.out_acute_bird]
        self.out_acuconb = pd.Series([msg_pass if boo else msg_fail for boo in boo_ratios])
        #boolean = self.out_acute_bird < 0.1
        #self.out_acuconb = boolean.map(lambda x:
        #                               'Drinking water exposure alone is NOT a potential concern for birds'
        #                               if x is True else
        #                               'Exposure through drinking water alone is a potential concern for birds')
        return self.out_acuconb

    # Acute exposures for mammals
    def acute_mamm(self):
        """
        For acute exposures, if the ratio of the upper bound dose to the adjusted LD50 value is <0.1,
        the risk assessor can conclude that pesticide exposure to mammals or birds through drinking
        water by itself is not an exposure route of concern. If the ratio of the upper bound dose to
        the adjusted LD50 value is >=0.1, the risk assessor can conclude that pesticide exposure to
        mammals or birds through drinking water by itself is an exposure route of concern.
        """
        self.out_acute_mamm = self.out_dose_mamm / self.out_at_mamm
        return self.out_acute_mamm

    def acuconm(self):
        """
        Message stating whether or not a risk is present
        """
        msg_pass = 'Drinking water exposure alone is NOT a potential concern for mammals'
        msg_fail = 'Exposure through drinking water alone is a potential concern for mammals'
        boo_ratios = [ratio < 0.1 for ratio in self.out_acute_mamm]
        self.out_acuconm = pd.Series([msg_pass if boo else msg_fail for boo in boo_ratios])
        return self.out_acuconm

    # Chronic Exposures for birds
    def chron_bird(self):
        """
        For chronic exposures, if the ratio of the upper bound dose to the adjusted chronic
        toxicity value is <1, the risk assessor can conclude that pesticide exposure to mammals
        or birds through drinking water by itself is not an exposure route of concern. If the
        ratio of the upper bound dose to the adjusted chronic toxicity value is >=1, the risk
        assessor can conclude that pesticide exposure to mammals or birds through drinking water
        by itself is an exposure route of concern.
        """
        self.out_chron_bird = self.out_dose_bird / self.out_det
        return self.out_chron_bird

    def chronconb(self):
        """
        Message stating whether or not a risk is present
        """
        msg_pass = 'Drinking water exposure alone is NOT a potential concern for birds'
        msg_fail = 'Exposure through drinking water alone is a potential concern for birds'
        boo_ratios = [ratio < 1 for ratio in self.out_chron_bird]
        self.out_chronconb = pd.Series([msg_pass if boo else msg_fail for boo in boo_ratios])
        return self.out_chronconb

    # Chronic exposures for mammals
    def chron_mamm(self):
        """
        For chronic exposures, if the ratio of the upper bound dose to the adjusted chronic
        toxicity value is <1, the risk assessor can conclude that pesticide exposure to mammals
        or birds through drinking water by itself is not an exposure route of concern. If the
        ratio of the upper bound dose to the adjusted chronic toxicity value is >=1, the risk
        assessor can conclude that pesticide exposure to mammals or birds through drinking water
        by itself is an exposure route of concern.
        """
        self.out_chron_mamm = self.out_dose_mamm / self.out_act
        return self.out_chron_mamm

    def chronconm(self):
        """
        Message stating whether or not a risk is present
        """
        msg_pass = 'Drinking water exposure alone is NOT a potential concern for mammals'
        msg_fail = 'Exposure through drinking water alone is a potential concern for mammals'
        boo_ratios = [ratio < 1 for ratio in self.out_chron_mamm]
        self.out_chronconm = pd.Series([msg_pass if boo else msg_fail for boo in boo_ratios])
        return self.out_chronconm

max_stars_repo_name: qed-uber/ubertool

max_issues_repo_name: qed-uber/ubertool

max_forks_repo_name: quanted/ubertool