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

from __future__ import division
from __future__ import print_function

import io
import os
import sys
import math
import argparse
import numpy as np

work_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(work_dir, "../"))

from hapi.metrics import Metric
from hapi.model import Model, Input, Loss, set_device
from hapi.text.text import SequenceTagging

from utils.check import check_gpu, check_version
from utils.configure import PDConfig
from reader import LacDataset, LacDataLoader

import paddle.fluid as fluid
from paddle.fluid.optimizer import AdamOptimizer

__all__ = ["SeqTagging", "LacLoss", "ChunkEval"]

class SeqTagging(Model):
    def __init__(self, args, vocab_size, num_labels, length=None,
                 mode="train"):
        super(SeqTagging, self).__init__()
        """
        define the lexical analysis network structure
        word: stores the input of the model
        for_infer: a boolean value, indicating if the model to be created is for training or predicting.

        return:
            for infer: return the prediction
            otherwise: return the prediction
        """
        self.mode_type = mode
        self.word_emb_dim = args.word_emb_dim
        self.vocab_size = vocab_size
        self.num_labels = num_labels
        self.grnn_hidden_dim = args.grnn_hidden_dim
        self.emb_lr = args.emb_learning_rate if 'emb_learning_rate' in dir(
            args) else 1.0
        self.crf_lr = args.emb_learning_rate if 'crf_learning_rate' in dir(
            args) else 1.0
        self.bigru_num = args.bigru_num
        self.batch_size = args.batch_size
        self.init_bound = 0.1
        self.length = length

        self.sequence_tagging = SequenceTagging(
            vocab_size=self.vocab_size,
            num_labels=self.num_labels,
            batch_size=self.batch_size,
            word_emb_dim=self.word_emb_dim,
            grnn_hidden_dim=self.grnn_hidden_dim,
            emb_learning_rate=self.emb_lr,
            crf_learning_rate=self.crf_lr,
            bigru_num=self.bigru_num,
            init_bound=self.init_bound,
            length=self.length)

    def forward(self, *inputs):
        """
        Configure the network
        """
        word = inputs[0]
        lengths = inputs[1]
        if self.mode_type == "train" or self.mode_type == "test":
            target = inputs[2]
            outputs = self.sequence_tagging(word, lengths, target)
        else:
            outputs = self.sequence_tagging(word, lengths)
        return outputs

class Chunk_eval(fluid.dygraph.Layer):
    def __init__(self,
                 num_chunk_types,
                 chunk_scheme,
                 excluded_chunk_types=None):
        super(Chunk_eval, self).__init__()
        self.num_chunk_types = num_chunk_types
        self.chunk_scheme = chunk_scheme
        self.excluded_chunk_types = excluded_chunk_types

    def forward(self, input, label, seq_length=None):
        precision = self._helper.create_variable_for_type_inference(
            dtype="float32")
        recall = self._helper.create_variable_for_type_inference(
            dtype="float32")
        f1_score = self._helper.create_variable_for_type_inference(
            dtype="float32")
        num_infer_chunks = self._helper.create_variable_for_type_inference(
            dtype="int64")
        num_label_chunks = self._helper.create_variable_for_type_inference(
            dtype="int64")
        num_correct_chunks = self._helper.create_variable_for_type_inference(
            dtype="int64")
        this_input = {"Inference": input, "Label": label}
        if seq_length is not None:
            this_input["SeqLength"] = seq_length
        self._helper.append_op(
            type='chunk_eval',
            inputs=this_input,
            outputs={
                "Precision": [precision],
                "Recall": [recall],
                "F1-Score": [f1_score],
                "NumInferChunks": [num_infer_chunks],
                "NumLabelChunks": [num_label_chunks],
                "NumCorrectChunks": [num_correct_chunks]
            },
            attrs={
                "num_chunk_types": self.num_chunk_types,
                "chunk_scheme": self.chunk_scheme,
                "excluded_chunk_types": self.excluded_chunk_types or []
            })
        return (num_infer_chunks, num_label_chunks, num_correct_chunks)

class LacLoss(Loss):
    def __init__(self):
        super(LacLoss, self).__init__()
        pass

    def forward(self, outputs, labels):
        avg_cost = outputs[1]
        return avg_cost

class ChunkEval(Metric):
    def __init__(self, num_labels, name=None, *args, **kwargs):
        super(ChunkEval, self).__init__(*args, **kwargs)
        self._init_name(name)
        self.chunk_eval = Chunk_eval(
            int(math.ceil((num_labels - 1) / 2.0)), "IOB")
        self.reset()

    def add_metric_op(self, *args):
        crf_decode = args[0]
        lengths = args[2]
        label = args[3]
        (num_infer_chunks, num_label_chunks,
         num_correct_chunks) = self.chunk_eval(
             input=crf_decode, label=label, seq_length=lengths)
        return [num_infer_chunks, num_label_chunks, num_correct_chunks]

    def update(self, num_infer_chunks, num_label_chunks, num_correct_chunks,
               *args, **kwargs):
        self.infer_chunks_total += num_infer_chunks
        self.label_chunks_total += num_label_chunks
        self.correct_chunks_total += num_correct_chunks
        precision = float(
            num_correct_chunks) / num_infer_chunks if num_infer_chunks else 0
        recall = float(
            num_correct_chunks) / num_label_chunks if num_label_chunks else 0
        f1_score = float(2 * precision * recall) / (
            precision + recall) if num_correct_chunks else 0
        return [precision, recall, f1_score]

    def reset(self):
        self.infer_chunks_total = 0
        self.label_chunks_total = 0
        self.correct_chunks_total = 0

    def accumulate(self):
        precision = float(
            self.correct_chunks_total
        ) / self.infer_chunks_total if self.infer_chunks_total else 0
        recall = float(
            self.correct_chunks_total
        ) / self.label_chunks_total if self.label_chunks_total else 0
        f1_score = float(2 * precision * recall) / (
            precision + recall) if self.correct_chunks_total else 0
        res = [precision, recall, f1_score]
        return res

    def _init_name(self, name):
        name = name or 'chunk eval'
        self._name = ['precision', 'recall', 'F1']

    def name(self):
        return self._name

def main(args):
    place = set_device(args.device)
    fluid.enable_dygraph(place) if args.dynamic else None

    inputs = [
        Input(
            [None, None], 'int64', name='words'), Input(
                [None], 'int64', name='length'), Input(
                    [None, None], 'int64', name='target')
    ]

    labels = [Input([None, None], 'int64', name='labels')]

    feed_list = None if args.dynamic else [
        x.forward() for x in inputs + labels
    ]

    dataset = LacDataset(args)
    train_dataset = LacDataLoader(args, place, phase="train")

    vocab_size = dataset.vocab_size
    num_labels = dataset.num_labels
    model = SeqTagging(args, vocab_size, num_labels, mode="train")

    optim = AdamOptimizer(
        learning_rate=args.base_learning_rate,
        parameter_list=model.parameters())

    model.prepare(
        optim,
        LacLoss(),
        ChunkEval(num_labels),
        inputs=inputs,
        labels=labels,
        device=args.device)

    if args.init_from_checkpoint:
        model.load(args.init_from_checkpoint)

    if args.init_from_pretrain_model:
        model.load(args.init_from_pretrain_model, reset_optimizer=True)

    model.fit(train_dataset.dataloader,
              epochs=args.epoch,
              batch_size=args.batch_size,
              eval_freq=args.eval_freq,
              save_freq=args.save_freq,
              save_dir=args.save_dir)

if __name__ == '__main__':
    args = PDConfig(yaml_file="sequence_tagging.yaml")
    args.build()
    args.Print()

    use_gpu = True if args.device == "gpu" else False
    check_gpu(use_gpu)
    check_version()

    main(args)


text:
from makeDome import makeCircle
from shapely.geometry import Point, box
import elevation2stl
import MapMaker3d

printSize = 80;
floor = 0;
base_height = 1;
dot_r = 0.75;

def workflow2(fname):
	streets = [	{'label':'Butler', 	'x':37, 	'y':65,	'angle':-60},
				{'label':'42nd',	'x':76,		'y':23,	'angle':45},
				{'label':'44th',	'x':20,		'y':17,	'angle':45},
				{'label':'45th',	'x':53,		'y':75,	'angle':45},
				{'label':'46th',	'x':14,		'y':63,	'angle':45}]
	streetLabels(streets,fname)

def workflow(fname):
	streets = [	{'label':'Butler', 'x': 49, 'y': 22, 'angle':60},
				{'label':'42nd','x':4,'y':34,'angle':-45},
				{'label':'44th','x':60,'y':27,'angle':-45},
				{'label':'45th','x':30,'y':86,'angle':-45},
				{'label':'46th','x':65,'y':75,'angle':-45}]
	streetLabels(streets,fname)

def streetLabels(streets,fname):
	elevation2stl.startSTL(fname)
	label_cnt = 0;
	frames = []
	for street in streets:
		name = street['label'];
		pos = (street['x'],street['y'])
		angle = street['angle']
		print 'adding: ' + name
		label_cnt = label_cnt + 1
		frame = buildBasePolyFile(name,pos,fname,3,angle,label_cnt)
		frames.append(frame)
		#stl(name,fname,3,pos,angle)
		string2braille(name,fname,pos,3,angle)
	buildFramePolyFile(fname,frames)
	stl(name,fname,3,pos,angle,label_cnt)
	elevation2stl.endSTL

def total(string,fname,coarseness):
	elevation2stl.startSTL(fname)
	#buildBasePolyFile(string,(1,1),fname,coarseness);
	angle = 45;
	stl(string,fname,coarseness,angle)
	string2braille(string,fname,(20,20),coarseness,angle)
	elevation2stl.endSTL(fname)

