text:
#!/usr/bin/python

import platform; print(platform.platform())
import sys; print("Python", sys.version)
import os
import numpy as np
import pandas as pd
from joblib import load
from sklearn.metrics import mean_squared_error

# set environment variables
MODEL_FILE = os.environ["MODEL_FILE"]
SCALER_FILE = os.environ["SCALER_FILE"]
DATA_FILE = os.environ["DATA_FILE"]
DATA_DIR = os.environ["DATA_DIR"]
DATA_PATH = os.path.join(DATA_DIR, DATA_FILE)

def inference():
    # load docker_data and transform docker_data
    data = pd.read_csv(DATA_PATH)
    scaler = load(SCALER_FILE)
    data_scaled = scaler.transform(data)

    X, y = data_scaled[:, :-1], data_scaled[:, -1]

    # load model, make and evaluate predictions
    reg_model = load(MODEL_FILE)
    preds = reg_model.predict(X)
    rmse = np.sqrt(mean_squared_error(y, preds))
    print("RMSE: %f" % rmse)

    # save predictions
    output_path = os.path.join(DATA_DIR, 'output.csv')
    pd.DataFrame(preds).to_csv(output_path)

if __name__ == '__main__':
    inference()

max_stars_repo_name: danielhkt/containerize-me

max_issues_repo_name: danielhkt/containerize-me

max_forks_repo_name: danielhkt/containerize-me


text:
from flask import Flask, render_template, Response
# from camera import VideoCamera

import cv2
import imutils
import time
import face_recognition
import numpy as np
import pickle

data = pickle.loads(open("encodings.pickle", "rb").read())
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, frame = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.

        # convert the input frame from BGR to RGB then resize it to have
        # a width of 750px (to speedup processing)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        rgb = imutils.resize(frame, width=750)
        r = frame.shape[1] / float(rgb.shape[1])

        # detect the (x, y)-coordinates of the bounding boxes
        # corresponding to each face in the input frame, then compute
        # the facial embeddings for each face
        boxes = face_recognition.face_locations(rgb, model="hog")
        encodings = face_recognition.face_encodings(rgb, boxes)
        names = []

        # loop over the facial embeddings
        for encoding in encodings:
            # attempt to match each face in the input image to our known
            # encodings
            matches = face_recognition.compare_faces(data["encodings"], encoding, tolerance=0.5)
            name = "Unknown"

            # check to see if we have found a match
            if True in matches:
                # find the indexes of all matched faces then initialize a
                # dictionary to count the total number of times each face
                # was matched
                matchedIdxs = [i for (i, b) in enumerate(matches) if b]
                counts = {}

                # loop over the matched indexes and maintain a count for
                # each recognized face face
                for i in matchedIdxs:
                    name = data["names"][i]
                    counts[name] = counts.get(name, 0) + 1

                # determine the recognized face with the largest number
                # of votes (note: in the event of an unlikely tie Python
                # will select first entry in the dictionary)
                name = max(counts, key=counts.get)

            names.append(name)
            # print(name)

        # loop over the recognized faces
        for ((top, right, bottom, left), name) in zip(boxes, names):
            # rescale the face coordinates
            top = int(top * r)
            right = int(right * r)
            bottom = int(bottom * r)
            left = int(left * r)

            # draw the predicted face name on the image
            cv2.rectangle(frame, (left, top), (right, bottom),
                (0, 255, 0), 2)
            y = top - 15 if top - 15 > 15 else top + 15
            # cv2.putText(img, text, position, font, fontScale, color, thickness, lineType, bottomLeftOrigin)
            cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)

        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()

ext: py

reason: import numpy


text:
"""
Glycan class
still very buggy, idiosyncratic, fragile and completely ad-hoc
"""
import numpy as np
import pandas as pd
from .biomolecules import Biomolecule
from ..templates.glycans import templates_gl
from ..utils.constants import constants
from ..utils.pdbIO import _builder_from_pdb_gl
from ..utils.geometry import get_torsional, set_torsional

class Glycan(Biomolecule):
    """Glycan object"""

    def __init__(self, pdb=None, linkages=None):
        """initialize a new Glycan from a PDB

        Parameters
        ----------
        pdf : file
            Protein data bank file.
            For the moment this will only work with "nice" files:
        Returns
        ----------
        Glycan object
        """
        if pdb is not None:
            (self.sequence,
             self.coords,
             self._names,
             self._elements,
             self.occupancies,
             self.bfactors,
             self._offsets,
             self._exclusions) = _builder_from_pdb_gl(pdb, 'glycan',
                                                      regularize=True,
                                                      linkages=linkages)

            self._rotation_indices = _get_rotation_indices_gl(self,
                                                              linkages=linkages)
        else:
            "Please provide a sequence or a pdb file"

    def at_coords(self, resnum, selection=None):
        """
        Returns the coordinate of an specified residue and atom (optionally)

        Parameters
        ----------
        resnum : int
            residue number from which to obtain the coordinates
        selection : string or None
            selection from which to obtain the coordinates. If none is provided
            it will return the coordinates of the whole residue (default). Use
            a valid atom name.

        Returns
        ----------
        coords: array
            Cartesian coordinates of a given residue or a subset of atoms in a
            given residue.
        """
        offsets = self._offsets
        offset_0, offset_1 = offsets[resnum], offsets[resnum + 1]
        rescoords = self.coords[offset_0: offset_1]

        if selection is None:
            return rescoords
        else:
            resname = self.sequence[resnum]
            resinfo = templates_gl[resname]
            idx = resinfo.atom_names.index(selection)
            return rescoords[idx]

    def get_phi(self, resnum):
        """
        Compute the dihedral angle phi (OR-C1-O'x-C'x)

        Parameters
        ----------
        resnum : int
            residue number from which to compute torsional
        """
        if resnum < len(self) - 1:
            coords = self.coords
            _, _, _, m, n, o, p = self._rotation_indices[resnum]['phi']
            a = coords[m]
            b = coords[n]
            c = coords[o]
            d = coords[p]
            return get_torsional(a, b, c, d) * constants.radians_to_degrees
        else:
            return np.nan

    def get_psi(self, resnum):
        """
        Compute the dihedral angle psi (C1-O'x-C'x-C'x-1)

        Parameters
        ----------
        resnum : int
            residue number from which to compute torsional
        """
        if resnum < len(self) - 1:
            coords = self.coords
            _, _, _, m, n, o, p = self._rotation_indices[resnum]['psi']
            a = coords[m]
            b = coords[n]
            c = coords[o]
            d = coords[p]
            return get_torsional(a, b, c, d) * constants.radians_to_degrees
        else:
            return np.nan

    def set_phi(self, resnum, theta):
        """
        set the phi torsional angle (OR-C1-O'x-C'x) to the value theta

        Parameters
        ----------
        resnum : int
            residue number from which to compute torsional
        theta : float
            value of the angle to set in degrees
        """
        if resnum + 1 < len(self):
            theta_rad = (self.get_phi(resnum) - theta) * \
                constants.degrees_to_radians
            xyz = self.coords
            i, j, idx_rot, _, _, _, _ = self._rotation_indices[resnum]['phi']
            set_torsional(xyz, i, j, idx_rot, theta_rad)

    def set_psi(self, resnum, theta):
        """
        set the psi torsional angle (C1-O'x-C'x-C'x-1) to the value theta

        Parameters
        ----------
        resnum : int
            residue number from which to compute torsional
        theta : float
            value of the angle to set in degrees
        """
        if resnum + 1 < len(self):
            theta_rad = (self.get_psi(resnum) - theta) * \
                constants.degrees_to_radians
            xyz = self.coords
            i, j, idx_rot, _, _, _, _ = self._rotation_indices[resnum]['psi']
            set_torsional(xyz, i, j, idx_rot, theta_rad)

    def get_torsionals(self,  n_digits=2):
        """
        Compute all phi, psi and chi torsional angles of a given molecule

        Parameters
        ----------
        n_digits : int
            Number of decimal digits used to round the torsional values
            (default 2 digits).

        Returns
        ----------
        DataFrame with the glycan sequence and torsional angles.

        """
        all_tors = []
        for i, res in enumerate(self.sequence[:-1]):
            tors = []
            tors.append(round(self.get_phi(i), n_digits))
            tors.append(round(self.get_psi(i), n_digits))
            all_tors.append([res] + tors)
        else:
            labels = ['res', 'phi', 'psi']
        df = pd.DataFrame.from_records(all_tors, columns=labels)
        return df

def _get_rotation_indices_gl(self, linkages):
    """
    Precompute indices that are then used to rotate along the i-j axis only the
    proper portion of the coordinates array. Works only for glycans.

    Returns
    rotation_indices : dict of dict of tuples
        dictionary of residue numbers, dicctionary of torsionals (phi or psi),
    7-element tuple: The first 3 elements are atom i (int), atom j (int)
    indices of the atoms that will be rotated (array). Last four elements atom
    ids for the atoms defining a torsional angle.
    """
    rotation_indices = []
    lenght = len(self.coords)
    offsets = self._offsets
    seq = self.sequence
    rotation_indices = {}
    for resnum in range(0, len(self) - 1):
        res = resnum
        d = {}
        linkage = linkages[resnum]
        if isinstance(linkage, tuple):
            resnum = linkage[0]
            linkage = linkage[1]
        if linkage > 0:  # forward reading
            this = offsets[resnum]  # index of C1
            post = offsets[resnum + 1]
            resname_this = seq[resnum]
            resname_post = seq[resnum + 1]
            pre_idx_rot = list(range(post, lenght))
        else:  # backward reading
            this = offsets[resnum + 1]  # index of C1
            post = offsets[resnum]
            resname_this = seq[resnum + 1]
            resname_post = seq[resnum]
            pre_idx_rot = list(range(0, this))
            linkage = abs(linkage)

        template_at_names_this = templates_gl[resname_this].atom_names
        template_at_names_post = templates_gl[resname_post].atom_names
        OR_idx = template_at_names_this.index('OR')
        O_idx = template_at_names_post.index('O{}'.format(linkage))
        C_idx = template_at_names_post.index('C{}'.format(linkage))
        # following IUPAC for 1-1 bonds use C'x+1 instead of C'x-1
        # check http://www.glycosciences.de/spec/ppc/ and
        # http://www.chem.qmul.ac.uk/iupac/2carb/ for details
        if linkage == 1:
            fourth_point = linkage + 1
        else:
            fourth_point = linkage - 1
        C__idx = template_at_names_post.index('C{}'.format(fourth_point))

        ###  phi  ###
        j = post + O_idx
        l = post + C_idx
        # making idx_rot an array makes rotation faster later
        idx_rot = np.asarray(pre_idx_rot)
        # the terms of the tuple are the indices of:
        # (two atoms defining the axis of rotation, the atoms that will be rotated)
        # and (OR-C1-O'x-C'x)
        d['phi'] = this, j, idx_rot, this + OR_idx, this, j, l

        ### psi ###
        pre_idx_rot.remove(j)
        #if linkages[resnum] > 0:
        #    pre_idx_rot.remove(j)
        #else:
        #    pre_idx_rot.append(j)
        # making idx_rot an array makes rotation faster later
        idx_rot = np.asarray(pre_idx_rot)
        # the terms of the tuple are the indices of:
        # (two atoms defining the axis of rotation, the atoms that will be rotated)
        # (C1-O'x-C'x-C'x-1)
        d['psi'] = j, l, idx_rot, this, j, l, post + C__idx
        rotation_indices[res] = d
    return rotation_indices


