text:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  8 22:46:34 2019

@author: Samuel
"""
import pandas as pd
import numpy as np
import pickle
import six
import warnings
from itertools import cycle
from collections import OrderedDict
from scipy.sparse import csr_matrix
from sklearn.base import BaseEstimator, TransformerMixin

class DiscreteMinxin(object):
    def get_obj_cols(self, df):
        dtypes_to_encode=['object','category']
        cols = df.select_dtypes(include=dtypes_to_encode).columns.tolist()
        return cols

    def save(self, path):
        with open(path,'wb+') as fp:
            pickle.dump(self.__dict__, fp)

    def load(self,path):
        with open(path,'rb') as fp:
            dict_param = pickle.load(fp)
        return dict_param

class OneHotEncoder(BaseEstimator, TransformerMixin, DiscreteMinxin):

    def __init__(self, dummy_na=True, handle_unknown='ignore',
                 category_threshold=50, drop_threshold_cols=True,
                 replace_na=-99):
        """
        parameter
        ---------
        dummy_na: bool, defualt True
        handle_unknown: str, 'error' or 'ignore'
        category_threshold: columns of categories more then this threhold will
                            not be encoded
        drop_threshold_cols: drop columns that not satisfy category_threshold
                             or columns of one category
        """
        self.dummy_na = dummy_na
        self.handle_unknown = handle_unknown
        self.category_threshold = category_threshold
        self.drop_threshold_cols = drop_threshold_cols
        self.encode_cols= []
        self.drop_cols=[]
        self.mapping = {}
        self.replace_na = replace_na
        self._dim = None

    def fit(self, X, y=None, cols_to_encoder=None, extra_numeric_cols=None):
        """
        parameter
        ----------
        X: DataFrame obj to generate one-hot-encoder rule

        cols_to_encoders: specify the columns  to be  encoded
        extra_numeric_cols: if cols_to_encoder is provided this param will
           not be used, otherwise all object columns and extra_numeric_cols
           will be encoded.
        """
        if not isinstance(X, pd.DataFrame):
            raise TypeError('X should be DataFrame object')

        if y is not None:
            if y not in X.columns:
                raise ValueError('y is not in X.columns during fit')
            self._dim = X.shape[1] -1
        else:
            self._dim = X.shape[1]

        if cols_to_encoder is None:
            cols = self.get_obj_cols(X)
            cols += list(extra_numeric_cols) if extra_numeric_cols is not None \
                    else []
        else:
            cols = cols_to_encoder

        cols = list(set(cols))

        #'no colums to encoder'
        if len(cols)==0:
            return
        # remove target if it is included in data source
        if isinstance(y, six.string_types) and y  in cols:
            cols.remove(y)

        # re-order cols by original order
        cols = sorted(cols, key=X.columns.get_loc)
        # convert na to pre -defined vlaue
        df = X[cols].fillna(self.replace_na, downcast='infer')
        # generato rules
        cats_list = pd.Series()
        for col in cols:
            cats_list[col] = df[col].unique().tolist()
            if not self.dummy_na and self.replace_na in cats_list[col]:
                cats_list[col].remove(self.replace_na)

        cats_cnt = cats_list.apply(lambda x: len(x))
        # exclude columns of too manay categories or just one category
        drop_mask = (cats_cnt > self.category_threshold) | (cats_cnt==1)
        drop_index = cats_cnt[drop_mask].index
        cats_list = cats_list[~cats_list.index.isin(drop_index)]

        self.drop_cols = drop_index.tolist()
        self.encode_cols = cats_list.index.tolist()

        maps={}
        for col in self.encode_cols:
            # map each val in col into a index
            val_list = cats_list[col]
            val_map = OrderedDict({val:i for i,val in enumerate(val_list)})
            maps[col] = val_map

        self.mapping = maps

    def transform(self, X, y=None, dtype=None, inplace=False):
        """
        parameter
        -----------
        dtype: specifies the dtype of encoded value
        """

        if not isinstance(X, pd.DataFrame):
            raise TypeError('X shoule be DataFrame object')
        if y is not None:
            if y not in X.columns:
                raise ValueError('y not in X.column during transform')
            if self._dim != X.shape[1] -1:
                raise ValueError('dimension error')
        elif self._dim != X.shape[1] :
            raise ValueError('dimension error')

        if not inplace:
            X = X.copy() # X=X.copy(deep=True)

        if self.drop_threshold_cols:
            X.drop(self.drop_cols, axis=1, inplace=True)

        data_to_encode = X[self.encode_cols].fillna(self.replace_na,
                          downcast='infer')
        with_dummies = [X.drop(self.encode_cols,axis=1)]

        prefix = self.encode_cols
        prefix_sep = cycle(['_'])

        for (col, pre, sep) in zip(data_to_encode.iteritems(), prefix,
                                   prefix_sep):
            # col is (col_name, col_series) type
            dummy = self._encode_column(col[1], pre, sep, dtype = dtype)
            with_dummies.append(dummy)

        result = pd.concat(with_dummies, axis=1)

        return result

    def _encode_column(self, data, prefix, prefix_sep, dtype):

        if dtype is None:
            dtype = np.uint8

        maps = self.mapping[prefix]
        dummy_strs = cycle([u'{prefix}{sep}{val}'])
        dummy_cols = [dummy_str.format(prefix=prefix,sep=prefix_sep,val=str(v))
                      for dummy_str, v in zip(dummy_strs, maps.keys())]

        if isinstance(data, pd.Series):
            index = data.index
        else:
            index = None

        row_idxs= []
        col_idxs= []
        for i, v in enumerate(data):
            idx = maps.get(v,None)
            if idx is None:
                print("{} only exist in test column '{}'".format(v, prefix))
            else:
                row_idxs.append(i)
                col_idxs.append(idx)
        sarr = csr_matrix((np.ones(len(row_idxs)),(row_idxs,col_idxs)),shape=
                          (len(data),len(dummy_cols)), dtype=dtype)

        out = pd.SparseDataFrame(sarr, index=index, columns=dummy_cols,
                           default_fill_value=0,dtype=dtype)

        return out.astype(dtype)

class MeanEncoder(BaseEstimator, TransformerMixin, DiscreteMinxin):
    def __init__(self, dummy_na = True, handle_unknown='prior', n_critical=1,
                 scale_factor=1, drop_last = False, replace_na= -99):
        """
        dummy_na: bool,if False the null values will be repaced with prior after
                  transform
        handle_unknown: str, 'error' of 'prior'
        drop_last: bool,whether to get C-1 categories out of C by removing the
                   last class.
        n_critical: the critical point that the posterior will contribute more
        scale_factor: scale the smoothing factor
        replace_na : int
        """
        self.dummy_na = dummy_na
        self.handle_unknown =handle_unknown
        self.n_critical = n_critical
        self.scale_factor = scale_factor
        self.drop_last = drop_last
        self.mapping={}
        self.prior=None
        self.encode_cols= None
        self.replace_na = replace_na  #
        self._dim =None     # attribution dimension

    def fit(self, X, y, cols_to_encode=None, extra_numeric_cols=None):

        if not isinstance(X, pd.DataFrame):
            raise ValueError('X should be DataFrame type')

        if isinstance(y, six.string_types):
            if y in X.columns:
                self._dim = X.shape[1] - 1
                X = X.rename(columns={y:'_y_'})
            else:
                raise ValueError('y not in X.columns during fit')
        else:
            self._dim = X.shape[1]
            y = pd.Series(y, name='_y_')
            X = X.join(y)

        X['_y_'] = X['_y_'].astype(int)

        # get encoder columns
        if cols_to_encode is None:
            cols = self.get_obj_cols(X)
            cols += list(extra_numeric_cols) if extra_numeric_cols is not None\
                    else []
        else:
            cols = cols_to_encode
        cols = list(set(cols))

        if len(cols)==0:
            return
        # re-order cols by original order
        cols = sorted(cols, key=X.columns.get_loc)
        self.encode_cols = cols

        data_to_encode = X[self.encode_cols+['_y_']]
        # convert na to a pre-defined value
        data_to_encode.fillna(self.replace_na, downcast='infer',inplace=True)
        prior = data_to_encode['_y_'].value_counts()/len(data_to_encode['_y_'])
        prior.sort_index(axis=0,inplace=True)
        prior.name='prior'
        self.prior = prior  #series

        maps = {}
        for col in self.encode_cols:
            ctb = pd.crosstab(index=data_to_encode[col], columns=data_to_encode['_y_'])
            # deal with missing y.
            ctb = ctb.reindex(columns=prior.index, fill_value = 0)
            ctb.sort_index(axis=1,inplace=True)
            # calculate posterior
            post = ctb.apply(lambda x: x/x.sum(), axis =1)
            # calcalate smoothing factor of prior and posterior
            smooth = ctb.applymap(lambda x: 1/(1+np.exp(-(x-self.n_critical)/self.scale_factor)))
            smooth_prior = (1-smooth).multiply(prior,axis=1) # DataFrame multiple series
            smooth_post =  smooth.multiply(post)
            codes = smooth_prior + smooth_post
            # normalize
            codes = codes.divide(codes.sum(axis=1),axis=0)
            # encode na with prior if na is not treated as a cateogry
            if not self.dummy_na and self.replace_na in codes.index:
                codes.loc[self.replace_na,:]=self.prior
            maps[col] =codes

        self.mapping = maps

    def transform(self, X, y=None):
        if not isinstance(X, pd.DataFrame):
            raise ValueError('X should be DataFrame type')
        if isinstance(y, six.string_types) and y in X.columns:
            if  self._dim != X.shape[1] -1:
                raise ValueError('dimension error')
        elif self._dim != X.shape[1]:
            raise ValueError('dimension error')

        if not self.encode_cols:
            return X

        data_to_encode = X[self.encode_cols]
        #fill na
        data_to_encode.fillna(self.replace_na, downcast='infer',inplace=True)

        with_dummies = [X.drop(self.encode_cols,axis=1)]

        prefix = self.encode_cols
        prefix_sep = cycle(['_'])

        for (col, pre, sep) in zip(data_to_encode.iteritems(), prefix,
                                   prefix_sep):
            # col is (col_name, col_series) type
            dummy = self._encode_column(col[1], pre, sep)
            with_dummies.append(dummy)

        result = pd.concat(with_dummies, axis=1)

        return result

    def _encode_column(self, data, prefix, prefix_sep):

        maps = self.mapping[prefix]
        dummy_strs = cycle([u'{prefix}{sep}{val}'])
        dummy_cols = [dummy_str.format(prefix=prefix,sep=prefix_sep,val=str(v))
                      for dummy_str, v in zip(dummy_strs, maps.columns)]

        if isinstance(data, pd.Series):
            index = data.index
        else:
            index = None

        enc_df = maps.loc[data.values,:] # NaN with unknonw value
        #handle unknown value
        if not all(data.isin(maps.index)):
            msg = "unknown category {} in column '{}'".format(
                        data[~data.isin(maps.index)].values, prefix)
            if self.handle_unknown=='error' :
                raise ValueError(msg)
            else:
                print(msg)
                enc_df.fillna(self.prior, inplace=True)

        enc_df.index = index
        enc_df.columns = dummy_cols
        if self.drop_last:
            enc_df = enc_df.iloc[:,:-1]

        return enc_df

class WoeEncoder(BaseEstimator, TransformerMixin, DiscreteMinxin):
    """
     currently only support discrete variable encode.
    """
    def __init__(self, dummy_na = True, handle_unknown='zero', replace_na=-99,
                 reg = 1):
        '''
        dummy_na: bool, if true null value is treated as a category, otherwise
                  null value will be filled with zero.
        handle_unknown: one of ('zero', 'error')
        reg: int, bayesian prior value  to avoid divding by zero when calculate woe.

        '''
        self.dummy_na = dummy_na
        self.handle_unknown = handle_unknown
        self.replace_na = replace_na
        self.mapping ={}
        self.reg = reg
        self._dim = None

    def fit(self, X, y, cols_to_encode = None, extra_numeric_cols=None):

        if not isinstance(X, pd.DataFrame):
            raise ValueError('X should be DataFrame type')

        if isinstance(y, six.string_types):
            if y in X.columns:
                self._dim = X.shape[1] - 1
                X = X.rename(columns={y:'_y_'})
            else:
                raise ValueError('y not in X.columns during fit')
        else:
            self._dim = X.shape[1]
            y = pd.Series(y, name='_y_')
            X = X.join(y)
        # target label as '_y_'
        X['_y_'] = X['_y_'].astype(int)

        # get encoder columns
        if cols_to_encode is None:
            cols = self.get_obj_cols(X)
            cols += list(extra_numeric_cols) if extra_numeric_cols is not None \
                    else []
        else:
            cols = cols_to_encode
        # re-order cols by original order
        self.encode_cols = sorted(list(set(cols)), key=X.columns.get_loc)

        if len(self.encode_cols)==0:
            return

        data_to_encode = X[self.encode_cols+['_y_']]
        # convert na to a predefined value
        data_to_encode.fillna(self.replace_na, downcast='infer',inplace=True)

        self._pos = data_to_encode['_y_'].sum()  # global positive count
        self._neg = len(data_to_encode['_y_']) - self._pos # global negative count

        maps ={}
        for col in self.encode_cols:
            woe = self._compute_woe(data_to_encode, col, '_y_') # return series
            maps[col] = woe

        self.mapping = maps

    def _compute_woe(self, df, var, y='_y_'):
        grp = df[y].groupby(df[var]).agg(['sum',lambda x: x.count()-x.sum()])
        grp = grp.rename(columns={'sum':'pos', '<lambda>':'neg'})
        #use bayesian prior value to avoid dividing by zero
        woe = np.log((grp['pos']+self.reg)/(grp['neg']+self.reg)) - \
              np.log((self._pos+2*self.reg)/(self._neg+2*self.reg))
        if not self.dummy_na and self.replace_na in woe:
            woe[self.replace_na] = 0.0
        return woe

    def transform(self, X, y=None, inplace=False):
        if not isinstance(X, pd.DataFrame):
            raise ValueError('X should be DataFrame type')
        if isinstance(y, six.string_types) and y in X.columns:
            if  self._dim != X.shape[1] -1:
                raise ValueError('dimension error')
        elif self._dim != X.shape[1]:
            raise ValueError('dimension error')

        if not self.encode_cols:
            return X

        if not inplace:
            X = X.copy()

        X[self.encode_cols] = X[self.encode_cols].fillna(self.replace_na,
                                     downcast = 'infer')
        msg = "unseen category {} in column '{}'"
        for col in self.encode_cols:
            X[col] = X[col].map(self.mapping[col]) # unseen value filled with NaN
            #handle unknown value
            if any(X[col].isnull()):
                if self.handle_unknown == 'error':
                    raise ValueError(msg.format(X[X[col].isnull()][col].values, col))
                else:
                    print(msg.format(X[X[col].isnull()][col].values, col))
                    X[col] = X[col].fillna(0.0)
        return X

if __name__== '__main__':
    warnings.filterwarnings('ignore')

    print('OneHotEncoder'.center(40,'-'))
    df = pd.DataFrame([[1,2,3,'a',2.0],[1,np.nan,4,'6',3.0],[2,3,4,5,6],
                       [2.0,3,4,5,np.nan]],columns=['x','y','z','j','k'])
    df.index=['1','2','3','4']
    ohe = OneHotEncoder(dummy_na=False)
    ohe.fit(df,extra_numeric_cols=['y','x','j'])
    print(df)
    print(ohe.transform(df))

    print('MeanEncoder'.center(40,'-'))
    df = pd.DataFrame([[1,2,3],[np.nan,3,4],[2,3,6],[2,4,2]],columns=['a','b','c'])
    y = pd.Series([1,0,0,1])

    mec = MeanEncoder(drop_last=False,dummy_na=True)
    mec.fit(df,y,extra_numeric_cols=['a','b'])
    print(mec.transform(df))
    df2 = pd.DataFrame([[1,2,3],[np.nan,3,4],[3,3,6],[2,4,2]],columns=['a','b','c'])
    print(mec.transform(df2))

    print('woe encdoer'.center(40,'-'))
    wec = WoeEncoder()
    wec.fit(df,y, extra_numeric_cols=['a','b','c'])
    print(wec.transform(df))


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 10 14:44:39 2021

@author: nkruyer3
"""
import numpy as np
from process_model import BDO_production_film
from process_model import membrane_sep as MEM