def stl(string,fname,coarseness,pos,angle,label_cnt):
	global printSize
	import triangle
	import triangle.plot
	import matplotlib.pyplot as plt
	import numpy as np
	from shapely.geometry import box, LineString
	#buildBasePolyFile(string,pos,fname,coarseness,angle)

	print '/Users/bttaylor/Documents/TactileMap/PolyFiles/b_' + fname + '_frame'
	frames = triangle.get_data('/Users/bttaylor/Documents/TactileMap/PolyFiles/b_' + fname + '_frame');
	framesT = triangle.triangulate(frames,'p');
	for tri in framesT['triangles']:
		pt1 = Point(framesT['vertices'][tri[0]][0],framesT['vertices'][tri[0]][1])
		pt2 = Point(framesT['vertices'][tri[1]][0],framesT['vertices'][tri[1]][1])
		pt3 = Point(framesT['vertices'][tri[2]][0],framesT['vertices'][tri[2]][1])
		tri_v1 = [pt1.x,pt1.y,base_height]
		tri_v2 = [pt2.x,pt2.y,base_height]
		tri_v3 = [pt3.x,pt3.y,base_height]
		elevation2stl.printTriangle(tri_v1,tri_v2,tri_v3,fname)

	for i in range(1,label_cnt + 1):
		print '/Users/bttaylor/Documents/TactileMap/PolyFiles/b_' + fname + '_' + str(i)
		nondot = triangle.get_data('/Users/bttaylor/Documents/TactileMap/PolyFiles/b_' + fname + '_' + str(i));
		non = triangle.triangulate(nondot,'p');
		for tri in non['triangles']:
			pt1 = Point(non['vertices'][tri[0]][0],non['vertices'][tri[0]][1])
			pt2 = Point(non['vertices'][tri[1]][0],non['vertices'][tri[1]][1])
			pt3 = Point(non['vertices'][tri[2]][0],non['vertices'][tri[2]][1])
			tri_v1 = [pt1.x,pt1.y,base_height]
			tri_v2 = [pt2.x,pt2.y,base_height]
			tri_v3 = [pt3.x,pt3.y,base_height]
			elevation2stl.printTriangle(tri_v1,tri_v2,tri_v3,fname)

	# nondot = triangle.get_data('/Users/bttaylor/Documents/TactileMap/PolyFiles/b_' + fname);
	# #print nondot
	# edges = box(0,0,printSize,printSize);
	# edge_pts = []

	# non = triangle.triangulate(nondot,'p');
	# for tri in non['triangles']:
	# 	pt1 = Point(non['vertices'][tri[0]][0],non['vertices'][tri[0]][1])
	# 	pt2 = Point(non['vertices'][tri[1]][0],non['vertices'][tri[1]][1])
	# 	pt3 = Point(non['vertices'][tri[2]][0],non['vertices'][tri[2]][1])
	# 	tri_v1 = [pt1.x,pt1.y,base_height]
	# 	tri_v2 = [pt2.x,pt2.y,base_height]
	# 	tri_v3 = [pt3.x,pt3.y,base_height]
	# 	elevation2stl.printTriangle(tri_v1,tri_v2,tri_v3,fname)
	# for seg in non['segments']:
	# 	line = LineString(non['vertices'][seg])
	# 	if line.touches(edges):
	# 		XYpt1 = Point(non['vertices'][seg[0]])
	# 		XYpt2 = Point(non['vertices'][seg[1]])
	# 		edge_pts.append(XYpt1.coords[0]);
	# 		edge_pts.append(XYpt2.coords[0]);
	# #print edge_pts
	# edge_pts.append([0,0])
	# edge_pts.append([0,printSize])
	# edge_pts.append([printSize,0])
	# edge_pts.append([printSize,printSize])
	# edge_pt_array = np.array(edge_pts)
	# base = dict(vertices=edge_pt_array);
	# baseT = triangle.triangulate(base);
	# #triangle.plot.compare(plt,base,baseT);

	# for tri in baseT['triangles']:
	# 	pt1 = Point(baseT['vertices'][tri[0]][0],baseT['vertices'][tri[0]][1])
	# 	pt2 = Point(baseT['vertices'][tri[1]][0],baseT['vertices'][tri[1]][1])
	# 	pt3 = Point(baseT['vertices'][tri[2]][0],baseT['vertices'][tri[2]][1])
	# 	tri_v1 = [pt1.x,pt1.y,floor]
	# 	tri_v2 = [pt2.x,pt2.y,floor]
	# 	tri_v3 = [pt3.x,pt3.y,floor]

	# 	elevation2stl.printTriangle(tri_v1,tri_v2,tri_v3,fname)

	#print edges
	pt1 = [0, 0, floor]
	pt2 = [0, 0, base_height]
	pt3 = [0, printSize, floor]
	pt4 = [0, printSize, base_height]
	elevation2stl.printTriangle(pt1,pt2,pt4,fname)
	elevation2stl.printTriangle(pt1,pt3,pt4,fname)
	pt3 = [printSize, 0, floor]
	pt4 = [printSize, 0, base_height]
	elevation2stl.printTriangle(pt1,pt2,pt4,fname)
	elevation2stl.printTriangle(pt1,pt3,pt4,fname)
	pt1 = [printSize, printSize, floor]
	pt2 = [printSize, printSize, base_height]
	elevation2stl.printTriangle(pt1,pt2,pt4,fname)
	elevation2stl.printTriangle(pt1,pt3,pt4,fname)
	pt3 = [0, printSize, floor]
	pt4 = [0, printSize, base_height]
	elevation2stl.printTriangle(pt1,pt2,pt4,fname)
	elevation2stl.printTriangle(pt1,pt3,pt4,fname)
	#print bottom
	pt1 = [0, 0, floor]
	pt2 = [0, printSize, floor]
	pt3 = [printSize, printSize, floor]
	pt4 = [printSize, 0, floor]
	elevation2stl.printTriangle(pt1,pt3,pt2,fname)
	elevation2stl.printTriangle(pt1,pt4,pt3,fname)

	#plt.show()

