text:
import numpy as np
from scipy.integrate import ode

from .name2idx import C, V
from .set_model import diffeq, param_values, initial_values

def solveode(diffeq, y0, tspan, args):
    sol = ode(diffeq)
    sol.set_integrator(
        'vode', method='bdf', with_jacobian=True,
        atol=1e-9, rtol=1e-9, min_step=1e-8
    )
    sol.set_initial_value(y0, tspan[0])
    sol.set_f_params(args)

    T = [tspan[0]]
    Y = [y0]

    while sol.successful() and sol.t < tspan[-1]:
        sol.integrate(sol.t+1.)
        T.append(sol.t)
        Y.append(sol.y)

    return np.array(T), np.array(Y)

class Simulation(object):

    tspan = range(1801)
    t = np.array(tspan)
    conditions = [
        'EGF00_HRG05',
        'EGF00_HRG10',
        'EGF05_HRG00',
        'EGF05_HRG05',
        'EGF05_HRG10',
        'EGF10_HRG00',
        'EGF10_HRG05',
        'EGF10_HRG10',
    ]

    ERK_act = np.empty((len(t), len(conditions)))
    Akt_act = np.empty((len(t), len(conditions)))

    x = param_values()
    y0 = initial_values()

    for i, condition in enumerate(conditions):
        if condition == 'EGF00_HRG05':
            y0[V.E] = 0.0
            y0[V.H] = 0.5
        elif condition == 'EGF00_HRG10':
            y0[V.E] = 0.0
            y0[V.H] = 10.0
        elif condition == 'EGF05_HRG00':
            y0[V.E] = 0.5
            y0[V.H] = 0.0
        elif condition == 'EGF05_HRG05':
            y0[V.E] = 0.5
            y0[V.H] = 0.5
        elif condition == 'EGF05_HRG10':
            y0[V.E] = 0.5
            y0[V.H] = 10.0
        elif condition == 'EGF10_HRG00':
            y0[V.E] = 10.0
            y0[V.H] = 0.0
        elif condition == 'EGF10_HRG05':
            y0[V.E] = 10.0
            y0[V.H] = 0.5
        elif condition == 'EGF10_HRG10':
            y0[V.E] = 10.0
            y0[V.H] = 10.0

        (T, Y) = solveode(diffeq, y0, tspan, tuple(x))

        ERK_act[:,i] = Y[:,V.ERKstar] + Y[:,V.pERK_ERKPpase]
        Akt_act[:,i] = Y[:,V.Aktstar]


text:
# -*- coding: utf-8 -*-
"""
:mod:`Solver` -- ODE Solvers
============================

.. module :: solver
.. moduleauthor :: Olivier Verdier <olivier.verdier@gmail.com>

The class :class:`~odelab.solver.Solver` takes care of calling the numerical scheme to produce data, and of storing that data.

The higher level class is :class:`Solver`, which is initialized with an instance of a :class:`Scheme` class.
"""
from __future__ import division

import numpy as np

import itertools
import time

from odelab.plotter import Plotter

import warnings
from .store import Store, SimpleStore

from contextlib import contextmanager

class Solver (object):
	"""
	General Solver class, that takes care of calling the step function and storing the intermediate results.

	"""

	def __init__(self, scheme, system, path=None, init_scheme=None):
		"""
:Parameters:
	system : :class:`odelab.system.System`
		Object describing the system. The requirement on that class may vary depending on the scheme.
	scheme : :class:`odelab.scheme.Scheme`
		Scheme to be used to perform the actual simulation.
	path : :string:
		Path to the file where to save the produced data (if ``None``, a tempfile is created).
		"""
		self.system = system
		self.scheme = scheme
		self.init_scheme = init_scheme
		if path is None:
			self.store = SimpleStore()
		else:
			self.store = Store(path)

	# max_iter = max_iter_factor * (time/h)
	max_iter_factor = 100

	def initialize(self, u0=None, name=None):
		"""
Initialize the solver to the initial condition :math:`u(0) = u0`.

:param array u0: initial condition; if it is not provided, it is set to the previous initial condition.
:param scalar time: span of the simulation
:param string name: name of this simulation
		"""
		self.current_scheme = None
		if u0 is None: # initial condition not provided
			raise self.NotInitialized("You must provide an initial condition.")
		if np.isscalar(u0):
			u0 = [u0] # todo: test if this is necessary
		u0 = np.array(u0)
		raw_event0 = np.hstack([u0, 0])
		event0 = self.system.preprocess(raw_event0)

		self.set_name(name=name)

		info = {
				'u0':u0,
				}
		# save system and scheme information in order to recover if unpickling fails
		solver_info = {
			'system_class': repr(type(self.system)),
			'scheme_class': repr(type(self.scheme)),
			'init_scheme_class': repr(type(self.init_scheme)),
			'solver_class': repr(type(self)),
			}

		self.store.initialize(event0, self.name)

		with self.open_store(write=True):

			# store the metadata
			self.store['init_params'] = info

			self.store['solver_info'] = solver_info
			self.store['solver'] = self

			# duration counter:
			self.store['duration'] = 0.

			# append the initial condition:
			self.store.append(event0)

	@contextmanager
	def open_store(self, write=False):
		"""
Method to open the data store. Any access to the events must make use of this method::

	with solver.open_store() as events:
		...
		"""
		with self.store.open(write) as events:
			yield events

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

	def generate(self, events):
		"""
		Generates the (t,u) values.
		"""
		last_event = events[:, -1]
		event = last_event
		init_stage = self.store.get_nb_stage(events)
		tail_length = self.scheme.tail_length
		for stage in itertools.count(init_stage): # infinite loop
			if stage < tail_length: # not enough past values to run main scheme
				if stage == 1:
					self.set_scheme(self.init_scheme, events)
			elif self.current_scheme is None: # main scheme kicks in
				self.set_scheme(self.scheme, events)
			event = self.step()
			yield event

	class FinalTimeNotReached(Exception):
		"""
		Raised when the final time was not reached within the given ``max_iter`` number of iterations.
		"""

	class Unstable(Exception):
		"""
		Raised when the scheme produces NaN values.
		"""

	class NotInitialized(Exception):
		"""
		Raised when the solver is not properly initialized.
		"""

	class RuntimeError(Exception):
		"""
		Raised to relay an exception occurred while running the solver.
		"""

	class NotRun(Exception):
		"""
		Raised when trying to access the events although the solver is empty.
		"""

	@contextmanager
	def simulating(self):
		self._start_time = time.time()
		with self.open_store(write=True) as events:
			yield events
			end_time = time.time()
			duration = end_time - self._start_time
			self.store['duration'] += duration

	catch_runtime = True # whether to catch runtime exception (not catching allows to see the traceback)

	t_tol = 1e-12 # tolerance to tell whether the final time is reached

	def run(self, time, max_iter=None):
		"""
		Run the simulation for a given time.

:param scalar time: the time span for which to run;
:param max_iter: the maximum number of iterations; if ``None``, an estimate is computed base on the time step and time span
		"""
		if not hasattr(self,'name'):
			raise self.NotInitialized("You must call the `initialize` method before you can run the solver.")

		self._max_iter = max_iter
		if self._max_iter is None:
			# generous estimation of the maximum number of iterations
			self._max_iter = int(time/self.scheme.h * self.max_iter_factor)

		with self.simulating() as events:
			# start from the last time we stopped
			generator = self.generate(events)
			t0 = events[-1, -1]
			tf = t0 + time # final time

			if self.with_progressbar:
				progress_bar.maxval = time
				progress_bar.widgets[0] = self.name
				progress_bar.start()

			for iteration in range(self._max_iter): # todo: use enumerate
				try:
					event = next(generator)
				except Exception as e:
					if self.catch_runtime:
						raise self.RuntimeError('%s raised after %d steps: %s' % (type(e).__name__, iteration, e.args), e, iteration)
					else:
						raise
				else:
					if np.any(np.isnan(event)):
						raise self.Unstable('Unstable after %d steps.' % iteration)

					self.store.append(event)
					t = event[-1]
					if self.with_progressbar:
						progress_bar.update(np.real(t-t0))
					if t > tf - self.t_tol:
						break
			else:
				raise self.FinalTimeNotReached("Reached maximal number of iterations: {0}".format(self._max_iter))

	def set_name(self, name=None):
		"""
		Set or guess a name for this session.
		"""
		if name is not None:
			self.name = name
		else:
			guess = self.guess_name()
			self.name = guess

	def guess_name(self):
		return "{system}_{scheme}".format(system=type(self.system).__name__, scheme=type(self.scheme).__name__, )

	def get_u(self, index, process=True):
		"""
		Return u[index] after post-processing.
		"""
		with self.open_store() as events:
			event = events[:, index]
		if process:
			event = self.system.postprocess(event)
		return event

	def get_events(self, t0=None, time=None, sampling_rate=1.):
		"""
		Return the events from time t0, during time `time`, sampled.
		"""
		ts = self.get_times()
		if len(ts) == 1:
			raise self.NotRun('The solver has not been run. If you wanted to check the initial condition, use Solver.initial().')
		if t0 is None:
			t0 = ts[0]
		if time is None:
			time = ts[-1] - ts[0]
		indices = np.where((ts >= t0) & (ts < t0 + time))[0]
		size = len(indices)
		initial_index = indices[0]
		final_index = indices[-1]+1
		stride = int(np.ceil(1/sampling_rate))
		with self.open_store() as events:
			result = events[:, slice(initial_index, final_index, stride)]
			return result

	def get_times(self):
		with self.open_store() as events:
			times = events[-1]
		return times

	def final_time(self):
		with self.open_store() as events:
			final = events[-1, -1]
		return final

	def initial(self, process=True):
		"""
		Convenience method to obtain the initial condition.
		"""
		return self.get_u(0, process)

	def final(self, process=True):
		"""
		Convenience method to obtain the last computed value.
		"""
		return self.get_u(-1, process)

	def plot(self, *args, **kwargs):
		"""
Plot using the plotter object from :method:`odelab.Solver.plotter`.
		"""
		plotter = self.plotter(*args, **kwargs)
		plotter.plot()
		return plotter

	def plotter(self, components=None, plot_exact=True, error=False, time_component=None, t0=None, time=None, **plot_args):
		"""
Constructs a plotter object.
		"""
		plotter = Plotter(self)
		plotter.setup(plot_exact, error)
		plotter.components = components
		plotter.time_component = time_component
		plotter.plot_args = plot_args
		plotter.t0 = t0
		plotter.time = time
		return plotter

	def plot_function(self, function, *args, **kwargs):
		"""
		Plot a given function of the state. May be useful to plot constraints or energy.

		This is now a convenience function that calls the method :meth:`plot`.

:param string function: name of the method to call on the current system object

		:Example:
			the code ``solver.plot_function('energy')`` will call the method ``solver.system.energy`` on the current stored solution points.
		"""
		return self.plot(*args, components=[function], **kwargs)

	def plot2D(self, time_component=0, other_component=1, *args, **kwargs):
		"""
		Plot components vs another one
		"""
		return self.plot(other_component, time_component=time_component, *args, **kwargs)

	def __repr__(self):
		solver = type(self).__name__
		scheme = repr(self.scheme)
		if self.init_scheme is not None:
			init_scheme = '({0})'.format(repr(self.init_scheme))
		else:
			init_scheme = ''
		system = repr(self.system)
		return '<{solver}: {scheme}{init_scheme} {system}>'.format(solver=solver, scheme=scheme, init_scheme=init_scheme, system=system)

	def set_scheme(self, scheme, events):
		self.current_scheme = scheme
		self.current_scheme.system = self.system
		self.current_scheme.initialize(events)

	def step(self):
		return self.current_scheme.do_step()

