text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
All MLC Analyzer tab objects and functions for the main DVH Analytics bokeh program
Created on Sat Nov 3 2018
@author: Dan Cutright, PhD
"""

from __future__ import print_function
from bokeh.models.widgets import Select, Button, Div, DataTable
from bokeh.plotting import figure
from bokeh.models import Range1d, Spacer
from bokeh.layouts import row, column
import os
import numpy as np
import time
from .columns import Columns
from ..tools import mlc_analyzer as mlca
from ..tools.io.database.sql_connector import DVH_SQL
from ..tools.get_settings import get_settings, parse_settings_file
from ..tools.io.preferences.options import load_options

options = load_options()

class MLC_Analyzer:
    def __init__(self, sources, custom_title, data_tables):

        mlc_viewer_columns = Columns().mlc_viewer

        self.sources = sources

        self.mlc_data = []

        mlc_analyzer_options = [''] + sources.dvhs.data['mrn']

        self.options = [''] + sources.dvhs.data['mrn']
        self.mrn_select = Select(value='', options=mlc_analyzer_options, width=200, title='MRN')
        self.study_date_select = Select(value='', options=[''], width=200, title='Sim Study Date')
        self.uid_select = Select(value='', options=[''], width=400, title='Study Instance UID')
        self.plan_select = Select(value='', options=[''], width=200, title='Plan')
        self.fx_grp_select = Select(value='', options=[''], width=200, title='Fx Group')
        self.beam_select = Select(value='', options=[''], width=200, title='Beam')
        self.cp_select = Select(value='', options=[''], width=50, title='CP')

        self.mrn_select.on_change('value', self.mrn_ticker)
        self.study_date_select.on_change('value', self.study_date_ticker)
        self.uid_select.on_change('value', self.uid_ticker)
        self.plan_select.on_change('value', self.plan_ticker)
        self.fx_grp_select.on_change('value', self.fx_grp_ticker)
        self.beam_select.on_change('value', self.beam_ticker)
        self.cp_select.on_change('value', self.cp_ticker)

        self.mlc_viewer = figure(plot_width=500, plot_height=500, match_aspect=True,
                                 tools="crosshair,save")
        self.mlc_viewer.xaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE
        self.mlc_viewer.yaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE
        self.mlc_viewer.xaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE
        self.mlc_viewer.yaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE
        self.mlc_viewer.min_border_left = options.MIN_BORDER
        self.mlc_viewer.min_border_bottom = options.MIN_BORDER
        self.mlc_viewer.xaxis.axis_label = "X-Axis (mm)"
        self.mlc_viewer.yaxis.axis_label = "Y-Axis (mm)"
        self.mlc_viewer.quad(top='top', bottom='bottom', left='left', right='right',
                             source=self.sources.mlc_viewer, alpha=0.25, color='color')
        self.mlc_viewer_previous_cp = Button(label="<", button_type="primary", width=50)
        self.mlc_viewer_next_cp = Button(label=">", button_type="primary", width=50)
        self.mlc_viewer_play_button = Button(label="Play", button_type="success", width=100)
        self.mlc_viewer_beam_score = Div(text="Beam Complexity Score: ", width=300)

        self.mlc_viewer_data_table = DataTable(source=self.sources.mlc_summary, columns=mlc_viewer_columns,
                                               editable=False, width=700, height=425, index_position=None)

        self.mlc_viewer_previous_cp.on_click(self.mlc_viewer_go_to_previous_cp)
        self.mlc_viewer_next_cp.on_click(self.mlc_viewer_go_to_next_cp)
        self.mlc_viewer_play_button.on_click(self.mlc_viewer_play)
        self.sources.mlc_summary.selected.on_change('indices', self.update_cp_on_selection)

        self.mlc_viewer.x_range = Range1d(-options.MAX_FIELD_SIZE_X / 2, options.MAX_FIELD_SIZE_X / 2)
        self.mlc_viewer.y_range = Range1d(-options.MAX_FIELD_SIZE_Y / 2, options.MAX_FIELD_SIZE_Y / 2)
        self.mlc_viewer.xgrid.grid_line_color = None
        self.mlc_viewer.ygrid.grid_line_color = None

        self.layout = column(Div(text="DVH Analytics v%s" % options.VERSION),
                             row(custom_title['1']['mlc_analyzer'], Spacer(width=50),
                                 custom_title['2']['mlc_analyzer']),
                             row(self.mrn_select, self.study_date_select,
                                 self.uid_select),
                             row(self.plan_select),
                             Div(text="
", width=800),
                             row(self.fx_grp_select, self.beam_select,
                                 self.mlc_viewer_previous_cp, self.mlc_viewer_next_cp,
                                 Spacer(width=10), self.mlc_viewer_play_button, Spacer(width=10),
                                 self.mlc_viewer_beam_score),
                             row(self.mlc_viewer, data_tables.mlc_viewer))

    def mrn_ticker(self, attr, old, new):
        if new == '':
            self.study_date_select.options = ['']
            self.study_date_select.value = ''
            self.uid_select.options = ['']
            self.uid_select.value = ''

        else:
            new_options = []
            for i in range(len(self.sources.plans.data['mrn'])):
                if self.sources.plans.data['mrn'][i] == new:
                    new_options.append(self.sources.plans.data['sim_study_date'][i])
                    new_options.sort()
            old_sim_date = self.study_date_select.value
            self.study_date_select.options = new_options
            self.study_date_select.value = new_options[0]
            if old_sim_date == new_options[0]:
                self.update_uid()

    def study_date_ticker(self, attr, old, new):
        self.update_uid()

    def uid_ticker(self, attr, old, new):
        rt_plan = DVH_SQL().query('DICOM_Files', 'folder_path, plan_file', "study_instance_uid = '%s'" % new)[0]
        folder_path = rt_plan[0]
        if not os.path.isdir(folder_path):
            folder_path = os.path.join(parse_settings_file(get_settings('import'))['imported'],
                                       os.path.split(folder_path)[-1])
        plan_files = [os.path.join(folder_path, file_path) for file_path in rt_plan[1].split(', ')]

        self.plan_select.options = plan_files
        self.plan_select.value = plan_files[0]

    def plan_ticker(self, attr, old, new):
        if os.path.isfile(new):
            try:
                self.mlc_data = mlca.Plan(new)
            except:
                self.mlc_data = []
                print('MLC import failure: %s' % new)
            self.fx_grp_select.options = [str(i + 1) for i in range(len(self.mlc_data.fx_group))]
            if self.fx_grp_select.value == self.fx_grp_select.options[0]:
                self.update_beam()
            else:
                self.fx_grp_select.value = '1'
        else:
            self.mlc_data = []
            self.fx_grp_select.options = ['']
            self.fx_grp_select.value = ''
            self.mlc_viewer_beam_score.text = 'DICOM plan file not found!!!'

    def fx_grp_ticker(self, attr, old, new):
        self.update_fx_grp()
        if old == new:
            self.update_beam()

    def beam_ticker(self, attr, old, new):
        self.update_beam()

        # update beam score
        scores = self.sources.mlc_summary.data['cmp_score']
        mu = self.sources.mlc_summary.data['cum_mu'][-1]
        score = np.sum(scores) / float(mu)
        self.mlc_viewer_beam_score.text = "Beam Complexity Score: %s" % round(score, 3)

    def cp_ticker(self, attr, old, new):
        self.update_mlc_viewer()
        self.sources.mlc_summary.selected.indices = [int(new) - 1]

    def update_mrn(self):
        new_options = [mrn for mrn in self.sources.plans.data['mrn']]
        if new_options:
            new_options.sort()
            self.mrn_select.options = new_options
            self.mrn_select.value = new_options[0]
        else:
            self.mrn_select.options = ['']
            self.mrn_select.value = ''

    def update_uid(self):
        if self.mrn_select.value != '':
            new_options = []
            for i in range(len(self.sources.plans.data['mrn'])):
                if self.sources.plans.data['mrn'][i] == self.mrn_select.value and \
                                self.sources.plans.data['sim_study_date'][i] == self.study_date_select.value:
                    new_options.append(self.sources.plans.data['uid'][i])
            self.uid_select.options = new_options
            self.uid_select.value = new_options[0]

        self.update_fx_grp()

    def update_fx_grp(self):
        if self.mlc_data:
            fx_grp = self.mlc_data.fx_group[int(self.fx_grp_select.value) - 1]
            beam_options = ["%s: %s" % (i+1, j) for i, j in enumerate(fx_grp.beam_names)]
            self.beam_select.options = beam_options
            if self.beam_select.value == self.beam_select.options[0]:
                self.update_beam()
            else:
                self.beam_select.value = beam_options[0]

    def update_beam(self):
        if self.mlc_data:
            fx_grp = self.mlc_data.fx_group[int(self.fx_grp_select.value) - 1]
            beam_number = int(self.beam_select.value.split(':')[0])
            beam = fx_grp.beam[beam_number-1]
            cp_count = beam.control_point_count
            cp_numbers = [str(i + 1) for i in range(cp_count)]
            self.cp_select.options = cp_numbers
            if self.cp_select.value == self.cp_select.options[0]:
                self.update_mlc_viewer()
            else:
                self.cp_select.value = cp_numbers[0]
            self.sources.mlc_summary.data = beam.summary

    def update_mlc_viewer(self):
        if self.mlc_data:
            fx_grp = self.mlc_data.fx_group[int(self.fx_grp_select.value) - 1]
            beam_number = int(self.beam_select.value.split(':')[0])
            beam = fx_grp.beam[beam_number - 1]
            cp_index = int(self.cp_select.value) - 1

            x_min, x_max = -options.MAX_FIELD_SIZE_X / 2, options.MAX_FIELD_SIZE_X / 2
            y_min, y_max = -options.MAX_FIELD_SIZE_Y / 2, options.MAX_FIELD_SIZE_Y / 2

            borders = {'top': [y_max, y_max, y_max, beam.jaws[cp_index]['y_min']],
                       'bottom': [y_min, y_min, beam.jaws[cp_index]['y_max'], y_min],
                       'left': [x_min, beam.jaws[cp_index]['x_max'], x_min, x_min],
                       'right': [beam.jaws[cp_index]['x_min'], x_max, x_max, x_max]}
            for edge in list(borders):
                borders[edge].extend(beam.mlc_borders[cp_index][edge])
            borders['color'] = [options.JAW_COLOR] * 4 + [options.MLC_COLOR] * len(beam.mlc_borders[cp_index]['top'])

        self.sources.mlc_viewer.data = borders

    def mlc_viewer_go_to_previous_cp(self):
        index = self.cp_select.options.index(self.cp_select.value)
        self.cp_select.value = self.cp_select.options[index - 1]

    def mlc_viewer_go_to_next_cp(self):
        index = self.cp_select.options.index(self.cp_select.value)
        if index + 1 == len(self.cp_select.options):
            index = -1
        self.cp_select.value = self.cp_select.options[index + 1]

    def mlc_viewer_play(self):
        if self.cp_select.value == self.cp_select.options[-1]:
            self.cp_select.value = self.cp_select.options[0]
        start = self.cp_select.options.index(self.cp_select.value)
        end = len(self.cp_select.options) - 1

        for i in range(start, end):
            self.mlc_viewer_go_to_next_cp()
            time.sleep(options.CP_TIME_SPACING)

    def update_cp_on_selection(self, attr, old, new):
        if new:
            self.cp_select.value = self.cp_select.options[min(new)]

max_stars_repo_name: cutright/DVH-Analytics-Bokeh

max_issues_repo_name: cutright/DVH-Analytics-Bokeh

max_forks_repo_name: cutright/DVH-Analytics-Bokeh


text:
#   Copyright 2017 ProjectQ-Framework (www.projectq.ch)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

"""Transformations acting on operators and RDMs."""
from __future__ import absolute_import

import itertools

import numpy
from future.utils import iteritems
from projectq.ops import QubitOperator

from fermilib.ops import (FermionOperator,
                          normal_ordered,
                          InteractionOperator,
                          InteractionRDM)
from fermilib.ops._interaction_operator import InteractionOperatorError
from fermilib.utils import (count_qubits,
                            jordan_wigner_sparse,
                            qubit_operator_sparse)

def get_sparse_operator(operator, n_qubits=None):
    """Map a Fermion, Qubit, or InteractionOperator to a SparseOperator."""
    if isinstance(operator, InteractionOperator):
        return get_sparse_interaction_operator(operator)
    elif isinstance(operator, FermionOperator):
        return jordan_wigner_sparse(operator, n_qubits)
    elif isinstance(operator, QubitOperator):
        if n_qubits is None:
            n_qubits = count_qubits(operator)
        return qubit_operator_sparse(operator, n_qubits)

def get_sparse_interaction_operator(interaction_operator):
    fermion_operator = get_fermion_operator(interaction_operator)
    sparse_operator = jordan_wigner_sparse(fermion_operator)
    return sparse_operator

def get_interaction_rdm(qubit_operator, n_qubits=None):
    """Build an InteractionRDM from measured qubit operators.

    Returns: An InteractionRDM object.
    """
    # Avoid circular import.
    from fermilib.transforms import jordan_wigner
    if n_qubits is None:
        n_qubits = count_qubits(qubit_operator)
    one_rdm = numpy.zeros((n_qubits,) * 2, dtype=complex)
    two_rdm = numpy.zeros((n_qubits,) * 4, dtype=complex)

    # One-RDM.
    for i, j in itertools.product(range(n_qubits), repeat=2):
        transformed_operator = jordan_wigner(FermionOperator(((i, 1), (j, 0))))
        for term, coefficient in iteritems(transformed_operator.terms):
            if term in qubit_operator.terms:
                one_rdm[i, j] += coefficient * qubit_operator.terms[term]

    # Two-RDM.
    for i, j, k, l in itertools.product(range(n_qubits), repeat=4):
        transformed_operator = jordan_wigner(FermionOperator(((i, 1), (j, 1),
                                                              (k, 0), (l, 0))))
        for term, coefficient in iteritems(transformed_operator.terms):
            if term in qubit_operator.terms:
                two_rdm[i, j, k, l] += coefficient * qubit_operator.terms[term]

    return InteractionRDM(one_rdm, two_rdm)

def get_interaction_operator(fermion_operator, n_qubits=None):
    """Convert a 2-body fermionic operator to InteractionOperator.

    This function should only be called on fermionic operators which
    consist of only a_p^\dagger a_q and a_p^\dagger a_q^\dagger a_r a_s
    terms. The one-body terms are stored in a matrix, one_body[p, q], and
    the two-body terms are stored in a tensor, two_body[p, q, r, s].

    Returns:
       interaction_operator: An instance of the InteractionOperator class.

    Raises:
        TypeError: Input must be a FermionOperator.
        TypeError: FermionOperator does not map to InteractionOperator.

    Warning:
        Even assuming that each creation or annihilation operator appears
        at most a constant number of times in the original operator, the
        runtime of this method is exponential in the number of qubits.
    """
    if not isinstance(fermion_operator, FermionOperator):
        raise TypeError('Input must be a FermionOperator.')

    if n_qubits is None:
        n_qubits = count_qubits(fermion_operator)
    if n_qubits < count_qubits(fermion_operator):
        raise ValueError('Invalid number of qubits specified.')

    # Normal order the terms and initialize.
    fermion_operator = normal_ordered(fermion_operator)
    constant = 0.
    one_body = numpy.zeros((n_qubits, n_qubits), complex)
    two_body = numpy.zeros((n_qubits, n_qubits,
                            n_qubits, n_qubits), complex)

    # Loop through terms and assign to matrix.
    for term in fermion_operator.terms:
        coefficient = fermion_operator.terms[term]

        # Handle constant shift.
        if len(term) == 0:
            constant = coefficient

        elif len(term) == 2:
            # Handle one-body terms.
            if [operator[1] for operator in term] == [1, 0]:
                p, q = [operator[0] for operator in term]
                one_body[p, q] = coefficient
            else:
                raise InteractionOperatorError('FermionOperator does not map '
                                               'to InteractionOperator.')

        elif len(term) == 4:
            # Handle two-body terms.
            if [operator[1] for operator in term] == [1, 1, 0, 0]:
                p, q, r, s = [operator[0] for operator in term]
                two_body[p, q, r, s] = coefficient
            else:
                raise InteractionOperatorError('FermionOperator does not map '
                                               'to InteractionOperator.')

        else:
            # Handle non-molecular Hamiltonian.
            raise InteractionOperatorError('FermionOperator does not map '
                                           'to InteractionOperator.')

    # Form InteractionOperator and return.
    interaction_operator = InteractionOperator(constant, one_body, two_body)
    return interaction_operator

def get_fermion_operator(interaction_operator):
    """Output InteractionOperator as instance of FermionOperator class.

    Returns:
        fermion_operator: An instance of the FermionOperator class.
    """
    # Initialize with identity term.
    fermion_operator = FermionOperator((), interaction_operator.constant)
    n_qubits = count_qubits(interaction_operator)

    # Add one-body terms.
    for p, q in itertools.product(range(n_qubits), repeat=2):
        fermion_operator += FermionOperator(
            ((p, 1), (q, 0)), coefficient=interaction_operator[p, q])

    # Add two-body terms.
    for p, q, r, s in itertools.product(range(n_qubits), repeat=4):
        coefficient = interaction_operator[p, q, r, s]
        fermion_operator += FermionOperator(((p, 1), (q, 1),
                                             (r, 0), (s, 0)),
                                            coefficient)

    return fermion_operator

ext: py

max_stars_repo_name: ProjectQ-Framework/FermiLib

max_issues_repo_name: ProjectQ-Framework/FermiLib

max_forks_repo_name: ProjectQ-Framework/FermiLib

reason: import numpy


text:
from queue import Empty
import numpy as np
import time
import datetime
import pickle
from collections import deque
from itertools import islice, chain
import logging
# import niVis.Leap as Leap
import ctypes, struct
from ctypes import addressof
import multiprocessing

class NotEnoughDataError(Exception):
    pass

class RingBuffer:
    # A ring buffer to hold numpy array

    def __init__(self,shape,dtype=float,dt=1,start_timestamp=0):
        #shape is the shape of the ring buffer, as in the shape argument to np.zeros
        # the shape is assume to be time x dimension
        self.length = shape[0]
        self.absWriteHead = 0 #the absolute write head
        self.absReadHead = 0
        self.absRecordHead = 0
        self.buffer = np.zeros(shape,dtype=dtype)
        self.timestamps = np.zeros((self.length,)) #time stamp of the data, will be tracked automatically
        self.timestamps[0] = start_timestamp
        self.dt = dt #dt of each data point
        self.start_timestamp = start_timestamp
        self.latest_time = self.start_timestamp
        self.isRecordingEnabled = False
        self.dataFile = None
        self.recordingBlockSize = self.length//10 #how many data to write to the disk each time
        self.dtype = dtype

    def set_buffer_size(self,shape,dtype=float,dt=1,start_timestamp = 0):
        # reset the buffer size
        self.buffer = np.zeros(shape,dtype=dtype)
        self.start_timestamp = start_timestamp
        self.timestamp = np.zeros((self.length,))
        self.dt = dt
        self.length = shape[0]
        self.absWriteHead = 0 #the absolute write head
        self.absReadHead = 0
        self.absRecordHead = 0
        self.isRecordingEnabled = False
        self.dataFile = None
        self.recordingBlockSize = self.length//10 #how many data to write to the disk each time
        self.dtype = dtype

    def writeHead(self):
        return self.absWriteHead % self.length

    def write(self,xs):
        # Write the array to the Ring buffer
        # The format should be in (time x channel)
        # Portion of the array will wrap back to the beginning if it exceeds the buffer length
        if self.dtype==bytes:
            xsLength = len(xs)
        else:
            xsLength = xs.shape[0]
            if self.buffer.shape[1:] != xs.shape[1:] :
                raise ValueError('Shape mismatch')

        # TODO: may need to so things differently for bytes data
        ts_segment = np.linspace(0, xs.shape[0]*self.dt, xs.shape[0])
        ts_segment += self.latest_time
        self.latest_time += xs.shape[0]*self.dt

        if (self.writeHead()+xsLength > self.length):
            # need to wrap around to the start

            data2write = self.length - self.writeHead()
            remainData = xsLength - data2write
            if self.dtype == bytes:
                self.buffer[self.writeHead():,:] = self.bytes2numpy(xs[:data2write])
                self.buffer[:remainData,:] = self.bytes2numpy(xs[data2write:])
            else:
                self.buffer[self.writeHead():,:] = xs[:data2write,:]
                self.buffer[:remainData,:] = xs[data2write:,:]

                #keep track of time
                # self.timestamps[self.writeHead:] = ts_segment[:data2write]
                # self.timestamps[:remainData] = ts_segment[data2write:]
        else:
            if self.dtype == bytes:
                self.buffer[self.writeHead():self.writeHead()+xsLength,:] = self.bytes2numpy(xs)
            else:
                self.buffer[self.writeHead():self.writeHead()+xsLength,:] = xs
                self.timestamps[self.writeHead():self.writeHead()+xsLength] = ts_segment

        self.absWriteHead += xsLength

        len2read = self.absWriteHead - self.absRecordHead

        if self.isRecordingEnabled and (len2read > self.recordingBlockSize):
            #write the data to file
            if len2read>self.length:
                #if the record head is too far behind
                x = self.readLatest(self.length)
                self.absRecordHead = self.absWriteHead #update the record head
            else:
                x = self.readLatest(len2read)
                self.absRecordHead += len2read

            self.write2file(x)

    def readContinous(self,length,wait=False,timeoutCount=np.inf, verbose=False):
        #Read continous data from the beginning
        count =0
        if wait:
            #wait till data is ready
            while((self.absWriteHead-self.absReadHead)<length):
                if verbose:
                    print(f'write: {self.absWriteHead} read: {self.absReadHead}')
                time.sleep(0.01)
                count +=1
                if count>timeoutCount:
                    raise RuntimeError('Buffer read timeout!')
        else:
            if (self.absWriteHead-self.absReadHead)<length:
                raise NotEnoughDataError

        x = self.read(self.absReadHead, length)
        self.absReadHead+=length
        return x

    def readLatest(self, length=None, return_timestamps=False, verbose=False):

        if length is None:
            length = min(self.absWriteHead,self.length)

        #return the latest data
        if length > self.length:
            raise ValueError('Size request larger than buffer size')

        idx = (self.writeHead()- np.arange(length,0,-1)) % self.length #find index relative to the start of buffer

        if verbose:
            print(f'writeHead: {self.writeHead()} idx: {idx}')

        if not return_timestamps:
            return self.buffer[idx,:]
        else:
            return (self.buffer[idx,:], self.timestamps[idx])

    def bytes2numpy(self, b):
        return np.expand_dims(np.frombuffer(b,dtype='S1', count=len(b)),1)

    def getWrappedData(self,x, localStart,length2read):
        #Get data from x, if the length2read is longer than the array length, wrap around to the beginning
        # x should be in the format (time, channel)
        length = x.shape[0]
        chunk1 = x[localStart:,:]
        chunk2 = x[:(length2read - (length-localStart)),:]
        chunk = np.vstack([chunk1,chunk2])
        assert chunk.shape[0]==length2read
        return chunk

    def read(self,absStart,range,return_timstamps=False):
        #read the data from the buffer as specified by the start index (from the very beginning)
        #and its range
        if absStart < (self.absWriteHead - self.length):
            raise ValueError('Data already overwritten')

        # Over wrapping to old data
        length2read = min(range,self.absWriteHead-absStart)

        # idx = np.arange(absStart,absStart+length2read)
        # return self.buffer[idx % self.length,:]

        # use a more efficient read
        localStart = absStart % self.length
        if (localStart + length2read) > self.length:
            chunk1 = self.buffer[localStart:,:]
            chunk2 = self.buffer[:(length2read - (self.length-localStart)),:]
            chunk = np.vstack([chunk1,chunk2])
            assert chunk.shape[0]==length2read

            if not return_timstamps:
                return chunk
            else:
                #also assemble the timestamp
                ts= self.getWrappedData(self.timestamps,localStart,length2read)
                return (chunk,ts)

        else:
            if not return_timstamps:
                return self.buffer[localStart:localStart+length2read,:]
            else:
                ts = self.timestamps[localStart:localStart+length2read]
                return (self.buffer[localStart:localStart+length2read,:], ts)

    def startRecording(self, filename=None, recordingBlockSize=None):
        if recordingBlockSize:
            self.recordingBlockSize = recordingBlockSize

        if self.recordingBlockSize > self.length:
            raise ValueError('Record block size is larger than buffer size')

        if not self.isRecordingEnabled:
            #start recording, create a data file
            if not filename:
                ts = time.time()
                filename = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')+'.pkl'
            self.dataFile = open(filename,'wb')
            self.absRecordHead = self.absWriteHead
            self.isRecordingEnabled = True

            return filename

    def stopRecording(self):
        if self.isRecordingEnabled:
            self.isRecordingEnabled = False

            #Flush the remaining data
            segment = self.absWriteHead - self.absRecordHead
            x = self.readLatest(segment)
            self.write2file(x)

            #close file and stop recording
            self.dataFile.close()
            self.dataFile = None

    def write2file(self,x):
        #write data to file
        pickle.dump(x,self.dataFile)

    @staticmethod
    def readPickleStreamFile(filename):
        x = []
        segmentCount = 0
        # fastest way is to append then concatenate together
        with open(filename,'rb') as f:
            while True:
                try:
                    x.append(pickle.load(f))
                    segmentCount += 1
                    # print(f'{segmentCount}')
                except EOFError:
                    break

        print(f'Total segment read: {segmentCount}')
        return np.vstack(x)

max_stars_repo_name: teristam/pyneurode

max_issues_repo_name: teristam/pyneurode

max_forks_repo_name: teristam/pyneurode


text:
# Copyright 2021 Alibaba Group Holding Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
"""Test for hook of session run."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
from distutils.version import LooseVersion as Version
import six
import numpy as np
import tensorflow as tf
from tensorflow.python.platform import test
from tensorflow.python.framework.versions import __version__