def buildFramePolyFile(fname,frames):
	f = open(MapMaker3d.directory +  'PolyFiles/b_' + fname + '_frame.poly','w')
	f.write("# Braille " + fname + "_frame.poly\n")
	vertices = 4 + 4*len(frames)
	f.write(str(vertices) + ' 2 0 1\n')

	f.write("#the exteriors of the frame\n")
	f.write('1 0 0 2\n')
	f.write('2 0 ' + str(printSize) + ' 2\n')
	f.write('3 ' + str(printSize) + ' ' + str(printSize) + ' 2\n')
	f.write('4 ' + str(printSize) + ' 0 2\n')
	f.write("#the end of the exteriors\n")

	f_cnt = 1;
	v_cnt = 5;
	boundary = 3;
	f.write("#the interior sections\n")
	for frame in frames:
		f.write("#Hole " + str(f_cnt) + '\n')
		f.write(str(v_cnt) + ' ' + str(frame.bounds[0]) + ' ' +str(frame.bounds[1]) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(frame.bounds[0]) + ' ' +str(frame.bounds[3]) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(frame.bounds[2]) + ' ' +str(frame.bounds[3]) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(frame.bounds[2]) + ' ' +str(frame.bounds[1]) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		boundary = boundary + 1
		f_cnt = f_cnt + 1

	f.write("#the segments\n")
	f.write(str(vertices) + ' 1\n')
	f.write("#exterior segments\n")
	f.write("1 1 2 2\n")
	f.write("2 2 3 2\n")
	f.write("3 3 4 2\n")
	f.write("4 4 1 2\n")

	f_cnt = 1;
	v_cnt = 5;
	boundary = 3;
	f.write("#the interior segments\n")
	for frame in frames:
		f.write("#Interior Segment " + str(f_cnt) + '\n')
		f.write(str(v_cnt) + ' ' + str(v_cnt) + ' ' +str(v_cnt + 1) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(v_cnt) + ' ' +str(v_cnt + 1) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(v_cnt) + ' ' +str(v_cnt + 1) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		f.write(str(v_cnt) + ' ' + str(v_cnt) + ' ' +str(v_cnt - 3) + ' ' + str(boundary) + '\n')
		v_cnt = v_cnt + 1
		boundary = boundary + 1
		f_cnt = f_cnt + 1
	f.write("#Holes\n")
	f.write(str(len(frames)) + '\n')
	f_cnt = 1
	for frame in frames:
		f.write(str(f_cnt) + ' ' + str((frame.bounds[0] + frame.bounds[2])/2) + ' ' + str((frame.bounds[1] + frame.bounds[3])/2) + '\n')
		f_cnt = f_cnt + 1

def buildBasePolyFile(string,pos,fname,coarseness,angle,cnt):
	global printSize

	symbol = getSymbolicString(string,pos,coarseness,angle);
	#symbol is a collection of points making up the outermost part of each braille dot
	vertices = len(symbol)*4*coarseness + 4
	#coarseness indicates the number of points used per quandrant (4*coarseness)
	#There are the 4 corner verticies  ( + 4 )
	print 'characters: ',len(string)
	width = (len(string)-1)*6.5 + 3.9
	#this isn't correct if there is a number. that adds another character
	height = 14.8
	if (pos[1] - 7.8) < 0:
		frame = box(pos[0],pos[1],pos[0]+width,pos[1]+6.3)
	else:
		frame = box(pos[0],pos[1]-7.8,pos[0]+width,pos[1]+6.3)

	#f = open(dir + 'PolyFiles/b_' + filename + '.poly','w')
	f = open(MapMaker3d.directory +  'PolyFiles/b_' + fname + '_' + str(cnt) + '.poly','w')
	f.write("# Braille " + fname + ".poly\n")
	f.write(str(vertices) + ' 2 0 1\n')

	f.write("#the exteriors of the frame\n")
	#f.write('1 0 0 2\n')
	#f.write('2 0 ' + str(printSize) + ' 2\n')
	#f.write('3 ' + str(printSize) + ' ' + str(printSize) + ' 2\n')
	#f.write('4 ' + str(printSize) + ' 0 2\n')
	f.write('1 ' + str(pos[0]) + ' ' + str(frame.bounds[1]) + ' 2\n')
	#f.write('1 ' + str(pos[0]) + ' ' + str(pos[1] - 8.5) + ' 2\n')
	f.write('2 ' + str(pos[0]) + ' ' + str(pos[1] + 6.3) + ' 2\n')
	f.write('3 ' + str(pos[0] + width) + ' ' + str(pos[1] + 6.3) + ' 2\n')
	f.write('4 ' + str(pos[0] + width) + ' ' + str(frame.bounds[1]) + ' 2\n')
	#f.write('4 ' + str(pos[0] + width) + ' ' + str(pos[1] - 8.5) + ' 2\n')
	f.write("#the end of the exteriors\n")

	v_cnt = 5;
	boundary = 3;
	dot_cnt = 1;

	hole_centers = []
	for dot in symbol:
		f.write("#Hole " + str(dot_cnt) + "\n")
		dot_cnt = dot_cnt + 1
		hole_x = 0;
		hole_y = 0;
		for point in dot:
			hole_x = hole_x + point.x;
			hole_y = hole_y + point.y;
			f.write(str(v_cnt) + ' ' + str(point.x) + ' ' + str(point.y) + ' ' + str(boundary) + '\n')
			v_cnt = v_cnt + 1
		hole_x = hole_x / len(dot)
		hole_y = hole_y / len(dot)
		hole_centers.append((hole_x,hole_y));
		boundary = boundary + 1
	f.write("#the end of holes\n")
	f.write("#the segments\n")
	f.write(str(vertices) + ' 1\n')
	f.write("#exterior segments\n")

	f.write('1 1 2 2\n')
	f.write('2 2 3 2\n')
	f.write('3 3 4 2\n')
	f.write('4 4 1 2\n')

	boundary = 3;
	f.write("#interior segments\n")
	cnt = 5;
	for dot in symbol:
		f.write('#interior segment ' + str(boundary - 2) + '\n')
		for i in range(0,len(dot)):
			if(i < len(dot) - 1):
				f.write(str(i + cnt) + ' ' + str(i + cnt) + ' ' + str(i+ cnt + 1) + ' ' + str(boundary) + '\n')
			else:
				f.write(str(i + cnt) + ' ' + str(i + cnt) + ' ' + str(cnt) + ' ' + str(boundary) + '\n')
		cnt = cnt + len(dot)
		boundary = boundary + 1

	f.write("# X holes\n")
	f.write(str(len(hole_centers)) + '\n')
	cnt = 1
	for hole in hole_centers:
		f.write(str(cnt) + ' ' + str(hole[0]) + ' ' + str(hole[1]) + '\n')
		cnt = cnt + 1
	return frame

def string2braille(string,fname,pos,coarseness,angle):
	#elevation2stl.startSTL(fname)
	#pos = (0,0)
	prev_num = False;
	for char in string:
		if ord(char) >= 48 and ord(char) <= 57:
			#is a number
			if not prev_num:
				pos = char2braille('#',pos,fname,coarseness,angle)

			if ord(char) == 48:
				#'0' -> 'j'
				char = 'j'
			else:
				char = chr(ord(char) + 48)
			pos = char2braille(char,pos,fname,coarseness,angle)
			prev_num = True;
		else:
			pos = char2braille(char,pos,fname,coarseness,angle)
			prev_num = False
	#elevation2stl.endSTL(fname)

def getSymbolicString(string,pos,coarseness,angle):
	import math
	import numpy as np
	rad = math.radians(angle);
	rotation_matrix = np.array([[math.cos(rad), math.sin(-rad)],[math.sin(rad), math.cos(rad)]]);
	symbols = [];
	prev_num = False;
	for char in string:
		#symbols = symbols + getSymbolicChar(char,pos,coarseness,angle)
		if ord(char) >= 48 and ord(char) <= 57:
			if not prev_num:
				symbols = symbols + getSymbolicChar('#',pos,coarseness,angle)
				offset = np.array([6.5,0])
				offset = np.dot(rotation_matrix,offset);
				pos = (pos[0] + offset[0], pos[1] + offset[1])

			if ord(char) == 48:
				#'0' -> 'j'
				char = 'j'
			else:
				char = chr(ord(char) + 48)

			symbols = symbols + getSymbolicChar(char,pos,coarseness,angle)
			prev_num = True
			offset = np.array([6.5,0])
		else:
			symbols = symbols + getSymbolicChar(char,pos,coarseness,angle)
			prev_num = False
			offset = np.array([6.5, 0]);
		offset = np.dot(rotation_matrix,offset);
		pos = (pos[0] + offset[0], pos[1] + offset[1])
	return symbols

def getSymbolicChar(char,pos,coarseness,angle):
	global dot_r
	import blar
	import math
	import numpy as np
	rad = math.radians(angle);
	rotation_matrix = np.array([[math.cos(rad), math.sin(-rad)],[math.sin(rad), math.cos(rad)]]);
	symbol = [];
	pattern = getBraillePattern(char);
	if(pattern[0]):
		center = np.array([dot_r, dot_r + 4.8]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + dot_r * math.cos(rad),pos[1] + 4.8 * math.sin(rad)),coarseness);
		symbol.append(outer);
	if(pattern[1]):
		center = np.array([dot_r, dot_r + 2.4]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + 0,pos[1] + 2.4),coarseness);
		symbol.append(outer);
	if(pattern[2]):
		center = np.array([dot_r, dot_r]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + 0,pos[1] + 0),coarseness);
		symbol.append(outer);
	if(pattern[3]):
		center = np.array([dot_r + 2.4, dot_r + 4.8]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + 2.4,pos[1] + 4.8),coarseness);
		symbol.append(outer);
	if(pattern[4]):
		center = np.array([dot_r + 2.4, dot_r + 2.4]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + 2.4,pos[1] + 2.4),coarseness);
		symbol.append(outer);
	if(pattern[5]):
		center = np.array([dot_r + 2.4, dot_r]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + 2.4,pos[1] + 0),coarseness);
		symbol.append(outer);

	if ord(char) >= 48 and ord(char) <= 57:
		#numbers are represented with an additional symbol

		sym2 = []
		for dot in symbol:
			dot2 = [];
			for point in dot:
				offset = np.array([6.5, 0]);
				offset = np.dot(rotation_matrix,offset);
				dot2.append(Point(point.x + offset[0],point.y + offset[1],point.z))
			sym2.append(dot2)
		symbol = getSymbolicChar('#',pos,coarseness,angle) + sym2

	return symbol

def char2braille(char,pos,fname,coarseness,angle):

	import math
	import numpy as np
	import blar
	rad = math.radians(angle);
	rotation_matrix = np.array([[math.cos(rad), math.sin(-rad)],[math.sin(rad), math.cos(rad)]]);

	if ord(char) >= 48 and ord(char) <= 57:
		#numbers get additional symbol
		pos = char2braille('#',pos,fname,coarseness,angle)
	pattern = getBraillePattern(char);
	if(pattern[0]):
		center = np.array([dot_r, dot_r + 4.8]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		# makeDome.makeDome(.3,Point(pos[0] + 0,pos[1] + 4.8),coarseness);
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 0,pos[1] + 4.8),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	if(pattern[1]):
		center = np.array([dot_r, dot_r + 2.4]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		# makeDome.makeDome(.3,Point(pos[0] + 0,pol[1] + 2.4),coarseness);
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 0,pos[1] + 2.4),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	if(pattern[2]):
		center = np.array([dot_r, dot_r ]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		# makeDome.makeDome(.3,Point(pos[0] + 0,pos[1] + 0),coarseness);
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 0,pos[1] + 0),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	if(pattern[3]):
		center = np.array([dot_r + 2.4, dot_r + 4.8]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		# makeDome.makeDome(.3,Point(pos[0] + 2.4,pos[1] + 4.8),coarseness);
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 2.4,pos[1] + 4.8),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	if(pattern[4]):
		center = np.array([dot_r + 2.4, dot_r + 2.4]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		# makeDome.makeDome(.3,Point(pos[0] + 2.4,pos[1] + 2.4),coarseness);
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 2.4,pos[1] + 2.4),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	if(pattern[5]):
		center = np.array([dot_r + 2.4, dot_r]);
		center = np.dot(rotation_matrix,center);
		(tri,outer) = blar.makeCircle(dot_r,Point(pos[0] + center[0],pos[1] + center[1]),coarseness);
		#makeDome.makeDome(.3,Point(pos[0] + 2.4,pos[1] + 0),coarseness)
		#(tri,outer) = makeCircle(.75,Point(pos[0] + 2.4,pos[1] + 0),coarseness);
		for i in range(0,len(tri)):
			    p1 = [tri[i][0].x,tri[i][0].y,tri[i][0].z + base_height]
			    p2 = [tri[i][1].x,tri[i][1].y,tri[i][1].z + base_height]
			    p3 = [tri[i][2].x,tri[i][2].y,tri[i][2].z + base_height]
			    elevation2stl.printTriangle(p1,p2,p3,fname)
	offset = np.array([6.5, 0]);
	offset = np.dot(rotation_matrix,offset);
	return (pos[0] + offset[0], pos[1] + offset[1])

def getBraillePattern(char):
	#currently ignoring capitalization
	if ord(char) >= 65 and ord(char) <=90:
		char = chr(ord(char) + 32)
	return{
		'a':[True ,False,False,False,False,False],
		'b':[True ,True ,False,False,False,False],
		'c':[True ,False,False,True ,False,False],
		'd':[True ,False,False,True ,True ,False],
		'e':[True ,False,False,False,True ,False],
		'f':[True ,True ,False,True ,False,False],
		'g':[True ,True ,False,True ,True ,False],
		'h':[True ,True ,False,False,True ,False],
		'i':[False,True ,False,True ,False,False],
		'j':[False,True ,False,True ,True ,False],
		'k':[True ,False,True ,False,False,False],
		'l':[True ,True ,True ,False,False,False],
		'm':[True ,False,True ,True ,False,False],
		'n':[True ,False,True ,True ,True ,False],
		'o':[True ,False,True ,False,True ,False],
		'p':[True ,True ,True ,True ,False,False],
		'q':[True ,True ,True ,True ,True ,False],
		'r':[True ,True ,True ,False,True ,False],
		's':[False,True ,True ,True ,False,False],
		't':[False,True ,True ,True ,True ,False],
		'u':[True ,False,True ,False,False,True ],
		'v':[True ,True ,True ,False,False,True ],
		'w':[False,True ,False,True ,True ,True ],
		'x':[True ,False,True ,True ,False,True ],
		'y':[True ,False,True ,True ,True ,True ],
		'z':[True ,False,True ,False,True ,True ],
		'1':[True ,False,False,False,False,False],
		'2':[True ,True ,False,False,False,False],
		'3':[True ,False,False,True ,False,False],
		'4':[True ,False,False,True ,True ,False],
		'5':[True ,False,False,False,True ,False],
		'6':[True ,True ,False,True ,False,False],
		'7':[True ,True ,False,True ,True ,False],
		'8':[True ,True ,False,False,True ,False],
		'9':[False,True ,False,True ,False,False],
		'0':[False,True ,False,True ,True ,False],
		'.':[False,True ,False,False,True ,True ],
		',':[False,True ,False,False,False,False],
		'?':[False,True ,True ,False,False,True ],
		';':[False,True ,True ,False,False,False],
		'!':[False,True ,True ,False,True ,False],
		'"':[False,True ,True ,False,False,True ],
		'#':[False,False,True ,True ,True ,True ],
	}.get(char,[False,False,False,False,False,False])

