text:
import pytest
import numpy as np
from numpy.testing import assert_allclose
import openpnm as op
from openpnm.models.misc import constant

class MultiPhaseTest:

    def setup_class(self):
        self.net = op.network.Cubic(shape=[10, 10, 10])
        self.water = op.phase.Water(network=self.net, name="water")
        self.air = op.phase.Air(network=self.net, name="air")
        self.oil = op.phase.Water(network=self.net, name="oil")

    def test_multiphase_init(self):
        phases = [self.air, self.water]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)

        assert np.all(m['pore.occupancy.all'] == 0.0)
        assert np.all(m['throat.occupancy.all'] == 0.0)

        assert self.air.name in m.settings['phases']
        assert self.water.name in m.settings['phases']

    def test_multiphase_no_occupancy_yet(self):
        phases = [self.air, self.water]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)
        self.water['pore.temperature'] = 300

        assert np.all(self.water['pore.temperature'] == 300)

        with pytest.raises(Exception):
            m['pore.temperature']

    def test_multiphase_set_occupancy_w_indices_only(self):
        phases = [self.air, self.water]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)

        Ps_water = np.array([0, 1, 2])
        Ps_water_mask = self.net.to_mask(pores=Ps_water)
        Ts_water = np.array([4, 12, 22])
        Ts_water_mask = self.net.to_mask(throats=Ts_water)

        m.set_occupancy(self.water, pores=Ps_water, throats=Ts_water)

        assert m["pore.occupancy.water"][Ps_water_mask].mean() == 1
        assert m["pore.occupancy.water"][~Ps_water_mask].mean() == 0
        assert m["throat.occupancy.water"][Ts_water_mask].mean() == 1
        assert m["throat.occupancy.water"][~Ts_water_mask].mean() == 0

    def test_multiphase_set_occupancy_w_values_only(self):
        phases = [self.air, self.water]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)

        Pvals = np.array([0.5, 0.9, 0.01])
        Tvals = np.array([0.9, 0.01])

        # Pvals must be Np-long if not accompanied by "pores" argument
        with pytest.raises(Exception):
            m.set_occupancy(self.water, Pvals=Pvals)

        # Tvals must be Nt-long if not accompanied by "throats" argument
        with pytest.raises(Exception):
            m.set_occupancy(self.water, Tvals=Tvals)

        # Set pore occupancy
        m.set_occupancy(self.water, Pvals=1)
        assert m["pore.occupancy.water"].mean() == 1
        Pvals = np.ones(self.net.Np) * 0.5
        m.set_occupancy(self.water, Pvals=Pvals)
        assert m["pore.occupancy.water"].mean() == 0.5

        # Setting throat occupancy
        m.set_occupancy(self.water, Tvals=1)
        assert m["throat.occupancy.water"].mean() == 1
        Tvals = np.ones(self.net.Nt) * 0.54
        m.set_occupancy(self.water, Tvals=Tvals)
        assert m["throat.occupancy.water"].mean() == 0.54

    def test_multiphase_set_occupancy_w_pore_indices_and_Pvals(self):
        phases = [self.air, self.water]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)

        Ps_water = np.array([0, 1, 2])
        Pvals = np.array([0.5, 0.9, 0.01])
        Ps_water_mask = self.net.to_mask(Ps_water)
        Ts_water = np.array([4, 12, 22])
        Ts_water_mask = self.net.to_mask(throats=Ts_water)
        Tvals = np.array([0.3, 0.4, 0.1])

        # Pvals/Tvals and pores/throats; same array length
        m.set_occupancy(
            phase=self.water,
            pores=Ps_water,
            throats=Ts_water,
            Pvals=Pvals,
            Tvals=Tvals
        )
        assert_allclose(m["pore.occupancy.water"][Ps_water], Pvals)
        assert_allclose(m["throat.occupancy.water"][Ts_water], Tvals)
        assert m["pore.occupancy.water"][~Ps_water_mask].mean() == 0
        assert m["throat.occupancy.water"][~Ts_water_mask].mean() == 0

        # Pvals and pores; inconsistent size
        with pytest.raises(Exception):
            m.set_occupancy(self.water, pores=[1, 5, 10], Pvals=[0.5, 0])

        # Tvals and throats; inconsistent size
        with pytest.raises(Exception):
            m.set_occupancy(self.water, throats=[10, 52, 0], Tvals=[0.5, 0.01])

        # Pvals/Tvals.size = 1 and pores/throats.size > 1
        m.set_occupancy(
            phase=self.water,
            pores=Ps_water,
            throats=Ts_water,
            Pvals=0.25,
            Tvals=0.23
        )
        assert m["pore.occupancy.water"][Ps_water].mean() == 0.25
        assert m["throat.occupancy.water"][Ts_water].mean() == 0.23

    def test_multiphase_automatic_throat_occupancy(self):
        m = op.contrib.MultiPhase(network=self.net, phases=[self.air,
                                                           self.water])
        pores = np.random.choice(self.net.Ps, size=100, replace=False)
        Pvals = np.random.random(pores.size)
        m.set_occupancy(self.water, pores=pores, Pvals=Pvals)
        P1, P2 = self.net["throat.conns"].T
        oc1, oc2 = [m["pore.occupancy.water"][x] for x in (P1, P2)]
        # Throats take average occupancy of adjacent pores
        m._set_automatic_throat_occupancy(mode="mean")
        assert_allclose(m["throat.occupancy.water"], (oc1+oc2)/2)
        # Throats take maximum occupancy of adjacent pores
        m._set_automatic_throat_occupancy(mode="max")
        assert_allclose(m["throat.occupancy.water"], np.maximum(oc1, oc2))
        # Throats take minimum occupancy of adjacent pores
        m._set_automatic_throat_occupancy(mode="min")
        assert_allclose(m["throat.occupancy.water"], np.minimum(oc1, oc2))

    def test_multiphase_occupancy_set_single_phase(self):
        m = op.contrib.MultiPhase(network=self.net)
        self.water['pore.temperature'] = 300
        assert np.all(self.water['pore.temperature'] == 300)
        m.set_occupancy(phase=self.water, Pvals=1, Tvals=1)
        assert np.all(m['pore.temperature'] == 300)

    def test_multiphase_occupancy_set_two_phase(self):
        m = op.contrib.MultiPhase(network=self.net)
        self.water['pore.temperature'] = 300
        self.air['pore.temperature'] = 200

        assert np.all(self.water['pore.temperature'] == 300)
        assert np.all(self.air['pore.temperature'] == 200)

        Ps = self.net['pore.coords'][:, 0] < 3
        Ts = self.net.to_mask(throats=self.net.find_neighbor_throats(Ps))

        m.set_occupancy(phase=self.water, Pvals=Ps, Tvals=Ts)
        m.set_occupancy(phase=self.air, Pvals=~Ps, Tvals=~Ts)

        assert np.all(m['pore.temperature'] >= 200)
        assert np.all(m['pore.temperature'] <= 300)

    def test_mutliphase_partition_coef(self):
        phases = [self.water, self.air, self.oil]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)

        x, y, z = self.net["pore.coords"].T
        ps_water = self.net.Ps[(y <= 3) + (y >= 8)]
        ps_air = self.net.Ps[(y > 3) * (y < 6)]
        ps_oil = self.net.Ps[(y >= 6) * (y < 8)]

        # Phase arrangement (y-axis): W | A | O | W
        m.set_occupancy(phase=self.water, pores=ps_water)
        m.set_occupancy(phase=self.air, pores=ps_air)
        m.set_occupancy(phase=self.oil, pores=ps_oil)

        K_air_water = 2.0
        K_air_oil = 1.8
        K_water_oil = 0.73

        m.set_binary_partition_coef(
            phases=[self.air, self.water], model=constant, value=K_air_water
        )
        m.set_binary_partition_coef(
            phases=[self.air, self.oil], model=constant, value=K_air_oil
        )
        m.set_binary_partition_coef(
            phases=[self.water, self.oil], model=constant, value=K_water_oil
        )

        K_aw = m["throat.partition_coef.air:water"]
        K_ao = m["throat.partition_coef.air:oil"]
        K_wo = m["throat.partition_coef.water:oil"]
        K_global = m["throat.partition_coef.all"]

        assert np.isclose(K_aw.mean(), K_air_water)
        assert np.isclose(K_ao.mean(), K_air_oil)
        assert np.isclose(K_wo.mean(), K_water_oil)

        # Get water-air interface throats
        tmp1 = self.net.find_neighbor_throats(ps_water, mode="xor")
        tmp2 = self.net.find_neighbor_throats(ps_air, mode="xor")
        Ts_water_air = np.intersect1d(tmp1, tmp2)

        # Get air-oil interface throats
        tmp1 = self.net.find_neighbor_throats(ps_air, mode="xor")
        tmp2 = self.net.find_neighbor_throats(ps_oil, mode="xor")
        Ts_air_oil = np.intersect1d(tmp1, tmp2)

        # Get oil-water interface throats
        tmp1 = self.net.find_neighbor_throats(ps_oil, mode="xor")
        tmp2 = self.net.find_neighbor_throats(ps_water, mode="xor")
        Ts_oil_water = np.intersect1d(tmp1, tmp2)

        # K_global for water-air interface must be 1/K_air_water
        assert np.isclose(K_global[Ts_water_air].mean(), 1/K_air_water)

        # K_global for air-oil interface must be K_air_oil (not 1/K_air_oil)
        assert np.isclose(K_global[Ts_air_oil].mean(), K_air_oil)
        # K_global for oil-water interface must be 1/K_water_oil
        assert np.isclose(K_global[Ts_oil_water].mean(), 1/K_water_oil)
        # K_global for single-phase regions must be 1.0
        temp = Ts_water_air, Ts_air_oil, Ts_oil_water
        interface_throats = np.hstack(temp)
        Ts_single_phase = np.setdiff1d(self.net.Ts, interface_throats)
        assert np.isclose(K_global[Ts_single_phase].mean(), 1.0)

    def test_multiphase_invalid_phase(self):
        pn = op.network.Cubic(shape=[3, 3, 3])
        water = op.phase.Water(network=pn)
        m = op.contrib.MultiPhase(network=self.net)
        with pytest.raises(Exception):
            m.set_occupancy(phase=water)

    def test_multiphase_invalid_occupancy(self):
        m = op.contrib.MultiPhase(network=self.net, phases=[self.water, self.air])
        # The next line ideally should throw an Exception, but warning for now
        m.set_occupancy(phase=self.water, Pvals=1.5, Tvals=2.5)

    def test_format_interface_prop(self):
        phases = [self.water, self.air]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)
        propname = m._format_interface_prop(propname="throat.foo", phases=phases)
        assert propname == "throat.foo.water:air"

    def test_get_phases_names(self):
        phases = [self.water, self.air]
        m = op.contrib.MultiPhase(network=self.net, phases=phases)
        names = m._get_phases_names("throat.foo.air:water")
        assert names[0] == "air"
        assert names[1] == "water"

    def test_assemble_partition_coef_global(self):
        # Create network and 3 pure phases
        net = op.network.Cubic([6, 1, 1])
        air = op.phase.Air(network=net, name="air")
        water = op.phase.Water(network=net, name="water")
        oil = op.phase.Water(network=net, name="oil")

        # Create MultiPhase and set phase occupancies
        m = op.contrib.MultiPhase(network=net, phases=[water, air, oil])
        m.set_occupancy(air, pores=[0, 1])
        m.set_occupancy(water, pores=[2, 3])
        m.set_occupancy(oil, pores=[4, 5])

        # Check if K_global is initially ones
        K_actual = m._assemble_partition_coef_global()
        assert_allclose(K_actual, 1.)

        # Add binary partition coefficient models
        m.set_binary_partition_coef([water, air], model=constant, value=0.7)
        m.set_binary_partition_coef([water, oil], model=constant, value=1.1)

        # Check if K_global is correctly populated after models were added
        K_actual = m._assemble_partition_coef_global()
        assert_allclose(K_actual, [1., 1/0.7, 1., 1.1, 1])