import epl
from epl.parallel.hooks import _append_replicated_fetches

# pylint: disable=missing-docstring,unused-argument,unused-variable
class RunHookTest(test.TestCase):
  def test_for_append_replicated_fetches(self):
    epl.init(config=epl.Config({"communication.gradients_reduce_method": "sum"}))
    with epl.Cluster(worker_hosts="127.0.0.1:8001", worker_index=0):
      with epl.replicate(device_count=1):
        num_x = np.random.randint(0, 10, (500, 20)).astype(dtype=np.float32)
        num_y = np.random.randint(0, 10, 500).astype(dtype=np.int64)
        dataset = tf.data.Dataset.from_tensor_slices((num_x, num_y)) \
                                .batch(10).repeat(1)
        iterator = dataset.make_initializable_iterator()
        tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS,
                             iterator.initializer)
        x, labels = iterator.get_next()

        logits = tf.layers.dense(x, 2)
        logits = tf.layers.dense(logits, 10)
        loss = tf.losses.sparse_softmax_cross_entropy(labels=labels,
                                                      logits=logits)
        epl.add_to_collection(loss, epl.GraphKeys.GLOBAL_MEAN_OBJECTS)
        global_step = tf.train.get_or_create_global_step()
        optimizer = tf.train.MomentumOptimizer(learning_rate=0.001,
                                               momentum=0.9)
        train_op = optimizer.minimize(loss, global_step=global_step)
      tf.train.MonitoredTrainingSession()

      # Test for a single operation/tensor.
      fetches = loss
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      self.assertEqual(fetches.name, "EPL_PARALLEL_STRATEGY/truediv:0")
      self.assertListEqual(replicas, [
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0"
      ])

      fetches = train_op
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      # Test for nvidia-tf(1.15.4) and deeprec(1.15.5).
      if Version(__version__) >= Version("1.15.4") and Version(__version__) < Version("2.0"):
        suffix = "/group_deps"
      else:
        suffix = ""
      self.assertEqual(fetches.name, "Momentum" + suffix)
      self.assertEqual(replicas, [
          "EPL_REPLICA_1/Momentum" + suffix, "EPL_REPLICA_2/Momentum" +
          suffix, "EPL_REPLICA_3/Momentum" + suffix
      ])

      # Test for list fetches.
      fetches = [loss, train_op]
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      fetches = [fetch.name for fetch in fetches]
      replicas = [rep.name for rep in replicas]
      self.assertListEqual(
          fetches, ["EPL_PARALLEL_STRATEGY/truediv:0", "Momentum" + suffix])
      self.assertListEqual(replicas, [
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0", "EPL_REPLICA_1/Momentum" +
          suffix, "EPL_REPLICA_2/Momentum" + suffix,
          "EPL_REPLICA_3/Momentum" + suffix
      ])

      # Test for type of dict.
      fetches = {"loss": loss, "train_op": train_op}
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      self.assertEqual(fetches["loss"].name,
                       "EPL_PARALLEL_STRATEGY/truediv:0")
      self.assertEqual(fetches["train_op"].name, "Momentum" + suffix)
      if six.PY2:
        self.assertListEqual(replicas, [
            "EPL_REPLICA_1/Momentum" + suffix, "EPL_REPLICA_2/Momentum" +
            suffix, "EPL_REPLICA_3/Momentum" + suffix,
            "EPL_PARALLEL_STRATEGY/truediv_1:0",
            "EPL_PARALLEL_STRATEGY/truediv_2:0",
            "EPL_PARALLEL_STRATEGY/truediv_3:0"
        ])
      else:
        self.assertListEqual(replicas, [
            "EPL_PARALLEL_STRATEGY/truediv_1:0",
            "EPL_PARALLEL_STRATEGY/truediv_2:0",
            "EPL_PARALLEL_STRATEGY/truediv_3:0", "EPL_REPLICA_1/Momentum" +
            suffix, "EPL_REPLICA_2/Momentum" + suffix,
            "EPL_REPLICA_3/Momentum" + suffix
        ])

      # Test for type of OrderedDict
      fetches = collections.OrderedDict()
      fetches["loss"] = loss
      fetches["train_op"] = train_op
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      self.assertEqual(fetches["loss"].name,
                       "EPL_PARALLEL_STRATEGY/truediv:0")
      self.assertEqual(fetches["train_op"].name, "Momentum" + suffix)
      self.assertListEqual(replicas, [
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0", "EPL_REPLICA_1/Momentum" +
          suffix, "EPL_REPLICA_2/Momentum" + suffix,
          "EPL_REPLICA_3/Momentum" + suffix
      ])

      # Test for type of tuple.
      fetches = (loss, train_op)
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      self.assertEqual(fetches[0].name, "EPL_PARALLEL_STRATEGY/truediv:0")
      self.assertEqual(fetches[1].name, "Momentum" + suffix)
      self.assertListEqual(replicas, [
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0", "EPL_REPLICA_1/Momentum" +
          suffix, "EPL_REPLICA_2/Momentum" + suffix,
          "EPL_REPLICA_3/Momentum" + suffix
      ])

      # Test for type of namedtuple.
      fetch_type = collections.namedtuple("fetch_type", ["loss", "train_op"])
      fetches = fetch_type(loss=loss, train_op=train_op)
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      replicas = [rep.name for rep in replicas]
      self.assertEqual(fetches.loss.name, "EPL_PARALLEL_STRATEGY/truediv:0")
      self.assertEqual(fetches.train_op.name, "Momentum" + suffix)
      self.assertListEqual(replicas, [
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0", "EPL_REPLICA_1/Momentum" +
          suffix, "EPL_REPLICA_2/Momentum" + suffix,
          "EPL_REPLICA_3/Momentum" + suffix
      ])

      # Test for nested list fetches.
      def _flatten(li):
        return sum(
            ([x] if not isinstance(x, list) else _flatten(x) for x in li), [])

      fetches = [labels, [train_op, logits, [loss, global_step]]]
      replicas = []
      fetches = _append_replicated_fetches(fetches, replicas)
      fetches = _flatten(fetches)
      fetches = [fetch.name for fetch in fetches]
      replicas = [rep.name for rep in replicas]
      self.assertListEqual(fetches, [
          "IteratorGetNext:1", "Momentum" + suffix, "dense_1/BiasAdd:0",
          "EPL_PARALLEL_STRATEGY/truediv:0", "global_step:0"
      ])
      self.assertListEqual(replicas, [
          "EPL_REPLICA_1/IteratorGetNext:1",
          "EPL_REPLICA_2/IteratorGetNext:1",
          "EPL_REPLICA_3/IteratorGetNext:1", "EPL_REPLICA_1/Momentum" +
          suffix, "EPL_REPLICA_2/Momentum" + suffix,
          "EPL_REPLICA_3/Momentum" + suffix,
          "EPL_REPLICA_1/dense_1/BiasAdd:0",
          "EPL_REPLICA_2/dense_1/BiasAdd:0",
          "EPL_REPLICA_3/dense_1/BiasAdd:0",
          "EPL_PARALLEL_STRATEGY/truediv_1:0",
          "EPL_PARALLEL_STRATEGY/truediv_2:0",
          "EPL_PARALLEL_STRATEGY/truediv_3:0",
          "EPL_REPLICA_1/global_step:0",
          "EPL_REPLICA_2/global_step:0",
          "EPL_REPLICA_3/global_step:0"
      ])

    # Test for nested list with dict.
    fetches = [labels, {"loss": loss}]
    replicas = []
    fetches = _append_replicated_fetches(fetches, replicas)
    replicas = [rep.name for rep in replicas]
    self.assertEqual(fetches[0].name, "IteratorGetNext:1")
    self.assertEqual(fetches[1]["loss"].name,
                     "EPL_PARALLEL_STRATEGY/truediv:0")
    self.assertListEqual(replicas, [
        "EPL_REPLICA_1/IteratorGetNext:1",
        "EPL_REPLICA_2/IteratorGetNext:1",
        "EPL_REPLICA_3/IteratorGetNext:1",
        "EPL_PARALLEL_STRATEGY/truediv_1:0",
        "EPL_PARALLEL_STRATEGY/truediv_2:0",
        "EPL_PARALLEL_STRATEGY/truediv_3:0"
    ])

    # Test for nested list with tuple.
    fetches = [labels, (loss, global_step)]
    replicas = []
    fetches = _append_replicated_fetches(fetches, replicas)
    replicas = [rep.name for rep in replicas]
    self.assertEqual(fetches[0].name, "IteratorGetNext:1")
    self.assertEqual(fetches[1][0].name, "EPL_PARALLEL_STRATEGY/truediv:0")
    self.assertEqual(fetches[1][1].name, "global_step:0")
    self.assertListEqual(replicas, [
        "EPL_REPLICA_1/IteratorGetNext:1",
        "EPL_REPLICA_2/IteratorGetNext:1",
        "EPL_REPLICA_3/IteratorGetNext:1",
        "EPL_PARALLEL_STRATEGY/truediv_1:0",
        "EPL_PARALLEL_STRATEGY/truediv_2:0",
        "EPL_PARALLEL_STRATEGY/truediv_3:0",
        "EPL_REPLICA_1/global_step:0",
        "EPL_REPLICA_2/global_step:0",
        "EPL_REPLICA_3/global_step:0"
    ])