ext: py

max_stars_repo_name: bttaylor/TactileMaps

max_issues_repo_name: bttaylor/TactileMaps

max_forks_repo_name: bttaylor/TactileMaps

reason: import numpy


text:
import time

import numpy as np
from threading import Thread

from djitellopy import Tello
from .helpers import distance

class DroneProcessor:
    FINISH_DRAWING_HOLD_TIME_S = 2

    def __init__(self, max_area_cm=100, starting_move_up_cm=50, min_length_between_points_cm=5,
                 max_speed=30):
        """

        :param max_area_cm: Maximum length that drone can move from starting point in both axes.
        :param starting_move_up_cm: How many cms should drone go up after the takeoff
        :param min_length_between_points_cm: Minimum length between points, to reduce number of points from detection.
        """
        self.max_area = max_area_cm
        self.min_length_between_points_cm = min_length_between_points_cm
        self.max_speed = max_speed

        self.tello = Tello()
        self.tello.connect()
        self.tello.streamon()
        self.tello.takeoff()
        self.tello.move_up(starting_move_up_cm)

        self.tello_ping_thread = Thread(target=self.ping_tello)
        self.should_stop_pinging_tello = False

    def get_last_frame(self):
        return self.tello.get_frame_read().frame

    def finish_drawing(self):
        """
        Finish drawing, by stopping drone in air for a while and then force it to land. Disable video streaming.

        :return:
        """
        self.tello.send_rc_control(0, 0, 0, 0)
        time.sleep(self.FINISH_DRAWING_HOLD_TIME_S)
        self.tello.land()
        self.tello.streamoff()

    def ping_tello(self):
        """
        Ping tello to prevent it from landing while drawing.

        :return:
        """
        while True:
            time.sleep(1)
            self.tello.send_command_with_return("command")
            print(f"Battery level: {self.tello.get_battery()}")
            if self.should_stop_pinging_tello:
                break

    def start_pinging_tello(self):
        """
        Starts thread that pings Tello drone, to prevent it from landing while drawing

        :return:
        """
        self.tello_ping_thread.start()

    def stop_pinging_tello(self):
        """
        Stop pinging tello to make it available to control

        :return:
        """
        self.should_stop_pinging_tello = True
        self.tello_ping_thread.join()

    def rescale_points(self, point_list, is_int=False):
        """
        Rescale points from 0-1 range to range defined by max_area.

        :param point_list:
        :param is_int:
        :return: Points rescaled to max_area
        """
        temp_list = []
        for point in point_list:
            temp_point = []
            for coordinate in point:
                coordinate = coordinate * self.max_area
                if is_int:
                    temp_point.append(int(coordinate))
                else:
                    temp_point.append(coordinate)
            temp_list.append(temp_point)
        return temp_list

    def discrete_path(self, rescaled_points):
        """
        Reduce number of points in list, so the difference between next points needs to be at least
        min_length_between_points_cm

        :param rescaled_points:
        :return:
        """
        last_index = -1
        length = 0
        while length < self.min_length_between_points_cm:
            last_index -= 1
            length = distance(rescaled_points[-1], rescaled_points[last_index])

        last_index = len(rescaled_points) + last_index
        discrete_path = [rescaled_points[0]]
        actual_point = 0
        for ind, point in enumerate(rescaled_points):
            if ind > last_index:
                discrete_path.append(rescaled_points[-1])
                break
            if distance(rescaled_points[actual_point], point) > 5:
                discrete_path.append(point)
                actual_point = ind

        return discrete_path

    def reproduce_discrete_path_by_drone(self, discrete_path):
        """
        Converts discrete path to velocity commands and sends them to drone, so the drone reproduce the path

        :param discrete_path: list of [x, y] points, which represents distance in each axis between previous point in
         list
        :return:
        """
        for current_point in discrete_path:
            ang = np.arctan2(current_point[0], current_point[1])
            x_speed = int(np.sin(ang) * self.max_speed)
            y_speed = -int(np.cos(ang) * self.max_speed)

            euclidean_distance = (current_point[0] ** 2 + current_point[1] ** 2) ** 0.5
            move_time = euclidean_distance / self.max_speed
            self.tello.send_rc_control(x_speed, 0, y_speed, 0)
            time.sleep(move_time)


text:
from jigsaw import JigsawPlay
from puzzle import Puzzle
import unittest
import puzzle as pz
import numpy as np

class TestMethods(unittest.TestCase):

    def test_solve_easy(self):
        puzz = Puzzle()
        puzz.set_state([[1, 2, 3],
                        [4, 7, 5],
                        [6, 8, 0]])
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve()
        self.assertEqual(len(steps), 3)

    def test_solve_good_evaluator(self):
        puzz = Puzzle('12375846 ')
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve('good')
        self.assertEqual(len(steps), 7)

    def test_solve_fair_evaluator(self):
        puzz = Puzzle('12375846 ')
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve('fair')
        self.assertEqual(len(steps), 7)

    def test_solve_weak_evaluator(self):
        puzz = Puzzle('12375846 ')
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve('weak')
        self.assertEqual(len(steps), 7)

    def test_solve_bad_evaluator(self):
        puzz = Puzzle('12375846 ')
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve('bad')
        self.assertEqual(len(steps), 7)

    def test_solve_complex(self):
        puzz = Puzzle()
        puzz.set_state([[1, 2, 3],
            [7, 5, 8],
            [4, 6, 0]])
        jigsaw = JigsawPlay(puzz)
        steps = jigsaw.solve('good')
        self.assertEqual(len(steps), 7)

max_stars_repo_name: SamVanhoutte/python-musings

max_issues_repo_name: SamVanhoutte/python-musings

max_forks_repo_name: SamVanhoutte/python-musings


text:
import codecs
import json
import os

import numpy as np

from pathlib import Path

from .evaluate import evaluate_with_gin

from disentanglement_lib.config.unsupervised_study_v1 import sweep as unsupervised_study_v1

def compute_metrics(exp_path,
                    dataset,
                    random_seed,
                    epoch=0,
                    prefix="epoch"):
    overwrite = True
    _study = unsupervised_study_v1.UnsupervisedStudyV1()
    evaluation_configs = sorted(_study.get_eval_config_files())

    expected_evaluation_metrics = [
        "factor_vae_metric",
        "modularity_explicitness",
        "sap_score",
        "mig",
        "dci"
    ]

    for gin_eval_config in evaluation_configs:
        metric_name = gin_eval_config.split("/")[-1].replace(".gin", "")
        if metric_name not in expected_evaluation_metrics:
            continue
        print(f"Evaluationg Metric: {metric_name}")
        result_path = os.path.join(exp_path, "metrics", metric_name)
        representation_path = os.path.join(exp_path, "representation")
        eval_bindings = [
            f"evaluation.name = '{metric_name}'"
        ]
        evaluate_with_gin(
            representation_path,
            result_path,
            dataset,
            overwrite,
            [gin_eval_config],
            random_seed,
            eval_bindings)

    # Gather evaluation results
    evaluation_result_template = "{}/metrics/{}/results/aggregate/evaluation.json"
    final_scores = {}
    for _metric_name in expected_evaluation_metrics:
        evaluation_json_path = evaluation_result_template.format(
            exp_path,
            _metric_name
        )
        evaluation_results = json.loads(
                open(evaluation_json_path, "r").read()
        )

        if _metric_name == "factor_vae_metric":
            _score = evaluation_results["evaluation_results.eval_accuracy"]
            final_scores["factor_vae_metric"] = _score
        elif _metric_name == "beta_vae_sklearn":
            _score = evaluation_results["evaluation_results.eval_accuracy"]
            final_scores["beta_vae_metric"] = _score
        elif _metric_name == "modularity_explicitness":
            _score = evaluation_results["evaluation_results.modularity_score"]
            final_scores["modularity_score"] = _score
        elif _metric_name == "dci":
            _score = evaluation_results["evaluation_results.disentanglement"]
            final_scores["dci"] = _score
        elif _metric_name == "mig":
            _score = evaluation_results["evaluation_results.discrete_mig"]
            final_scores["mig"] = _score
        elif _metric_name == "sap_score":
            _score = evaluation_results["evaluation_results.SAP_score"]
            final_scores["sap_score"] = _score
        elif _metric_name == "irs":
            _score = evaluation_results["evaluation_results.IRS"]
            final_scores["irs"] = _score
        else:
            raise Exception("Unknown metric name : {}".format(_metric_name))

    metric_result_dir = exp_path.parent
    metric_result_path = Path(f"{metric_result_dir}/metric_results.json")
    if metric_result_path.exists():
        with open(metric_result_path, "r") as f:
            metric_results = json.load(f)
        metric_results[f"{prefix}{epoch}"] = final_scores
        save_json(metric_results, metric_result_path)
    else:
        metric_results = {f"{prefix}{epoch}": final_scores}
        save_json(metric_results, metric_result_path)
    print("Final Scores : ", final_scores)

class JsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(JsonEncoder, self).default(obj)

def save_json(config: dict, save_path: Path):
    f = codecs.open(str(save_path), mode="w", encoding="utf-8")
    json.dump(config, f, indent=4, cls=JsonEncoder, ensure_ascii=False)
    f.close()


text:
from __future__ import print_function

'''
    RNARedPrintSampler.py: Class that uses system calls to sample sequences with the RNARedPrint program. It implements the same interface as the DependencyGraph of RNAblueprint. This makes it possible to use it within RNAsketch to optimize sequences.
'''

__author__ = "Stefan Hammer"
__copyright__ = "Copyright 2017"
__version__ = "0.1"
__maintainer__ = "Stefan Hammer"
__email__ = "s.hammer@univie.ac.at"