SingleStepSolver = Solver

def load_solver(path, name):
	"""
Create a solver object from a path to an hdf5 file.
	"""
	import tables
	with tables.openFile(path, 'r') as f:
		events = f.getNode('/'+name)
		try:
			solver = events.attrs['solver']
		except KeyError:
			solver = load_solver_v2(path, name)
		if not isinstance(solver, Solver): # pickling has failed
			warnings.warn('Loading failed')
			solver = Solver(scheme=None, system=None, path=path)
			solver.name = name
		solver.store = Store(path)
		solver.store.name = solver.name
	return solver

def load_solver_v2(path, name):
	"""
Create a solver object from a path to an hdf5 file.
	"""
	import tables
	with tables.openFile(path, 'r') as f:
		events = f.getNode('/'+name)
		info = events.attrs['solver_info']
		if isinstance(info, dict):
			system = info['system']
			scheme = info['scheme']
			solver_class = info['solver_class']
			solver = solver_class(system=system, scheme=scheme, path=path)
		else:
			solver = Solver(scheme=None, system=None, path=path)
		solver.name = name
	return solver

# try to import progressbar and use it if it is available
try:
	import progressbar as pb
	widgets = ['', ' ', pb.Timer('%s'), ' ', pb.Percentage(), ' ', pb.Bar('='), ' ',  ' ', pb.ETA(),  ]
	progress_bar = pb.ProgressBar(widgets=widgets)
	del pb
	with_progressbar = True
except ImportError:
	with_progressbar = False

Solver.with_progressbar = with_progressbar

ext: py

max_stars_repo_name: olivierverdier/odelab

max_issues_repo_name: olivierverdier/odelab

max_forks_repo_name: olivierverdier/odelab

reason: import numpy


text:
################################################################################
"""
`electricpy` Package - `math` Module.

>>> from electricpy import math as epmath

Focussed on simplifying common mathematic formulas for electrical engineering,
this module exposes a few common functions like convolution, step-functions,
etc.

Built to support operations similar to Numpy and Scipy, this package is designed
to aid in scientific calculations.
"""
################################################################################

# Import Required Packages
import numpy as _np
import scipy.signal as _sig
from scipy.integrate import quad as integrate

# Define convolution function
def convolve(tuple):
    """
    Filter Convolution Function.

    Given a tuple of terms, convolves all terms in tuple to
    return one tuple as a numpy array.

    Parameters
    ----------
    tuple:      tuple of numpy.ndarray
                Tuple of terms to be convolved.

    Returns
    -------
    c:          The convolved set of the individual terms.
                i.e. numpy.ndarray([ x1, x2, x3, ..., xn ])
    """
    c = _sig.convolve(tuple[0], tuple[1])
    if (len(tuple) > 2):
        # Iterate starting with second element and continuing
        for i in range(2, len(tuple)):
            c = _sig.convolve(c, tuple[i])
    return (c)

# Define Step function
def step(t):
    r"""
    Step Function [ u(t) ].

    Simple implimentation of numpy.heaviside function to provide standard
    step-function as specified to be zero at :math:`x < 0`, and one at
    :math:`x \geq 0`.

    Examples
    --------
    >>> import numpy as np
    >>> from electricpy.math import step
    >>> t = np.array([-10, -8, -5, -3, 0, 1, 2, 5, 7, 15])
    >>> x = step(t)
    array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

    Parameters
    ----------
    t:  arraylike
        Time samples for which the step response should be generated.
    """
    return (_np.heaviside(t, 1))

# Arbitrary Waveform RMS Calculating Function
def funcrms(func, T):
    """
    Root-Mean-Square (RMS) Evaluator for Callable Functions.

    Integral-based RMS calculator, evaluates the RMS value
    of a repetative signal (f) given the signal's specific
    period (T)

    Parameters
    ----------
    func:   float
            The periodic function, a callable like f(t)
    T:      float
            The period of the function f, so that f(0)==f(T)

    Returns
    -------
    RMS:    The RMS value of the function (f) over the interval ( 0, T )
    """
    fn = lambda x: func(x) ** 2
    integral, _ = integrate(fn, 0, T)
    return _np.sqrt(1 / T * integral)

# Define Gaussian Function
def gaussian(x, mu=0, sigma=1):
    """
    Gaussian Function.

    This function is designed to generate the gaussian
    distribution curve with configuration mu and sigma.

    Parameters
    ----------
    x:      float
            The input (array) x.
    mu:     float, optional
            Optional control argument, default=0
    sigma:  float, optional
            Optional control argument, default=1

    Returns
    -------
    Computed gaussian (numpy.ndarray) of the input x
    """
    return (1 / (sigma * _np.sqrt(2 * _np.pi)) *
            _np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)))

# Define Gaussian Distribution Function
def gausdist(x, mu=0, sigma=1):
    """
    Gaussian Distribution Function.

    This function is designed to calculate the generic
    distribution of a gaussian function with controls
    for mu and sigma.

    Parameters
    ----------
    x:      numpy.ndarray
            The input (array) x
    mu:     float, optional
            Optional control argument, default=0
    sigma:  float, optional
            Optional control argument, default=1

    Returns
    -------
    F:      numpy.ndarray
            Computed distribution of the gausian function at the
            points specified by (array) x
    """
    # Define Integrand
    def integrand(sq):
        return _np.exp(-sq ** 2 / 2)

    try:
        lx = len(x)  # Find length of Input
    except:
        lx = 1  # Length 1
        x = [x]  # Pack into list
    F = _np.zeros(lx, dtype=_np.float64)
    for i in range(lx):
        x_tmp = x[i]
        # Evaluate X (altered by mu and sigma)
        X = (x_tmp - mu) / sigma
        integral = integrate(integrand, _np.NINF, X)  # Integrate
        result = 1 / _np.sqrt(2 * _np.pi) * integral[0]  # Evaluate Result
        F[i] = result
    # Return only the 0-th value if there's only 1 value available
    if len(F) == 1:
        F = F[0]
    return F

# Define Probability Density Function
def probdensity(func, x, x0=0, scale=True):
    """
    Probability Density Function.

    This function uses an integral to compute the probability
    density of a given function.

    Parameters
    ----------
    func:   function
            The function for which to calculate the PDF
    x:      numpy.ndarray
            The (array of) value(s) at which to calculate
            the PDF
    x0:     float, optional
            The lower-bound of the integral, starting point
            for the PDF to be calculated over, default=0
    scale:  bool, optional
            The scaling to be applied to the output,
            default=True

    Returns
    -------
    sumx:   numpy.ndarray
            The (array of) value(s) computed as the PDF at
            point(s) x
    """
    sumx = _np.array([])
    try:
        lx = len(x)  # Find length of Input
    except:
        lx = 1  # Length 1
        x = [x]  # Pack into list
    # Recursively Find Probability Density
    for i in range(lx):
        sumx = _np.append(sumx, integrate(func, x0, x[i])[0])
    # Return only the 0-th value if there's only 1 value available
    if len(sumx) == 1:
        sumx = sumx[0]
    else:
        if scale:
            mx = sumx.max()
            sumx /= mx
        elif scale != False:
            sumx /= scale
    return sumx

# Define Real FFT Evaluation Function
def rfft(arr, dt=0.01, absolute=True, resample=True):
    """
    RFFT Function.

    This function is designed to evaluat the real FFT
    of a input signal in the form of an array or list.

    Parameters
    ----------
    arr:        numpy.ndarray
                The input array representing the signal
    dt:         float, optional
                The time-step used for the array,
                default=0.01
    absolute:   bool, optional
                Control argument to force absolute
                values, default=True
    resample:   bool, optional
                Control argument specifying whether
                the FFT output should be resampled,
                or if it should have a specific
                resampling rate, default=True

    Returns
    -------
    FFT Array
    """
    # Calculate with Absolute Values
    if absolute:
        fourier = abs(_np.fft.rfft(arr))
    else:
        fourier = _np.fft.rfft(arr)
    if resample:
        # Evaluate the Downsampling Ratio
        dn = int(dt * len(arr))
        # Downsample to remove unnecessary points
        fixedfft = filter.dnsample(fourier, dn)
        return (fixedfft)
    elif not resample:
        return (fourier)
    else:
        # Condition Resample Value
        resample = int(resample)
        # Downsample to remove unnecessary points
        fixedfft = filter.dnsample(fourier, resample)
        return fixedfft

reason: import numpy,import scipy,from scipy


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json
import sys
import os
import tensorflow as tf
import numpy as np
from tqdm import tqdm
from collections import defaultdict
import time

os.environ["CUDA_VISIBLE_DEVICES"]="0"

from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense, Bidirectional, concatenate, multiply, Conv1D, MaxPooling1D, \
    GlobalAveragePooling1D, Input, CuDNNGRU
from tensorflow.python.keras.optimizers import Adam,  RMSprop
from tensorflow import keras

from PIPR.embeddings.seq2tensor import s2t
from utils import Metrictor_PPI