# pylint: enable=missing-docstring,unused-argument,unused-variable

if __name__ == "__main__":
  test.main()

max_stars_repo_name: SeaOfOcean/EasyParallelLibrary


text:
import time
import numpy as np
import cv2

from lib.pipelines.crop_image_pipeline import crop_image
from lib.pipelines.erode_dialate_pipeline import dilate_image, erode_image
from lib.pipelines.find_circles_pipeline import find_filtered_hough_circles, find_valid_circles
from lib.pipelines.find_contours_pipeline import find_and_filter_contours
from lib.pipelines.hsv_threshold_pipeline import run_hsv_threshold
from rapid_react.cargo.cargo_pipeline_params import CargoPipelineParams

class CargoPipeline:
    def __init__(self, params: CargoPipelineParams):
        self.params = params

        self.contours_failed_contour_filtering = []
        self.contours_failed_circle_matching = []
        self.unmatched_circles = []

        self.surviving_contours = []
        self.surviving_circles = []

        self.best_contour = None
        self.threshold_image = None

        self.prev_time = time.time()
        self.processing_time = None
        self.process_start = self.prev_time
        self.stopwatch = {}

        self.process_ctr = 0

    def debug_results(self):
        def debug_contour(contour):
            rect = cv2.boundingRect(contour)
            aspect_ratio = rect[2] / rect[3]
            area = cv2.contourArea(contour)
            if area <= 1:
                return ""
            return f"    Rect: {rect}\tRatio: {aspect_ratio}\tArea: {area}\n"

        output = ""

        output += "Contours: Failed Filtering\n"
        for contour in self.contours_failed_contour_filtering:
            output += debug_contour(contour)

        output += "Contours: Failed Circle Matching\n"
        for contour in self.contours_failed_circle_matching:
            output += debug_contour(contour)

        output += "Contours: Passed!\n"
        for contour in self.surviving_contours:
            output += debug_contour(contour)

        print(output)

    def stopwatch_split(self, tag):
        t = time.time()
        diff = t - self.prev_time
        self.stopwatch[tag] = diff
        self.prev_time = t

    def reset(self):

        self.contours_failed_contour_filtering = []
        self.contours_failed_circle_matching = []
        self.unmatched_circles = []

        self.surviving_contours = []
        self.surviving_circles = []

        self.best_contour = None
        self.threshold_image = None

        self.prev_time = time.time()
        self.process_start = self.prev_time
        self.processing_time = None
        self.stopwatch = {}

    def annotate_image(self, image):

        BAD_CONTOUR_COLOR = (0, 0, 0)
        UNMATCHED_CONTOUR_COLOR = (252, 0, 249)
        UNMATCHED_CIRCLES_COLOR = (125, 1, 123)

        MATCHED_CONTOURS_COLOR = (72, 250, 43)
        MATCHED_CIRCLES_COLOR = (71, 182, 54)
        BEST_CONTOUR_COLOR = (0, 255, 255)

        def annotate_circles(circles, color):
            for i in circles:
                cv2.circle(image, (i[0], i[1]), i[2], color, 2)

        def annotate_contours(contours, color):
            cv2.drawContours(image, contours, -1, color, 2)

        annotate_circles(self.unmatched_circles, UNMATCHED_CIRCLES_COLOR)
        annotate_contours(self.contours_failed_contour_filtering, BAD_CONTOUR_COLOR)
        annotate_contours(self.contours_failed_circle_matching, UNMATCHED_CONTOUR_COLOR)

        annotate_circles(self.surviving_circles, MATCHED_CIRCLES_COLOR)
        annotate_contours(self.surviving_contours, MATCHED_CONTOURS_COLOR)

        if self.best_contour is not None:
            x, y, w, h = cv2.boundingRect(self.best_contour)
            cv2.rectangle(image, (x, y), (x + w, y + h), BEST_CONTOUR_COLOR, 2)

    def run(self, image):
        self.process_ctr += 1

        self.reset()

        img_threshold = run_hsv_threshold(image, self.params.hsv)
        self.stopwatch_split("HSV Threshold")

        img_threshold = crop_image(img_threshold, self.params.image_crop)
        self.stopwatch_split("Crop")

        good_contours, self.contours_failed_contour_filtering = find_and_filter_contours(img_threshold, self.params.contour_filtering)
        self.stopwatch_split("Contours")

        filtered_hough_circles, filtered_threshold_image = find_filtered_hough_circles(img_threshold, good_contours, self.params.circle_detection)
        self.stopwatch_split("Find Circles")

        self.surviving_contours, self.surviving_circles, self.contours_failed_circle_matching, self.unmatched_circles = find_valid_circles(good_contours, filtered_hough_circles, self.params.circle_detection)
        self.stopwatch_split("Match Circles")

        # self.threshold_image = cv2.bitwise_and(image, image, mask=img_threshold)
        self.threshold_image = cv2.bitwise_and(image, image, mask=filtered_threshold_image)

        if len(self.surviving_contours) > 0:
            self.best_contour = max(self.surviving_contours, key=cv2.contourArea)

        self.annotate_image(image)
        self.stopwatch_split("Annotate")

        self.processing_time = time.time() - self.process_start

        return np.array([[]]) if self.best_contour is None else self.best_contour

    def print_stopwatch(self, throttle=0):
        if throttle != 0 and self.process_ctr % throttle != 0:
            return

        output = f"Timing: "
        if self.processing_time != 0:
            output += f"{1 / self.processing_time:0.00f} FPS "
        output += f"{self.processing_time}\n"
        for key in self.stopwatch:
            output += f"    {key:20}: {self.stopwatch[key]}\n"

        print(output)