from distutils.dir_util import copy_tree
import os
import sys
import uuid
import shutil
import timeit
import subprocess as sp
import math
from RNAsketch import *

import numpy as np
import re
import scipy.stats as stats

from Structure import RNAStructure

class RPSampler(object):
    '''
    RPSampler is a python wrapper for RNARedPrint and implements a similar interface to the RNAblueprint DependencyGraphMT object.

    :param structures: List of Dot-bracket structure strings
    :param constraint: Sequence constraints in IUPACK notation
    :param model: String specifying the model, can be 'uniform',  'nussinov', 'basepairs' or 'stacking'
    :param weights: List of integers specifying the boltzmann weight for the target structures, range in ]0, +infinity]
    :param gcweight: Integer specifying the GC-weight, ]0, +infinity]
    :param temperature: Temperature for the folding predictions (default: 37.0 degree celsius)
    :param stacksize: Size of one sequence sampling batch (default: 1000 sequences)
    :param StopConstruct: Bool specifying if we want to benchmark the construction time. Measured time can be obtained from construction_time after construction (default: False)
    :param RedPrintFolder: Location of the RNARedPrint project folder (default: './RNARedPrint/')
    :param debug: Bool to print debug statements (default: False)
    '''
    def __init__(self, structures, constraint='', model='nussinov', weights=[], gcweight=1, temperature=37.0, stacksize=1000, StopConstruct=False, RedPrintFolder = None, debug=False):
        self._structures = structures
        self._constraint = constraint
        self.model = model
        self._samplestack = []
        self._stacksize = stacksize
        self.sample_time = 0
        self.construction_time = 0
        self.number_of_connected_components = 1
        self._current = 0
        self.gcweight = gcweight
        self._temperature = temperature
        self.weights = weights
        self._debug = debug

        if not RedPrintFolder:
            RedPrintFolder = self._get_path('')
            if not RedPrintFolder:
                RedPrintFolder = './RNARedPrint/'
        # copy RNARedprint binary to temp toDirectory for multithread
        self._copy_RNAredprint_folder(RedPrintFolder)

        # call RNAredprint to get construction time
        if StopConstruct:
            self._call_RNAredprint(0)

    def __del__(self):
         shutil.rmtree(self._RedPrintFolder, ignore_errors=True)

    def _get_path(self, subfolder):
        '''
        Try to detect the RNARedPrint project folder location

        :param subfolder: string specifying a subfolder, e.g. 'bin/'
        :return: string with the complete project path
        '''
        if 'REDPRINT' in os.environ:
            return os.environ['REDPRINT'] + subfolder
        else:
            return subfolder

    @property
    def model(self):
        '''
        String specifying the sampling model.

        :return: string, can be 'uniform',  'nussinov', 'basepairs' or 'stacking'
        '''
        return self._model
    @model.setter
    def model(self, value):
        modelarg = None
        if value == 'uniform':
            modelarg = 0
        elif value == 'nussinov':
            modelarg = 1
        elif value == 'basepairs':
            modelarg = 2
        elif value == 'stacking':
            modelarg = 3
        else:
            raise ValueError('Modelarg not set!')
        self._modelarg = modelarg
        self._model = value

    @property
    def weights(self):
        '''
        List of integers specifying the boltzmann weight for the target structures, range in ]0, +infinity]

        '''
        return self._weights
    @weights.setter
    def weights(self, value):
        # standard weights for boltzmann sampling
        if not value:
            value = [math.exp(1/((self._temperature + 273.15)*0.00198717))] * len(self._structures)
        # check amount of weights
        if len(value) != len(self._structures):
            raise ValueError('Amount of weights must be equal to the amount of structures')
        for w in value:
            if w <= 0:
                raise ValueError('Weights must be in range ]0, +infinity]')
        self._weights = value

    @property
    def gcweight(self):
        '''
        Integer specifying the GC-weight, ]0, +infinity]
        '''
        return self._gcweight
    @gcweight.setter
    def gcweight(self, value):
        if value <= 0:
            raise ValueError('GC weight must be in range ]0, +infinity]')
        else:
            self._gcweight = value

    def dump_new_stack(self):
        '''
        Draws a sample from RNARedPrint and returns the list of generated sequences at once. The variable stacksize specifies the amount of sequences.

        :return: List of RNA sequence strings
        '''
        self._samplestack = []
        self._current = 0

        newseqs, energies = self._call_RNAredprint(number=(self._stacksize))
        return newseqs, energies

    def sample(self):
        '''
        Sample a new sequence.

        :return: Returns 0 for compatibility to DependencyGraphMT.
        '''
        self._current += 1
        return 0

    def sample_clocal(self):
        '''
        Compatibility to the DependencyGraphMT object, calls sample() internally.
        '''
        self.sample()

    def sample_plocal(self):
        '''
        Compatibility to the DependencyGraphMT object, calls sample() internally.
        '''
        self.sample()

    def get_sequence(self):
        '''
        Get the sampled sequence as a string.

        :return: RNA Sequence string in IUPACK notation
        '''
        if len(self._samplestack) <= self._current:
            if self._stacksize <= self._current:
                self._stacksize += 1000
            self._get_new_sample()
        return self._samplestack[self._current]

    def revert_sequence(self, amount):
        '''
        Compatibility to the DependencyGraphMT object, does nothing internally.
        '''
        #self._current -= amount
        pass

    def set_sequence(self, seq):
        '''
        Compatibility to the DependencyGraphMT object, does nothing internally.
        '''
        pass

    def set_history_size(self, size):
        '''
        Compatibility to the DependencyGraphMT object, does nothing internally.
        '''
        pass
        #if self._stacksize != size:
        #    self._stacksize = size
        #    self._get_new_sample()

    def _get_new_sample(self):
        # generate list of sequences with RNAredprint
        if (self._debug):
            print('# getting new sequences: ', self._stacksize, len(self._samplestack), str(self._stacksize - len(self._samplestack)))
        newseqs, _ = self._call_RNAredprint(number=(self._stacksize - len(self._samplestack)))
        self._samplestack.extend(newseqs)

    def _copy_RNAredprint_folder(self, RedPrintFolder):
        # copy subdirectory example
        fromDirectory = RedPrintFolder
        toDirectory = "/tmp/RNARedPrint-" + str(uuid.uuid4())
        self._RedPrintFolder = toDirectory
        copy_tree(fromDirectory, toDirectory)

    def _call_RNAredprint(self, number=1000):
        # resolve PKs
        structuresNoPK = {}
        weights = {}
        for i, s in enumerate(self._structures):
            structuresNoPK[s] = RNAStructure(s).resolvePKs()
            weights[s] = self._weights[i]

        # create structure and weight list
        structures_cmd = []
        weights_cmd = []
        for struct, NoPKs in structuresNoPK.items():
            for NoPK in NoPKs:
                structures_cmd.append('"'+str(NoPK)+'"')
                weights_cmd.append(weights[struct])

        cmd = ['./RNARedPrint'] + structures_cmd + ['--num', str(number), '--model', str(self._modelarg), '-gcw', str(self._gcweight), '--weights', ','.join(map(str, weights_cmd))]
        #cmd=['ls']
        cmdstring = " ".join(cmd)
        if (self._debug):
            print("# ", cmdstring)
        #cmd = ['RNAblueprint', '-n', str(number)]
        #stdin = '\n'.join(structures+[constraint])
        #p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
        start = timeit.default_timer()
        p = sp.Popen(cmdstring, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True, cwd=self._RedPrintFolder + "/bin/")
        #out, err = p.communicate(input=stdin)
        out, err = p.communicate(input=None)
        if err:
            exit(err)
        if number == 0:
            self.construction_time = timeit.default_timer() - start
        else:
            self.sample_time += timeit.default_timer() - start

        if (self._debug):
            print("# ", out, err)
        lines = out.splitlines()
        seqpattern = re.compile(r"^[AUGC]+")
        strucpattern = re.compile(r"^[\.\(\)]+$")
        epattern = re.compile(r"E(\d)=(-?[\d]+\.?[\d]*)")

        structures = []
        sequences = []
        energies = []
        for l in lines:
            s = re.search(strucpattern, l, flags=0)
            if s:
                structures.append(s.group(0))
            else:
                s = re.search(seqpattern, l, flags=0)
                if s:
                    sequences.append(s.group(0))
                    struct_energy = {}
                    for e in re.finditer(epattern, l, flags=0):
                        struct_energy[structures[int(e.group(1))-1]] = float(e.group(2))
                    energy = {}
                    for i, s in enumerate(self._structures):
                        energy[i] = 0
                        for sNoPKs in structuresNoPK[s]:
                            energy[i] += struct_energy[sNoPKs]
                    energies.append(energy)
        return sequences, energies

reason: import numpy,import scipy


text:
# -*- coding: utf-8 -*-
"""implement a polynomial basis function."""

import numpy as np

def build_poly(x, degree):
    """polynomial basis functions for input data x, for j=0 up to j=degree."""
    return x.reshape(x.shape[0], 1) ** np.arange(degree + 1)


text:
import os
import numpy as np

datafolder="/home/jliu447/lossycompression/MIRANDA/extracted"

ebs=[i*1e-4 for i in range(1,10)]+[i*1e-3 for i in range(1,10)]+[i*1e-2 for i in range(1,11)]

cr=np.zeros((29,51),dtype=np.float32)
psnr=np.zeros((29,51),dtype=np.float32)
filelist=os.listdir(datafolder)
filelist=[x for x in filelist if x.split('.')[-1]=='f32']
filelist=sorted(filelist)
for i,eb in enumerate(ebs):
    cr[i+1][0]=eb
    psnr[i+1][0]=eb
    for j,filename in enumerate(filelist):
        idx=int(filename.split('-')[1])
        cr[0][j+1]=idx
        psnr[0][j+1]=idx
        filepath=os.path.join(datafolder,filename)
        comm="sz -z -f -i %s -M REL -R %f -3 384 384 256" % (filepath,eb)
        os.system(comm)
        szpath=filepath+".sz"
        comm="sz -x -f -i %s -s %s -3 384 384 256 -a>tempmirandasz.txt" % (filepath,szpath)
        os.system(comm)
        with open("tempmirandasz.txt","r") as f:
            lines=f.read().splitlines()
            p=eval(lines[4].split(',')[0].split('=')[1])

            r=eval(lines[7].split('=')[1])

            cr[i+1][j+1]=r
            psnr[i+1][j+1]=p

        comm="rm -f %s" % szpath
        os.system(comm)
        comm="rm -f %s" % szpath+".out"
        os.system(comm)
        os.system("rm -f tempmirandasz.txt")

