text:
import os
import numpy as np

test_directory = os.path.dirname(os.path.realpath(__file__))
bsb_directory = '/'.join(test_directory.split('/')[:-1]) + '/'

def z_test_of_proportion(a_yes, a_no, b_yes, b_no):
    a_total = a_yes + a_no
    b_total = b_yes + b_no
    a_prop = a_yes / a_total
    b_prop = b_yes / b_total
    p_hat = (a_yes + b_yes) / (a_total + b_total)
    try:
        return (a_prop - b_prop) / np.sqrt(p_hat * (1 - p_hat) * (1 / a_total + 1 / b_total))
    except RuntimeWarning:
        return 0


text:
import torch
from torch import nn, optim
from torchvision import models
import numpy as np
from collections import OrderedDict

def create_model(arch, hidden_units , prob_dropout):
    # Create model
    model = eval("models." + arch + "(pretrained=True)")

    model.epochs = 0

    # To prevent backprop through parameters freeze parameters
    for param in model.parameters():
        param.requires_grad = False

    # Determine input units of the selected model
    if 'densenet' in arch:
        input_units = model.classifier.in_features
    elif arch == 'alexnet':
        input_units = model.classifier[1].in_features
    elif 'vgg' in arch:
        input_units = model.classifier[0].in_features
    elif 'resnet' in arch:
        input_units = model.fc.in_features
    else:
        raise NameError('The model ' + arch + ' you chose is currently not available in this application. Please check whether you typed your model correctly')

    classifier = nn.Sequential(OrderedDict([
        ('fc1', nn.Linear(input_units, hidden_units)),
        ('relu1', nn.ReLU()),
        ('dropout1', nn.Dropout(prob_dropout)),
        ('fc2', nn.Linear(hidden_units,102)),
        ('output', nn.LogSoftmax(dim=1))
    ]))

    model.classifier = classifier

    # Save additional model parameters
    model.name = arch
    model.input_units = input_units
    model.hidden_units = hidden_units
    model.prob_dropout = prob_dropout

    return model

def validation(model, loader, criterion, device):
    with torch.no_grad():
        accuracy = 0
        loss = 0
        for inputs, labels in loader:
            inputs, labels = inputs.to(device), labels.to(device)

            output = model.forward(inputs)
            loss += criterion(output, labels).item()

            ps = torch.exp(output)
            equality = (labels.data == ps.max(dim=1)[1])
            accuracy += equality.type(torch.FloatTensor).mean()

        loss = loss/len(loader)
        accuracy = accuracy/len(loader)

    return loss, accuracy

def train_model(model, trainloader, learning_rate, epochs, gpu, print_every = 40, validloader=None):
    steps = 0
    criterion = nn.NLLLoss()
    optimizer = optim.Adam(model.classifier.parameters(), lr=learning_rate)

    model.train()
    # Train model on gpu if available
    #device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    if gpu:
        device = "cuda:0"
    else:
        device = "cpu"
    model.to(device)

    for e in range(epochs):
        running_loss = 0
        for inputs, labels in trainloader:
            steps += 1
            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()

            output = model.forward(inputs)
            loss = criterion(output, labels)
            loss.backward()
            optimizer.step()
            running_loss += loss.item()

            if steps % print_every == 0:
                #print("calculate accuracy")

                model.eval()

                # Calculate loss and accuracy of validation set
                if validloader:
                    loss_val, accuracy_val = validation(model, validloader, criterion, device)

                    print("Epoch: {}/{}\t".format(e+1,epochs),
                          ("Step: {}\t".format(steps)),
                          ("Loss (test): {:.4f}\t".format(running_loss/print_every)),
                         ("Loss (val): {:.4f}\t".format(loss_val)),
                         ("Accuracy (val): {:.4f}\n".format(accuracy_val)))
                else:
                    print("Epoch: {}/{}\t".format(e+1,epochs),
                          ("Loss: {:.4f}".format(running_loss/print_every)))
                running_loss = 0

                model.train()
        model.epochs += 1

        return model, optimizer

def save_model(model, optimizer):
    checkpoint = {'state_dict': model.state_dict(),
                  'class_to_idx': model.class_to_idx,
                  'name': model.name,
                  'input_units': model.input_units,
                  'hidden_units': model.hidden_units,
                  'prob_dropout': model.prob_dropout,
                  'n_epochs': model.epochs,
                  'optimizer' : optimizer.state_dict}

    torch.save(checkpoint, 'checkpoint.pth')

    print("model saved to checkpoint.pth")

def load_model(checkpoint, gpu):

    if gpu:
        device = "cuda:0"
    else:
        device = "cpu"

    if device == "cuda:0":
        checkpoint = torch.load(checkpoint, map_location={'cpu': 'cuda:0'})
    else:
        checkpoint = torch.load(checkpoint, map_location={'cuda:0': 'cpu'})

    # Create the identical model
    model = eval("models." + checkpoint['name'] + "(pretrained=True)")
    #model = models.densenet121(pretrained=True)

    for param in model.parameters():
        param.requires_grad = False

    # Create the classifier
    classifier = nn.Sequential(OrderedDict([
    ('fc1', nn.Linear(checkpoint['input_units'], checkpoint['hidden_units'])),
    ('relu1', nn.ReLU()),
    ('dropout1', nn.Dropout(checkpoint['prob_dropout'])),
    ('fc2', nn.Linear(checkpoint['hidden_units'],102)),
    ('output', nn.LogSoftmax(dim=1))
    ]))

    if hasattr(model, 'classifier'):
        model.classifier = classifier
    elif hasattr(model, 'fc'):
        model.fc = classifier

    model.load_state_dict(checkpoint['state_dict'])

    model.epochs = checkpoint['n_epochs']
    model.class_to_idx = checkpoint['class_to_idx']

    return model

def predict(image, model, top_k, gpu, cat_to_name=None):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''
    if gpu:
        device = "cuda:0"
    else:
        device = "cpu"

    # Add additional "image index dimension"
    image = np.expand_dims(image,0)

    image_tensor = torch.FloatTensor(image)

    model.eval()

    with torch.no_grad():
        model.to(device)
        image_tensor = image_tensor.to(device)
        output = model.forward(image_tensor)

        ps = torch.exp(output)

        probs, probs_index = ps.topk(top_k)

        probs, probs_index = probs.tolist()[0], probs_index.tolist()[0]

        class_to_idx = model.class_to_idx
        idx_to_class = {y:x for x,y in class_to_idx.items()}

        classes = [idx_to_class[x] for x in probs_index]

        # Use a mapping of categories to real names
        if cat_to_name:
            classes = [cat_to_name[str(x)] for x in classes]

    model.train()
    return probs, classes

ext: py

reason: import numpy


text:
import numpy as np
import sys
import curses
import traceback
import copy
from collections import namedtuple

from helpers.utils import *

from GameClasses import *

RENDER = True# not is_train_mode()

FIELD_SIZE = Size(40,20) # w,h
START_PLAYER_POSITION = Point(FIELD_SIZE.w/2,0) #x,y
WIN_TIME = int(FIELD_SIZE.h * 3)

#game objects
sym_player = "P"
sym_bullet = "*"
sym_bonus = "+"
sym_empty = " "

#keys
left_key = "a"
right_key = "d"
up_key = "w"
down_key = "s"
none_key = "*"
actions = [left_key,right_key,up_key,down_key,none_key]

vocab = {
	sym_empty:0,
	sym_player:1,
	sym_bullet:2,
	sym_bonus:3,
}
vocab_rev = {v: k for k, v in vocab.items()}

# generating vocabs ids
class Game():
	__myscreen = curses.initscr() if RENDER else None

	def __update_game_state(self):
		self.__game_state = np.zeros(FIELD_SIZE.shape()) # empty
		self.__game_state[self.__player_position.x][self.__player_position.y] = vocab[sym_player]
		self.__animation_time += 1

		# emit bullets
		for emitter in self.__emitters:
			bullet = emitter.emit(self.__animation_time,self.__bullets)

		# move or delete bullets
		bullet_for_deleting = []
		for bullet in self.__bullets:
			bullet.move(self.__animation_time)
			if bullet.origin.x >= 0 and bullet.origin.x < FIELD_SIZE.w and bullet.origin.y >= 0 and bullet.origin.y < FIELD_SIZE.h:
				self.__game_state[bullet.origin.x][bullet.origin.y] = vocab[sym_bullet]
			else:
				bullet_for_deleting.append(bullet)
		self.__bullets = [x for x in self.__bullets if x not in bullet_for_deleting]

	def __del__(self):
		self.stop()

	# public
	def print_controls(self):
		if RENDER:
			self.__myscreen.addstr("q - quit, left_key - {}, right_key - {}, up_key - {}, down_key - {}".format(left_key,right_key,up_key,down_key))

	def reset(self):
		origin = Point(FIELD_SIZE.w/2, FIELD_SIZE.h * 0.8)
		self.__animation_time = 0
		self.__emitters = [
		CircleWithHoleBulletEmitter(origin, PI * -0.5, PI * 1.2, 0.5, 12)
		# VarAngleBulletEmitter(origin, PI * 0., PI * 1.2, 30, True, 1, 1),
		# VarAngleBulletEmitter(origin, PI * 2., PI * 0.8, 30, False, 1, 1),
		# CircleBulletEmitter(origin, 10, 1, 6),
		]
		self.__bullets = []
		self.__player_position = copy.copy(START_PLAYER_POSITION)
		self.__update_game_state()

	def render(self):
		if RENDER:
			y_offset = 2
			self.__myscreen.addstr(y_offset - 1,0,"animation_time: " + str(self.__animation_time) + " WIN_TIME: " + str(WIN_TIME) + "  ")
			for x in range(FIELD_SIZE.w):
				for y in range(FIELD_SIZE.h):
					sym = vocab_rev[int(self.__game_state[x][y])]
					y_rev = FIELD_SIZE.h - 1 - y
					self.__myscreen.addstr(y_rev + y_offset,x,sym)
				self.__myscreen.addstr("\n")
			self.__myscreen.refresh()

	def send_key(self,pressed_key):
		if pressed_key == "q":
			if RENDER:
				curses.endwin() # stty sane
			exit()

		if pressed_key == up_key:
			self.__player_position.y += 1

		if pressed_key == down_key:
			self.__player_position.y -= 1

		if pressed_key == left_key:
			self.__player_position.x -= 1

		if pressed_key == right_key:
			self.__player_position.x += 1

		self.__player_position.x = clamp(self.__player_position.x,0,FIELD_SIZE.w - 1)
		self.__player_position.y = clamp(self.__player_position.y,0,FIELD_SIZE.h - 1)

		self.__update_game_state()

		for bullet in self.__bullets:
			if self.__player_position == bullet.origin:
				self.reset()
				return GameResult.los

		if self.__animation_time % WIN_TIME == 0:
			return GameResult.win

		return GameResult.none

	# aiplayer requirements
	def get_actions(self):
		return actions

	def get_num_actions(self):
		return len(actions)

	def state_shape(self):
		NUM_OF_IMAGE_CHANNELS = 1
		return (NUM_OF_IMAGE_CHANNELS,) + FIELD_SIZE.shape()

	def get_state(self):
		return state_with_channels(self.__game_state)

	def get_train_data(self,action_id):
		animation_time = self.__animation_time
		emitters = copy.deepcopy(self.__emitters)
		bullets = copy.deepcopy(self.__bullets)
		player_position = copy.copy(self.__player_position)

		old_state = np.copy(self.__game_state)
		reward = self.send_key(actions[action_id])
		new_state = np.copy(self.__game_state)
		game_over = reward != GameResult.none

		self.__animation_time = animation_time
		self.__emitters = emitters
		self.__bullets = bullets
		self.__player_position = player_position
		self.__game_state = np.copy(old_state)

		return state_with_channels(old_state), action_id, reward, state_with_channels(new_state), game_over

	def stop(self):
		if RENDER:
			curses.endwin()

def state_with_channels(data):
	return np.expand_dims(data, axis=0)


text:
# Infinite Feature Selection.

# Inputs:
#   x_train: matrix T (samples) by n (number of features).
#   y_train: column vector of labels (-1,1).
#   verbose: boolean variable.

# Outputs:
#   RANKED: vector of indicies of x_train from the best to the worst feature.

import math

import numpy as np
from scipy import stats