max_stars_repo_name: GirlsOfSteelRobotics/GirlsOfSteelFRC

max_issues_repo_name: GirlsOfSteelRobotics/GirlsOfSteelFRC

max_forks_repo_name: GirlsOfSteelRobotics/GirlsOfSteelFRC


text:
# Script provides some synthetic waveforms for forward modeling
#

from scipy.signal import butter, lfilter

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

if __name__ == "__main__":
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl

    # Sample rate and desired cutoff frequencies (in Hz).
    fs = 20.0
    lowcut = 1.5
    highcut = 3.0

    # Filter a noisy signal.
    T = 20.0
    nsamples = T * fs
    t = np.linspace(0, T, nsamples, endpoint=False)
    a = 0.02
    f0 = 2.31

    x = 1.0 * np.random.rand(len(t))

    x[0:100] = 0
    x[-200:] = 0

    mpl.rc('figure', figsize=(15, 5))

    plt.figure(2)
    plt.clf()

    # template 1

    templates_count = 4
    templates = []
    a = [1.0, 0.8, 0.6, 0.9]
    for ii in range(templates_count):
        cur_templ = butter_bandpass_filter(x.copy(), lowcut, highcut, fs, order=3)
        cur_templ += 0.2 * (np.random.rand(len(x)) - 0.5)
        cur_templ = a[ii] * butter_bandpass_filter(cur_templ, lowcut, highcut, fs, order=3)
        templates.append(cur_templ)

    traces = []
    for cur_templ in templates:
        cur_trace = cur_templ.copy()
        cur_trace[0:100] = 0; cur_trace[-150:] = 0
        cur_trace += 0.07 * (np.random.rand(len(x)) - 0.5)
        cur_trace = butter_bandpass_filter(cur_trace, 0.2, 4, fs, order=3)
        traces.append(cur_trace)

    td = range(0, 5000)

    cc_trace = np.zeros(len(td))
    for ii in range(50):
        rand_index = np.random.randint(0, len(td))
        rand_value = 0.36 * np.random.rand() + 0.3
        cc_trace[rand_index] = rand_value

    # mpl.rc('figure', figsize=(15, 3))

    # plot traces
    for ii in range(templates_count):
        plt.subplot(templates_count, 1, ii + 1)
        plt.plot(t, traces[ii], 'r')
        plt.plot(t, templates[ii], 'b')
    plt.show()

    # plot templates
    for ii in range(templates_count):
        plt.subplot(templates_count, 1, ii + 1)
        axes = plt.gca()
        axes.set_ylim([-0.3, 0.3])
        plt.plot(t, templates[ii])
    plt.show()

    # plot cc trace
    plt.plot(td, cc_trace)
    # plt.show()

    # write templates
    for tt in range(len(templates)):
        with open('template' + str(tt) + '.bln', 'w') as file_to_write:
            file_to_write.write(str(len(t)) + '\n')
            for ii in range(len(t)):
                file_to_write.write(str(t[ii]) + ',' + str(templates[tt][ii]) + '\n')

    # write traces
    for tt in range(len(traces)):
        with open('trace' + str(tt) + '.bln', 'w') as file_to_write:
            file_to_write.write(str(len(t)) + '\n')
            for ii in range(len(t)):
                file_to_write.write(str(t[ii]) + ',' + str(traces[tt][ii]) + '\n')

    # write cc trace
    with open('cc.bln', 'w') as file_to_write:
        file_to_write.write(str(len(td)) + '\n')
        for ii in range(len(t)):
            file_to_write.write(str(td[ii]) + ',' + str(cc_trace[ii]) + '\n')