text:
import os,sys, math
import torch
import numpy                    as np
import torchvision.transforms   as transforms
from datasets                   import pascal3d, pascal3d_kp

from IPython import embed
from .Paths import *

root_dir     = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
dataset_root = pascal3d_root

def get_data_loaders(dataset, batch_size, num_workers, model, num_classes = 12):

    image_size = 227
    train_transform   = transforms.Compose([transforms.ToTensor(),
                        transforms.Normalize(   mean=(0., 0., 0.),
                                                 std=(1./255., 1./255., 1./255.)
                                             ),
                        transforms.Normalize(   mean=(104, 116.668, 122.678),
                                                std=(1., 1., 1.)
                                            )
                        ])

    test_transform   = transforms.Compose([transforms.ToTensor(),
                        transforms.Normalize(   mean=(0., 0., 0.),
                                                 std=(1./255., 1./255., 1./255.)
                                             ),
                        transforms.Normalize(   mean=(104, 116.668, 122.678),
                                                std=(1., 1., 1.)
                                            )
                        ])

    # # The New transform for ImageNet Stuff
    # new_transform   = transforms.Compose([
    #                                     transforms.ToTensor(),
    #                                     transforms.Normalize(mean=(0.485, 0.456, 0.406),
    #                                     std=(0.229, 0.224, 0.225))])

    if dataset == "pascal":
        csv_train = os.path.join(root_dir, 'data/pascal3d_train.csv')
        csv_test  = os.path.join(root_dir, 'data/pascal3d_valid.csv')

        train_set = pascal3d(csv_train, dataset_root= dataset_root, transform = train_transform, im_size = image_size)
        test_set  = pascal3d(csv_test,  dataset_root= dataset_root, transform = test_transform,  im_size = image_size)
    elif dataset == "pascalEasy":
        csv_train = os.path.join(root_dir, 'data/pascal3d_train_easy.csv')
        csv_test  = os.path.join(root_dir, 'data/pascal3d_valid_easy.csv')

        train_set = pascal3d(   csv_train, dataset_root= dataset_root,
                                transform = train_transform, im_size = image_size)
        test_set  = pascal3d(   csv_test,  dataset_root= dataset_root,
                                transform = test_transform,  im_size = image_size)

    elif dataset == "pascalVehKP":
        csv_train = os.path.join(root_dir, 'data/veh_pascal3d_kp_train.csv')
        csv_test  = os.path.join(root_dir, 'data/veh_pascal3d_kp_valid.csv')

        train_set = pascal3d_kp(csv_train,
                                dataset_root= dataset_root,
                                transform = train_transform,
                                im_size = image_size,
                                num_classes = num_classes)

        test_set  = pascal3d_kp(csv_test,
                                dataset_root= dataset_root,
                                transform = test_transform,
                                im_size = image_size,
                                num_classes = num_classes)

    elif dataset == "pascalKP":
        csv_train = os.path.join(root_dir, 'data/pascal3d_kp_train.csv')
        csv_test  = os.path.join(root_dir, 'data/pascal3d_kp_valid.csv')

        train_set = pascal3d_kp(csv_train, dataset_root= dataset_root, transform = train_transform, im_size = image_size)
        test_set  = pascal3d_kp(csv_test,  dataset_root= dataset_root, transform = test_transform,  im_size = image_size)
    else:
        print("Error in load_datasets: Dataset name not defined.")

    # Generate data loaders
    train_loader = torch.utils.data.DataLoader( dataset = train_set,
                                                batch_size =batch_size,
                                                shuffle = True ,
                                                num_workers =num_workers,
                                                drop_last   = True)

    test_loader  = torch.utils.data.DataLoader( dataset=test_set,
                                                batch_size=batch_size,
                                                shuffle = False,
                                                num_workers=num_workers,
                                                drop_last = False)

    return train_loader, test_loader

max_stars_repo_name: mbanani/pytorch-clickhere-cnn

max_issues_repo_name: mbanani/pytorch-clickhere-cnn

max_forks_repo_name: mbanani/pytorch-clickhere-cnn


text:
# preprocess data
import numpy as np
import re

def get_most_common_features(target, all_features, max = 3, min = 3):
    res = []
    main_keys = target.split('_')

    for feature in all_features:
        if target == feature:
            continue

        f_keys = feature.split('_')
        common_key_num = len(list(set(f_keys) & set(main_keys)))

        if common_key_num >= min and common_key_num <= max:
            res.append(feature)

    return res

def build_net(target, all_features):
    # get edge_indexes, and index_feature_map
    main_keys = target.split('_')
    edge_indexes = [
        [],
        []
    ]
    index_feature_map = [target]

    # find closest features(nodes):
    parent_list = [target]
    graph_map = {}
    depth = 2

    for i in range(depth):
        for feature in parent_list:
            children = get_most_common_features(feature, all_features)

            if feature not in graph_map:
                graph_map[feature] = []

            # exclude parent
            pure_children = []
            for child in children:
                if child not in graph_map:
                    pure_children.append(child)

            graph_map[feature] = pure_children

            if feature not in index_feature_map:
                index_feature_map.append(feature)
            p_index = index_feature_map.index(feature)
            for child in pure_children:
                if child not in index_feature_map:
                    index_feature_map.append(child)
                c_index = index_feature_map.index(child)

                edge_indexes[1].append(p_index)
                edge_indexes[0].append(c_index)

        parent_list = pure_children

    return edge_indexes, index_feature_map

def construct_data(data, feature_map, labels=0):
    res = []

    for feature in feature_map:
        if feature in data.columns:
            res.append(data.loc[:, feature].values.tolist())
        else:
            print(feature, 'not exist in data')
    # append labels as last
    sample_n = len(res[0])

    if type(labels) == int:
        res.append([labels]*sample_n)
    elif len(labels) == sample_n:
        res.append(labels)

    return res

def build_loc_net(struc, all_features, feature_map=[]):

    index_feature_map = feature_map
    edge_indexes = [
        [],
        []
    ]
    for node_name, node_list in struc.items():
        if node_name not in all_features:
            continue

        if node_name not in index_feature_map:
            index_feature_map.append(node_name)

        p_index = index_feature_map.index(node_name)
        for child in node_list:
            if child not in all_features:
                continue

            if child not in index_feature_map:
                print(f'error: {child} not in index_feature_map')
                # index_feature_map.append(child)

            c_index = index_feature_map.index(child)
            # edge_indexes[0].append(p_index)
            # edge_indexes[1].append(c_index)
            edge_indexes[0].append(c_index)
            edge_indexes[1].append(p_index)

    return edge_indexes

max_stars_repo_name: pinaryazgan/GDN

max_issues_repo_name: pinaryazgan/GDN

max_forks_repo_name: pinaryazgan/GDN


text:
# This piece of software is bound by The MIT License (MIT)
# Copyright (c) 2014 Siddharth Agrawal
# Code written by : Siddharth Agrawal
# Email ID : siddharth.950@gmail.com

import numpy
import math
import time
import scipy.io
import scipy.optimize
import matplotlib.pyplot

###########################################################################################
""" The Sparse Autoencoder Linear class """

class SparseAutoencoderLinear(object):

    #######################################################################################
    """ Initialization of Autoencoder object """

    def __init__(self, visible_size, hidden_size, rho, lamda, beta):

        """ Initialize parameters of the Autoencoder object """

        self.visible_size = visible_size    # number of input units
        self.hidden_size = hidden_size      # number of hidden units
        self.rho = rho                      # desired average activation of hidden units
        self.lamda = lamda                  # weight decay parameter
        self.beta = beta                    # weight of sparsity penalty term

        """ Set limits for accessing 'theta' values """

        self.limit0 = 0
        self.limit1 = hidden_size * visible_size
        self.limit2 = 2 * hidden_size * visible_size
        self.limit3 = 2 * hidden_size * visible_size + hidden_size
        self.limit4 = 2 * hidden_size * visible_size + hidden_size + visible_size

        """ Initialize Neural Network weights randomly
            W1, W2 values are chosen in the range [-r, r] """

        r = math.sqrt(6) / math.sqrt(visible_size + hidden_size + 1)

        rand = numpy.random.RandomState(int(time.time()))

        W1 = numpy.asarray(rand.uniform(low = -r, high = r, size = (hidden_size, visible_size)))
        W2 = numpy.asarray(rand.uniform(low = -r, high = r, size = (visible_size, hidden_size)))

        """ Bias values are initialized to zero """

        b1 = numpy.zeros((hidden_size, 1))
        b2 = numpy.zeros((visible_size, 1))

        """ Create 'theta' by unrolling W1, W2, b1, b2 """

        self.theta = numpy.concatenate((W1.flatten(), W2.flatten(),
                                        b1.flatten(), b2.flatten()))

    #######################################################################################
    """ Returns elementwise sigmoid output of input array """

    def sigmoid(self, x):

        return (1 / (1 + numpy.exp(-x)))

    #######################################################################################
    """ Returns the cost of the Autoencoder and gradient at a particular 'theta' """

    def sparseAutoencoderLinearCost(self, theta, input):

        """ Extract weights and biases from 'theta' input """

        W1 = theta[self.limit0 : self.limit1].reshape(self.hidden_size, self.visible_size)
        W2 = theta[self.limit1 : self.limit2].reshape(self.visible_size, self.hidden_size)
        b1 = theta[self.limit2 : self.limit3].reshape(self.hidden_size, 1)
        b2 = theta[self.limit3 : self.limit4].reshape(self.visible_size, 1)

        """ Compute output layers by performing a feedforward pass
            Computation is done for all the training inputs simultaneously """

        hidden_layer = self.sigmoid(numpy.dot(W1, input) + b1)
        output_layer = numpy.dot(W2, hidden_layer) + b2

        """ Estimate the average activation value of the hidden layers """

        rho_cap = numpy.sum(hidden_layer, axis = 1) / input.shape[1]

        """ Compute intermediate difference values using Backpropagation algorithm """

        diff = output_layer - input

        sum_of_squares_error = 0.5 * numpy.sum(numpy.multiply(diff, diff)) / input.shape[1]
        weight_decay         = 0.5 * self.lamda * (numpy.sum(numpy.multiply(W1, W1)) +
                                                   numpy.sum(numpy.multiply(W2, W2)))
        KL_divergence        = self.beta * numpy.sum(self.rho * numpy.log(self.rho / rho_cap) +
                                                    (1 - self.rho) * numpy.log((1 - self.rho) / (1 - rho_cap)))
        cost                 = sum_of_squares_error + weight_decay + KL_divergence

        KL_div_grad = self.beta * (-(self.rho / rho_cap) + ((1 - self.rho) / (1 - rho_cap)))

        del_out = diff
        del_hid = numpy.multiply(numpy.dot(numpy.transpose(W2), del_out) + numpy.transpose(numpy.matrix(KL_div_grad)),
                                 numpy.multiply(hidden_layer, 1 - hidden_layer))

        """ Compute the gradient values by averaging partial derivatives
            Partial derivatives are averaged over all training examples """

        W1_grad = numpy.dot(del_hid, numpy.transpose(input))
        W2_grad = numpy.dot(del_out, numpy.transpose(hidden_layer))
        b1_grad = numpy.sum(del_hid, axis = 1)
        b2_grad = numpy.sum(del_out, axis = 1)

        W1_grad = W1_grad / input.shape[1] + self.lamda * W1
        W2_grad = W2_grad / input.shape[1] + self.lamda * W2
        b1_grad = b1_grad / input.shape[1]
        b2_grad = b2_grad / input.shape[1]

        """ Transform numpy matrices into arrays """

        W1_grad = numpy.array(W1_grad)
        W2_grad = numpy.array(W2_grad)
        b1_grad = numpy.array(b1_grad)
        b2_grad = numpy.array(b2_grad)

        """ Unroll the gradient values and return as 'theta' gradient """

        theta_grad = numpy.concatenate((W1_grad.flatten(), W2_grad.flatten(),
                                        b1_grad.flatten(), b2_grad.flatten()))

        return [cost, theta_grad]