class InfFS:
    # Take in input the matrix e the label vector and return a matrix
    # of data for every different label.
    def takeLabel(self, x_train, y_train):
        counter = x_train.shape[0] - 1
        s_n = x_train
        s_p = x_train
        while 1:
            if y_train[counter] == 1:
                s_n = np.delete(s_n, counter, axis=0)
            else:
                s_p = np.delete(s_p, counter, axis=0)
            counter = counter - 1
            if counter == -1:
                break
        return s_p, s_n

    # Function that help to define priors_corr.
    def defPriorsCorr(self, mu_s_n, mu_s_p):
        pcorr = mu_s_p
        counter = 0
        while counter < len(pcorr):
            pcorr[counter] = (pcorr[counter] - mu_s_n[counter]) * (
                pcorr[counter] - mu_s_n[counter]
            )
            counter = counter + 1
        return pcorr

    # Function to subtract the min value of the matrix to all it's elements.
    def SubtractMin(self, corr_ij):
        m = 10100
        for i in range(0, corr_ij.shape[0]):  # Find the min.
            for j in range(0, corr_ij.shape[1]):
                if corr_ij[i, j] < m:
                    m = corr_ij[i, j]

        for i in range(0, corr_ij.shape[0]):  # Subtract the min value.
            for j in range(0, corr_ij.shape[1]):
                corr_ij[i, j] = corr_ij[i, j] - m

        return corr_ij

    # Function to divide every element of the matrix to his maximum value.
    def DivideByMax(self, corr_ij):
        m = -1
        for i in range(0, corr_ij.shape[0]):  # Find the max.s
            for j in range(0, corr_ij.shape[1]):
                if corr_ij[i, j] > m:
                    m = corr_ij[i, j]

        for i in range(0, corr_ij.shape[0]):  # Divide by the maximum value.
            for j in range(0, corr_ij.shape[1]):
                corr_ij[i, j] = corr_ij[i, j] / m

        return corr_ij

    # Handmaded bsxfunction that take the max.
    def bsxfun(self, STD):
        m = np.zeros((STD.shape[0], STD.shape[0]))
        for i in range(0, STD.shape[0]):
            for j in range(0, STD.shape[0]):
                if STD[i] > STD[j]:
                    m[i, j] = STD[i]
                else:
                    m[i, j] = STD[j]
        return m

    def infFS(self, x_train, y_train, alpha, supervision, verbose):
        # Start of point one.

        if supervision:
            s_p, s_n = self.takeLabel(x_train, y_train)
            mu_s_n = s_n.mean(0)
            mu_s_p = s_p.mean(0)
            priors_corr = self.defPriorsCorr(mu_s_n, mu_s_p)
            st = np.power(np.std(s_p, ddof=1, axis=0), 2)
            st = st + np.power(np.std(s_n, ddof=1, axis=0), 2)
            for i in range(0, len(st)):
                if st[i] == 0:
                    st[i] = 10000
            corr_ij = priors_corr
            for i in range(0, len(corr_ij)):
                corr_ij[i] = corr_ij[i] / st[i]
            corr_ij = np.dot(corr_ij.T[:, None], corr_ij[None, :])
            corr_ij = self.SubtractMin(corr_ij)
            corr_ij = self.DivideByMax(corr_ij)
        else:
            corr_ij, pval = stats.spearmanr(x_train)
            for i in range(0, corr_ij.shape[0]):
                for j in range(0, corr_ij.shape[1]):
                    if (
                        math.isnan(corr_ij[i, j])
                        or corr_ij[i, j] < -1
                        or corr_ij[i, j] > 1
                    ):
                        corr_ij[i, j] = 0

        # After if.
        STD = np.std(x_train, ddof=1, axis=0)
        STDMatrix = self.bsxfun(STD)
        STDMatrix = self.SubtractMin(STDMatrix)
        sigma_ij = self.DivideByMax(STDMatrix)
        for i in range(0, sigma_ij.shape[0]):
            for j in range(0, sigma_ij.shape[1]):
                if (
                    math.isnan(sigma_ij[i, j])
                    or sigma_ij[i, j] < -1
                    or sigma_ij[i, j] > 1
                ):
                    sigma_ij[i, j] = 0

        # End of point one.

        # Start of the point two.
        if verbose:
            print("2) Building the graph G = <V,E> \n")
        A = alpha * corr_ij + (1 - alpha) * sigma_ij
        # End of the point two.

        # Start of the point three.
        if verbose:
            print("3) Letting paths tend to infinite \n")

        I_matrix = np.identity(A.shape[0])
        r = 0.9 / max(np.linalg.eigvals(A))  # Setting the r values.
        y = I_matrix - (r * A)
        S = np.linalg.inv(y) - I_matrix
        # End of point three.

        # Start of point four.
        if verbose:
            print("4) Estimating energy scores \n")

        WEIGHT = np.sum(S, axis=1)
        # End of point four.

        # Start of point five.
        if verbose:
            print("5) Features ranking")

        RANKED = np.argsort(WEIGHT)
        RANKED = np.flip(RANKED, 0)
        RANKED = RANKED.T
        WEIGHT = WEIGHT.T
        return RANKED, WEIGHT
        # End of point five.

max_stars_repo_name: dunnkers/infinite-selection

max_issues_repo_name: dunnkers/infinite-selection

max_forks_repo_name: dunnkers/infinite-selection

reason: import numpy,from scipy


text:
import os
import sys
import argparse
import glob

import cv2
import numpy
from tqdm import tqdm

from utils import get_image_paths, load_images, stack_images
from training_data import get_training_data

from model import autoencoder_A
from model import autoencoder_B
from model import encoder, decoder_A, decoder_B

# check out directory directory and create if necessary
if not os.path.exists("./models/"):
    os.makedirs("./models/")

try:
    encoder.load_weights( "models/encoder.h5"   )
    decoder_A.load_weights( "models/decoder_A.h5" )
    decoder_B.load_weights( "models/decoder_B.h5" )
    print( "loaded existing model" )
except:
    # no model to load create new one
    pass

def save_model_weights():
    encoder.save_weights( "models/encoder.h5"   )
    decoder_A.save_weights( "models/decoder_A.h5" )
    decoder_B.save_weights( "models/decoder_B.h5" )
    print( "save model weights" )

if __name__ == "__main__":
    # parse command line options
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument("--src", help="Name of a Celebrity (e.g. Angela Merkel)")
    parser.add_argument("--dst", help="Name of a Person to swap the face with (e.g. Taylor Swift)")
    parser.add_argument("--epochs", default=100000, type=int, help="Number of Epochs to train")
    args = parser.parse_args()

    # check out directory directory and create if necessary
    if not os.path.exists("./out/"):
        os.makedirs("./out/")
    # empty directory
    for f in glob.glob(os.path.join("./out/", "*.jpg")):
        os.remove(f)

    # load dataset images
    images_A = get_image_paths( "data/faces/{}".format(args.src.lower().replace(" ", "_")))
    images_B = get_image_paths( "data/faces/{}".format(args.dst.lower().replace(" ", "_")))
    # map between 0 and 1 and normalize images
    images_A = load_images( images_A ) / 255.0
    images_B = load_images( images_B ) / 255.0
    images_A += images_B.mean( axis=(0,1,2) ) - images_A.mean( axis=(0,1,2) )

    # create 100 preview images during training (to not spam your disk)
    print_rate = args.epochs // 100
    if print_rate < 1:
        print_rate = 1

    # iterate epochs and train
    for epoch in tqdm(range(args.epochs)):
        # get next training batch
        batch_size = 64
        warped_A, target_A = get_training_data( images_A, batch_size )
        warped_B, target_B = get_training_data( images_B, batch_size )

        # train and calculate loss
        loss_A = autoencoder_A.train_on_batch( warped_A, target_A )
        loss_B = autoencoder_B.train_on_batch( warped_B, target_B )

        if epoch % 100 == 0:
            # print training loss
            # print( loss_A, loss_B )

            # save model every 100 steps
            save_model_weights()
            test_A = target_A[0:14]
            test_B = target_B[0:14]

        if epoch % print_rate == 0:
            # visualize result (orginal, decoderA result, decoderB result)
            figure_A = numpy.stack([
                test_A,
                autoencoder_A.predict( test_A ),
                autoencoder_B.predict( test_A ),
                ], axis=1 )
            figure_B = numpy.stack([
                test_B,
                autoencoder_B.predict( test_B ),
                autoencoder_A.predict( test_B ),
                ], axis=1 )

            # stack images together to create a preview sheet
            figure = numpy.concatenate( [ figure_A, figure_B ], axis=0 )
            figure = figure.reshape( (4,7) + figure.shape[1:] )
            figure = stack_images( figure )

            # create image and write to disk
            figure = numpy.clip( figure * 255, 0, 255 ).astype('uint8')
            cv2.imwrite( "./out/" + str(epoch) + ".jpg", figure )

    # save our model after training has finished
    save_model_weights()

max_stars_repo_name: phiomy/youtube-video-face-swap-forked

max_issues_repo_name: phiomy/youtube-video-face-swap-forked

max_forks_repo_name: phiomy/youtube-video-face-swap-forked


text:
from benchmark import Benchmark, benchmark
import astropy.units as u
import pytest

@benchmark(
    {
        "log.final.star.LXUVStellar": {
            "value": 4.988314e21,
            "unit": u.kg * u.m ** 2 / u.sec ** 3,
        },
        "log.final.ProxCenELim.EnvelopeMass": {"value": 0.000526, "unit": u.Mearth,},
        "log.final.ProxCenELim.PlanetRadius": {"value": 1.335673, "unit": u.Rearth},
        "log.final.ProxCenELim.SurfWaterMass": {"value": 1.500000, "unit": u.TO},
        "log.final.ProxCenRRLim.EnvelopeMass": {"value": 0.000300, "unit": u.Mearth,},
        "log.final.ProxCenRRLim.PlanetRadius": {"value": 1.339401, "unit": u.Rearth},
        "log.final.ProxCenRRLim.SurfWaterMass": {"value": 1.500000, "unit": u.TO},
        "log.final.ProxCenAuto.EnvelopeMass": {"value": 0.000526, "unit": u.Mearth,},
        "log.final.ProxCenAuto.PlanetRadius": {"value": 1.335673, "unit": u.Rearth},
        "log.final.ProxCenAuto.SurfWaterMass": {"value": 1.500000, "unit": u.TO},
        "log.final.LC17ELim.EnvelopeMass": {"value": 0.000000, "unit": u.Mearth,},
        "log.final.LC17ELim.PlanetRadius": {"value": 1.063513, "unit": u.Rearth},
        "log.final.LC17ELim.SurfWaterMass": {"value": 1.304141, "unit": u.TO},
        "log.final.LC17RRLim.EnvelopeMass": {"value": 0.000000, "unit": u.Mearth,},
        "log.final.LC17RRLim.PlanetRadius": {"value": 1.063513, "unit": u.Rearth},
        "log.final.LC17RRLim.SurfWaterMass": {"value": 1.347668, "unit": u.TO},
        "log.final.LC17Auto.EnvelopeMass": {"value": 0.000000, "unit": u.Mearth,},
        "log.final.LC17Auto.PlanetRadius": {"value": 1.063513, "unit": u.Rearth},
        "log.final.LC17Auto.SurfWaterMass": {"value": 1.347368, "unit": u.TO},
    }
)
class TestHabEvapCore(Benchmark):
    pass

max_stars_repo_name: teald/vplanet

max_issues_repo_name: teald/vplanet

max_forks_repo_name: teald/vplanet

reason: import astropy


text:
import abc
import fractions
import numbers

import attr
import numpy as np

from .sum import Summation
from .util import i_to_the_power

__all__ = ['Base', 'Conj', 'Integral', 'Mult', 'T', 'HIXZ']

class Z(abc.ABC):
    """
    The functional part of a scalar term.  This is the actual part we're able
    to pulse-shape to achieve the results in the paper.

    Nothing actually instantiates the base class, it's just used as an abstract
    base for the other components.
    """
    def __init__(self, z):
        self.z = z

    def __repr__(self):
        return ''.join([self.__class__.__name__, '(', repr(self.z), ')'])

    def integrate(self):
        return Integral(self)

    def __mul__(self, other):
        if not isinstance(other, Z):
            return NotImplemented

        return Mult([self, other])

    def __eq__(self, other): return zcmp(self, other) == 0
    def __lt__(self, other): return zcmp(self, other) <  0
    def __le__(self, other): return zcmp(self, other) <= 0
    def __gt__(self, other): return zcmp(self, other) >  0
    def __ge__(self, other): return zcmp(self, other) >= 0

class Base(Z):
    def conj(self):
        return Conj(self)

class Conj(Z):
    def conj(self):
        return self.z

class Integral(Z):
    def conj(self):
        return Integral(self.z.conj())

class Mult(Z):
    def __init__(self, z):
        super().__init__(z)
        self.z = tuple(sorted(flatten_multiply(z)))

    def __repr__(self):
        return ''.join([self.__class__.__name__,
                        '((', ', '.join(map(repr, self.z)), '))'])

    def conj(self):
        return Mult(z.conj() for z in self.z)