max_stars_repo_name: valeryTech/lpe-correlate

max_issues_repo_name: valeryTech/lpe-correlate

max_forks_repo_name: valeryTech/lpe-correlate

reason: import numpy,from scipy


text:
# Import arcpy module
import arcpy
import csv
from numpy import array, isnan
import os

class ConvertFlowlinesToCSVForCF(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Convert Flowlines to CSV for CF"
        self.description = ("Convert flowlines to csv valid for RAPID CF script")
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        in_drainage_line = arcpy.Parameter(
            displayName='Input Drainage Line Features',
            name='in_drainage_line_features',
            datatype='GPFeatureLayer',
            parameterType='Required',
            direction='Input')
        in_drainage_line.filter.list = ['Polyline']

        param1 = arcpy.Parameter(name="stream_ID",
                                 displayName="Stream ID",
                                 direction="Input",
                                 parameterType="Required",
                                 datatype="Field"
                                 )
        param1.parameterDependencies = ["in_drainage_line_features"]
        param1.filter.list = ['Short', 'Long']

        param2 = arcpy.Parameter(name='out_comid_lat_lon_z',
                                 displayName='Output comid_lat_lon_z file',
                                 direction='Output',
                                 parameterType='Required',
                                 datatype='DEFile')

        params = [in_drainage_line, param1, param2]

        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        if parameters[2].altered:
            (dirnm, basenm) = os.path.split(parameters[2].valueAsText)
            if not basenm.endswith(".csv"):
                parameters[2].value = os.path.join(dirnm, "{}.csv".format(basenm))

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        arcpy.env.overwriteOutput = True

        # Script arguments
        Input_Features = parameters[0].valueAsText
        streamID = parameters[1].valueAsText
        Output_Table = parameters[2].valueAsText
        Intermediate_Feature_Points = os.path.join("in_memory", "flowline_centroid_points")
        Intermediate_Feature_Points_Projected = os.path.join(arcpy.env.scratchGDB, "flowline_centroid_points_project")

        # Process: Feature To Point
        arcpy.AddMessage("Converting flowlines to points ...")
        arcpy.FeatureToPoint_management(Input_Features, Intermediate_Feature_Points, "CENTROID")

        # Process: Make sure projection is GCS_WGS_1984
        dsc = arcpy.Describe(Intermediate_Feature_Points)
        if dsc.spatialReference.Name == "Unknown":
            messages.addErrorMessage("Unknown Spatial Reference. Please fix to continue.")
        elif dsc.spatialReference.Name != "GCS_WGS_1984":
            arcpy.AddMessage("Projecting to GCS_WGS_1984 from %s ..." % dsc.spatialReference.Name)
            arcpy.Project_management(in_dataset=Intermediate_Feature_Points,
                                     out_dataset=Intermediate_Feature_Points_Projected,
                                     out_coor_system="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]",
                                     )
            arcpy.Delete_management(Intermediate_Feature_Points)
        else:
            Intermediate_Feature_Points_Projected = Intermediate_Feature_Points

        # Process: Add XY Coordinates
        arcpy.AddMessage("Adding XY coordinates to points ...")
        arcpy.AddXY_management(Intermediate_Feature_Points_Projected)

        # write only desired fields to csv
        arcpy.AddMessage("Writing output to csv ...")
        original_field_names = [f.name for f in arcpy.ListFields(Intermediate_Feature_Points_Projected)]
        # arcpy.AddMessage("Field names: %s" % original_field_names)
        # COMID,Lat,Lon,Elev_m
        actual_field_names = [streamID, "", ""]
        needed_field_names = ['point_y', 'point_x', 'point_z']
        elevation_exists = False
        for original_field_name in original_field_names:
            original_field_name_lower = original_field_name.lower()
            if original_field_name_lower == needed_field_names[0]:
                actual_field_names[1] = original_field_name
            elif original_field_name_lower == needed_field_names[1]:
                actual_field_names[2] = original_field_name
            elif original_field_name_lower == needed_field_names[2]:
                actual_field_names.append(original_field_name)
                elevation_exists = True

        # check to make sure all fields exist
        for index, field_name in enumerate(actual_field_names):
            if field_name == "":
                messages.addErrorMessage("Field name %s not found." % needed_field_names[index - 1])
                raise arcpy.ExecuteError

        # print valid field names to table
        with open(Output_Table, 'w') as outfile:
            writer = csv.writer(outfile)
            writer.writerow(['COMID', 'Lat', 'Lon', 'Elev_m'])
            with arcpy.da.SearchCursor(Intermediate_Feature_Points_Projected, actual_field_names) as cursor:
                for row in cursor:
                    # if no elevation found, append zero
                    if not elevation_exists:
                        row += (0,)
                    # make sure all values are valid
                    np_row = array(row)
                    np_row[isnan(np_row)] = 0
                    writer.writerow([int(np_row[0])] + np_row[1:].tolist())

        # delete intermediate layer
        arcpy.Delete_management(Intermediate_Feature_Points_Projected)
        # add warning messages
        if not elevation_exists:
            arcpy.AddMessage("WARNING: Elevation not found. Zero elevations added.")
        arcpy.AddMessage("WARNING: NaN value(s) replaced with zero. Please check output for accuracy.")

        return

max_stars_repo_name: rileyhales/dataTools

max_issues_repo_name: rileyhales/dataTools

max_forks_repo_name: rileyhales/dataTools

reason: from numpy


text:
import sys
import time

import numpy as np
import matplotlib.pyplot as plt

from acs.objective import fitness
from acs.instance import Instance, print_instance

from utils.timer import Timer
from utils.misc import sigmoid, evaluate_population_random, evaluate_population_fixed

from algorithms.pso.config import Config, Evaluator

def particle_swarm_optmization(instance, config, fitness_function, out_info=None, verbose=False):
    num_particles = config.num_particles

    if config.evaluator == Evaluator.FIXED_EVALUATOR:
        evaluate_function = evaluate_population_fixed
    else:
        evaluate_function = evaluate_population_random

    def counter_fitness(individual, instance, student, timer, print_results=False, data=None):
        nonlocal cost_counter
        cost_counter += 1
        return fitness_function(individual, instance, student, timer, print_results, data=data)

    if out_info is not None:
        out_info['best_fitness'] = []
        out_info['partial_fitness'] = []
        out_info['perf_counter'] = []
        out_info['process_time'] = []
        out_info['cost_value'] = []

    results = []

    for student in range(instance.num_learners):
        if verbose:
            print('[PSO] Students progress: %d / %d (%d%%)' % (student + 1, instance.num_learners, (student + 1) * 100 / instance.num_learners))
        cost_counter = 0
        iteration_counter = 0
        stagnation_counter = 0

        if out_info is not None:
            out_info['best_fitness'].append([])
            out_info['partial_fitness'].append([])
            out_info['perf_counter'].append([])
            out_info['process_time'].append([])
            out_info['cost_value'].append([])

        timer = Timer()

        timer.add_time()
        particle_velocity = np.random.rand(num_particles, instance.num_materials) * (2 * config.max_velocity) - config.max_velocity
        particle_position = evaluate_function(particle_velocity)

        local_best_position = np.copy(particle_position)
        local_best_fitness = np.apply_along_axis(counter_fitness, 1, local_best_position, instance, student, timer)

        global_best_index = np.argmin(local_best_fitness, axis=0)
        global_best_position = np.copy(local_best_position[global_best_index])
        global_best_fitness = local_best_fitness[global_best_index]

        timer.add_time("initialization")

        start_perf_counter = time.perf_counter()
        start_process_time = time.process_time()
        while ((not config.cost_budget or cost_counter < config.cost_budget) and
               (not config.num_iterations or iteration_counter < config.num_iterations) and
               (not config.max_stagnation or stagnation_counter < config.max_stagnation)):
            old_global_best_fitness = global_best_fitness

            if out_info is not None:
                out_info["best_fitness"][-1].append(global_best_fitness)
                fitness_function(global_best_position, instance, student, timer, data=out_info["partial_fitness"][-1])
                out_info["perf_counter"][-1].append(time.perf_counter() - start_perf_counter)
                out_info["process_time"][-1].append(time.process_time() - start_process_time)
                out_info["cost_value"][-1].append(cost_counter)

            timer.add_time()
            local_influence = np.tile(config.local_influence_parameter * np.random.random(num_particles), (instance.num_materials, 1)).T
            global_influence = np.tile(config.global_influence_parameter * np.random.random(num_particles), (instance.num_materials, 1)).T

            local_distance = local_best_position.astype(int) - particle_position.astype(int)
            global_distance = global_best_position.astype(int) - particle_position.astype(int)

            particle_velocity = (config.inertia_parameter * particle_velocity
                                 + local_influence * local_distance
                                 + global_influence * global_distance)
            particle_velocity = np.clip(particle_velocity, -config.max_velocity, config.max_velocity)
            timer.add_time("update_velocity")

            # Calcula as novas posições
            particle_position = evaluate_function(particle_velocity)
            timer.add_time("update_position")

            # Calcula os novos resultados
            particle_new_fitness = np.apply_along_axis(counter_fitness, 1, particle_position, instance, student, timer)
            timer.add_time("update_fitness")

            # Calcula a mascara de melhores valores para cada particula
            change_mask = (particle_new_fitness < local_best_fitness)

            # Altera o melhor resultado de cada particula
            local_best_position[change_mask] = np.copy(particle_position[change_mask])
            local_best_fitness[change_mask] = particle_new_fitness[change_mask]

            # Encontra o melhor resultado entre todas as particulas
            global_best_index = np.argmin(local_best_fitness)
            global_best_position = np.copy(local_best_position[global_best_index])
            global_best_fitness = local_best_fitness[global_best_index]

            iteration_counter += 1
            if global_best_fitness < old_global_best_fitness:
                stagnation_counter = 0
            else:
                stagnation_counter += 1

            timer.add_time("update_best")

        if out_info is not None:
            out_info["best_fitness"][-1].append(global_best_fitness)
            fitness_function(global_best_position, instance, student, timer, data=out_info["partial_fitness"][-1])
            out_info["perf_counter"][-1].append(time.perf_counter() - start_perf_counter)
            out_info["process_time"][-1].append(time.process_time() - start_process_time)
            out_info["cost_value"][-1].append(cost_counter)

        results.append((global_best_position, global_best_fitness))

    return results

max_stars_repo_name: martinsadw/evolutionary-computation

max_issues_repo_name: martinsadw/evolutionary-computation

max_forks_repo_name: martinsadw/evolutionary-computation


text:
'''
Color Detection in Python with OpenCV
    Create by Henry Dang ==> See the tutorial here:
    https://henrydangprg.com/2016/06/26/color-detection-in-python-with-opencv/
Adapted by Marcelo Rovai - MJRoBot.org @8Feb18
'''

import cv2
import numpy as np

# Read the picure - The 1 means we want the image in BGR
img = cv2.imread('yellow_object.JPG', 1)

# resize imag to 20% in each axis
img = cv2.resize(img, (0,0), fx=0.2, fy=0.2)
# convert BGR image to a HSV image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# NumPy to create arrays to hold lower and upper range
# The “dtype = np.uint8” means that data type is an 8 bit integer

lower_range = np.array([24, 100, 100], dtype=np.uint8)
upper_range = np.array([44, 255, 255], dtype=np.uint8)

# create a mask for image
mask = cv2.inRange(hsv, lower_range, upper_range)

# display both the mask and the image side-by-side
cv2.imshow('mask',mask)
cv2.imshow('image', img)

# wait to user to press [ ESC ]
while(1):
  k = cv2.waitKey(0)
  if(k == 27):
    break

cv2.destroyAllWindows()


text:
import sys
from thales_remote.connection import ThalesRemoteConnection
from thales_remote.script_wrapper import PotentiostatMode,ThalesRemoteScriptWrapper

from thales_file_import.ism_import import IsmImport
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import EngFormatter, StrMethodFormatter
from matplotlib import ticker,colors,cm

from jupyter_utils import executionInNotebook, notebookCodeToPython

if __name__ == "__main__":
    zenniumConnection = ThalesRemoteConnection()
    connectionSuccessful = zenniumConnection.connectToTerm("localhost", "ScriptRemote")
    if connectionSuccessful:
        print("connection successfull")
    else:
        print("connection not possible")
        sys.exit()

    zahnerZennium = ThalesRemoteScriptWrapper(zenniumConnection)

    zahnerZennium.forceThalesIntoRemoteScript()

    zahnerZennium.setEISNaming("individual")
    zahnerZennium.setEISOutputPath(r"C:\THALES\temp")

    zahnerZennium.setPotentiostatMode(PotentiostatMode.POTMODE_POTENTIOSTATIC)
    zahnerZennium.setAmplitude(10e-3)
    zahnerZennium.setLowerFrequencyLimit(100)
    zahnerZennium.setStartFrequency(100)
    zahnerZennium.setUpperFrequencyLimit(500000)
    zahnerZennium.setLowerNumberOfPeriods(10)
    zahnerZennium.setLowerStepsPerDecade(10)
    zahnerZennium.setUpperNumberOfPeriods(20)
    zahnerZennium.setUpperStepsPerDecade(10)
    zahnerZennium.setScanDirection("startToMin")
    zahnerZennium.setScanStrategy("single")

    potentialsToMeasure = np.linspace(0,0.3,13)
    print(potentialsToMeasure)

    for potential in potentialsToMeasure:
        filename = "{:d}_mvdc".format(int(round(potential*1000)))
        print("step: " + filename)
        zahnerZennium.setEISOutputFileName(filename)
        zahnerZennium.setPotential(potential)

        zahnerZennium.enablePotentiostat()
        zahnerZennium.measureEIS()
        zahnerZennium.disablePotentiostat()

    zenniumConnection.disconnectFromTerm()

    absoluteImpedances = []
    phases = []

    for potential in potentialsToMeasure:
        ismFile = IsmImport(r"C:\THALES\temp\{:d}_mvdc.ism".format(int(round(potential*1000))))
        absoluteImpedances.append(ismFile.getImpedanceArray())
        phases.append(ismFile.getPhaseArray())

    absoluteImpedances = np.array(absoluteImpedances)
    phases = np.array(phases)
    phases = np.abs(phases * (360 / (2 * np.pi)))

    impedanceFrequencies = ismFile.getFrequencyArray()

    X,Y = np.meshgrid(impedanceFrequencies,potentialsToMeasure)

    impedanceFigure, impedancePlot = plt.subplots(1,1)
    impedanceFigure.suptitle("Impedance vs. DC Voltage vs. Frequency")

    ticks = np.power(10, np.arange(np.floor(np.log10(absoluteImpedances.min())-1), np.ceil(np.log10(absoluteImpedances.max())+1)))
    levels = np.logspace(np.floor(np.log10(absoluteImpedances.min())-1), np.ceil(np.log10(absoluteImpedances.max())), num=200)
    impedanceContour = impedancePlot.contourf(X, Y, absoluteImpedances, levels = levels, norm = colors.LogNorm(absoluteImpedances.min(), absoluteImpedances.max(), True), cmap="jet")

    impedancePlot.set_xlabel(r"Frequency")
    impedancePlot.set_xscale("log")
    impedancePlot.xaxis.set_major_formatter(EngFormatter(unit="Hz"))

    impedancePlot.set_ylabel(r"DC Voltage")
    impedancePlot.yaxis.set_major_formatter(EngFormatter(unit="V"))

    impedanceBar = impedanceFigure.colorbar(impedanceContour, ticks=ticks, format=EngFormatter(unit="$\Omega$"))
    impedanceBar.set_label('| Impedance |')
    impedanceFigure.set_size_inches(14, 12)
    plt.tight_layout()
    plt.show()
    impedanceFigure.savefig("impedance_contour.svg")

    phaseFigure, phasePlot = plt.subplots(1, 1)
    phaseFigure.suptitle("Phase vs. DC Voltage vs. Frequency")

    levels = np.linspace(phases.min(), phases.max(), 91)
    phaseContour = phasePlot.contourf(X, Y, phases, levels = levels, cmap="jet")

    phasePlot.set_xlabel(r"Frequency")
    phasePlot.set_xscale("log")
    phasePlot.xaxis.set_major_formatter(EngFormatter(unit="Hz"))

    phasePlot.set_ylabel(r"DC Voltage")
    phasePlot.yaxis.set_major_formatter(EngFormatter(unit="V"))

    phaseBar = phaseFigure.colorbar(phaseContour, format=StrMethodFormatter("{x:.0f}$Â°$"))
    phaseBar.set_label("| Phase |")

    phaseFigure.set_size_inches(14, 12)
    plt.tight_layout()
    plt.show()
    phaseFigure.savefig("phase_contour.svg")

    if executionInNotebook() == True:
        notebookCodeToPython("EISvsParameter.ipynb")

max_stars_repo_name: Zahner-elektrik/Thales-Remote-Python

max_issues_repo_name: Zahner-elektrik/Thales-Remote-Python

max_forks_repo_name: Zahner-elektrik/Thales-Remote-Python


text:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import hits

n = 100
m = 200
plot = True
# generate a random graph with n nodes and m edges.
G1 = nx.gnm_random_graph(n, m)
G2 = nx.gnm_random_graph(n, m)

A = np.asarray(nx.to_numpy_matrix(G1))
B = np.asarray(nx.to_numpy_matrix(G2))

hits.comparison(A, B, steps=50, normalize=True, lapse=True)

# if plot:
#     plt.subplot(121)
#     nx.draw(G2, with_labels=True, font_weight='bold')
#     plt.show()

max_issues_repo_name: Stupiderick/graph-similarity

max_forks_repo_name: Stupiderick/graph-similarity

reason: import numpy,import networkx


text:
import os
import datetime
import tensorflow as tf
import numpy as np

def cnn_train(x_train, y_train, FLAGS, w2v_model, cnn_dir, v_train):
    with tf.Graph().as_default():
        session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
        sess = tf.Session(config=session_conf)
        with sess.as_default():
            cnn = MicroTableCNN(micro_table_size=FLAGS.micro_table_size, num_classes=y_train.shape[1],
                                channel_num=w2v_model.vector_size, col_filters=FLAGS.col_filters,
                                row_filters=FLAGS.row_filters, cell_prop_size=FLAGS.cell_prop_size,
                                FC_size=FLAGS.FC_size,
                                num_filters=FLAGS.num_filters, prop2vec_dim=FLAGS.prop2vec_dim,
                                use_property_vector=FLAGS.use_property_vector)

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

            # Keep track of gradient values and sparsity (optional)
            grad_summaries = []
            for g, v in grads_and_vars:
                if g is not None:
                    grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g))
                    grad_summaries.append(grad_hist_summary)
                    grad_summaries.append(sparsity_summary)
            grad_summaries_merged = tf.summary.merge(grad_summaries)

            # Summaries for loss and accuracy
            loss_summary = tf.summary.scalar("loss", cnn.loss)
            acc_summary = tf.summary.scalar("accuracy", cnn.accuracy)
            train_summary_op = tf.summary.merge([loss_summary, acc_summary, grad_summaries_merged])
            train_summary_dir = os.path.join(cnn_dir, "summaries", "train")
            train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph)

            # Checkpoint directory
            checkpoint_dir = os.path.abspath(os.path.join(cnn_dir, "checkpoints"))
            checkpoint_prefix = os.path.join(checkpoint_dir, "model")
            if not os.path.exists(checkpoint_dir):
                os.makedirs(checkpoint_dir)
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)

            # Initialize all variables
            sess.run(tf.global_variables_initializer())

            def train_step(feeds):
                _, step, summaries, loss, accuracy = sess.run(
                    [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy], feeds)
                time_str = datetime.datetime.now().isoformat()
                if step % FLAGS.evaluate_every == 0:
                    print("\t {}: step {}, train loss {:g}, train acc {:g}".format(time_str, step, loss, accuracy))
                train_summary_writer.add_summary(summaries, step)

            def train_eva(feeds):
                loss, accuracy = sess.run([cnn.loss, cnn.accuracy], feeds)
                print("\t finally, train loss {:g}, train acc {:g}".format(loss, accuracy))

            if FLAGS.use_property_vector:
                batches = batch_iter(list(zip(x_train, y_train, v_train)), FLAGS.num_epochs, FLAGS.batch_size)
                current_step = 0
                for batch in batches:
                    x_batch, y_batch, v_batch = zip(*batch)
                    feed_dict = {cnn.input_x: x_batch, cnn.input_y: y_batch, cnn.input_v: v_batch,
                                 cnn.dropout_keep_prob: FLAGS.dropout_keep_prob}
                    train_step(feed_dict)
                    current_step = tf.train.global_step(sess, global_step)
            else:
                batches = batch_iter(list(zip(x_train, y_train)), FLAGS.num_epochs, FLAGS.batch_size)
                current_step = 0
                for batch in batches:
                    x_batch, y_batch = zip(*batch)
                    feed_dict = {cnn.input_x: x_batch, cnn.input_y: y_batch,
                                 cnn.dropout_keep_prob: FLAGS.dropout_keep_prob}
                    train_step(feed_dict)
                    current_step = tf.train.global_step(sess, global_step)

            if FLAGS.use_property_vector:
                feed_dict = {cnn.input_x: x_train, cnn.input_y: y_train, cnn.input_v: v_train,
                             cnn.dropout_keep_prob: FLAGS.dropout_keep_prob}
            else:
                feed_dict = {cnn.input_x: x_train, cnn.input_y: y_train,
                             cnn.dropout_keep_prob: FLAGS.dropout_keep_prob}
            train_eva(feed_dict)

            path = saver.save(sess, checkpoint_prefix, global_step=current_step)
            print("\t Saved model checkpoint to {}\n".format(os.path.basename(path)))