###########################################################################################
""" Preprocesses the dataset using ZCA Whitening """

def preprocessDataset(data, num_patches, epsilon):

    """ Subtract mean of each patch separately """

    mean_patch = numpy.mean(data, axis = 1, keepdims = True)
    data       = data - mean_patch

    """ Compute the ZCA Whitening matrix """

    sigma           = numpy.dot(data, numpy.transpose(data)) / num_patches
    [u, s, v]       = numpy.linalg.svd(sigma)
    rescale_factors = numpy.diag(1 / numpy.sqrt(s + epsilon))
    zca_white       = numpy.dot(numpy.dot(u, rescale_factors), numpy.transpose(u));

    """ Apply ZCA Whitening to the data """

    data = numpy.dot(zca_white, data)

    return data, zca_white, mean_patch

###########################################################################################
""" Loads the image patches from the mat file """

def loadDataset():

    """ Loads the dataset as a numpy array
        The dataset is originally read as a dictionary """

    images = scipy.io.loadmat('stlSampledPatches.mat')
    images = numpy.array(images['patches'])

    return images

###########################################################################################
""" Visualizes the obtained optimal W1 values as images """

def visualizeW1(opt_W1, vis_patch_side, hid_patch_side):

    """ Add the weights as a matrix of images """

    figure, axes = matplotlib.pyplot.subplots(nrows = hid_patch_side,
                                              ncols = hid_patch_side)

    """ Rescale the values from [-1, 1] to [0, 1] """

    opt_W1 = (opt_W1 + 1) / 2

    """ Define useful values """

    index  = 0
    limit0 = 0
    limit1 = limit0 + vis_patch_side * vis_patch_side
    limit2 = limit1 + vis_patch_side * vis_patch_side
    limit3 = limit2 + vis_patch_side * vis_patch_side

    for axis in axes.flat:

        """ Initialize image as array of zeros """

        img = numpy.zeros((vis_patch_side, vis_patch_side, 3))

        """ Divide the rows of parameter values into image channels """

        img[:, :, 0] = opt_W1[index, limit0 : limit1].reshape(vis_patch_side, vis_patch_side)
        img[:, :, 1] = opt_W1[index, limit1 : limit2].reshape(vis_patch_side, vis_patch_side)
        img[:, :, 2] = opt_W1[index, limit2 : limit3].reshape(vis_patch_side, vis_patch_side)

        """ Plot the image on the figure """

        image = axis.imshow(img, interpolation = 'nearest')
        axis.set_frame_on(False)
        axis.set_axis_off()
        index += 1

    """ Show the obtained plot """

    matplotlib.pyplot.show()

###########################################################################################
""" Loads data, trains the Autoencoder and visualizes the learned weights """

def executeSparseAutoencoderLinear():

    """ Define the parameters of the Autoencoder """

    image_channels  = 3      # number of channels in the image patches
    vis_patch_side  = 8      # side length of sampled image patches
    hid_patch_side  = 20     # side length of representative image patches
    num_patches     = 100000 # number of training examples
    rho             = 0.035  # desired average activation of hidden units
    lamda           = 0.003  # weight decay parameter
    beta            = 5      # weight of sparsity penalty term
    max_iterations  = 400    # number of optimization iterations
    epsilon         = 0.1    # regularization constant for ZCA Whitening

    visible_size = vis_patch_side * vis_patch_side * image_channels # number of input units
    hidden_size  = hid_patch_side * hid_patch_side                  # number of hidden units

    """ Load the dataset and preprocess using ZCA Whitening """

    training_data = loadDataset()
    training_data, zca_white, mean_patch = preprocessDataset(training_data, num_patches, epsilon)

    """ Initialize the Autoencoder with the above parameters """

    encoder = SparseAutoencoderLinear(visible_size, hidden_size, rho, lamda, beta)

    """ Run the L-BFGS algorithm to get the optimal parameter values """

    opt_solution  = scipy.optimize.minimize(encoder.sparseAutoencoderLinearCost, encoder.theta,
                                            args = (training_data,), method = 'L-BFGS-B',
                                            jac = True, options = {'maxiter': max_iterations})
    opt_theta     = opt_solution.x
    opt_W1        = opt_theta[encoder.limit0 : encoder.limit1].reshape(hidden_size, visible_size)

    """ Visualize the obtained optimal W1 weights """

    visualizeW1(numpy.dot(opt_W1, zca_white), vis_patch_side, hid_patch_side)

executeSparseAutoencoderLinear()

max_stars_repo_name: mrgloom/Sparse-Autoencoder-Linear

max_issues_repo_name: mrgloom/Sparse-Autoencoder-Linear

max_forks_repo_name: mrgloom/Sparse-Autoencoder-Linear

reason: import numpy,import scipy


text:
# This script is an example of how we would calculate the gibbs formation of a compound. the input file contains
# reactions with single metabolites on the product side.

# this method is not recommended by Elad Noor and Ron Milo.

import numpy as np
import scipy.stats as st
from component_contribution.thermodynamic_constants import default_RT
from component_contribution.component_contribution_trainer import ComponentContribution
from component_contribution.all_model import AllModel
from component_contribution.CfB_functions import add_thermo_comp_info, calc_concentration
import os

# Physiological conditions
pH = 7.2
I = 0.1        # ionic strength (M)
T = 298.15     # temperature    (K)
conc_mM = 1.0  # comcentration  (mM)

# Confidence level
conf = 0.95

# INPUT Reactions as file or string of MOL files
example_path = os.path.dirname(os.path.realpath(__file__))
with open('../examples/example_bigg_mets', 'r') as fp:

    reaction_strings = fp.readlines()

# Parse the reaction strings into a "KEGG" model
model = AllModel.from_formulas(reaction_strings, 'bigg', raise_exception=True)

# Train component-contribution predictive model
cc = ComponentContribution.init()

# Add the component-contribution estimation for reaction's untransformed dG0
# (i.e. using major MS at pH 7 for each of the reactants)
# model.add_thermo(cc)
add_thermo_comp_info(model, cc)

# Change in Gibbs free energy at a particular pH and ionic strength
dG0_prime, dG0_std, sqrt_Sigma = model.get_transformed_dG0(pH,I,T)

# Critical value for margin of error
Z = st.norm.ppf(1-(1-conf)/2)

# Print results
for i, rxn_str in enumerate(reaction_strings):
    print '-' * 60
    print rxn_str.strip()
    #  Gibbs formation energy in standard conditions
    print "dG0  = %8.1f +- %5.1f kj/mol" % (model.dG0[i, 0], dG0_std[i, 0] * Z)
    # Gibbs formation energy at a particular pH and ionic strength
    print "dG'0 = %8.1f +- %5.1f kj/mol" % (dG0_prime[i, 0], dG0_std[i, 0] * Z)

max_stars_repo_name: biosustain/component-contribution

max_issues_repo_name: biosustain/component-contribution

max_forks_repo_name: biosustain/component-contribution


text:
#CASA script to create cutouts of fits cubes

directoryA = '/Volumes/TARDIS/Work/askap/'
directoryB = '/Volumes/NARNIA/pilot_cutouts/'

import numpy as np
sources=np.loadtxt('/Users/emma/GitHub/possum-tools/DataProcess/pilot_sources.txt',dtype='str')