@attr.s(frozen=True)
class HIXZ:
    h = attr.ib(converter=tuple)
    i = attr.ib(converter=int)
    x = attr.ib(converter=fractions.Fraction)
    z = attr.ib(type=Z)

    def __attrs_post_init__(self):
        if self.i not in [0, 1]:
            sign, i = i_to_the_power(self.i)
            # Slight hack to temporarily override attr's class freezing.
            object.__setattr__(self, 'i', i)
            object.__setattr__(self, 'x', sign*self.x)

    def conj(self):
        return type(self)(self.h,
                          self.i,
                          -self.x if self.i else self.x,
                          self.z.conj())

    def integrate(self):
        return type(self)(self.h, self.i, self.x, self.z.integrate())

    def __add__(self, other):
        if isinstance(other, HIXZ) and (self.h == other.h)\
                                   and (self.i == other.i)\
                                   and (self.z == other.z):
            x = self.x + other.x
            if x == 0:
                return None
            return attr.evolve(self, x=x)
        return NotImplemented

    def __radd__(self, other): return self.__add__(other)

    def __sub__(self, other):
        return self.__add__(-other)

    def __rsub__(self, other):
        return (-self).__add__(other)

    def __neg__(self):
        return self.__mul__(-1)

    def __mul__(self, other):
        if isinstance(other, numbers.Rational):
            return HIXZ(self.h, self.i, other * self.x, self.z)
        if isinstance(other, numbers.Complex):
            if other.real and other.imag:
                return NotImplemented
            x, i = (other.real, 0) if other.imag == 0 else (other.imag, 1)
            x = fractions.Fraction(x)
            if self.i + i == 2:
                x = -x
            return HIXZ(self.h, (self.i+i) % 2, x*self.x, self.z)
        if isinstance(other, HIXZ):
            sign, i = i_to_the_power(self.i + other.i)
            x = sign * self.x * other.x
            h_ = tuple(ours + theirs for ours, theirs in zip(self.h, other.h))
            return HIXZ(h_, i, x, self.z*other.z)
        return NotImplemented

    def __rmul__(self, other): return self.__mul__(other)

def hxz_hash(hxz):
    return (hxz.h, repr(hxz.z))

class T(Summation):
    max_h = np.inf

    def __init__(self, arg=(0,), i=0, const=1, f=1, hash_=hxz_hash):
        if isinstance(arg, dict):
            self._fast_init(arg, hash_)
            return
        if isinstance(arg, HIXZ):
            arg = [arg]
        arg = list(arg)
        if arg and isinstance(arg[0], numbers.Integral):
            arg = [HIXZ(tuple(arg), i, const, Base(f))]
        arg = [a for a in arg if sum(a.h) <= self.max_h]
        super().__init__(list(arg), hash_)

    def copy(self):
        return type(self)(self._dict.copy(), hash_=self._hash)

    def integrate(self):
        return type(self)((x.integrate() for x in self), hash_=self._hash)

    def conj(self):
        return type(self)((x.conj() for x in self), hash_=self._hash)

def flatten_multiply(zs):
    for z in zs:
        if isinstance(z, Mult):
            yield from flatten_multiply(z.z)
        else:
            yield z

_num = {Base: 0, Conj: 1, Integral: 2, Mult: 3}
def zcmp(a, b):
    """
    Lazy implementation of a comparison between two functional representations
    (the `Z` class).  It's old-style `cmp` format because I couldn't be
    bothered to write 6 different comparison functions when I was just quickly
    prototyping it.
    """
    if not isinstance(a, Z) and not isinstance(b, Z):
        return (a > b) - (a < b)
    elif not isinstance(a, Z):
        return 1
    elif not isinstance(b, Z):
        return -1
    # Impose some canonical ordering to ensure a unique sort.
    an, bn = _num[type(a)], _num[type(b)]
    if an != bn:
        return an - bn
    if not isinstance(a, Mult):
        return zcmp(a.z, b.z)
    # Mult.z is sorted by the __init__ method.
    for aa, bb in zip(a.z, b.z):
        c = zcmp(aa, bb)
        if c != 0:
            return c
    return len(a.z) - len(b.z)


text:
import ismrmrd
import numpy as np
import struct
import ctypes

from gadgetron.external.constants import uint64, GadgetMessageIdentifier, GADGET_MESSAGE_RECON_DATA
from gadgetron.types.serialization import NDArray
import dataclasses
from typing import Optional, List

uint16 = struct.Struct('<H')

class SamplingLimit(ctypes.Structure):
    _fields_ = [
        ('min', ctypes.c_uint16),
        ('center', ctypes.c_uint16),
        ('max', ctypes.c_uint16)
    ]

    def __iter__(self):
        return iter([self.min, self.center, self.max])

class SamplingDescription(ctypes.Structure):
    _fields_ = [
        ('encoded_FOV', ctypes.c_float * 3),
        ('recon_FOV', ctypes.c_float * 3),
        ('encoded_matrix', ctypes.c_uint16 * 3),
        ('recon_matrix', ctypes.c_uint16 * 3),
        ('sampling_limits', SamplingLimit * 3)
    ]

@dataclasses.dataclass
class ReconBuffer:
    data: NDArray[np.complex64]
    trajectory: Optional[NDArray[np.float32]]
    density : Optional[NDArray[np.float32]]
    headers : NDArray[ismrmrd.AcquisitionHeader]
    sampling_description : SamplingDescription

@dataclasses.dataclass
class ReconBit:
    data : ReconBuffer
    ref : Optional[ReconBuffer]

@dataclasses.dataclass
class ReconData:
    bits: List[ReconBit]

    def __getitem__(self, item):
        return self.bits[item]

    def __iter__(self):
        return iter(self.bits)

max_stars_repo_name: dchansen/gadgetron-python

max_issues_repo_name: dchansen/gadgetron-python

max_forks_repo_name: dchansen/gadgetron-python


text:
from __future__ import absolute_import, print_function
import ctypes
from ctypes import c_int, c_uint, c_byte, c_ubyte, c_short, c_double, c_float, c_long
from ctypes import pointer, byref, windll, cdll
import time
import numpy as np
import os
from threading import Lock

import platform
import logging

from enum import Enum

from . import andor_ccd_consts as consts

logger = logging.getLogger(__name__)

DEFAULT_TEMPERATURE = -80
DEFAULT_EM_GAIN = 10
DEFAULT_OUTPUT_AMP = 0  # 0 is electron multiplication, 1 is conventional

# Read modes for the EMCCD:
class AndorReadMode(Enum):
    FullVerticalBinning = 0
    MultiTrack = 1
    RandomTrack = 2
    SingleTrack = 3
    Image = 4

def _err(retval):
    if retval == consts.DRV_SUCCESS:
        return retval
    else:
        err_name = consts.consts_by_num.get(retval)
        raise IOError( "Andor DRV Failure {}: {}".format(retval, err_name))