if __name__ == '__main__':

    t = MultiPhaseTest()
    self = t
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print(f'Running test: {item}')
            t.__getattribute__(item)()

max_stars_repo_name: xu-kai-xu/OpenPNM

max_issues_repo_name: xu-kai-xu/OpenPNM

max_forks_repo_name: xu-kai-xu/OpenPNM

reason: import numpy,from numpy


text:
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm

Xmin   = -0.2
Xmax   = 1.6
deltaX = 0.05
X0      = np.linspace(Xmin, Xmax, int((Xmax - Xmin)/deltaX))

Tmin = 0
Tmax = 2
deltaT = 0.15
T      = np.linspace(Tmin, Tmax, int((Tmax - Tmin)/deltaT))

# U_t + F(U(X,t))_x = 0
# U(X, T) is constant

# initilize F
F = lambda U: U**2/2
FD = lambda U: U
FStarSolve = lambda: 0

def initial_conditions(x):
	if x <= 0.0:
		return 1.0
	elif 0.0< x <=1.0:
		return 1.0 - x
	elif x > 0.0:
		return 0.0

def characteristic_solution(x0, t):
	x = []
	for i in range(len(x0)):
		x.append(initial_conditions(x0[i])*t + x0[i])
	return np.array(x)

def plot_characteristic(x, t):
	for i in range(x.shape[0]):
		plt.plot(x[i], t, 'g')
	plt.show()

# Rankine-Hugoniot condition
def RHCondition(F, U):
	s = []
	for i in range(len(U) - 1):
		s.append((F(U[i+1]) - F(U[i]))/ (U[i+1] - U[i]))
	return s
plot_characteristic(characteristic_solution(X0, T), T)

def Upwind_Method(F, FD, U, k, h):
	for i in range(1, len(U) - 1):
		if FD(U[i]) >= 0:
			U[i] = U[i] - (k/h)*(F(U[i]) - F(U[i-1]))
		else:
			U[i] = U[i] - (k/h)*(F(U[i +1]) - F(U[i]))
	return U

def Lax_Friedrichs_scheme(F, FD, U, k, h):
	for i in range(1, len(U) - 1):
		U[i] =0.5*(U[i+1] + U[i-1]) - (k/(2.*h))*(F(U[i +1]) - F(U[i]))
	return U

def Mac_Cornack_scheme(F, FD, U, k, h):
	Ustar = lambda u, up1: u - (k/h)*(F(up1) - F(u))
	for i in range(1, len(U) - 1):
		U[i] =0.5*(U[i] + Ustar(U[i], U[i+1])) -\
		 (k/h)*(F(Ustar(U[i], U[i+1])) - F(Ustar(U[i-1], U[i])))
	return U

def Richtmyer_two_step_Lax_Wendroff_scheme(F, FD, U, k, h):
	Ustar = lambda u, up1: 0.5*(u + up1) - (k/h)*(F(up1) - F(u))
	for i in range(1, len(U) - 1):
		U[i] = U[i] - (k/h)*(F(Ustar(U[i], U[i+1])) - F(Ustar(U[i-1], U[i])))
	return U

def Gudonov_Method(F, FD, FStarSolve, U, k, h):
	def Speed(u, up1):
		return float(F(up1) - F(u))/(up1 - u)

	def Ustar(u, up1):
		if (FD(u) >= 0) and (FD(up1) >= 0):
			return u
		elif (FD(u) < 0) and (FD(up1) < 0):
			return up1
		elif (FD(u) >= 0) and (FD(up1) < 0):
			if Speed(u, up1) >= 0:
				return u
			else:
				return up1
		elif (FD(up1) >= 0) and (FD(u) < 0):
			return FStarSolve()

	for i in range(1, len(U) - 1):
		U[i] = U[i] -(k/h)*(F(Ustar(U[i], U[i+1])) - F(Ustar(U[i-1], U[i])))
	return U

def find_solution(U0, T, nsteps, k, h, method='Richtmyer_two_step_Lax_Wendroff_scheme'):
	tsteps = int(T/k) + 1
	U = np.zeros((len(U0), tsteps))
	U[:, 0] = U0

	# print ("[INFO] tsteps: {}, nsteps: {}".format(tsteps, nsteps))
	for tt in tqdm(range(tsteps - 1)):
		for xx in range(nsteps):
			# print "[Secondary INFO] tt: {}: Utt: {}".format(tt, U[:, tt])
			if method == 'upwind':
				U[:, tt + 1] = Upwind_Method(F, FD, U[:, tt], k, h)
			elif method == 'Mac_Cornack_scheme':
				U[:, tt + 1] = Mac_Cornack_scheme(F, FD, U[:, tt], k, h)
			elif method == 'Lax_Friedrichs_scheme':
				U[:, tt + 1] = Lax_Friedrichs_scheme(F, FD, U[:, tt], k, h)
			elif method == 'Richtmyer_two_step_Lax_Wendroff_scheme':
				U[:, tt + 1] = Gudonov_Method(F, FD, FStarSolve, U[:, tt], k, h)
			elif method == 'Gudonov_Method':
				U[:, tt + 1] = Gudonov_Method(F, FD, FStarSolve, U[:, tt], k, h)

	return U

U0 = np.array([initial_conditions(x) for x in X0])
legend_array = []
for T in [0, 0.005, 0.5, 1.0]:
	U = find_solution(U0, T, 2, 0.005, 0.14)
	plt.plot(X0, U[:, -1])
	legend_array.append('t = {}'.format(T))
plt.legend(legend_array)
plt.show()
"""
legend_array = []
k = 0.005
h = 0.05
T = 1.0
U0 = np.array([initial_conditions(x) for x in X0])
U = find_solution(U0, T, 25, k, h)

# plt.ion()
for tt in range(int(T/k)):
	if tt % 20 == 0:
		# plt.clf()
		plt.plot(X0[1:-1], (1.0 - U[1:-1, tt])/2.0)
		# plt.pause(0.2)
		legend_array.append('t = {}'.format(tt*k))
		plt.legend(legend_array)
plt.show()
"""

ext: py

reason: import numpy


text:
import cPickle as pickle
import numpy as np
import theano_funcs
import utils
import vgg16

from lasagne.layers import set_all_param_values, get_all_param_values
from os.path import join
from tqdm import tqdm