#Calculates water usage for biofilm process
def water_film(land, flow):
    PBR_water = land*1.84*2.18
    enz_water = flow*60*48
    ferm_water = flow*72

    return PBR_water, enz_water, ferm_water

#Calculates power usage for biofilm process
def power_film(land, flow, enz, ferm, prod, enz_yield, ferm_yield):
    PBR_power = 3.883e-5*land*1.84 #Source - Ozkan et al, 2012
    enz_power = enz/1000*1.5
    ferm_power = ferm/1000*1.5
    LLE_power = flow*60/1000*1.5
    mem_power = mem_heat(land,prod, enz_yield, ferm_yield)/60

    return sum([PBR_power, enz_power, ferm_power, LLE_power, mem_power])

#Calculates power usage in the membrane separator
def mem_heat(land, prod, enz_yield, ferm_yield):
    res = BDO_production_film(land, prod, enz_yield, ferm_yield)
    flows = res[2].x
    mem_out = MEM(0.5, flows[6:9])
    h2o_flow = mem_out.y[0,:]
    bdo_flow = mem_out.y[1,:]
    but_flow = mem_out.y[2,:]

    H_h2o = 2.4 #kJ/g
    H_bdo = 0.644 #kJ/g
    H_but = 0.739 #kJ/g

    h2o_heat = [-H_h2o*(h2o_flow[i]-h2o_flow[i-1]) for i in range(1,len(h2o_flow))]
    bdo_heat = [-H_bdo*(bdo_flow[i]-bdo_flow[i-1]) for i in range(1,len(bdo_flow))]
    but_heat = [-H_but*(but_flow[i]-but_flow[i-1]) for i in range(1,len(but_flow))]

    heat = sum(h2o_heat) + sum(bdo_heat) + sum(but_heat) #kJ/min

    return heat

#calculates payload mass for the biofilm growth mode
#input = cyano cultivation land, biomass productivity, volume of enzyme reactor, volume of fermenter...
#volume of lle unit, density of biofilm growth substrate, density of reactor material
#returns mass of each unit operation
def mass_film(land, prod, enz, ferm, lle, PBR_dens, reac_dens):
    PBR_mass = (land*1.84)*0.0003*100*100*100*PBR_dens/1000/1000
    frame = frame_mass(land)
    DAP_mass = prod*500*land*0.002/22.2*132.06/1000000*1.2
    NH3_mass = ((prod*500*land*0.029/22.2*1.2)-(DAP_mass*1000000/132.06*2))*17.031/1000000
    TE_mass = 1.84*land*2.18*2.12837/1000000 #mass of trace elements, based on Cogne, 2002
    enz_mass = tank_mass(enz, reac_dens) + 3*enz*50/1000000 #adds in total mass of enzymes needed
    ferm_mass = tank_mass(ferm, reac_dens)
    LLE_mass = tank_mass(lle, reac_dens) + 0.001*60*24*500*810/1000000 #add mass of butanol needed
    mem_mass = 0.000623 #assume membrane mass does not fluctuate much, <<1% of total mass so small changes won't majorly impact total mass
    sto_tank_mass = 0.545 #2,3-BDO and O2 required = constant --> storage tank same'
    h2o_treat_mass = 0.008

    return PBR_mass, frame, DAP_mass, NH3_mass, TE_mass, enz_mass, ferm_mass, LLE_mass, mem_mass, sto_tank_mass, h2o_treat_mass