class AndorCCD(object):

    def __init__(self, debug = False, initialize_to_defaults=True):

        self.debug = debug
        self.lock = Lock()

        if platform.architecture()[0] == '64bit':
            andorlibpath = r"C:\Program Files\Andor SOLIS\atmcd64d_legacy.dll"
            if not os.path.exists(andorlibpath):
                andorlibpath = str(os.path.join(os.path.dirname(__file__),"atmcd64d.dll"))
        else:
            andorlibpath = str(os.path.join(os.path.dirname(__file__),"atmcd32d.dll"))
        #print andorlibpath

        self.andorlib = windll.LoadLibrary(andorlibpath)

        if self.debug:  logger.debug("AndorCCD initializing")

        with self.lock: _err(self.andorlib.Initialize(''))
        if self.debug: logger.debug("Andor CCD Library Initialization Successful")

        self.get_head_model()

        self.get_serial_number()

        self.get_hardware_version()

        self.get_software_version()

        self.get_detector_shape()

        self.get_num_ad_channels()
        self.get_num_output_amplifiers()

        if initialize_to_defaults:
            self.set_ad_channel() #set default AD channel
            self.set_aq_single_scan() # set to single scan by default
            self.set_num_accumulations(1)
            self.set_num_kinetics(1)

        # EM gain
        self.em_mode = self.has_em_ccd()
        if self.em_mode:
            self.get_EM_gain_range()
            self.get_EMCCD_gain()

        #shift speeds
        self.read_shift_speeds()
        if initialize_to_defaults:
            if self.em_mode: self.set_hs_speed_em()
            self.set_hs_speed_conventional()
            self.set_vs_speed()

        # gains
        self.get_preamp_gains()
        if initialize_to_defaults:
            self.set_preamp_gain()

        # temperature
        self.get_temperature_range()
        self.get_temperature()

        if initialize_to_defaults:
            self.set_temperature(DEFAULT_TEMPERATURE)
            self.set_cooler_on()

            # Initialize the camera
            self.set_shutter_open(False)             # Shutter closed
            self.set_output_amp(DEFAULT_OUTPUT_AMP)  # Default output amplifier

            if self.em_mode:
                self.set_EMCCD_gain(DEFAULT_EM_GAIN)     # Default EM Gain

    ##### Initialization Functions

    def get_head_model(self):
        headModel = ctypes.create_string_buffer(consts.MAX_PATH)
        with self.lock: _err(self.andorlib.GetHeadModel(headModel))
        self.headModel = headModel.raw.decode().strip('\x00')
        print(self.headModel)
        if self.debug: logger.debug("Head model: "+ repr(self.headModel))
        return self.headModel

    def get_serial_number(self):
        serialNumber = c_int(-1)
        with self.lock: _err(self.andorlib.GetCameraSerialNumber(byref(serialNumber)))
        self.serialNumber = serialNumber.value
        if self.debug: logger.debug('Serial Number: %g' % self.serialNumber)
        return serialNumber.value

    def get_hardware_version(self):
        HW = [ c_int(i) for i in range(6) ]
        with self.lock: _err(self.andorlib.GetHardwareVersion( *[ byref(h) for h in HW ] ))
        self.hardware_version = tuple([ h.value for h in HW])
        if self.debug: logger.debug('Hardware information: {}'.format( repr(self.hardware_version)))
        return self.hardware_version

    def get_software_version(self):
        SW = [ c_int(i) for i in range(6) ]
        with self.lock: _err(self.andorlib.GetSoftwareVersion( *[byref(s) for s in SW] ))
        self.software_version = tuple([ s.value for s in SW ])
        if self.debug: logger.debug('Software information: %s' % repr(self.software_version))
        return self.software_version

    def get_detector_shape(self):
        """ returns number of pixels Nx, Ny"""
        pixelsX = c_int(1)
        pixelsY = c_int(1)

        with self.lock: _err(self.andorlib.GetDetector(byref(pixelsX), byref(pixelsY)))
        self.Nx = pixelsX.value
        self.Ny = pixelsY.value
        if self.debug: logger.debug("Dimensions: {} {}".format( self.Nx, self.Ny ))
        return self.Nx, self.Ny

    def get_num_ad_channels(self):
        numADChan = c_int(-1)
        retval = self.andorlib.GetNumberADChannels(byref(numADChan))
        assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
        self.numADChan = numADChan.value
        if self.debug: logger.debug( '# of AD channels [expecting one]: %g' % self.numADChan )
        return self.numADChan

    def get_num_output_amplifiers(self):
        ampNum = c_int(-1)
        retval = self.andorlib.GetNumberAmp(byref(ampNum))
        assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
        self.ampNum = ampNum.value
        if self.debug: logger.debug( 'Number of output amplifiers: %g' % self.ampNum )
        return self.ampNum

    def get_preamp_gains(self):
        numGains = c_int(-1)
        with self.lock: _err(self.andorlib.GetNumberPreAmpGains(pointer(numGains)))
        if self.debug: logger.debug('# of gains: %g '% numGains.value)
        self.numGains = numGains.value
        self.preamp_gains = []
        gain = c_float(-1)
        for i in range(numGains.value) :
            with self.lock: _err(self.andorlib.GetPreAmpGain(i, byref(gain)))
            self.preamp_gains.append(gain.value)
        if self.debug: logger.debug('Preamp gains available: %s' % self.preamp_gains)
        return self.preamp_gains

    def has_em_ccd(self):
        try:
            self.get_EM_gain_range()
            return True
        except IOError:
            return False

    #####

    def set_ad_channel(self,chan_i=0):
        assert chan_i in range(0,self.numADChan)
        with self.lock: _err(self.andorlib.SetADChannel(int(chan_i)))
        self.ad_chan = chan_i
        return self.ad_chan

    def create_buffer(self):
        if self.aq_mode in ('single', 'accumulate', 'run_till_abort'):
            self.buffer = np.zeros(shape=(self.Ny_ro, self.Nx_ro), dtype=np.int32 )
        elif self.aq_mode == 'kinetic':
            self.get_num_kinetics()
            self.buffer = np.zeros(shape=(self.num_kin, self.Ny_ro, self.Nx_ro), dtype=np.int32)
        else:
            raise ValueError("Andor Unkown acq mode {}".format(self.aq_mode))
        print(self.buffer.shape)
        return self.buffer

    ##### ReadOut Modes #######################
    def set_readout_mode(self, ro_mode):
        if (ro_mode == AndorReadMode.FullVerticalBinning):
            self.set_ro_full_vertical_binning()
        elif (ro_mode == AndorReadMode.Image):
            self.set_ro_image_mode()
        elif (ro_mode == AndorReadMode.MultiTrack):
            raise NotImplementedError()
        elif (ro_mode == AndorReadMode.RandomTrack):
            raise NotImplementedError()
        elif (ro_mode == AndorReadMode.SingleTrack):
            self.set_ro_single_track(256, 20)

    def set_read_mode_by_name(self,name):
        read_mode_dict = dict(
            FullVerticalBinning = 0,
            MultiTrack = 1,
            RandomTrack = 2,
            SingleTrack = 3,
            Image = 4)

        readout_mode_id = read_mode_dict[name]
        self.set_readmode(readout_mode_id)

    def set_read_mode(self, mode_id):
        with self.lock: _err(self.andorlib.SetReadMode(mode_id))

    def set_ro_full_vertical_binning(self, hbin=1):
        self.ro_mode = 'FULL_VERTICAL_BINNING'
        with self.lock: _err(self.andorlib.SetReadMode(0)) # sets to FVB
        self.ro_fvb_hbin = hbin
        with self.lock: _err(self.andorlib.SetFVBHBin(self.ro_fvb_hbin))
        #self.outputHeight = 1
        self.Nx_ro = int(self.Nx/hbin)
        self.Ny_ro = 1
        self.create_buffer()

    def set_ro_single_track(self, center, width = 1, hbin = 1):
        self.ro_mode = 'SINGLE_TRACK'
        with self.lock: _err(self.andorlib.SetReadMode(3))
        with self.lock: _err(self.andorlib.SetSingleTrack(c_int(center), c_int(width)) )

        with self.lock: _err(self.andorlib.SetSingleTrackHBin(c_int(hbin)))

        self.ro_st_hbin = hbin
        self.Nx_ro = int(self.Nx/hbin)
        self.Ny_ro = 1

        self.ro_single_track_center = center
        self.ro_single_track_width = width

        self.create_buffer()

    def set_ro_multi_track(self, number, height, offset):
        # NOT YET IMPLEMENTED
        # SetReadMode(1)
        # SetMulitTrack
        raise NotImplementedError
        #returns bottom, gap

    def set_ro_random_track(self, positions):
        # NOT YET IMPLEMENTED
        # SetReadMode(2)
        #SetRandomTracks(numberoftracks, positionarray)
        raise NotImplementedError

    def set_ro_image_mode(self,hbin=1,vbin=1,hstart=1,hend=None,vstart=1,vend=None):
        self.ro_mode = 'IMG'
        with self.lock: _err(self.andorlib.SetReadMode(4))

        if hend is None:
            hend = self.Nx
        if vend is None:
            vend = self.Ny

        assert hend > hstart
        assert vend > vstart

        self.hbin = hbin
        self.vbin = vbin

        self.hstart = hstart
        self.hend   = hend

        self.vstart = vstart
        self.vend   = vend

        with self.lock: _err(self.andorlib.SetImage(c_int(hbin),   c_int(vbin),
                          c_int(hstart), c_int(hend),
                          c_int(vstart), c_int(vend) ))

        self.Nx_ro = int((self.hend-self.hstart+1)/self.hbin)
        self.Ny_ro = int((self.vend-self.vstart+1)/self.vbin)

        logger.debug("self.Nx_ro: {}, self.Ny_ro: {}".format( self.Nx_ro, self.Ny_ro ))

        self.create_buffer()

    ### Function to return the binning based on the current readout mode ####
    def get_current_hbin(self):
        if self.ro_mode == 'IMG':
            return self.hbin
        elif self.ro_mode == 'SINGLE_TRACK':
            return self.ro_st_hbin
        elif self.ro_mode == 'FULL_VERTICAL_BINNING':
            return self.ro_fvb_hbin

    ##### Acquisition Modes #####
    def set_aq_mode(self, mode):
        print('set_aq_mode', mode)
        assert mode in ('single', 'accumulate', 'kinetic', 'run_till_abort')
        if mode == 'single': return self.set_aq_single_scan()
        if mode == 'accumulate': return self.set_aq_accumulate_scan()
        if mode == 'kinetic': return self.set_aq_kinetic_scan()
        if mode == 'run_till_abort': return self.set_aq_run_till_abort_scan()

    def get_aq_mode(self):
        return self.aq_mode

    def set_aq_single_scan(self, exposure=None):
        self.aq_mode = 'single'
        with self.lock: _err(self.andorlib.SetAcquisitionMode(1))

        if exposure is not None:
            with self.lock: _err(self.andorlib.SetExposureTime(c_float(exposure)))

    def set_aq_accumulate_scan(self, exposure_time=None, num_acc=None, cycle_time=None):
        self.aq_mode = 'accumulate'

        with self.lock: _err(self.andorlib.SetAcquisitionMode(2))

        if exposure_time is not None:
            with self.lock: _err(self.andorlib.SetExposureTime(c_float(exposure_time)))

        if num_acc is not None:
            with self.lock: _err(self.andorlib.SetNumberAccumulations(num_acc))

        # cycle_time only valid with internal trigger
        if cycle_time is not None:
            with self.lock: _err(self.andorlib.SetAccumulationCycleTime(cycle_time))

    def set_aq_kinetic_scan(self, exp_time=None,
                            num_acc=None, acc_time=None,
                            num_kin=None, kin_time=None):
        self.aq_mode = 'kinetic'

        with self.lock: _err(self.andorlib.SetAcquisitionMode(3))

        if exp_time is not None:
            with self.lock: _err(self.andorlib.SetExposureTime(c_float(exp_time)))
        if num_acc is not None:
            with self.lock: _err(self.andorlib.SetNumberAccumulations(num_acc))
        if acc_time is not None:
            with self.lock: _err(self.andorlib.SetAccumulationCycleTime(acc_time))
        if num_kin is not None:
            with self.lock: _err(self.andorlib.SetNumberKinetics(num_kin))
        # kinetic cycle time only valid with internal trigger
        if kin_time is not None:
            with self.lock: _err(self.andorlib.SetKineticCycleTime(kin_time))
        print('kinetic')

    def set_aq_run_till_abort_scan(self):
        self.aq_mode = 'run_till_abort'
        #SetAcquistionMode(5) SetExposureTime(0.3) SetKineticCycleTime(0)
        print('set_aq_run_till_abort_scan')
        with self.lock: _err(self.andorlib.SetAcquisitionMode(5))
        print('set_aq_run_till_abort_scan')

    def set_aq_fast_kinetic_scan(self):
        # NOT YET IMPLEMENTED
        raise NotImplementedError()

    def set_aq_frame_transfer_scan(self):
        # NOT YET IMPLEMENTED
        raise NotImplementedError()

    ##### Triggering ##########
    trigger_modes = dict(
                     internal = 0,
                     external = 1,
                     external_start = 6,
                     external_exposure = 7,
                     external_fvb_em = 9,
                     software = 10)

    def set_trigger_mode(self, mode='internal'):
        mode = mode.lower()
        retval = self.andorlib.SetTriggerMode(self.trigger_modes[mode])
        assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval

    ####### Shift Speeds and Gain ##########

    def read_shift_speeds(self):
        # h speeds
        numHSSpeeds = c_int(-1)
        self.numHSSpeeds_EM = []
        self.numHSSpeeds_Conventional = []
        for chan_i in range(self.numADChan):
            if self.em_mode:
                retval = self.andorlib.GetNumberHSSpeeds(chan_i, 0, byref(numHSSpeeds)) # EM mode
                assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
                self.numHSSpeeds_EM.append(numHSSpeeds.value)

                retval = self.andorlib.GetNumberHSSpeeds(chan_i, 1, byref(numHSSpeeds)) # conventional mode mode
                assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
                self.numHSSpeeds_Conventional.append(numHSSpeeds.value)
            else:
                retval = self.andorlib.GetNumberHSSpeeds(chan_i, 0, byref(numHSSpeeds)) # EM mode
                assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
                self.numHSSpeeds_Conventional.append(numHSSpeeds.value)

        logger.debug('# of horizontal speeds EM: {}'.format(self.numHSSpeeds_EM))
        logger.debug('# of horizontal speeds Conventional: {}'.format(self.numHSSpeeds_Conventional))

        self.HSSpeeds_EM = []
        self.HSSpeeds_Conventional = []
        speed = c_float(0)
        for chan_i in range(self.numADChan):
            self.HSSpeeds_EM.append([])
            if self.em_mode:
                hsspeeds = self.HSSpeeds_EM[chan_i]
                for i in range(self.numHSSpeeds_EM[chan_i]):
                    retval = self.andorlib.GetHSSpeed(chan_i, 0, i, byref(speed)) # EM mode
                    assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
                    hsspeeds.append(speed.value)
                conventional_index = 1
            else:
                conventional_index = 0

            self.HSSpeeds_Conventional.append([])
            hsspeeds = self.HSSpeeds_Conventional[chan_i]
            for i in range(self.numHSSpeeds_Conventional[chan_i]):
                #print chan_i, i
                retval = self.andorlib.GetHSSpeed(chan_i,  conventional_index, i, byref(speed)) # Conventional mode
                assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
                hsspeeds.append(speed.value)

        logger.debug('EM Horizontal speeds: {} MHz'.format(self.HSSpeeds_EM))
        logger.debug('Conventional Horizontal speeds: {} MHz'.format(self.HSSpeeds_Conventional))
        #Vertical  speeds
        numVSSpeeds = c_int(-1)
        retval = self.andorlib.GetNumberVSSpeeds(byref(numVSSpeeds))
        if retval == 20991: # DRV_NOT_SUPPORTED, for the case of iDus IR InGaAs single line detectors
            self.numVSSpeeds = 0
        else:
            assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
            self.numVSSpeeds = numVSSpeeds.value

        self.VSSpeeds = []
        speed = c_float(0)
        for i in range(self.numVSSpeeds):
            retval = self.andorlib.GetVSSpeed(i, byref(speed))
            assert retval == consts.DRV_SUCCESS, "Andor DRV Failure %i" % retval
            self.VSSpeeds.append(speed.value)
        if self.debug: logger.debug( 'Vertical speeds [microseconds per pixel shift]: %s' % self.VSSpeeds)

    def get_hs_speed_val_conventional(self, speed_index):
        pass

    def set_hs_speed_em(self,speed_index=0):
        logger.debug("set_hs_speed_em {}".format(speed_index))
        assert 0 <= speed_index < self.numHSSpeeds_EM[self.ad_chan]
        with self.lock: _err(self.andorlib.SetHSSpeed(0, speed_index)) # 0 = default speed (fastest), #arg0 -> EM mode = 0

    def set_hs_speed_conventional(self,speed_index=0):
        print("set_hs_speed_conventional", speed_index, self.ad_chan)
        assert 0 <= speed_index < self.numHSSpeeds_Conventional[self.ad_chan]
        if self.em_mode:
            with self.lock: _err(self.andorlib.SetHSSpeed(1, speed_index)) # 0 = default speed (fastest), #arg0 -> conventional = 1
        else:
            with self.lock: _err(self.andorlib.SetHSSpeed(0, speed_index)) # 0 = default speed (fastest), #arg0 -> conventional = 1

    def set_vs_speed(self, speed_index=0):
        assert 0 <= speed_index < self.numVSSpeeds
        with self.lock: _err( self.andorlib.SetVSSpeed(speed_index))

    def set_preamp_gain(self, gain_i = 0):
        with self.lock: _err( self.andorlib.SetPreAmpGain(gain_i) )
        self.preamp_gain_i = gain_i
        #print 'Preamp gain set to  = ', self.preamp_gain[ind]

    ####### Image Rotate and Flip ###########
    # done in Andor SDK (not on camera)

    def get_image_flip(self):
        hflip, vflip = c_int(-1), c_int(-1)
        with self.lock: _err( self.andorlib.GetImageFlip(byref(hflip), byref(vflip)))
        self.hflip = bool(hflip.value)
        self.vflip = bool(vflip.value)
        return self.hflip, self.vflip

    def set_image_flip(self, hflip=True, vflip=False):
        with self.lock: _err(self.andorlib.SetImageFlip( c_int(bool(hflip)), c_int(bool(vflip))))

    def get_image_hflip(self):
        return self.get_image_flip()[0]

    def set_image_hflip(self, hflipNew):
        hflipOld, vflipOld = self.get_image_flip()
        self.set_image_flip(hflipNew, vflipOld)
        logger.debug( "set_image_hflip: {}".format(hflipNew))

    def get_image_vflip(self):
        return self.get_image_flip()[1]

    def set_image_vflip(self, vflipNew):
        hflipOld, vflipOld = self.get_image_flip()
        self.set_image_flip(hflipOld, vflipNew)
        logger.debug(  "set_image_vflip: {}".format( vflipNew ))

    def set_image_rotate(self, rotate=0):
        # 0 - No rotation
        # 1 - Rotate 90 degrees clockwise
        # 2 - Rotate 90 degrees anti-clockwise
        assert rotate in [0,1,2]
        with self.lock: _err(self.andorlib.SetImageRotation(c_int(rotate)))

    ####### Shutter Control ##########

    def set_shutter_auto(self):
        with self.lock: _err(self.andorlib.SetShutter(0, 0, 0, 0))

    def set_shutter_open(self, open=True):
        if open:
            with self.lock: _err(self.andorlib.SetShutter(0, 1, 0, 0))
        else:
            self.set_shutter_close()

    def set_shutter_close(self):
        with self.lock: _err(self.andorlib.SetShutter(0, 2, 0, 0))

    ####### Temperature Control ###########

    def set_cooler_on(self):
        with self.lock: _err(self.andorlib.CoolerON())
        self.cooler_on = True

    def set_cooler_off(self):
        with self.lock: _err(self.andorlib.CoolerOFF())
        self.cooler_on = False

    def set_cooler(self, coolerOn):
        if coolerOn:
            self.set_cooler_on()
        else:
            self.set_cooler_off()

    def get_cooler(self):
        return self.cooler_on

    def get_temperature_range(self):
        min_t, max_t = c_int(0), c_int(0)
        with self.lock: _err(self.andorlib.GetTemperatureRange( byref(min_t), byref(max_t) ))
        self.min_temp = min_t.value
        self.max_temp = max_t.value
        return self.min_temp, self.max_temp

    def set_temperature(self, new_temp):
        with self.lock: _err( self.andorlib.SetTemperature(c_int(new_temp)))
        self.get_temperature()

    def get_temperature(self):
        lastTemp = c_int(0)
        with self.lock:
            retval = self.andorlib.GetTemperature(byref(lastTemp))
        if retval == consts.DRV_ACQUIRING:
            raise IOError( "Camera busy acquiring" )
        elif retval in (consts.DRV_NOT_INITIALIZED, consts.DRV_ERROR_ACK):
            _err(retval)
        else:
            self.temperature = lastTemp.value
            self.temperature_status_num = retval
            return self.temperature

    temp_status_dict = {
        consts.DRV_TEMP_OFF: 'OFF',
        consts.DRV_TEMP_NOT_STABILIZED: 'NOT_STABILIZED',
        consts.DRV_TEMP_STABILIZED: 'STABILIZED',
        consts.DRV_TEMP_NOT_REACHED: 'NOT_REACHED',
        consts.DRV_TEMP_NOT_SUPPORTED: 'NOT_SUPPORTED',
        consts.DRV_TEMP_DRIFT: 'DRIFT',
        }

    def get_temperature_status(self):
        self.get_temperature()
        return self.temp_status_dict[self.temperature_status_num]

    """
    @property
    def temperature_status_str(self):
        DRV_TEMP_STABILIZED
        DRV_TEMP_NOT_REACHED
        DRV_TEMP_DRIFT
        DRV_TEMP_NOT_STABILIZED

    def is_temperature_stable(self):
        "call get_temperature first"
        if self.temperature_status == consts.
    """

    #### Acquire ####

    # StartAcquisition() --> GetStatus() --> GetAcquiredData()

    def start_acquisition(self):
        with self.lock: _err(self.andorlib.StartAcquisition())

    def abort_acquisition(self):
        with self.lock: _err(self.andorlib.AbortAcquisition())

    _status_name_dict = {
        consts.DRV_IDLE: "IDLE",
        consts.DRV_TEMPCYCLE: "TEMPCYCLE",
        consts.DRV_ACQUIRING: "ACQUIRING",
        consts.DRV_ACCUM_TIME_NOT_MET: "ACCUM_TIME_NOT_MET",
        consts.DRV_KINETIC_TIME_NOT_MET: "KINETIC_TIME_NOT_MET",
        consts.DRV_ERROR_ACK: "ERROR_ACK",
        consts.DRV_ACQ_BUFFER: "ACQ_BUFFER",
        consts.DRV_SPOOLERROR: "SPOOLERROR",
    }
    def get_status(self):
        status = c_int(-1)
        with self.lock: _err(self.andorlib.GetStatus(byref(status)))
        self.status_id   = status.value
        self.status_name = self._status_name_dict[self.status_id]
        return self.status_name

    def get_acquired_data(self):
        #print("buffer size", self.buffer.size)
        with self.lock: _err(self.andorlib.GetAcquiredData(self.buffer.ctypes.data_as(ctypes.POINTER(c_long)), c_uint(self.buffer.size)))
        return self.buffer

    #### Acquisition Timings #####################

    def get_acquisition_timings(self):
        exposure = c_float(-1)
        accum   = c_float(-1)
        kinetic = c_float(-1)

        with self.lock: _err(self.andorlib.GetAcquisitionTimings(byref(exposure), byref(accum), byref(kinetic)))

        self.exposure_time = exposure.value
        self.accumulation_time = accum.value
        self.kinetic_cycle_time = kinetic.value

        return self.exposure_time, self.accumulation_time, self.kinetic_cycle_time

    def set_exposure_time(self, dt):
        with self.lock: _err(self.andorlib.SetExposureTime(c_float(dt)))
        self.get_acquisition_timings()
        if self.debug : logger.debug( 'set exposure to: {}'.format( self.exposure_time))
        return self.exposure_time

    def get_exposure_time(self):
        return self.get_acquisition_timings()[0]

    def set_num_accumulations(self, num):
        with self.lock: _err(self.andorlib.SetNumberAccumulations(num))
        self.num_acc = num

    def get_num_accumulations(self):
        return self.num_acc

    def set_num_kinetics(self, num):
        """This function will set the number of scans (possibly accumulated scans) to be taken
            during a single acquisition sequence. This will only take effect if the acquisition mode is
            Kinetic Series"""
        print('set_num_kinetics', num)
        with self.lock: _err(self.andorlib.SetNumberKinetics(int(num)))
        self.num_kin = num

    def get_num_kinetics(self):
        return self.num_kin

    def set_accumulation_cycle_time(self, acc_time):
        with self.lock: _err(self.andorlib.SetAccumulationCycleTime(c_float(acc_time)))

    def set_kinetic_cycle_time(self, kin_time):
        with self.lock: _err(self.andorlib.SetKineticCycleTime(c_float(kin_time)))

    ###### Electron Multiplication Mode (EM) ########
    def set_EM_advanced(self, state=True):
        with self.lock: _err(self.andorlib.SetEMGainRange(c_int(state)))

    def get_EM_gain_range(self):
        low, high = c_int(-1), c_int(-1)
        with self.lock: _err(self.andorlib.GetEMGainRange(byref(low),byref(high)))
        self.em_gain_range = (low.value, high.value)
        return self.em_gain_range

    def get_EMCCD_gain(self):
        gain = c_int(-1)
        with self.lock: _err(self.andorlib.GetEMCCDGain(byref(gain)))
        self.em_gain = gain.value
        return self.em_gain

    def set_EMCCD_gain(self, gain):
        low,high = self.em_gain_range
        assert low <= gain <= high
        with self.lock: _err(self.andorlib.SetEMCCDGain(c_int(gain)))

    def set_output_amp(self, amp):
        with self.lock: _err(self.andorlib.SetOutputAmplifier(c_int(amp)))
        self.output_amp = amp

    def get_output_amp(self):
        return self.output_amp

    def close(self):
        with self.lock: _err(self.andorlib.ShutDown())

    def get_total_number_images_acquired(self):
        num = c_long(0)
        with self.lock: _err(self.andorlib.GetTotalNumberImagesAcquired(byref(num)))
        return num.value

    def get_number_new_images(self):
        """
        This function will return information on the number of
        new images (i.e. images which have not yet been
        retrieved) in the circular buffer. This information can
         be used with GetImages to retrieve a series of the
        latest images. If any images are overwritten in
         the circular buffer they can no longer be retrieved
         and the information returned will treat overwritten images as having been retrieved.
        """
        first = c_long(0)
        last = c_long(0)
        with self.lock: _err(self.andorlib.GetNumberNewImages(byref(first),byref(last)))
        return first.value, last.value

    def get_number_available_images(self):
        first = c_long(0)
        last = c_long(0)
        with self.lock: _err(self.andorlib.GetNumberAvailableImages(byref(first),byref(last)))
        return first.value, last.value

    def get_images(self,first,last, buf):
        validfirst = c_long(0)
        validlast = c_long(0)

        with self.lock: _err(self.andorlib.GetImages(c_long(first), c_long(last),
                                buf.ctypes.data_as(ctypes.POINTER(c_long)),
                                c_uint(buf.size),
                                byref(validfirst),byref(validlast)))

        return validfirst, validlast, buf

    def get_oldest_image(self, arr=None):
        if arr is None:
            arr = np.zeros((self.Ny_ro, self.Nx_ro), dtype=np.int32)

        arr_ptr = arr.ctypes.data_as(ctypes.POINTER(c_long))
        arr_size = c_uint(arr.size)
        with self.lock:
            retval = self.andorlib.GetOldestImage(arr_ptr, arr_size)
        #print("GetOldestImage", retval)
        if retval == consts.DRV_NO_NEW_DATA: # DRV_NO_NEW_DATA
            #print("no new data")
            return None
        else:
            _err(retval)
        return arr