def train_geometric_matching():
    trans_params = {
        'rotation': (0, 0),
        'offset':   (0, 0),
        'flip':     (False, False),
        'shear':    (0., 0.),
        'stretch':  (1. / 2, 2),
    }

    print('building model')
    layers = vgg16.build_model((None, 3, 227, 227))

    # file to store the learned weights
    weightsfile = join('weights', 'weights.pickle')

    # initialize the feature extraction layers
    pretrainfile = join('weights', 'vgg16.pkl')
    print('initializing feature extraction layers from %s' % (pretrainfile))
    with open(pretrainfile, 'rb') as f:
        data = pickle.load(f)
    # weights are tied, no need to initialize a and b
    set_all_param_values(layers['pool4a'], data['param values'][0:20])

    # used to initialize from learned weights
    #with open(weightsfile, 'rb') as f:
    #    param_values = pickle.load(f)
    #set_all_param_values(layers['trans'], param_values)

    mean = data['mean value']

    max_epochs = 5000
    batch_size = 16
    sample_every = 25  # visualizes network output every n epochs
    sample_dir = join('data', 'samples')

    # set this to point to the root of Pascal VOC-2011
    voc_fpath = '/media/hdd/hendrik/datasets/pascal-2011'
    train_fpaths, valid_fpaths = utils.train_val_split(voc_fpath)

    print('compiling theano functions for training')
    train_func = theano_funcs.create_train_func(layers)
    print('compiling theano functions for validation')
    valid_func = theano_funcs.create_valid_func(layers)

    try:
        for epoch in range(1, max_epochs + 1):
            print('epoch %d' % (epoch))
            train_losses = []
            num_train_idx = (len(train_fpaths) + batch_size - 1) / batch_size
            train_iter = utils.get_batch_idx(len(train_fpaths), batch_size)
            for i, idx in tqdm(train_iter, total=num_train_idx, leave=False):
                X_crop_train, X_warp_train, M_train =\
                    utils.prepare_synth_batch(train_fpaths[idx], mean,
                                              trans_params)
                M, train_loss = train_func(X_crop_train, X_warp_train, M_train)
                train_losses.append(train_loss)
                if epoch % sample_every == 0:
                    utils.plot_samples(X_crop_train, X_warp_train, M, mean,
                                       prefix=join(sample_dir, 'train_%d' % i))
            print(' train loss = %.6f' % (np.mean(train_losses)))

            valid_losses = []
            num_valid_idx = (len(valid_fpaths) + batch_size - 1) / batch_size
            valid_iter = utils.get_batch_idx(len(valid_fpaths), batch_size)
            for i, idx in tqdm(valid_iter, total=num_valid_idx, leave=False):
                X_crop_valid, X_warp_valid, M_valid =\
                    utils.prepare_synth_batch(valid_fpaths[idx], mean,
                                              trans_params)
                M, valid_loss = valid_func(X_crop_valid, X_warp_valid, M_valid)
                valid_losses.append(valid_loss)
                if epoch % sample_every == 0:
                    utils.plot_samples(X_crop_valid, X_warp_valid, M, mean,
                                       prefix=join(sample_dir, 'valid_%d' % i))
            print(' valid loss = %.6f' % (np.mean(valid_losses)))
    except KeyboardInterrupt:
        print('caught ctrl-c, stopped training')

    print('saving weights to %s' % (weightsfile))
    weights = get_all_param_values(layers['trans'])
    with open(weightsfile, 'wb') as f:
        pickle.dump(weights, f, protocol=pickle.HIGHEST_PROTOCOL)

if __name__ == '__main__':
    train_geometric_matching()

max_stars_repo_name: hjweide/convnet-for-geometric-matching

max_issues_repo_name: hjweide/convnet-for-geometric-matching

max_forks_repo_name: hjweide/convnet-for-geometric-matching

reason: import numpy,import theano


text:
import os
from PyQt5 import QtCore, QtWidgets, QtGui

import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
from matplotlib.pyplot import Axes
from mpl_toolkits.axes_grid1.axes_divider import make_axes_area_auto_adjustable

import csv
import numpy as np
from PIL import Image

def load_image_pil(path):
    """
    Read images using the PIL lib
    """
    file = Image.open(str(path))  # 'I;16B'
    return np.array(file.getdata()).reshape(file.size[::-1])

def load_image_ascii(path):
    """
    Read ASCII images using the csv lib
    """
    delimiter = '\t'
    data = []
    for row in csv.reader(open(path), delimiter=delimiter):
        data.append(row[:-1])
    img = np.array(data).astype(np.double)
    return img

def brush_to_color_tuple(brush):
    r, g, b, a = brush.color().getRgbF()
    return r, g, b

class MplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)

        ax = Axes(fig, [0., 0., 1., 1.])
        fig.add_axes(ax)
        self.axes = ax
        self.fig = fig

        self.canvas = FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

        #self.figure.subplots_adjust(left= 0.15, bottom=0.15)
        window_brush = self.window().palette().window()
        fig.set_facecolor(brush_to_color_tuple(window_brush))
        fig.set_facecolor(brush_to_color_tuple(window_brush))

        self.reset()

    def reset(self):
        self.image_handlers = None
        self.line_handlers = []
        self.axes.clear()
        self.axes.set_axis_off() # must be after clear()
        self.draw() # test

    def compute_initial_figure(self):
        pass

    def axis_on(self):
        self.axes.set_axis_on()
        make_axes_area_auto_adjustable(self.axes)

    def update_image(self, image):
        if self.image_handlers is None:
            self.image_handlers = self.axes.imshow(image)
        else:
            self.image_handlers.set_data(image)
            # amplitude and phase have dramatically different ranges, so rescaling is necessary
            self.image_handlers.autoscale()
        self.draw()

    def update_plot(self, xValues, yValues):

        num_plots = yValues.shape[1]
        if len(self.line_handlers) == 0:
            for i in range(num_plots):
                h = self.axes.plot(xValues, yValues[:,i])
                self.line_handlers.append(h[0])
        else:
            for hidx, h in enumerate(self.line_handlers):
                h.set_data(xValues, yValues[:,hidx])
            self.axes.relim()
            self.axes.autoscale_view()

        self.draw()

class MplCanvasTool(QtWidgets.QWidget):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        super().__init__(parent)

        fig = Figure(figsize=(width, height), dpi=dpi)
        ax = Axes(fig, [0., 0., 1., 1.])
        ax.set_axis_off()
        fig.add_axes(ax)
        self.ax = ax
        self.fig = fig
        self.canvas = FigureCanvas(fig)
        self.toolbar = NavigationToolbar(self.canvas, self, False)

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        self.setLayout(layout)

        self.fig.canvas.mpl_connect('button_press_event', self._on_press)
        self.fig.canvas.mpl_connect('button_release_event', self._on_release)
        self.fig.canvas.mpl_connect('motion_notify_event', self._on_motion)

        self.crop_x0 = None
        self.crop_x1 = None
        self.crop_y0 = None
        self.crop_y1 = None

        self.bad_flag = False

    def _on_press(self, event):
        if event.inaxes:
            self.crop_x0 = event.xdata
            self.crop_y0 = event.ydata

    def _on_release(self, event):
        self.crop_x1 = event.xdata
        self.crop_y1 = event.ydata
        if event.inaxes:
            if (self.crop_x0, self.crop_y0) == (self.crop_x1, self.crop_y1):
                pass
                # if self.bad_flag:
                #     self.bad_pixels_widget.addItem('%d, %d' %
                #                                    (int(round(self.crop_x1)),
                #                                     int(round(self.crop_y1))))
            elif self.set_roi_enabled:
                pass
                # if self.his_enabled:
                #     roi_crop = self.roi_img_equ[int(round(self.crop_y0)):
                #                                 int(round(self.crop_y1)),
                #                                 int(round(self.crop_x0)):
                #                                 int(round(self.crop_x1))]
                # else:
                #     roi_crop = self.roi_img[int(round(self.crop_y0)):
                #                             int(round(self.crop_y1)),
                #                             int(round(self.crop_x0)):
                #                             int(round(self.crop_x1))]
                # self.crop_ax.imshow(roi_crop,
                #                     interpolation='nearest',
                #                     origin='upper',
                #                     cmap=self._ref_color_map,
                #                     extent=[int(round(self.crop_x0)),
                #                             int(round(self.crop_x1)),
                #                             int(round(self.crop_y0)),
                #                             int(round(self.crop_y1))])
                #
                # tfont = {'size': '22',
                #          'weight': 'semibold'
                #          }
                #
                # msg = ('ROI will be set as (%d, %d) - (%d, %d)' %
                #        (int(round(self.crop_x0)), int(round(self.crop_y0)),
                #         int(round(self.crop_x1)), int(round(self.crop_y1))))
                # self.crop_ax.set_title(msg,
                #                        **tfont)
                # self.crop_canvas.draw()
                # self.crop_widget.show()

    def _on_motion(self, event):
        pass
        # if self.set_roi_enabled and event.button == 1 and event.inaxes:
        #     self.rect.set_width(event.xdata - self.crop_x0)
        #     self.rect.set_height(event.ydata - self.crop_y0)
        #     self.rect.set_xy((self.crop_x0, self.crop_y0))
        #     self.ax.figure.canvas.draw()

    def update_image(self, image):
        self.ax.cla()
        self.ax.set_axis_off()
        self.ax.imshow(image)
        self.canvas.draw()

# import matplotlib.pyplot as plt
# img = load_image_pil('../../test.tif')
# plt.imshow(img)
# plt.show()

max_stars_repo_name: bsmind/dpc

max_issues_repo_name: bsmind/dpc

max_forks_repo_name: bsmind/dpc


text:
#!/usr/bin/env python
# coding: utf-8

# In[1]:

#import basic librarys
import numpy as np #is used for mathematical formulas
import matplotlib.pyplot as plt # is used for plot grafics

#Time [ms] simulation
T = 20
#Number of samples
N = 100
#Time [ms] for Plot
t=np.linspace(0,T,N*T)

#Define sinus funktion amplidude[x], frequency[Hz], angle in [degree], order[1,2,3,...,n]
def sin(a,f,angle_deg,k):
    phi=angle_deg*(np.pi/180)
    x=a*np.sin(2*np.pi*f*k*t/1000+phi*k)
    return x

#Define Plot
def graph(x,legend):
    plt.plot(t,x,linewidth=2, label=legend)