for i in range(0,sources.shape[0]):
	objectname=sources[i,0]
	POSSUMSB=sources[i,3]
	EMUSB=sources[i,4]
	ra=sources[i,1]
	dec=sources[i,2]
	sourcecentre=ra+','+dec
	fov=sources[i,6]#arcsec
	print(objectname)

	region='centerbox[['+sourcecentre+'], ['+fov+'arcsec, '+fov+'arcsec]]'
	possum_outfile=directoryB+objectname+'/'+objectname+'_POSSUM.fits'
	emu_outfile=directoryB+objectname+'/'+objectname+'_EMU.fits'

	#POSSUM
	if POSSUMSB == '5038':
		#this is the Early Science data
		possum_cont_filename = '/Volumes/NARNIA/PawseySync/DRAGN_1_0p8_A/DRAGN_1_0p8_A/image.i.SB5038.cont.restored.fits'

	else:
		possum_cont_filename = directoryA +'fullfields/image.i.SB'+POSSUMSB+'.cont.taylor.0.restored.fits'

	if POSSUMSB == '10035':
		print('Skipping POSSUM: bad SB10035')
	else:
		imsubimage(imagename=possum_cont_filename,outfile='possum_cont_temp',region=region,overwrite=True,dropdeg=True)
		exportfits(imagename='possum_cont_temp',fitsimage=possum_outfile,overwrite=True)

		#cubes
		i_filename = '/Volumes/NARNIA/leakage_corrected/image.restored.i.SB'+POSSUMSB+'.contcube.linmos.13arcsec.leakage.zernike.holoI.fits'
		q_filename = '/Volumes/NARNIA/leakage_corrected/image.restored.q.SB'+POSSUMSB+'.contcube.linmos.13arcsec.leakage.zernike.holoI.fits'
		u_filename = '/Volumes/NARNIA/leakage_corrected/image.restored.u.SB'+POSSUMSB+'.contcube.linmos.13arcsec.leakage.zernike.holoI.fits'

		imsubimage(imagename=i_filename,outfile='i_im_temp',region=region,overwrite=True,dropdeg=True)
		imsubimage(imagename=q_filename,outfile='q_im_temp',region=region,overwrite=True,dropdeg=True)
		imsubimage(imagename=u_filename,outfile='u_im_temp',region=region,overwrite=True,dropdeg=True)
		exportfits(imagename='i_im_temp',fitsimage=objectname+'_POSSUM_i.fits',overwrite=True)
		exportfits(imagename='q_im_temp',fitsimage=objectname+'_POSSUM_q.fits',overwrite=True)
		exportfits(imagename='u_im_temp',fitsimage=objectname+'_POSSUM_u.fits',overwrite=True)
	#EMU
	if EMUSB != 'NaN':

		if EMUSB=='10083':
			i_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.i.SB10083.contcube.conv.fits'
			q_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.q.SB10083.contcube.conv.fits'
			u_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.u.SB10083.contcube.conv.fits'
			cont_EMU_filename= '/Volumes/NARNIA/fullfields/image.i.SB10083.cont.taylor.0.restored.conv.fits'

			imsubimage(imagename=i_EMU_filename,outfile='i_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=q_EMU_filename,outfile='q_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=u_EMU_filename,outfile='u_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=cont_EMU_filename,outfile='EMU_cont_im_temp',region=region,overwrite=True,dropdeg=True)
			exportfits(imagename='i_EMU_im_temp',fitsimage=objectname+'_EMU_i.fits',overwrite=True)
			exportfits(imagename='q_EMU_im_temp',fitsimage=objectname+'_EMU_q.fits',overwrite=True)
			exportfits(imagename='u_EMU_im_temp',fitsimage=objectname+'_EMU_u.fits',overwrite=True)
			exportfits(imagename='EMU_cont_im_temp',fitsimage=emu_outfile,overwrite=True)
		elif EMUSB=='10635':
			i_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.i.SB10635.contcube.v2.conv.fits'
			q_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.q.SB10635.contcube.v2.conv.fits'
			u_EMU_filename = '/Volumes/NARNIA/fullfields/image.restored.u.SB10635.contcube.v2.conv.fits'
			cont_EMU_filename= '/Volumes/NARNIA/fullfields/image.i.SB10635.cont.taylor.0.restored.fits'
			imsubimage(imagename=i_EMU_filename,outfile='i_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=q_EMU_filename,outfile='q_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=u_EMU_filename,outfile='u_EMU_im_temp',region=region,overwrite=True,dropdeg=True)
			imsubimage(imagename=cont_EMU_filename,outfile='EMU_cont_im_temp',region=region,overwrite=True,dropdeg=True)
			exportfits(imagename='i_EMU_im_temp',fitsimage=objectname+'_EMU_i.fits',overwrite=True)
			exportfits(imagename='q_EMU_im_temp',fitsimage=objectname+'_EMU_q.fits',overwrite=True)
			exportfits(imagename='u_EMU_im_temp',fitsimage=objectname+'_EMU_u.fits',overwrite=True)
			exportfits(imagename='EMU_cont_im_temp',fitsimage=emu_outfile,overwrite=True)
		else:
			#no cubes
			emu_filename= directoryA +'fullfields/image.i.SB'+EMUSB+'.cont.taylor.0.restored.fits'
			imsubimage(imagename=emu_filename,outfile='emu_cont_temp',region=region,overwrite=True,dropdeg=True)
			exportfits(imagename='emu_cont_temp',fitsimage=emu_outfile,overwrite=True)
			os.system("rm -r emu_cont_temp")

	#tidy up
	os.system("rm -r *_temp")
	os.system("mv *{}* {}/".format(objectname,objectname))

max_stars_repo_name: EmmaAlexander/possum-tools

max_issues_repo_name: EmmaAlexander/possum-tools

max_forks_repo_name: EmmaAlexander/possum-tools


text:
import numpy as np

class GroundTruthDetections:

    def __init__(self, fname= 'TownCentre-groundtruth.top'):
        self.all_dets = np.loadtxt(fname ,delimiter=',') #load detections
        self._frames = int(self.all_dets[:, 1].max())+1 #0 to 4500 inclusive

    '''as in practical realtime MOT, the detector doesn't run on every single frame'''
    def _do_detection(self, detect_prob = .4):

        return int(np.random.choice(2, 1, p=[1 - detect_prob, detect_prob]))

    '''returns the detected items positions or [] if no detection'''
    def get_detected_items(self,frame):

        if self._do_detection() or frame == 0:
            return self.all_dets[self.all_dets[:, 1] == frame, 8:]

        else:
            return []

    def get_total_frames(self):
        return self._frames


text:
"""
========================
The cosegregation module
========================

Measuring proximity in GAM datasets
===================================

The fundamental principle behind GAM is that two regions which are in close
proximity in the nucleus should be frequently found in the same thin
nuclear slice - i.e. that more proximal genomic regions co-segregate more
frequently than less proximal genomic regions. The cosegregation module
provides functions that calculate the co-segregation of genomic regions,
thereby allowing the relative nuclear proximity of different regions
to be inferred.

Co-segregation, the number of times that location x and location y are
found in the same :ref:`nuclear profile <NPs>`, is the simplest measure
of proximity but there are alternative approaches. Other ways of generating a
:ref:`proximity matrix <proximity_matrices>` generally attempt to normalize for
the differential detection of the two locations. For example, if locations x
and y are detected in 100 :ref:`NPs` and location z is detected in only 20, the
co-segregation of x and y will likely be higher than that between x and z even
if their respective nuclear proximities are the same. In general, the approach
that best accounts for such detection effects is the
:func:`normalized linkage disequilibrium <get_dprime>` (or Dprime).

"""
from __future__ import print_function
import itertools
import time
import warnings
import sys

import numpy as np

from .cosegregation_internal import cosegregation_2d, cosegregation_3d, \
        linkage_2d, linkage_3d, dprime_2d
from . import segregation, matrix
from .utils import format_genomic_distance

class InvalidDataError(Exception):
    """
    Exception raised if segregation data contains anything other than 0s and 1s.
    """
    pass

def regions_are_valid(regions):
    """
    Test whether any regions contain values other than the integers 0 and 1

    :param list regions: List of :ref:`regions <regions>` to check.
    :returns: Returns False if any regions contain invalid data, otherwise True.
    """

    allowed_values = set([0, 1])
    region_unique_values = [
        set(np.unique(np.array(region).ravel())) for region in regions]
    invalid_regions = [not region_values.issubset(
        allowed_values) for region_values in region_unique_values]

    if anyinvalid_regions):
        return False

    return True

def prepare_regions(regions):
    """Checks and formats a list of regions

    Takes a list of :ref:`regions <regions>`, checks if they are valid
    (see :func:`regions_are_valid`) and converts them to integer arrays.
    If there is only one region in the list, cosegregation should be calculated
    for that region against itself, so returns a list containing the same
    region twice

    :param list regions: List of :ref:`regions <regions>` to check.
    :returns: list of integer :class:`numpy arrays <numpy.ndarray>`.
    """

    if not regions_are_valid(regions):
        raise InvalidDataError('Region contains integers greater than 1')

    if len(regions) == 1:
        regions = regions * 2

    regions = [np.array(r.astype(int)) for r in regions]

    return regions

def cosegregation_frequency_ndim(loci):
    """Take a table of n columns and return the n-dimensional co-segregation frequencies.

    This function accepts an integer :class:`numpy array <numpy.ndarray>` of size n by x, where
    n is the number of genomic windows (loci) and x is the number of samples. It calculates
    the number of times from 0 to n of the windows are all found in the same tube.

    This is a pure python function, so it is relatively slow, but it can calculate
    the co-segregation of any number of windows.

    :param loci: Array giving the segregation of n loci across x samples.
    :type loci: :class:`~numpy.ndarray`
    :returns: An n-dimensional contingency table giving the cosegregation frequencies of all \
    possible combinations of the n loci.
    """

    counts_shape = (2,) * loci.shape[0]

    counts = np.zeros(counts_shape)

    for sample in loci.T:

        counts[tuple(sample)] += 1

    return counts

def get_index_combinations(regions):
    """Returns all possible index combinations for a list of regions.

    Takes a list of :ref:`regions <regions>` and returns a generator
    that will iterate through indices for all combinations of columns
    in the three regions. Column indices for each region are consecutive,
    for example, if the first region has 20 columns, the last column of
    region 1 will have the index 19 (since indices are 0-based) and the
    first column of region 2 will have the index 20.

    :param list regions: List of :ref:`regions <regions>`.
    :returns: generator that yields tuples of integer indices.
    """

    indexes = []
    start = 0

    assert len(regions) > 1

    for region in regions:

        indexes.append(list(range(start, start + len(region))))
        start = max(indexes[-1]) + 1

    return itertools.product(*indexes)

def cosegregation_nd(*regions):
    """Get the full co-segregation table for n regions.

    Takes a list of :ref:`regions <regions>` and calculates the
    full cosegregation frequency matrix for all possible combinations
    of loci from the n regions. For example, if three regions are
    given, position (x,y,z) of the output matrix is a three-dimensional
    contingency table, giving the number of times locus x cosegregates
    with loci y and z.

    This function has a pure python inner and outer loop, so it is
    very slow, but it has the advantage of being applicable in an
    unlimited number of dimensions. Optimized functions for
    obtaining the co-segregation of two or three regions can
    be found in the cosegregation_optimized module.

    :param list regions: List of :ref:`regions <regions>`.
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the co-segregation \
            of all possible combinations of windows within the different regions.
    """

    combinations = get_index_combinations(regions)

    full_data = np.concatenate(regions, axis=0).astype(int)

    def get_frequency(indices):
        """
        Internal function to get the cosegregation_frequency for
        a given combination of columns from full_data.
        """

        return cosegregation_frequency_ndim(full_data[indices, :])

    result = list(map(get_frequency, combinations))

    result_shape = tuple([len(region)
                          for region in regions]) + (2, ) * len(regions)

    freqs = np.array(result).reshape(result_shape)

    return freqs

def get_cosegregation_from_regions(*regions):
    """Get the full co-segregation table for n regions, using optimized
    functions where available.

    This is a wrapper which determines the correct co-segregation
    function to call based on the number of regions. Where there
    are two or three regions, optimized C functions are used. Where
    there are more than three regions, the non-optimized python
    algorithm is used.

    :param list regions: List of :ref:`regions <regions>`.
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the co-segregation \
            of all possible combinations of windows within the different regions.
    """

    regions = prepare_regions(regions)

    if len(regions) == 2:
        coseg_func = cosegregation_2d
    elif len(regions) == 3:
        coseg_func = cosegregation_3d
    else:
        coseg_func = cosegregation_nd

    return coseg_func(*regions)

def get_cosesgregation(segregation_data, *location_strings):
    """Calculate co-segregation frequencies for a given genomic
    location or locations. Where only one location is given,
    co-segregation is calculated for that region against itself.

    :param segregation_data: Input :ref:`segregation table <segregation_table>`
    :param str location_strings: One or more :ref:`location strings <location_string>`
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the co-segregation \
            of all possible combinations of windows within the different regions.
    """

    regions = [segregation.region_from_location_string(
        segregation_data, l) for l in location_strings]

    return get_cosegregation_from_regions(*regions)

def get_linkage_from_regions(*regions):
    """Get the full linkage disequilibrium matrix for n regions, using optimized
    functions where available.

    This is a wrapper which determines the correct linkage
    function to call based on the number of regions. Where there
    are two regions, an optimized C function is used. Where
    there are three regions, linkage is calculated following the
    method given in
    `Hastings (1984) <http://www.ncbi.nlm.nih.gov/pmc/articles/PMC1202243/>`_.
    This usage will raise a warning, since three-dimensional linkage is non-
    trivial and not necessarily obvious. Linkage matrices for more than three
    regions are not currently supported.

    :param list regions: List of :ref:`regions <regions>`.
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the linkage disequilibrium \
            of all possible combinations of windows within the different regions.
    """

    regions = prepare_regions(regions)

    if len(regions) == 2:
        linkage_func = linkage_2d
    elif len(regions) == 3:
        warnings.warn(
            '3D linkage is calculated following Hastings - Genetics (1984) 106:153-164. '
            'Please check and make sure this is what you want.')
        linkage_func = linkage_3d
    else:
        raise NotImplementedError(
            'No linkage function is defined for {} regions'.format(
                len(regions)))

    return linkage_func(*regions)