if __name__ == '__main__':
    import time

    cam = AndorCCD(debug=True)

    cam.set_ro_image_mode()
    cam.set_trigger_mode('internal')
    cam.set_exposure_time(1.0)
    #cam.set_shutter_open()
    cam.andorlib.SetOutputAmplifier(0) # EMCCD

    cam.read_shift_speeds()

    cam.andorlib.SetOutputAmplifier(1) # Conventional

    #cam.set_hs_speed(1)
    cam.andorlib.SetEMGainMode(1)
    print("EM_gain_range", cam.get_EM_gain_range())
    cam.start_acquisition()
    stat = "ACQUIRING",
    while stat != "IDLE":
        time.sleep(0.1)
        stati, stat = cam.get_status()
    cam.get_acquired_data()
    cam.set_shutter_close()

    import pylab as pl
    pl.imshow(cam.buffer, interpolation='nearest', origin='lower')
    pl.show()


text:
'''
(*)~---------------------------------------------------------------------------
Pupil - eye tracking platform
Copyright (C) 2012-2018 Pupil Labs

Distributed under the terms of the GNU
Lesser General Public License (LGPL v3.0).
See COPYING and COPYING.LESSER for license details.
---------------------------------------------------------------------------~(*)
'''
import numpy as np
import OpenGL.GL as gl
import pyglui.cygl.utils as cygl_utils
from pyglui import ui
from pyglui.pyfontstash import fontstash as fs

import gl_utils
from plugin import System_Plugin_Base

COLOR_LEGEND_WORLD = cygl_utils.RGBA(0.66, 0.86, 0.461, 1.)
COLOR_LEGEND_EYE_RIGHT = cygl_utils.RGBA(0.9844, 0.5938, 0.4023, 1.)
COLOR_LEGEND_EYE_LEFT = cygl_utils.RGBA(0.668, 0.6133, 0.9453, 1.)
NUMBER_SAMPLES_TIMELINE = 4000