#Function to calcualte mass of reactor tank - based on common specificiations
def tank_mass(vol, density):
    v = (vol*1.2)/1000
    r = np.cbrt(v/(5*np.pi))
    h = r*5
    SA = 2*np.pi*r*h + 2*np.pi*r**2
    steel_v = SA*0.006
    mass = steel_v*density

    return mass

#Calculates mass of cyanobacterial support frame
def frame_mass(land):
    mass = 1.154*land/10000*1.38*0.38 # 1.154 = (78/7.85)*(mass of biofilm PBR + water + algae + nutrients/ mass of suspended PBR + water + algae + nutrients) - i.e. mass frame must support
    return mass

#Determines total water, power and mass usage for complete process using biofilm growth
#Inputs = cyano cultivation land, flow rate to enzyme digester, biomass productivity, digester yield, fermenter yield, ...
#biofilm growth substrate density, reactor material density
#Returns water in L, power in kW and mass in tons
def specs(land, flow, prod, enz_yield, ferm_yield, PBR_dens, reac_dens):
    PBR, enz, ferm = water_film(land,flow)
    water = sum([PBR, enz, ferm])
    power = power_film(land, flow, enz, ferm, prod, enz_yield, ferm_yield)
    mass = sum(mass_film(land, prod, enz, ferm, flow*60, PBR_dens, reac_dens))

    return water, power, mass

ext: py

max_stars_repo_name: PPYLab/bioISRU

max_issues_repo_name: PPYLab/bioISRU

max_forks_repo_name: PPYLab/bioISRU

reason: import numpy


text:
from quaternion import *
import timeit
import numpy as np
import cv2
import random

class Node(object):
	def __init__(self, _id, _layer, _x, _y):
		self.id = _id
		self.layer = _layer
		self.x = _x
		self.y = _y

		return

class Link(object):
	def __init__(self, _id, _type, _node0, _node1):
		self.id = _id
		self.type = _type
		self.layer = _node0.layer
		self.node0 = _node0
		self.node1 = _node1

		return

class Arrow(object):
	def __init__(self, _id, _type, _node0, _node1):
		self.id = _id
		# 0: down, 1: up, 2: both
		self.type = _type
		self.layer = _node0.layer
		self.node0 = _node0
		self.node1 = _node1

		return

class Step(object):
	def __init__(self, _dx, _dy, _pressure, _current_floor, _scale=1.0):
		self.dx = _dx * _scale
		self.dy = _dy * _scale
		self.pressure = _pressure
		self.current_floor = _current_floor

		self.current_position = None

		return

class Particle(object):
	def __init__(self, _x, _y, _pressure, _current_floor, _scale, _rotate_angle):
		self.x = _x
		self.y = _y
		self.pressure = _pressure
		self.current_floor = _current_floor

		self.scale = _scale
		self.rotate_angle = _rotate_angle
		self.rotation = Quaternion()
		self.rotation.RotateZ(_rotate_angle)

		return