def get_linkage(segregation_data, *location_strings):
    """Calculate the linkage disequilibrium matrix for a given genomic
    location or locations. Where only one location is given,
    linkage is calculated for that region against itself.

    :param segregation_data: Input :ref:`segregation table <segregation_table>`
    :param str location_strings: One or more :ref:`location strings <location_string>`
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the linkage disequilibrium \
            of all possible combinations of windows within the different regions.
    """

    regions = [segregation.region_from_location_string(
        segregation_data, l) for l in location_strings]

    return get_linkage_from_regions(*regions)

def get_dprime_from_regions(*regions):
    """Get the full normalized linkage disequilibrium (D') matrix for n
    regions.

    This is a wrapper which determines the correct normalized linkage
    function to call based on the number of regions. Only two-dimensional
    normalized linkage matrices are currently supported. Where only one
    region is given, normalized linkage is calculated for that region against
    itself.

    :param list regions: List of :ref:`regions <regions>`.
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the normalized linkage \
            disequilibrium of all possible combinations of windows within the different regions.
    """

    regions = prepare_regions(regions)

    if len(regions) == 2:
        dprime_func = dprime_2d
    else:
        raise NotImplementedError(
            'There is currently no implementation of normalized linkage '
            'disequilibrium for more than 2 dimensions')

    return dprime_func(*regions)

def get_dprime(segregation_data, *location_strings):
    """Calculate the normalized linkage disequilibrium (D') matrix for a given
    genomic location or locations. Where only one location is given, normalized
    linkage is calculated for that region against itself.  Where two regions
    are given, linkage is calculated for region one against region two.

    :param segregation_data: Input :ref:`segregation table <segregation_table>`
    :param str location_strings: One or more :ref:`location strings <location_string>`.
    :returns: :ref:`proximity matrix <proximity_matrices>` giving the normalized linkage \
            disequilibrium of all possible combinations of windows within the different regions.
    """

    regions = [segregation.region_from_location_string(
        segregation_data, l) for l in location_strings]

    return get_dprime_from_regions(*regions)

MATRIX_TYPES = {
    'dprime': get_dprime_from_regions,
    'linkage': get_linkage_from_regions,
    'cosegregation': get_cosegregation_from_regions,
}

def get_regions_and_windows(segregation_data, location_strings):
    """Get the windows which fall into a given genomic location, and the
    segregation of those windows across samples.

    For each location string, find the rows of the :ref:`segregation table <segregation_table>`
    that overlap the given genomic location. Return the resulting
    :ref:`segregation table <segregation_table>` subsets (i.e. :ref:`regions`),
    and a list of the locations of the windows within each region.

    :param segregation_data: Input :ref:`segregation table <segregation_table>`
    :param list location_strings: One or more :ref:`location strings <location_string>`
    :returns: A list of :ref:`regions` and a list of tuples giving \
            window locations in the form (chromosome, start, stop).
    """

    if len(location_strings) == 1:
        location_strings = location_strings * 2

    regions = [
        segregation.region_from_location_string(
            segregation_data,
            location_str) for location_str in location_strings]

    windows = [np.array(list(region.index)) for region in regions]

    return regions, windows

def matrix_from_segregation_file(
        segregation_file, location_strings, matrix_type='dprime'):
    """Get the proximity matrix between the given genomic locations, and the
    locations of the genomic windows corresponding to each axis of the
    proximity matrix.

    Calculate the :ref:`proximity matrix <proximity_matrices>` between the
    windows in different genomic locations. If only one location string is
    given, calculate the matrix for that location against itself. For example,
    if location_strings is ['chr1'], the matrix will give the proximity between
    all windows on chromosome 1 (x-axis) against all windows on chromosome 1
    (y-axis).  Alternatively, if location_strings is ['chr1', 'chr2'], the
    matrix will give the proximity between windows on chromosome 1 (x-axis) and
    windows on chromosome 2 (y-axis).

    Three types of :ref:`proximity matrix <proximity_matrices>` are supported, 'cosegregation' (see
    :func:`get_cosegregation_from_regions`), 'linkage' (see
    :func:`get_linkage_from_regions`) and 'dprime' (see
    :func:`get_dprime_from_regions`).

    The function also returns a list of the windows that correspond to
    the axes of the :ref:`proximity matrix <proximity_matrices>`.

    :param segregation_file: Path to input :ref:`segregation table <segregation_table>`
    :param list location_strings: One or more :ref:`location strings <location_string>`
    :returns: A :ref:`proximity matrix <proximity_matrices>` for the given \
            genomic locations, and a list of tuples giving window locations \
            in the form (chromosome, start, stop).

    """

    segregation_data = segregation.open_segregation(segregation_file)

    regions, windows = get_regions_and_windows(
        segregation_data, location_strings)
    matrix_func = MATRIX_TYPES[matrix_type]
    contact_matrix = matrix_func(*regions)

    return contact_matrix, windows

def create_and_save_contact_matrix(segregation_file, location_strings,
                                   output_file, output_format,
                                   matrix_type='dprime'):
    """Calculate the proximity matrix for the given genomic locations and save it
    to disk.

    Calculate the :ref:`proximity matrix <proximity_matrices>` between the
    windows in different genomic locations. If only one location string is
    given, calculate the matrix for that location against itself. For example,
    if location_strings is ['chr1'], the matrix will give the proximity between
    all windows on chromosome 1 (x-axis) against all windows on chromosome 1
    (y-axis).  Alternatively, if location_strings is ['chr1', 'chr2'], the
    matrix will give the proximity between windows on chromosome 1 (x-axis) and
    windows on chromosome 2 (y-axis).

    Three types of :ref:`proximity matrix <proximity_matrices>` are supported, 'cosegregation' (see
    :func:`get_cosegregation_from_regions`), 'linkage' (see
    :func:`get_linkage_from_regions`) and 'dprime' (see
    :func:`get_dprime_from_regions`).

    Three output formats are supported: 'npz', 'txt' and 'csv'. 'txt' and
    'csv' outputs also support gzip compression ('txt.gz' and 'csv.gz'
    formats). See :ref:`matrix_formats` for more details.

    :param str segregation_file: Path to input :ref:`segregation table <segregation_table>`.
    :param list location_strings: One or more :ref:`location strings <location_string>`.
    :param str output_file: Path to use for saving output file.
    :param str output_format: Format to use when saving matrix. \
            (see :ref:`matrix_formats` for more details)
    :param str matrix_type: Type of :ref:`proximity matrix <proximity_matrices>`\
            to calculate.
    """

    print('starting calculation for {}'.format(' x '.join(location_strings)))
    start_time = time.clock()

    contact_matrix, windows = matrix_from_segregation_file(
        segregation_file, location_strings, matrix_type)

    size_string = ' x '.join([str(s) for s in contact_matrix.shape])
    print('region size is: {}'.format(size_string), end=' ')
    print('Calculation took {0}s'.format(time.clock() - start_time))
    print('Saving matrix to file {}'.format(output_file))

    output_func = matrix.OUTPUT_FORMATS[output_format]

    output_func(windows, contact_matrix, output_file)

    print('Done!')

def get_output_file(
        segregation_file,
        location_strings,
        matrix_type,
        output_format):
    """Automatically generate an output file path for a proximity matrix given
    the input segregation file.

    This function generates an output path in the format:

    <input_segregation_path>.<region1>_x_<region2>_<matrix_type>.<extension>

    :param str segregation_file: Path to input :ref:`segregation table <segregation_table>`.
    :param list location_strings: One or more :ref:`location strings <location_string>`.
    :param str matrix_type: Type of :ref:`proximity matrix <proximity_matrices>`\
            to calculate.
    :param str output_format: Format to use when saving matrix. \
            (see :ref:`matrix_formats` for more details)
    :returns: Path to save matrix file.

    >>> get_output_file('/path/to/segregation_file.table', ['chr1'], 'dprime', 'txt.gz')
    '/path/to/segregation_file.chr1_dprime.txt.gz'
    >>> get_output_file('/path/to/segregation_file.table', ['chr1', 'chr2'], 'linkage', 'npz')
    '/path/to/segregation_file.chr1_x_chr2_linkage.npz'
    >>> get_output_file('/path/to/segregation_file.table', ['chr1:10000000-20000000'],
    ...                 'cosegregation', 'csv')
    '/path/to/segregation_file.chr1_10.0Mb-20.0Mb_cosegregation.csv'

    """

    segregation_base = '.'.join(segregation_file.split('.')[:-1])
    locations = []
    for loc_string in location_strings:

        # If there is no colon, we are asked for a whole chromosome
        if not ':' in loc_string:
            locations.append(loc_string)
            continue

        chrom, start, stop = segregation.parse_location_string(loc_string)
        start, stop = format_genomic_distance(
            start), format_genomic_distance(stop)
        formatted_location = '{chrom}_{start}-{stop}'.format(chrom=chrom,
                                                             start=start,
                                                             stop=stop)
        locations.append(formatted_location)
    region_string = '_x_'.join(locations)

    return '{}.{}_{}.{}'.format(
        segregation_base,
        region_string,
        matrix_type,
        output_format)

def matrix_from_args(args):
    """Extract parameters from an argparse namespace object and pass them to
    create_and_save_contact_matrix.
    """

    if args.output_format is None:
        if len(args.regions) > 2:
            args.output_format = 'npz'
        elif args.output_file is None:
            args.output_format = 'txt.gz'
        elif args.output_file == '-':
            args.output_format = 'txt'
        else:
            args.output_format = matrix.detect_file_type(args.output_file)

    if args.output_file is None:
        args.output_file = get_output_file(
            args.segregation_file, args.regions,
            args.matrix_type, args.output_format)

    elif args.output_file == '-':
        args.output_file = sys.stdout

    create_and_save_contact_matrix(args.segregation_file, args.regions,
                                   args.output_file, args.output_format,
                                   args.matrix_type)

def matrix_from_doit(output_file, segregation_file, region):
    """Partial function that passes parameters from doit to
    create_and_save_contact_matrix
    """

    create_and_save_contact_matrix(segregation_file, region,
                                   output_file, 'txt.gz', 'dprime')

max_stars_repo_name: eeroshok/gamtools-dev

max_issues_repo_name: eeroshok/gamtools-dev

max_forks_repo_name: eeroshok/gamtools-dev


text:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from readSto import readStoFile
from interpDFrame import interpDFrame

def sumCMC(dfCMC):
    col_list= list(dfCMC)
    col_list.remove('time')
    colOut = df[col_list].sum(axis=1)
    return colOut

# run, free-speed
id_results_dir = '../ID/results_run';
cmc_results_dir = '../CMC/run/results';

# Right Foot Strike (RFS) times
RFS1 = 0.863
RFS2 = 1.546

# ---------------------------------------------------------
# Load simulation outputs from file,
# normalize time to % gait cycle,
# and calculate net muscle-generated joint moments

#----------------------------------------------------------
# ID
id_filename = id_results_dir +'/inverse_dynamics.sto'

df = readStoFile(id_filename)
#df.columns

# put columns of interest into a new dataframe
time = df['time']
# min time is the same for id and cmc
min_time = np.amin(time)
hipflex = df['hip_flexion_r_moment']
hipflex.name = 'hipflex'
kneeext = -df['knee_angle_r_moment']
kneeext.name = 'kneeext'
ankleext = -df['ankle_angle_r_moment']
ankleext.name = 'ankleext'