class System_Timelines(System_Plugin_Base):
    def __init__(self, g_pool, show_world_fps=True, show_eye_fps=True):
        super().__init__(g_pool)
        self.show_world_fps = show_world_fps
        self.show_eye_fps = show_eye_fps
        self.cache_fps_data()

    def init_ui(self):
        self.glfont = fs.Context()
        self.glfont.add_font('opensans', ui.get_opensans_font_path())
        self.glfont.set_font('opensans')
        self.fps_timeline = ui.Timeline('Recorded FPS', self.draw_fps, self.draw_fps_legend)
        self.fps_timeline.content_height *= 2
        self.g_pool.user_timelines.append(self.fps_timeline)

    def deinit_ui(self):
        self.g_pool.user_timelines.remove(self.fps_timeline)
        self.fps_timeline = None

    def cache_fps_data(self):
        fps_world = self.calculate_fps(self.g_pool.timestamps)
        fps_eye0 = self.calculate_fps(self.g_pool.pupil_positions_by_id[0].timestamps)
        fps_eye1 = self.calculate_fps(self.g_pool.pupil_positions_by_id[1].timestamps)

        t0, t1 = self.g_pool.timestamps[0], self.g_pool.timestamps[-1]
        self.cache = {'world': fps_world, 'eye0': fps_eye0, 'eye1': fps_eye1,
                      'xlim': [t0, t1], 'ylim': [0, 210]}

    def calculate_fps(self, timestamps):
        if len(timestamps) > 1:
            fps = 1. / np.diff(timestamps)
            return tuple(zip(timestamps, fps))
        return ()

    def draw_fps(self, width, height, scale):
        with gl_utils.Coord_System(*self.cache['xlim'], *self.cache['ylim']):
            if self.show_world_fps:
                cygl_utils.draw_points(self.cache['world'], size=2*scale, color=COLOR_LEGEND_WORLD)
            if self.show_eye_fps:
                cygl_utils.draw_points(self.cache['eye0'], size=2*scale, color=COLOR_LEGEND_EYE_RIGHT)
                cygl_utils.draw_points(self.cache['eye1'], size=2*scale, color=COLOR_LEGEND_EYE_LEFT)

    def draw_fps_legend(self, width, height, scale):
        self.glfont.push_state()
        self.glfont.set_align_string(v_align='right', h_align='top')
        self.glfont.set_size(15. * scale)
        self.glfont.draw_text(width, 0, self.fps_timeline.label)

        legend_height = 13. * scale
        pad = 10 * scale

        if self.show_world_fps:
            self.glfont.draw_text(width, legend_height, 'world FPS')
            cygl_utils.draw_polyline([(pad, legend_height + pad * 2 / 3),
                                      (width / 2, legend_height + pad * 2 / 3)],
                                     color=COLOR_LEGEND_WORLD, line_type=gl.GL_LINES, thickness=4.*scale)
            legend_height += 1.5 * pad

        if self.show_eye_fps:
            self.glfont.draw_text(width, legend_height, 'eye1 FPS')
            cygl_utils.draw_polyline([(pad, legend_height + pad * 2 / 3),
                                      (width / 2, legend_height + pad * 2 / 3)],
                                     color=COLOR_LEGEND_EYE_LEFT, line_type=gl.GL_LINES, thickness=4.*scale)
            legend_height += 1.5 * pad

            self.glfont.draw_text(width, legend_height, 'eye0 FPS')
            cygl_utils.draw_polyline([(pad, legend_height + pad * 2 / 3),
                                      (width / 2, legend_height + pad * 2 / 3)],
                                     color=COLOR_LEGEND_EYE_RIGHT, line_type=gl.GL_LINES, thickness=4.*scale)

        self.glfont.pop_state()

    def on_notify(self, notification):
        if notification['subject'] == 'pupil_positions_changed':
            self.cache_fps_data()
            self.fps_timeline.refresh()


text:
import unittest

import numpy as np
import tensorflow as tf
from deepspeech_training.util.helpers import ValueRange, get_value_range, pick_value_from_range, tf_pick_value_from_range

class TestValueRange(unittest.TestCase):

    def _ending_tester(self, value, value_type, expected):
        result = get_value_range(value, value_type)
        self.assertEqual(result, expected)

    def test_int_str_scalar(self):
        self._ending_tester('1', int, ValueRange(1, 1, 0))

    def test_int_str_scalar_radius(self):
        self._ending_tester('1~3', int, ValueRange(1, 1, 3))

    def test_int_str_range(self):
        self._ending_tester('1:2', int, ValueRange(1, 2, 0))

    def test_int_str_range_radius(self):
        self._ending_tester('1:2~3', int, ValueRange(1, 2, 3))

    def test_int_scalar(self):
        self._ending_tester(1, int, ValueRange(1, 1, 0))

    def test_int_2tuple(self):
        self._ending_tester((1, 2), int, ValueRange(1, 2, 0))

    def test_int_3tuple(self):
        self._ending_tester((1, 2, 3), int, ValueRange(1, 2, 3))

    def test_float_str_scalar(self):
        self._ending_tester('1.0', float, ValueRange(1.0, 1.0, 0.0))

    def test_float_str_scalar_radius(self):
        self._ending_tester('1.0~3.0', float, ValueRange(1.0, 1.0, 3.0))

    def test_float_str_range(self):
        self._ending_tester('1.0:2.0', float, ValueRange(1.0, 2.0, 0.0))

    def test_float_str_range_radius(self):
        self._ending_tester('1.0:2.0~3.0', float, ValueRange(1.0, 2.0, 3.0))

    def test_float_scalar(self):
        self._ending_tester(1.0, float, ValueRange(1.0, 1.0, 0.0))

    def test_float_2tuple(self):
        self._ending_tester((1.0, 2.0), float, ValueRange(1.0, 2.0, 0.0))

    def test_float_3tuple(self):
        self._ending_tester((1.0, 2.0, 3.0), float, ValueRange(1.0, 2.0, 3.0))

    def test_float_int_3tuple(self):
        self._ending_tester((1, 2, 3), float, ValueRange(1.0, 2.0, 3.0))

class TestPickValueFromFixedRange(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestPickValueFromFixedRange, self).__init__(*args, **kwargs)
        self.session = tf.Session()
        self.clock_ph = tf.placeholder(dtype=tf.float64, name='clock')

    def _ending_tester(self, value_range, clock, expected):
        with tf.Session() as session:
            tf_pick = tf_pick_value_from_range(value_range, clock=self.clock_ph)

            def run_pick(_, c):
                return session.run(tf_pick, feed_dict={self.clock_ph: c})

            is_int = isinstance(value_range.start, int)
            for pick, int_type, float_type in [(pick_value_from_range, int, float), (run_pick, np.int32, np.float32)]:
                result = pick(value_range, clock)
                self.assertEqual(result, expected)
                self.assertTrue(isinstance(result, int_type if is_int else float_type))

    def test_int_0(self):
        self._ending_tester(ValueRange(1, 3, 0), 0.0, 1)

    def test_int_half(self):
        self._ending_tester(ValueRange(1, 3, 0), 0.5, 2)

    def test_int_1(self):
        self._ending_tester(ValueRange(1, 3, 0), 1.0, 3)

    def test_float_0(self):
        self._ending_tester(ValueRange(1.0, 2.0, 0.0), 0.0, 1.0)

    def test_float_half(self):
        self._ending_tester(ValueRange(1.0, 2.0, 0.0), 0.5, 1.5)

    def test_float_1(self):
        self._ending_tester(ValueRange(1.0, 2.0, 0.0), 1.0, 2.0)

class TestPickValueFromRandomizedRange(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestPickValueFromRandomizedRange, self).__init__(*args, **kwargs)
        self.session = tf.Session()
        self.clock_ph = tf.placeholder(dtype=tf.float64, name='clock')

    def _ending_tester(self, value_range, clock_min, clock_max, expected_min, expected_max):
        with self.session as session:
            tf_pick = tf_pick_value_from_range(value_range, clock=self.clock_ph)

            def run_pick(_, c):
                return session.run(tf_pick, feed_dict={self.clock_ph: c})

            is_int = isinstance(value_range.start, int)
            clock_range = np.arange(clock_min, clock_max, (clock_max - clock_min) / 100.0)
            for pick, int_type, float_type in [(pick_value_from_range, int, float), (run_pick, np.int32, np.float32)]:
                results = [pick(value_range, c) for c in clock_range]
                self.assertGreater(len(set(results)), 80)
                self.assertTrue(all(map(lambda x: expected_min <= x <= expected_max, results)))
                self.assertTrue(all(map(lambda x: isinstance(x, int_type if is_int else float_type), results)))

    def test_int_0(self):
        self._ending_tester(ValueRange(10000, 30000, 10000), 0.0, 0.1, 0, 22000)

    def test_int_half(self):
        self._ending_tester(ValueRange(10000, 30000, 10000), 0.4, 0.6, 8000, 32000)

    def test_int_1(self):
        self._ending_tester(ValueRange(10000, 30000, 10000), 0.8, 1.0, 16000, 40000)

    def test_float_0(self):
        self._ending_tester(ValueRange(10000.0, 30000.0, 10000.0), 0.0, 0.1, 0.0, 22000.0)

    def test_float_half(self):
        self._ending_tester(ValueRange(10000.0, 30000.0, 10000.0), 0.4, 0.6, 8000.0, 32000.0)

    def test_float_1(self):
        self._ending_tester(ValueRange(10000.0, 30000.0, 10000.0), 0.8, 1.0, 16000.0, 40000.0)

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

max_stars_repo_name: adimaini/WEAT-WEFAT

max_issues_repo_name: adimaini/WEAT-WEFAT

max_forks_repo_name: adimaini/WEAT-WEFAT


text:
#%%
import logging
import requests
import json
import numpy as np
import os
import pandas as pd
from azure.storage.blob import ContentSettings
from azure.storage.blob import BlockBlobService
from io import StringIO
from adal import AuthenticationContext
from . import fetch_blob as fetching_service
#ce4d996051a1005da9245562212cb070efdba9d2

#python3.6 -m venv funcenv... this creates the funcenv
# source funcenv/bin/activate... this activates virtual environment created above
# func host start after each change
# pip install -r requirements.txt
#ce4d996051a1005da9245562212cb070efdba9d2

#python3.6 -m venv funcenv... this creates the funcenv
# source funcenv/bin/activate... this activates virtual environment created above
# func host start after each change
# pip install -r requirements.txt
#%%
blob_account_name = os.getenv("BlobAccountName")
blob_account_key = os.getenv("BlobAccountKey")
block_blob_service = BlockBlobService(account_name=blob_account_name,
                                      account_key=blob_account_key)
out_blob_final = os.getenv("OutBlobFinal")
#%%
AUTHORITY = 'https://login.microsoftonline.com/gemtudev.onmicrosoft.com'

# Click on this link to get the Swagger API reference
base_url = 'https://gemtu-ws5arp-api.azurewebsites.net'
WORKBENCH_API_URL = 'https://gemtu-ws5arp-api.azurewebsites.net'
#base_url = 'https://gemtu-ws5arp-api.azurewebsites.net'

# This is the application ID of the blockchain workbench web API
# Login to the directory of the tenant -> App registrations -> 'Azure Blockchain Workbench *****-***** ->
# copy the Application ID
RESOURCE = 'a33cc4fb-e3f2-4c23-a005-b46819f58f07'

#Service principal app id & secret/key:
CLIENT_APP_Id = 'c8c2dab5-db8b-4ae2-8210-45b7a335708e'
CLIENT_SECRET = 'Rh95dZrJobHe3fB/GyhxhPyIRtW8DKmThmFl+CfmtI4='
#%%
auth_context = AuthenticationContext(AUTHORITY)
#%%
def clean(req_body):
    dfCreate = fetch_blobs(out_blob_final)
    #create_contract(14, 14, 1, testPayload3b)
    json_array= populate_workbench(dfCreate)
    result = create_json_blob(json_array)
    return 'Success'
#%%
# Read/process CSV into pandas df
def fetch_blobs(out_blob_final):
    # Create container & blob dictionary with helper function
    blob_dict = fetching_service.blob_to_dict(out_blob_final)
    # create DF
    filter_string = "final"
    df = fetching_service.blob_dict_to_df(blob_dict, filter_string)
    logging.info(df.dtypes)
    return df
#%%
def populate_workbench(dfCreate):
    json_array = []
    #logging.warning(dfCreate.head())
    for index, row in dfCreate.iterrows():
        try:
            #logging.warning(dfCreate.iloc[index])
            payload = make_create_payload(dfCreate,index)
            json_array+=[payload]
            #logging.warning(payload)
            #resp = create_contract(workflowId,contractCodeId,connectionId,payload)
            #createdContracts.append(resp.text)
        except:
            print('contract creation failed')
            continue
    #logging.warning(payload)
    return json_array
