text:
"""
$Id: testRegrid2.py 2324 2012-07-02 19:04:05Z dkindig $

Unit tests for regrid2.gsRegrid

"""

import cdms2
import regrid2
import numpy as np
import unittest
import openCreateData
PLOT = False
if PLOT:
    import matplotlib as mpl
    from matplotlib import pylab

class TestTimeRegridding(unittest.TestCase):

    def setUp(self):

        filename = "so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc"
        h=cdms2.open('./' + filename)
        self.hso = h('so')[0, 0, ...]
        self.hGrid = [self.hso.getLatitude(), self.hso.getLongitude()]
        self.grid2D = self.hso.getGrid()

        filename = "clt.nc"
        f=cdms2.open(filename)
        self.fclt = f('clt')
        self.fGrid = [self.fclt.getLatitude(), self.fclt.getLongitude()]
        g2D = self.fclt.getGrid().toCurveGrid()
        self.grid2D = [g2D.getLatitude()[:], g2D.getLongitude()[:]]

        filename = "era40_tas_sample.nc"
        g=cdms2.open(filename)
        self.gtas = g('tas')
        self.gGrid = [self.gtas.getLatitude(), self.gtas.getLongitude()]
        g2D = self.gtas.getGrid().toCurveGrid()
        self.grid2D = [g2D.getLatitude()[:], g2D.getLongitude()[:]]

        self.gcltVal010101 = 74.0087890625
        self.ftasVal1010 = 283.39410400390625
        self.soVal1010 = 33.88642120361328
        self.MissingValue = 1.e20
        self.eps = 1e-4

    def test1_Var_Regrid_with_time(self):
        test = 'test var.regrid with time'
        gclt = self.fclt.regrid(self.gtas.getGrid())
        self.assertEqual(gclt[0,...].shape, self.gtas[0,...].shape)
        self.assertEqual(gclt.shape[0], self.fclt.shape[0])
        self.assertLess(abs(gclt.mean()-self.fclt.mean()), 1)

    def test2_tasWithLevelsUsingLibCF(self):
        test = 'Time test using regrid2 gsRegrid'
        r2 = regrid2.GenericRegrid(self.gGrid, self.fGrid,
                                   dtype = self.gtas.dtype,
                                   regridMethod='linear',
                                   regridTool='libcf')
        r2.computeWeights()
        fShape = list(self.gtas.shape[:-2]) + list(self.fGrid[0].shape)
        ftas = np.ma.masked_array(np.ones(fShape, self.gtas.dtype)*self.gtas.missing_value,
                                  mask = np.zeros(fShape))
        ftas.missing_value = self.gtas.missing_value
        r2.apply(self.gtas, ftas)
        if PLOT:
            vmin = self.gtas.min()
            vmax = self.gtas.max()
            pylab.subplot(1,4,1)
            pylab.pcolor(self.gtas[-1,...], vmin=vmin, vmax=vmax)
            pylab.colorbar()
            pylab.title('self.gtas[-1,...]')
            pylab.subplot(1,4,2)
            pylab.pcolor(ftasOld[-1,...], vmin=vmin, vmax=vmax)
            pylab.colorbar()
            pylab.title('ftasOld[-1,...]')
            pylab.subplot(1,4,3)
            pylab.pcolor(ftas[0,...], vmin=vmin, vmax=vmax)
            pylab.colorbar()
            pylab.title('ftas[-1,...]')
            pylab.subplot(1,4,4)
            pylab.pcolor(ftas[-1,...] - ftasOld[-1,...], vmin=-1, vmax=1)
            pylab.colorbar()
            pylab.title('ftas[-1,...] - ftasOld[-1,...]')

        self.assertEqual(ftas[0,...].shape, self.fclt[0,...].shape)
        self.assertEqual(ftas.shape[0], self.gtas.shape[0])
        self.assertGreater(abs(ftas[ftas>0].mean()-self.gtas.mean()), 1)

    def test3_use_gsRegrid(self):
        test = 'test 1 - using gsRegrid'
        ro = regrid2.gsRegrid.Regrid(self.gGrid, self.fGrid)
        ro.computeWeights(20, .01*(180./(46)))
        ftas = np.zeros(self.fclt[0,...].shape, self.gtas.dtype)
        ro.apply(self.gtas[0,...], ftas)
        gtas0 = self.gtas[0,...]
        self.assertLess(abs(ftas[ftas>0].mean()-gtas0.mean()), 1)

    def test4_gsRegrid_Masking(self):
        # Masking
        test = 'test mask attached to data for gsRegrid'
        r3 = regrid2.GenericRegrid(self.hGrid, self.fGrid,
                                   dtype = self.hso.dtype,
                                   regridMethod='linear',
                                   regridTool='libcf')
        r3.computeWeights()
        gShape = list(self.hso.shape[:-2]) + list(self.fGrid[0].shape)
        gso = np.ma.masked_array(np.zeros(gShape, self.hso.dtype), mask = np.zeros(gShape))
        r3.apply(self.hso, gso)
        maxv = gso <= self.hso.max()+1
        aso = gso > 0
        bb = (np.array(maxv,np.int32) + np.array(aso,np.int32))==2
        self.assertLess(abs(gso[bb].mean()-self.hso.mean()), 1)
        self.assertFalse(gso.mask[10,10])
        self.assertFalse(gso.mask[3,3])

    def test5_gsRegrid_set_mask_before_regridding(self):
        test = 'test set mask and compute before regridding'
        so = self.hso
        sGrid = [so.getLatitude(), so.getLongitude()]
        r4 = regrid2.GenericRegrid(self.hGrid, self.fGrid,
                                   dtype = so.dtype,
                                   regridMethod = 'linear',
                                   regridTool = 'libcf',
                                   srcGridMask = self.hso.mask)
        r4.computeWeights()
        gShape = list(self.hso.shape[:-2]) + list(self.fGrid[0].shape)
        gso = np.ma.masked_array(np.zeros(gShape, self.hso.dtype), mask = np.zeros(gShape))
        r4.apply(so, gso)
        maxv = gso <= self.hso.max()+1
        aso = gso > 0
        bb = (np.array(maxv,np.int32) + np.array(aso,np.int32))==2
        self.assertLess(gso[bb].mean()-self.hso.mean(), 1)
        self.assertFalse(gso.mask[10,10])
        self.assertFalse(gso.mask[3,3])

if __name__ == '__main__':
    print ""
    suite = unittest.TestLoader().loadTestsFromTestCase(TestTimeRegridding)
    unittest.TextTestRunner(verbosity = 1).run(suite)
    if PLOT: pylab.show()


text:
import numpy as np

class DataLoaderIter():
    def __init__(self, data, batch_size):
        self.batch_size = batch_size
        self.data = data
        self.size = len(data)
        self.start = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.start >= self.size:
            raise StopIteration
        start = self.start
        end = min(self.start + self.batch_size, self.size)
        self.start = end
        return self.data[start : end]

class DataLoader(object):
    def __init__(self, data, batch_size, shuffle = False):
        self.batch_size = batch_size
        self.data = data
        self.shuffle = shuffle
        self.size = len(data)
        self.start = 0

    def __iter__(self):
        if self.shuffle:
            np.random.shuffle(self.data)
        return DataLoaderIter(self.data, self.batch_size)

ext: py

max_stars_repo_name: yagweb/rbm

max_issues_repo_name: yagweb/rbm

max_forks_repo_name: yagweb/rbm

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 22 11:55:48 2018