# make dataframe from the id joint moments
idJointMoments = pd.concat([time, hipflex, kneeext, ankleext], axis=1)
idJointMoments_interp = interpDFrame(idJointMoments, RFS1, RFS2, min_time)

#plt.plot(idJointMoments_interp['percentgaitcycle'], idJointMoments_interp['hipflex'], '-')
#plt.show()

#----------------------------------------------------------
# Hip flexion moment (CMC, Muscle Analysis)
fname = cmc_results_dir + '/cmc_MuscleAnalysis_Moment_hip_flexion_r.sto'
df = readStoFile(fname)
time = df['time']
hipflex = sumCMC(df)
hipflex.name = 'hipflex'

# Knee *extension* moment (CMC, Muscle Analysis)
fname = cmc_results_dir + '/cmc_MuscleAnalysis_Moment_knee_angle_r.sto'
df = readStoFile(fname)
kneeext = -sumCMC(df)
kneeext.name = 'kneeext'

# Ankle *extension* moment (CMC, Muscle Analysis)
fname = cmc_results_dir + '/cmc_MuscleAnalysis_Moment_ankle_angle_r.sto'
df = readStoFile(fname)
ankleext = -sumCMC(df)
ankleext.name = 'ankleext'

cmcJointMoments = pd.concat([time, hipflex, kneeext, ankleext], axis=1)
cmcJointMoments_interp = interpDFrame(cmcJointMoments, RFS1, RFS2, min_time)

#
#plt.figure
#plt.plot(idJointMoments_interp['percentgaitcycle'], idJointMoments_interp['hipflex'], '*b')
#plt.plot(cmcJointMoments_interp['percentgaitcycle'], idJointMoments_interp['hipflex'], '+r')
#plt.show()

# magic to open plots in new window
#  %matplotlib

fig = plt.figure()
ax1 = plt.subplot(3,1,1)
ax2 = plt.subplot(3,1,2)
ax3 = plt.subplot(3,1,3)

# ------------------------------------------------------
# Plot ID joint moments
ax1.plot(idJointMoments_interp['percentgaitcycle'], idJointMoments_interp['hipflex'], 'b')
ax2.plot(idJointMoments_interp['percentgaitcycle'], idJointMoments_interp['kneeext'], 'b')
ax3.plot(idJointMoments_interp['percentgaitcycle'], idJointMoments_interp['ankleext'], 'b')

# ------------------------------------------------------
# Plot CMC muscle-generated joint moments
ax1.plot(cmcJointMoments_interp['percentgaitcycle'], cmcJointMoments_interp['hipflex'], 'r')
ax2.plot(cmcJointMoments_interp['percentgaitcycle'], cmcJointMoments_interp['kneeext'], 'r')
ax3.plot(cmcJointMoments_interp['percentgaitcycle'], cmcJointMoments_interp['ankleext'], 'r')

# ------------------------------------------------------
# Label plots
ax1.set(ylabel='hip flexion moment')
ax1.set(xlim=[0,100])
ax1.set(ylim=[-100,100])

ax2.set(ylabel='knee extension moment')
ax2.set(xlim=[0,100])
ax2.set(ylim=[-100,100])

ax3.set(ylabel='ankle plantarflexion moment')
ax3.set(xlim=[0,100])
ax3.set(ylim=[-50,150])

plt.xlabel('percent gait cycle')

# ------------------------------------------------------
# Compute RMS and peak errors between CMC and muscle-generated joint moments
matID = idJointMoments_interp.values[:,1:4]
maxID = np.amax(matID, axis=0)

matCMC = cmcJointMoments_interp.values[:,1:4]
maxCMC = np.amax(matCMC, axis = 0)

matDiff = np.subtract(matID, matCMC)
rms_id_cmc = np.sqrt(np.mean(matDiff**2, axis = 0))

peak_id_cmc = np.amax(np.abs(matDiff), axis = 0)

rms_id_cmc_norm = rms_id_cmc/maxID
peak_id_cmc_norm = peak_id_cmc/maxID

# ------------------------------------------------------
# Print to console
print 'Hip Flexion Moment'
print 'rmsdiff=',rms_id_cmc[0]
print 'rmsdiff norm to peak ID=', rms_id_cmc_norm[0]
print 'peak diff=', peak_id_cmc[0]
print 'peak diff norm to peak ID=', peak_id_cmc_norm[0]
print ' '
print 'Knee Extension Moment'
print 'rmsdiff=',rms_id_cmc[1]
print 'rmsdiff norm to peak ID=', rms_id_cmc_norm[1]
print 'peak diff=', peak_id_cmc[1]
print 'peak diff norm to peak ID=', peak_id_cmc_norm[1]
print ' '
print 'Ankle Extension Moment'
print 'rmsdiff=',rms_id_cmc[2]
print 'rmsdiff norm to peak ID=', rms_id_cmc_norm[2]
print 'peak diff=', peak_id_cmc[2]
print 'peak diff norm to peak ID=', peak_id_cmc_norm[2]

plt.show()

max_stars_repo_name: rebrik/OpenSimFullBodyWithPython

max_issues_repo_name: rebrik/OpenSimFullBodyWithPython

max_forks_repo_name: rebrik/OpenSimFullBodyWithPython


text:
import sys
import numpy as np