#Define Grid for PLots
def grid():
    plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0)
    plt.grid(b=True, color='black', linestyle='-',alpha=0.1)
    plt.xlim(0, T)
    plt.ylabel(r'$\hat x$ [Amplitude]', fontsize='x-large')
    axes1 = plt.gca()
    axes1.set_xlabel("t [Time in ms]", fontsize='x-large')

# In[2]:

'''
####dictionary####

#Sinus like this
y=sin(230,50,45,1)

#Basic polt
graph('funktion','legend')

#Basic plot title
plt.title('Sinus Funkion',fontsize='x-large')

#print plot
plt.show()
'''

# In[3 ]:

#example Sinus
fx=sin(1,50,0,1) #use funktion (x=1, f=50Hz,phi=0°,1st Order)
graph(fx,"Sinus")#use funktion for generating a graph
plt.show()       #show the plot

# In[4 ]:
%load_ext watermark

# python, ipython, packages, and machine characteristics
%watermark -v -m -p pandas,numpy,matplotlib.pyplot,watermark

# date
print (" ")
%watermark -u -n -t -z


text:
import itertools

import numpy as np
import networkx as nx
from typedecorator import params, returns

__all__ = [ 'similarity_flooding' ]

def similarity_flooding(G):
    """ A generalization of algorithm BVD04.

    Reference
    ---------
    [1] Melnik, Sergey, Hector Garcia-Molina, and Erhard Rahm.
        "Similarity Flooding: A Versatile Graph Matching Algorithm and
        Its Application to Schema Matching." ICDE, 2002.
    """
    pass

if __name__ == '__main__':
    pass

max_stars_repo_name: vishalbelsare/graphsim

max_issues_repo_name: syyunn/graphsim

max_forks_repo_name: syyunn/graphsim

reason: import numpy,import networkx


text:
"""Generate a sequence from a trained model."""

import os
import pickle
import tensorflow as tf
import numpy as np
import random
import string

from modeler.network import Network

def gen(savedir, lookupfile, datafile, author, num):
    """Generate a sequence from the model stored in the logdir."""

    with open('{}.meta'.format(datafile), 'rb') as file_handler:
        vocab_map, _, author_map = pickle.load(file_handler)
        vocab_size = len(vocab_map)
        author_size = len(author_map)

        index_map = {i: char for char, i in vocab_map.items()}

    with open(os.path.join(savedir, 'params.pkl'), 'rb') as file_handler:
        params = pickle.load(file_handler)

    sequences = tf.placeholder(tf.int32, [None, None])
    labels = tf.placeholder(tf.int32, [None, None])
    contexts = tf.placeholder(tf.int32, [None, None])
    network = Network(
        sequences,
        labels,
        contexts,
        vocab_size,
        author_size,
        **vars(params),
        training=False,
    )

    config = tf.ConfigProto(allow_soft_placement=True)
    with tf.Session(config=config) as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state(savedir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

            names = []

            seed = '▶'
            seq = [[vocab_map[seed]]] * num
            auth = [[author_map[author]]] * num
            out, = sess.run([network.out_node], {
                sequences: seq,
                contexts: auth,
            })
            for seq in out:
                name = ''.join([index_map.get(x) for x in seq])
                if '◀' in name:
                    names.append(name[:name.index('◀')])
                else:
                    names.append(name)

            return names
        else:
            raise Exception('Checkpoint not found')

max_stars_repo_name: crestonbunch/neural-namer

max_issues_repo_name: crestonbunch/neural-namer

max_forks_repo_name: crestonbunch/neural-namer


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

import librosa
import numpy as np
import pandas as pd
import tqdm

from parakeet.audio import LogMagnitude
from parakeet.datasets import LJSpeechMetaData
from parakeet.exps.waveflow.config import get_cfg_defaults

class Transform(object):
    def __init__(self, sample_rate, n_fft, win_length, hop_length, n_mels, fmin,
                 fmax):
        self.sample_rate = sample_rate
        self.n_fft = n_fft
        self.win_length = win_length
        self.hop_length = hop_length
        self.n_mels = n_mels
        self.fmin = fmin
        self.fmax = fmax

        self.spec_normalizer = LogMagnitude(min=1e-5)

    def __call__(self, example):
        wav_path, _, _ = example

        sr = self.sample_rate
        n_fft = self.n_fft
        win_length = self.win_length
        hop_length = self.hop_length
        n_mels = self.n_mels
        fmin = self.fmin
        fmax = self.fmax

        wav, loaded_sr = librosa.load(wav_path, sr=None)
        assert loaded_sr == sr, "sample rate does not match, resampling applied"

        # Pad audio to the right size.
        frames = int(np.ceil(float(wav.size) / hop_length))
        fft_padding = (n_fft - hop_length) // 2  # sound
        desired_length = frames * hop_length + fft_padding * 2
        pad_amount = (desired_length - wav.size) // 2

        if wav.size % 2 == 0:
            wav = np.pad(wav, (pad_amount, pad_amount), mode='reflect')
        else:
            wav = np.pad(wav, (pad_amount, pad_amount + 1), mode='reflect')

        # Normalize audio.
        wav = wav / np.abs(wav).max() * 0.999

        # Compute mel-spectrogram.
        # Turn center to False to prevent internal padding.
        spectrogram = librosa.core.stft(
            wav,
            hop_length=hop_length,
            win_length=win_length,
            n_fft=n_fft,
            center=False)
        spectrogram_magnitude = np.abs(spectrogram)

        # Compute mel-spectrograms.
        mel_filter_bank = librosa.filters.mel(
            sr=sr, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax)
        mel_spectrogram = np.dot(mel_filter_bank, spectrogram_magnitude)

        # log scale mel_spectrogram.
        mel_spectrogram = self.spec_normalizer.transform(mel_spectrogram)

        # Extract the center of audio that corresponds to mel spectrograms.
        audio = wav[fft_padding:-fft_padding]
        assert mel_spectrogram.shape[1] * hop_length == audio.size

        # there is no clipping here
        return audio, mel_spectrogram

def create_dataset(config, input_dir, output_dir):
    input_dir = Path(input_dir).expanduser()
    dataset = LJSpeechMetaData(input_dir)

    output_dir = Path(output_dir).expanduser()
    output_dir.mkdir(exist_ok=True)

    transform = Transform(config.sample_rate, config.n_fft, config.win_length,
                          config.hop_length, config.n_mels, config.fmin,
                          config.fmax)
    file_names = []

    for example in tqdm.tqdm(dataset):
        fname, _, _ = example
        base_name = os.path.splitext(os.path.basename(fname))[0]
        wav_dir = output_dir / "wav"
        mel_dir = output_dir / "mel"
        wav_dir.mkdir(exist_ok=True)
        mel_dir.mkdir(exist_ok=True)

        audio, mel = transform(example)
        np.save(str(wav_dir / base_name), audio)
        np.save(str(mel_dir / base_name), mel)

        file_names.append((base_name, mel.shape[-1], audio.shape[-1]))

    meta_data = pd.DataFrame.from_records(file_names)
    meta_data.to_csv(
        str(output_dir / "metadata.csv"), sep="\t", index=None, header=None)
    print("saved meta data in to {}".format(
        os.path.join(output_dir, "metadata.csv")))

    print("Done!")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="create dataset")
    parser.add_argument(
        "--config",
        type=str,
        metavar="FILE",
        help="extra config to overwrite the default config")
    parser.add_argument(
        "--input", type=str, help="path of the ljspeech dataset")
    parser.add_argument(
        "--output", type=str, help="path to save output dataset")
    parser.add_argument(
        "--opts",
        nargs=argparse.REMAINDER,
        help="options to overwrite --config file and the default config, passing in KEY VALUE pairs"
    )
    parser.add_argument(
        "-v", "--verbose", action="store_true", help="print msg")

    config = get_cfg_defaults()
    args = parser.parse_args()
    if args.config:
        config.merge_from_file(args.config)
    if args.opts:
        config.merge_from_list(args.opts)
    config.freeze()
    if args.verbose:
        print(config.data)
        print(args)

    create_dataset(config.data, args.input, args.output)


text:
import tensorflow as tf
import numpy as np
import scipy.io
import sys
import os.path
from src.convolutional import set_convolutional
from src.crops import extract_crops_z, extract_crops_x, pad_frame, resize_images
sys.path.append('../')

# the follow parameters *have to* reflect the design of the network to be imported
_conv_stride = np.array([2,1,1,1,1])
_filtergroup_yn = np.array([0,1,0,1,1], dtype=bool)
_bnorm_yn = np.array([1,1,1,1,0], dtype=bool)
_relu_yn = np.array([1,1,1,1,0], dtype=bool)
_pool_stride = np.array([2,1,0,0,0]) # 0 means no pool
_pool_sz = 3
_bnorm_adjust = True
assert len(_conv_stride) == len(_filtergroup_yn) == len(_bnorm_yn) == len(_relu_yn) == len(_pool_stride), ('These arrays of flags should have same length')
assert all(_conv_stride) >= True, ('The number of conv layers is assumed to define the depth of the network')
_num_layers = len(_conv_stride)

class siamfcGraph:
    def __init__(self,filename,pos_x_ph,pos_y_ph,z_sz_ph,x_sz0_ph,x_sz1_ph,x_sz2_ph):
        self.filename = filename
        self.pos_x_ph = pos_x_ph
        self.pos_y_ph = pos_y_ph
        self.z_sz_ph = z_sz_ph
        self.x_sz0_ph = x_sz0_ph
        self.x_sz1_ph = x_sz1_ph
        self.x_sz2_ph = x_sz2_ph