class MapMatching(object):
	def __init__(self):
		map_path = 'map\\'
		map_filename = 'core.txt'

		self.floor_number = 1
		self.map_images = []
		self.floor_height = 5.0
		self.start_floor = 0
		self.start_x = 0.0
		self.start_y = 0.0
		self.dir_angle = 0.0
		self.meter_to_pixel = 1.0
		self.node_filename = None
		self.link_filename = None
		self.arrow_filename = None
		self.polygon_filename = None

		in_file = open(map_path + map_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			if len(tSeq) < 1:
				continue
			if tSeq[0] == 'floor':
				self.floor_number = int(tSeq[1])
				for i in range(self.floor_number):
					self.map_images.append(
						cv2.imread(
							map_path + tSeq[i + 2].split('\n')[0]
						)
					)
			elif tSeq[0] == 'height':
				self.floor_height = float(tSeq[1])
			elif tSeq[0] == 'start':
				self.start_floor = int(tSeq[1])
			elif tSeq[0] == 'start_point':
				self.start_x = float(tSeq[1])
				self.start_y = float(tSeq[2])
			elif tSeq[0] == 'dir':
				self.dir_angle = float(tSeq[1])
			elif tSeq[0] == 'scale':
				self.meter_to_pixel = 1.0 / float(tSeq[1])
			elif tSeq[0] == 'node':
				self.node_filename = tSeq[1].split('\n')[0]
			elif tSeq[0] == 'link':
				self.link_filename = tSeq[1].split('\n')[0]
			elif tSeq[0] == 'arrow':
				self.arrow_filename = tSeq[1].split('\n')[0]
			elif tSeq[0] == 'polygon':
				self.polygon_filename = tSeq[1].split('\n')[0]
		in_file.close()

		self.current_floor = self.start_floor
		self.current_position = Vector3()
		self.current_position.x = self.start_x
		self.current_position.y = self.start_y
		self.current_position.z = self.dir_angle
		self.delta_pressure_to_delta_height = -9.0
		self.start_pressure = -1.0
		self.pressure_gain = 0.0

		self.map_width = self.map_images[0].shape[1]
		self.map_height = self.map_images[0].shape[0]

		self.resize_width = self.map_width
		self.resize_height = self.map_height

		self.video_writer = cv2.VideoWriter('video.avi', cv2.VideoWriter_fourcc('X', 'V', 'I', 'D'), 20, (self.resize_width, self.resize_height), True)

		self.floor_changed = False
		self.last_floor = self.current_floor

		self.initial_rotation = Quaternion()
		self.initial_rotation.RotateZ(self.dir_angle)

		self.nodes = {}
		self.links = []
		self.arrows = []
		self.polygon_zone = []
		for i in range(self.floor_number):
			self.links.append([])
			self.arrows.append([])
			self.polygon_zone.append(
				np.ones(
					(self.map_height, self.map_width, 1),
					dtype=np.uint8
				) * 255
			)

		if self.node_filename is None:
			print('Error: node file is None...')
			return

		in_file = open(map_path + self.node_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			if len(tSeq) < 1:
				continue
			tNode = Node(
				int(tSeq[0]),
				int(tSeq[1]),
				float(tSeq[2]),
				float(tSeq[3])
			)
			self.nodes[tNode.id] = tNode
		in_file.close()

		if self.link_filename is None:
			print('Error: link file is None...')

		in_file = open(map_path + self.link_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			if len(tSeq) < 1:
				continue
			tLink = Link(
				int(tSeq[0]),
				int(tSeq[1]),
				self.nodes[int(tSeq[2])],
				self.nodes[int(tSeq[3])]
			)
			self.links[tLink.layer].append(tLink)
		in_file.close()

		if self.arrow_filename is None:
			print('Error: arrow file is None...')

		in_file = open(map_path + self.arrow_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			tArrow = Arrow(
				int(tSeq[0]),
				int(tSeq[1]),
				self.nodes[int(tSeq[2])],
				self.nodes[int(tSeq[3])]
			)
			self.arrows[tArrow.layer].append(tArrow)
		in_file.close()

		if self.polygon_filename is None:
			print('Error: polygon file is None...')

		in_file = open(map_path + self.polygon_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			temp_type = int(tSeq[1])
			if temp_type != 1:
				continue
			temp_node_number = int(tSeq[2])
			temp_points = []
			temp_layer = None
			for i in range(temp_node_number):
				tNode = self.nodes[int(tSeq[i + 3])]
				temp_points.append((int(tNode.x), int(tNode.y)))
				temp_layer = tNode.layer
			temp_points_array = np.array([temp_points], dtype=np.int32)
			cv2.fillPoly(self.polygon_zone[temp_layer], temp_points_array, temp_type)
		in_file.close()

		in_file = open(map_path + self.polygon_filename, 'r')
		in_file.readline()
		for tLine in in_file.readlines():
			tSeq = tLine.split(',')
			temp_type = int(tSeq[1])
			if temp_type == 1:
				continue
			temp_node_number = int(tSeq[2])
			temp_points = []
			temp_layer = None
			for i in range(temp_node_number):
				tNode = self.nodes[int(tSeq[i + 3])]
				temp_points.append((int(tNode.x), int(tNode.y)))
				temp_layer = tNode.layer
			temp_points_array = np.array([temp_points], dtype=np.int32)
			cv2.fillPoly(self.polygon_zone[temp_layer], temp_points_array, temp_type)
		in_file.close()

		self.steps = []

		self.particles = []
		for i in range(9):
			for j in range(5):
				self.particles.append(
					Particle(
						self.start_x,
						self.start_y,
						self.start_pressure + self.pressure_gain,
						self.start_floor,
						1.0 + (i - 4) * 0.025,
						self.dir_angle + (j - 2) * 0.05
					)
				)

		self.particle_number_max = 512
		particle_number = self.particle_number_max - 45
		for i in range(particle_number):
			self.particles.append(
				Particle(
					self.start_x,
					self.start_y,
					self.start_pressure + self.pressure_gain,
					self.start_floor,
					random.uniform(0.9, 1.1),
					self.dir_angle + random.uniform(-0.1, 0.1)
				)
			)

		self.view_particle = True

		self.window_name = 'Current Position'

		self.position_filename = 'log\\position.csv'
		log_file = open(self.position_filename, 'w')
		log_file.write('x,y,z')
		log_file.close()

		self.time_start = 1515688724000
		self.log_list = []

		self.IPIN_filename = 'log\\kyushu.csv'
		log_file = open(self.IPIN_filename, 'w')
		temp_longitude, temp_latitude, temp_floor = self.MapToWorld(self.current_position.x, self.current_position.y, self.current_floor)
		log_file.write(
			'%i,%.9f,%.9f,%i,%i' %
			(
				self.time_start + int(timeit.default_timer() * 1000),
				temp_longitude,
				temp_latitude,
				temp_floor,
				0
			)
		)
		log_file.close()

		return

	def Release(self):
		cv2.destroyWindow(self.window_name)

		self.video_writer.release()

		return

	def MapToWorld(self, _x, _y, _floor):
		homography = [
			[-2.10388667722e-05, -0.000262518473727, -1.63503055188],
			[0.000604240370159, 0.00774479195666, 47.2205108312],
			[1.27234617272e-05, 0.000163991977084, 1.0]
		]

		rx = homography[0][0] * _x + homography[0][1] * _y + homography[0][2]
		ry = homography[1][0] * _x + homography[1][1] * _y + homography[1][2]
		rw = homography[2][0] * _x + homography[2][1] * _y + homography[2][2]
		rx /= rw
		ry /= rw

		return rx, ry, _floor - 1

	def DistanceToLine(self, _line, _pt):
		line_length = math.sqrt((_line[0][0] - _line[1][0]) ** 2 + (_line[0][1] - _line[1][1]) ** 2)
		dist_0 = math.sqrt((_pt[0] - _line[0][0]) ** 2 + (_pt[1] - _line[0][1]) ** 2)
		dot_0 = (_pt[0] - _line[0][0]) * (_line[1][0] - _line[0][0]) + (_pt[1] - _line[0][1]) * (_line[1][1] - _line[0][1])
		line_projection = dot_0 / line_length

		if line_projection < 0.0:
			return math.sqrt((_pt[0] - _line[0][0]) ** 2 + (_pt[1] - _line[0][1]) ** 2)
		if line_projection > line_length:
			return math.sqrt((_pt[0] - _line[1][0]) ** 2 + (_pt[1] - _line[1][1]) ** 2)

		return math.sqrt(max(0.01, dist_0 ** 2 - line_projection ** 2))

	def IntersectBase(self, _x0, _y0, _x1, _y1, _x2, _y2, _x3, _y3):
		threshold = 0.1

		mid_x_0 = (_x0 + _x1) * 0.5
		mid_y_0 = (_y0 + _y1) * 0.5

		mid_x_1 = (_x2 + _x3) * 0.5
		mid_y_1 = (_y2 + _y3) * 0.5

		if (abs(mid_x_1 - mid_x_0) > abs(_x1 - mid_x_0) + abs(_x3 - mid_x_1) + threshold) or (abs(mid_y_1 - mid_y_0) > abs(_y1 - mid_y_0) + abs(_y3 - mid_y_1) + threshold):
			return False

		return True

	def Intersect(self, _x0, _y0, _x1, _y1, _x2, _y2, _x3, _y3):
		if not self.IntersectBase(_x0, _y0, _x1, _y1, _x2, _y2, _x3, _y3):
			return False, -1.0, -1.0

		s_x_0 = _x1 - _x0
		s_y_0 = _y1 - _y0
		s_x_1 = _x3 - _x2
		s_y_1 = _y3 - _y2

		determinant = s_x_0 * s_y_1 - s_x_1 * s_y_0
		threshold = 0.0001
		if abs(determinant) < threshold:
			return False, -1.0, -1.0

		s = (s_x_0 * (_y0 - _y2) - s_y_0 * (_x0 - _x2)) / determinant
		if s < 0.0 or s > 1.0:
			return False, -1.0, -1.0

		t = (s_x_1 * (_y0 - _y2) - s_y_1 * (_x0 - _x2)) / determinant
		if t < 0.0 or t > 1.0:
			return False, -1.0, -1.0

		return True, t, s

	def GetCurrentFloor(self, _current_pressure):
		current_floor = self.start_floor
		delta_height = (_current_pressure - (self.start_pressure + self.pressure_gain)) * self.delta_pressure_to_delta_height
		while delta_height > self.floor_height * 0.5:
			current_floor += 1
			delta_height -= self.floor_height
		while delta_height < -self.floor_height * 0.5:
			current_floor -= 1
			delta_height += self.floor_height

		if current_floor >= self.floor_number:
			current_floor = self.floor_number - 1
			print('Pressure error, start pressure: %.3f, current pressure: %.3f' % (self.start_pressure + self.pressure_gain, _current_pressure))
		if current_floor < 0:
			current_floor = 0
			print('Pressure error, start pressure: %.3f, current pressure: %.3f' % (self.start_pressure + self.pressure_gain, _current_pressure))

		return current_floor

	def UpdateCurrentFloor(self, _current_pressure):
		if not self.floor_changed:
			self.last_floor = self.current_floor
		self.current_floor = self.start_floor
		delta_height = (_current_pressure - (self.start_pressure + self.pressure_gain)) * self.delta_pressure_to_delta_height
		while delta_height > self.floor_height * 0.5:
			self.current_floor += 1
			delta_height -= self.floor_height
		while delta_height < -self.floor_height * 0.5:
			self.current_floor -= 1
			delta_height += self.floor_height

		if self.current_floor >= self.floor_number:
			self.current_floor = self.floor_number - 1
			print('Pressure error, start pressure: %.3f, current pressure: %.3f' % (self.start_pressure + self.pressure_gain, _current_pressure))
		if self.current_floor < 0:
			self.current_floor = 0
			print('Pressure error, start pressure: %.3f, current pressure: %.3f' % (self.start_pressure + self.pressure_gain, _current_pressure))

		if self.current_floor != self.last_floor:
			self.floor_changed = True

			temp_particles = self.particles
			self.particles = []
			for temp_particle in temp_particles:
				temp_x = int(temp_particle.x)
				temp_y = int(temp_particle.y)
				if (temp_x > -1) and (temp_x < self.map_width) and (temp_y > -1) and (temp_y < self.map_height) and (self.polygon_zone[self.current_floor][temp_y][temp_x] != 0):
					self.particles.append(temp_particle)

		return

	def UpdateCurrentAngle(self):
		particle_number = len(self.particles)
		if particle_number > 0:
			if particle_number > 1:
				base_angle = self.particles[0].rotate_angle
				self.current_position.z = 0.0
				for i in range(1, particle_number):
					temp_angle = self.particles[i].rotate_angle - base_angle
					while temp_angle > math.pi:
						temp_angle -= math.pi * 2.0
					while temp_angle < -math.pi:
						temp_angle += math.pi * 2.0
					self.current_position.z += temp_angle
				self.current_position.z /= particle_number
				self.current_position.z += base_angle
			else:
				self.current_position.z = self.particles[0].rotate_angle

		while self.current_position.z > math.pi:
			self.current_position.z -= math.pi * 2.0
		while self.current_position.z < -math.pi:
			self.current_position.z += math.pi * 2.0

		return

	def CheckNewParticle(self, _start, _end, _floor):
		for tLink in self.links[_floor]:
			tNode0 = tLink.node0
			tNode1 = tLink.node1
			temp_hit, temp_t, temp_s = self.Intersect(_start[0], _start[1], _end[0], _end[1], tNode0.x, tNode0.y, tNode1.x, tNode1.y)
			if temp_hit:
				return False

		return True

	def BackTrackingParticle(self, _particle, _step, _position_check_number=0, _position_error_max=2.0):
		last_x = _particle.x
		last_y = _particle.y
		scale = _particle.scale
		rotation = _particle.rotation

		temp_position_check_number = _position_check_number

		for tStep in _step:
			displacement = rotation.RotateVector(tStep.dx, tStep.dy, 0.0)
			current_x = last_x - displacement.x * scale
			current_y = last_y - displacement.y * scale
			for tLink in self.links[tStep.current_floor]:
				tNode0 = tLink.node0
				tNode1 = tLink.node1
				temp_hit, temp_t, temp_s = self.Intersect(last_x, last_y, current_x, current_y, tNode0.x, tNode0.y, tNode1.x, tNode1.y)
				if temp_hit:
					return False
			if temp_position_check_number > 0:
				temp_distance = math.sqrt((current_x - tStep.current_position.x) ** 2 + (current_y - tStep.current_position.y) ** 2)
				if temp_distance > _position_error_max:
					return False
				temp_position_check_number -= 1
			last_x = current_x
			last_y = current_y

		return True

	def CheckSteps(self, _position_start, _steps, _scale, _rotation):
		last_x = _position_start.x
		last_y = _position_start.y
		for tStep in _steps:
			displacement = _rotation.RotateVector(tStep.dx, tStep.dy, 0.0)
			current_x = last_x + displacement.x * _scale
			current_y = last_y + displacement.y * _scale
			for tLink in self.links[tStep.current_floor]:
				tNode0 = tLink.node0
				tNode1 = tLink.node1
				temp_hit, temp_t, temp_s = self.Intersect(last_x, last_y, current_x, current_y, tNode0.x, tNode0.y, tNode1.x, tNode1.y)
				if temp_hit:
					return False
			last_x = current_x
			last_y = current_y

		return True

	def CheckParticleSteps(self, _particle, _steps):
		for tStep in _steps:
			displacement = _particle.rotation.RotateVector(tStep.dx, tStep.dy, 0.0)
			current_x = _particle.x + displacement.x * _particle.scale
			current_y = _particle.y + displacement.y * _particle.scale
			for tLink in self.links[tStep.current_floor]:
				tNode0 = tLink.node0
				tNode1 = tLink.node1
				temp_hit, temp_t, temp_s = self.Intersect(_particle.x, _particle.y, current_x, current_y, tNode0.x, tNode0.y, tNode1.x, tNode1.y)
				if temp_hit:
					return False
			_particle.x = current_x
			_particle.y = current_y

		return True

	def WritePosition(self):
		log_file = open(self.position_filename, 'a')
		log_file.write(
			'\n%.3f,%.3f,%d' %
			(
				self.current_position.x,
				self.current_position.y,
				self.current_floor
			)
		)
		log_file.close()

		log_file = open(self.IPIN_filename, 'a')
		temp_longitude, temp_latitude, temp_floor = self.MapToWorld(self.current_position.x, self.current_position.y, self.current_floor)
		temp_index = 0
		if len(self.log_list) > 0:
			temp_index = self.log_list[0]
			self.log_list = self.log_list[1:]
			print('Logged index %i' % temp_index)
		log_file.write(
			'\n%i,%.9f,%.9f,%i,%i' %
			(
				self.time_start + int(timeit.default_timer() * 1000),
				temp_longitude,
				temp_latitude,
				temp_floor,
				temp_index
			)
		)
		log_file.close()

		return

	def ShowMap(self):
		image_show = self.map_images[self.current_floor].copy()
		if self.view_particle:
			for tParticle in self.particles:
				cv2.circle(
					image_show,
					(int(tParticle.x), int(tParticle.y)),
					1,
					(255, 0, 0),
					thickness=-1
				)
		cv2.circle(
			image_show,
			(int(self.current_position.x), int(self.current_position.y)),
			3,
			(0, 0, 255),
			thickness=-1
		)
		resized_image = cv2.resize(image_show, (self.resize_width, self.resize_height))
		cv2.imshow(self.window_name, resized_image)
		cv2.waitKey(5)
		self.video_writer.write(resized_image)

		return

	def TryRevive(self, _radius, _angle_range, _max_try_time, _back_tracking_step_number, _pressure, _position_check_number=0, _position_error_max=2.0):
		back_tracking_steps = []
		for i in range(1, _back_tracking_step_number + 1):
			if self.steps[-i].current_floor != self.current_floor:
				break
			back_tracking_steps.append(self.steps[-i])

		for i in range(_max_try_time):
			temp_angle = random.uniform(-math.pi, math.pi)
			temp_radius = random.uniform(0.0, _radius)
			new_x = self.current_position.x + math.cos(temp_angle) * temp_radius
			new_y = self.current_position.y + math.sin(temp_angle) * temp_radius
			temp_x = int(new_x)
			temp_y = int(new_y)
			if (temp_x > -1) and (temp_x < self.map_width) and (temp_y > -1) and (temp_y < self.map_height) and (self.polygon_zone[self.current_floor][temp_y][temp_x] != 0):
				new_particle = Particle(
					new_x,
					new_y,
					_pressure,
					self.current_floor,
					random.uniform(0.9, 1.1),
					self.current_position.z + random.uniform(-_angle_range, _angle_range)
				)
				if self.BackTrackingParticle(new_particle, back_tracking_steps, _position_check_number=_position_check_number, _position_error_max=_position_error_max):
					self.particles.append(new_particle)

		return

	def UpdatePressureGain(self, _dx, _dy, _z, _threshold):
		temp_rotation = Quaternion()
		temp_rotation.RotateZ(self.current_position.z)
		temp_displacement = temp_rotation.RotateVector(_dx, -_dy, 0.0)
		temp_x = int(self.current_position.x + temp_displacement.x * self.meter_to_pixel)
		temp_y = int(self.current_position.y + temp_displacement.y * self.meter_to_pixel)

		if (temp_x > -1) and (temp_x < self.map_width) and (temp_y > -1) and (temp_y < self.map_height) and (self.polygon_zone[self.current_floor][temp_y][temp_x] != 1):
			self.pressure_gain = _z - self.start_pressure - (self.current_floor - self.start_floor) * self.floor_height / self.delta_pressure_to_delta_height

		return

	def Update(self, _dx, _dy, _z, _check_floor=True):
		step_length = math.sqrt(_dx ** 2 + _dy ** 2)
		if (step_length < 0.1) or (step_length > 1.5):
			print('Dropped step, length: %.3fm' % step_length)
			return

		self.UpdatePressureGain(_dx, _dy, _z, 5.0 * self.meter_to_pixel)

		self.UpdateCurrentFloor(_z)

		temp_scale = self.meter_to_pixel
		temp_step = Step(_dx, -_dy, _z, self.current_floor, _scale=temp_scale)
		self.steps.append(temp_step)

		step_number = len(self.steps)

		temp_particles = self.particles
		self.particles = []
		x_sum = 0.0
		y_sum = 0.0
		for tParticle in temp_particles:
			if self.CheckParticleSteps(tParticle, [temp_step]):
				x_sum += tParticle.x
				y_sum += tParticle.y
				self.particles.append(tParticle)

		particle_number = len(self.particles)
		try_time_max = 8
		sample_number_max = 1
		back_tracking_step_number = min(32, step_number) + 1

		if particle_number > 0:
			self.current_position.x = x_sum / particle_number
			self.current_position.y = y_sum / particle_number
			self.UpdateCurrentAngle()
		else:
			temp_rotation = Quaternion()
			temp_rotation.RotateZ(self.current_position.z)
			temp_displacement = temp_rotation.RotateVector(temp_step.dx, temp_step.dy, 0.0)
			self.current_position.x += temp_displacement.x
			self.current_position.y += temp_displacement.y
		self.WritePosition()
		self.steps[-1].current_position = Vector3(_v=self.current_position)

		if particle_number > 0:
			sample_number = min(sample_number_max, particle_number // 2 + 1)
			generate_radius_min = 0.2 * self.meter_to_pixel
			generate_radius_max = 2.0 * self.meter_to_pixel
			temp_particles = []
			new_particle_number = self.particle_number_max - particle_number
			while new_particle_number > 0:
				slice_particles = random.sample(self.particles, sample_number)
				x_sum = 0.0
				y_sum = 0.0
				angle_sum = 0.0
				for tParticle in slice_particles:
					x_sum += tParticle.x
					y_sum += tParticle.y
					angle_sum += tParticle.rotate_angle
				x_sum /= sample_number
				y_sum /= sample_number
				angle_sum /= sample_number
				angle_sum += random.uniform(-0.1, 0.1)
				try_time = try_time_max
				while try_time > 0:
					try_time -= 1
					temp_angle = random.uniform(-math.pi, math.pi)
					temp_radius = random.uniform(generate_radius_min, generate_radius_max)
					new_x = x_sum + math.cos(temp_angle) * temp_radius
					new_y = y_sum + math.sin(temp_angle) * temp_radius
					new_particle = Particle(
						new_x,
						new_y,
						_z,
						self.current_floor,
						random.uniform(0.9, 1.1),
						angle_sum
					)
					if self.CheckNewParticle((x_sum, y_sum), (new_x, new_y), self.current_floor):
						back_tracking_steps = []
						for i in range(1, back_tracking_step_number):
							if self.steps[-i].current_floor != self.current_floor:
								break
							back_tracking_steps.append(self.steps[-i])
						if self.BackTrackingParticle(new_particle, back_tracking_steps, _position_check_number=0, _position_error_max=3.0):
							temp_particles.append(new_particle)
							break
				new_particle_number -= 1
			self.particles.extend(temp_particles)
		else:
			print('Try reviving...')
			self.TryRevive(10.0 * self.meter_to_pixel, 0.3, 256, min(16, step_number), _z, _position_check_number=0, _position_error_max=3.0)

		if _check_floor and self.floor_changed:
			step_number_min = 6
			step_number_test = 3
			back_step_number_max = 40
			# step_number_min >= step_number_test * 2
			delta_height_threshold = 0.3
			height_distance_threshold = 0.3 * self.floor_height
			height_distance_threshold_min = 0.25
			distance_threshold = 20.0 * self.meter_to_pixel
			if step_number > step_number_min:
				temp_floor_check = True
				for i in range(1, step_number_test + 1):
					if self.steps[-i].current_floor != self.current_floor:
						temp_floor_check = False
						break
				temp_delta_height = (self.steps[-1].pressure - self.steps[-1 - step_number_test].pressure) * self.delta_pressure_to_delta_height
				temp_height_distance = (self.steps[-1].pressure - (self.start_pressure + self.pressure_gain)) * self.delta_pressure_to_delta_height - (self.current_floor - self.start_floor) * self.floor_height
				if (((abs(temp_height_distance) < height_distance_threshold) and (abs(temp_delta_height) < delta_height_threshold)) or (abs(temp_height_distance) < height_distance_threshold_min)) and temp_floor_check:
					back_step_index = min(step_number - step_number_test, back_step_number_max + step_number_test) + 1
					temp_floor_flag = True
					for i in range(step_number_test + 1, back_step_index):
						if temp_floor_flag:
							if self.steps[-i].current_floor != self.current_floor:
								temp_floor_flag = False
						if not temp_floor_flag:
							if self.steps[-i].current_floor != self.last_floor:
								break
						temp_delta_height = (self.steps[-i].pressure - self.steps[-i - step_number_test].pressure) * self.delta_pressure_to_delta_height
						temp_height_distance = (self.steps[-i].pressure - (self.start_pressure + self.pressure_gain)) * self.delta_pressure_to_delta_height - (self.last_floor - self.start_floor) * self.floor_height
						if ((abs(temp_height_distance) < height_distance_threshold) and (abs(temp_delta_height) < delta_height_threshold)) or (abs(temp_height_distance) < height_distance_threshold_min):
							entrance_arrow = None
							entrance_distance = 0.0
							temp_type = 1
							if self.current_floor > self.last_floor:
								temp_type = 0
							for tArrow in self.arrows[self.current_floor]:
								if (tArrow.type == temp_type) or (tArrow.type == 2):
									temp_entrance_distance = math.sqrt((self.steps[-i].current_position.x - tArrow.node0.x) ** 2 + (self.steps[-i].current_position.y - tArrow.node0.y) ** 2)
									if (entrance_arrow is None) or (temp_entrance_distance < entrance_distance):
										entrance_arrow = tArrow
										entrance_distance = temp_entrance_distance
							if (entrance_arrow is not None) and (entrance_distance < distance_threshold):
								self.particles = []
								self.current_position.x = entrance_arrow.node1.x
								self.current_position.y = entrance_arrow.node1.y
								for j in range(5):
									for k in range(9):
										self.particles.append(
											Particle(
												entrance_arrow.node1.x,
												entrance_arrow.node1.y,
												self.steps[-1 - step_number_test].pressure,
												self.current_floor,
												1.0 + (k - 4) * 0.025,
												self.current_position.z + (j - 2) * 0.05
											)
										)
								temp_steps = self.steps[-step_number_test:]
								self.steps = self.steps[:-step_number_test]
								for tStep in temp_steps:
									self.Update(tStep.dx / self.meter_to_pixel, -tStep.dy / self.meter_to_pixel, tStep.pressure, _check_floor=False)
							break
					self.floor_changed = False
			else:
				self.floor_changed = False

		return

max_stars_repo_name: rairyuu/PDR-with-Map-Matching

max_issues_repo_name: Nronaldo/PDR-with-Map-Matching

max_forks_repo_name: rairyuu/PDR-with-Map-Matching


text:
from scikit2pmml import scikit2pmml
from sklearn.datasets import load_breast_cancer
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler

cancer = load_breast_cancer()
X = cancer.data.astype(np.float32)
y = cancer.target.astype(np.int32)
std = StandardScaler()
X_scaled = std.fit_transform(X)

model = LogisticRegression()
model.fit(X_scaled, y)

params = {
    'pmml_version': '4.2',
    'feature_names': cancer.feature_names,
    'target_values': cancer.target_names,
    'target_name': 'tumor_type',
    'copyright': 'Václav Čadek',
    'description': 'Simple Logistic Regression model.',
    'model_name': 'Breast Cancer Model'
}

scikit2pmml(estimator=model, transformer=std, file='cancer.pmml', **params)


text:
import os
import numpy as np
from gym.wrappers import Monitor
from tensorboardX import SummaryWriter
from tensorflow.keras.optimizers import RMSprop

from utilities.environment import get_env
from utilities.agent import define_model
from utilities.replay_buffer import ReplayBuffer

class Agent:
    def __init__(self, state_size, action_size, save_dir='agent_save'):
        self.action_size = action_size
        self.time_steps = 100000
        self.epsilon = 1.0
        self.epsilon_min = 0.01
        self.gamma = 0.99  # Discount Rate
        self.memory = ReplayBuffer(5000)

        self.save_dir = save_dir
        if not os.path.exists(self.save_dir):
            os.makedirs(self.save_dir)

        self.policy_model = define_model(state_size, action_size)
        self.target_model = define_model(state_size, action_size)

    def act(self, state):
        if np.random.rand() <= self.epsilon:
            return np.random.randint(self.action_size)
        else:
            act_values = self.policy_model.predict(state[np.newaxis])
            return np.argmax(act_values[0])

    def adjust_ep(self, step):
        fraction = min(1.0, float(step) / self.time_steps)
        self.epsilon = 1 + fraction * (self.epsilon_min - 1)

    def update_target_model(self, name):
        self.policy_model.save_weights('{}/{}'.format(self.save_dir, name))
        self.target_model.load_weights('{}/{}'.format(self.save_dir, name))

    def replay(self, batch_size):
        states, actions, rewards, next_states, dones = self.memory.sample(batch_size)
        next_action = np.argmax(self.policy_model.predict(next_states), axis=-1)
        target = rewards + (1-dones) * self.gamma * self.target_model.predict(next_states)[np.arange(batch_size), next_action]
        target_f = self.policy_model.predict(states)
        target_f[np.arange(batch_size), actions] = target
        self.policy_model.fit(states, target_f, epochs=1, verbose=0)

if __name__ == '__main__':
    writer = SummaryWriter('logs')

    env = get_env('PongNoFrameskip-v4', 4)
    env = Monitor(env, 'video', video_callable=lambda ep: ep % 50 == 0, force=True)

    action_size = env.action_space.n
    state_size = env.observation_space.shape
    agent = Agent(state_size, action_size)
    agent.update_target_model('model-base.h5')
    agent.policy_model.compile(loss='huber_loss', optimizer=RMSprop(learning_rate=0.0001))

    batch_size = 32
    num_steps = 1000000
    start_steps = 10000
    update_freq = 100
    episode_rewards = [0.0]

    state = env.reset()
    for step in range(num_steps):

        agent.adjust_ep(step)
        action = agent.act(state)
        next_state, reward, done, info = env.step(action)
        agent.memory.add(state, action, reward, next_state, done)

        state = next_state
        episode_rewards[-1] += reward

        if step > start_steps:
            agent.replay(batch_size)
            if step % update_freq == 0:
                agent.update_target_model('model-{}.h5'.format(step))

        if done:
            num_episodes = len(episode_rewards)
            mean_100ep_reward = round(np.mean(episode_rewards[-100:]), 2)

            print('episode: {}, step: {}/{}, mean reward: {}, epsilon: {:.3f}'.format(
                num_episodes, step+1, num_steps, mean_100ep_reward, agent.epsilon))
            writer.add_scalar('reward', mean_100ep_reward, num_episodes)
            writer.add_scalar('epsilon', agent.epsilon, num_episodes)
            writer.flush()

            state = env.reset()
            episode_rewards.append(0.0)


text:
import sys

sys.path.append('/usr/local/lib/python/')

import openmesh as OM
import numpy as np
import scipy.sparse as sparse
import REESMath.vector3 as V3
import REESMath.quaternion as Q
import REESMath.matrix3 as M3
import REESMass.mass as MASS
import REESMesh.mesh as MESH
import REESMesh.factory as MESH_FACTORY
import REESUtility.util as UTIL
from REESSimulation.types import *
from REESMath.functions import sinc
from math import cos
import xml.etree.ElementTree as ET
import os.path

def transform_shape_into_body_frame(shape):
    prop = MASS.compute_mass_properties(shape.mesh, 1.0)
    (shape.r, shape.q, shape.mass, shape.inertia) = MASS.xform_model_2_body_space(prop)
    #
    # shape.r and shape.q gives the rigid body transform from body to model space
    # We need to do the inverse transform here
    #
    MESH.translate(shape.mesh, -shape.r)
    MESH.rotate(shape.mesh, Q.conjugate(shape.q))

def get_position_vector(engine):
    x = np.zeros(len(engine.rigid_bodies)*7, dtype=np.float64)
    k = 0
    for body in engine.rigid_bodies.values():
        offset = 7*k
        x[offset:offset+3] = body.r
        x[offset+3:offset+7] = body.q
        k += 1
    return x

def set_position_vector(engine, x):
    k = 0
    for body in engine.rigid_bodies.values():
        offset = 7*k
        body.r = x[offset:offset+3]
        body.q = x[offset+3:offset+7]
        k += 1

def get_velocity_vector(engine):
    u = np.zeros((len(engine.rigid_bodies)*6,), dtype=np.float64)
    k = 0
    for body in engine.rigid_bodies.values():
        offset = 6*k
        u[offset:offset+3] = body.v
        u[offset+3:offset+6] = body.w
        k += 1
    return u

def set_velocity_vector(engine, u):
    k = 0
    for body in engine.rigid_bodies.values():
        offset = 6*k
        body.v = u[offset:offset+3]
        body.w = u[offset+3:offset+6]
        k += 1

def compute_keyframe_motion(keyframes, time):
    if time < keyframes[0][0]:
        r = keyframes[0][1]
        q = keyframes[0][2]
        return r, q, V3.zero(), V3.zero()

    N = len(keyframes)

    if time >= keyframes[N-1][0]:
        r = keyframes[N-1][1]
        q = keyframes[N-1][2]
        return r, q, V3.zero(), V3.zero()

    for n in range(N):
        keyframe0 = keyframes[n]
        keyframe1 = keyframes[(n+1) % N]

        time0 = keyframe0[0]
        r0 = keyframe0[1]
        q0 = keyframe0[2]

        time1 = keyframe1[0]
        r1 = keyframe1[1]
        q1 = keyframe1[2]

        if time0 <= time < time1:
            dT = time1 - time0
            t = (time - time0) / dT
            r = r0 + (r1-r0)*t
            q = Q.slerp(q0, q1, t)
            v = (r1-r0)/dT
            dQ = Q.prod(q1, Q.conjugate(q0))
            (theta, n) = Q.to_angle_axis(dQ)
            w = n*(theta/dT)
            return r, q, v, w
    raise RuntimeError('compute_keyframe_motion(): Internal error, could not find keyframes to interpolate from')

def record_motion(engine):

    if not engine.motion_recorder.on:
        return

    for body in engine.rigid_bodies.values():
        engine.motion_recorder.record(engine.solver_params.current_time, body)

def save_restart_data(engine):

    if not engine.solver_params.save_restart:
        return

    filename = os.path.join(engine.solver_params.restart_path, engine.solver_params.restart_filename)

    ext = os.path.splitext(filename)[-1].lower()

    if ext != '.xml':
        raise RuntimeError('save_restart_data(): save: file was not a xml file')

    root = ET.Element('restart')

    root.attrib['time'] = str(engine.solver_params.current_time)

    for body in engine.rigid_bodies.values():
        body_tag = ET.SubElement(root, 'body')
        body_tag.attrib['name'] = body.name
        body_tag.attrib['r'] = UTIL.array2string(body.r)
        body_tag.attrib['q'] = UTIL.array2string(body.q)
        body_tag.attrib['v'] = UTIL.array2string(body.v)
        body_tag.attrib['w'] = UTIL.array2string(body.w)

    UTIL.xml_pretty_indent(root)
    tree = ET.ElementTree(root)
    tree.write(filename)

def position_update(engine, x, u, dt):

    new_time = engine.solver_params.current_time + dt

    k = 0

    for body in engine.rigid_bodies.values():
        x_offset = 7*k
        u_offset = 6*k

        r = x[x_offset:x_offset+3]
        q = x[x_offset+3:x_offset+7]
        v = u[u_offset:u_offset+3]
        w = u[u_offset+3:u_offset+6]

        if body.is_free and not body.use_finite_update:

            r += v * dt

            q += Q.prod(
                Q.from_vector3(w),
                q
            ) * dt * 0.5

        elif body.is_free and body.use_finite_update and body.finite_update_rotation_axis is not None:

            r += v * dt

            # Split angular velocity into parallel (finite update) and orthogonal components (infinitesimal update)
            w_finite = np.dot(w, body.finite_update_rotation_axis) * body.finite_update_rotation_axis
            w_infinitesimal = w - w_finite

            # Apply finite rotation around rotation axis
            theta = np.linalg.norm(w_finite)*dt*0.5
            vec = w_finite * sinc(theta) * dt * 0.5
            q_finite = Q.from_array([cos(theta), vec[0], vec[1], vec[2]])
            q = Q.prod(q_finite, q)

            # Apply infinitesimal rotation update
            q += Q.prod(
                Q.from_vector3(w_infinitesimal),
                q
            ) * dt * 0.5

        elif body.is_free and body.use_finite_update and body.finite_update_rotation_axis is None:

            r += v * dt

            # Apply finite rotation around axis n = w / |w| with angle theta = |w|*dt
            theta = np.linalg.norm(w)*dt*0.5
            vec = body.w * sinc(theta) * dt * 0.5
            q_finite = Q.from_array([cos(theta), vec[0], vec[1], vec[2]])
            q = Q.prod(q_finite, q)

        elif body.is_scripted:

            if body.scripted_motion is None:
                raise RuntimeError('position_update(): scripted body did not have any scripted motion?')
            else:
                r, q, v, w = compute_keyframe_motion(body.scripted_motion.keyframes, new_time)

            u[u_offset:u_offset+3] = v
            u[u_offset+3:u_offset+6] = w

        x[x_offset:x_offset+3] = r
        x[x_offset+3:x_offset+7] = Q.unit(q)

        k += 1

def compute_total_external_forces(engine, x, u):
    f_ext = np.zeros((len(engine.rigid_bodies)*6,), dtype=np.float64)
    k = 0
    for body in engine.rigid_bodies.values():
        x_offset = 7*k
        u_offset = 6*k

        F = V3.zero()
        T = V3.zero()

        if body.is_free:
            r = x[x_offset:x_offset+3]
            q = x[x_offset+3:x_offset+7]
            v = u[u_offset:u_offset+3]
            w = u[u_offset+3:u_offset+6]

            for force_type in body.forces:
                (Fi, Ti) = force_type.compute(body, r, q, v, w)
                F += Fi
                T += Ti
            R = Q.to_matrix(q)
            I = MASS.update_inertia_tensor(R, body.inertia)
            T -= np.cross(w, np.dot(I, w), axis=0)

        f_ext[u_offset:u_offset+3] = F
        f_ext[u_offset+3:u_offset+6] = T

        k += 1
    return f_ext

def compute_inverse_mass_matrix(engine, x):
    N = len(engine.rigid_bodies)
    blocks = [np.zeros((3, 3), dtype=np.float64) for _ in range(N*2)]
    k = 0
    for body in engine.rigid_bodies.values():
        x_offset = 7*k  # Position offset into x-array
        if body.is_free:
            q = x[x_offset+3:x_offset+7]  # Extract rotation part
            R = Q.to_matrix(q)
            I = MASS.update_inertia_tensor(R, 1.0 / body.inertia)
            m = 1.0 / body.mass
            blocks[2*k] = M3.diag(m, m, m)
            blocks[2*k+1] = I
        k += 1
    D = sparse.block_diag(blocks)  # TODO Kenny 2017-02-12: Verify this is the most efficient way of doing this!
    W = D.tobsr(blocksize=(3, 3))
    return W

def compute_jacobian_matrix(engine):
    N = len(engine.rigid_bodies)
    K = len(engine.contact_points)

    cols = N*6
    rows = K*4
    for name, joint in engine.joints.items():
        rows += joint.dimensions()
    #for name, limit in engine.joint_limits.items():
    #    rows += limit.dimensions()
    #for name, motor in engine.joint_motors.items():
    #    rows += motor.dimensions()
    #J = sparse.csr_matrix()
    return None

def simulation_stepper(engine, dt):
    x = get_position_vector(engine)
    u = get_velocity_vector(engine)

    f_ext = compute_total_external_forces(engine, x, u)
    W = compute_inverse_mass_matrix(engine, x)

    # TODO Do collision detection and solve for constraint forces here

    u += W.dot(f_ext*dt)

    position_update(engine, x, u, dt)

    set_position_vector(engine, x)
    set_velocity_vector(engine, u)

    engine.solver_params.current_time += dt

def motion_player(engine, dt):

    new_time = engine.solver_params.current_time + dt

    if len(engine.motion_recorder.storage) <= 0:
        raise RuntimeError('player() No recorded motion data exist')

    for body in engine.rigid_bodies.values():

        if body.name not in engine.motion_recorder.storage:
            raise RuntimeError('player() Body was not in recorded motion storage')

        keyframe_motion = engine.motion_recorder.storage[body.name]

        if keyframe_motion is None:
            raise RuntimeError('player() could not find motion data for this body')

        r, q, v, w = compute_keyframe_motion(keyframe_motion.keyframes, new_time)

        body.r = r
        body.q = Q.unit(q)
        body.v = v
        body.w = w

def simulate(engine):
    if engine is None:
        return False

    if not engine.solver_params.on:
        return False

    if not engine.solver_params.mode == 'simulate':
        return False

    if engine.solver_params.current_time >= engine.solver_params.total_time:
        return False

    if engine.solver_params.current_time == 0.0:
        record_motion(engine)

    T = engine.solver_params.total_time - engine.solver_params.current_time
    T_safe = min(T, 1.0 / engine.solver_params.fps)
    dt = engine.solver_params.time_step

    while T_safe > 0:
        dt_safe = min(dt, T_safe)
        simulation_stepper(engine, dt_safe)
        T_safe = 0 if dt_safe < dt else T_safe - dt_safe

    record_motion(engine)

    save_restart_data(engine)

    # Just for debugging visualization
    if len(engine.contact_points) == 0:
        engine.contact_points = [ContactPoint(V3.rand(-1.0, 1.0), V3.rand(-1.0, 1.0), ) for _ in range(100)]

    return True

def play(engine):
    if engine is None:
        return False

    if not engine.solver_params.on:
        return False

    if not engine.solver_params.mode == 'play':
        return False

    if engine.solver_params.current_time >= engine.solver_params.total_time:
        return False

    T = engine.solver_params.total_time - engine.solver_params.current_time
    T_safe = min(T, 1.0 / engine.solver_params.fps)
    dt = engine.solver_params.time_step

    while T_safe > 0:
        dt_safe = min(dt, T_safe)
        motion_player(engine, dt_safe)
        T_safe = 0 if dt_safe < dt else T_safe - dt_safe

    return True

max_stars_repo_name: erleben/pyREES

max_issues_repo_name: erleben/pyREES

max_forks_repo_name: erleben/pyREES

reason: import numpy,import scipy


text:
#!/usr/bin/env python
import time
import sys

import numpy as np

from fffit import ga
from scipy.optimize import rosen

def sphere(x):
    total = 0
    return -np.dot(x,x)

def rastrigin(x):
    total = 0
    return -np.sum((x)**2 - 10 * np.cos(2*np.pi*x) + 10)

def rosenbrock(x):
    """ Rosenbrock test function

    As implemented in scipy."""
    return -rosen(x)

def main():
    np.set_printoptions(suppress=True, precision=4, linewidth=150)
    popsize = 100
    ranges = 100
    ndim = 30
    fit = []
    times = []
    step = []
    bounds = np.array((np.ones(ndim) * ranges * -1, np.ones(ndim) * ranges))
    w = 0.728994
    c = (0.5, 2.5)

    for run in range(20):
        print(f"Starting run {run}...", file=sys.stderr)
        start_time = time.perf_counter()
        p = ga.Genetic(maxiter=1000, goal=0, cross_over='two_points',
                       mutation_probability=0.01, mutation='uniform',
                       selection_method='elitism',num_parents=2,
                       num_elitism=10, bounds=bounds)
        p.ncpu = 1
        p.populate(popsize, x0=np.zeros(ndim), bounds=bounds)
        start_time = time.perf_counter()
        p.run(rastrigin, enum_particles=False, add_step_num=False, DEBUG=f'fitness_sphere_{run}.dat')
        stop_time = time.perf_counter()
        step.append(p.step_number)
        fit.append(p.fitness)
        times.append(stop_time - start_time)

    stepavg = np.mean(step)
    stepstdev = np.std(step)
    fitavg = np.mean(fit)
    fitstdev = np.std(fit)
    timeavg = np.mean(times)
    timestdev = np.std(times)

    print(f"Fitness: {fitavg:e} ± {fitstdev:e}")
    print(f"step: {stepavg:e} ± {stepstdev:e}")
    print(f"Time: {timeavg} ± {timestdev}")
    print(f"troca de population: {p.cont_new_population}")

if __name__ == '__main__':
    main()

max_stars_repo_name: jhonatheberson/artificial-intelligence

max_issues_repo_name: jhonatheberson/artificial-intelligence

max_forks_repo_name: jhonatheberson/artificial-intelligence

reason: import numpy,from scipy


text:
import numpy as np
from BDQuaternions import Rotation, Conventions

conventions = Conventions()
rotation = Rotation()
rotation.euler_angles_convention = 'Bunge'
print(rotation)

rotation.axis_angle = ([1, 0, 0], np.deg2rad(90))
print(rotation)

rotation2 = Rotation(rotation.quadruple * np.array([-1, -1, -1, -1]))
rotation2.euler_angles_convention = 'Bunge'
print(rotation2, 3*np.pi/2)
print(rotation2 == rotation)

print(conventions.list_euler_angles_conventions('special'))
print(conventions.list_euler_angles_conventions('derived'))
print(conventions.list_euler_angles_conventions(['special', 'derived']))
print(conventions.list_euler_angles_conventions('general'))

max_stars_repo_name: bond-anton/Quaternions

max_issues_repo_name: bond-anton/Quaternions

max_forks_repo_name: bond-anton/BDQuaternions


text:
import numpy as np
import time
import darr
from pathlib import Path
from darr.numtype import numtypesdescr, minmaxints

def datetimestring():
    return time.strftime('%Y%m%d%H%M%S')

def create_arrays(basepath='.'):
    basepath = Path(basepath) / 'examplearrays' / 'arrays'
    basepath.mkdir(parents=True, exist_ok=True)
    metadata = {
        "channels": [
            0,
            9
        ],
        "comments": "This example array has metadata, which is stored in a "
                    "separate JSON file. Metadata in Darr is a dictionary "
                    "that can contain anything that is JSON serializable.",
        "datetime": f"{datetimestring()}",
        "samplingrate": 25000.0
    }
    # 2D
    ar = np.arange(0,16).reshape(8,2) # sums to 120, axis 0 sums to 56, 1 to 64
    car = np.array(ar, dtype='complex128') + 2.0j
    for numtype in numtypesdescr.keys():
        if numtype.startswith('complex'):
            a = car
        else:
            a = ar
        darr.asarray(basepath / f'array_{numtype}_2D.darr', a,
                     dtype=numtype, metadata=metadata, overwrite=True)
    # 1D
    ar = [1, 3, 5, 7, 9, 11, 14] # 0:7 sums to 50
    car = np.array(ar, dtype='complex128') + 2.0j
    for numtype in numtypesdescr.keys():
        if numtype.startswith('complex'):
            a = car
        elif 'int' in numtype:
            imin, imax = minmaxints[numtype]
            a = ar[:]
            a.extend([imin,imax])
        else:
            a = ar
        darr.asarray(basepath / f'array_{numtype}_1D.darr', a,
                     dtype=numtype, metadata=metadata, overwrite=True)

def create_raggedarrays(basepath='.'):
    basepath = Path(basepath) / 'examplearrays' / 'raggedarrays'
    basepath.mkdir(parents=True, exist_ok=True)
    metadata = {
        "channels": [
            0,
            9
        ],
        "comments": "This example array has metadata, which is stored in a "
                    "separate JSON file. Metadata in dArray is a dictionary "
                    "that can contain anything that is JSON serializable.",
        "datetime": f"{datetimestring()}",
        "samplingrate": 25000.0
    }
    rar = [[[1, 2], [3, 4], [4, 6], [7, 8], [9, 10], [11, 12]],
           [[13, 14], [15, 16], [17, 18], [19, 20], [21, 22], [23, 24]],
           [[25, 26], [27, 28], [29, 30]],
           np.ones((0, 2)),
           [[31, 32], [33, 34], [35, 36], [37, 38]],
           [[39, 40]],
           [[41, 42], [43, 44], [45, 46]],
           [[47, 48]],
           [[49, 50], [51, 52]]]
    rarj = [np.array(sa, dtype='complex128') + 1.3j for sa in rar]
    for numtype in numtypesdescr.keys():
        if numtype.startswith('complex'):
            ar = rarj
        else:
            ar = rar
        _ = darr.asraggedarray(basepath / f'raggedarray_{numtype}.darr',
                               ar, dtype=numtype, metadata=metadata,
                               overwrite=True)

def create_codefile_array_idl(arraydirpath):
    allcode = []
    for arraypath in Path(arraydirpath).glob('*.darr'):
        ar = darr.Array(arraypath)
        code = ar.readcode('idl', abspath=True)
        if code is not None:
            code = code[:-1] # get rid of EOL
            allcode.append(f'; {arraypath.name}')
            allcode.append(code)
            if len(ar.shape) > 1: #2D
                allcode.append(f'; next should sum to {np.sum(ar)}')
                allcode.append(f'TOTAL(a)')
                allcode.append(f'; next should sum to {np.sum(ar[:,0])}')
                allcode.append(f'TOTAL(a[0,*])\n')
                allcode.append(f'; next should sum to {np.sum(ar[:, 1])}')
                allcode.append(f'TOTAL(a[1,*])\n')
            else: # 1D
                allcode.append(f'; next should sum to {np.sum(ar[:7])}')
                allcode.append(f'TOTAL(a[0:6])')
                if 'int' in ar.dtype.name:
                    allcode.append(f'; next should be {ar[7:]}')
                    allcode.append(f'a[7:*]')

    return '\n'.join(allcode)

def create_codefile_array_r(arraydirpath):
    allcode = []
    for arraypath in Path(arraydirpath).glob('*.darr'):
        ar = darr.Array(arraypath)
        code = ar.readcode('R', abspath=True)
        if code is not None:
            code = code[:-1] # get rid of EOL
            allcode.append(f'# {arraypath.name}')
            allcode.append(code)
            if len(ar.shape) > 1: # 2d
                allcode.append(f'# next should sum to {np.sum(ar)}')
                allcode.append(f'sum(a)')
                allcode.append(f'# next should sum to {np.sum(ar[:,0])}')
                allcode.append(f'sum(a[1,])\n')
                allcode.append(f'# next should sum to {np.sum(ar[:, 1])}')
                allcode.append(f'sum(a[2,])\n')
            else: # 1D
                allcode.append(f'# next should sum to {np.sum(ar[:7])}')
                allcode.append(f'sum(a[1:7])')
                if 'int' in ar.dtype.name:
                    allcode.append(f'# next should be {ar[7:]}')
                    allcode.append(f'a[8:9]')
    return '\n'.join(allcode)

def create_codefile_array_matlab(arraydirpath):
    allcode = []
    for arraypath in Path(arraydirpath).glob('*.darr'):
        ar = darr.Array(arraypath)
        code = ar.readcode('matlab', abspath=True)
        if code is not None:
            code = code[:-1] # get rid of EOL
            allcode.append(f'# {arraypath.name}')
            allcode.append(code)
            if len(ar.shape) > 1: # 2d
                allcode.append(f'# next should sum to {np.sum(ar)}')
                allcode.append(f'sum(a(:))')
                allcode.append(f'# next should sum to {np.sum(ar[:,0])}')
                allcode.append(f'sum(a(1,:))\n')
                allcode.append(f'# next should sum to {np.sum(ar[:, 1])}')
                allcode.append(f'sum(a(2,:))\n')
            else: # 1D
                allcode.append(f'# next should sum to {np.sum(ar[:7])}')
                allcode.append(f'sum(a(1:7))')
                if 'int' in ar.dtype.name:
                    allcode.append(f'# next should be {ar[7:]}')
                    allcode.append(f'a(8:9)')
    return '\n'.join(allcode)

def create_codefile_array_julia(arraydirpath):
    allcode = []
    for arraypath in Path(arraydirpath).glob('*.darr'):
        ar = darr.Array(arraypath)
        code = ar.readcode('julia_ver1', abspath=True)
        if code is not None:
            code = code[:-1] # get rid of EOL
            allcode.append(f'# {arraypath.name}')
            allcode.append(code)
            if len(ar.shape) > 1: # 2d
                allcode.append(f'# next should sum to {np.sum(ar)}')
                allcode.append(f'print(sum(a[:]))')
                allcode.append(f'# next should sum to {np.sum(ar[:,0])}')
                allcode.append(f'print(sum(a[1,:]))\n')
                allcode.append(f'# next should sum to {np.sum(ar[:, 1])}')
                allcode.append(f'print(sum(a[2,:]))\n')
            else: # 1D
                allcode.append(f'# next should sum to {np.sum(ar[:7])}')
                allcode.append(f'print(sum(a[1:7]))')
                if 'int' in ar.dtype.name:
                    allcode.append(f'# next should be {ar[7:]}')
                    allcode.append(f'print(a[8:9])')
    return '\n'.join(allcode)

if __name__ == "__main__":
    create_arrays()
    create_raggedarrays()

max_stars_repo_name: gjlbeckers-uu/DiskArray

max_issues_repo_name: gjlbeckers-uu/DiskArray

max_forks_repo_name: gjlbeckers-uu/DiskArray


text:
import numpy as np
import cv2 as cv
import os
import json
import numpy as np
import pandas as pd
import itertools
import argparse

def read_json(file):
    with open(file) as f:
        data = json.load(f)
    return data

def get_keypoints(data):
    '''
    Get the keypoints detected in the frame
    'data' is the info read in by read_json(folder, frame), basically a json file loaded into useable form
    '''
    kp = np.zeros([len(data['people']), 75])
    if len(kp)==0:
        return np.ones([1,75]).reshape([1,-1])*np.nan
    for i in range(len(data['people'])):
        kp[i] = data['people'][i]['pose_keypoints_2d']
    confidences = kp[:,2::3].sum(axis=1)
    return kp[np.argmax(confidences), :].reshape([1,-1])

def main(json_path, csv_path):
    files = [f for f in os.listdir(json_path) if 'json' in f]
    df = pd.DataFrame()
    i=0
    for f in files:
        data = read_json(f'{json_path}/{f}')
        keypoints = pd.DataFrame(get_keypoints(data))
        df = df.append(keypoints, ignore_index=True)
        i=i+1
        if i%1000==0:
            df.to_csv(csv_path)
    df.to_csv(csv_path)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('-i', '--input', type = str, help='path to the json  with openpose data')
    parser.add_argument('-o', '--output', type = str, help='name of the csv to storing resulting dataframe')
    args = parser.parse_args()
    main(args.input, args.output)

max_stars_repo_name: interaction-lab/modeling-pipeline

max_issues_repo_name: interaction-lab/modeling-pipeline

max_forks_repo_name: interaction-lab/modeling-pipeline


text:
import numpy as np
import pandas as pd

from src.helpers import paths

def find_nulls(df):
    print("# Columns with Null Records:")
    for clm in df.columns:
        null_count = df[clm].isnull().sum()
        if null_count > 0:
            print("\t\"{}\": {} records".format(clm, null_count))

if __name__ == "__main__":
    train_path = paths.raw.train_dataset()
    train_data = pd.read_csv(train_path)
    find_nulls(train_data)

    train_path = paths.interim.train_dataset()
    train_data = pd.read_csv(train_path)
    find_nulls(train_data)

    # test_path = paths.raw.test_dataset()
    # test_data = pd.read_csv(test_path)
    # find_nulls(test_data)

    # test_path = paths.interim.test_dataset()
    # test_data = pd.read_csv(test_path)
    # find_nulls(test_data)