from Hist import Histogram
# -------------------------------------------------------------
# A class to read the input file of the program and store the
# relevant data.
#
# All the routines used to read input entries return an errorMessage.
# If there is no error while executing the routine then the
# message will simply be a 'No error' string
#
# -------------------------------------------------------------
class input_data:

	# boolean flags that are changed to true if the corresponding entry was found in the input
	ee_histo_parsed     = False
	use_wl_parsed       = False
	use_tmmc_parsed     = False
	iseed_parsed        = False
	read_data_parsed    = False
	sim_steps_parsed    = False
	tp_IonPair_parsed   = False
	tp_Ion_parsed       = False
	set_temp_parsed     = False
	init_dos_parsed     = False
	write_wl_parsed     = False
	write_tmmc_parsed   = False
	write_dump_parsed   = False
	roam_ee_with_parsed = False

	# info that can be read from the input file
	# (in comment the corresponding keyword of the input)
	boundary_min        = 0.0    # ee_histo
	boundary_max        = 0.0    # ee_histo
	desired_inc         = 0.0    # ee_histo
	lnf                 = 0.0    # use_wl
	lnf_scaler          = 0.0    # use_wl
	ratio_crit          = 0.0    # use_wl
	lnf_crit            = 0.0    # use_wl
	use_wl_bool         = False  # use_wl
	use_tmmc_bool       = False  # use_tmmc
	NStepsUpdateTM      = 0      # use_tmmc
	iseed               = 0      # iseed
	DataFile            = ''     # read_data
	NSteps_equil        = 0      # sim_steps
	NSteps_prod         = 0      # sim_steps
	NSteps_subEns       = 0      # sim_steps
	cationName          = ''     # tp_IonPair
	anionName           = ''     # tp_IonPair
	iTypeTestCat        = 0      # tp_IonPair
	iTypeTestAn         = 0      # tp_IonPair
	IonName             = ''     # tp_Ion
	iTypeTestIon        = 0      # tp_Ion
	tp_IonCharge        = 0.0    # tp_Ion
	Temp                = 298.15 # set_temp
	outFile_wl          = ''     # write_Wl
	wstep_wl            = 0      # write_Wl
	outFile_tmmc        = ''     # write_tmmc
	wstep_tmmc          = 0      # write_tmmc
	outFile_dump        = ''     # write_dump
	wstep_dump          = 0      # write_dump
	ee_method           = ''     # roam_ee_with

	NoErrorMessage      = 'No error' # no error flag

	# ----------------------------------------------------------------
	# Initialize function
	# ----------------------------------------------------------------
	def __init__(self,InputFile):

		NLinesRead = 0

		with open(InputFile) as input:
			for line in input:
				NLinesRead += 1
				if (line == "\n"):
					continue # empty line

				# ignore anything that follows after a '#' as a comment
				line        = line.partition('#')[0]
				if (len(line) == 0):
					continue

				line        = line.rstrip()
				lineArgs    = line.split()

				if  (lineArgs[0] == 'ee_histo' ):
					errorMessage           = self.ee_histo(lineArgs)
					self.ee_histo_parsed   = True
				elif(lineArgs[0] == 'use_wl'   ):
					errorMessage           = self.use_wl(lineArgs)
					self.use_wl_parsed     = True
				elif(lineArgs[0] == 'use_tmmc' ):
					errorMessage           = self.use_tmmc(lineArgs)
					self.use_tmmc_parsed   = True
				elif(lineArgs[0] == 'iseed'    ):
					errorMessage           = self.read_iseed(lineArgs)
					self.iseed_parsed      = True
				elif(lineArgs[0] == 'read_data'):
					errorMessage           = self.read_data(lineArgs)
					self.read_data_parsed  = True
				elif(lineArgs[0] == 'sim_steps' ):
					errorMessage           = self.sim_steps(lineArgs)
					self.sim_steps_parsed  = True
				elif(lineArgs[0] == 'tp_IonPair'):
					errorMessage           = self.tp_IonPair(lineArgs)
					self.tp_IonPair_parsed = True
				elif(lineArgs[0] == 'tp_Ion'):
					errorMessage           = self.tp_Ion(lineArgs)
					self.tp_Ion_parsed     = True
				elif(lineArgs[0] == 'set_temp'):
					errorMessage           = self.set_temp(lineArgs)
					self.set_temp_parsed   = True
				elif(lineArgs[0] == 'init_dos'):
					errorMessage           = self.init_dos(lineArgs)
					self.init_dos_parsed   = True
				elif(lineArgs[0] == 'write_wl'    ):
					errorMessage           = self.write_wl(lineArgs)
					self.write_wl_parsed   = True
				elif(lineArgs[0] == 'write_tmmc'  ):
					errorMessage           = self.write_tmmc(lineArgs)
					self.write_tmmc_parsed = True
				elif(lineArgs[0] == 'write_dump'  ):
					errorMessage           = self.write_dump(lineArgs)
					self.write_dump_parsed = True
				elif(lineArgs[0] == 'roam_ee_with'):
					errorMessage             = self.roam_ee_with(lineArgs)
					self.roam_ee_with_parsed = True
				else:
					print('input_data.__init__ : ERROR - Unrecognised input file entry at line %d\n' %NLinesRead)
					print('                      %s is not recognised\n' %lineArgs[0]    )
					sys.exit()

				if (errorMessage != self.NoErrorMessage):
					print('input_data.__init__ : ERROR - Erroneous entry at line %d\n' %NLinesRead)
					print(' > Printing ERROR info ...\n')
					print(' > %s\n' %errorMessage)

					sys.exit()

  # ----------------------------------
	def ee_histo(self,lineArgs):
		if (len(lineArgs) != 4):
			return 'ee_histo: Argument mismatch. Specify: min,max,desired_inc' # error
		self.boundary_min = float(lineArgs[1])
		self.boundary_max = float(lineArgs[2])
		self.desired_inc  = float(lineArgs[3])

		return self.NoErrorMessage

	# ----------------------------------
	def use_wl(self,lineArgs):
		if   (len(lineArgs) < 2):
			return 'use_wl: Called without arguments. Please choose yes/no' # error
		elif (not self.ee_histo_parsed):
			return 'use_wl: Cant find an EE histogram. Define one using ee_histo' # error

		if  (lineArgs[1] == 'yes'):
			if (len(lineArgs) != 6):
				return 'use_wl: Argument mismatch. Specify(after yes): lnf, lnf_scaler, ratio_crit, lnf_crit' # error

			self.use_wl_bool = True
			self.lnf         = float(lineArgs[2])
			self.lnf_scaler  = float(lineArgs[3])
			self.ratio_crit  = float(lineArgs[4])
			self.lnf_crit    = float(lineArgs[5])
		elif(lineArgs[1] == 'no' ):
			self.use_wl_bool = False

		return self.NoErrorMessage

	# ----------------------------------
	def use_tmmc(self,lineArgs):
		if   (len(lineArgs) < 2):
			return 'use_tmmc: Called without arguments. Please choose yes/no' # error
		elif (not self.ee_histo_parsed):
			return 'use_tmmc: Cant find an EE histogram. Define one using ee_histo' # error

		self.use_tmmc = False
		if (lineArgs[1] == 'yes'):
			if   (len(lineArgs) != 3):
				return 'use_tmmc: Argument mismatch. Please choose yes/no' # error
			self.use_tmmc_bool  = True
			self.NStepsUpdateTM = int(lineArgs[2])

		return self.NoErrorMessage

	# ----------------------------------
	def read_iseed(self,lineArgs):
		if (len(lineArgs) != 2):
			return 'iseed: Argument mismatch. Only specify the desired seed' # error
		self.iseed      = int(lineArgs[1])
		return self.NoErrorMessage

	def read_data(self,lineArgs):
		if (len(lineArgs) != 2):
			return 'read_data: Argument mismatch. Only specify the data file name'

		self.DataFile = str(lineArgs[1])
		return self.NoErrorMessage

	# ----------------------------------
	def sim_steps(self,lineArgs):
		if  (len(lineArgs) != 4):
			return 'sim_steps: Argument mismatch. Specify: NSteps_equil, NSteps_prod, NSteps_subEns' # error
		elif(float(lineArgs[1]) < 0 or float(lineArgs[2]) < 0 or float(lineArgs[3]) < 0):
			return 'sim steps: Simulation steps cannot be negative' # error
		self.NSteps_equil   = int(lineArgs[1])
		self.NSteps_prod    = int(lineArgs[2])
		self.NSteps_subEns  = int(lineArgs[3])
		return self.NoErrorMessage

	# ----------------------------------
	def tp_IonPair(self,lineArgs):
		if  (len(lineArgs) != 5):
			return 'tp_IonPair: Argument mismatch. Specify: cationName, iTypeCat, anionName, iTypeAn' # error
		self.iTypeTestCat = int(lineArgs[1])
		self.cationName   = str(lineArgs[2])
		self.iTypeTestAn  = int(lineArgs[3])
		self.anionName    = str(lineArgs[4])
		return self.NoErrorMessage

	# ----------------------------------
	def tp_Ion(self,lineArgs):
		if  (len(lineArgs) != 4):
			return 'tp_IonPair: Argument mismatch. Specify: iType, Name, charge(full)' # error

		self.iTypeTestIon = int(lineArgs[1])
		self.IonName      = str(lineArgs[2])
		self.tp_IonCharge = float(lineArgs[3])
		return self.NoErrorMessage

	# ----------------------------------
	def set_temp(self,lineArgs):
		if  (len(lineArgs) != 2):
			return 'set_temp: Argument mismatch. Only specify the simulation temperature' # error
		elif(float(lineArgs[1]) < 0):
			return 'set_temp: Negative temperature'  # error
		self.Temp = float(lineArgs[1])
		return self.NoErrorMessage

	# ----------------------------------
	def init_dos(self,lineArgs):
		if   (len(lineArgs) != 2):
			return 'init_dos: Argument mismatch. Specify just the file path and file name of the initial DOS' # error
		elif (not self.ee_histo_parsed):
			return 'init_dos: Cant find an EE histogram. Define one using ee_histo' # error

		EEHisto_aux    = Histogram(self.boundary_min, self.boundary_max, self.desired_inc)
		NSubs          = EEHisto_aux.NBins
		self.dos_init  = np.zeros(NSubs, np.double)
		dos_init_check = np.zeros(NSubs, dtype=bool)
		iEntry         = -1

		with open(str(lineArgs[1])) as dosFile:
			for line in dosFile:
				if (line == "\n"):
					continue # empty line

				# ignore anything that follows after a '#' as a comment
				line        = line.partition('#')[0]
				if (len(line) == 0):
					continue

				line        = line.rstrip()
				lineArgsDos = line.split()

				if (iEntry == EEHisto_aux.NBins-1):
					dosFile.close()
					return 'init_dos: The dos_init provided has more sub-ensembles that ee_histo' # error

				# Dos should be located in the third column
				iEntry                += 1
				self.dos_init[iEntry]	 = lineArgsDos[2]
				dos_init_check[iEntry] = True

		if (not all(dos_init_check)):
			return 'init_dos: The dos_init provided has less sub-ensembles that ee_histo' # error

		return self.NoErrorMessage

	# ----------------------------------
	def write_wl(self,lineArgs):
		if   (len(lineArgs) != 3):
			return 'write_wl: Argument mismatch. Specify: write_step(fs), fileName' # error
		elif (not self.use_wl_parsed):
			return 'write_wl: Cant find a WL histogram. Define one using use_wl' # error

		self.wstep_wl   = int(lineArgs[1])
		self.outFile_wl =     lineArgs[2]

		return self.NoErrorMessage

	# ----------------------------------
	def write_tmmc(self,lineArgs):
		if   (len(lineArgs) != 3):
			return 'write_tmmc: Argument mismatch. Specify: write_step(fs), fileName' # error
		elif (not self.use_tmmc_parsed):
			return 'write_tmmc: Cant find a TMMC histogram. Define one using use_tmmc' # error

		self.wstep_tmmc   = int(lineArgs[1])
		self.outFile_tmmc =     lineArgs[2]

		return self.NoErrorMessage

	# ----------------------------------
	def write_dump(self,lineArgs):
		if   (len(lineArgs) != 3):
			return 'write_dump: Argument mismatch. Specify: write_step(fs), fileName' # error

		self.wstep_dump   = int(lineArgs[1])
		self.outFile_dump =     lineArgs[2]

		return self.NoErrorMessage

	# ----------------------------------
	def roam_ee_with(self,lineArgs):
		if   (len(lineArgs) != 2):
			return 'roam_ee_with: Argument mismatch. Specify how to roam the ee' # error

		use_wl_parsed   = self.use_wl_parsed
		use_tmmc_parsed	= self.use_tmmc_parsed

		ee_method       = str(lineArgs[1])

		if (not ee_method == 'wl'  ) and (not ee_method == 'tmmc') and (not ee_method == 'wl_and_tmmc'):
			return 'roam_ee_with: Supported options wl/tmmc/wl_and_tmmc'

		if  (ee_method == 'wl'  ) and (not use_wl_parsed):
			return 'roam_ee_with: Cant find a WL histogram. Define one using use_wl' # error
		elif(ee_method == 'tmmc') and (not use_tmmc_parsed):
			return 'roam_ee_with: Cant find a TMMC histogram. Define one using use_tmmc' # error
		elif(ee_method == 'wl_and_tmmc'):
			if (not use_wl_parsed) or (not use_tmmc_parsed):
				return 'roam_ee_with: Cant find WL or TMMC histogram. Define both using use_wl and use_tmmc' # error

		self.ee_method = ee_method

		return self.NoErrorMessage


text:
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
"""Eval SinGAN"""
import numpy as np
from mindspore import Tensor, context
from mindspore.train.serialization import load_checkpoint, load_param_into_net
import src.functions as functions
from src.model import get_model
from src.imresize import imresize
from src.config import get_arguments
from src.manipulate import SinGAN_generate

def preLauch():
    """parse the console argument"""
    parser = get_arguments()

    # Eval Device.
    parser.add_argument('--mode', type=str, default='test')
    parser.add_argument('--device_target', type=str, default='Ascend')
    parser.add_argument('--device_id', type=int, default=1, help='device id of Ascend (Default: 0)')

    # Directories.
    parser.add_argument('--input_dir', type=str, default='data', help='input image dir')
    parser.add_argument('--input_name', type=str, default='thunder.jpg', help='input image name')
    parser.add_argument('--n_gen', type=int, default=50, help='number of images to generate at last stage')
    parser.add_argument('--out', type=str, default='eval_Output', help='output dir')
    opt = parser.parse_args()
    functions.post_config(opt)

    context.set_context(save_graphs=False, device_id=opt.device_id, \
                            device_target=opt.device_target, mode=context.GRAPH_MODE)
    return opt

def main():
    """main_eval"""
    opt = preLauch()
    Gs = []
    Zs = []
    reals = []
    NoiseAmp = []
    real = functions.read_image(opt)
    functions.adjust_scales2image(real, opt)
    scale_num = 0
    while scale_num < opt.stop_scale + 1:
        opt.out_ = functions.generate_dir2save(opt)
        opt.outf = '%s/%d' % (opt.out_, scale_num)
        G_curr, _ = get_model(scale_num, opt)
        load_param_into_net(G_curr, load_checkpoint('%s/%d/netG.ckpt' % (opt.out_, scale_num)))
        Gs.append(G_curr)

        z_curr = Tensor(np.load('%s/z_curr.npy' % (opt.outf)))
        Zs.append(z_curr)

        noise_amp = Tensor(np.load('%s/noise_amp.npy' % (opt.outf)))
        NoiseAmp.append(noise_amp)

        scale_num += 1
    real_ = functions.read_image(opt)
    real = imresize(real_, opt.scale1, opt)
    reals = functions.creat_reals_pyramid(real, reals, opt)
    SinGAN_generate(Gs, Zs, reals, NoiseAmp, opt)
    print("===========eval success================")

if __name__ == '__main__':
    main()

max_stars_repo_name: mindspore-ai/models

max_issues_repo_name: mindspore-ai/models

max_forks_repo_name: mindspore-ai/models


text:
import logging
from typing import Tuple, Optional

import math

import numpy as np
from matplotlib.gridspec import GridSpec

from model.limits import Limits, LimitsDialog, ValuesDialog, DecibelRangeCalculator
from model.preferences import GRAPH_X_AXIS_SCALE, GRAPH_X_MIN, GRAPH_X_MAX, GRAPH_EXPAND_Y

logger = logging.getLogger('magnitude')

SINGLE_SUBPLOT_SPEC = GridSpec(1, 1).new_subplotspec((0, 0), 1, 1)