@author: mmeites
"""

""" Basic introduction to unit testing with Python
@brief get started with python testing by looking at https://docs.pytest.org/en/latest/getting-started.html#getstarted. Note that any script to be ran within the python testing framework (pytest) should follow the standard test discovery rules (https://docs.pytest.org/en/latest/goodpractices.html#test-discovery)
"""

def test_basicTrue():
    """ one of the simplest test that does nothing except saying it works..."""
    assert True

#testing session 1 functions
def load_S1_script():
    """
        utility function that tris to load the script written along the first lesson
        @throws an ImportError exception if the script file does not exist
        @return the script as a loaded module
    """
    S1_script_filename='assignments/Session1/S1_algotools.py'
    import imp
    s1_algotools=imp.load_source('session_1_script', S1_script_filename)
    return  s1_algotools

#load the scripts to check
def test_session1script_exists():
    try:
        load_S1_script()
        assert True
    except  ImportError:
        print('Expected script not found, carrefuly check the assignement instructions ')
        assert False

def check_S1_selective_average(testList):
    ##
    # utility function that asserts if load_S1_script().average_above_zero works fine
    # @param testList a list of values onto average_above_zero is applied
    # @test ensures the function returns the correct average value
    import numpy as np
    #another way to process the positive elements average to compare with
    positive_elements_float_array=np.array([i for i in testList if i >= 0], dtype=float)
    reference_average_value=np.mean(positive_elements_float_array)
    assert load_S1_script().average_above_zero(testList) ==reference_average_value # mm

def test_S1_selective_average_non_zeros_values():
    ##
    # @test validates average_above_zero works fine with integer values >0
    check_S1_selective_average([1,2,3,4,-7])

def test_S1_selective_average_with_zeros_values():
    ##
    # @test validates average_above_zero works fine with integer values >=0
    check_S1_selective_average([0,1,2,3,4,-7])

def test_S1_selective_average_with_negative_values():
    ##
    # @test validates average_above_zero works fine with integer values <=0
    check_S1_selective_average([0,-7])

def test_S1_selective_average_with_string_values():
    ##
    # @test validates average_above_zero works fine with integer values <=0
    check_S1_selective_average(['ab','c'])

def test_S1_selective_average_with_empty_list():
    ##
    # @test validates average_above_zero works fine with an empty list
    try:
        check_S1_selective_average([])
        assert False
    except ValueError:
        assert True

###
#Exercice2
###
def check_S1_max_value(testList):
# @param testList a list of values onto max_value is applied
   #import numpy as np
   #another way to search the maximum value in an array
   #elements_float_array=np.array([i for i in testList if i >= 0 ], dtype=float)
   #reference_max_value = np.amax(elements_float_array)
   assert load_S1_script().max_value(testList)  == ( 4,3) , "values aren't equals"

def test_S1_max_values_basic():
	check_S1_max_value([1,2,3,4,-7])

def test_S1_max_values_with_negative_values():
	check_S1_max_value([-3,-7])

def test_S1_max_values_with_string_values():
	check_S1_max_value(['ab','c'])

def test_S1_max_value_empty_list():
    try:
        check_S1_max_value([])
        assert False
    except ValueError:
        assert True

###
#Exercice3
###
def check_S1_reverse_table(testList):
    assert load_S1_script().reverse_table(testList)   ==[-5, 3, 2, 1]

def test_S1_reverse_table_basic():
	check_S1_reverse_table([1,2,3,-7])

def test_S1_reverse_table_with_string_values():
	check_S1_max_value(['ab','c'])

def test_S1_reverse_table_empty_list():
    try:
        check_S1_reverse_table([])
        assert False
    except ValueError:
        assert True

###
#Exercice4
###
def check_S1_bounding_box(inputMat):
    import numpy as np
    np.testing.assert_array_equal(load_S1_script().roi_bbox(inputMat),[[2,3],[2,4],[3,3],[3,4]])

def test_S1_bounding_box_basic():
    inputMat=[[False,False,False,False,False,False]
 [False,False,False,False,False,False]
 [False,False,False,True,True,False]
 [False,False,False,True,True,False]
 [False,False,False,False,False,False]]
    check_S1_bounding_box(inputMat)

def test_S1_bounding_box_with_integer_values():
    inputMat=[[0,1,2,3,4,5],[5,4,3,2,1,0]]
    check_S1_bounding_box(inputMat)

def test_S1_bounding_box_without_matrix_argument():
    inputMat=[1]
    check_S1_bounding_box(inputMat)


text:
#-----------------------------------------------------------------------------
# Copyright (c) 2020, Greg Landrum
# All rights reserved.
#
# The full license is in the LICENSE file, distributed with this software.
#-----------------------------------------------------------------------------

from rdkit import Chem
import pandas as pd
import intake
import numpy as np
import gzip

class SmilesSource(intake.source.base.DataSource):
    " Inspired by example here: https://github.com/intake/intake-examples/blob/master/tutorial/data_engineer.ipynb "
    container = 'dataframe'
    name = 'smiles'
    version = '0.1.0'
    partition_access = True
    suppl_factory = Chem.SmilesMolSupplier

    def __init__(self,
                 filename,
                 metadata=None,
                 nPartitions=1,
                 smilesColumn='smiles',
                 useSmarts=False,
                 **kwargs):
        super(SmilesSource, self).__init__(metadata=metadata)
        self._fname = filename
        self._smilesColumn = smilesColumn
        self._useSmarts = useSmarts
        for k in kwargs:
            if kwargs[k] == 'None':
                kwargs[k] = None
        self._supplkwargs = kwargs
        # this is, hopefully, for future use:
        self._nPartitions = nPartitions

    def _get_schema(self):
        " reads property names from the first molecule and uses those "
        # FIX handle error if the first molecule doesn't exist
        df = pd.read_csv(self._fname, nrows=1, **self._supplkwargs)
        dt = {'mol': 'O'}
        for pn, pv in zip(df.columns, df.iloc[0]):
            dt[pn] = np.dtype(type(pv)).name
        self._colNames = list(df.columns)
        return intake.source.base.Schema(datashape=None,
                                         dtype=dt,
                                         shape=(None, len(dt)),
                                         npartitions=self._nPartitions,
                                         extra_metadata=self._supplkwargs)

    def _get_partition(self, i):
        df = pd.read_csv(self._fname, **self._supplkwargs)
        partSize = len(df) // self._nPartitions
        start = i * partSize
        if i == self._nPartitions - 1:
            end = len(df)
        else:
            end = start + partSize
        res = []
        for idx in range(start, end):
            row = df.iloc[idx]
            if not self._useSmarts:
                m = Chem.MolFromSmiles(row[self._smilesColumn])
            else:
                m = Chem.MolFromSmarts(row[self._smilesColumn])
            if m is not None:
                nrow = {'mol': m}
                for k in self._colNames:
                    nrow[k] = row[k]
                res.append(nrow)
        return pd.DataFrame(res)

    def read(self):
        self._load_metadata()
        return pd.concat(
            [self.read_partition(i) for i in range(self.npartitions)])

    def _close(self):
        # close any files, sockets, etc
        self.suppl = None

max_stars_repo_name: greglandrum/intake-rdk

max_issues_repo_name: greglandrum/intake-rdkit

max_forks_repo_name: greglandrum/intake-rdkit


text:
import numpy as np
import cv2

# 定义旋转rotate函数
def rotate(img, angle, center=None, scale=1.0):
    # 获取图像尺寸
    (h, w) = img.shape[:2]

    # 旋转中心的缺失值为图像中心
    if center is None:
        center = (w / 2, h / 2)

    # 调用计算旋转矩阵函数
    M = cv2.getRotationMatrix2D(center, angle, scale)

    # 使用OpenCV仿射变换函数实现旋转操作
    rotated = cv2.warpAffine(img, M, (w, h))

    # 返回旋转后的图像
    return rotated

im = cv2.imread('me.jpg')
cv2.imshow("Orig", im)

# 对原图做旋转操作
# 逆时针45度
rotated = rotate(im, 45)
cv2.imshow("Rotate1", rotated)
# 顺时针20度
rotated = rotate(im, -20)
cv2.imshow("Rotate2", rotated)
# 逆时针90度
rotated = rotate(im, 90)
cv2.imshow("Rotate3", rotated)

cv2.waitKey()
cv2.destroyAllWindows()


text:
import scipy.ndimage

# Our numerical workhorses
import numpy as np
import pandas as pd

# Import the project utils
import sys
sys.path.insert(0, '../')
import NB_sortseq_utils as utils

# Import matplotlib stuff for plotting
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from IPython.core.pylabtools import figsize

# Seaborn, useful for graphics
import seaborn as sns

sns.set_palette("deep", color_codes=True)
utils.set_plotting_style1()

#===============================================================================
# Set output directory
#===============================================================================
output = 'output_figs/'

#======================================================#
# load sequences and convert to matrix representation
#======================================================#

mut_region_start = 0
mut_region_length = 70

data_fn = '../../data/sortseq_pymc_dump/20150513_marR_MG1655_LB_na_mut1_4bins_seq.csv'

f = open(data_fn)
roi_list = [(line.split(',')[0][mut_region_start:mut_region_start+mut_region_length], line.split(',')[1].strip()) for line in f if line.strip()]
f.close()

# We only want unqiue sequences
# read lines into one big list and transform into a set. This
# automatically gets rid of duplicate lines of sequences.
lines_unique = list(set(roi_list))
N = len(lines_unique)

# Convert each sequence into matrix representation
seq_mat = np.zeros((4,mut_region_length,N),dtype=int)

for i, line in enumerate(lines_unique):
    seq_mat[:,:,i] = utils.seq2mat(line[0])

#======================================================#
# We also need to know what the wild-type sequence was
# to calculate number of mutations. Lets load those in.
#======================================================#

data_fn_wt = 'input_data/seq_WT_marmut1.txt'

seq_mat_wt = np.zeros((4,mut_region_length,1),dtype=int)
seq_mat_wt_inv = np.zeros((4,mut_region_length,1),dtype=int)

f = open(data_fn_wt)
for i, line in enumerate(f):
    if line.strip():
        sb = line.split(',')
        seq_mat_wt[:,:,i] = utils.seq2mat(sb[0][0:mut_region_length])
f.close()

# To determine whether a matrix has a mutation, I will
# multiply (inner product) it by an inverted wild-type
# sequence (positions associated with wild-type sequence
# have value 0, and 1 otherwise). The sum of this matrix
# product will give the number of mutations.
# Let's generate this inverted wild-type matrix.
for i in range(0,mut_region_length):
    for j in range(0,4):
        if seq_mat_wt[j,i] ==1:
            seq_mat_wt_inv[j,i] = 0
        else:
            seq_mat_wt_inv[j,i] = 1

#======================================================#
# calculate 'mutation' matrices
#======================================================#

seq_mat_mut = np.zeros((4,mut_region_length,N),dtype=int)
for i in range(0,N):
    seq_mat_mut[:,:,i] = seq_mat[:,:,i] * seq_mat_wt_inv[:,:,0]

#======================================================#
# generate array to hold fraction of possible
# single, two-point, and three-point mutations.
#======================================================#

N_mut = np.zeros(3,dtype=int)
N_possible = [(3*mut_region_length),
             (3*mut_region_length)*(3*(mut_region_length-1)),
             (3*mut_region_length)*(3*(mut_region_length-1))*(3*(mut_region_length-2))]
print(N_possible)
#======================================================#
# Determine number of unique single point mutations
#======================================================#
seq_mat_mut_temp = seq_mat_mut.copy()
for i in range(0,N):
    # A matrix will sum to 1 if it has 1 mutations.
    # Use this to check number of sequences with 1 mutations
    if seq_mat_mut_temp[:,:,i].sum() != 1:
        seq_mat_mut_temp[:,:,i] = seq_mat_mut_temp[:,:,i]*0.0

# Calculate fraction of possible 1 point mutants
N_mut[0] = seq_mat_mut_temp.sum()
print(N_mut[0])
print(100*N_mut[0]/N_possible[0])

#======================================================#
# Determine number of unique two point mutations
#======================================================#
seq_mat_mut_temp = seq_mat_mut.copy()
for i in range(0,N):
    # A matrix will sum to 2 if it has 2 mutations.
    # Use this to check number of sequences with 2 mutations
    if seq_mat_mut_temp[:,:,i].sum() != 2:
        seq_mat_mut_temp[:,:,i] = seq_mat_mut_temp[:,:,i]*0.0

# Divide by number of mutations, 2 (sum of each matrix will equal 2)
N_mut[1] = seq_mat_mut_temp.sum()/2

#======================================================#
# Determine number of unique three point mutations
#======================================================#
seq_mat_mut_temp = seq_mat_mut.copy()
for i in range(0,N):
    # A matrix will sum to 3 if it has 3 mutations.
    # Use this to check number of sequences with 3 mutations
    if seq_mat_mut_temp[:,:,i].sum() != 3:
        seq_mat_mut_temp[:,:,i] = seq_mat_mut_temp[:,:,i]*0.0

# Divide by number of mutations, 3 (sum of each matrix will equal 3)
N_mut[2] = seq_mat_mut_temp.sum()/3

#======================================================#
# plot the result
#======================================================#
fig = plt.figure(1, figsize(5, 4))
ax = plt.subplot(111)             # the first subplot in the first figure
plt.bar(np.arange(1,4),100*N_mut/N_possible)#, width=0.015)
ax.set_xticks([1,2,3])
ax.set_ylabel('percent of possible\nmutations samples')
ax.set_xlabel('number of mutations\nper sequence')
ax.grid(b=False)

plt.tight_layout()

figname_out = output + 'figS3_percent_possible_mutations.pdf'
fig.savefig(figname_out, format='pdf')

reason: import numpy,import scipy


text:
import sys
import os.path

if __name__ == "__main__":

    sys.path.insert(1,
        os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))

import os

import random
import numpy as np
import networkx as nx
import pandas as pd

import argparse

import pickle as pkl

from evaluate.evaluation_utils import compute_scores, evaluate_rank_AUROC_AP, evaluate_mean_average_precision

from utils.io import load_data, load_embedding
from utils.sampling import sample_non_edges

def parse_args():

	parser = argparse.ArgumentParser(description='Load Embeddings and evaluate reconstruction')

	parser.add_argument("--graph", dest="graph", type=str,
		help="edgelist to load.")
	parser.add_argument("--features", dest="features", type=str,
		help="features to load.")
	parser.add_argument("--labels", dest="labels", type=str,
		help="path to labels")

	parser.add_argument('--directed', action="store_true", help='flag to train on directed graph')

	parser.add_argument("--embedding", dest="embedding_directory",
		help="directory of embedding to load.")

	parser.add_argument("--test-results-dir", dest="test_results_dir",
		help="path to save results.")

	parser.add_argument("--seed", type=int, default=0)

	parser.add_argument("--dist_fn", dest="dist_fn", type=str,
		choices=["poincare", "hyperboloid", "euclidean",
			"kle", "klh", "st"])

	return parser.parse_args()

def main():

	args = parse_args()

	test_results_dir = args.test_results_dir
	if not os.path.exists(test_results_dir):
		os.makedirs(test_results_dir, exist_ok=True)

	test_results_filename = os.path.join(test_results_dir,
		"{}.pkl".format(args.seed))

	# if check_complete(test_results_filename, args.seed):
	# 	return

	# test_results_lock_filename = os.path.join(test_results_dir,
	# 	"test_results.lock")
	# touch(test_results_lock_filename)

	args.directed = True

	graph, _, _ = load_data(args)
	# assert nx.is_directed(graph)
	print ("Loaded dataset")
	print ()

	if isinstance(graph, nx.DiGraph):
		graph = nx.adjacency_matrix(graph,
			nodelist=sorted(graph),
			weight=None).astype(bool)

	random.seed(args.seed)

	test_edges = list(zip(*graph.nonzero()))
	num_edges = len(test_edges)

	nodes = set(range(graph.shape[0]))
	del graph
	test_non_edges = sample_non_edges(
		nodes,
		set(test_edges),
		num_edges)

	test_edges = np.array(test_edges)
	test_non_edges = np.array(test_non_edges)

	print ("number of test edges:", len(test_edges))
	print ("number of test non edges:", len(test_non_edges))

	embedding = load_embedding(args.dist_fn,
		args.embedding_directory)

	test_results = dict()

	(mean_rank_recon, ap_recon,
		roc_recon) = evaluate_rank_AUROC_AP(
			embedding,
			test_edges,
			test_non_edges,
			args.dist_fn)

	test_results.update(
		{"mean_rank_recon": mean_rank_recon,
		"ap_recon": ap_recon,
		"roc_recon": roc_recon})

	map_recon, precisions_at_k = \
		evaluate_mean_average_precision(
		embedding,
		test_edges,
		args.dist_fn)
	test_results.update({"map_recon": map_recon})

	for k, pk in precisions_at_k.items():
		print ("precision at", k, pk)
	test_results.update({"p@{}".format(k): pk
		for k, pk in precisions_at_k.items()})

	print ("saving test results to {}".format(
		test_results_filename))

	test_results = pd.Series(test_results)
	with open(test_results_filename, "wb") as f:
		pkl.dump(test_results, f, pkl.HIGHEST_PROTOCOL)

	print ("done")

if __name__ == "__main__":
	main()

reason: import numpy,import networkx


text:
# -*- coding: utf-8 -*-

import math
import numpy as np
from .loader import tomatrix
from .models import Recommender

__all__ = ["distance", "KDE", "KDEModel"]

def distance(point_x, point_y):
    """distance between two point, unit is meter
        usage:
        >>> print distance((1.0, 0.0), (0.0, 0.0))
        111319.490793
    """
    EARTH_RADIUS = 6378137.0
    radlat1 = point_x[0] * math.pi / 180.0
    radlat2 = point_y[0] * math.pi / 180.0

    x = radlat1 - radlat2
    y = (point_x[1] - point_y[1]) * math.pi / 180.0
    c = math.sqrt((math.sin(x * 0.5) ** 2) +
        math.cos(radlat1) * math.cos(radlat2)* (math.sin(y * 0.5) ** 2))
    lenght = 2.0 * math.asin(c) * EARTH_RADIUS
    return math.fabs(lenght)

class KDE(object):
    """Estamate problity that a user show up in a poi, using KDE method
       usage:
        >>> cks = {0:[0]}
        >>> locations = {0:(0.0, 0.0), 1: (0.00014, 0.0), 2: (0.00028, 0.0)}
        >>> k = KDE(cks, locations)
        >>> print k.probility(0, 1) > k.probility(0, 2)
        True
        >>> print k.probility(0, 1)
        0.398893835041
    """
    def __init__(self, checkins, locations, smooth=1.0):
        """
        matrix : user checkin data sparse matrix
        locations: poi latitude and longitude
                   {"loc1": (20.0, 30.0), ...}
        """
        self.matrix = tomatrix(checkins)
        self.locations = locations
        if smooth <= 0.0:
            raise ValueError("smooth should > 0.0")
        self.smooth = smooth

    def probility(self, user, item):
        pois = set(np.nonzero(self.matrix[user])[1])
        if len(pois) == 0 or item in pois:
            return 1.0

        sum_prob = 0.0
        for poi in pois:
            loc_x = self.locations[poi]
            loc_y = self.locations[item]
            _dis = distance(loc_x, loc_y) / 1000.0 # to kilometer
            x = _dis / self.smooth
            prob = math.pow(math.e, -0.5 * math.pow(x, 2))
            sum_prob += prob
        return sum_prob / (math.sqrt(2.0 * math.pi) * self.smooth * len(pois))

class KDEModel(Recommender):
    def __init__(self, checkins, locations, smooth=1.0):
        super(KDEModel, self).__init__(checkins)
        self.kde = KDE(checkins, locations, smooth)

    def predict(self, user, item):
        return self.kde.probility(user, item)

max_stars_repo_name: jchluo/poi

max_issues_repo_name: jchluo/poi

max_forks_repo_name: jchluo/poi


text:
from typing import Callable
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers.schedules import *
import segmentation_models as sm
from custom_objects.custom_metrics import CustomMeanIOU
from tensorflow.keras.models import Model
import numpy as np
import tensorflow as tf
from config import config as cfg

SM_FRAMEWORK=tf.keras

def unet_backbone_resnet34_bce_jaccard_loss():
    unet_model = sm.Unet('resnet34', classes=cfg['classes'], activation='softmax',input_shape=cfg['input_shape'], encoder_weights=None)

    unet_model.compile(
        'Adam',
        loss=sm.losses.bce_jaccard_loss,
        metrics=[CustomMeanIOU(cfg['classes'], dtype=np.float32),sm.metrics.f1_score],
    )
    return unet_model


text:
# --------------------------------------------------------
# Deep Iterative Matching Network
# Licensed under The Apache-2.0 License [see LICENSE for details]
# Written by Gu Wang
# --------------------------------------------------------
# generate rendered data with the poses generated in last step
from __future__ import print_function, division

import sys
import os

cur_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, os.path.join(cur_dir, ".."))
from lib.render_glumpy.render_py import Render_Py
import numpy as np
from lib.utils.mkdir_if_missing import mkdir_if_missing
from lib.pair_matching import RT_transform
import cv2
from tqdm import tqdm

idx2class = {
    1: "ape",
    2: "benchvise",
    # 3: 'bowl',
    4: "camera",
    5: "can",
    6: "cat",
    # 7: 'cup',
    8: "driller",
    9: "duck",
    10: "eggbox",
    11: "glue",
    12: "holepuncher",
    13: "iron",
    14: "lamp",
    15: "phone",
}
# config for render machine
width = 640
height = 480
K = np.array([[572.4114, 0, 325.2611], [0, 573.57043, 242.04899], [0, 0, 1]])
ZNEAR = 0.25
ZFAR = 6.0
depth_factor = 1000

LM6d_root = os.path.join(cur_dir, "../data/LINEMOD_6D/LM6d_converted/LM6d_refine")
observed_set_root = os.path.join(LM6d_root, "image_set/observed")
rendered_pose_path = "%s/LM6d_{}_rendered_pose_{}.txt" % (os.path.join(LM6d_root, "rendered_poses"))

# output_path
rendered_root_dir = os.path.join(LM6d_root, "data/rendered")
pair_set_dir = os.path.join(LM6d_root, "image_set")
mkdir_if_missing(rendered_root_dir)
mkdir_if_missing(pair_set_dir)
print("target path: {}".format(rendered_root_dir))
print("target path: {}".format(pair_set_dir))

def main():
    gen_images = True
    for class_idx, class_name in idx2class.items():
        train_pair = []
        val_pair = []
        print("start ", class_idx, class_name)
        if class_name in ["__back_ground__"]:
            continue
        # uncomment here to only generate data for ape
        # if class_name not in ['ape']:
        #     continue

        if gen_images:
            # init render
            model_dir = os.path.join(LM6d_root, "models", class_name)
            render_machine = Render_Py(model_dir, K, width, height, ZNEAR, ZFAR)

        for set_type in ["all"]:
            with open(os.path.join(observed_set_root, "{}_{}.txt".format(class_name, "all")), "r") as f:
                all_observed_list = [x.strip() for x in f.readlines()]
            # with open(
            #         os.path.join(observed_set_root, '{}_{}.txt'.format(
            #             class_name, 'train')), 'r') as f:
            #     train_observed_list = [x.strip() for x in f.readlines()]
            with open(os.path.join(observed_set_root, "{}_{}.txt".format(class_name, "test")), "r") as f:
                test_observed_list = [x.strip() for x in f.readlines()]

            with open(rendered_pose_path.format(set_type, class_name)) as f:
                str_rendered_pose_list = [x.strip().split(" ") for x in f.readlines()]
            rendered_pose_list = np.array([[float(x) for x in each_pose] for each_pose in str_rendered_pose_list])
            rendered_per_observed = 10
            assert len(rendered_pose_list) == 10 * len(all_observed_list), "{} vs {}".format(
                len(rendered_pose_list), len(all_observed_list)
            )
            for idx, observed_index in enumerate(tqdm(all_observed_list)):
                video_name, observed_prefix = observed_index.split("/")
                rendered_dir = os.path.join(rendered_root_dir, class_name)
                mkdir_if_missing(rendered_dir)
                for inner_idx in range(rendered_per_observed):
                    if gen_images:
                        # if gen_images and observed_index in test_observed_list and inner_idx == 0: # only generate my_val_v{}  # noqa:E501
                        image_file = os.path.join(rendered_dir, "{}_{}-color.png".format(observed_prefix, inner_idx))
                        depth_file = os.path.join(rendered_dir, "{}_{}-depth.png".format(observed_prefix, inner_idx))
                        rendered_idx = idx * rendered_per_observed + inner_idx
                        pose_rendered_q = rendered_pose_list[rendered_idx]

                        rgb_gl, depth_gl = render_machine.render(pose_rendered_q[:4], pose_rendered_q[4:])
                        rgb_gl = rgb_gl.astype("uint8")

                        depth_gl = (depth_gl * depth_factor).astype(np.uint16)

                        cv2.imwrite(image_file, rgb_gl)
                        cv2.imwrite(depth_file, depth_gl)

                        pose_rendered_file = os.path.join(
                            rendered_dir, "{}_{}-pose.txt".format(observed_prefix, inner_idx)
                        )
                        text_file = open(pose_rendered_file, "w")
                        text_file.write("{}\n".format(class_idx))
                        pose_rendered_m = np.zeros((3, 4))
                        pose_rendered_m[:, :3] = RT_transform.quat2mat(pose_rendered_q[:4])
                        pose_rendered_m[:, 3] = pose_rendered_q[4:]
                        pose_ori_m = pose_rendered_m
                        pose_str = "{} {} {} {}\n{} {} {} {}\n{} {} {} {}".format(
                            pose_ori_m[0, 0],
                            pose_ori_m[0, 1],
                            pose_ori_m[0, 2],
                            pose_ori_m[0, 3],
                            pose_ori_m[1, 0],
                            pose_ori_m[1, 1],
                            pose_ori_m[1, 2],
                            pose_ori_m[1, 3],
                            pose_ori_m[2, 0],
                            pose_ori_m[2, 1],
                            pose_ori_m[2, 2],
                            pose_ori_m[2, 3],
                        )
                        text_file.write(pose_str)

                    if observed_index in test_observed_list:
                        if inner_idx == 0:
                            val_pair.append(
                                "{} {}/{}_{}".format(observed_index, class_name, observed_prefix, inner_idx)
                            )
                    else:
                        train_pair.append("{} {}/{}_{}".format(observed_index, class_name, observed_prefix, inner_idx))

            train_pair_set_file = os.path.join(pair_set_dir, "train_{}.txt".format(class_name))
            train_pair = sorted(train_pair)
            with open(train_pair_set_file, "w") as text_file:
                for x in train_pair:
                    text_file.write("{}\n".format(x))

            test_pair_set_file = os.path.join(pair_set_dir, "my_val_{}.txt".format(class_name))
            val_pair = sorted(val_pair)
            with open(test_pair_set_file, "w") as text_file:
                for x in val_pair:
                    text_file.write("{}\n".format(x))
        print(class_name, " done")

def check_observed_rendered():
    from lib.utils.utils import read_img
    import matplotlib.pyplot as plt

    observed_dir = os.path.join(LM6d_root, "data/observed")

    for class_idx, class_name in idx2class.items():
        if class_name != "duck":
            continue
        print(class_name)
        observed_list_path = os.path.join(observed_set_root, "{}_train.txt".format(class_name))
        with open(observed_list_path, "r") as f:
            observed_list = [x.strip() for x in f.readlines()]
        for idx, observed_index in enumerate(observed_list):
            print(observed_index)
            prefix = observed_index.split("/")[1]
            color_observed = read_img(os.path.join(observed_dir, observed_index + "-color.png"), 3)
            color_rendered = read_img(os.path.join(rendered_root_dir, class_name, prefix + "_0-color.png"), 3)
            fig = plt.figure()  # noqa:F401
            plt.axis("off")
            plt.subplot(1, 2, 1)
            plt.imshow(color_observed[:, :, [2, 1, 0]])
            plt.axis("off")

            plt.subplot(1, 2, 2)
            plt.imshow(color_rendered[:, :, [2, 1, 0]])
            plt.axis("off")

            plt.show()

if __name__ == "__main__":
    main()
    # check_observed_rendered()
    print("{} finished".format(__file__))

max_issues_repo_name: greatwallet/mx-DeepIM

max_forks_repo_name: greatwallet/mx-DeepIM


text:
# -*- coding: utf-8 -*-
__author__ = 'Brice Olivier'

import mne
import os
import scipy.io
from sea import SynchronizedEEGTrial
from sea.config import DATA_PATH
import pandas as pd
import pytest

@pytest.fixture(scope="module")
def synchronized_eeg_trial_init():
    eeg_data = scipy.io.loadmat(os.path.join(DATA_PATH, 's01_sample.mat'),
                                squeeze_me=True, struct_as_record=False)['EEG']
    em_data = pd.read_csv(os.path.join(DATA_PATH, 's01_sample.csv'))
    channel_info = mne.io.read_epochs_eeglab(os.path.join(DATA_PATH, 'synchro_s01_test.set'), verbose='CRITICAL').info
    return SynchronizedEEGTrial(eeg_data, em_data, 's01', 'chasse_oiseaux-f1', channel_info)

def test_synchronized_eeg_new(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.subject_name == 's01'
    assert synchronized_eeg_trial_init.text_name == 'chasse_oiseaux-f1'

def test_get_channel_id(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_channel_id('Fp1') == 0

def test_get_time_index(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_time_index(0) == 188

def test_get_fixations_event_id(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.em_trial.shape[0] == len(synchronized_eeg_trial_init.get_fixations_event_id())

def test_get_first_fixation_time(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_first_fixation_time() == 194

def test_get_last_fixation_time(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_last_fixation_time() == 4365

def test_get_first_fixation_time_id(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_first_fixation_time_id() == 382

def test_get_last_fixation_time_id(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_last_fixation_time_id() == 4553

def test_get_em_epoch_start_time(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_em_epoch_start_time() == 194

def test_get_em_epoch_end_time(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.get_em_epoch_end_time() == 4365

def test_is_eeg_epoch_truncated(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.is_eeg_epoch_truncated() == False

def test_get_fixations_time(synchronized_eeg_trial_init):
    assert len(synchronized_eeg_trial_init.get_fixations_time()) == synchronized_eeg_trial_init.em_trial.shape[0]

def test_get_fixed_words(synchronized_eeg_trial_init):
    assert len(synchronized_eeg_trial_init.get_fixed_words()) == synchronized_eeg_trial_init.em_trial.shape[0]

def test_plot_activity(synchronized_eeg_trial_init):
    synchronized_eeg_trial_init.plot_activity('Fp1')

def test_compute_epoch_phases(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.compute_epoch_phases() == [(194, 889, 0), (889, 2213, 1), (2213, 4365, 3)]
    assert synchronized_eeg_trial_init.compute_epoch_phases(from_zero=True) == [(0, 695, 0), (695, 2019, 1), (2019, 4171, 3)]

def test_compute_modwt(synchronized_eeg_trial_init):
    assert synchronized_eeg_trial_init.compute_modwt().shape == (875910, 7)
    assert synchronized_eeg_trial_init.compute_modwt(standardize_trial=0).shape == (875910, 7)
    assert synchronized_eeg_trial_init.compute_modwt(standardize_trial=1).shape == (875910, 7)
    assert synchronized_eeg_trial_init.compute_modwt(standardize_trial=2).shape == (875910, 7)
    assert synchronized_eeg_trial_init.compute_modwt(standardize_trial=3).shape == (875910, 7)

max_stars_repo_name: PyENE/eega

max_issues_repo_name: PyENE/eega

max_forks_repo_name: PyENE/eega

reason: import scipy


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

def kai_distribution (n):
    x = np.zeros(1000)
    for i in range(n):
        x = x + [k ** 2 for k in np.random.normal(0, 1, 1000)]
    return x

def t_distribution (n):
    x = np.random.normal(0, 1, 1000)
    y = kai_distribution(n)
    x = x / np.sqrt(y/2)
    return x

def large_num_thm (n, distribution):
    plt_xlist = []
    plt_ylist = []
    for k in range(n):
        k = k * 10
        x_list = np.random.choice(distribution, (k+1), replace=True)
        xbar = 0.
        for i in range(k):
            xbar = xbar + x_list[i]
        xbar = xbar / (k+1)
        plt_xlist.append(k+1)
        plt_ylist.append(xbar)
    plt.figure()
    plt.plot(plt_xlist,plt_ylist)
    plt.savefig("large_num_thm_t.png")

def central_limit_thm (k,n,distribution,mu,sigma):
    plt_xlist = []
    for j in range(k):
        xbar = 0.
        x_list = np.random.choice(distribution, n, replace=True)
        for i in range(n):
            xbar = xbar + x_list[i]
        xbar = xbar / n
        z = (xbar - mu) / (np.sqrt(sigma)/np.sqrt(n))
        plt_xlist.append(z)
    gauss_list = np.random.normal(0, 1, k)
    plt.figure()
    plt.hist(plt_xlist, range=(-4, 4),bins=50,color='blue', alpha=0.5)
    plt.hist(gauss_list,range=(-4, 4),bins=50,color='red', alpha=0.5)
    plt.savefig("central_lim_thm_t.png")

if __name__ == "__main__" :
    large_num_thm (10000,t_distribution(2))
    central_limit_thm(1000000,100,t_distribution(2),0,1)

max_stars_repo_name: yoshi-ki/BACHELOR

max_issues_repo_name: yoshi-ki/BACHELOR

max_forks_repo_name: yoshi-ki/BACHELOR

reason: import numpy,from scipy


text:
from collections import defaultdict
import numpy as np
import scipy.signal
import mne
from scipy.stats import kurtosis
from mne.preprocessing.bads import _find_outliers
from mne.utils import logger
from mne.io.pick import pick_info, _picks_by_type

def _bad_mask_to_names(info, bad_mask):
    """Remap mask to ch names"""
    bad_idx = [np.where(m)[0] for m in bad_mask]
    return [[info['ch_names'][k] for k in epoch] for epoch in bad_idx]

def _combine_indices(bads):
    """summarize indices"""
    return list(set(v for val in bads.values() if len(val) > 0 for v in val))

def hurst(x):
    """Estimate Hurst exponent on a timeseries.

    The estimation is based on the second order discrete derivative.

    Parameters
    ----------
    x : 1D numpy array
        The timeseries to estimate the Hurst exponent for.

    Returns
    -------
    h : float
        The estimation of the Hurst exponent for the given timeseries.
    """
    y = np.cumsum(np.diff(x, axis=1), axis=1)

    b1 = [1, -2, 1]
    b2 = [1, 0, -2, 0, 1]

    # second order derivative
    y1 = scipy.signal.lfilter(b1, 1, y, axis=1)
    y1 = y1[:, len(b1) - 1:-1]  # first values contain filter artifacts

    # wider second order derivative
    y2 = scipy.signal.lfilter(b2, 1, y, axis=1)
    y2 = y2[:, len(b2) - 1:-1]  # first values contain filter artifacts

    s1 = np.mean(y1 ** 2, axis=1)
    s2 = np.mean(y2 ** 2, axis=1)

    return 0.5 * np.log2(s2 / s1)

def _efficient_welch(data, sfreq):
    """Calls scipy.signal.welch with parameters optimized for greatest speed
    at the expense of precision. The window is set to ~10 seconds and windows
    are non-overlapping.
    Parameters
    ----------
    data : array, shape (..., n_samples)
        The timeseries to estimate signal power for. The last dimension
        is assumed to be time.
    sfreq : float
        The sample rate of the timeseries.
    Returns
    -------
    fs : array of float
        The frequencies for which the power spectra was calculated.
    ps : array, shape (..., frequencies)
        The power spectra for each timeseries.
    """
    from scipy.signal import welch
    nperseg = min(data.shape[-1],
                  2 ** int(np.log2(10 * sfreq) + 1))  # next power of 2

    return welch(data, sfreq, nperseg=nperseg, noverlap=0, axis=-1)

def _freqs_power(data, sfreq, freqs):
    fs, ps = _efficient_welch(data, sfreq)
    try:
        return np.sum([ps[..., np.searchsorted(fs, f)] for f in freqs], axis=0)
    except IndexError:
        raise ValueError(
            ("Insufficient sample rate to  estimate power at {} Hz for line "
             "noise detection. Use the 'metrics' parameter to disable the "
             "'line_noise' metric.").format(freqs))

def _distance_correction(info, picks, x):
    """Remove the effect of distance to reference sensor.

    Computes the distance of each sensor to the reference sensor. Then
    regresses the effect of this distance out of the values in x.

    Parameters
    ----------
    info : instance of Info
        The measurement info. This should contain positions for all the
        sensors.
    picks : list of int
        Indices of the channels that correspond to the values in x.
    x : list of float
        Values to correct.

    Returns
    -------
    x_corr : list of float
        values in x corrected for the distance to reference sensor.
    """
    pos = np.array([info['chs'][ch]['loc'][:3] for ch in picks])
    ref_pos = np.array([info['chs'][ch]['loc'][3:6] for ch in picks])

    if np.any(np.all(pos == 0, axis=1)):
        raise ValueError('Cannot perform correction for distance to reference '
                         'sensor: not all selected channels have position '
                         'information.')
    if np.any(np.all(ref_pos == 0, axis=1)):
        raise ValueError('Cannot perform correction for distance to reference '
                         'sensor: the location of the reference sensor is not '
                         'specified for all selected channels.')

    # Compute angular distances to the reference sensor
    pos /= np.linalg.norm(pos, axis=1)[:, np.newaxis]
    ref_pos /= np.linalg.norm(ref_pos, axis=1)[:, np.newaxis]
    angles = [np.arccos(np.dot(a, b)) for a, b in zip(pos, ref_pos)]

    # Fit a quadratic curve to correct for the angular distance
    fit = np.polyfit(angles, x, 2)
    return x - np.polyval(fit, angles)

def find_bad_channels(epochs, picks=None, max_iter=1, thres=3,
                      eeg_ref_corr=False, use_metrics=None,
                      return_by_metric=False):
    """Automatically find and mark bad channels.

    Implements the first step of the FASTER algorithm.

    This function attempts to automatically mark bad EEG channels by performing
    outlier detection. It operated on epoched data, to make sure only relevant
    data is analyzed.

    Parameters
    ----------
    epochs : Instance of Epochs
        The epochs for which bad channels need to be marked
    picks : list of int | None
        Channels to operate on. Defaults to EEG channels.
    thres : float
        The threshold value, in standard deviations, to apply. A channel
        crossing this threshold value is marked as bad. Defaults to 3.
    max_iter : int
        The maximum number of iterations performed during outlier detection
        (defaults to 1, as in the original FASTER paper).
    eeg_ref_corr : bool
        If the EEG data has been referenced using a single electrode setting
        this parameter to True will enable a correction factor for the distance
        of each electrode to the reference. If an average reference is applied,
        or the mean of multiple reference electrodes, set this parameter to
        False. Defaults to False, which disables the correction.
    use_metrics : list of str
        List of metrics to use. Can be any combination of:
            'variance', 'correlation', 'hurst', 'kurtosis', 'line_noise'
        Defaults to all of them.
    return_by_metric : bool
        Whether to return the bad channels as a flat list (False, default) or
        as a dictionary with the names of the used metrics as keys and the
        bad channels found by this metric as values.

    Returns
    -------
    bads : list of str
        The names of the bad EEG channels.
    """
    metrics = {
        'variance': lambda x: np.var(x, axis=1),
        'correlation': lambda x: np.nanmean(
            np.ma.masked_array(
                np.corrcoef(x),
                np.identity(len(x), dtype=bool)
            ),
            axis=0),
        'hurst': lambda x: hurst(x),
        'kurtosis': lambda x: kurtosis(x, axis=1),
        'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'],
                                             [50, 60]),
    }

    if picks is None:
        picks = mne.pick_types(epochs.info, meg=False, eeg=True, exclude=[])
    if use_metrics is None:
        use_metrics = metrics.keys()

    # Concatenate epochs in time
    data = epochs.get_data()[:, picks]
    data = data.transpose(1, 0, 2).reshape(data.shape[1], -1)

    # Find bad channels
    bads = defaultdict(list)
    info = pick_info(epochs.info, picks, copy=True)
    for ch_type, chs in _picks_by_type(info):
        logger.info('Bad channel detection on %s channels:' % ch_type.upper())
        for metric in use_metrics:
            scores = metrics[metric](data[chs])
            if eeg_ref_corr:
                scores = _distance_correction(epochs.info, picks, scores)
            bad_channels = [epochs.ch_names[picks[chs[i]]]
                            for i in _find_outliers(scores, thres, max_iter)]
            logger.info('\tBad by %s: %s' % (metric, bad_channels))
            bads[metric].append(bad_channels)

    bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items())

    if return_by_metric:
        return bads
    else:
        return _combine_indices(bads)

def _deviation(data):
    """Computes the deviation from mean for each channel in a set of epochs.

    This is not implemented as a lambda function, because the channel means
    should be cached during the computation.

    Parameters
    ----------
    data : 3D numpy array
        The epochs (#epochs x #channels x #samples).

    Returns
    -------
    dev : list of float
        For each epoch, the mean deviation of the channels.
    """
    ch_mean = np.mean(data, axis=2)
    return ch_mean - np.mean(ch_mean, axis=0)

def find_bad_epochs(epochs, picks=None, thres=3, max_iter=1, use_metrics=None,
                    return_by_metric=False):
    """Automatically find and mark bad epochs.

    Implements the second step of the FASTER algorithm.

    This function attempts to automatically mark bad epochs by performing
    outlier detection.

    Parameters
    ----------
    epochs : Instance of Epochs
        The epochs to analyze.
    picks : list of int | None
        Channels to operate on. Defaults to EEG channels.
    thres : float
        The threshold value, in standard deviations, to apply. An epoch
        crossing this threshold value is marked as bad. Defaults to 3.
    max_iter : int
        The maximum number of iterations performed during outlier detection
        (defaults to 1, as in the original FASTER paper).
    use_metrics : list of str
        List of metrics to use. Can be any combination of:
            'amplitude', 'variance', 'deviation'
        Defaults to all of them.
    return_by_metric : bool
        Whether to return the bad channels as a flat list (False, default) or
        as a dictionary with the names of the used metrics as keys and the
        bad channels found by this metric as values.

    Returns
    -------
    bads : list of int
        The indices of the bad epochs.
    """

    metrics = {
        'amplitude': lambda x: np.mean(np.ptp(x, axis=2), axis=1),
        'deviation': lambda x: np.mean(_deviation(x), axis=1),
        'variance': lambda x: np.mean(np.var(x, axis=2), axis=1),
    }

    if picks is None:
        picks = mne.pick_types(epochs.info, meg=False, eeg=True,
                               exclude='bads')
    if use_metrics is None:
        use_metrics = metrics.keys()

    info = pick_info(epochs.info, picks, copy=True)
    data = epochs.get_data()[:, picks]

    bads = defaultdict(list)
    for ch_type, chs in _picks_by_type(info):
        logger.info('Bad epoch detection on %s channels:' % ch_type.upper())
        for metric in use_metrics:
            scores = metrics[metric](data[:, chs])
            bad_epochs = _find_outliers(scores, thres, max_iter)
            logger.info('\tBad by %s: %s' % (metric, bad_epochs))
            bads[metric].append(bad_epochs)

    bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items())
    if return_by_metric:
        return bads
    else:
        return _combine_indices(bads)

def _power_gradient(data, sfreq, prange):
    """Estimate the gradient of the power spectrum at upper frequencies.

    Parameters
    ----------
    data : array, shape (n_components, n_samples)
        The timeseries to estimate signal power for. The last dimension
        is presumed to be time.
    sfreq : float
        The sample rate of the timeseries.
    prange : pair of floats
        The (lower, upper) frequency limits of the power spectrum to use. In
        the FASTER paper, they set these to the passband of the lowpass filter.

    Returns
    -------
    grad : array of float
        The gradients of the timeseries.
    """
    fs, ps = _efficient_welch(data, sfreq)

    # Limit power spectrum to selected frequencies
    start, stop = (np.searchsorted(fs, p) for p in prange)
    if start >= ps.shape[1]:
        raise ValueError(("Sample rate insufficient to estimate {} Hz power. "
                          "Use the 'power_gradient_range' parameter to tweak "
                          "the tested frequencies for this metric or use the "
                          "'metrics' parameter to disable the "
                          "'power_gradient' metric.").format(prange[0]))
    ps = ps[:, start:stop]

    # Compute mean gradients
    return np.mean(np.diff(ps), axis=1)

def find_bad_components(ica, epochs, thres=3, max_iter=1, use_metrics=None,
                        prange=None, return_by_metric=False):
    """Implements the third step of the FASTER algorithm.

    This function attempts to automatically mark bad ICA components by
    performing outlier detection.

    Parameters
    ----------
    ica : Instance of ICA
        The ICA operator, already fitted to the supplied Epochs object.
    epochs : Instance of Epochs
        The untransformed epochs to analyze.
    thres : float
        The threshold value, in standard deviations, to apply. A component
        crossing this threshold value is marked as bad. Defaults to 3.
    max_iter : int
        The maximum number of iterations performed during outlier detection
        (defaults to 1, as in the original FASTER paper).
    use_metrics : list of str
        List of metrics to use. Can be any combination of:
            'eog_correlation', 'kurtosis', 'power_gradient', 'hurst',
            'median_gradient'
        Defaults to all of them.
    prange : None | pair of floats
        The (lower, upper) frequency limits of the power spectrum to use for
        the power gradient computation. In the FASTER paper, they set these to
        the passband of the highpass and lowpass filter. If None, defaults to
        the 'highpass' and 'lowpass' filter settings in ica.info.
    return_by_metric : bool
        Whether to return the bad channels as a flat list (False, default) or
        as a dictionary with the names of the used metrics as keys and the
        bad channels found by this metric as values.

    Returns
    -------
    bads : list of int
        The indices of the bad components.

    See also
    --------
    ICA.find_bads_ecg
    ICA.find_bads_eog
    """
    source_data = ica.get_sources(epochs).get_data().transpose(1, 0, 2)
    source_data = source_data.reshape(source_data.shape[0], -1)

    if prange is None:
        prange = (ica.info['highpass'], ica.info['lowpass'])
    if len(prange) != 2:
        raise ValueError('prange must be a pair of floats')

    metrics = {
        'eog_correlation': lambda x: x.find_bads_eog(epochs)[1],
        'kurtosis': lambda x: kurtosis(
            np.dot(
                x.mixing_matrix_.T,
                x.pca_components_[:x.n_components_]),
            axis=1),
        'power_gradient': lambda x: _power_gradient(source_data,
                                                    ica.info['sfreq'],
                                                    prange),
        'hurst': lambda x: hurst(source_data),
        'median_gradient': lambda x: np.median(np.abs(np.diff(source_data)),
                                               axis=1),
        'line_noise': lambda x: _freqs_power(source_data,
                                             epochs.info['sfreq'], [50, 60]),
    }

    if use_metrics is None:
        use_metrics = metrics.keys()

    bads = defaultdict(list)
    for metric in use_metrics:
        scores = np.atleast_2d(metrics[metric](ica))
        for s in scores:
            bad_comps = _find_outliers(s, thres, max_iter)
            logger.info('Bad by %s:\n\t%s' % (metric, bad_comps))
            bads[metric].append(bad_comps)

    bads = dict((k, np.concatenate(v).tolist()) for k, v in bads.items())
    if return_by_metric:
        return bads
    else:
        return _combine_indices(bads)

def find_bad_channels_in_epochs(epochs, picks=None, thres=3, max_iter=1,
                                eeg_ref_corr=False, use_metrics=None,
                                return_by_metric=False):
    """Implements the fourth step of the FASTER algorithm.

    This function attempts to automatically mark bad channels in each epochs by
    performing outlier detection.

    Parameters
    ----------
    epochs : Instance of Epochs
        The epochs to analyze.
    picks : list of int | None
        Channels to operate on. Defaults to EEG channels.
    thres : float
        The threshold value, in standard deviations, to apply. An epoch
        crossing this threshold value is marked as bad. Defaults to 3.
    max_iter : int
        The maximum number of iterations performed during outlier detection
        (defaults to 1, as in the original FASTER paper).
    eeg_ref_corr : bool
        If the EEG data has been referenced using a single electrode setting
        this parameter to True will enable a correction factor for the distance
        of each electrode to the reference. If an average reference is applied,
        or the mean of multiple reference electrodes, set this parameter to
        False. Defaults to False, which disables the correction.
    use_metrics : list of str
        List of metrics to use. Can be any combination of:
            'amplitude', 'variance', 'deviation', 'median_gradient'
        Defaults to all of them.
    return_by_metric : bool
        Whether to return the bad channels as a flat list (False, default) or
        as a dictionary with the names of the used metrics as keys and the
        bad channels found by this metric as values.

    Returns
    -------
    bads : list of lists of int
        For each epoch, the indices of the bad channels.
    """

    metrics = {
        'amplitude': lambda x: np.ptp(x, axis=2),
        'deviation': lambda x: _deviation(x),
        'variance': lambda x: np.var(x, axis=2),
        'median_gradient': lambda x: np.median(np.abs(np.diff(x)), axis=2),
        'line_noise': lambda x: _freqs_power(x, epochs.info['sfreq'],
                                             [50, 60]),
    }

    if picks is None:
        picks = mne.pick_types(epochs.info, meg=False, eeg=True,
                               exclude='bads')
    if use_metrics is None:
        use_metrics = metrics.keys()

    info = pick_info(epochs.info, picks, copy=True)
    data = epochs.get_data()[:, picks]
    bads = dict((m, np.zeros((len(data), len(picks)), dtype=bool)) for
                m in metrics)
    for ch_type, chs in _picks_by_type(info):
        ch_names = [info['ch_names'][k] for k in chs]
        chs = np.array(chs)
        for metric in use_metrics:
            logger.info('Bad channel-in-epoch detection on %s channels:'
                        % ch_type.upper())
            s_epochs = metrics[metric](data[:, chs])
            for i_epochs, scores in enumerate(s_epochs):
                if eeg_ref_corr:
                    scores = _distance_correction(epochs.info, picks, scores)
                outliers = _find_outliers(scores, thres, max_iter)
                if len(outliers) > 0:
                    bad_segment = [ch_names[k] for k in outliers]
                    logger.info('Epoch %d, Bad by %s:\n\t%s' % (
                        i_epochs, metric, bad_segment))
                    bads[metric][i_epochs, chs[outliers]] = True

    info = pick_info(epochs.info, picks, copy=True)
    if return_by_metric:
        bads = dict((m, _bad_mask_to_names(info, v)) for m, v in bads.items())
    else:
        bads = np.sum(list(bads.values()), axis=0).astype(bool)
        bads = _bad_mask_to_names(info, bads)

    return bads

def run_faster(epochs, thres=3, copy=True):
    """Run the entire FASTER pipeline on the data.
    """
    if copy:
        epochs = epochs.copy()

    # Step one
    logger.info('Step 1: mark bad channels')
    epochs.info['bads'] += find_bad_channels(epochs, thres=5)

    # Step two
    logger.info('Step 2: mark bad epochs')
    bad_epochs = find_bad_epochs(epochs, thres=thres)
    good_epochs = list(set(range(len(epochs))).difference(set(bad_epochs)))
    epochs = epochs[good_epochs]

    # Step three (using the build-in MNE functionality for this)
    logger.info('Step 3: mark bad ICA components')
    picks = mne.pick_types(epochs.info, meg=False, eeg=True, eog=True,
                           exclude='bads')
    ica = mne.preprocessing.run_ica(epochs, len(picks), picks=picks,
                                    eog_ch=['vEOG', 'hEOG'])
    ica.apply(epochs)

    # Step four
    logger.info('Step 4: mark bad channels for each epoch')
    bad_channels_per_epoch = find_bad_channels_in_epochs(epochs, thres=thres)
    for i, b in enumerate(bad_channels_per_epoch):
        if len(b) > 0:
            epoch = epochs[i]
            epoch.info['bads'] += b
            epoch.interpolate_bads_eeg()
            epochs._data[i, :, :] = epoch._data[0, :, :]

    # Now that the data is clean, apply average reference
    epochs.info['custom_ref_applied'] = False
    epochs, _ = mne.io.set_eeg_reference(epochs)
    epochs.apply_proj()

    # That's all for now
    return epochs

max_stars_repo_name: jdkoen/mne-faster

max_issues_repo_name: jdkoen/mne-faster

max_forks_repo_name: jdkoen/mne-faster

reason: import numpy,import scipy,from scipy


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 09:03:53 2019

Reading in csv transmission network data and saving to Python Data Structure
And enable manipulation of this info in python

Includes calculations of different properties, plus test case showing it works

@author: marianne aspbury
"""

## some tools for later
from IndividualClass import Individual
import numpy as np
import csv

'''
# Using this class to store information on individuals
class Individual:
    def __init__(self):
        self.ID = None
        self.infected_others = []
        self.infected_by = []
        self.time_infected_others =[]
        self.time_infected_by = []
        self.age_birth = -1
        self.age_death = -1
        self.num_partners = -1
        self.direct_children_num = 0
        self.total_children_num = 0
        '''

## Some tests to understand functionality
#my_dict = {}
#print(my_dict)
#ID_1 = 2135
#
#if ID_1 in my_dict:
#    print(my_dict)
#else:
#    my_dict[str(ID_1)] = Individual()
#
#print(my_dict[str(ID_1)].ID)
#
#my_dict[str(ID_1)].ID = ID_1
#print(my_dict[str(ID_1)].ID)
#
#indiv1 = Individual()
#indiv1.ID = 2135
#indiv1.infected_by.append(2134)
#
#print(indiv1)
#print(indiv1.ID)
#print(indiv1.infected_by)

### File locations to read in files
file_loc = 'C:\\Users\\mazmysta\OneDrive - Nexus365\\BDI_proj\\HIV_Transmission_Networks_WillProbert\\19-08-08-first_example_network\\'
individual_metadata_file = 'phylogenetic_individualdata_all_100314_CL01_Za_B_V1.2_patchall_Rand10_Run1_PCseed0_0.csv'
transmission_datafile = 'phylogenetic_transmission_all_100314_CL01_Za_B_V1.2_patchall_Rand10_Run1_PCseed0_0.csv'

### reading in IDs of all Individuals and creating class instances, saving to dictionary for access
my_dict={}
with open(file_loc + individual_metadata_file, 'r') as f:
     reader = csv.reader(f, delimiter=',')
#     for row in islice(reader,2000,25000):
     for row in reader:
#         if firstline:
#             firstline = False
#             print(row)
#             continue
         if row[6] == '1': ## We are only building a list of HIV infected individuals
#             print('yes')
             my_dict[row[1]] = Individual()
             my_dict[row[1]].ID = row[1]
             my_dict[row[1]].age_birth = row[4]
             my_dict[row[1]].age_death = row[5]
#             print(row[1])
#             print(my_dict[row[1]].__dict__)

#example class instance from dictionary
print(my_dict['2369_0'].__dict__)

## Find who 's infected who and store infected by & infected from info.
with open(file_loc + transmission_datafile, 'r') as f:
     reader = csv.reader(f, delimiter=',')
     for row in reader:
         if row[2] in my_dict: # Want to see if infector ID is in the dictionary
#             print('Found Infected individual:', row[2], '. Infector:', row[3])
#             print('Time of infection:', row[6])
#             print('Num of infector partners:', row[-1])
             my_dict[row[2]].infected_by.append(row[3])
             my_dict[row[2]].time_infected_by.append(row[6])
             if row[3] in my_dict:
#                 my_dict[row[3]].ID = row[3]
                 pass
             else:
                 print('extra is', row[3]) ## there are two seed cases noted with IDs starting -1
                 my_dict[row[3]] = Individual()
                 my_dict[row[3]].ID = row[3]
             my_dict[row[3]].infected_others.append(row[2])
             my_dict[row[3]].time_infected_others.append(row[6])
             my_dict[row[3]].num_partners = row[-1]

# proof of function
print(my_dict['2369_0'].__dict__)
print(my_dict[my_dict['2369_0'].infected_by[0]].__dict__)
print(my_dict['8876_1'].__dict__)

print(len(my_dict))
#49011 : 49009 'true' cases and two seeds '-1_1' and '-1_2' infectors.
for key in my_dict:
    if my_dict[key].infected_by == []:
        print(key)

## find out the child number - direct and overall - for each person
## direct child number is just by counting num IDs in infected_others ##
for key in my_dict:
#    print(my_dict[key].__dict__)
#    print(len(my_dict[key].infected_others))
    my_dict[key].direct_children_num = len(my_dict[key].infected_others)

#%%
## find out overall child number by iteratively summing direct_children_num of infected_others and their children
## but need to be careful that start accumulating from the bottom of the trees upwards - visit all bottom children before any parents etc.
## post-order tree traversal. Not sure how to go about this, will think

# reset, can += 1 to add to total_children_num for each true child, reset if wrong / repeat
for key in my_dict:
    my_dict[key].total_children_num = 0

# just something I was doing to find tips of tree and their direct parents but prob don't want this?
tips_zero_children = []
parents_of_zero_children = []

for key in my_dict:
    if my_dict[key].direct_children_num == 0:
        #print(key)
        tips_zero_children.append(key)
        parents_of_zero_children.extend(my_dict[key].infected_by)

#%% TEST CASE - simple tree - calculating overall children
        '''
        ---------0---------
        |                 |
      --1--------         12
      |         |
    --2--     --5-----
    |   |     |      |
    3   4     6    --7--
                   |    |
                 --8--  11
                 |   |
                 9   10
Should have 3=0, 4=0, 2=2,
            9=0, 10=0, 8=2, 11=0, 7=4, 6=0, 5=6
            1 = 10
            12 = 0
            0 = 11
'''

# setting up test case
test_dict = {}
for i in range(13):
    test_dict[str(i)] = Individual()
    test_dict[str(i)].ID = str(i)
    #print(str(i))

zeroth = test_dict['0']
zeroth.infected_others = ['1', '12']
test_dict['1'].infected_others = ['2', '5']
test_dict['2'].infected_others = ['3', '4']
test_dict['3'].infected_others = []
test_dict['4'].infected_others = []
test_dict['5'].infected_others = ['6', '7']
test_dict['6'].infected_others = []
test_dict['7'].infected_others = ['8', '11']
test_dict['8'].infected_others = ['9', '10']
test_dict['9'].infected_others = []
test_dict['10'].infected_others = []
test_dict['11'].infected_others = []
test_dict['12'].infected_others = []

test_dict['1'].infected_by = ['0']
test_dict['2'].infected_by = ['1']
test_dict['3'].infected_by = ['2']
test_dict['4'].infected_by = ['2']
test_dict['5'].infected_by = ['1']
test_dict['6'].infected_by = ['5']
test_dict['7'].infected_by = ['5']
test_dict['8'].infected_by = ['7']
test_dict['9'].infected_by = ['8']
test_dict['10'].infected_by = ['8']
test_dict['11'].infected_by = ['7']
test_dict['12'].infected_by = ['0']

print(test_dict['1'].__dict__)

test_tree_tips = []
for i in test_dict:
    if test_dict[i].infected_others == []:
        test_tree_tips.append(i)
        #print(i)
print(test_tree_tips)
#['3', '4', '6', '9', '10', '11', '12']

accounted_for = []
parents_working_list = ['0']
for i in test_tree_tips:
    start_tip = '0' # root of the tree
    for j in test_dict[start_tip].infected_others:
        child_visit = j
        print(j)
        if len(test_dict[j].infected_others) > 0:
            to_visit = test_dict[j].infected_others
            parents_working_list.append(j)
            print(parents_working_list)
            print(to_visit)
        else:
            infector = test_dict[j].infected_by[0]
            test_dict[infector].total_children_num += 1
            accounted_for.append(j)
            print(accounted_for)
            print(infector)
            print(test_dict[infector].total_children_num)

for x in parents_working_list:
    if len(test_dict[x].infected_others) > 0: #i.e. has children
        for y in test_dict[x].infected_others:
            if len(test_dict[y].infected_others) > 0: #i.e. has children
                parents_working_list.append(y)

for i in range(13):
    test_dict[str(i)].total_children_num = 0

x ='0'
accounted_for = []
tot_parents = []
i = 0
while len(accounted_for) < len(test_dict):
    print('while')
    if len(test_dict[x].infected_others) > 0 and not set(test_dict[x].infected_others).issubset(accounted_for): #i.e. x has kids
        print('len > 0, 1')
        temp_parent = x
        tot_parents.append(x)
        print(temp_parent, tot_parents)
        for kid in test_dict[x].infected_others:
            if kid in accounted_for:
                print(kid, 'accounted')
#                pass
            else:
                print(kid, 'not accounted')
                a = test_dict[x].infected_others.index(kid)
                x = test_dict[x].infected_others[a]
                break
    elif len(test_dict[x].infected_others) == 0:
        print('len = 0')
        for parent in tot_parents:
            test_dict[parent].total_children_num += 1
            print('x', x, 'parent', parent, 'tot_kids', test_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(test_dict[x].infected_by[0])
        print('tot parents', tot_parents)
        x = test_dict[x].infected_by[0]
    elif set(test_dict[x].infected_others).issubset(accounted_for):
        print('check tot parents. x is', x, 'tot parents is', tot_parents)
        if tot_parents == []: #check not at the root, if at the root need to stop.
            break
        for parent in tot_parents:
            test_dict[parent].total_children_num += 1
            print('x', x, 'parent', parent, 'tot_kids', test_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(test_dict[x].infected_by[0])
        print('tot parents', tot_parents)
        x = test_dict[x].infected_by[0]
#    if accounted_for == ['3', '4']:
#        if i > 8:
#            print('i is', i)
#            print('acc for', accounted_for)
#            break
#    print(x, 'x')
#    if i > 8:
#        print('acc for', accounted_for)
#        break
#    i += 1
#
for i in range(13):
    print(i, test_dict[str(i)].total_children_num)

## this works!! :) try for true dict

#%% TRUE CSV TRANSMISSION NETWORK - calculating overall children
    ## For actual dict
i = 0
for key in my_dict:
    print(my_dict[key].total_children_num)
    i+=1
    if i >10:
        break

# reset, can += 1 to add to total_children_num for each true child, reset if wrong / repeat
for key in my_dict:
    my_dict[key].total_children_num = 0

# two seeds '-1_1' and '-1_2' infectors.
for key in my_dict:
    if my_dict[key].infected_by == []:
        print(key)

#1st seed
x ='-1_-1'
accounted_for = []
tot_parents = []
i = 0
while len(accounted_for) < len(my_dict):
    #print('while')
    if len(my_dict[x].infected_others) > 0 and not set(my_dict[x].infected_others).issubset(accounted_for): #i.e. x has kids
        #print('len > 0, 1')
        temp_parent = x
        tot_parents.append(x)
        #print(temp_parent, tot_parents)
        for kid in my_dict[x].infected_others:
            if kid in accounted_for:
                #print(kid, 'accounted')
                pass
            else:
                #print(kid, 'not accounted')
                a = my_dict[x].infected_others.index(kid)
                x = my_dict[x].infected_others[a]
                break
    elif len(my_dict[x].infected_others) == 0:
        #print('len = 0')
        for parent in tot_parents:
            my_dict[parent].total_children_num += 1
            #print('x', x, 'parent', parent, 'tot_kids', my_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(my_dict[x].infected_by[0])
        #print('tot parents', tot_parents)
        x = my_dict[x].infected_by[0]
    elif set(my_dict[x].infected_others).issubset(accounted_for):
        #print('check tot parents. x is', x, 'tot parents is', tot_parents)
        if tot_parents == []: #check not at the root, if at the root need to stop.
            break
        for parent in tot_parents:
            my_dict[parent].total_children_num += 1
            #print('x', x, 'parent', parent, 'tot_kids', my_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(my_dict[x].infected_by[0])
        #print('tot parents', tot_parents)
        x = my_dict[x].infected_by[0]

#2nd seed
x ='-1_2'
accounted_for = []
tot_parents = []
i = 0
while len(accounted_for) < len(my_dict):
    #print('while')
    if len(my_dict[x].infected_others) > 0 and not set(my_dict[x].infected_others).issubset(accounted_for): #i.e. x has kids
        #print('len > 0, 1')
        temp_parent = x
        tot_parents.append(x)
        #print(temp_parent, tot_parents)
        for kid in my_dict[x].infected_others:
            if kid in accounted_for:
                #print(kid, 'accounted')
                pass
            else:
                #print(kid, 'not accounted')
                a = my_dict[x].infected_others.index(kid)
                x = my_dict[x].infected_others[a]
                break
    elif len(my_dict[x].infected_others) == 0:
        #print('len = 0')
        for parent in tot_parents:
            my_dict[parent].total_children_num += 1
            #print('x', x, 'parent', parent, 'tot_kids', my_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(my_dict[x].infected_by[0])
        #print('tot parents', tot_parents)
        x = my_dict[x].infected_by[0]
    elif set(my_dict[x].infected_others).issubset(accounted_for):
        #print('check tot parents. x is', x, 'tot parents is', tot_parents)
        if tot_parents == []: #check not at the root, if at the root need to stop.
            break
        for parent in tot_parents:
            my_dict[parent].total_children_num += 1
            #print('x', x, 'parent', parent, 'tot_kids', my_dict[parent].total_children_num)
        accounted_for.append(x)
        tot_parents.remove(my_dict[x].infected_by[0])
        #print('tot parents', tot_parents)
        x = my_dict[x].infected_by[0]

## inspect frequencies of diff numbers of kids
a = []
for item in my_dict:
    a.append(my_dict[item].total_children_num)
d = np.unique(a, return_counts=True)
#print('all times, frequency of diff pops', d)
print('requency of diff overall kids below', '\n', 'freqs:', d[0], 'counts:', d[1])
print(len(d[1]))

## compare numbers of individual children:
b = []
for item in my_dict:
    b.append(my_dict[item].direct_children_num)
e = np.unique(b, return_counts=True)
#print('all times, frequency of diff pops', d)
print('frequency of diff direct kids below', '\n', 'number:', e[0], '\n', 'counts:', e[1])
print(len(e[1]))

for key in my_dict:
    if my_dict[key].infected_by == []:
        print(key)
        print(my_dict[key].total_children_num)

## that did seem to work.... freq of total_child_num == 0 is same as freq direct_children_num == 0

'''class Individual:
    def __init__(self):
        self.ID = None
        self.infected_others = []
        self.infected_by = []
        self.time_infected_others =[]
        self.time_infected_by = []
        self.age_birth = -1
        self.age_death = -1
        self.num_partners = -1
        self.direct_children_num = 0
        self.total_children_num = 0'''

#%%  SAVE CALCULATED CLASS STRUCTURE AND INFO
# Pickle this custom class data to just load in in future
import pickle

#how to save
with open(file_loc + 'pickled_data_all.pickle', 'wb') as f:
    pickle.dump(my_dict, f)

# loading example
with open(file_loc + 'pickled_data_all.pickle', 'rb') as f:
    cc = pickle.load(f)

#proof it works
print('Pickle loaded, 1st root num kids: {}'.format(cc['-1_-1'].total_children_num))

#further proof
ct = 0
for key in cc:
    if ct < 5:
        print(cc[key])
        print(cc[key].__dict__)
    else:
        break
    ct += 1

max_stars_repo_name: marianne-aspbury/HIVsimulation

max_issues_repo_name: marianne-aspbury/HIVsimulation

max_forks_repo_name: marianne-aspbury/HIVsimulation


text:
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

import cv2
from sigmarsGarden.catalogue import Catalogue
from sigmarsGarden.config import R1440P_CONFIG
from sigmarsGarden.parse import getSquares
from sigmarsGarden.screenshot import get_screen
from sigmarsGarden.match import match_squares
import numpy as np

def main() -> None:
    c = Catalogue(Path("/home/lukas/templates"))

    x = cv2.imread("testboards/3.jpg")
    # x = get_screen()

    squares = getSquares(x, R1440P_CONFIG)
    match_squares(c, squares, debug=True)

if __name__ == "__main__":
    main()

max_stars_repo_name: corrodedHash/sigmarsGarden

max_issues_repo_name: corrodedHash/sigmarsGarden

max_forks_repo_name: corrodedHash/sigmarsGarden


text:
'''Average runtime 6.66 seconds on my computer -> The fastest solution!'''
import multiprocessing as mp
import numpy as np
from timeit import default_timer as t

def prepare_data():
    np.random.RandomState(100)
    arr = np.random.randint(0, 10, size=[300000, 150])
    return arr.tolist()

def find_nums_within_range_for_current_row(row, minimum, maximum):
    """Returns how many numbers lie within `maximum` and `minimum` in a given `row`"""
    count = 0
    for n in row:
        if minimum <= n <= maximum:
            count = count + 1
    return count

if __name__ == "__main__":
    data = prepare_data()

    start = t()

    with mp.Pool(mp.cpu_count()) as pool:
        pool.starmap(find_nums_within_range_for_current_row, [(row, 4, 8) for row in data])

    elapsed_time = t() - start
    print(f'Elapsed time: {elapsed_time:.2f} seconds')

max_stars_repo_name: Psykepro/Concurrency

max_issues_repo_name: Psykepro/Concurrency

max_forks_repo_name: Psykepro/Concurrency


text:
"""
File: test.py
Author: Nrupatunga
Email: nrupatunga.s@byjus.com
Github: https://github.com/nrupatunga
Description: test script
"""
from pathlib import Path

import cv2
import numpy as np
import torch
from imutils import paths

from core.trainers.filter_trainer import LitModel

def get_output(ckpt_path, model, gpu=True):
    data = cv2.imread(img_path) / 255.
    data = np.transpose(data, [2, 0, 1])
    data = torch.tensor(data).unsqueeze(0)
    data = data.float()

    if gpu:
        output = model(data.cuda()).squeeze().cpu().numpy()
    else:
        output = model(data).squeeze().numpy()

    output = np.transpose(output, [1, 2, 0])
    output = np.clip(output, 0, 1)
    output = output * 255
    output = output.astype(np.uint8)

    return output

if __name__ == "__main__":
    ckpt_path = './ckpt/Photographic-Style/epoch=138.ckpt'
    # ckpt_path = './ckpt/L0-smoothing/_ckpt_epoch_79.ckpt'
    imgs_dir = './images'
    out_dir = './results'
    gpu = True

    if gpu:
        model = LitModel.load_from_checkpoint(ckpt_path).cuda()
    else:
        model = LitModel.load_from_checkpoint(ckpt_path,
                                              map_location=torch.device('cpu'))
    model.eval()
    model.freeze()

    imgs_path = paths.list_images(imgs_dir)
    for img_path in imgs_path:
        out_name = Path(img_path).stem + '_out.png'
        Path(out_dir).mkdir(parents=True, exist_ok=True)
        out_path = str(Path(out_dir).joinpath(out_name))
        out = get_output(ckpt_path, model)
        cv2.imwrite(out_path, out)

max_stars_repo_name: nrupatunga/Fast-Image-Filters

max_issues_repo_name: delldu/Fast-Image-Filters

max_forks_repo_name: delldu/Fast-Image-Filters