#%%
def make_create_payload(df,index):
    # This function generates the payload json fed from the pandas df
    #logging.warning(df)
    #need to update this value
    workflowFunctionId = 93
    try:
        #logging.warning('Creating payload...\n')
        payload = {
            "workflowFunctionId": workflowFunctionId,
            "workflowActionParameters": [
                {
                    "name": "po",
                    "value": df['po'][index]
                }, {
                    "name": "itemno",
                    "value": df['itemno'][index]
                }, {
                    "name": "invno",
                    "value": df['invno'][index]
                }, {
                    "name": "signedinvval",
                    "value": df['signedinval'][index]
                }, {
                    "name": "invdate",
                    "value": df['invdate'][index]
                }, {
                    "name": "poformat",
                    "value": df['poformat'][index]
                }, {
                    "name": "popricematch",
                    "value": df['popricematch'][index]
                }, {
                    "name": "poinvpricematch",
                    "value": df['poinvpricematch'][index]
                }, {
                    "name": "initstate",
                    "value": df['initstate'][index]
                }, {
                    "name": "finalpo",
                    "value": df['finalpo'][index]
                }, {
                    "name": "finalresult",
                    "value": df['finalresult'][index]
                }
            ]
        }
        #payload = json.dumps(payload)
        #logging.warning('payload')
        #logging.warning(payload)
        return payload
    except:
        logging.warning('error in payload')

def create_json_blob(json_array):
    #outjson = json_array
    #myarray = np.asarray(json_array).tolist()
    myarray = pd.Series(json_array).to_json(orient='values')
    blob_file_name = "df_to_json.json"
    block_blob_service.create_blob_from_text(out_blob_final, blob_file_name, myarray)
    return 'Success'
#%%
#testPayload3b = json.dumps(testPayload3)
createdContracts = []
logging.warning('Creating contracts...\n')
def create_contract(workflowId, contractCodeId, connectionId, payload):
    if __name__ == '__main__':
        try:
            # Acquiring the token
            token = auth_context.acquire_token_with_client_credentials(
            RESOURCE, CLIENT_APP_Id, CLIENT_SECRET)
            #pprint(str(token))

            url = WORKBENCH_API_URL + '/api/v2/contracts'

            headers = {'Authorization': 'Bearer ' +
                   token['accessToken'], 'Content-Type': 'application/json'}

            params = {'workflowId': workflowId, 'contractCodeId': contractCodeId, 'connectionId': connectionId}

        # Making call to Workbench
            response = requests.post(url=url,data=payload,headers=headers,params=params)

            print('Status code: ' + str(response.status_code), '\n')
            print('Created contractId: ' + str(response.text), '\n', '\n')
            return response
        except Exception as error:
            print(error)
            return error


text:
#!/usr/bin/env python

__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"

''' Uses the support vector machine machinery to our complex networks '''

import os
import sklearn.svm  as sklsvm
import numpy as np
from constants import PERCENTAGE, INPUT_FOLDER, OUTPUT_FOLDER,  NORM, NUM_SETS

def write_accuracies(acc_train, acc_test, output_file, norm_type, set_number, bol_with_outlier):

    ''' Save the results for accuracy in a file '''

    with open(output_file, "a") as f:

        f.write(norm_type[:4] + ", " +  str(set_number) + ', ' + str(bol_with_outlier) + ', '  + \
            str(round(acc_train,3)) + ", " + str(round(acc_test, 3)) + "\n")

def fit_model_linear(data, truth, Chere=10e5):
    ''' Linear Support Vector Classification.
    http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html
    Dual select the algorithm to either solve the dual or primal optimization problem.
    Prefer dual=False when n_samples > n_features. '''

    model = sklsvm.LinearSVC(dual=False, C=Chere)

    model = model.fit(data, truth)

    return model

def fit_model_SVC(data, truth):
    ''' Support Vector Classification. The implementations is a based on libsvm.
    http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html '''

    model = sklsvm.SVC()

    model = model.fit(data, truth)

    return model

def classify_data(model, data, truth):
    """ Returns the accuracy score. Same as the method 'score()'"""

    guesses = model.predict(data)

    right = np.sum(guesses == truth)

    return float(right) / truth.shape[0]

def load_data(datafile_name):
     ''' Load the data and separate it by feature and labels '''

     data = np.loadtxt(datafile_name, delimiter = ',')

     # features
     X = data[:,:-1]

     # label
     Y = data[:,-1]

     return X, Y

def get_input_data(sett, set_number, norm_type,  bol_with_outlier):
    """ Return the name of the file for the parameters """

    if bol_with_outlier:

        return INPUT_FOLDER + 'together' + str(set_number) + "_" + sett + "_" + str(PERCENTAGE) + '_' + \
         norm_type + '_with_outlier' + ".data"

    else:

        return INPUT_FOLDER + 'together' + str(set_number) +  "_" + sett + "_" + str(PERCENTAGE) + '_' + \
        norm_type + ".data"

def main():

    ''' Set input/output variables '''

    folder = OUTPUT_FOLDER + 'log_reg/'

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

    output_file = folder + 'results.out'

    with open(output_file, "w") as f:

        f.write("# SVM RESULTS, TRAIN/TEST FRACTION: " + str(PERCENTAGE)  + "\n")

        f. write("# Normalization,  Set set_number, Outliers?,  Accu. Train, Accu Test\n")

    ''' Loop to each case file:
        - normalization type
        - set order number
        - with outlier or without outlier '''

    classification = []

    for norm_type in NORM:

        for set_number in range(1, NUM_SETS+1):

            ''' with outlier '''

            bol_with_outlier = True

            # get input and output file
            input_train =  get_input_data('train', set_number, norm_type,  bol_with_outlier)

            input_test = get_input_data('test', set_number, norm_type,  bol_with_outlier)

            # get data
            X_train, Y_train = load_data(input_train)

            X_test, Y_test = load_data(input_test)

            # classify linear and save
            model = fit_model_linear(X_train, Y_train)

            acc_train = classify_data(model, X_train, Y_train)

            acc_test = classify_data(model, X_test, Y_test)

            classification.append(str(round(acc_test,3))  +', ' + norm_type + ', ' + str(set_number) + ', ' + \
                str(bol_with_outlier)[0] + '\n')

            write_accuracies(acc_train, acc_test, output_file,  'linear ' + norm_type, set_number, bol_with_outlier)

            # classify linear and save
            model = fit_model_SVC(X_train, Y_train)

            acc_train = classify_data(model, X_train, Y_train)

            acc_test = classify_data(model, X_test, Y_test)

            classification.append(str(round(acc_test,3))  +', ' + norm_type + ', ' + str(set_number) + ', ' + \
                str(bol_with_outlier)[0] + '\n')

            write_accuracies(acc_train, acc_test, output_file,  'SVC ' + norm_type, set_number, bol_with_outlier)

            ''' withOUT outlier '''

            bol_with_outlier = False

            # get input and output file
            input_train =  get_input_data('train', set_number, norm_type,  bol_with_outlier)

            input_test = get_input_data('test', set_number, norm_type,  bol_with_outlier)

            # get data
            X_train, Y_train = load_data(input_train)

            X_test, Y_test = load_data(input_test)

            # classify linear and save
            model = fit_model_linear(X_train, Y_train)

            acc_train = classify_data(model, X_train, Y_train)

            acc_test = classify_data(model, X_test, Y_test)

            classification.append(str(round(acc_test,3))  +', ' + norm_type + ', ' + str(set_number) + ', ' + \
                str(bol_with_outlier)[0] + '\n')

            write_accuracies(acc_train, acc_test, output_file,  'linear ' + norm_type, set_number, bol_with_outlier)

            # classify linear and save
            model = fit_model_SVC(X_train, Y_train)

            acc_train = classify_data(model, X_train, Y_train)

            acc_test = classify_data(model, X_test, Y_test)

            classification.append(str(round(acc_test,3))  +', ' + norm_type + ', ' + str(set_number) + ', ' + \
                str(bol_with_outlier)[0] + '\n')

            write_accuracies(acc_train, acc_test, output_file,  'SVC ' + norm_type, set_number, bol_with_outlier)

    ''' Order the best classifications and save in the end of the file, to help analysis'''
    classification.sort()

    with open(output_file + 'good_feature', "a") as f:

        f.write("\n\n\nClassification\n\n")

        for feat in classification:

            f.write(feat + '\n')

        f.write("\n")

    print 'Results saved at ' +  folder

    print 'Done!!!'

if __name__ == '__main__':
    main()


text:
from .rxdException import RxDException

try:
    import scipy
except:
    raise RxDException('NEURON RxD module requires SciPy')

from . import rxd
from .species import Species
from .region import Region
from .rate import Rate
from .reaction import Reaction
from . import geometry
from .multiCompartmentReaction import MultiCompartmentReaction
from .rxd import re_init, set_solve_type
try:
  from . import dimension3
except:
  pass
from .rangevar import RangeVar
from .geometry import membrane, inside, Shell, FractionalVolume, FixedCrossSection, FixedPerimeter, ScalableBorder, DistributedBoundary
from .plugins import set_solver
# deprecated:
# from geometry import ConstantArea, ConstantVolume
# TODO: if we ever separate Parameter and State from species, then we need to
#       rembember to call rxd._do_nbs_register()
Parameter = Species
State = Species

def _model_view(tree):
    from . import species
    from neuron import h
    species_dict = species._get_all_species()
    if 'TreeViewItem' not in dir(h): return
    if species_dict:
        rxd_head = h.TreeViewItem(None, 'Reaction Diffusion Items')
        # TODO: do the species disappear if they go out of scope? or does this overcount?
        rxd_species = h.TreeViewItem(rxd_head, '%d Species/State/Parameter' % len(species_dict))
        species_children = [h.TreeViewItem(rxd_species, str(name)) for name in species_dict]
        rxd_reactions = h.TreeViewItem(rxd_head, '%d Reaction/Rate/MultiCompartmentReaction' % len([r for r in rxd._all_reactions if r() is not None]))
        tree.append(rxd_head)

max_stars_repo_name: ramcdougal/nrnclean

max_issues_repo_name: ramcdougal/nrnclean

max_forks_repo_name: ramcdougal/nrnclean

reason: import scipy


text:
import numpy as np
from Util.kernelUtil import sampleCurves, doGPR
from Kernels.linearOffsetKernel import LinearWithOffset

#kernel = GPy.kern.LogisticBasisFuncKernel(1, 0) * kLIN
# kernel = kS + kC
# kernel = kPER
kernel = LinearWithOffset(1)
# kernel = kLIN

kernel.plot()
print(kernel)
sampleCurves(kernel)

X = np.linspace(-50, 50, 101)[:, None]

# Y = (1 + np.tanh( -(X - 5) / 10 )) * 5 + np.random.randn(101, 1) #- 100
Y = 20 + 0.5 * X + np.random.randn(101, 1) * 2

doGPR(X, Y, kernel, 10)

max_stars_repo_name: juanluislm/GPy-ABCD

max_issues_repo_name: juanluislm/GPy-ABCD

max_forks_repo_name: juanluislm/GPy-ABCD


text:
# Anthony Krivonos
# src/algorithms/skeleton.py

# Global Imports
import numpy as np
import math

# Local Imports
from utility import *
from enums import *
from mathematics import *

# Local Model Imports
from models.portfolio import *
from models.price import *
from models.quote import *

# Parent Class Import
from algorithms.__algorithm import *

# Abstract: Thoroughly commented file used to describe how to write algorithms on Quantico.