class AxesManager:
    def __init__(self, data_provider, axes, fill_curves, fill_alpha, show_in_legend=True):
        self.__provider = data_provider
        self.__axes = axes
        self.__fill_curves = fill_curves
        self.__fill_alpha = fill_alpha
        self.reference_curve = None
        self.__curves = {}
        self.__polygons = {}
        self.__show_in_legend = show_in_legend

    @property
    def show_in_legend(self):
        return self.__show_in_legend

    def artists(self):
        '''
        Gets the artists painting on this axes.
        :return: the artists.
        '''
        return list(self.__curves.values())

    def curve_names(self):
        '''
        The artist names.
        :return: the names.
        '''
        return list(self.__curves.keys())

    def display_curves(self):
        '''
        Displays the cached data on the specified axes, removing existing curves as required.
        :param data: the data.
        :param curves: the stored curves.
        :param axes: the axes on which to display.
        '''
        if self.__provider is not None:
            data = self.__provider(reference=self.reference_curve)
            if len(data) > 0:
                curve_names = [self.__create_or_update_curve(x) for x in data if data is not None]
            else:
                curve_names = []
            self.__delete_old(curve_names, self.__curves)
            self.__delete_old(curve_names, self.__polygons)

    def __delete_old(self, names, artists):
        to_delete = {name: artist for name, artist in artists.items() if name not in names}
        for name, artist in to_delete.items():
            artist.remove()
            del artists[name]

    def __create_or_update_curve(self, data):
        '''
        sets the data on the curve, creating a new artist if necessary.
        :param data: the data to set on the curve.
        :param the updated (or created) curve.
        :return the curve name.
        '''
        curve = self.__curves.get(data.name, None)
        if curve:
            curve.set_data(data.x, data.y)
            curve.set_linestyle(data.linestyle)
            curve.set_color(data.colour)
        else:
            self.__curves[data.name] = self.__axes.semilogx(data.x, data.y,
                                                            linewidth=2,
                                                            antialiased=True,
                                                            linestyle=data.linestyle,
                                                            color=data.colour,
                                                            label=data.name,
                                                            pickradius=2)[0]
        if self.__fill_curves:
            polygon = self.__polygons.get(data.name, None)
            if polygon:
                polygon.remove()
            self.__polygons[data.name] = self.__axes.fill_between(data.x, data.y,
                                                                  color=data.colour,
                                                                  alpha=self.__fill_alpha)
        return data.name

    def get_ylimits(self, x1, x2):
        '''
        :param x1: the lower x limit.
        :param x2: the upper x limit.
        :return: min y, max y
        '''
        values = [self.__get_ylimits(c.get_xdata(), c.get_ydata(), x1, x2) for c in self.__curves.values()]
        if len(values) > 0:
            miny = math.floor(min([x[0] for x in values]))
            maxy = math.ceil(max([x[1] for x in values]))
            return miny, maxy
        else:
            return 0, 0

    @staticmethod
    def __get_ylimits(x, y, x1, x2):
        '''
        :param x: the x data.
        :param y: the y data.
        :param x1: the lower x limit.
        :param x2: the upper x limit.
        :return: the y data range in the x limits.
        '''
        x1_idx = np.argmax(np.array(x) >= x1)
        x2_idx = np.argmax(np.array(x) >= x2)
        if x2_idx == 0:
            x2_idx = len(x) - 1
        visible_y = np.array(y)[x1_idx:x2_idx]
        return visible_y.min(), visible_y.max()

    def make_legend(self, lines, ncol):
        '''
        makes a legend for the given lines.
        :param lines: the lines to display
        :param ncol: the no of columns to put them in.
        :return the legend.
        '''
        return self.__axes.legend(lines, [l.get_label() for l in lines], loc=3, ncol=ncol, fancybox=True, shadow=True)

    def hide_axes_if_empty(self):
        '''
        Hides the axes if there is nothing here.
        '''
        if self.__axes is not None:
            self.__axes.get_yaxis().set_visible(len(self.curve_names()) > 0)

class MagnitudeModel:
    '''
    Allows a set of filters to be displayed on a chart as magnitude responses.
    '''

    def __init__(self, name, chart, preferences, primary_data_provider, primary_name, primary_prefix='dBFS',
                 secondary_data_provider=None, secondary_name=None, secondary_prefix='dBFS',
                 show_legend=lambda: True, y_range_calc=DecibelRangeCalculator(60),
                 subplot_spec=SINGLE_SUBPLOT_SPEC, redraw_listener=None, grid_alpha=0.5, x_min_pref_key=GRAPH_X_MIN,
                 x_max_pref_key=GRAPH_X_MAX, x_scale_pref_key=GRAPH_X_AXIS_SCALE, fill_curves=False, fill_alpha=0.5,
                 allow_line_resize=False, fill_primary=False, fill_secondary=False, y2_range_calc=None,
                 show_y2_in_legend=True, x_lim: Optional[Tuple[int, int]] = None):
        self.__name = name
        self.__chart = chart
        self.__redraw_listener = redraw_listener
        self.__show_legend = show_legend
        self.__linked_y = y2_range_calc is None
        if allow_line_resize:
            self.__chart.canvas.mpl_connect('pick_event', self.__adjust_line_size)
        primary_axes = self.__chart.canvas.figure.add_subplot(subplot_spec)
        primary_axes.set_ylabel(f"{primary_prefix} ({primary_name})")
        primary_axes.grid(linestyle='-', which='major', linewidth=1, alpha=grid_alpha)
        primary_axes.grid(linestyle='--', which='minor', linewidth=1, alpha=grid_alpha * 0.5)
        self.__primary = AxesManager(primary_data_provider, primary_axes, fill_curves or fill_primary, fill_alpha)
        if secondary_data_provider is None:
            secondary_axes = None
        else:
            secondary_axes = primary_axes.twinx()
            secondary_axes.set_ylabel(f"{secondary_prefix} ({secondary_name})")
            # bump the z axis so pick events are directed to the primary
            primary_axes.set_zorder(secondary_axes.get_zorder() + 1)
            primary_axes.patch.set_visible(False)
        self.__secondary = AxesManager(secondary_data_provider, secondary_axes, fill_curves or fill_secondary,
                                       fill_alpha, show_in_legend=show_y2_in_legend)
        if isinstance(y_range_calc, DecibelRangeCalculator) and not y_range_calc.expand_range:
            y_range_calc.expand_range = preferences.get(GRAPH_EXPAND_Y)
        if x_lim is None:
            x_lim = (preferences.get(x_min_pref_key), preferences.get(x_max_pref_key))
        self.limits = Limits(self.__repr__(), self.__redraw_func, primary_axes,
                             x_lim=x_lim, y1_range_calculator=y_range_calc, axes_2=secondary_axes,
                             x_scale=preferences.get(x_scale_pref_key), y2_range_calculator=y2_range_calc)
        self.limits.propagate_to_axes(draw=True)
        self.__legend = None
        self.__legend_cid = None
        self.redraw()

    def __adjust_line_size(self, event):
        ''' Increases or decreases the line size with each click as long as shift is not held down. '''
        from matplotlib.lines import Line2D
        if isinstance(event.artist, Line2D):
            line = event.artist
            if line in self.__primary.artists() or line in self.__secondary.artists():
                if event.mouseevent.button:
                    if event.mouseevent.button == 1:
                        line.set_linewidth(line.get_linewidth() + 1)
                        self.__chart.canvas.draw_idle()
                    elif event.mouseevent.button == 2 or event.mouseevent.button == 3:
                        line.set_linewidth(max(1, line.get_linewidth() - 1))
                        self.__chart.canvas.draw_idle()

    def __redraw_func(self):
        self.__chart.canvas.draw_idle()
        if callable(self.__redraw_listener):
            self.__redraw_listener()

    def __repr__(self):
        return self.__name

    def redraw(self):
        '''
        Gets the current state of the graph
        '''
        self.__display_all_curves()
        self.__make_legend()
        self.__chart.canvas.draw_idle()

    def show_limits(self):
        '''
        Shows the limits dialog.
        '''
        LimitsDialog(self.limits).exec()

    def show_full_range(self):
        '''
        Sets limits to full range.
        '''
        self.limits.update(x_min=10, x_max=20000, x_scale='log', draw=True)

    def show_sub_only(self):
        '''
        Sets limits to sub only.
        '''
        self.limits.update(x_min=1, x_max=160, x_scale='linear', draw=True)

    def show_values(self):
        '''
        Shows the values dialog.
        '''
        ValuesDialog(self.__primary.artists() + self.__secondary.artists()).exec()

    def get_curve_names(self, primary=True):
        '''
        :param primary: if true get the primary curves.
        :return: the names of all the curves in the chart.
        '''
        return self.__primary.curve_names() if primary else self.__secondary.curve_names()

    def __display_all_curves(self):
        '''
        Updates all the curves with the currently cached data.
        '''
        self.__primary.display_curves()
        self.__secondary.display_curves()
        self.limits.configure_x_axis()
        self.__secondary.hide_axes_if_empty()
        primary_ylim = self.__primary.get_ylimits(self.limits.x_min, self.limits.x_max)
        secondary_ylim = self.__secondary.get_ylimits(self.limits.x_min, self.limits.x_max)
        primary_range = primary_ylim[1] - primary_ylim[0]
        secondary_range = secondary_ylim[1] - secondary_ylim[0]
        if secondary_range > 0 and self.__linked_y is True:
            range_delta = primary_range - secondary_range
            if range_delta > 0:
                secondary_ylim = (secondary_ylim[0] - range_delta, secondary_ylim[1])
            elif range_delta < 0:
                primary_ylim = (primary_ylim[0] + range_delta, primary_ylim[1])
        self.limits.on_data_change(primary_ylim, secondary_ylim)

    def __make_legend(self):
        '''
        Add a legend that allows you to make a line visible or invisible by clicking on it.
        ripped from https://matplotlib.org/2.0.0/examples/event_handling/legend_picking.html
        and https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot
        '''
        if self.__legend is not None:
            self.__legend.remove()
        if self.__legend_cid is not None:
            self.__chart.canvas.mpl_disconnect(self.__legend_cid)

        if self.__show_legend():
            lines = self.__primary.artists() + (self.__secondary.artists() if self.__secondary.show_in_legend else [])
            if len(lines) > 0:
                ncol = int(len(lines) / 3) if len(lines) % 3 == 0 else int(len(lines) / 3) + 1
                self.__legend = self.__primary.make_legend(lines, ncol)
                lined = dict()
                for legline, origline in zip(self.__legend.get_lines(), lines):
                    legline.set_pickradius(5)  # 5 pts tolerance
                    lined[legline] = origline

                # find the line corresponding to the legend proxy line and toggle the alpha
                def onpick(event):
                    if self.__legend is not None and event.artist in self.__legend.get_lines():
                        legline = event.artist
                        origline = lined[legline]
                        vis = not origline.get_visible()
                        origline.set_visible(vis)
                        legline.set_alpha(1.0 if vis else 0.2)
                        self.__chart.canvas.draw_idle()

                self.__legend_cid = self.__chart.canvas.mpl_connect('pick_event', onpick)

    def normalise(self, primary=True, curve=None):
        '''
        Sets the data series that will act as the reference.
        :param curve: the reference curve (if any).
        '''
        if primary is True:
            self.__primary.reference_curve = curve
        else:
            self.__secondary.reference_curve = curve
        self.redraw()

    def set_visible(self, visible):
        ''' changes chart visibility '''
        self.__chart.setVisible(visible)

    def is_visible(self):
        '''
        :return: true if the chart is visible.
        '''
        return self.__chart.isVisible()

    def export_chart(self, status_bar=None):
        ''' Exports the chart. '''
        from app import SaveChartDialog, MatplotlibExportProcessor
        SaveChartDialog(self.__chart, self.__name, self.__chart.canvas.figure,
                        MatplotlibExportProcessor(self.__chart.canvas.figure), statusbar=status_bar).exec()

max_stars_repo_name: bmiller/beqdesigner

max_issues_repo_name: bmiller/beqdesigner

max_forks_repo_name: bmiller/beqdesigner