def get_session(gpu_fraction=0.9):
    '''Assume that you have 6GB of GPU memory and want to allocate ~2GB'''

    num_threads = os.environ.get('OMP_NUM_THREADS')
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)

    if num_threads:
        return tf.Session(config=tf.ConfigProto(
            gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
    else:
        return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

def prepare_data(id2seq_file, emb_file):
    seq_size = 2000
    seq2t = s2t(emb_file)
    pseq_dict = {}
    for line in tqdm(open(id2seq_file)):
        line = line.strip().split('\t')
        if line[0] not in pseq_dict.keys():
            pseq_dict[line[0]] = line[1]

    dim = seq2t.dim

    with open(f'../../../../data/{dataset}_ppi_with_labels.json', 'r') as f:
        ppi_with_labels = json.load(f)

    id2index = ppi_with_labels['names']
    idx2name = {v: k for k, v in zip(id2index.keys(), id2index.values())}

    seq_tensor = np.array([seq2t.embed_normalized(pseq_dict[idx2name[i]], seq_size) for i in range(len(id2index))]).astype(dtype=np.float16)

    seq_index1 = []
    seq_index2 = []
    class_labels = []
    for interaction in ppi_with_labels['dict'].keys():
        p1, p2 = interaction.split('__')
        seq_index1.append(id2index[p1])
        seq_index2.append(id2index[p2])
        label_id = ppi_with_labels['dict'][interaction]
        class_labels.append(ppi_with_labels['labels'][label_id])

    seq_index1 = np.array(seq_index1)
    seq_index2 = np.array(seq_index2)
    class_labels = np.array(class_labels)
    return seq_tensor, seq_index1, seq_index2, class_labels, dim

def make_splits(dataset, mode, seeds, seq_index1, seq_index2):
    splits = []

    for seed in seeds:
        split_path = f'../../../../train_valid_index_json/{dataset}_{mode}_{seed}.json'

        with open(split_path, 'r') as f:
            split_dict = json.load(f)

        train_ids = split_dict['train_index']
        test_ids = split_dict['valid_index']

        counts = defaultdict(int)
        for i in train_ids:
            counts[seq_index1[i]] = 1
            counts[seq_index2[i]] = 1

        test_bs = []
        test_es = []
        test_ns = []
        for i in test_ids:
            seen = counts[seq_index1[i]] + counts[seq_index2[i]]
            if seen == 2:
                test_bs.append(i)
            elif seen == 1:
                test_es.append(i)
            elif seen == 0:
                test_ns.append(i)
            else:
                print(counts[i])

        splits.append((train_ids, test_bs, test_es, test_ns))

    return splits

def build_model(dim, hidden_dim=25):
    seq_size = 2000
    seq_input1 = Input(shape=(seq_size, dim), name='seq1')
    seq_input2 = Input(shape=(seq_size, dim), name='seq2')
    l1=Conv1D(hidden_dim, 3)
    r1=Bidirectional(CuDNNGRU(hidden_dim, return_sequences=True))
    l2=Conv1D(hidden_dim, 3)
    r2=Bidirectional(CuDNNGRU(hidden_dim, return_sequences=True))
    l3=Conv1D(hidden_dim, 3)
    r3=Bidirectional(CuDNNGRU(hidden_dim, return_sequences=True))
    l4=Conv1D(hidden_dim, 3)
    r4=Bidirectional(CuDNNGRU(hidden_dim, return_sequences=True))
    l5=Conv1D(hidden_dim, 3)
    r5=Bidirectional(CuDNNGRU(hidden_dim, return_sequences=True))
    l6=Conv1D(hidden_dim, 3)
    s1=MaxPooling1D(3)(l1(seq_input1))
    s1=concatenate([r1(s1), s1])
    s1=MaxPooling1D(3)(l2(s1))
    s1=concatenate([r2(s1), s1])
    s1=MaxPooling1D(2)(l3(s1))
    s1=concatenate([r3(s1), s1])
    s1=MaxPooling1D(2)(l4(s1))
    s1=concatenate([r4(s1), s1])
    s1=MaxPooling1D(2)(l5(s1))
    s1=concatenate([r5(s1), s1])
    s1=l6(s1)
    s1=GlobalAveragePooling1D()(s1)
    s2=MaxPooling1D(3)(l1(seq_input2))
    s2=concatenate([r1(s2), s2])
    s2=MaxPooling1D(3)(l2(s2))
    s2=concatenate([r2(s2), s2])
    s2=MaxPooling1D(2)(l3(s2))
    s2=concatenate([r3(s2), s2])
    s2=MaxPooling1D(2)(l4(s2))
    s2=concatenate([r4(s2), s2])
    s2=MaxPooling1D(2)(l5(s2))
    s2=concatenate([r5(s2), s2])
    s2=l6(s2)
    s2=GlobalAveragePooling1D()(s2)
    merge_text = multiply([s1, s2])
    x = Dense(hidden_dim, activation='linear')(merge_text)
    x = keras.layers.LeakyReLU(alpha=0.3)(x)
    x = Dense(int((hidden_dim+7)/2), activation='linear')(x)
    x = keras.layers.LeakyReLU(alpha=0.3)(x)
    main_output = Dense(7, activation='sigmoid')(x)
    merge_model = Model(inputs=[seq_input1, seq_input2], outputs=[main_output])
    return merge_model

def train_test(dataset, batch_size, n_epochs, learning_rate, seeds):
    # Note: if you use another PPI dataset, this needs to be changed to a corresponding dictionary file.
    if dataset == 'STRING':
        id2seq_file = "../../../../data/protein.STRING_all_connected.sequences.dictionary.tsv"
    else:
        id2seq_file = f"../../../../data/protein.{dataset}.sequences.dictionary.tsv"

    emb_file = "../../../../data/vec5_CTC.txt"

    seq_tensor, seq_index1, seq_index2, class_labels, dim = prepare_data(id2seq_file, emb_file)

    for mode in ['random', 'bfs', 'dfs']:
        splits = make_splits(dataset, mode, seeds, seq_index1, seq_index2)
        split_nr = 0

        for train, tbs, tes, tns in splits:
            print(
                f'_________________Training on {dataset}, {mode} mode with seed {seeds[split_nr]}, batch size {batch_size}_______________')

            # Training
            t = time.time()
            merge_model = build_model(dim)
            adam = Adam(lr=learning_rate, amsgrad=True)
            rms = RMSprop(lr=learning_rate)

            merge_model.compile(optimizer=adam, loss='categorical_crossentropy')
            merge_model.fit([seq_tensor[seq_index1[train]], seq_tensor[seq_index2[train]]], class_labels[train],
                            batch_size=batch_size, epochs=n_epochs)

            print('Training took ', (time.time() - t) / 60)

            tf.keras.models.save_model(merge_model,
                                       f'../../../save_model/PIPR_{dataset}_{mode}_{batch_size}_{seeds[split_nr]}')
            split_nr += 1

            # Testing
            results = []
            test_sets = [tbs + tes + tns, tbs, tes, tns]
            for i, test in enumerate(test_sets):
                if len(test) == 0:
                    results.append(None)
                    continue
                t = time.time()
                pred = merge_model.predict([seq_tensor[seq_index1[test]], seq_tensor[seq_index2[test]]])

                pred_bool = pred > 0.5

                print('Prediction took ', time.time() - t)
                metrics = Metrictor_PPI(pred_bool, class_labels[test])
                metrics.show_result()

                print(f"---------------- valid-test-{['all', 'bs', 'es', 'ns'][i]} result --------------------")

                print("Recall: {}, Precision: {}, F1: {}".format(metrics.Recall, metrics.Precision, metrics.F1))

                results.append(metrics.F1)

            with open(f'../../../../save_test_results/PIPR_{dataset}_{mode}', 'a') as f:
                f.write(", ".join(['adam', mode, str(batch_size), str(learning_rate)]))
                f.write(': ')
                f.write(", ".join(str(r) for r in results))
                f.write('\n')

if __name__ == '__main__':
    # set training parameters
    batch_size = 1024
    learning_rate = 0.001
    n_epochs = 100
    dataset = 'STRING'
    seeds = [0]  # , 42, 100, 600, 2000]

    tf.keras.backend.set_session(get_session())
    train_test(dataset, batch_size, n_epochs, learning_rate, seeds)

max_stars_repo_name: zrimseku/Reproducibility-Challenge

max_issues_repo_name: zrimseku/Reproducibility-Challenge

max_forks_repo_name: zrimseku/Reproducibility-Challenge


text:
#!/usr/bin/env python

import numpy as np
import numpy
import chainer
import chainer.functions as F
import chainer.links as L
from chainer import cuda, optimizers, serializers, Variable

from chainer import function
from chainer.utils import type_check

class MeanAbsoluteError(function.Function):

    """Mean absolute error function."""

    def check_type_forward(self, in_types):
        type_check.expect(in_types.size() == 2)
        type_check.expect(
            in_types[0].dtype == numpy.float32,
            in_types[1].dtype == numpy.float32,
            in_types[0].shape == in_types[1].shape
        )

    def forward_cpu(self, inputs):
        x0, x1 = inputs
        self.diff = x0 - x1
        diff = self.diff.ravel()
        return numpy.array(abs(diff).sum() / diff.size, dtype=diff.dtype),

    def forward_gpu(self, inputs):
        x0, x1 = inputs
        self.diff = x0 - x1
        diff = self.diff.ravel()
        return abs(diff).sum() / diff.dtype.type(diff.size),

    def backward(self, inputs, gy):
        xp = cuda.get_array_module(*inputs)
        coeff = gy[0] * gy[0].dtype.type(1. / self.diff.size)
        gx0 = coeff * xp.sign(self.diff)
        return gx0, -gx0

def mean_absolute_error(x0, x1):
    """Mean absolute error function.
    This function computes mean absolute error between two variables. The mean
    is taken over the minibatch.
    """
    return MeanAbsoluteError()(x0, x1)

class LNET(chainer.Chain):

    def __init__(self):
        super(LNET, self).__init__(
            c0=L.Convolution2D(3, 32, 3, 1, 1),
            c1=L.Convolution2D(32, 64, 4, 2, 1),
            c2=L.Convolution2D(64, 64, 3, 1, 1),
            c3=L.Convolution2D(64, 128, 4, 2, 1),
            c4=L.Convolution2D(128, 128, 3, 1, 1),
            c5=L.Convolution2D(128, 256, 4, 2, 1),
            c6=L.Convolution2D(256, 256, 3, 1, 1),
            c7=L.Convolution2D(256, 512, 4, 2, 1),
            c8=L.Convolution2D(512, 512, 3, 1, 1),

            dc8=L.Deconvolution2D(1024, 512, 4, 2, 1),
            dc7=L.Convolution2D(512, 256, 3, 1, 1),
            dc6=L.Deconvolution2D(512, 256, 4, 2, 1),
            dc5=L.Convolution2D(256, 128, 3, 1, 1),
            dc4=L.Deconvolution2D(256, 128, 4, 2, 1),
            dc3=L.Convolution2D(128, 64, 3, 1, 1),
            dc2=L.Deconvolution2D(128, 64, 4, 2, 1),
            dc1=L.Convolution2D(64, 32, 3, 1, 1),
            dc0=L.Convolution2D(64, 1, 3, 1, 1),

            bnc0=L.BatchNormalization(32),
            bnc1=L.BatchNormalization(64),
            bnc2=L.BatchNormalization(64),
            bnc3=L.BatchNormalization(128),
            bnc4=L.BatchNormalization(128),
            bnc5=L.BatchNormalization(256),
            bnc6=L.BatchNormalization(256),
            bnc7=L.BatchNormalization(512),
            bnc8=L.BatchNormalization(512),

            bnd8=L.BatchNormalization(512),
            bnd7=L.BatchNormalization(256),
            bnd6=L.BatchNormalization(256),
            bnd5=L.BatchNormalization(128),
            bnd4=L.BatchNormalization(128),
            bnd3=L.BatchNormalization(64),
            bnd2=L.BatchNormalization(64),
            bnd1=L.BatchNormalization(32)
            # l = L.Linear(3*3*256, 2)'
        )

    def enc(self, x):
        e0 = F.relu(self.bnc0(self.c0(x)))
        e1 = F.relu(self.bnc1(self.c1(e0)))
        e2 = F.relu(self.bnc2(self.c2(e1)))
        e3 = F.relu(self.bnc3(self.c3(e2)))
        e4 = F.relu(self.bnc4(self.c4(e3)))
        e5 = F.relu(self.bnc5(self.c5(e4)))
        e6 = F.relu(self.bnc6(self.c6(e5)))
        e7 = F.relu(self.bnc7(self.c7(e6)))
        e8 = F.relu(self.bnc8(self.c8(e7)))
        return [e0, e2, e4, e6, e7, e8]

    def calc(self, x):
        e0, e2, e4, e6, e7, e8 = self.enc(x)

        d8 = F.relu(self.bnd8(self.dc8(F.concat([e7, e8]))))
        del e7, e8
        d7 = F.relu(self.bnd7(self.dc7(d8)))
        del d8
        d6 = F.relu(self.bnd6(self.dc6(F.concat([e6, d7]))))
        del d7, e6
        d5 = F.relu(self.bnd5(self.dc5(d6)))
        del d6
        d4 = F.relu(self.bnd4(self.dc4(F.concat([e4, d5]))))
        del d5, e4
        d3 = F.relu(self.bnd3(self.dc3(d4)))
        del d4
        d2 = F.relu(self.bnd2(self.dc2(F.concat([e2, d3]))))
        del d3, e2
        d1 = F.relu(self.bnd1(self.dc1(d2)))
        del d2
        d0 = self.dc0(F.concat([e0, d1]))

        return d0

    def __call__(self, x, t):
        h = self.calc(x)
        loss_c = mean_absolute_error(h, t)
        loss = loss_c
        chainer.report({'loss': loss, 'loss_c': loss_c}, self)
        return loss

max_stars_repo_name: ohong/pretty-whale

max_issues_repo_name: ececleon/PaintsChainer

max_forks_repo_name: ececleon/PaintsChainer


text:
import tensorflow as tf
import sonnet as snt

#f(PI(wij*xj+bij)), not sure this is correctly implemented yet!
class Mul_YKJ(snt.AbstractModule):
    def __init__(self,
            output_size,
            activation=None,
            weight_initializer=tf.keras.initializers.TruncatedNormal(dtype=tf.float32),
            bias_initializer=tf.keras.initializers.TruncatedNormal(dtype=tf.float32),
            weight_regularizer=tf.contrib.layers.l1_regularizer(0.001),
            bias_regularizer=None,
            activity_regularizer=None,
            name="mul_YKJ"
        ):
        super(Mul_YKJ,self).__init__(name=name)
        self.output_size = output_size
        self.activation = activation
        self.weight_initializer = weight_initializer
        self.bias_initializer = bias_initializer
        self.weight_regularizer = weight_regularizer
        self.bias_regularizer = bias_regularizer
        self.activity_regularizer = activity_regularizer
    def _build(self, inputs):
        input_size = inputs.shape.as_list()[-1]
        #print("INPUT SIZE: %d" % input_size)

        #Weights
        W = tf.get_variable(self.module_name+"W", (self.output_size, input_size), tf.float32, self.weight_initializer, self.weight_regularizer)

        #Biases
        B = tf.get_variable(self.module_name+"B", (self.output_size, input_size), tf.float32, self.bias_initializer, self.bias_regularizer)

        rows = W*inputs + B
        #print("ROWS SIZE: %s" % str(rows.shape.as_list()))
        h = tf.reduce_prod(rows, axis=(-1), keepdims=True)
        #print("H SIZE: %s" % str(h.shape.as_list()))
        if self.activation!=None:
            h = self.activation(h)
        if self.activity_regularizer != None:
            act_reg = self.activity_regularizer(h)
            tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, act_reg)
        return h

if __name__ == "__main__":
    import numpy as np
    dataset_size = 1000000
    xx = np.array([np.random.randint(-999,1000, (2,)) for e in range(dataset_size)], dtype=np.float32)
    yy = np.array([[np.prod(x)] for x in xx], dtype=np.float32)

    #print(xx)
    #print(yy)

    ins = tf.placeholder(tf.float32, (None,2))
    targets  = tf.placeholder(tf.float32, (None,1))
    plusminusone_regularizer = (lambda b: 0.001*tf.reduce_mean(tf.pow(tf.abs(b)-1,2)))
    mul_layer = Mul_YKJ(1, bias_regularizer=plusminusone_regularizer)
    outs = mul_layer(ins)

    deviation = outs-targets
    print("DEVIATION SHAPE: %s" % str(deviation.shape.as_list()))
    loss_comps = deviation/(tf.abs(targets)+0.5)
    print("LOSS_COMPS SHAPE: %s" % str(loss_comps.shape.as_list()))
    error = tf.nn.l2_loss(loss_comps)/dataset_size
    reg_loss = tf.losses.get_regularization_loss()
    #print(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    #reg_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.1)
    train_op = optimizer.minimize(error+reg_loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        #training
        for epoch in range(400):
            _,err,errr = sess.run([train_op, error, reg_loss], feed_dict={ins: xx, targets: yy})
            print("Error: %g, regularization loss: %g" % (err, errr))


text:
#!/bin/env python
#
# Script name: IDP_html_gen.py
#
# Description: Script to generate IDP page of QC html report.
#
## Author: Justin Wang

import pandas as pd
import numpy as np
import sys
import os
from ast import literal_eval

def formatter(x):
    try:
        return "{:e}".format(float(x))
    except:
        return x

def generate_full_IDPoi_data(df, IDP_dir):
    """Function that adds IDP values to an existing IDP dataframe, using the
    relevant IDP txt from the subject's IDP directory. Each IDP txt file
    corresponds with a IDP category.

    Parameters
    ----------
    df : pd.DataFrame
        Dataframe containing details about IDPs, no values present.
    IDP_dir : string
        Full path to the directory containing the subject's IDP output
        txt files.

    Returns
    ----------
    output : pd.DataFrame
        Dataframe containing details about IDPs, with values included
    """

    flag = False
    #output df placeholder
    output = pd.DataFrame(

        columns=[
            "num","short","category","num_in_cat","long","unit","dtype","description","value"
        ],
    )

    #for each IDP category, access its corresponding IDP value file
    for category in df["category"].unique():

        #sub-df containing only IDPs for this category
        df_sub = df[df["category"] == category]

        #open the caregory's IDP value txt file, clean whitespaces, and split into a df
        cat_data = []
        try:
            with open(IDP_dir + category + ".txt") as my_file:
                for line in my_file:
                    line = line.strip()
                    line = line.split(" ")
                    cat_data.append(line)
            cat_data = pd.DataFrame(cat_data)
            cat_data = cat_data.T
            cat_data.columns = ["value"]
            cat_data["num_in_cat"] = cat_data.index + 1

            cat_data = cat_data.astype({"num_in_cat": int})
            df_sub = df_sub.astype({"num_in_cat": int})

            #inner join the category's IDP values with the sub-df for this category
            df_sub = df_sub.merge(cat_data, how="left", on="num_in_cat")

            #left should work - used to be inner join. now left join to show if there are any missing IDP values

            #if this is the first sub-df, then the output df is the same as the sub-df for now
            #otherwise, append sub-df ot output df
            if not flag:
                output = df_sub
                flag = True
            else:
                output = output.append(df_sub, ignore_index=True)

        except:
            print(IDP_dir + category + ".txt file missing")

    return output

def IDP_postprocessing(subj, IDP_list_path, IDPoi_list_path, thresholds_txt):
    """Function that generates the IDP page of the QC report for a
    subject.

    TODO: remove duplicate code by having a single function used
            twice - once for low-priority and once for high
    TODO: handle missing low-priority lines in the IDPoi
    TODO: separate low and high priority IDPs in report

    Parameters
    ----------
    subj : string
        Full path to subject's directory.
    IDP_list_path : string
        Full path to IDP list (file containing IDP information).
    IDPoi_list_path : string
         Full path to IDPoi list (file containing list of IDPs of interest).
    """

    #remove trailing forward slashes in subject paths
    if subj.endswith("/"):
        subj = subj[:-1]

    QC_dir = subj + "/QC/html/"
    IDP_dir = subj + "/IDP_files/"

    if not os.path.exists(IDP_dir):
        os.makedirs(IDP_dir)

    subjName = subj[subj.rfind("/") + 1 :]

    #reading and cleaning each line of IDP list
    IDP_list = []
    with open(IDP_list_path) as my_file:
        for line in my_file:
            line = line.strip()
            IDP_list.append(line)
    i = 0
    #remove header
    IDP_list = IDP_list[1:]

    #cleaning just in case - should run fine even if data is already clean
    while i < len(IDP_list):
        IDP_list[i] = IDP_list[i].replace('"', "")
        IDP_list[i] = " ".join(IDP_list[i].split())
        IDP_list[i] = " ".join(IDP_list[i].split("\\t"))
        IDP_list[i] = IDP_list[i].split(" ", 7)
        i += 1

    #IDP list dataframe containing details about every IDP
    IDP = pd.DataFrame(
        IDP_list,
        columns=[
            "num",
            "short",
            "category",
            "num_in_cat",
            "long",
            "unit",
            "dtype",
            "description",
        ],
    )

    #reading each line of IDPoi list
    IDPoi_list = []

    with open(IDPoi_list_path) as my_file:
        for line in my_file:
            line = line.strip()
            IDPoi_list.append(line)
    IDPoi = np.array(IDPoi_list)

    #splitting IDPois into high and low priority dataframes
    priority = True
    priority_array = []
    non_priority_array = []
    for line in IDPoi:
        if line == "HIGH_PRIORITY":
            priority = True
        elif line == "LOW_PRIORITY":
            priority = False
        else:
            if priority:
                priority_array.append(line)
            else:
                non_priority_array.append(line)

    priority_df = pd.DataFrame(priority_array, columns=["short"])
    non_priority_df = pd.DataFrame(non_priority_array, columns=["short"])

    #filling details about the IDPs for both priority and non priority IDPoi dfs
    #by merging the dfs with the IDP list dataframe
    #TODO handle IDPoi that dont exist/typoed
    priority_df = priority_df[["short"]]
    priority_df = priority_df.merge(IDP, how="inner", left_on="short", right_on="short")

    non_priority_df = non_priority_df[["short"]]
    non_priority_df = non_priority_df.merge(
        IDP, how="inner", left_on="short", right_on="short"
    )

    #get values for each IDPoi
    #TODO: handle empty IDP values
    priority_output = generate_full_IDPoi_data(priority_df, IDP_dir)
    non_priority_output = generate_full_IDPoi_data(non_priority_df, IDP_dir)

    #retain
    priority_output=priority_output[["num","short","category","num_in_cat","long","unit","dtype","description","value"]]

    non_priority_output=non_priority_output[["num","short","category","num_in_cat","long","unit","dtype","description","value"]]

    new_IDP_output = pd.read_csv(r"" + IDP_dir + "tvb_new_IDPs.tsv", delimiter = "\t")
    #new_IDP_output=new_IDP_output[["num","short","category","num_in_cat","long","unit","dtype","description","value"]]

    #prior, non prior, new tvb IDP compiled output
    last_IDP_num=int(IDP['num'].iloc[-1])
    new_IDP_output['num']  += last_IDP_num
    compiled_IDPs = pd.concat([priority_output,new_IDP_output])
    compiled_IDPs = pd.concat([compiled_IDPs,non_priority_output])

    try:
        #need ot handle missing value as a failure as well
        #filling in threshold values

        failed_IDP=""

        thresholds = pd.read_csv(thresholds_txt, sep="\t")

        IDPs_with_thresholds = compiled_IDPs.merge(thresholds, how="left", on="short")
        IDPs_with_thresholds["flag"] = ""
        passed_QC_flag=True

        for index, row in IDPs_with_thresholds.iterrows():
            IDP_failed = True

            current_range=row["accepted_ranges"]
            if str(current_range) == "nan" or str(current_range) == "NaN":
                IDPs_with_thresholds.at[index,"flag"]="N/A"
            #only add flag colours for IDPs which are in the thresholds txt
            else:
                #iterate through ranges
                for x in current_range.split(";"):
                    x=literal_eval(x)
                    if x[0]=="-inf":
                        if row["value"] <= x[1]:
                            IDP_failed=False

                    elif x[1]=="inf":
                        if row["value"] >= x[0]:
                            IDP_failed=False

                    else:
                        if float(row["value"]) >= float(x[0]) and float(row["value"]) <= float(x[1]):
                            IDP_failed=False
                if IDP_failed:
                    passed_QC_flag=False
                    failed_IDP+="\t"+row["short"]+":"+str(row["value"])
                    IDPs_with_thresholds.at[index,"flag"]=IDP_failed

        if not passed_QC_flag:
            failed_IDP=subj+failed_IDP
            f = open(os.path.join(os.path.dirname(subj),"IDP_flags.txt"), "a")
            f.write(failed_IDP+"\n")
            f.close()

        #print(IDPs_with_thresholds)

        #return values back to scientific notation
        priority_output['num'] = priority_output['num'].astype(np.int64)
        non_priority_output['num'] = non_priority_output['num'].astype(np.int64)
        compiled_IDPs['num'] = compiled_IDPs['num'].astype(np.int64)
        IDPs_with_thresholds['num'] = IDPs_with_thresholds['num'].astype(np.int64)
        new_IDP_output['num'] = new_IDP_output['num'].astype(np.int64)

        # new_IDP_output['value'] = new_IDP_output['value'].apply(lambda x: "{:e}".format(float(x)))
        # IDPs_with_thresholds['value'] = IDPs_with_thresholds['value'].apply(lambda x: "{:e}".format(float(x)))

        try:
            #merging with threshold information
            priority_output['value']=priority_output['value'].astype(str)
            non_priority_output['value']=non_priority_output['value'].astype(str)
            compiled_IDPs['value']=compiled_IDPs['value'].astype(str)
            new_IDP_output['value']=new_IDP_output['value'].astype(str)
            IDPs_with_thresholds['value']=IDPs_with_thresholds['value'].astype(str)

            priority_output = priority_output.merge(IDPs_with_thresholds, how="left", on=["num","short","category","num_in_cat","long","unit","dtype","description","value"])
            non_priority_output = non_priority_output.merge(IDPs_with_thresholds, how="left", on=["num","short","category","num_in_cat","long","unit","dtype","description","value"])
            compiled_IDPs = compiled_IDPs.merge(IDPs_with_thresholds, how="left", on=["num","short","category","num_in_cat","long","unit","dtype","description","value"])

            # try:
                # IDPs_with_thresholds["value"] = IDPs_with_thresholds.value.astype(float)
            new_IDP_output = new_IDP_output.merge(IDPs_with_thresholds, how="left", on=["num","short","category","num_in_cat","long","unit","dtype","description","value"])
            # except:
            #     print("ERROR: merge issue between new IDP and thresholded IDPs")

            #scientific notation for values
            priority_output['value'] = priority_output['value'].apply(formatter)
            non_priority_output['value'] = non_priority_output['value'].apply(formatter)
            new_IDP_output['value'] = new_IDP_output['value'].apply(formatter)
            compiled_IDPs['value'] = compiled_IDPs['value'].apply(formatter)
        except:
            print("ERROR: merging or formatting error")

        #save IDPois to txt files for future reference
        priority_output.to_csv(
            r"" + IDP_dir + "priority_IDPs.tsv",
            header=priority_output.columns.values,
            index=None,
            sep="\t",
            mode="w",
        )
        non_priority_output.to_csv(
            r"" + IDP_dir + "non_priority_IDPs.tsv",
            header=non_priority_output.columns.values,
            index=None,
            sep="\t",
            mode="w",
        )
        compiled_IDPs.to_csv(
            r"" + IDP_dir + "significant_IDPs.tsv",
            header=compiled_IDPs.columns.values,
            index=None,
            sep="\t",
            mode="w",
        )
        new_IDP_output.to_csv(
            r"" + IDP_dir + "tvb_new_IDPs.tsv",
            header=compiled_IDPs.columns.values,
            index=None,
            sep="\t",
            mode="w",
        )

    except:
        print("Error: thresholds or merging error")
    #need to save each txt priority txt again with merge to include flags and the threshold
    #need to gen colour based of flag and presence of threshold

if __name__ == "__main__":
    """Function that adds IDP values to an existing IDP dataframe, using the
    relevant IDP txt from the subject's IDP directory. Each IDP txt file
    corresponds with a IDP category.

    Usage
    ----------
    python  IDP_html_gen.py  subj  IDP_list_path  IDPoi_list_path

    Arguments
    ----------
    subj :
        Full path to subject's directory.

    IDP_list_path :
        Full path to IDP list (file containing IDP information).
        ukbb_IDP_list.txt
        IDP list is a tab delimited file with columns:  "num",
                                                        "short",
                                                        "category",
                                                        "num_in_cat",
                                                        "long",
                                                        "unit",
                                                        "dtype",
                                                        "description"
    IDPoi_list_path :
        Full path to IDPoi list (file containing list of IDPs of interest).
        IDPoi file must contain a line with "HIGH PRIORITY" followed by
        shortform IDP names (one per line) that are high priority. Any
        low priority IDPs should follow (one per line) a new line
        with "LOW PRIORITY".

    """
    # try:
    IDP_postprocessing(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

max_stars_repo_name: yilewang/tvb-ukbb

max_issues_repo_name: yilewang/tvb-ukbb

max_forks_repo_name: yilewang/tvb-ukbb


text:
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 17:55:53 2018

@author: daniel
"""

import numpy as np
import matplotlib as mpl
import pylab as plt
import pandas as pd
import json
import io
import os

data_dir = "./coherence_data"

azi = np.loadtxt(os.path.join(data_dir, "memory_Azimuth_120117_header.txt"))
t_azi = np.loadtxt(os.path.join(data_dir, "azi_time.txt"))
vis = np.loadtxt(os.path.join(data_dir, "memory_Visibility_120117_header.txt"))
counts_lr = np.loadtxt(os.path.join(data_dir, "memory_CountsLR_120117_header.txt"))
counts_l_fit = np.loadtxt(os.path.join(data_dir, "Counts-L-fit.txt"))
counts_r_fit = np.loadtxt(os.path.join(data_dir, "Counts-R-fit.txt"))

t = np.loadtxt(os.path.join(data_dir, "time_corrected.txt"))

watt_factor = 2.54591

counts_lr[:,1:] = counts_lr[:,1:]*watt_factor
counts_l_fit [:,1:] = counts_l_fit [:,1:]*watt_factor
counts_r_fit [:,1:] = counts_r_fit [:,1:]*watt_factor

f = plt.figure(0)

ax = plt.subplot(311)

plt.errorbar(t,counts_lr[:,1],yerr=counts_lr[:,2], c='b', label='R', ls='', marker='o')
plt.plot(counts_r_fit [:,0],counts_r_fit [:,1])

plt.errorbar(t,counts_lr[:,3],yerr=counts_lr[:,4], c='r', label='L', ls='', marker='o')
plt.plot(counts_l_fit [:,0],counts_l_fit [:,1])
plt.xlim(-0.1,0.9)

plot_dict={}

h_L=pd.DataFrame(index=t,data=counts_lr[:,1])
h_L_err=pd.DataFrame(index=t,data=counts_lr[:,2])
h_R=pd.DataFrame(index=t,data=counts_lr[:,3])
h_R_err=pd.DataFrame(index=t,data=counts_lr[:,4])

h_L_fit=pd.DataFrame(index=counts_r_fit [:,0],data=counts_l_fit [:,1])
h_R_fit=pd.DataFrame(index=counts_r_fit [:,0],data=counts_r_fit [:,1])

plot_dict['311']={
    'A':{'type':'errorbar','y':h_L[0].to_json(),'yerr':h_L_err[0].to_json(),'ylabel':r'Leistung (fW)','num':'a','label':'L','xlim':(-0.1,0.9),'ylim':(0,40)} ,
    'B':{'type':'errorbar','y':h_R[0].to_json(),'yerr':h_R_err[0].to_json(),'label':'R'},
    'C':{'type':'plot','y':h_L_fit[0].to_json()},
    'D':{'type':'plot','y':h_R_fit[0].to_json()}

}

ax = plt.subplot(312)

plt.errorbar(t_azi,azi[:,1],yerr=azi[:,2], c='b', label='R', ls='', marker='o')
plt.xlim(-0.1,0.9)
plt.ylim(0,2*np.pi)

h=pd.DataFrame(index=t_azi[1:7],data=azi[1:7,1])
h_err=pd.DataFrame(index=t_azi[1:7],data=azi[1:7,2])

plot_dict['312']={
    'A':{'type':'errorbar','y':h[0].to_json(),'yerr':h_err[0].to_json(),'ylabel':r'Azimuth (rad)','num':'b','xlim':(-0.1,0.9),'ylim':(0,2*np.pi)}
}

ax = plt.subplot(313)

plt.errorbar(t_azi,vis[:,1],yerr=vis[:,2], c='b', label='R', ls='', marker='o')
plt.xlim(-0.1,0.9)
plt.ylim(0,1)

h=pd.DataFrame(index=t_azi[1:7],data=vis[1:7,1])
h_err=pd.DataFrame(index=t_azi[1:7],data=vis[1:7,2])

plot_dict['313']={
    'A':{'type':'errorbar','y':h[0].to_json(),'yerr':h_err[0].to_json(),'ylabel':r'Visibility','xlabel':r'Zeit ($\mu s$)','num':'c','xlim':(-0.1,0.9),'ylim':(0,1)}
}

with io.open('memory_coherence.json', 'w+') as f:
  f.write(unicode(json.dumps(plot_dict, ensure_ascii=False,indent=4)))

plt.show()

max_stars_repo_name: dtiarks/ThesisPlot

max_issues_repo_name: dtiarks/ThesisPlot

max_forks_repo_name: dtiarks/ThesisPlot


text:
# Copyright 2022 Google LLC.
#
# 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.

"""Tests for long attention classes."""

import dataclasses
from typing import Optional

from absl.testing import absltest
from absl.testing import parameterized
import jax
from jax import random
import jax.numpy as jnp
import numpy as np
from flaxformer.architectures.longt5 import long_attention
from flaxformer.architectures.longt5 import relative_position_biases_general
from flaxformer.components.attention import dense_attention

# Parse absl flags test_srcdir and test_tmpdir.
jax.config.parse_flags_with_absl()

RelativePositionBiasesGeneral = (
    relative_position_biases_general.RelativePositionBiasesGeneral)

@dataclasses.dataclass(frozen=True)
class EncoderLocalSelfAttArgs:
  num_heads: int = 1
  local_radius: int = 2
  batch_size: int = 2
  qkv_features: int = 3
  out_features: int = 4
  q_len: int = 5
  features: int = 6
  broadcast_dropout: bool = True
  dropout_rate: float = 0.1
  enable_dropout: bool = True
  use_bias: bool = True
  rescale_logits: bool = True
  float32_logits: bool = False
  split_head_kernel: bool = False
  kernels_to_fuse: Optional[str] = None  # Only 'qkv' is supported.
  relpos_bias: Optional[RelativePositionBiasesGeneral] = None

  def init_args(self):
    return dict(
        num_heads=self.num_heads,
        local_radius=self.local_radius,
        qkv_features=self.qkv_features,
        out_features=self.out_features,
        broadcast_dropout=self.broadcast_dropout,
        dropout_rate=self.dropout_rate,
        use_bias=self.use_bias,
        rescale_logits=self.rescale_logits,
        float32_logits=self.float32_logits,
        split_head_kernel=self.split_head_kernel,
        kernels_to_fuse=self.kernels_to_fuse,
        relpos_bias=self.relpos_bias)

  def apply_args(self):
    inputs = jnp.ones((self.batch_size, self.q_len, self.features))
    inputs_mask = jnp.ones((self.batch_size, self.q_len))
    return {
        'inputs': inputs,
        'inputs_mask': inputs_mask,
        'enable_dropout': self.enable_dropout
    }

class LongAttentionTest(parameterized.TestCase):

  def test_local_self_attention_shape(self):
    # This test only checks for shape but tries to make sure all code paths are
    # reached.
    dropout_rng = random.PRNGKey(0)
    batch_size, num_heads, seq_len, qk_depth, v_depth = 1, 2, 8, 3, 5
    local_radius = 1
    block_len = local_radius + 1  # 2
    num_blocks = seq_len // block_len + bool(seq_len % block_len)  # 4

    query = jnp.ones((batch_size, seq_len, num_heads, qk_depth))
    key = jnp.ones((batch_size, seq_len, num_heads, qk_depth))
    value = jnp.ones((batch_size, seq_len, num_heads, v_depth))
    bias = jnp.ones(
        (batch_size, num_blocks, num_heads, block_len, 3 * block_len))

    args = dict(
        query=query,
        key=key,
        value=value,
        local_radius=local_radius,
        bias=bias,
        rescale_logits=True,
        dropout_rng=dropout_rng,
        dropout_rate=0.5,
        enable_dropout=True,
    )

    output = long_attention._local_self_attention(
        **args, broadcast_dropout=True)
    self.assertEqual(output.shape, (batch_size, seq_len, num_heads, v_depth))

    # Make sure we also reach the code path where we don't broadcast dropout.
    output = long_attention._local_self_attention(
        **args, broadcast_dropout=False)
    self.assertEqual(output.shape, (batch_size, seq_len, num_heads, v_depth))

  def test_encoder_local_self_attention(self):
    rngs = {'params': random.PRNGKey(0), 'dropout': random.PRNGKey(1)}
    args = EncoderLocalSelfAttArgs()
    model = long_attention.EncoderLocalSelfAttention(**args.init_args())
    y, _ = model.init_with_output(rngs, **args.apply_args())
    self.assertEqual(y.shape, (args.batch_size, args.q_len, args.out_features))

  def test_encoder_local_self_attention_cast_logits_float32(self):
    rngs = {'params': random.PRNGKey(0), 'dropout': random.PRNGKey(1)}
    args = EncoderLocalSelfAttArgs(float32_logits=True)
    model = long_attention.EncoderLocalSelfAttention(**args.init_args())
    y, _ = model.init_with_output(rngs, **args.apply_args())
    self.assertEqual(y.shape, (args.batch_size, args.q_len, args.out_features))

  def test_encoder_local_self_attention_no_rescale_logits(self):
    rngs = {'params': random.PRNGKey(0), 'dropout': random.PRNGKey(1)}
    args = EncoderLocalSelfAttArgs(rescale_logits=False)
    model = long_attention.EncoderLocalSelfAttention(**args.init_args())
    y, _ = model.init_with_output(rngs, **args.apply_args())
    self.assertEqual(y.shape, (args.batch_size, args.q_len, args.out_features))

  def test_encoder_local_self_attention_no_out_features(self):
    rngs = {'params': random.PRNGKey(0), 'dropout': random.PRNGKey(1)}
    args = EncoderLocalSelfAttArgs(out_features=None)
    model = long_attention.EncoderLocalSelfAttention(**args.init_args())
    y, _ = model.init_with_output(rngs, **args.apply_args())
    self.assertEqual(y.shape, (args.batch_size, args.q_len, args.features))

  def test_encoder_local_self_attention_with_kernel_fusion(self):
    rngs = {'params': random.PRNGKey(0), 'dropout': random.PRNGKey(1)}
    args = EncoderLocalSelfAttArgs(
        split_head_kernel=True, kernels_to_fuse='qkv')
    model = long_attention.EncoderLocalSelfAttention(**args.init_args())
    y, _ = model.init_with_output(rngs, **args.apply_args())
    self.assertEqual(y.shape, (args.batch_size, args.q_len, args.out_features))

  @parameterized.named_parameters(
      ('even_blocking', 15),
      ('uneven_blocking', 16),
      ('degenerate_blocking', 35),
  )
  def test_encoder_local_self_attention_logic(self, local_radius):
    # This test checks the logic of the local attention calculations by
    # comparing with the output of `MultiHeadDotProductAttention`
    # (full attention) after manually applying the local sparsity pattern.
    # The outputs should be identical for non-padding tokens.

    keys = random.split(random.PRNGKey(0), 4)

    batch_size = 3
    seq_len = 64
    in_features = 11
    out_features = 12
    num_heads = 5
    dtype = jnp.float32

    inputs = random.normal(keys[0], (batch_size, seq_len, in_features))
    inputs_mask = random.bernoulli(keys[1], 0.9, (batch_size, seq_len))
    inputs_mask = inputs_mask.astype(jnp.bool_)
    segment_ids = jnp.cumsum(
        random.bernoulli(keys[2], 0.1, (batch_size, seq_len)), axis=-1)
    # `positions` is unused in `EncoderLocalSelfAttention`, so we set to zeros.
    positions = jnp.zeros_like(segment_ids)

    att_config = dict(
        num_heads=num_heads,
        dtype=dtype,
        qkv_features=15,
        out_features=out_features)

    relpos_bias = RelativePositionBiasesGeneral(
        num_heads=num_heads, num_buckets=32, max_distance=128, dtype=dtype)

    local_att = long_attention.EncoderLocalSelfAttention(
        local_radius=local_radius, relpos_bias=relpos_bias, **att_config)

    full_att = dense_attention.MultiHeadDotProductAttention(
        use_bias=True, **att_config)

    local_att_output, local_att_vars = local_att.init_with_output(
        keys[3],
        inputs,
        inputs_mask,
        segment_ids=segment_ids,
        positions=positions,
        enable_dropout=False)

    relpos_bias_vars = dict(params=local_att_vars['params']['relpos_bias'])

    # Full attention uses the same variables as local attention (ignoring
    # `relpos_bias`).
    full_att_vars = local_att_vars

    rp_bucket = relpos_bias.full_att_rp_bucket(
        qlen=seq_len, klen=seq_len, bidirectional=True)
    bias = relpos_bias.apply(relpos_bias_vars, rp_bucket)
    mask = dense_attention.make_attention_mask(
        inputs_mask, inputs_mask, dtype=dtype)
    mask = dense_attention.combine_masks(
        mask,
        dense_attention.make_attention_mask(
            segment_ids, segment_ids, jnp.equal, dtype=dtype))

    # Overlay local sparsity attention mask for full attention case.
    range_array = np.arange(seq_len)
    locality_mask = np.abs(range_array[np.newaxis, :] -
                           range_array[:, np.newaxis]) <= local_radius
    # [1, 1, seq_len, seq_len] shape
    locality_mask = locality_mask[np.newaxis, np.newaxis, :, :]
    mask = dense_attention.combine_masks(mask, locality_mask)

    full_att_output = full_att.apply(
        full_att_vars, inputs, inputs, mask, bias, enable_dropout=False)

    np.testing.assert_array_equal(local_att_output.shape,
                                  (batch_size, seq_len, out_features))
    np.testing.assert_array_equal(local_att_output.shape, full_att_output.shape)

    # Padding tokens may have different embeddings which we'll want to ignore
    # in our comparison, so we "clear" them to zero.
    def clear_padding(array):
      return array * inputs_mask[..., jnp.newaxis].astype(dtype)

    np.testing.assert_allclose(
        clear_padding(local_att_output),
        clear_padding(full_att_output),
        atol=1e-5)

  @parameterized.named_parameters(
      ('even_blocking', 15),
      ('uneven_blocking', 16),
      ('degenerate_blocking', 35),
      ('uneven_blocking_use_kernel_fusion', 16, True),
      ('even_blocking_causal', 15, False, True),
      ('uneven_blocking_causal', 16, False, True),
      ('degenerate_blocking_causal', 35, False, True),
      ('uneven_blocking_use_kernel_fusion_causal', 16, True, True),
  )
  def test_etc_transient_global_self_attention(self,
                                               local_radius,
                                               use_kernel_fusion=False,
                                               causal=False):
    # This test just makes sure the layer successfully runs with different
    # input sizes.

    keys = random.split(random.PRNGKey(0), 3)

    batch_size = 3
    seq_len = 64
    tokens_per_block = 4
    in_features = 11
    out_features = 12
    num_heads = 5
    dtype = jnp.float32

    inputs = random.normal(keys[0], (batch_size, seq_len, in_features))

    # Construct realistic packed inputs.
    new_segment_marker = random.bernoulli(keys[2], 0.1, (batch_size, seq_len))
    segment_ids = jnp.cumsum(new_segment_marker, axis=-1)
    # We make the last segment padding.
    is_padding = segment_ids == jnp.max(segment_ids, axis=-1, keepdims=True)
    inputs_mask = jnp.logical_not(is_padding)
    # Create positions based on segments.
    arange = np.broadcast_to(np.arange(seq_len), segment_ids.shape)
    positions = arange - np.maximum.accumulate(
        new_segment_marker * arange, axis=-1)
    positions *= inputs_mask

    relpos_bias = RelativePositionBiasesGeneral(
        num_heads=num_heads, num_buckets=32, max_distance=128, dtype=dtype)

    side_relpos_bias = RelativePositionBiasesGeneral(
        num_heads=num_heads, num_buckets=32, max_distance=128, dtype=dtype)

    att_layer = long_attention.EtcTransientGlobalSelfAttention(
        num_heads=num_heads,
        tokens_per_block=tokens_per_block,
        local_radius=local_radius,
        dtype=dtype,
        causal=causal,
        qkv_features=15,
        out_features=out_features,
        rescale_logits=use_kernel_fusion,
        split_head_kernel=use_kernel_fusion,
        kernels_to_fuse='kv' if use_kernel_fusion else None,
        relpos_bias=relpos_bias,
        side_relpos_bias=side_relpos_bias,
    )

    output, _ = att_layer.init_with_output(
        keys[3],
        inputs,
        inputs_mask,
        segment_ids=segment_ids,
        positions=positions,
        enable_dropout=False)

    np.testing.assert_array_equal(output.shape,
                                  (batch_size, seq_len, out_features))

  def test_make_etc_fixed_block_ids(self):
    # See this documentation for an example of what packed inputs look like:
    # https://github.com/google/seqio/blob/main/seqio/utils.py#L292
    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        ],
        dtype=np.bool_)
    segment_ids = [
        [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 0, 0],  #
        [1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
    ]
    positions = [
        [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0, 1, 2],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 0, 1, 2, 3, 0, 0],  #
        [0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7],  #
        list(range(16)),  #
    ]
    block_ids, global_segment_ids = long_attention.make_etc_fixed_block_ids(
        tokens_per_block=3,
        inputs_mask=inputs_mask,
        segment_ids=segment_ids,
        positions=positions)

    np.testing.assert_array_equal(
        block_ids,
        [
            [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4],  #
            [0, 0, 0, 1, 1, 1, 1, 1, -1, -1, 2, 2, 2, 2, -1, -1],  #
            [-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 1],  #
            [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4],  #
        ])
    np.testing.assert_array_equal(
        global_segment_ids,
        [
            [1, 1, 2, 2, 3],  #
            [1, 1, 3, 0, 0],  #
            [6, 6, 0, 0, 0],  #
            [1, 1, 1, 1, 1],  #
        ])

  def test_make_etc_fixed_block_ids_without_packing(self):
    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 0, 0, 0],  #
        ],
        dtype=np.bool_)

    block_ids, global_segment_ids = long_attention.make_etc_fixed_block_ids(
        tokens_per_block=3, inputs_mask=inputs_mask)

    np.testing.assert_array_equal(
        block_ids,
        [
            [0, 0, 0, 1, 1, 1, 1, 1],  #
            [0, 0, 0, 1, 1, 1, -1, -1],  #
            [0, 0, 0, 0, 0, -1, -1, -1],  #
        ])
    np.testing.assert_array_equal(
        global_segment_ids,
        [
            [1, 1],  #
            [1, 1],  #
            [1, 0],  #
        ])

  def test_make_etc_fixed_block_ids_without_orphan_adoption(self):
    # See this documentation for an example of what packed inputs look like:
    # https://github.com/google/seqio/blob/main/seqio/utils.py#L292
    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        ],
        dtype=np.bool_)
    segment_ids = [
        [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 0, 0],  #
        [1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
    ]
    positions = [
        [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0, 1, 2],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 0, 1, 2, 3, 0, 0],  #
        [0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7],  #
        list(range(16)),  #
    ]
    block_ids, global_segment_ids = long_attention.make_etc_fixed_block_ids(
        tokens_per_block=3,
        inputs_mask=inputs_mask,
        segment_ids=segment_ids,
        positions=positions,
        adopt_orphan_tokens=False)

    np.testing.assert_array_equal(
        block_ids,
        [
            [0, 0, 0, 1, 1, 1, -1, 2, 2, 2, 3, 3, 3, 4, 4, 4],  #
            [0, 0, 0, 1, 1, 1, -1, -1, -1, -1, 2, 2, 2, -1, -1, -1],  #
            [-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 1, 1, 1, -1, -1],  #
            [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, -1],  #
        ])
    np.testing.assert_array_equal(
        global_segment_ids,
        [
            [1, 1, 2, 2, 3],  #
            [1, 1, 3, 0, 0],  #
            [6, 6, 0, 0, 0],  #
            [1, 1, 1, 1, 1],  #
        ])

  def test_make_etc_fixed_block_ids_without_packing_nor_adoption(self):
    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 0, 0, 0],  #
        ],
        dtype=np.bool_)

    block_ids, global_segment_ids = long_attention.make_etc_fixed_block_ids(
        tokens_per_block=3, inputs_mask=inputs_mask, adopt_orphan_tokens=False)

    np.testing.assert_array_equal(
        block_ids,
        [
            [0, 0, 0, 1, 1, 1, -1, -1],  #
            [0, 0, 0, 1, 1, 1, -1, -1],  #
            [0, 0, 0, -1, -1, -1, -1, -1],  #
        ])
    np.testing.assert_array_equal(
        global_segment_ids,
        [
            [1, 1],  #
            [1, 1],  #
            [1, 0],  #
        ])

  def test_orphan_token_identification(self):
    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 0, 0, 0],  #
        ],
        dtype=np.bool_)

    orphan_tokens = long_attention.identify_orphan_tokens(
        tokens_per_block=3, inputs_mask=inputs_mask)
    np.testing.assert_array_equal(
        orphan_tokens,
        [
            [0, 0, 0, 0, 0, 0, 1, 1],  #
            [0, 0, 0, 0, 0, 0, 0, 0],  #
            [0, 0, 0, 1, 1, 0, 0, 0],  #
        ])

    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        ],
        dtype=np.bool_)
    positions = [
        [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0, 1, 2],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 0, 1, 2, 3, 0, 0],  #
        [0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7],  #
        list(range(16)),  #
    ]
    orphan_tokens = long_attention.identify_orphan_tokens(
        tokens_per_block=3, inputs_mask=inputs_mask, positions=positions)
    np.testing.assert_array_equal(
        orphan_tokens,
        [
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],  #
            [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],  #
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],  #
        ])

  def test_mask_to_bias(self):
    mask1 = np.array([1, 0], dtype=bool)
    bias1 = long_attention.mask_to_bias(mask1, dtype=np.float32)
    np.testing.assert_array_equal(bias1, np.array([0, -1e10], dtype=np.float32))
    assert bias1.dtype == np.float32

    mask2 = np.array([[1, 0], [0, 0]], dtype=bool)
    bias2 = long_attention.mask_to_bias(mask2, dtype=np.float32)
    np.testing.assert_array_equal(
        bias2, np.array([[0, -1e10], [-1e10, -1e10]], dtype=np.float32))
    assert bias2.dtype == np.float32

  def test_make_side_relpos(self):
    tokens_per_block = 3
    inputs_mask = np.array([
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    ])  #
    positions = np.array([
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3]
    ])  #
    segment_ids = np.array([
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2]
    ])  #

    side_relative_positions = long_attention._make_side_relpos(
        tokens_per_block,
        inputs_mask,
        segment_ids,
        positions,
        adopt_orphan_tokens=True)

    # certain biases are not important b/c they will be masked out; we represent
    # these with NaNs and ignore their positions in testing.
    x = np.nan
    expected_relative_positions = np.array([
        [
            [0, 1, 2, 3],  #
            [0, 1, 2, 3],  #
            [0, 1, 2, 3],  #
            [-1, 0, 1, 2],  #
            [-1, 0, 1, 2],  #
            [-1, 0, 1, 2],  #
            [-2, -1, 0, 1],  #
            [-2, -1, 0, 1],  #
            [-2, -1, 0, 1],  #
            [-3, -2, -1, 0],  #
            [-3, -2, -1, 0],  #
            [-3, -2, -1, 0]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [x, x, x, x],  #
            [x, x, x, x]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [x, x, x, 0],  #
            [x, x, x, 0],  #
            [x, x, x, 0]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [x, x, 0, x],  #
            [x, x, 0, x],  #
            [x, x, 0, x],  #
            [x, x, 0, x]
        ]
    ])  #
    positions_to_compare = np.isfinite(expected_relative_positions)
    np.testing.assert_array_equal(
        side_relative_positions[positions_to_compare],
        expected_relative_positions[positions_to_compare])

    side_relative_positions = long_attention._make_side_relpos(
        tokens_per_block,
        inputs_mask,
        segment_ids,
        positions,
        adopt_orphan_tokens=False)
    expected_relative_positions = np.array([
        [
            [0, 1, 2, 3],  #
            [0, 1, 2, 3],  #
            [0, 1, 2, 3],  #
            [-1, 0, 1, 2],  #
            [-1, 0, 1, 2],  #
            [-1, 0, 1, 2],  #
            [-2, -1, 0, 1],  #
            [-2, -1, 0, 1],  #
            [-2, -1, 0, 1],  #
            [-3, -2, -1, 0],  #
            [-3, -2, -1, 0],  #
            [-3, -2, -1, 0]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-3, -2, -1, x],  #
            [x, x, x, x],  #
            [x, x, x, x]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [x, x, x, 0],  #
            [x, x, x, 0],  #
            [x, x, x, 0]
        ],  #
        [
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [0, 1, 2, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-1, 0, 1, x],  #
            [-2, -1, 0, x],  #
            [-2, -1, 0, x],  #
            [x, x, 0, x],  #
            [x, x, 0, x],  #
            [x, x, 0, x],  #
            [x, x, -1, x]
        ]
    ])  #
    positions_to_compare = np.isfinite(expected_relative_positions)
    np.testing.assert_array_equal(
        side_relative_positions[positions_to_compare],
        expected_relative_positions[positions_to_compare])

    inputs_mask = np.array(
        [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
        ],
        dtype=np.bool_)
    segment_ids = [
        [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 0, 0],  #
        [1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6],  #
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],  #
    ]
    positions = [
        [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0, 1, 2],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 0, 1, 2, 3, 0, 0],  #
        [0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7],  #
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]  #
    ]
    side_relative_positions = long_attention._make_side_relpos(
        tokens_per_block,
        inputs_mask,
        segment_ids,
        positions,
        adopt_orphan_tokens=True)

    expected_relative_positions = np.array([
        [
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0]
        ],  #
        [
            [0, 1, x, x, x],  #
            [0, 1, x, x, x],  #
            [0, 1, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x]
        ],  #
        [
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3]
        ],  #
        [
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0]
        ]
    ])  #

    positions_to_compare = np.isfinite(expected_relative_positions)
    np.testing.assert_array_equal(
        side_relative_positions[positions_to_compare],
        expected_relative_positions[positions_to_compare])

    side_relative_positions = long_attention._make_side_relpos(
        tokens_per_block,
        inputs_mask,
        segment_ids,
        positions,
        adopt_orphan_tokens=False)

    expected_relative_positions = np.array([
        [
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0]
        ],  #
        [
            [0, 1, x, x, x],  #
            [0, 1, x, x, x],  #
            [0, 1, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-1, 0, x, x, x],  #
            [-2, -1, x, x, x],  #
            [-2, -1, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, 0, x, x],  #
            [x, x, -1, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x]
        ],  #
        [
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [x, x, x, x, x],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2]
        ],  #
        [
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [0, 1, 2, 3, 4],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-1, 0, 1, 2, 3],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-2, -1, 0, 1, 2],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-3, -2, -1, 0, 1],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-4, -3, -2, -1, 0],  #
            [-5, -4, -3, -2, -1]
        ]
    ])  #

    positions_to_compare = np.isfinite(expected_relative_positions)
    np.testing.assert_array_equal(
        side_relative_positions[positions_to_compare],
        expected_relative_positions[positions_to_compare])

if __name__ == '__main__':
  absltest.main()

max_stars_repo_name: tomweingarten/flaxformer

max_issues_repo_name: tomweingarten/flaxformer

max_forks_repo_name: tomweingarten/flaxformer

reason: import numpy,import jax,from jax


text:
import unittest
import numpy as np

from robo.acquisition_functions.ei import EI

from test.dummy_model import DemoModel

class TestEI(unittest.TestCase):

    def setUp(self):
        self.X = np.random.rand(10, 2)
        self.y = np.sinc(self.X * 10 - 5).sum(axis=1)
        self.model = DemoModel()
        self.model.train(self.X, self.y)

    def test_compute(self):
        ei = EI(self.model)

        X_test = np.random.rand(5, 2)
        a = ei.compute(X_test, derivative=False)
        assert a.shape[0] == X_test.shape[0]
        assert len(a.shape) == 1

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

max_stars_repo_name: lebrice/RoBO

max_issues_repo_name: lebrice/RoBO

max_forks_repo_name: lebrice/RoBO


text:
import sys
sys.path.append('../lib')

import numpy as np
import mlp

from sklearn import cross_validation
from sklearn import preprocessing

# http://archive.ics.uci.edu/ml/datasets/Letter+Recognition
fname = 'data/letter.csv'

n_instances = 2000

n_attributes = 16
attributes_cols = range(1, 17)

n_classes = 26
classes_col = 0

examples = np.genfromtxt(fname, delimiter=',', usecols=attributes_cols)
examples = preprocessing.MinMaxScaler().fit_transform(examples)

classes = np.genfromtxt(fname, dtype=None, delimiter=',', usecols=classes_col)

targets = np.zeros((n_instances, n_classes))
for class_, target in zip(classes, targets):
    index = ord(class_) - ord('A')
    target[index] = 1.0

nn = mlp.MLP([n_attributes + 1, 12, n_classes], debug=True)

kfolds = cross_validation.KFold(n_instances, n_folds=5, shuffle=True)
for train_indices, test_indices in kfolds:
    train_examples = examples[train_indices]
    train_targets = targets[train_indices]

    nn.fit(train_examples, train_targets, n_epochs=1000, learning_rate=0.2)

    test_examples = examples[test_indices]
    test_targets = targets[test_indices]

    count = 0.0
    for example, target in zip(test_examples, test_targets):
        count += target[nn.predict(example)]
    print "accuracy:", count / len(test_targets)

max_issues_repo_name: dreloong/mlp

max_forks_repo_name: dreloong/mlp


text:
"""
Unit tests for binomialp.py
"""
import math

from nose.tools import assert_equal, assert_almost_equal, assert_less, raises
from nose.plugins.attrib import attr
from scipy.stats import binom

from ..binomialp import binomial_p

def test_binomial_p():
    assert_almost_equal(binomial_p(5, 10, 0.5, 10**5, 'greater')[0], 1-binom.cdf(4, 10, 0.5), 2)
    assert_almost_equal(binomial_p(5, 10, 0.5, 10**5, 'less')[0], binom.cdf(5, 10, 0.5), 2)
    assert_almost_equal(binomial_p(5, 10, 0.5, 10**5, 'two-sided')[0], 1, 2)
    assert_equal(len(binomial_p(5, 10, 0.5, 10, 'greater', keep_dist=True)), 3)

    res1 = binomial_p(5, 10, 0.5, 10**2, 'greater', keep_dist=True, seed=12345)
    res2 = binomial_p(5, 10, 0.5, 10**2, 'greater', keep_dist=False, seed=12345)
    assert_equal(res1[0], res2[0])

    res1 = binomial_p(5, 10, 0.5, 10**2, 'less', keep_dist=True, seed=12345)
    res2 = binomial_p(5, 10, 0.5, 10**2, 'less', keep_dist=False, seed=12345)
    assert_equal(res1[0], res2[0])

    res1 = binomial_p(5, 10, 0.5, 10**2, 'two-sided', keep_dist=True, seed=12345)
    res2 = binomial_p(5, 10, 0.5, 10**2, 'two-sided', keep_dist=False, seed=12345)
    assert_equal(res1[0], res2[0])

@raises(ValueError)
def test_binomial_badinput():
    binomial_p(10, 5, 0.5)

max_stars_repo_name: SKFnordquist/permute

max_issues_repo_name: SKFnordquist/permute

max_forks_repo_name: SKFnordquist/permute

reason: from scipy


text:
import torch
import os
import xml.etree.ElementTree as ET
import numpy as np
import collections

from trash_lite_model import TrashLiteModel
from engine import train_one_epoch, evaluate
from PIL import Image
import utils
import transforms as T

class TrashDataset(object):
    def __init__(self, root, transforms, obj_types):
        self.root = root
        self.obj_types = obj_types
        self.transforms = transforms
        # load all image files, sorting them to
        # ensure that they are aligned
        annotation_files = list(sorted(os.listdir(os.path.join(root, "Annotations"))))
        self.annotation = []
        for afile in annotation_files:
            file_path = os.path.join(root, "Annotations", afile)
            anno_dict = self.parse_voc_xml(
                ET.parse(file_path).getroot())
            self.annotation.append(anno_dict)

    def parse_voc_xml(self, node):
        voc_dict = {}
        children = list(node)
        if children:
            def_dic = collections.defaultdict(list)
            for dc in map(self.parse_voc_xml, children):
                for ind, v in dc.items():
                    def_dic[ind].append(v)
            voc_dict = {
                node.tag:
                    {ind: v[0] if len(v) == 1 else v
                     for ind, v in def_dic.items()}
            }
        if node.text:
            text = node.text.strip()
            if not children:
                voc_dict[node.tag] = text
        return voc_dict

    def __getitem__(self, idx):
        annotation = self.annotation[idx]['annotation']
        img_path = os.path.join(self.root, "JPEGImages", annotation['folder'], annotation['filename'])
        img = Image.open(img_path).convert("RGB")
        num_objs = len(annotation['object'])
        boxes = []
        labels = []
        if not isinstance(annotation['object'], list):
            annotation['object'] = [annotation['object']]
        for obj in annotation['object']:
            bnbox = obj['bndbox']
            boxes.append([float(bnbox['xmin']), float(bnbox['ymin']), float(bnbox['xmax']), float(bnbox['ymax'])])
            labels.append(self.obj_types[obj['name']])

        boxes = torch.as_tensor(boxes, dtype=torch.float32)
        # there is only one class
        labels = torch.LongTensor(labels)

        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs,), dtype=torch.int64)

        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        target["image_id"] = image_id
        target["area"] = area
        target["iscrowd"] = iscrowd

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target

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

def get_transform(train):
    transforms = []
    transforms.append(T.ToTensor())
    if train:
        transforms.append(T.RandomHorizontalFlip(0.5))
    return T.Compose(transforms)

num_classes = 3 #Background, Shoe, Cola
classes = {"Shoes":1,"Cola":2}
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

model = TrashLiteModel(num_classes)
dataset = TrashDataset("Dataset",get_transform(train=True),classes)
dataset_test = TrashDataset('Dataset', get_transform(train=False),classes)

# # split the dataset in train and test set
# indices = torch.randperm(len(dataset)).tolist()
# dataset = torch.utils.data.Subset(dataset, indices[:-50])
# dataset_test = torch.utils.data.Subset(dataset_test, indices[-50:])

# define training and validation data loaders
data_loader = torch.utils.data.DataLoader(
    dataset, batch_size=2, shuffle=True, num_workers=4,
    collate_fn=utils.collate_fn)

data_loader_test = torch.utils.data.DataLoader(
    dataset_test, batch_size=1, shuffle=False, num_workers=4,
    collate_fn=utils.collate_fn)

# construct an optimizer
params = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.005,
                            momentum=0.9, weight_decay=0.0005)
# and a learning rate scheduler
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                               step_size=3,
                                               gamma=0.1)

model.to(device)
num_epochs = 10
for epoch in range(num_epochs):
    # train for one epoch, printing every 10 iterations
    print("Epoch: "+ str(epoch))
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
    # update the learning rate
    lr_scheduler.step()
    #evaluate on the test dataset
    #evaluate(model, data_loader_test, device=device)
model.eval()
torch.save(model.state_dict(), "./trash.pt")

max_stars_repo_name: CyranoChen/jarvis

max_issues_repo_name: CyranoChen/jarvis

max_forks_repo_name: CyranoChen/jarvis