class SkeletonAlgorithm(Algorithm):

    # __init__:Void
    # param query:Query => Query object for API access.
    # param sec_interval:Integer? => Time interval in seconds for event handling.
    # param test:Boolean? => Set to True if backtesting, false otherwise.
    # param cash:Float? => Must set this amount (user's buying power) if backtesting. Otherwise, leave alone.
    def __init__(self, query, portfolio, sec_interval = 900, test = False, cash = 0.00):

        # Name the algorithm something creative
        algorithm_name = "Skeleton"

        # Set algorithm properties
        buy_range = (0.00, 20.00) # Lower and upper bounds for stocks to purchase (in this case, $0 to $20)

        # Set custom properties
        self.initialize_your_own_variables_here = True

        """
            NOTES:
            - Useful methods and properties available anywhere:
              - self.value(): Returns the current value of the user's portfolio.
              - self.price(symbol): Returns the current ask price of the string symbol.
              - self.cash: Stores the user's current cash, updated at each event call.
              - self.on_custom_timer(func, repeat_sec, start_d64, stop_d64): Calls a custom timer using datetime64 objects.
              - self.log(message, type): Logs messages both into the console and in the algorithm object.
              - self.get_logs(last): Get last # (or all, if none) of logs in the algorithm.
              - Algorithm.buy(symbol, quantity, stop, limit): Performs a stop/limit buy.
              - Algorithm.sell(symbol, quantity, stop, limit): Performs a stop/limit sell.
              - Algorithm.cancel(order_id): Cancels the order with the given ID.
              - Algorithm.cancel_open_orders(): Cancels all of the user's open orders.
            - Otherwise, access trading methods via the query property. (See query.py for all methods.)
        """

        # Call super.__init__
        Algorithm.__init__(self, query, portfolio, sec_interval, name = algorithm_name, buy_range = buy_range, test = test, cash = cash)

    # initialize:void
    # NOTE: Configures the algorithm to run indefinitely.
    def initialize(self):
        Algorithm.initialize(self)

        """
            NOTES:
            - This function is automatically called at the end of the parent class's constructor.
            - It will only be called once, so do long-term configurations here.
            - An example would be defining the types of stocks to buy and making a list of symbols.
        """

        pass

    #
    # Event Functions
    #

    # on_market_will_open:Void
    # param cash:Float => User's buying power.
    # param prices:{String:Float}? => Map of symbols to ask prices.
    # NOTE: Called an hour before the market opens.
    def on_market_will_open(self, cash = None, prices = None):
        Algorithm.on_market_will_open(self, cash, prices)

        """
            NOTES:
            - ALWAYS call the super method before running any other code.
            - NEVER access cash and prices. They are only necessary in backtesting.
            - This function is called an hour before markets open.
        """

        pass

    # on_market_open:Void
    # param cash:Float => User's buying power.
    # param prices:{String:Float}? => Map of symbols to ask prices.
    # NOTE: Called exactly when the market opens.
    def on_market_open(self, cash = None, prices = None):
        Algorithm.on_market_open(self, cash, prices)

        """
            NOTES:
            - ALWAYS call the super method before running any other code.
            - NEVER access cash and prices. They are only necessary in backtesting.
            - This function is called precisely when markets open.
        """

        pass

    # while_market_open:Void
    # param cash:Float => User's buying power.
    # param prices:{String:Float}? => Map of symbols to ask prices.
    # NOTE: Called on an interval while market is open.
    def while_market_open(self, cash = None, prices = None):
        Algorithm.while_market_open(self, cash, prices)

        """
            NOTES:
            - ALWAYS call the super method before running any other code.
            - NEVER access cash and prices. They are only necessary in backtesting.
            - This function is called repeatedly, on the provided sec_interval, between market open and close times.
        """

        pass

    # on_market_close:Void
    # param cash:Float => User's buying power.
    # param prices:{String:Float}? => Map of symbols to ask prices.
    # NOTE: Called exactly when the market closes.
    def on_market_close(self, cash = None, prices = None):
        Algorithm.on_market_close(self, cash, prices)

        """
            NOTES:
            - ALWAYS call the super method before running any other code.
            - NEVER access cash and prices. They are only necessary in backtesting.
            - This function is called on market close.
        """

        pass

    #
    # Algorithm
    #

    """
        NOTES:
        - Define all algorithm-specific functions here.
    """

max_stars_repo_name: saaib/Quantico

max_issues_repo_name: saaib/Quantico

max_forks_repo_name: saaib/Quantico


text:
import pandas as pd
#from __init__ import remove_invalid_genes
from flask import jsonify, request
from scipy import stats

def remove_invalid_genes(data):

	data.index = data.index.astype('str')

	return data[~data.index.str.contains('\.')]

def preprocess_file(input_file):
	"""Loads file as pandas DataFrame and removes NA rows
	:param file: filename (str)
	:return modified filename (DataFrame)
	"""

	init = None

	try:
		init = pd.read_csv(input_file, index_col='gene', sep= ",")

	except ValueError as ve:
		if (str(ve) == "Index gene invalid"):

			try:
				init = pd.read_csv(input_file, index_col='gene', sep= "\t")

			except:
				return jsonify(message = "Error: Values in " + input_file + " neither comma- nor tab-separated!"),500

		else:
			return jsonify(message = "Error: Values in " + input_file + " neither comma- nor tab-separated!"),500

	variance = init.var(axis=1)
	median = init.median(axis=1)

	init['var'] = [stats.percentileofscore(variance, a, 'rank') for a in variance]
	init['median'] = [stats.percentileofscore(median, a, 'rank') for a in median]

	# remove columns with dot
	init = remove_invalid_genes(init)

	# drop NA
	init.dropna(inplace=True)

	# remove duplicated indices
	init = init.loc[~init.index.duplicated(keep='first')]

	return init

max_stars_repo_name: Integrative-Transcriptomics/OmicsTIDE

max_issues_repo_name: Integrative-Transcriptomics/OmicsTIDE

max_forks_repo_name: Integrative-Transcriptomics/OmicsTIDE

reason: from scipy


text:
import sys
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

plt.figure()

# 混淆矩阵
confusion_matrix=[[60.,40.],
                  [25.,75.]]
# confusion_matrix=[[26.,24.],
#                   [3.,47.]]
# classes=len(confusion_matrix)
# for i in range(classes):
#     classSum=sum(confusion_matrix[i])
#     for j in range(classes):
#         confusion_matrix[i][j]/=classSum

confusion=pd.DataFrame({'Normal':confusion_matrix[:][0],'Abnormal':confusion_matrix[:][1]})
confusion.index=('Normal','Abnormal')
print(confusion)

cmap=sns.color_palette("Reds",as_cmap=True)
# sns.heatmap(confusion_matrix, annot = True, cmap=cmap,vmin=0,vmax=1)
sns.heatmap(confusion, annot = True, cmap=cmap)

plt.xlabel('Predictions',fontsize = 19)
plt.ylabel('Truth',fontsize = 19)

# plt.title('Histogram of the Dataset', fontsize = 19)
# plt.savefig("figure/HeatMap.png",dpi=200)
plt.savefig("figure/HeatMap.png",dpi=200)


text:
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      lukea_000
#
# Created:     15/11/2013
# Copyright:   (c) lukea_000 2013
0# Licence:     <your licence>
#-------------------------------------------------------------------------------
import cv2
import numpy
#testing open cv stuff with python
def main():
    opencv_haystack =cv2.imread('adam.jpg')
    opencv_needle = cv2.imread('adam_rightnostril.jpg')
    ngrey = cv2.cvtColor(opencv_needle, cv2.COLOR_BGR2GRAY)
    hgrey = cv2.cvtColor(opencv_haystack, cv2.COLOR_BGR2GRAY)
    import pdb
    pdb.set_trace()
    # build feature detector and descriptor extractor
    hessian_threshold = 175
    detector = cv2.SURF(hessian_threshold)
    (hkeypoints, hdescriptors) = detector.detect(hgrey, None, useProvidedKeypoints = False)
    (nkeypoints, ndescriptors) = detector.detect(ngrey, None, useProvidedKeypoints = False)

    # extract vectors of size 64 from raw descriptors numpy arrays
    rowsize = len(hdescriptors) / len(hkeypoints)
    if rowsize > 1:
        hrows = numpy.array(hdescriptors, dtype = numpy.float32).reshape((-1, rowsize))
        nrows = numpy.array(ndescriptors, dtype = numpy.float32).reshape((-1, rowsize))
        print "haystack rows shape", hrows.shape
        print "needle rows shape", nrows.shape
    else:
        print '*****************************************************8888'
        hrows = numpy.array(hdescriptors, dtype = numpy.float32)
        nrows = numpy.array(ndescriptors, dtype = numpy.float32)
        rowsize = len(hrows[0])

    # kNN training - learn mapping from hrow to hkeypoints index
    samples = hrows
    responses = numpy.arange(len(hkeypoints), dtype = numpy.float32)
    print "sample length", len(samples), "response length", len(responses)
    knn = cv2.KNearest()
    knn.train(samples,responses)

    # retrieve index and value through enumeration
    for i, descriptor in enumerate(nrows):
        descriptor = numpy.array(descriptor, dtype = numpy.float32).reshape((1, rowsize))
        print i, 'descriptor shape', descriptor.shape, 'sample shape', samples[0].shape
        retval, results, neigh_resp, dists = knn.find_nearest(descriptor, 1)
        res, dist =  int(results[0][0]), dists[0][0]
        print 'result', res, 'distance', dist

        if dist < 0.1:
            # draw matched keypoints in red color
            color = (0, 0, 255)
        else:
            # draw unmatched in blue color
            color = (255, 0, 0)
        # draw matched key points on haystack image
        x,y = hkeypoints[res].pt
        center = (int(x),int(y))
        cv2.circle(opencv_haystack,center,2,color,-1)
        # draw matched key points on needle image
        x,y = nkeypoints[i].pt
        center = (int(x),int(y))
        cv2.circle(opencv_needle,center,2,color,-1)

    cv2.imshow('haystack',opencv_haystack)
    cv2.imshow('needle',opencv_needle)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

max_stars_repo_name: AjayAgAI/optimeyes

max_issues_repo_name: AjayAgAI/optimeyes

max_forks_repo_name: AjayAgAI/optimeyes


text:
# %%
# based on: https://towardsdatascience.com/five-confidence-intervals-for-proportions-that-you-should-know-about-7ff5484c024f
import numpy as np
from scipy.stats import norm

def calculate_wald_type_CI(p, n, confidence_level = 0.95):
    standard_dev = np.sqrt(p * (1-p) / n)
    z = norm.ppf(1 - (1 - confidence_level) / 2)  #returns the value of thresholds at which conf.level has to be cut at. for 95% CI, this is -1.96 and +1.96
    ci = [p - z * standard_dev, p + z * standard_dev]
    return ci

# %%

if __name__ == "__main__":
    print(calculate_wald_type_CI(0.81, 5000))
# %%

max_stars_repo_name: IDSC-io/vre-tgn

max_issues_repo_name: IDSC-io/vre-tgn

max_forks_repo_name: IDSC-io/vre-tgn


text:
from symengine import (Integer, symbols, sin, cos, pi, E, I,
    function_symbol, DenseMatrix, sympify)

def test_sage_conversions():
    try:
        import sage.all as sage
    except ImportError:
        return

    x, y = sage.SR.var('x y')
    x1, y1 = symbols('x, y')

    # Symbol
    assert x1._sage_() == x
    assert x1 == sympify(x)

    # Integer
    assert Integer(12)._sage_() == sage.Integer(12)
    assert Integer(12) == sympify(sage.Integer(12))

    # Rational
    assert (Integer(1) / 2)._sage_() == sage.Integer(1) / 2
    assert Integer(1) / 2 == sympify(sage.Integer(1) / 2)

    # Operators
    assert x1 + y == x1 + y1
    assert x1 * y == x1 * y1
    assert x1 ** y == x1 ** y1
    assert x1 - y == x1 - y1
    assert x1 / y == x1 / y1

    assert x + y1 == x + y
    assert x * y1 == x * y
    # Doesn't work in Sage 6.1.1ubuntu2
    # assert x ** y1 == x ** y
    assert x - y1 == x - y
    assert x / y1 == x / y

    # Conversions
    assert (x1 + y1)._sage_() == x + y
    assert (x1 * y1)._sage_() == x * y
    assert (x1 ** y1)._sage_() == x ** y
    assert (x1 - y1)._sage_() == x - y
    assert (x1 / y1)._sage_() == x / y

    assert x1 + y1 == sympify(x + y)
    assert x1 * y1 == sympify(x * y)
    assert x1 ** y1 == sympify(x ** y)
    assert x1 - y1 == sympify(x - y)
    assert x1 / y1 == sympify(x / y)

    # Functions
    assert sin(x1) == sin(x)
    assert sin(x1)._sage_() == sage.sin(x)
    assert sin(x1) == sympify(sage.sin(x))

    assert cos(x1) == cos(x)
    assert cos(x1)._sage_() == sage.cos(x)
    assert cos(x1) == sympify(sage.cos(x))

    assert function_symbol('f', x1, y1)._sage_() == sage.function('f', x, y)
    assert function_symbol('f', 2 * x1, x1 + y1).diff(x1)._sage_() == sage.function('f', 2 * x, x + y).diff(x)

    # For the following test, sage needs to be modified
    # assert sage.sin(x) == sage.sin(x1)

    # Constants
    assert pi._sage_() == sage.pi
    assert E._sage_() == sage.e
    assert I._sage_() == sage.I

    assert pi == sympify(sage.pi)
    assert E == sympify(sage.e)

    # SympyConverter does not support converting the following
    # assert I == sympify(sage.I)

    # Matrix
    assert DenseMatrix(1, 2, [x1, y1])._sage_() == sage.matrix([[x, y]])

    # SympyConverter does not support converting the following
    # assert DenseMatrix(1, 2, [x1, y1]) == sympify(sage.matrix([[x, y]]))

# This string contains Sage doctests, that execute all the functions above.
# When you add a new function, add it here as well.
"""
TESTS::
    sage: test_sage_conversions()
"""

max_stars_repo_name: pbrady/symengine

max_issues_repo_name: pbrady/symengine

max_forks_repo_name: pbrady/symengine

reason: import sage