def build_tracking_graph(final_score_sz, design, env):
    # Make a queue of file names
    # filename_queue = tf.train.string_input_producer(frame_name_list, shuffle=False, capacity=num_frames)
    # image_reader = tf.WholeFileReader()
    # # Read a whole file from the queue
    # image_name, image_file = image_reader.read(filename_queue)
    graph1 = tf.Graph()
    with graph1.as_default():
        filename = tf.placeholder(tf.string, [], name='filename')
        pos_x_ph = tf.placeholder(tf.float64, [], name='pos_x_ph')
        pos_y_ph = tf.placeholder(tf.float64, [], name='pos_y_ph')
        z_sz_ph = tf.placeholder(tf.float64, [], name='z_sz_ph')
        x_sz0_ph = tf.placeholder(tf.float64, [], name='x_sz0_ph')
        x_sz1_ph = tf.placeholder(tf.float64, [], name='x_sz1_ph')
        x_sz2_ph = tf.placeholder(tf.float64, [], name='x_sz2_ph')
        sfcg = siamfcGraph(filename,pos_x_ph,pos_y_ph,z_sz_ph,x_sz0_ph,x_sz1_ph,x_sz2_ph)
        image_file = tf.read_file(filename)
        # Decode the image as a JPEG file, this will turn it into a Tensor
        image = tf.image.decode_png(image_file)
        image = 255.0 * tf.image.convert_image_dtype(image, tf.float32)
        frame_sz = tf.shape(image)
        # used to pad the crops
        if design.pad_with_image_mean:
            avg_chan = tf.reduce_mean(image, axis=(0,1), name='avg_chan')
        else:
            avg_chan = None
        # pad with if necessary
        frame_padded_z, npad_z = pad_frame(image, frame_sz, pos_x_ph, pos_y_ph, z_sz_ph, avg_chan)
        frame_padded_z = tf.cast(frame_padded_z, tf.float32)
        # extract tensor of z_crops
        z_crops = extract_crops_z(frame_padded_z, npad_z, pos_x_ph, pos_y_ph, z_sz_ph, design.exemplar_sz)
        frame_padded_x, npad_x = pad_frame(image, frame_sz, pos_x_ph, pos_y_ph, x_sz2_ph, avg_chan)
        frame_padded_x = tf.cast(frame_padded_x, tf.float32)
        # extract tensor of x_crops (3 scales)
        x_crops = extract_crops_x(frame_padded_x, npad_x, pos_x_ph, pos_y_ph, x_sz0_ph, x_sz1_ph, x_sz2_ph, design.search_sz)
        # use crops as input of (MatConvnet imported) pre-trained fully-convolutional Siamese net
        template_z, templates_x, p_names_list, p_val_list = _create_siamese(os.path.join(env.root_pretrained,design.net), x_crops, z_crops)
        template_z = tf.squeeze(template_z)
        templates_z = tf.stack([template_z, template_z, template_z])
        # compare templates via cross-correlation
        scores = _match_templates(templates_z, templates_x, p_names_list, p_val_list)
        # upsample the score maps
        scores_up = tf.image.resize_images(scores, [final_score_sz, final_score_sz],
            method=tf.image.ResizeMethod.BICUBIC, align_corners=True)
    return filename, image, templates_z, scores_up, graph1, sfcg

# import pretrained Siamese network from matconvnet
def _create_siamese(net_path, net_x, net_z):
    # read mat file from net_path and start TF Siamese graph from placeholders X and Z
    params_names_list, params_values_list = _import_from_matconvnet(net_path)

    # loop through the flag arrays and re-construct network, reading parameters of conv and bnorm layers
    for i in xrange(_num_layers):
        print '> Layer '+str(i+1)
        # conv
        conv_W_name = _find_params('conv'+str(i+1)+'f', params_names_list)[0]
        conv_b_name = _find_params('conv'+str(i+1)+'b', params_names_list)[0]
        print '\t\tCONV: setting '+conv_W_name+' '+conv_b_name
        print '\t\tCONV: stride '+str(_conv_stride[i])+', filter-group '+str(_filtergroup_yn[i])
        conv_W = params_values_list[params_names_list.index(conv_W_name)]
        conv_b = params_values_list[params_names_list.index(conv_b_name)]
        # batchnorm
        if _bnorm_yn[i]:
            bn_beta_name = _find_params('bn'+str(i+1)+'b', params_names_list)[0]
            bn_gamma_name = _find_params('bn'+str(i+1)+'m', params_names_list)[0]
            bn_moments_name = _find_params('bn'+str(i+1)+'x', params_names_list)[0]
            print '\t\tBNORM: setting '+bn_beta_name+' '+bn_gamma_name+' '+bn_moments_name
            bn_beta = params_values_list[params_names_list.index(bn_beta_name)]
            bn_gamma = params_values_list[params_names_list.index(bn_gamma_name)]
            bn_moments = params_values_list[params_names_list.index(bn_moments_name)]
            bn_moving_mean = bn_moments[:,0]
            bn_moving_variance = bn_moments[:,1]**2 # saved as std in matconvnet
        else:
            bn_beta = bn_gamma = bn_moving_mean = bn_moving_variance = []

        # set up conv "block" with bnorm and activation
        net_x = set_convolutional(net_x, conv_W, np.swapaxes(conv_b,0,1), _conv_stride[i], \
                            bn_beta, bn_gamma, bn_moving_mean, bn_moving_variance, \
                            filtergroup=_filtergroup_yn[i], batchnorm=_bnorm_yn[i], activation=_relu_yn[i], \
                            scope='conv'+str(i+1), reuse=False)

        # notice reuse=True for Siamese parameters sharing
        net_z = set_convolutional(net_z, conv_W, np.swapaxes(conv_b,0,1), _conv_stride[i], \
                            bn_beta, bn_gamma, bn_moving_mean, bn_moving_variance, \
                            filtergroup=_filtergroup_yn[i], batchnorm=_bnorm_yn[i], activation=_relu_yn[i], \
                            scope='conv'+str(i+1), reuse=True)

        # add max pool if required
        if _pool_stride[i]>0:
            print '\t\tMAX-POOL: size '+str(_pool_sz)+ ' and stride '+str(_pool_stride[i])
            net_x = tf.nn.max_pool(net_x, [1,_pool_sz,_pool_sz,1], strides=[1,_pool_stride[i],_pool_stride[i],1], padding='VALID', name='pool'+str(i+1))
            net_z = tf.nn.max_pool(net_z, [1,_pool_sz,_pool_sz,1], strides=[1,_pool_stride[i],_pool_stride[i],1], padding='VALID', name='pool'+str(i+1))

    print

    return net_z, net_x, params_names_list, params_values_list

def _import_from_matconvnet(net_path):
    mat = scipy.io.loadmat(net_path)
    net_dot_mat = mat.get('net')
    # organize parameters to import
    params = net_dot_mat['params']
    params = params[0][0]
    params_names = params['name'][0]
    params_names_list = [params_names[p][0] for p in xrange(params_names.size)]
    params_values = params['value'][0]
    params_values_list = [params_values[p] for p in xrange(params_values.size)]
    return params_names_list, params_values_list

# find all parameters matching the codename (there should be only one)
def _find_params(x, params):
    matching = [s for s in params if x in s]
    assert len(matching)==1, ('Ambiguous param name found')
    return matching

def _match_templates(net_z, net_x, params_names_list, params_values_list):
    # finalize network
    # z, x are [B, H, W, C]
    net_z = tf.transpose(net_z, perm=[1,2,0,3])
    net_x = tf.transpose(net_x, perm=[1,2,0,3])
    # z, x are [H, W, B, C]
    Hz, Wz, B, C = tf.unstack(tf.shape(net_z))
    Hx, Wx, Bx, Cx = tf.unstack(tf.shape(net_x))
    # assert B==Bx, ('Z and X should have same Batch size')
    # assert C==Cx, ('Z and X should have same Channels number')
    net_z = tf.reshape(net_z, (Hz, Wz, B*C, 1))
    net_x = tf.reshape(net_x, (1, Hx, Wx, B*C))
    net_final = tf.nn.depthwise_conv2d(net_x, net_z, strides=[1,1,1,1], padding='VALID')
    # final is [1, Hf, Wf, BC]
    net_final = tf.concat(tf.split(net_final, 3, axis=3), axis=0)
    # final is [B, Hf, Wf, C]
    net_final = tf.expand_dims(tf.reduce_sum(net_final, axis=3), axis=3)
    # final is [B, Hf, Wf, 1]
    if _bnorm_adjust:
        bn_beta = params_values_list[params_names_list.index('fin_adjust_bnb')]
        bn_gamma = params_values_list[params_names_list.index('fin_adjust_bnm')]
        bn_moments = params_values_list[params_names_list.index('fin_adjust_bnx')]
        bn_moving_mean = bn_moments[:,0]
        bn_moving_variance = bn_moments[:,1]**2
        net_final = tf.layers.batch_normalization(net_final, beta_initializer=tf.constant_initializer(bn_beta),
                                                gamma_initializer=tf.constant_initializer(bn_gamma),
                                                moving_mean_initializer=tf.constant_initializer(bn_moving_mean),
                                                moving_variance_initializer=tf.constant_initializer(bn_moving_variance),
                                                training=False, trainable=False)

    return net_final

reason: import numpy,import scipy


text:
'''20211216: burn point locations from a csv into a raster mask..
..use this to convert csv groundref locations into format used by TRU students!

* input csv file must include columns named: row, col
* raster file provided is only used to read expected image dimensions from the header.'''
from misc import *
import matplotlib
import numpy
args = sys.argv