np.savetxt("sz_miranda_cr.txt",cr,delimiter='\t')
np.savetxt("sz_miranda_psnr.txt",psnr,delimiter='\t')


text:
#  Tencent is pleased to support the open source community by making GNES available.
#
#  Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
from typing import List, Tuple, Any

import numpy as np

from .helper import ListKeyIndexer
from ..base import BaseChunkIndexer as BCI

class AnnoyIndexer(BCI):

    def __init__(self, num_dim: int, data_path: str, metric: str = 'angular', n_trees: int = 10, *args, **kwargs):
        """
        Initialize an AnnoyIndexer

        :param num_dim: when set to -1, then num_dim is auto decided on first .add()
        :param data_path: index data file managed by the annoy indexer
        :param metric:
        :param n_trees:
        :param args:
        :param kwargs:
        """
        super().__init__(*args, **kwargs)
        self.num_dim = num_dim
        self.data_path = data_path
        self.metric = metric
        self.n_trees = n_trees
        self.helper_indexer = ListKeyIndexer()

    def post_init(self):
        from annoy import AnnoyIndex
        self._index = AnnoyIndex(self.num_dim, self.metric) if self.num_dim >= 0 else None
        try:
            if not os.path.exists(self.data_path):
                raise FileNotFoundError('"data_path" is not exist')
            if os.path.isdir(self.data_path):
                raise IsADirectoryError('"data_path" must be a file path, not a directory')
            self._index.load(self.data_path)
        except:
            self.logger.warning('fail to load model from %s, will create an empty one' % self.data_path)

    @BCI.update_helper_indexer
    def add(self, keys: List[Tuple[int, Any]], vectors: np.ndarray, weights: List[float], *args, **kwargs):
        last_idx = self.helper_indexer.num_chunks

        if len(vectors) != len(keys):
            raise ValueError('vectors length should be equal to doc_ids')

        if vectors.dtype != np.float32:
            raise ValueError("vectors should be ndarray of float32")

        if self._index is None:
            from annoy import AnnoyIndex
            # means num_dim in unknown during init
            self.num_dim = vectors.shape[1]
            self._index = AnnoyIndex(self.num_dim, self.metric)

        for idx, vec in enumerate(vectors):
            self._index.add_item(last_idx + idx, vec)

    def query(self, keys: 'np.ndarray', top_k: int, *args, **kwargs) -> List[List[Tuple]]:
        self._index.build(self.n_trees)
        if keys.dtype != np.float32:
            raise ValueError('vectors should be ndarray of float32')
        res = []
        for k in keys:
            ret, relevance_score = self._index.get_nns_by_vector(k, top_k, include_distances=True)
            chunk_info = self.helper_indexer.query(ret)
            res.append([(*r, s) for r, s in zip(chunk_info, relevance_score)])
        return res

    def __getstate__(self):
        d = super().__getstate__()
        self._index.save(self.data_path)
        return d

max_stars_repo_name: micro-pixel/gnes


text:
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Experiment s \"barevným\" modelem jater.

Pro spuštění zkuste help:

python experiments/20130919_liver_statistics.py --help

Měřené vlastnosti se přidávají do get_features().
Pro přidání dalších dat, editujte příslušný yaml soubor.