''' Predict with CNN
    input: test_x (each row represents one sample vector)
    output: test_p (an array, each item represents the score of one sample)
'''

def cnn_predict(test_x, cnn_dir):
    checkpoint_dir = os.path.join(cnn_dir, 'checkpoints')
    checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)

    graph = tf.Graph()
    with graph.as_default():
        session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
        sess = tf.Session(config=session_conf)

        with sess.as_default():
            # Load the saved meta graph and restore variables
            saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
            saver.restore(sess, checkpoint_file)
            input_x = graph.get_operation_by_name("input_x").outputs[0]
            dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
            scores = graph.get_operation_by_name("output/scores").outputs[0]

            test_y = sess.run(scores, {input_x: test_x, dropout_keep_prob: 1.0})

    return test_y

''' Generate batches of the samples
    In each epoch, samples are traversed one time batch by batch
'''

def batch_iter(data, num_epochs, batch_size, shuffle=True):
    data = np.array(data)
    data_size = len(data)
    num_batches = int((data_size - 1) / batch_size) + 1
    for epoch in range(num_epochs):
        if shuffle:
            batch_shuffle_indices = np.random.permutation(np.arange(data_size))
            shuffled_data = data[batch_shuffle_indices]
        else:
            shuffled_data = data

        if num_batches > 0:
            for batch_num in range(num_batches):
                start_index = batch_num * batch_size
                end_index = min((batch_num + 1) * batch_size, data_size)
                yield shuffled_data[start_index:end_index]
        else:
            yield shuffled_data