# instructions to run
if len(args) < 2:
    err('usage:\n\tpython3 csv_simple_rasterize_onto [csv file name] [raster filename]')

csv_fn, fn = args[1], args[2]  # csv and raster input file names
for i in [fn, csv_fn]:
    assert_exists(i)  # check file exists
hdr = hdr_fn(args[2]) # check raster header file exists
samples, lines, bands = [int(i) for i in read_hdr(hdr)]  # read raster image dims
npx = samples * lines # number of image pixels

flag = csv_fn[:-4].split('_')[-1]
ofn = fn + '_mask_' + flag + '.bin'
print('+w', ofn)  # output mask file location to write
ohn = ofn[:-4] + '.hdr'  # output header file name to write

fields, data = read_csv(args[1]) # open the csv data
for i in ['row', 'col']:  # check for required fields
    if i not in fields:
        err('required field: ' + i)
N = len(data[0])  # number of csv records
f_i = {fields[i]: i for i in range(len(fields))} # col index lookup

# rasterize the locations
dat = numpy.zeros(npx)
for i in range(N):
    row, col = [int(x) for x in
                [data[f_i['row']][i], data[f_i['col']][i]]]
    print('  row,col=', row, col)
    dat[row * samples + col] = 1.  # burn in the point location
write_binary(dat, ofn)  # write output raster mask

write_hdr(ohn, samples, lines, 1)  # write output header

max_stars_repo_name: breadcrumbbuilds/bcws-psu-research

max_issues_repo_name: breadcrumbbuilds/bcws-psu-research

max_forks_repo_name: breadcrumbbuilds/bcws-psu-research


text:
# HW2 Harris Detector implement
# HarrisDetector class
# Author : Yuanfan Guo
# Rev.0 2018.10.8
# Rev.1 2018.10.9
import cv2
import numpy as np
import matplotlib.pyplot as plt

class HarrisDetector():
    def __init__(self,ThresHold = 0.02, WindowSize = 5 , K=0.05, Blocksize = 5):
        self.ThresHold = ThresHold      #Above threshold will count as corner
        self.K = K                      #K is Empirical Constant, 0.04~0.06
        self.WindowSize = WindowSize    #The size of the window
        self.Blocksize = Blocksize            #The Blocksize (usually 3 or 5)
    def SetPara(self,ThresHold,WindowSize,K,Blocksize):
        #Set ThresHold, WindowSize and K and Blocksize of detector

        self.ThresHold = ThresHold
        self.K = K
        self.WindowSize = WindowSize
        self.Blocksize = Blocksize
    def DetectCorner(self,InImg):
        #InImg : image matrix (gray or color)
        #Detect corners in an img and return Corner list and new image
        Result = InImg.copy()
        gray_img = cv2.cvtColor(InImg, cv2.COLOR_BGR2GRAY)

        img = np.array(gray_img,dtype=np.float64)
        CornerResponse = np.zeros([img.shape[0],img.shape[1]])
        #Calculate Ixx Ixy and Iyy
        Ixx,Ixy,Iyy = self.CalGradient(img)

        Half_w = int(self.WindowSize/2)
        for x in range(Half_w, img.shape[0]-Half_w):
            for y in range(Half_w , img.shape[1]-Half_w):

                ##Compute sums of derivatives

                M11 = sum(Ixx[x - Half_w: x + Half_w + 1, y - Half_w: y + Half_w + 1].ravel())
                M12 = sum(Ixy[x - Half_w: x + Half_w + 1, y - Half_w: y + Half_w + 1].ravel())
                M22 = sum(Iyy[x - Half_w: x + Half_w + 1, y - Half_w: y + Half_w + 1].ravel())

                ##Get Eigen Value and R
                Det = M11*M22 - M12**2
                Trace = M11 + M22

                R = Det - self.K*(Trace**2)
                CornerResponse[x][y] = R
        #Find Local max  Value and plot corner
        Cornerlist = []
        CornerMax = max(CornerResponse.ravel())
        HalfBlcok = int(self.Blocksize/2)
        for x in range(HalfBlcok, img.shape[0]-HalfBlcok):
            for y in range(HalfBlcok , img.shape[1]-HalfBlcok):
                LocalMax = max(CornerResponse[x - HalfBlcok:x+HalfBlcok+1,y-HalfBlcok:y+HalfBlcok+1].ravel())
                #only the Localmax value count as corner
                if (CornerResponse[x, y] >= CornerMax * self.ThresHold) and (CornerResponse[x, y] == LocalMax):
                    Cornerlist.append((x, y))
                    #Plot the Corner point
                    Result[x - HalfBlcok:x+HalfBlcok+1,y-HalfBlcok:y+HalfBlcok+1] = [0,0,255]
        return Result, Cornerlist

    def CalGradient(self,img):
        dy, dx = np.gradient(img)
        Ixx = dx**2
        Iyy = dy**2
        Ixy = dx*dy

        return Ixx,Ixy,Iyy


text:
"""
General module for integral generation and access.
"""
try:
    from pyquante2.ctwo import ERI_hgp as ERI
except:
    print("Couldn't find cython int routine")
    from pyquante2.ints.hgp import ERI_hgp as ERI

from pyquante2.clibint import Libint_ERI

try:
    from pyquante2.cone import S,T,V
except:
    print("Couldn't find cython int routine")
    from pyquante2.ints.one import S,T,V

from pyquante2.utils import pairs
from itertools import product
import numpy as np

# This is the old part of the code. It has now been replaced with the one
#  below it, which takes 8x as much space, but is significantly faster.
#  It's also more elegant code.
class twoe_integrals_compressed(object):
    """
    >>> from pyquante2.geo.samples import h
    >>> from pyquante2.basis.basisset import basisset
    >>> bfs = basisset(h,'sto-3g')
    >>> twoe_integrals_compressed(bfs)
    array([ 0.77460594])
    """
    def __init__(self,bfs):
        nbf = self.nbf = len(bfs)
        self.totlen = nbf*(nbf+1)*(nbf*nbf+nbf+2)/8
        self._2e_ints = np.empty(self.totlen,'d')

        for i,j,k,l in iiterator(nbf):
            self._2e_ints[iindex(i,j,k,l)] = ERI(bfs[i],bfs[j],bfs[k],bfs[l])

    def __getitem__(self,pos): return self._2e_ints[iindex(*pos)]
    def __repr__(self): return repr(self._2e_ints)

    def fetch_2jk(self,i,j):
        nbf = self.nbf
        temp = np.empty(nbf**2,'d')
        kl = 0
        for k,l in product(range(nbf),repeat=2):
            temp[kl] = 2*self[i,j,k,l]-self[i,k,j,l]
            kl += 1
        return temp

    def fetch_j(self,i,j):
        nbf = self.nbf
        temp = np.empty(nbf**2,'d')
        kl = 0
        for k,l in product(range(nbf),repeat=2):
            temp[kl] = self[i,j,k,l]
            kl += 1
        return temp

    def fetch_k(self,i,j):
        nbf = self.nbf
        temp = np.empty(nbf**2,'d')
        kl = 0
        for k,l in product(range(nbf),repeat=2):
            temp[kl] = self[i,k,j,l]
            kl += 1
        return temp

    def make_operator(self,D,fetcher):
        nbf = self.nbf
        D1 = np.reshape(D,(nbf*nbf,))
        G = np.empty((nbf,nbf),'d')
        for i,j in pairs(range(nbf)):
            temp = fetcher(i,j) # replace temp with fetcher()
            G[i,j] = G[j,i] = np.dot(D1,temp)
        return G

    def get_2jk(self,D): return self.make_operator(D,self.fetch_2jk)
    def get_j(self,D): return self.make_operator(D,self.fetch_j)
    def get_k(self,D): return self.make_operator(D,self.fetch_k)

class twoe_integrals(object):
    """
    >>> from pyquante2.geo.samples import h
    >>> from pyquante2.basis.basisset import basisset
    >>> bfs = basisset(h,'sto-3g')
    >>> twoe_integrals(bfs)
    array([ 0.77460594])
    """
    def __init__(self,bfs):
        nbf = self.nbf = len(bfs)
        self._2e_ints = np.empty((nbf,nbf,nbf,nbf),'d')
        ints = self._2e_ints

        for i,j,k,l in iiterator(nbf):
            ints[i,j,k,l] = ints[j,i,k,l] = ints[i,j,l,k] = ints[j,i,l,k] = \
                            ints[k,l,i,j] = ints[l,k,i,j] = ints[k,l,j,i] = \
                            ints[l,k,j,i] = ERI(bfs[i],bfs[j],bfs[k],bfs[l])

    def __getitem__(self,*args): return self._2e_ints.__getitem__(*args)
    def __repr__(self): return repr(self._2e_ints.ravel())

    def transform(self,c): return np.einsum('aI,bJ,cK,dL,abcd->IJKL',c,c,c,c,self._2e_ints)
    def transform_mp2(self,c,nocc):
        transform_1 = np.einsum('aI, abcd->Ibcd', c[:,:nocc], self._2e_ints)
        transform_2 = np.einsum('bJ, abcd->aJcd', c[:,nocc:], transform_1)
        transform_3 = np.einsum('cK, abcd->abKd', c[:,:nocc], transform_2)
        transform_4 = np.einsum('dL, abcd->abcL', c[:,nocc:], transform_3)
        return transform_4
        # This is very slow
        # return np.einsum('aI,bJ,cK,dL,abcd->IJKL',c[:,:nocc],c,c[:,:nocc],c,self._2e_ints)

    # This turns out to be slower:
    #def get_j(self,D): return np.einsum('ij,ijkl->kl',D,self._2e_ints)
    def get_j(self,D): return np.einsum('kl,ijkl->ij',D,self._2e_ints)
    def get_k(self,D): return np.einsum('ij,ikjl->kl',D,self._2e_ints)
    def get_2jk(self,D): return 2*self.get_j(D)-self.get_k(D)