"""

# import funkcí z jiného adresáře
import sys
import os.path

path_to_script = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(path_to_script, "../src/"))
sys.path.append(os.path.join(path_to_script, "../extern/pyseg_base/src"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/sed3/"))
sys.path.append(os.path.join(path_to_script,
                             "../extern/lbp/"))
#sys.path.append(os.path.join(path_to_script, "../extern/"))
#import featurevector

from loguru import logger
# logger = logging.getLogger()

#import apdb
#  apdb.set_trace();
#import scipy.io
import numpy as np
#import scipy
#from scipy import sparse
#import traceback
import itertools

# ----------------- my scripts --------
import sed3
#import dcmreaddata1 as dcmr
#import dcmreaddata as dcmr
import argparse
#import sed3

import misc
import datareader
import matplotlib.pyplot as plt
import experiments

def feat_hist_by_segmentation(data3d_orig, data3d_seg, visualization=True):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    hist1, bin_edges1 = np.histogram(data3d_orig[data3d_seg > 0], bins=bins)
    hist2, bin_edges2 = np.histogram(data3d_orig[data3d_seg <= 0], bins=bins)
    #import pdb; pdb.set_trace()
    if visualization:
        plt_liver = plt.step(bin_edges1[1:], hist1)
        plt_rest = plt.step(bin_edges2[1:], hist2)
        plt.legend([plt_liver, plt_rest], ['Liver', 'Other tissue'])
        #plt.plot(bin_edges1[1:], hist1, bin_edges2[1:], hist2)
        plt.show()
    fv_hist = {'hist1': hist1,
               'hist2': hist2,
               'bins': bins
               }
    return fv_hist

def feat_hist(data3d_orig):
    bins = range(-1024, 1024, 1)
    bins = range(-512, 512, 1)
    bins = range(-512, 512, 10)
    bins = range(-512, 512, 64)
    bins = range(-512, 512, 100)
    hist1, bin_edges1 = np.histogram(data3d_orig, bins=bins)
    return hist1

def get_features(data3d_orig, data3d_seg, feature_fcn, visualization=True):
    u"""
    Sem doplníme všechny naše měření.

    Pro ukázku jsem vytvořil měření
    histogramu.
    data3d_orig: CT data3d_orig
    data3d_seg: jedničky tam, kde jsou játra

    """

    featur = feature_fcn(data3d_orig)
    #featur = {}
    #featur['hist'] = feat_hist(data3d_orig, visualization)
    #featur['lbp'] = lbp(data3d_orig, data3d_seg, visualization)

    return featur

def sample_input_data():
    inputdata = {
        'basedir': '/home/mjirik/data/medical/',
        'data': [
            {'sliverseg': 'data_orig/sliver07/training-part1/liver-seg001.mhd', 'sliverorig': 'data_orig/sliver07/training-part1/liver-orig001.mhd'},
            {'sliverseg': 'data_orig/sliver07/training-part1/liver-seg002.mhd', 'sliverorig': 'data_orig/sliver07/training-part1/liver-orig002.mhd'},
        ]
    }

    sample_data_file = os.path.join(path_to_script,
                                    "20130919_liver_statistics.yaml")
    #print sample_data_file, path_to_script
    misc.obj_to_file(inputdata, sample_data_file, filetype='yaml')

#def voe_metric(vol1, vol2, voxelsize_mm):

def write_csv(data, filename="20130919_liver_statistics.yaml"):
    import csv
    with open(filename, 'wb') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=';',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL
                                )
        for label in data:
            spamwriter.writerow([label] + data[label])
            #spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

#def training_dataset_prepare
def read_data_orig_and_seg(inputdata, i):
    """ Loads data_orig and data_seg from yaml file
    """

    reader = datareader.DataReader()
    data3d_a_path = os.path.join(inputdata['basedir'],
                                 inputdata['data'][i]['sliverseg'])
    data3d_a, metadata_a = reader.Get3DData(data3d_a_path, dataplus_format=False)

    data3d_b_path = os.path.join(inputdata['basedir'],
                                 inputdata['data'][i]['sliverorig'])
    data3d_b, metadata_b = reader.Get3DData(data3d_b_path, dataplus_format=False)

    #import pdb; pdb.set_trace()
    data3d_seg = (data3d_a > 0).astype(np.int8)
    data3d_orig = data3d_b

    return data3d_orig, data3d_seg

def one_experiment_setting_for_whole_dataset(inputdata, tile_shape,
                                             feature_fcn, classif_fcn, train,
                                             visualization=False):
    fvall = []
    fv_tiles = []
    indata_len = len(inputdata['data'])
    indata_len = 3

    for i in range(0, indata_len):
        data3d_orig, data3d_seg = read_data_orig_and_seg(inputdata, i)

        feat_hist_by_segmentation(data3d_orig, data3d_seg, visualization)

        if visualization:
            pyed = sed3.sed3(data3d_orig,
                                                 contour=data3d_seg)
            pyed.show()

            #import pdb; pdb.set_trace()
        #fvall.insert(i, get_features(
        #    data3d_orig,
            #ltl = (labels_train_lin_float * 10).astype(np.int8)
            #labels_train = arrange_to_tiled_data(cidxs, tile_shape,
            #                                     d_shp, ltl)

            #pyed = sed3.sed3(labels_train, contour=labels)

# @TODO vracet něco inteligentního, fvall je prázdný
    return fvall

def make_product_list(list_of_feature_fcn, list_of_classifiers):
#   TODO work with list_of_feature_fcn and list_of_classifiers
    featrs_plus_classifs = itertools.product(list_of_feature_fcn,
                                             list_of_classifiers)
    return featrs_plus_classifs

def experiment(path_to_yaml, featrs_plus_classifs,
               tile_shape, visualization=False, train=False):

    inputdata = misc.obj_from_file(path_to_yaml, filetype='yaml')

    #import ipdb; ipdb.set_trace()  # noqa BREAKPOINT

    results = []

    for fpc in featrs_plus_classifs:
        feature_fcn = fpc[0]
        classif_fcn = fpc[1]

        fvall = one_experiment_setting_for_whole_dataset(
            inputdata, tile_shape,
            feature_fcn, classif_fcn, train, visualization)

        result = {'params': str(fpc), 'fvall': fvall}
        results.append(result)
        print(results)

    return results

def main():

    ## logger = logging.getLogger()
    # logger = logging.getLogger()

    logger.setLevel(logging.WARNING)
    ch = logging.StreamHandler()
    logger.addHandler(ch)

    #logger.debug('input params')

    parser = argparse.ArgumentParser(
        description='Compute features on liver and other tissue.')
    parser.add_argument('-si', '--sampleInput', action='store_true',
                        help='generate sample intput data', default=False)
    parser.add_argument('-v', '--visualization',  action='store_true',
                        help='Turn on visualization', default=False)
    parser.add_argument('-i', '--input', help='input yaml file',
                        default="20130919_liver_statistics.yaml")
    parser.add_argument('-o', '--output', help='output file',
                        default="20130919_liver_statistics_results.pkl")
    parser.add_argument('-t', '--train', help='Training', default=False,
                        action='store_true'
                        )
    args = parser.parse_args()

    if args.sampleInput:
        sample_input_data()
    # input parser
    #path_to_yaml = os.path.join(path_to_script, args.input)
    path_to_yaml = args.input

    #write_csv(fvall)
    list_of_feature_fcn = [feat_hist]
    from sklearn import svm
    from sklearn.naive_bayes import GaussianNB

    list_of_classifiers = [svm.SVC, GaussianNB]
    tile_shape = [1, 100, 100]
    featrs_plus_classifs = make_product_list(list_of_feature_fcn,
                                             list_of_classifiers)

    result = experiment(path_to_yaml, featrs_plus_classifs,
                        tile_shape=tile_shape,
                        visualization=args.visualization, train=args.train)

# Ukládání výsledku do souboru
    output_file = os.path.join(path_to_script, args.output)
    misc.obj_to_file(result, output_file, filetype='pickle')

if __name__ == "__main__":
    main()

max_stars_repo_name: mjirik/lisa

max_issues_repo_name: mjirik/lisa

max_forks_repo_name: mjirik/lisa

reason: import numpy,import scipy,from scipy


text:
import random
import numpy as np

def getStateFromString(state):
    import hashlib
    return int(hashlib.md5(state).hexdigest()[:8], 16)

rng_dictionary = {}
rng_dictionary[getStateFromString("default")] = np.random.RandomState(10)

def randint(minimum, maximum, state="default"):
	if getStateFromString(state) not in rng_dictionary.keys():
		rng_dictionary[getStateFromString(state)]  = np.random.RandomState()

	if minimum == maximum:
		return minimum

	return rng_dictionary[getStateFromString(state)].randint(minimum, maximum)

def random(state="default"):
	if getStateFromString(state) not in rng_dictionary.keys():
		rng_dictionary[getStateFromString(state)]  = np.random.RandomState()

	return rng_dictionary[getStateFromString(state)].rand()

def choice(list_objects, how_many=1, state="default"):
	if getStateFromString(state) not in rng_dictionary.keys():
		rng_dictionary[getStateFromString(state)]  = np.random.RandomState()

	return rng_dictionary[getStateFromString(state)].choice(list_objects, how_many)

def setSeed(state, seed):
	rng_dictionary[getStateFromString(state)] = np.random.RandomState(seed)

def shuffle(list, state="default"):
	if getStateFromString(state) not in rng_dictionary.keys():
		rng_dictionary[getStateFromString(state)]  = np.random.RandomState()

	return rng_dictionary[getStateFromString(state)].shuffle(list)

max_stars_repo_name: ehauckdo/mcedit-filters

max_issues_repo_name: ehauckdo/mcedit-filters

max_forks_repo_name: ehauckdo/mcedit-filters


text:
from networktables import NetworkTables
from cscore import CameraServer, UsbCamera
import numpy as np

import time

import config
from shooter_targeting import ShooterTargeting

def main():
    print('!! vision.py launched !!')
    # NetworkTables.initialize(server='localhost')  # tfw u r the robot
    sd = NetworkTables.getTable('SmartDashboard')

    cs = CameraServer.getInstance()
    cs.enableLogging()

    #################
    #  GEAR CAMERA  #
    #################

    # No targeting, just vision
    gear_camera = cs.startAutomaticCapture(dev=config.GEAR_CAMERA,
                                           name='Gear Camera')
    gear_camera.setResolution(config.TARGETING_CAMERA_WIDTH,
                              config.TARGETING_CAMERA_HEIGHT)

    print('!! vision.py started capture !!')

    ####################
    #  SHOOTER CAMERA  #
    ####################

    shooter_camera = UsbCamera('Shooter Raw', config.SHOOTER_CAMERA)
    # shooter_camera = cs.startAutomaticCapture(dev=config.SHOOTER_CAMERA)
    shooter_camera.setResolution(config.TARGETING_CAMERA_WIDTH,
                                 config.TARGETING_CAMERA_HEIGHT)
    shooter_targeting = ShooterTargeting()

    # Get a CvSink. This will capture images from the camera
    # cvSink = cs.getVideo(name='cam%d' % config.SHOOTER_CAMERA)
    cvSink = cs.getVideo(camera=shooter_camera)

    # (optional) Setup a CvSource. This will send images back to the Dashboard
    outputStream = cs.putVideo('Targeting Cam',
                               config.TARGETING_CAMERA_HEIGHT,
                               config.TARGETING_CAMERA_WIDTH)

    # Allocating new images is very expensive, always try to preallocate
    img = np.zeros(shape=(config.TARGETING_CAMERA_HEIGHT,
                          config.TARGETING_CAMERA_WIDTH, 3),
                   dtype=np.uint8)

    while True:
        # Tell the CvSink to grab a frame from the camera and put it
        # in the source image.  If there is an error notify the output.
        elapsed, img = cvSink.grabFrame(img)
        if elapsed == 0:
            # Send the output the error.
            outputStream.notifyError(cvSink.getError())
            # skip the rest of the current iteration
            time.sleep(10)
            continue

        # Process
        result, processed_img = shooter_targeting.process(img)

        outputStream.putFrame(processed_img)
        if result:
            sd.putBoolean(config.TARGETING_SHOOTER_FOUND_KEY, True)
            sd.putNumberArray(config.TARGETING_SHOOTER_CENTER_KEY,
                              result['center'])
        else:
            sd.putBoolean(config.TARGETING_SHOOTER_FOUND_KEY, False)

        time.sleep(10)

max_stars_repo_licenses: Unlicense MIT

max_issues_repo_licenses: Unlicense MIT

max_forks_repo_licenses: Unlicense MIT


text:
import pytest

import numpy as np
import six
import time
import warnings

import pescador.util

def _eq_batch(b1, b2):

    for k in six.iterkeys(b1):
        assert np.allclose(b1[k], b2[k])

def _eq_lists(b1, b2):

    assert len(b1) == len(b2)

    for i, j in zip(b1, b2):
        assert np.allclose(i, j)

def _eq_list_of_dicts(b1, b2):
    results = []
    results.append(len(b1) == len(b2))

    if results[-1]:
        for i in range(len(b1)):
            for k in six.iterkeys(b1[i]):
                results.append(np.allclose(b1[i][k], b2[i][k]))

    return np.all(results)

def finite_generator(n, size=2, lag=None):

    for i in range(n):
        yield {'X': np.tile(np.array([[i]]), (size, 1))}
        if lag is not None:
            time.sleep(lag)

def md_generator(dimension, n, size=2, items='X'):
    """Produce `n` dicts of `dimension`-rank arrays under the names in `items`.
    """
    shape = [size] * dimension

    M = len(items)
    for i in range(n):

        yield {item: i * M * np.ones(shape) + j
               for j, item in enumerate(items)}

def infinite_generator(size=2, offset=0):

    i = 0
    while True:
        yield {'X': np.tile(np.array([[i]]), (size, 1)) + offset}
        i = i + 1

def __zip_generator(n, size1, size2):

    for b1, b2 in zip(finite_generator(n, size=size1),
                      finite_generator(n, size=size2)):
        yield dict(X=b1['X'], Y=b2['X'])

@pytest.mark.parametrize(
    'n1,n2', [pytest.mark.xfail((5, 10), raises=pescador.util.PescadorError),
              pytest.mark.xfail((5, 15), raises=pescador.util.PescadorError),
              pytest.mark.xfail((10, 5), raises=pescador.util.PescadorError),
              pytest.mark.xfail((15, 5), raises=pescador.util.PescadorError),
              (5, 5), (10, 10), (15, 15)])
def test_batch_length(n1, n2):
    generator, n = __zip_generator(3, n1, n2), n1

    for batch in generator:
        assert pescador.util.batch_length(batch) == n

def test_warning_deprecated():

    @pescador.util.deprecated('old_version', 'new_version')
    def __dummy():
        return True

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:
        x = __dummy()

        # Make sure we still get the right value
        assert x is True

        # And that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is DeprecationWarning

        # And that it says the right thing (roughly)
        assert 'deprecated' in str(out[0].message).lower()

def test_warning_moved():

    @pescador.util.moved('from', 'old_version', 'new_version')
    def __dummy():
        return True

    warnings.resetwarnings()
    warnings.simplefilter('always')
    with warnings.catch_warnings(record=True) as out:
        x = __dummy()

        # Make sure we still get the right value
        assert x is True

        # And that the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is DeprecationWarning

        # And that it says the right thing (roughly)
        assert 'moved' in str(out[0].message).lower()

def test_warning_rename_kw_pass():
    warnings.resetwarnings()
    warnings.simplefilter('always')

    ov = pescador.util.Deprecated()
    nv = 23

    with warnings.catch_warnings(record=True) as out:
        v = pescador.util.rename_kw('old', ov, 'new', nv, '0', '1')

        assert v == nv

        # Make sure no warning triggered
        assert len(out) == 0

def test_warning_rename_kw_fail():
    warnings.resetwarnings()
    warnings.simplefilter('always')

    ov = 27
    nv = 23

    with warnings.catch_warnings(record=True) as out:
        v = pescador.util.rename_kw('old', ov, 'new', nv, '0', '1')

        assert v == ov

        # Make sure the warning triggered
        assert len(out) > 0

        # And that the category is correct
        assert out[0].category is DeprecationWarning

        # And that it says the right thing (roughly)
        assert 'renamed' in str(out[0].message).lower()

max_stars_repo_name: beasteers/pescador

max_issues_repo_name: beasteers/pescador

max_forks_repo_name: beasteers/pescador


text:
#################################################
# Import Dependencies
#################################################

import datetime as dt
import numpy as np
from dateutil.relativedelta import relativedelta

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect

from flask import Flask, jsonify

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)

# Save reference to the table
Measurement=Base.classes.measurement
Station=Base.classes.station

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

#################################################
# Flask Routes
#################################################

@app.route("/")
def welcome():
    return (
        f"Welcome to the Climate API!
"
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/2017-01-01
"
        f"/api/v1.0/2017-01-01/2017-01-10"
    )

@app.route("/api/v1.0/precipitation")
def precipitation():

    # Create our session (link) to the DB
    session = Session(engine)

    # Calculate the date 1 year ago from the last data point in the database

    #Find max value of date in the database
    result=session.query(Measurement).order_by(Measurement.date.desc()).first()
    last_date=result.date

    #Calculate the date 1 year from max date
    year_ago=(dt.datetime.strptime(last_date, '%Y-%m-%d') - relativedelta(years=1)).date()
    #print(year_ago)

    # Perform a query to retrieve the date and precipitation scores
    data=session.query(Measurement.date,Measurement.prcp)\
    .filter(Measurement.date>=year_ago)\
    .filter(Measurement.date<=last_date).all()

    #Close session after use
    session.close()

    # Convert list of tuples into dictionary with date as id and precipitation quantity as value
    precp_dict=dict(data)

    return jsonify(precp_dict)

@app.route("/api/v1.0/stations")
def station():
    # Create our session (link) to the DB
    session = Session(engine)

    #List of unique stations
    station_count=session.query(Station.name).filter(Measurement.station == Station.station).distinct().all()

    #dictionary to store name and count of occurrence
    station={}
    station["name"]=[]

    #Assign values to each staion based on query result
    for row in station_count:
        station["name"].append(row[0])

    #Close session after use
    session.close()

    return jsonify(station)

@app.route("/api/v1.0/tobs")
def tobs_data():

    # Create our session (link) to the DB
    session = Session(engine)

    # Find the station with most records in last one year
    #Find max value of date in the database
    result=session.query(Measurement).order_by(Measurement.date.desc()).first()
    last_date=result.date

    #Calculate the date 1 year from max date
    year_ago=(dt.datetime.strptime(last_date, '%Y-%m-%d') - relativedelta(years=1)).date()
    #print(year_ago)

    #Find station with most number of records

    query=session.query(Measurement).group_by(Measurement.station).order_by(func.count(Measurement.station).desc()).first()
    most_active_station=query.station
    # Perform a query to retrieve the date and tobs value for most active station
    data=session.query(Measurement.date,Measurement.tobs).filter(Measurement.station==most_active_station)\
    .filter(Measurement.date>=year_ago).filter(Measurement.date<=last_date)\
    .all()

    #Close session after use
    session.close()

    # Convert list of tuples into dictionary with date as id and tobs as value
    station_date_tobs=dict(data)
    return jsonify(station_date_tobs)

"""
@app.route("/api/v1.0/<start>")
def stats_start(start):

    # Create our session (link) to the DB
    session = Session(engine)

    #Find min, average and max temperature observation for dates equal to greater than start date passed in the API
    data=session.query(func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
    .filter(func.strftime("%Y-%m-%d", Measurement.date) >=start)\
    .all()

    #Close session after use
    session.close()

    stats_start_dict={}
    #dict(data)

    for row in data:
        stats_start_dict["Min_temp"]=row[0]
        stats_start_dict["Avg_temp"]=row[1]
        stats_start_dict["Max_temp"]=row[2]

    return jsonify(stats_start_dict)
"""
@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def stats_Start_end(start,end=None):

    # Create our session (link) to the DB
    session = Session(engine)

    #Find min, average and max temperature observation for dates equal to greater than start date passed in the API
    if end!=None:
        data=session.query(func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
        .filter(func.strftime("%Y-%m-%d", Measurement.date) >=start)\
        .filter(func.strftime("%Y-%m-%d", Measurement.date) <=end)\
        .all()
    else:
        #Find min, average and max temperature observation for dates equal to greater than start date passed in the API
        data=session.query(func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
        .filter(func.strftime("%Y-%m-%d", Measurement.date) >=start)\
        .all()

    #Close session after use
    session.close()

    stats_start_end_dict={}
    #dict(data)

    for row in data:
        stats_start_end_dict["Min_temp"]=row[0]
        stats_start_end_dict["Avg_temp"]=row[1]
        stats_start_end_dict["Max_temp"]=row[2]

    return jsonify(stats_start_end_dict)

if __name__ == "__main__":
    app.run(debug=True)


text:
"""Test conversion to non-parametrized types in pydatatypes.typing module."""

from typing import Any, List, Dict, Mapping, Sequence
from collections import OrderedDict

import pytest

from pydatatypes.typing import NoneType, TypeConversionError
from pydatatypes.test import assert_convert_success, assert_convert_failure, ExampleValues

# Quick examples for type checking and conversion (mutually exclusive partitions)
EXAMPLES = {
	'int': [0, 1, -1, 42],
	'float': [1.0, 5.234, -2342.333],
	'bool': [True, False],
	'string': ['foo', ''],
	'mapping': [{}, {'foo': 1, 'bar': 'baz'}, OrderedDict({1: 'a', 2: 'b'})],
	'sequence': [[], [1, 2, 'x'], (), ('a', None), range(10)],
	'set': [set(), {1, 2, 'x'}, frozenset(), frozenset({1, 2, 'x'})],
	'none': [None],
	'other': [slice(1, 3), Exception("Shouldn't work"), lambda x: x + 1],
}

# Aliases for groups of EXAMPLES keys
EXAMPLE_ALIASES = {
	'collection': {'mapping', 'sequence', 'set'},
	'all': set(EXAMPLES.keys()),
}

def check_convert(examples, converter, type_, keys, *, omit=None, eq=True):
	"""
	Check conversion of all example values to type_. Examples from keys in
	keys are expected to work, the others are expected to fail.
	"""
	# Expected to work
	for val in examples.values(keys, omit=omit):
		assert_convert_success(converter, val, type_, eq=eq)

	# Expected to fail
	for val in examples.values(keys, complement=True, omit=omit):
		assert_convert_failure(converter, val, type_)

def check_isinstance(examples, converter, type_, keys, *, omit=None):
	"""
	Test instance checking for all example values to type_. Examples from keys
	in "keys" are expected to work, the others are expected to fail.
	"""

	# Expected to work
	for val in examples.values(keys, omit=omit):
		assert converter.isinstance(val, type_)
		converter.ensure_isinstance(val, type_)

	# Expected to fail
	for val in examples.values(keys, complement=True, omit=omit):
		assert not converter.isinstance(val, type_)

		with pytest.raises(TypeConversionError):
			converter.ensure_isinstance(val, type_)

@pytest.fixture()
def examples():
	return ExampleValues(EXAMPLES, EXAMPLE_ALIASES)

def test_any(converter, examples):
	check_isinstance(examples, converter, Any, ['all'])

	# Conversion should return exact same object
	for value in examples.values(['all']):
		assert converter.convert(value, Any) is value

def test_none(converter, examples):
	check_isinstance(examples, converter, NoneType, ['none'])
	check_convert(examples, converter, NoneType, ['none'])

def test_int(converter, examples):
	check_isinstance(examples, converter, int, ['int', 'bool'])
	check_convert(examples, converter, int, ['int', 'bool'])

def test_float(converter, examples):
	check_isinstance(examples, converter, float, ['float'])
	check_convert(examples, converter, float, ['float'], omit=['int', 'bool'])

def test_bool(converter, examples):
	check_isinstance(examples, converter, bool, ['bool'])
	check_convert(examples, converter, bool, ['bool'])

def test_str(converter, examples):
	check_isinstance(examples, converter, str, ['string'])
	check_convert(examples, converter, str, ['string'])

@pytest.mark.parametrize('type_', [list, List, Sequence])
def test_convert_list(converter, examples, type_):

	# Check conversion failure on all non-sequences
	check_convert(examples, converter, type_, [], omit=['sequence'])

	# Check sequence conversion
	for value in examples.values(['sequence']):
		converted = converter.convert(value, type_)
		assert isinstance(converted, list)
		assert converted == list(value)

@pytest.mark.parametrize('type_', [dict, Dict, Mapping])
def test_convert_dict(converter, examples, type_):

	# Check conversion failure on all non-mappings
	check_convert(examples, converter, type_, [], omit=['mapping'])

	# Check mapping conversion
	for value in examples.values(['mapping']):
		converted = converter.convert(value, type_)
		assert isinstance(converted, dict)
		assert converted == dict(value)

def test_is_collection_type(converter):
	types = [list, List, dict, Dict, Sequence, Mapping]
	values = [[], [1, 2], range(3), dict(x=3), OrderedDict(), set()]

	# Non-parametrized types, should mirror builtin isinstance()
	for type_ in types:
		for value in values:
			assert converter.isinstance(value, type_) == isinstance(value, type_)

def test_convert_numpy(converter):
	"""Test conversion of numpy scalars to Python ints and floats."""
	import numpy as np

	for inttype in [np.int8, np.int16, np.int32, np.int64]:
		np_n = inttype(3)
		n = converter.convert(np_n, int)
		assert isinstance(n, int)
		assert n == np_n

	for floattype in [np.float32, np.float64]:
		np_x = floattype(3)
		x = converter.convert(np_x, float)
		assert isinstance(x, float)
		assert x == np_x

max_stars_repo_name: jlumpe/pydatatypes

max_issues_repo_name: jlumpe/pydatatypes

max_forks_repo_name: jlumpe/pydatatypes


text:
""" Proximal operators prox_g and corresponding functions g """

# Author: Remi Flamary <remi.flamary@unice.fr>
#
# License: MIT License

import numpy as np

def reg_l1(x, lambd=1, **kwargs):
    """
    l1 regularization (LASSO)

    x : vector to project
    lambd: regularization term
    """
    return lambd * np.sum(np.abs(x))

def prox_l1(x, lambd=1, **kwargs):
    """
    l1 regularization proximal operator (LASSO)

    x : vector to project
    lambd: regularization term
    """
    return np.sign(x) * np.maximum(np.abs(x) - lambd, 0)

def reg_l2(x, lambd=1, **kwargs):
    """
    l2 regularization (ridge)

    x : vector to project
    lambd: regularization term
    """
    return lambd * np.sum(x**2) / 2

def prox_l2(x, lambd=1, **kwargs):
    """
    l2 regularization proximity operator (ridge)

    x : vector to project
    lambd: reg
    """
    return x / (1 + lambd)

max_stars_repo_name: vishalbelsare/PyOptim

max_issues_repo_name: vishalbelsare/PyOptim

max_forks_repo_name: vishalbelsare/PyOptim