''' A CNN for classification of micro tables:
    + a Conv layer with multiple filters
        1) filters over the target column with size of e.g., 2*1*D and 3*1*D
        2) filter over the combination of target column and a surrounding column, with size of 1*2*D
    + a max-pooling layer over features (aggregation on row dimension)
    + a fully connected layer
    + a softmax layer for classification
'''

class MicroTableCNN(object):

    def __init__(self, micro_table_size, num_classes, channel_num, col_filters, row_filters, cell_prop_size, FC_size,
                 num_filters, prop2vec_dim, use_property_vector):
        M, N = micro_table_size

        # Placeholders for input, output and dropout
        self.input_x = tf.placeholder(tf.float32, [None, M, (N + 1), channel_num], name="input_x")
        self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
        if use_property_vector:
            self.input_v = tf.placeholder(tf.float32, [None, prop2vec_dim], name="input_v")

        # Get (coherence) features over the target column, i.e., col_filters
        pooled_outputs_col = []
        for i, f in enumerate(col_filters):
            x_col_0 = self.input_x[:, :, 0:1, :]
            with tf.name_scope("col-%d" % f):
                f_shape = [f, 1, channel_num, num_filters]
                Conv_W = tf.Variable(tf.truncated_normal(f_shape, stddev=0.1), name="conv_%d_W" % f)
                Conv_b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="conv_%d_b" % f)
                conv = tf.nn.conv2d(x_col_0, Conv_W, strides=[1, 1, 1, 1], padding="VALID", name="conv_%d" % f)
                h = tf.nn.relu(tf.nn.bias_add(conv, Conv_b), name="relu_%d" % f)
                pooled = tf.nn.max_pool(h, ksize=[1, M - f + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID',
                                        name="pool_%s" % f)
                pooled_outputs_col.append(pooled)

        # Get col (context) features, i.e., row_filters
        pooled_outputs_row = []
        for i, f in enumerate(row_filters):
            x_row_0 = self.input_x[:, 0:1, :, :]
            with tf.name_scope('row-%d' % f):
                f_shape = [1, f, channel_num, num_filters]
                Conv_W = tf.Variable(tf.truncated_normal(f_shape, stddev=0.1), name="conv_%d_W" % f)
                Conv_b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="conv_%d_b" % f)
                conv = tf.nn.conv2d(x_row_0, Conv_W, strides=[1, 1, 1, 1], padding="VALID", name="conv_%d" % f)
                h = tf.nn.relu(tf.nn.bias_add(conv, Conv_b), name="relu_%d" % f)
                pooled = tf.nn.max_pool(h, ksize=[1, 1, N + 1 - f + 1, 1], strides=[1, 1, 1, 1], padding='VALID',
                                        name="pool_%d" % f)
                pooled_outputs_row.append(pooled)

        # FC layer for features from cell + prop2vec
        with tf.name_scope('cell_prop'):
            if use_property_vector:
                self.cell_prop_input = tf.concat([self.input_x[:, 0, 0, :], self.input_v], 1)
                num_cell_prop = channel_num + prop2vec_dim
            else:
                self.cell_prop_input = self.input_x[:, 0, 0, :]
                num_cell_prop = channel_num
            Cell_Prop_W = tf.get_variable("cell_prop_W", shape=[num_cell_prop, cell_prop_size],
                                          initializer=tf.contrib.layers.xavier_initializer())
            Cell_Prop_b = tf.Variable(tf.constant(0.1, shape=[cell_prop_size]), name="cell_prop_b")
            self.cell_prop_out = tf.nn.xw_plus_b(self.cell_prop_input, Cell_Prop_W, Cell_Prop_b,
                                                 name='cell_prop_out')
            # self.cell_prop_out = tf.nn.relu_layer(self.cell_prop_input, Cell_Prop_W, Cell_Prop_b,
            #                                       name='cell_prop_out')
            # self.reg1 = tf.nn.l2_loss(Cell_Prop_W)

        num_filters_total = num_filters * (len(pooled_outputs_col) + len(pooled_outputs_row))
        if num_filters_total > 0:
            self.h_pool = tf.concat(pooled_outputs_col + pooled_outputs_row, 3)
            self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total])
            self.features = tf.concat([self.cell_prop_out, self.h_pool_flat], 1)
            num_features = num_filters_total + cell_prop_size
        else:
            self.features = self.cell_prop_out
            num_features = cell_prop_size

        with tf.name_scope("dropout"):
            self.f_drop = tf.nn.dropout(self.features, self.dropout_keep_prob)

        with tf.name_scope("FC"):
            FC_W = tf.get_variable("FC_W", shape=[num_features, FC_size],
                                   initializer=tf.contrib.layers.xavier_initializer())
            FC_b = tf.Variable(tf.constant(0.1, shape=[FC_size]), name="FC_b")
            fc_out = tf.nn.relu_layer(self.features, FC_W, FC_b)
            self.fc_out = tf.nn.dropout(fc_out, self.dropout_keep_prob)

        with tf.name_scope("output"):
            out_W = tf.get_variable("out_W", shape=[FC_size, num_classes],
                                    initializer=tf.contrib.layers.xavier_initializer())
            out_b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name="out_b")
            self.out = tf.nn.xw_plus_b(self.fc_out, out_W, out_b, name="out")
            self.scores = tf.nn.softmax(self.out, name='scores')
            self.predictions = tf.argmax(self.scores, 1, name="predictions")

        # Calculate mean cross-entropy loss
        with tf.name_scope("loss"):
            self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.out, labels=self.input_y))

        # Calculate accuracy
        with tf.name_scope("accuracy"):
            correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")