class libint_twoe_integrals(twoe_integrals):
    """
    >>> from pyquante2.geo.samples import h
    >>> from pyquante2.basis.basisset import basisset
    >>> bfs = basisset(h,'sto-3g')
    >>> twoe_integrals(bfs)
    array([ 0.77460594])
    """

    def __init__(self, bfs):
        nbf = self.nbf = len(bfs)
        self._2e_ints = np.empty((nbf,nbf,nbf,nbf),'d')
        ints = self._2e_ints

        ints_jikl = np.transpose(ints, axes=(1,0,2,3))
        ints_ijlk = np.transpose(ints, axes=(0,1,3,2))
        ints_jilk = np.transpose(ints, axes=(1,0,3,2))
        ints_klij = np.transpose(ints, axes=(2,3,0,1))
        ints_lkij = np.transpose(ints, axes=(3,2,0,1))
        ints_klji = np.transpose(ints, axes=(2,3,1,0))
        ints_lkji = np.transpose(ints, axes=(3,2,1,0))

        for i,j,k,l in libint_iterator(bfs):
        # TODO: sort i,j,k,l so did't need permutation
            shell = self.Permutable_ERI(bfs[i], bfs[j], bfs[k], bfs[l])
            ints[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_jikl[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_ijlk[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_jilk[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_klij[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_lkij[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_klji[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = \
            ints_lkji[i:i+bfs[i].nfunc,j:j+bfs[j].nfunc,k:k+bfs[k].nfunc,l:l+bfs[l].nfunc] = shell

    def Permutable_ERI(self, cgbf_a, cgbf_b, cgbf_c, cgbf_d):
        swap_ab = sum(cgbf_b.powers) > sum(cgbf_a.powers)
        swap_cd = sum(cgbf_d.powers) > sum(cgbf_c.powers)
        swap_abcd = sum(cgbf_a.powers) + sum(cgbf_b.powers) > sum(cgbf_c.powers) + sum(cgbf_d.powers)

        if swap_ab:
            cgbf_a, cgbf_b = cgbf_b, cgbf_a
        if swap_cd:
            cgbf_c, cgbf_d = cgbf_d, cgbf_c
        if swap_abcd:
            cgbf_a, cgbf_b, cgbf_c, cgbf_d = cgbf_c, cgbf_d, cgbf_a, cgbf_b

        shell = Libint_ERI(cgbf_a, cgbf_b, cgbf_c, cgbf_d)

        if swap_abcd:
            shell = np.swapaxes(shell,0,2)
            shell = np.swapaxes(shell,1,3)
        if swap_cd:
            shell = np.swapaxes(shell,2,3)
        if swap_ab:
            shell = np.swapaxes(shell,0,1)
        return shell

class onee_integrals(object):
    """
    >>> from pyquante2.geo.samples import h
    >>> from pyquante2.basis.basisset import basisset
    >>> bfs = basisset(h,'sto-3g')
    >>> i1 = onee_integrals(bfs,h)
    >>> i1.S
    array([[ 1.]])
    >>> i1.T
    array([[ 0.76003188]])
    >>> i1.V
    array([[-1.22661373]])
    """
    def __init__(self, bfs):
        nbf = self.nbf = len(bfs)
        self.S = np.empty((nbf,nbf),'d')
        self.T = np.empty((nbf,nbf),'d')
        self.V = np.empty((nbf,nbf),'d')
        for i,j in pairs(range(nbf)):
            ibf,jbf = bfs[i],bfs[j]
            self.S[i,j] = self.S[j,i] = S(ibf,jbf)
            self.T[i,j] = self.T[j,i] = T(ibf,jbf)
            self.V[i,j] = self.V[j,i] = sum(at.Z*V(ibf,jbf,at.r) for at in bfs.atoms)
        return

def iiterator(nbf):
    """
    Iterator over n**4 integral indices
    >>> list(iiterator(2))
    [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 1), (1, 1, 1, 1)]
    """
    for i,j in pairs(range(nbf)):
        ij = i*(i+1)/2+j
        for k,l in pairs(range(nbf)):
            kl = k*(k+1)/2+l
            if ij <= kl:
                yield i,j,k,l
    return

def libint_iterator(bfs):
    first_bfs = [i for i, b in enumerate(bfs) if b.is_first]
    for i,j in pairs(first_bfs):
        ij = i*(i+1)/2+j
        for k,l in pairs(first_bfs):
            kl = k*(k+1)/2+l
            if ij <= kl:
                yield i,j,k,l
    return

def iindex(i,j,k,l):
    """
    Indexing into the integral array
    >>> iindex(0,0,0,0)
    0
    >>> iindex(1,0,0,0)
    1
    >>> iindex(0,1,0,0)
    1
    >>> iindex(0,0,1,0)
    1
    >>> iindex(0,0,0,1)
    1
    >>> iindex(1,0,1,0)
    2
    >>> iindex(0,1,0,1)
    2
    >>> iindex(1,1,0,0)
    3
    >>> iindex(0,0,1,1)
    3
    """
    if i<j: i,j = j,i
    if k<l: k,l = l,k
    ij = (i*(i+1))//2+j
    kl = (k*(k+1))//2+l
    if ij < kl: ij,kl = kl,ij
    return (ij*(ij+1))//2+kl

if __name__ == '__main__':
    import doctest; doctest.testmod()


text:
import copy
import numpy as np
import modeling.modelcollection as mc
import basis.robot_math as rm
import robotsim._kinematics.jlchain as jl
import robotsim._kinematics.collisionchecker as cc

class GripperInterface(object):

    def __init__(self, pos=np.zeros(3), rotmat=np.eye(3), cdmesh_type='aabb', name='yumi_gripper'):
        self.name = name
        self.pos = pos
        self.rotmat = rotmat
        self.cdmesh_type = cdmesh_type # aabb, convexhull, or triangles
        # joints
        # - coupling - No coupling by default
        self.coupling = jl.JLChain(pos=self.pos, rotmat=self.rotmat, homeconf=np.zeros(0), name='coupling')
        self.coupling.jnts[1]['loc_pos'] = np.array([0, 0, .0])
        self.coupling.lnks[0]['name'] = 'coupling_lnk0'
        # toggle on the following part to assign an explicit mesh model to a coupling
        # self.coupling.lnks[0]['meshfile'] = os.path.join(this_dir, "meshes", "xxx.stl")
        # self.coupling.lnks[0]['rgba'] = [.2, .2, .2, 1]
        self.coupling.reinitialize()
        # jaw center
        self.jaw_center_loc_pos = np.zeros(3)
        self.jaw_center_loc_rotmat = np.eye(3)
        # jaw width
        self.jaw_width_rng = [0.0, 5.0]
        # collision detection
        self.cc = None
        # cd mesh collection for precise collision checking
        self.cdmesh_collection = mc.ModelCollection()

    def is_collided(self, obstacle_list=[], otherrobot_list=[]):
        """
        Interface for "is cdprimit collided", must be implemented in child class
        :param obstacle_list:
        :param otherrobot_list:
        :return:
        author: weiwei
        date: 20201223
        """
        return_val =  self.cc.is_collided(obstacle_list=obstacle_list, otherrobot_list=otherrobot_list)
        return return_val

    def is_mesh_collided(self, objcm_list=[], toggle_debug=False):
        for i, cdelement in enumerate(self.all_cdelements):
            pos = cdelement['gl_pos']
            rotmat = cdelement['gl_rotmat']
            self.cdmesh_collection.cm_list[i].set_pos(pos)
            self.cdmesh_collection.cm_list[i].set_rotmat(rotmat)
            iscollided, collided_points = self.cdmesh_collection.cm_list[i].is_mcdwith(objcm_list, True)
            if iscollided:
                if toggle_debug:
                    print(self.cdmesh_collection.cm_list[i].get_homomat())
                    self.cdmesh_collection.cm_list[i].show_cdmesh()
                    for objcm in objcm_list:
                        objcm.show_cdmesh()
                    for point in collided_points:
                        import modeling.geometricmodel as gm
                        gm.gen_sphere(point, radius=.001).attach_to(base)
                    print("collided")
                return True
        return False

    def fix_to(self, pos, rotmat):
        raise NotImplementedError

    def fk(self, motion_val):
        raise NotImplementedError

    def jaw_to(self, jaw_width):
        raise NotImplementedError

    def get_jawwidth(self):
        raise NotImplementedError

    def grip_at(self, gl_jaw_center, gl_hndz, gl_hndx, jaw_width):
        hnd_rotmat = np.eye(3)
        hnd_rotmat[:, 2] = rm.unit_vector(gl_hndz)
        hnd_rotmat[:, 0] = rm.unit_vector(gl_hndx)
        hnd_rotmat[:, 1] = np.cross(hnd_rotmat[:3, 2], hnd_rotmat[:3, 0])
        hnd_pos = gl_jaw_center - hnd_rotmat.dot(self.jaw_center_loc_pos)
        self.fix_to(hnd_pos, hnd_rotmat)
        self.jaw_to(jaw_width)
        return [jaw_width, gl_jaw_center, hnd_pos, hnd_rotmat]

    def show_cdprimit(self):
        self.cc.show_cdprimit()

    def unshow_cdprimit(self):
        self.cc.unshow_cdprimit()

    def show_cdmesh(self):
        for i, cdelement in enumerate(self.cc.all_cdelements):
            pos = cdelement['gl_pos']
            rotmat = cdelement['gl_rotmat']
            self.cdmesh_collection.cm_list[i].set_pos(pos)
            self.cdmesh_collection.cm_list[i].set_rotmat(rotmat)
        self.cdmesh_collection.show_cdmesh()

    def unshow_cdmesh(self):
        self.cdmesh_collection.unshow_cdmesh()

    def gen_stickmodel(self,
                       tcp_jntid=None,
                       tcp_loc_pos=None,
                       tcp_loc_rotmat=None,
                       toggle_tcpcs=False,
                       toggle_jntscs=False,
                       toggle_connjnt=False,
                       name='yumi_gripper_stickmodel'):
        raise NotImplementedError

    def gen_meshmodel(self,
                      tcp_jntid=None,
                      tcp_loc_pos=None,
                      tcp_loc_rotmat=None,
                      toggle_tcpcs=False,
                      toggle_jntscs=False,
                      rgba=None,
                      name='yumi_gripper_meshmodel'):
        raise NotImplementedError

    def enable_cc(self):
        self.cc = cc.CollisionChecker("collision_checker")

    def disable_cc(self):
        """
        clear pairs and nodepath
        :return:
        """
        for cdelement in self.cc.all_cdelements:
            cdelement['cdprimit_childid'] = -1
        self.cc = None

    def copy(self):
        self_copy = copy.deepcopy(self)
        # deepcopying colliders are problematic, I have to update it manually
        if self.cc is not None:
            for child in self_copy.cc.np.getChildren():
                self_copy.cc.ctrav.addCollider(child, self_copy.cc.chan)
        return self_copy


text:
# (c) 2019 by Authors
# This file is a part of centroFlye program.
# Released under the BSD license (see LICENSE file)

import argparse
from collections import Counter
from string import ascii_uppercase, ascii_lowercase

import numpy as np
import pandas as pd

from utils.bio import read_bio_seqs, compress_homopolymer

class SD_Report:
    class SD_Record:
        def __init__(self, r_id, pandas_df,
                     max_gap, mean_monomer_len, gap_symb):
            self.r_id = r_id
            self.monomers = pandas_df.monomer.to_list()
            self.r_st = pandas_df.r_st.to_list()
            self.r_en = pandas_df.r_en.to_list()
            self.triples = list(zip(self.monomers, self.r_st, self.r_en))
            self.score = pandas_df.score.to_list()
            self.alt_call = pandas_df.alt_call.to_list()

            self.string = \
                [self.monomers[0][0] if self.alt_call[0] == 'None'
                 else gap_symb]
            self.gaps = []
            for tr1, tr2, rel2 in \
                    zip(self.triples[:-1],
                        self.triples[1:],
                        self.alt_call[1:]):
                gap_len = tr2[1] - tr1[2]
                if gap_len > max_gap:
                    self.gaps.append((tr1[2], tr2[1]))
                    self.string.append(gap_symb *
                                       int(round(gap_len / mean_monomer_len)))
                if rel2 == 'None':
                    self.string.append(tr2[0])
                else:
                    self.string.append(gap_symb)

            self.string = ''.join(self.string)
            perc_lower_case = np.mean([c.islower() for c in self.string
                                       if c.lower() != c.upper()])
            if perc_lower_case > 0.5:
                self.string = self.string[::-1].swapcase()
                self.strand = '-'
            else:
                self.strand = '+'

            self.string = self.string.strip(gap_symb)
            self.split_strings = self.string.split(gap_symb)
            self.split_strings = [s for s in self.split_strings if len(s)]

    def __init__(self, SD_report_fn, monomers_fn, max_gap=100, gap_symb='?'):
        self.gap_symb = gap_symb
        monomers = read_bio_seqs(monomers_fn)
        mean_monomer_len = \
            np.mean([len(monomer) for monomer in monomers.values()])

        self.monomer_names_map = {}
        for monomer_name, ucode, lcode in \
                zip(monomers.keys(), ascii_uppercase, ascii_lowercase):
            self.monomer_names_map[monomer_name] = ucode
            self.monomer_names_map[monomer_name + "'"] = lcode

        self.records = {}
        df = pd.read_csv(SD_report_fn, sep='\t',
                         header=None,
                         names=['r_id', 'monomer',
                                'r_st', 'r_en',
                                'score',
                                'alt_call',
                                'alt_score'
                                ])
        df.monomer = df.monomer.apply(lambda x: self.monomer_names_map[x])
        df = df.groupby('r_id')
        for r_id, group in df:
            self.records[r_id] = \
                self.SD_Record(r_id, group,
                               max_gap=max_gap,
                               mean_monomer_len=mean_monomer_len,
                               gap_symb=self.gap_symb)

    def get_monomer_strings(self):
        return {r_id: record.string for r_id, record in self.records.items()
                if len(record.string)}

def get_ngap_symbols(monostrings, compr_hmp=False, gap_symb='?'):
    cnt = 0
    for monostring in monostrings.values():
        monostring = monostring.tostring()
        if compr_hmp:
            monostring = compress_homopolymer(monostring)
        cnt += Counter(monostring)[gap_symb]
    return cnt

def get_stats(monostrings, verbose=False, return_stats=True):
    stats = {}
    monostrings_lens = [len(monostr) for r_id, monostr in monostrings.items()]
    stats['ntranslations'] = len(monostrings_lens)
    stats['min_len'] = np.min(monostrings_lens)
    stats['max_len'] = np.max(monostrings_lens)
    stats['mean_len'] = np.mean(monostrings_lens)
    stats['tot_len'] = np.sum(monostrings_lens)
    stats['ngaps'] = get_ngap_symbols(monostrings)
    stats['pgaps'] = stats['ngaps'] / stats['tot_len']
    stats['ngap_runs'] = get_ngap_symbols(monostrings, compr_hmp=True)

    if verbose:
        print(f'Number of translations: {stats["ntranslations"]}')
        print(f'Min length = {stats["min_len"]}')
        print(f'Mean length = {stats["mean_len"]}')
        print(f'Max length = {stats["max_len"]}')
        print(f'Total length = {stats["tot_len"]}')

        print(f'#(%) Gap symbols = {stats["ngaps"]} ({stats["pgaps"]})')
        print(f'#Gap runs = {stats["ngap_runs"]}')
    if return_stats:
        return stats

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--input", help="Report of SD", required=True)
    parser.add_argument("-m",
                        "--monomers",
                        help="Fasta with monomers",
                        required=True)
    params = parser.parse_args()
    sd_report = SD_Report(params.input, params.monomers)
    get_stats(monostrings=sd_report.get_monomer_strings(),
              verbose=True, return_stats=False)

if __name__ == "__main__":
    main()

max_stars_repo_name: seryrzu/centroFlye

max_issues_repo_name: seryrzu/centroFlye

max_forks_repo_name: seryrzu/centroFlye


text:
#! /usr/bin/python

import numpy
import pyaudio
import math

class ToneGenerator(object):

    def __init__(self, samplerate=44100, frames_per_buffer=4410):
        self.p = pyaudio.PyAudio()
        self.samplerate = samplerate
        self.frames_per_buffer = frames_per_buffer
        self.streamOpen = False

    def sinewave(self):
        if self.buffer_offset + self.frames_per_buffer - 1 > self.x_max:
            # We don't need a full buffer or audio so pad the end with 0's
            xs = numpy.arange(self.buffer_offset,
                              self.x_max)
            tmp = self.amplitude * numpy.sin(xs * self.omega)
            out = numpy.append(tmp,
                               numpy.zeros(self.frames_per_buffer - len(tmp)))
        else:
            xs = numpy.arange(self.buffer_offset,
                              self.buffer_offset + self.frames_per_buffer)
            out = self.amplitude * numpy.sin(xs * self.omega)
        self.buffer_offset += self.frames_per_buffer
        return out

    def callback(self, in_data, frame_count, time_info, status):
        if self.buffer_offset < self.x_max:
            data = self.sinewave().astype(numpy.float32)
            return (data.tostring(), pyaudio.paContinue)
        else:
            return (None, pyaudio.paComplete)

    def is_playing(self):
        if self.stream.is_active():
            return True
        else:
            if self.streamOpen:
                self.stream.stop_stream()
                self.stream.close()
                self.streamOpen = False
            return False

    def play(self, frequency, duration, amplitude):
        self.omega = float(frequency) * (math.pi * 2) / self.samplerate
        self.amplitude = amplitude
        self.buffer_offset = 0
        self.streamOpen = True
        self.x_max = math.ceil(self.samplerate * duration) - 1
        self.stream = self.p.open(format=pyaudio.paFloat32,
                                  output_device_index=8,
                                  channels=1,
                                  rate=self.samplerate,
                                  output=True,
                                  frames_per_buffer=self.frames_per_buffer,
                                  stream_callback=self.callback)

###############################################################################
#                                 Usage Example                               #
###############################################################################

generator = ToneGenerator()

frequency_start = 512        # Frequency to start the sweep from
frequency_end = 512       # Frequency to end the sweep at
num_frequencies = 1       # Number of frequencies in the sweep
amplitude = 0.50            # Amplitude of the waveform
step_duration = 10        # Time (seconds) to play at each step

for frequency in numpy.logspace(math.log(frequency_start, 10),
                                math.log(frequency_end, 10),
                                num_frequencies):

    print("Playing tone at {0:0.2f} Hz".format(frequency))
    generator.play(frequency, step_duration, amplitude)
    while generator.is_playing():
        pass                # Do something useful in here (e.g. recording)