max_stars_repo_name: vishalbelsare/SemAIDA

max_issues_repo_name: vishalbelsare/SemAIDA

max_forks_repo_name: vishalbelsare/SemAIDA


text:
import taichi as ti
from PIL import Image
import numpy as np

from plyread2 import NPLYReader

ply = NPLYReader("./mesh/main-all.ply")

img = Image.open("./mesh/main-2-big.png").rotate(-90).resize((512,512))
imgnp = np.asarray(img)

gui = ti.GUI("wiew UV", res=(512,512))

while gui.running:

    gui.set_image(imgnp)

    for f in ply.faces:
        gui.line(ply.vertex[f[0], 6:8], ply.vertex[f[1], 6:8], color=0xaaaaaa)
        gui.line(ply.vertex[f[1], 6:8], ply.vertex[f[2], 6:8], color=0xaaaaaa)
        gui.line(ply.vertex[f[2], 6:8], ply.vertex[f[0], 6:8], color=0xaaaaaa)

    gui.show()


text:
import numpy as np

# m is usually set to 2 and r to 0.10-0.25 times standard deviation of lista

def Sample_Entropy(lista, m, r):
    # split list in an array of vectors with length 2 and 1 number overlap
    l_with_mvectors = []
    k = 0
    lista = np.array(lista)
    for i in range(len(lista[:-m+1])):
        l_with_mvectors.append(lista[i:i + m])

    # split list in an array of vectors with length 3 and 2 numbers overlap
    l_with_mplus1vectors = []
    i = 0
    for i in range (len(lista[:-m])):
        l_with_mplus1vectors.append(lista[i:i + m + 1])

    # create an inside function to calculate sum of logs of l_with2vectors and l_with3vectors
    def sumprobs(l, vectoSz, r):

        matches = []

        for en1,vector in enumerate(l):

            countSimilar = 0
            limits = [[vector[i] + r, vector[i] - r] for i in range(vectoSz)] # position m mth value, 0 is max 1 is min
            for en2,tmp in enumerate(l):

                if en1==en2: continue

                countSimilarE = 0
                for t in range(vectoSz):

                    #upperLim = vector[t] + r
                    #lowerLim = vector[t] - r

                    #if tmp[t] >= lowerLim and tmp[t] <= upperLim:
                    if tmp[t] >= limits[t][1] and tmp[t] <= limits[t][0]:
                        countSimilarE += 1

                if countSimilarE == vectoSz:
                    countSimilar += 1

            matches.append(countSimilar)

        prob = sum(matches) / (len(l) - 1)

        AiBi = prob/ (len(lista) - m)

        return AiBi

    Bi = sumprobs(l_with_mvectors, m, r)
    Ai = sumprobs(l_with_mplus1vectors, m + 1, r)

    A = ((len(lista) - m - 1) * (len(lista) - m) * Ai) / 2
    B = ((len(lista) - m - 1) * (len(lista) - m) * Bi) / 2

    sampen = -np.log(A / B)

    return sampen

max_stars_repo_name: spyrosviz/Parkinson-Prediction

max_issues_repo_name: spyrosviz/Elderly-Falls-Prediction

max_forks_repo_name: spyrosviz/Elderly-Falls-Prediction


text:
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A baseline random search in our standardized optimizer interface. Useful for baselines.
"""
import numpy as np

from bayesmark import np_util
from bayesmark.space import JointSpace

def suggest_dict(X, y, meta, n_suggestions=1, random=np_util.random):
    """Stateless function to create suggestions for next query point in random search optimization.

    This implements the API for general structures of different data types.

    Parameters
    ----------
    X : list(dict)
        Places where the objective function has already been evaluated. Not actually used in random search.
    y : :class:`numpy:numpy.ndarray`, shape (n,)
        Corresponding values where objective has been evaluated. Not actually used in random search.
    meta : dict(str, dict)
        Configuration of the optimization variables. See API description.
    n_suggestions : int
        Desired number of parallel suggestions in the output
    random : :class:`numpy:numpy.random.RandomState`
        Optionally pass in random stream for reproducibility.

    Returns
    -------
    next_guess : list(dict)
        List of `n_suggestions` suggestions to evaluate the objective function.
        Each suggestion is a dictionary where each key corresponds to a parameter being optimized.
    """
    # Warp and get bounds
    space_x = JointSpace(meta)
    X_warped = space_x.warp(X)
    bounds = space_x.get_bounds()
    _, n_params = _check_x_y(X_warped, y, allow_impute=True)
    lb, ub = _check_bounds(bounds, n_params)

    # Get the suggestion
    suggest_x = random.uniform(lb, ub, size=(n_suggestions, n_params))

    # Unwarp
    next_guess = space_x.unwarp(suggest_x)
    return next_guess

def _check_x_y(X, y, allow_impute=False):  # pragma: validator
    """Input validation for `suggest` routine."""
    if not (np.ndim(X) == 2):
        raise ValueError("X must be 2-dimensional got %s." % str(np.shape(X)))
    n_obs, n_params = np.shape(X)

    assert n_params >= 1, "We do not support suggest on empty space."

    if not (np.shape(y) == (n_obs,)):
        raise ValueError("y must be %s not %s." % (str((n_obs,)), str(np.shape(y))))

    if not np.all(np.isfinite(X)):
        raise ValueError("X must be finite.")

    n_real_obs = n_obs
    if allow_impute:
        if not np.all(np.isfinite(y) | np.isnan(y)):
            raise ValueError("y can't contain infs even with data imputation.")
        n_real_obs = np.sum(np.isfinite(y))
    else:
        if not np.all(np.isfinite(y)):
            raise ValueError("y must be finite when data imputation not used.")

    return n_real_obs, n_params

def _check_bounds(bounds, n_params):  # pragma: validator
    """Input validation for `suggest` routine."""
    if not (np.shape(bounds) == (n_params, 2)):
        raise ValueError("bounds must have shape %s not %s." % (str((n_params, 2)), str(np.shape(bounds))))

    lb, ub = np.asarray(bounds).T
    if not (np.all(np.isfinite(lb)) and np.all(np.isfinite(ub))):
        raise ValueError("bounds must be finite.")
    if not (np.all(lb <= ub)):
        raise ValueError("lower bound must be less than upper bound.")
    return lb, ub

max_stars_repo_name: goncaloperes/bayesmark

max_issues_repo_name: goncaloperes/bayesmark

max_forks_repo_name: goncaloperes/bayesmark


text:
def arg(x, y):
    import numpy as np
    return np.arctan2(y, x)

def get_cd(wcs):

    pc11, pc12, pc21, pc22 = wcs.wcs.get_pc().ravel()
    cdelt1, cdelt2 = wcs.wcs.get_cdelt()

    cd11 = cdelt1 * pc11
    cd12 = cdelt1 * pc12
    cd21 = cdelt2 * pc21
    cd22 = cdelt2 * pc22

    return cd11, cd12, cd21, cd22

def get_cdelt_crota(wcs):

    import numpy as np

    # This implements the algorithm from:
    #
    # Representations of celestial coordinates in FITS
    #          Calabretta & Greisen (2002)
    #
    # Section: 6.2. Supporting old interpreters

    cd11, cd12, cd21, cd22 = get_cd(wcs)

    if cd21 > 0:
        rho_a = arg(cd11, cd21)
    elif cd21 == 0:
        rho_a = 0
    else:
        rho_a = arg(-cd11, -cd21)

    if cd12 > 0:
        rho_b = arg(-cd22, cd12)
    elif cd12 == 0:
        rho_b = 0
    else:
        rho_b = arg(cd22, -cd12)

    rho_diff = abs(rho_a - rho_b)

    if rho_diff > np.pi:
        rho_diff -= 2 * np.pi

    if rho_diff > 1e-10:
        raise ValueError("WCS cannot be represented by CDELT/CROTA")

    rho = 0.5 * (rho_a + rho_b)

    if np.abs(np.cos(rho)) > 0.5:
        cdelt1 = cd11 / np.cos(rho)
        cdelt2 = cd22 / np.cos(rho)
    else:
        cdelt1 = cd21 / np.sin(rho)
        cdelt2 = -cd12 / np.sin(rho)

    crota2 = np.degrees(rho) % 360

    return cdelt1, cdelt2, crota2

max_stars_repo_name: pkgw/pyavm

max_issues_repo_name: pkgw/pyavm

max_forks_repo_name: pkgw/pyavm


text:
"""Utility methods for graph plotting."""

import os

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

def show_default_colors():
    def despine(fig):
        # Hide spines
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        # Hide ticks and labels
        ax.yaxis.set_ticks_position('none')
        ax.xaxis.set_ticks_position('none')
        ax.set_xticklabels([])
        ax.set_yticklabels([])

        return ax

    colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

    fig = plt.figure(figsize=(6, 1))
    ax = fig.add_subplot(111)

    for i, color in enumerate(colors):
        xy = (i, 0)
        rect = matplotlib.patches.Rectangle(xy, 1, 1, color=color)
        ax.add_patch(rect)

    despine(ax)
    plt.xlim([0, i + 1])
    plt.ylim([0, 1])
    plt.show()

def save_plot_for_figure(figure, file_name, path=None):
    """Save matplotlib plot to pdf file.

    Parameters
    ----------
    figure: matplotlib.figure.Figure
        Output of Model.evaluate().
    file_name: str
        Name for the output file.
    path: str
        Path to location to save file to.
    Returns
    -------
    Figure as pdf file.
    """
    file_extension = '.pdf'
    file_name += file_extension
    if path is not None:
        file_name = os.path.join(path, file_name)
    figure.savefig(file_name, dpi=300, bbox_inches='tight', transparent=True)

def format_output_intervals(intervals):
    """Convert intervals to time points for plotting.

    Parameters
    ----------
    intervals: torch.Tensor
        Model output intervals in days. Can be obtained from the
        "output_intervals" attribute of a "Model" instance.
    Returns
    -------
    Prediction time points as numpy.ndarray.
    """
    # Convert to years
    time_points = np.array(intervals) / 365
    # Define center of each interval as time point
    time_points[1:] = time_points[1:] - np.diff(time_points)[0] / 2

    return time_points

max_stars_repo_name: luisvalesilva/multisurv

max_issues_repo_name: luisvalesilva/multisurv

max_forks_repo_name: luisvalesilva/multisurv