from django.test import TestCase
from django.core.management import call_command
from mock import Mock, patch
from email_subscription.models import EmailMessage
from email_subscription import subscribe, unsubscribe, get_subscribed
from ott.models import User
from django.contrib.auth.models import Group

class EmailSubscriptionTest(TestCase):
    fixtures=['test_email_subscription.json']

    def test_initialization(self):
        self.assertEquals(2, EmailMessage.objects.count())
        message_pk = EmailMessage.objects.values_list('pk', flat=True)[0]
        call_command('init_email_subscription', interactive=False)
        self.assertGreater(EmailMessage.objects.count(), 2)
        self.assertTrue(0 == EmailMessage.objects.filter(pk = message_pk).count())

    def test_subscription(self):
        em = EmailMessage.objects.create(name='dumb', template='dumberer')
        em.groups = Group.objects.filter(pk__in = [1])
        em.save()
        self.assertEquals([9998], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

        subscribe(User.objects.get(pk=9999), em)
        self.assertEquals([9998], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

        u = User.objects.get(pk=9999)
        u.groups=Group.objects.filter(pk=1)
        u.save()
        subscribe(User.objects.get(pk=9999), em)
        self.assertEquals([9998, 9999], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

        unsubscribe(User.objects.get(pk=9998), em)
        self.assertEquals([9999], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

    def test_signals(self):
        em = EmailMessage.objects.get(pk=2)
        self.assertEquals([9998], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

        em.groups = Group.objects.filter(pk__in=[1, 4])
        em.save()

        self.assertEquals([9998, 9999], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))

        em.groups = Group.objects.filter(pk__in=[4])
        em.save()

        self.assertEquals([9999], map(lambda u: u.pk, get_subscribed(User.objects.filter(pk__in=[9998, 9999]), em.template)[0][1]))


# -*- coding: utf-8 -*-
import math
import numpy as np
import chainer, os, collections, six
from chainer import cuda, Variable, optimizers, serializers, function, optimizer
from chainer.utils import type_check
from chainer import functions as F
from chainer import links as L
import aae
import aae_style
from aae import activations, GradientClipping

class Conf(aae.Conf):
	def __init__(self):
		super(Conf, self).__init__()
		# number of category
		self.ndim_y = 10
		self.learning_rate_for_reconstruction_cost = 0.001
		self.learning_rate_for_adversarial_cost = 0.001
		self.learning_rate_for_semi_supervised_cost = 0.01
		self.momentum_for_reconstruction_cost = 0.9
		self.momentum_for_adversarial_cost = 0.1
		self.momentum_for_semi_supervised_cost = 0.9

		self.autoencoder_x_y_sample_y = False
		self.generator_shared_hidden_units = [500]

		self.discriminator_y_hidden_units = [500]
		self.discriminator_y_activation_function = "softplus"
		self.discriminator_y_apply_dropout = False
		self.discriminator_y_apply_batchnorm = False
		self.discriminator_y_apply_batchnorm_to_input = False

class AAE(aae_style.AAE):

	def __init__(self, conf, name="aae"):
		conf.check()
		self.conf = conf
		self.generator_x_yz, self.decoder_yz_x, self.discriminator_z, self.discriminator_y = self.build()
		self.name = name

		self.optimizer_generator_x_yz = optimizers.Adam(alpha=conf.learning_rate, beta1=conf.gradient_momentum)
		self.optimizer_generator_x_yz.setup(self.generator_x_yz)
		# self.optimizer_generator_x_yz.add_hook(optimizer.WeightDecay(0.00001))
		self.optimizer_generator_x_yz.add_hook(GradientClipping(conf.gradient_clipping))

		self.optimizer_decoder_yz_x = optimizers.Adam(alpha=conf.learning_rate, beta1=conf.gradient_momentum)
		self.optimizer_decoder_yz_x.setup(self.decoder_yz_x)
		# self.optimizer_decoder_yz_x.add_hook(optimizer.WeightDecay(0.00001))
		self.optimizer_decoder_yz_x.add_hook(GradientClipping(conf.gradient_clipping))

		self.optimizer_discriminator_z = optimizers.Adam(alpha=conf.learning_rate, beta1=conf.gradient_momentum)
		self.optimizer_discriminator_z.setup(self.discriminator_z)
		# self.optimizer_discriminator_z.add_hook(optimizer.WeightDecay(0.00001))
		self.optimizer_discriminator_z.add_hook(GradientClipping(conf.gradient_clipping))

		self.optimizer_discriminator_y = optimizers.Adam(alpha=conf.learning_rate, beta1=conf.gradient_momentum)
		self.optimizer_discriminator_y.setup(self.discriminator_y)
		# self.optimizer_discriminator_y.add_hook(optimizer.WeightDecay(0.00001))
		self.optimizer_discriminator_y.add_hook(GradientClipping(conf.gradient_clipping))

	def update_learning_rate(self, lr):
		self.optimizer_generator_x_yz.alpha = lr
		self.optimizer_discriminator_z.alpha = lr
		self.optimizer_discriminator_y.alpha = lr
		self.optimizer_decoder_yz_x.alpha = lr

	def update_momentum(self, momentum):
		self.optimizer_generator_x_yz.beta1 = momentum
		self.optimizer_discriminator_z.beta1 = momentum
		self.optimizer_discriminator_y.beta1 = momentum
		self.optimizer_decoder_yz_x.beta1 = momentum

	def build(self):
		wscale = 1

		generator_x_yz = self.build_generator_x_yz()
		decoder_yz_x = self.build_decoder_yz_x()
		discriminator_z = self.build_discriminator_z()
		discriminator_y = self.build_discriminator_y()

		return generator_x_yz, decoder_yz_x, discriminator_z, discriminator_y

	def build_generator_x_yz(self):
		conf = self.conf

		generator_attributes = {}
		generator_shared_units = [(conf.ndim_x, conf.generator_shared_hidden_units[0])]
		generator_shared_units += zip(conf.generator_shared_hidden_units[:-1], conf.generator_shared_hidden_units[1:])

		for i, (n_in, n_out) in enumerate(generator_shared_units):
			generator_attributes["shared_layer_%i" % i] = L.Linear(n_in, n_out, wscale=conf.wscale)
			if conf.batchnorm_before_activation:
				generator_attributes["shared_batchnorm_%i" % i] = L.BatchNormalization(n_out)
			else:
				generator_attributes["shared_batchnorm_%i" % i] = L.BatchNormalization(n_in)

		if len(conf.generator_hidden_units)  == 0:
			raise Exception()

		generator_units = [(conf.generator_shared_hidden_units[-1], conf.generator_hidden_units[0])]
		generator_units += zip(conf.generator_hidden_units[:-1], conf.generator_hidden_units[1:])

		for i, (n_in, n_out) in enumerate(generator_units):
			generator_attributes["layer_classifier_%i" % i] = L.Linear(n_in, n_out, wscale=conf.wscale)
			generator_attributes["layer_style_%i" % i] = L.Linear(n_in, n_out, wscale=conf.wscale)
			if conf.batchnorm_before_activation:
				generator_attributes["batchnorm_classifier_%i" % i] = L.BatchNormalization(n_out)
				generator_attributes["batchnorm_style_%i" % i] = L.BatchNormalization(n_out)
			else:
				generator_attributes["batchnorm_classifier_%i" % i] = L.BatchNormalization(n_in)
				generator_attributes["batchnorm_style_%i" % i] = L.BatchNormalization(n_in)

		if conf.q_z_x_type == aae.Q_Z_X_TYPE_DETERMINISTIC:
			generator_attributes["layer_output_classifier"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_y, wscale=conf.wscale)
			generator_attributes["layer_output_style"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_z, wscale=conf.wscale)
			generator_x_yz = SemiSupervisedMLPGenerator(**generator_attributes)

		elif conf.q_z_x_type == aae.Q_Z_X_TYPE_GAUSSIAN:
			generator_attributes["layer_output_classifier"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_y, wscale=conf.wscale)
			generator_attributes["layer_output_style_mean"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_z, wscale=conf.wscale)
			generator_attributes["layer_output_style_var"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_z, wscale=conf.wscale)
			generator_x_yz = SemiSupervisedGaussianGenerator(**generator_attributes)

		elif conf.q_z_x_type == aae.Q_Z_X_TYPE_UNIVERSAL_APPROXIMATOR_POSTERIOR:
			# generator_attributes["layer_output_classifier"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_y, wscale=conf.wscale)
			# generator_attributes["layer_output_style"] = L.Linear(conf.generator_hidden_units[-1], conf.ndim_z, wscale=conf.wscale)
			# generator_x_yz = UniversalApproximatorGenerator(**generator_attributes)
			raise Exception()

		else:
			raise Exception()

		generator_x_yz.n_layers = len(generator_units)
		generator_x_yz.n_shared_layers = len(generator_shared_units)
		generator_x_yz.activation_function = conf.generator_activation_function
		generator_x_yz.apply_dropout = conf.generator_apply_dropout
		generator_x_yz.apply_batchnorm = conf.generator_apply_batchnorm
		generator_x_yz.apply_batchnorm_to_input = conf.generator_apply_batchnorm_to_input
		generator_x_yz.batchnorm_before_activation = conf.batchnorm_before_activation

		if conf.gpu_enabled:
			generator_x_yz.to_gpu()

		return generator_x_yz

	def build_discriminator_y(self):
		conf = self.conf

		discriminator_y_attributes = {}
		discriminator_y_units = [(conf.ndim_y, conf.discriminator_y_hidden_units[0])]
		discriminator_y_units += zip(conf.discriminator_y_hidden_units[:-1], conf.discriminator_y_hidden_units[1:])
		discriminator_y_units += [(conf.discriminator_y_hidden_units[-1], 2)]
		for i, (n_in, n_out) in enumerate(discriminator_y_units):
			discriminator_y_attributes["layer_%i" % i] = L.Linear(n_in, n_out, wscale=conf.wscale)
			if conf.batchnorm_before_activation:
				discriminator_y_attributes["batchnorm_%i" % i] = L.BatchNormalization(n_out)
			else:
				discriminator_y_attributes["batchnorm_%i" % i] = L.BatchNormalization(n_in)

		discriminator_y = aae.SoftmaxClassifier(**discriminator_y_attributes)
		discriminator_y.n_layers = len(discriminator_y_units)
		discriminator_y.activation_function = conf.discriminator_y_activation_function
		discriminator_y.apply_dropout = conf.discriminator_y_apply_dropout
		discriminator_y.apply_batchnorm = conf.discriminator_y_apply_batchnorm
		discriminator_y.apply_batchnorm_to_input = conf.discriminator_y_apply_batchnorm_to_input
		discriminator_y.batchnorm_before_activation = conf.batchnorm_before_activation

		if conf.gpu_enabled:
			discriminator_y.to_gpu()

		return discriminator_y

	def encode_x_yz(self, x, noise=None, test=True, apply_f=True):
		if isinstance(self.generator_x_yz, aae.UniversalApproximatorGenerator):
			y_distribution, z = self.generator_x_yz(x, test=test, apply_f=apply_f, noise=noise)
		elif isinstance(self.generator_x_yz, SemiSupervisedGaussianGenerator):
			if apply_f:
				y_distribution, z = self.generator_x_yz(x, test=test, apply_f=apply_f)
			else:
				y_distribution, z_mean, z_ln_var = self.generator_x_yz(x, test=test, apply_f=apply_f)
				return y_distribution, z_mean, z_ln_var
		else:
			y_distribution, z = self.generator_x_yz(x, test=test, apply_f=apply_f)

		return y_distribution, z

	def sample_y(self, y_distribution, argmax=False, test=False):
		if isinstance(y_distribution, Variable):
			y_distribution = y_distribution.data

		batchsize = y_distribution.shape[0]
		n_labels = y_distribution.shape[1]
		if self.gpu_enabled:
			y_distribution = cuda.to_cpu(y_distribution)
		sampled_y = np.zeros((batchsize, n_labels), dtype=np.float32)
		if argmax:
			args = np.argmax(y_distribution, axis=1)
			for b in xrange(batchsize):
				sampled_y[b, args[b]] = 1
		else:
			for b in xrange(batchsize):
				label_id = np.random.choice(np.arange(n_labels), p=y_distribution[b])
				sampled_y[b, label_id] = 1
		sampled_y = Variable(sampled_y)
		if self.gpu_enabled:
			sampled_y.to_gpu()
		return sampled_y

	def sample_x_label(self, x, argmax=True, test=False):
		batchsize = x.data.shape[0]
		y_distribution, _ = self.generator_x_yz(x, test=test, apply_f=True)
		y_distribution = y_distribution.data
		n_labels = y_distribution.shape[1]
		if self.gpu_enabled:
			y_distribution = cuda.to_cpu(y_distribution)
		if argmax:
			sampled_label = np.argmax(y_distribution, axis=1)
		else:
			sampled_label = np.zeros((batchsize,), dtype=np.int32)
			labels = np.arange(n_labels)
			for b in xrange(batchsize):
				label_id = np.random.choice(labels, p=y_distribution[b])
				sampled_label[b] = label_id
		return sampled_label

	def zero_grads(self):
		self.optimizer_generator_x_yz.zero_grads()
		self.optimizer_decoder_yz_x.zero_grads()
		self.optimizer_discriminator_z.zero_grads()
		self.optimizer_discriminator_y.zero_grads()

	def update_autoencoder(self):
		self.optimizer_generator_x_yz.update()
		self.optimizer_decoder_yz_x.update()

	def update_discriminator(self):
		self.optimizer_discriminator_z.update()
		self.optimizer_discriminator_y.update()

	def update_generator(self):
		self.optimizer_generator_x_yz.update()

	def update_classifier(self):
		self.optimizer_generator_x_yz.update()

	def loss_autoencoder_unsupervised(self, x, noise=None):
		if isinstance(self.generator_x_yz, aae.UniversalApproximatorGenerator):
			y_distribution, z = self.generator_x_yz(x, test=False, apply_f=True, noise=noise)
		else:
			y_distribution, z = self.generator_x_yz(x, test=False, apply_f=True)

		if self.conf.autoencoder_x_y_sample_y:
			y = self.sample_y(y_distribution, argmax=False, test=False)
			_x = self.decoder_yz_x(y, z, test=False, apply_f=True)
		else:
			_x = self.decoder_yz_x(y_distribution, z, test=False, apply_f=True)

		return F.mean_squared_error(x, _x)

	def train_autoencoder_unsupervised(self, x, noise=None):
		loss = self.loss_autoencoder_unsupervised(x, noise=noise)

		self.zero_grads()
		loss.backward()
		self.update_autoencoder()

		if self.gpu_enabled:
			loss.to_cpu()

		return float(loss.data)

	def loss_classifier(self, x, label, noise=None):
		if isinstance(self.generator_x_yz, aae.UniversalApproximatorGenerator):
			y_raw_output, z = self.generator_x_yz(x, test=False, apply_f=False, noise=noise)
		elif isinstance(self.generator_x_yz, SemiSupervisedGaussianGenerator):
			y_raw_output, z_mean, z_ln_var = self.generator_x_yz(x, test=False, apply_f=False)
		else:
			y_raw_output, z = self.generator_x_yz(x, test=False, apply_f=False)

		return F.softmax_cross_entropy(y_raw_output, label)

	def train_classifier(self, x, label, noise=None):
		loss = self.loss_classifier(x, label, noise=noise)

		self.zero_grads()
		loss.backward()
		self.update_classifier()

		if self.gpu_enabled:
			loss.to_cpu()

		return float(loss.data)

	def loss_generator_x_yz(self, x, noise=None):
		xp = self.xp

		# We fool discriminator into thinking that z_fake comes from the true prior distribution.
		if isinstance(self.generator_x_yz, aae.UniversalApproximatorGenerator):
			y_distribution_fake, z_fake = self.generator_x_yz(x, test=False, apply_f=True, noise=noise)
		else:
			y_distribution_fake, z_fake = self.generator_x_yz(x, test=False, apply_f=True)

		p_fake_z = self.discriminator_z(z_fake, softmax=False, test=False)

		# y_fake = self.sample_y(y_distribution_fake, argmax=False, test=False)
		p_fake_y = self.discriminator_y(y_distribution_fake, softmax=False, test=False)

		# 0: Samples from true distribution
		# 1: Samples from generator
		loss_z = F.softmax_cross_entropy(p_fake_z, Variable(xp.zeros(p_fake_z.data.shape[0], dtype=np.int32)))
		loss_y = F.softmax_cross_entropy(p_fake_y, Variable(xp.zeros(p_fake_y.data.shape[0], dtype=np.int32)))

		return loss_z + loss_y

	def train_generator_x_yz(self, x, noise=None):
		loss = self.loss_generator_x_yz(x, noise=noise)

		self.zero_grads()
		loss.backward()
		self.update_generator()

		if self.gpu_enabled:
			loss.to_cpu()

		return float(loss.data)

	def loss_discriminator_yz(self, x, y_true, z_true, noise=None):
		xp = self.xp

		# y_true came from true prior distribution
		p_true_y = self.discriminator_y(y_true, softmax=False, test=False)

		# z_true came from true prior distribution
		p_true_z = self.discriminator_z(z_true, softmax=False, test=False)

		# 0: Samples from true distribution
		# 1: Samples from generator
		loss_true_y = F.softmax_cross_entropy(p_true_y, Variable(xp.zeros(p_true_y.data.shape[0], dtype=np.int32)))
		loss_true_z = F.softmax_cross_entropy(p_true_z, Variable(xp.zeros(p_true_z.data.shape[0], dtype=np.int32)))

		# z_fake was generated by generator
		if isinstance(self.generator_x_yz, aae.UniversalApproximatorGenerator):
			y_distribution_fake, z_fake = self.generator_x_yz(x, test=False, apply_f=True, noise=noise)
		else:
			y_distribution_fake, z_fake = self.generator_x_yz(x, test=False, apply_f=True)

		# y_fake = self.sample_y(y_distribution_fake, argmax=False, test=False)
		p_fake_y = self.discriminator_y(y_distribution_fake, softmax=False, test=False)
		loss_fake_y = F.softmax_cross_entropy(p_fake_y, Variable(xp.ones(p_fake_y.data.shape[0], dtype=np.int32)))

		p_fake_z = self.discriminator_z(z_fake, softmax=False, test=False)
		loss_fake_z = F.softmax_cross_entropy(p_fake_z, Variable(xp.ones(p_fake_z.data.shape[0], dtype=np.int32)))

		return loss_true_y + loss_fake_y + loss_true_z + loss_fake_z

	def train_discriminator_yz(self, x, y_true, z_true, noise=None):
		loss = self.loss_discriminator_yz(x, y_true, z_true, noise=noise)

		self.zero_grads()
		loss.backward()
		self.update_discriminator()

		if self.gpu_enabled:
			loss.to_cpu()

		return float(loss.data) / 2.0

class SemiSupervisedMLPGenerator(aae.MultiLayerPerceptron):

	def compute_hidden_output(self, x, test=False):
		f = activations[self.activation_function]
		chain = [x]

		# Shared units
		for i in range(self.n_shared_layers):
			u = chain[-1]
			if self.batchnorm_before_activation:
				u = getattr(self, "shared_layer_%i" % i)(u)

			if i == 0:
				if self.apply_batchnorm_to_input:
					u = getattr(self, "shared_batchnorm_%d" % i)(u, test=test)
			else:
				if self.apply_batchnorm:
					u = getattr(self, "shared_batchnorm_%d" % i)(u, test=test)

			if self.batchnorm_before_activation == False:
				u = getattr(self, "shared_layer_%i" % i)(u)

			output = f(u)

			if self.apply_dropout:
				output = F.dropout(output, train=not test)

			chain.append(output)

		chain_classifier = [chain[-1]]
		chain_style = [chain[-1]]

		for i in range(self.n_layers):
			u_classifier = chain_classifier[-1]
			u_style = chain_style[-1]

			if self.batchnorm_before_activation:
				u_classifier = getattr(self, "layer_classifier_%i" % i)(u_classifier)
				u_style = getattr(self, "layer_style_%i" % i)(u_style)

			if self.apply_batchnorm:
				u_classifier = getattr(self, "batchnorm_classifier_%d" % i)(u_classifier, test=test)
				u_style = getattr(self, "batchnorm_style_%d" % i)(u_style, test=test)

			if self.batchnorm_before_activation == False:
				u_classifier = getattr(self, "layer_classifier_%i" % i)(u_classifier)
				u_style = getattr(self, "layer_style_%i" % i)(u_style)

			output_classifier = f(u_classifier)
			output_style = f(u_style)

			if self.apply_dropout:
				output_classifier = F.dropout(output_classifier, train=not test)
				output_style = F.dropout(output_style, train=not test)

			chain_classifier.append(output_classifier)
			chain_style.append(output_style)

		return chain_classifier[-1], chain_style[-1]

	def forward_one_step(self, x, test=False):
		output_classifier, output_style = self.compute_hidden_output(x, test=test)
		output_classifier = self.layer_output_classifier(output_classifier)
		output_style = self.layer_output_style(output_style)
		return output_classifier, output_style

	def __call__(self, x, test=False, apply_f=True):
		output_classifier, output_style = self.forward_one_step(x, test=test)
		if apply_f:
			output_classifier = F.softmax(output_classifier)
		return output_classifier, output_style

class SemiSupervisedGaussianGenerator(SemiSupervisedMLPGenerator):

	def forward_one_step(self, x, test=False, clip=True):
		output_classifier, output_style = self.compute_hidden_output(x, test=test)
		output_classifier = self.layer_output_classifier(output_classifier)
		style_mean = self.layer_output_style_mean(output_style)
		style_ln_var = self.layer_output_style_var(output_style)

		# avoid nan
		if clip:
			clip_min = math.log(0.001)
			clip_max = math.log(10)
			style_ln_var = F.clip(style_ln_var, clip_min, clip_max)

		return output_classifier, style_mean, style_ln_var

	def __call__(self, x, test=False, apply_f=True):
		output_classifier, style_mean, style_ln_var = self.forward_one_step(x, test=test, clip=True)
		if apply_f:
			output_style = F.gaussian(style_mean, style_ln_var)
			output_classifier = F.softmax(output_classifier)
			return output_classifier, output_style
		return output_classifier, style_mean, style_ln_var


import numpy as np
from math import sin,cos,pi
from science import deselect_all, makeMaterial, setMaterial
from scipy import interpolate # need to activate correct yt version and do: pip3.4 install scipy

# what are two positions to make vector -> this will be our line segments
#p1 = [1,1,0]
#p2 = [1,0,0]

#p1 = [1,1,0]
#p2 = [0,0,1]

# each entry is x/y/z pt on line segement
#line_pts = np.array( ([0,0,0],[0,0,1], [1,0,1], [1,1,3]) )

# each entry is r/g/b values for each line segement
#colors = np.array( ([1,0,0], [0,1,0], [0,0,1]) )
#colorindex = np.array( [0,1,2] ) #, 1])

# how many faces for each cylinder?
nfaces = 16

# radius of cylinders (in BU now)?
r_cyl = 0.02

# maybe to add later: emissivities and transpariencies
#------------------------------------------
# streamlines stuff

import yt
from yt.visualization.api import Streamlines
import random

fname = '/Users/jillnaiman1/data/IsolatedGalaxy/galaxy0030/galaxy0030' # home

# now, get stream line
ds = yt.load(fname)

c = np.array([0.5]*3)
N = 10  # number of stream lines
scale = 1.0
pos_dx = np.random.random((N,3))*scale-scale/2.
pos = c+pos_dx

streamlines = Streamlines(ds,pos,'velocity_x', 'velocity_y', 'velocity_z', length=1.0)

streamlines.integrate_through_volume()

# select out 1 stream... for now
stream = streamlines.streamlines[1]
stream = stream[np.all(stream != 0.0, axis=1)] # doen something fancy

# units
stream = stream*ds.length_unit # into cm
# now, scale down to what we want
stream = stream/yt.units.cm
stream = stream/3.1e18 # pc

# for this case
stream = stream/1e5

x = stream[:,0]
y = stream[:,1]
z = stream[:,2]

#x = x - x[0]
#y = y - y[0]
#z = z - z[0]

# this will eventually be filled by an actual colormap
#  from data taken on the steamline
colors = np.zeros((len(stream),3))
for i in range(0,len(stream)):
    colors[i,0] = random.random() #r
    colors[i,1] = random.random() #g
    colors[i,2] = random.random() #b

max_nvals = 10 # interpolate down to this

# remap to a smaller number of points for simplicity
Nvals = max(max_nvals,floor(len(stream)/20.0))
#method='cubic' # or nearest, or linear - cubic takes awhile
method='linear' # or nearest, or linear
xn = np.linspace(x.min(),x.max(),Nvals)

# y(x)
f = interpolate.interp1d(x,y,kind=method)
yn = f(xn)

# z(x)
f = interpolate.interp1d(x,z,kind=method)
zn = f(xn)

# also remap colors
f = interpolate.interp1d(x,colors[:,0],kind=method)
r = f(xn)
f = interpolate.interp1d(x,colors[:,1],kind=method)
g = f(xn)
f = interpolate.interp1d(x,colors[:,2],kind=method)
b = f(xn)

rgb = []
for i in range(0,Nvals):
    rgb.append([r[i],g[i],b[i]])

# put back in a smaller stream
streamn = np.zeros((Nvals,3))

streamn[:,0] = xn
streamn[:,1] = yn
streamn[:,2] = zn

#-----------------------------------------
# fix this
line_pts = streamn
colors = rgb
colorindex = np.zeros(len(rgb))
for i in range(0,len(colorindex)):
    colorindex[i] = i

#------------------------------------------

def rotation_matrix(rotation_angle, u):
    #import numpy as np
    from numpy import matrix
    from math import sin, cos
    # find new x/y/z of face, start & end pts
    st = sin(rotation_angle*pi/180.0)
    ct = cos(rotation_angle*pi/180.0)
    # normalize rotation vector
    u = u/(u[0]**2. + u[1]**2. + u[2]**2.)**0.5
    # for ease of writing
    ux = u[0]
    uy = u[1]
    uz = u[2]
    # return matrix
    mat = matrix( ((ct+ux**2.*(1.-ct),ux*uy*(1.0-ct)-uz*st,ux*uz*(1.-ct)+uy*st),
                   (uy*ux*(1.0-ct)+uz*st,ct+uy**2.0*(1.-ct),uy*uz*(1.-ct)-ux*st),
                   (uz*ux*(1.-ct)-uy*st,uz*uy*(1.-ct)+ux*st,ct+uz**2.*(1-ct))) )
    return mat

#------------------------------------------

vtype = [("x","float"),("y","float"), ("z","float")]
# each line segment is surrounded by a cylinder
#  made up of rectangles, nfaces number of rectangles to be exact
#  each face is defined by 4 verticies which is why we have the *4 there...
#  this might be inefficient... but I think its also the way yt does it...
vertices = np.empty((nfaces*4)*(len(line_pts)-1), dtype=vtype) # stores vertices
vertices[:][:] = 0.0

# for saving upper faces

vnum = 0
#for i in range(0,len(line_pts)-1):
for i in range(0,10):
    # define the vector of the line segement, origion at the 1st pt
    u1 = [line_pts[i+1][0]-line_pts[i][0],
         line_pts[i+1][1]-line_pts[i][1],
         line_pts[i+1][2]-line_pts[i][2]]
    # find relations for perpendicular point
    a = 1.0 # these can be anything
    b = 1.0 # these can be anything
    uperp1 = [(u1[1]*a-u1[2]*b), (-u1[0]*a), (u1[0]*b)]
    # save upper faces
    #upperfaces[:] = 0.0
    # calculate rotation angle based on # of faces
    if i == 0: # for first, calculate all verticles
    #if True:
        for j in range(0,nfaces):
            rotation_angle_start = 360.0/nfaces*j
            rotation_angle_end = 360.0/nfaces*(j+1.0)
            # 1st matrix for first vertex
            matrix = rotation_matrix(rotation_angle_start, u1)
            # rotate uperp1 around u1 vector
            rv = np.dot(matrix,uperp1)
            # formatting
            v1 = np.array(rv)
            v1 = v1[0]
            # add to verticies
            # moar formating
            v1[np.abs(v1) < 1e-10] = 0.0
            vertices[:][vnum] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i]
            vnum += 1
            #
            # 2nd matrix for 2nd vertex
            vertices[:][vnum] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i+1]
            vnum += 1
            #
            # 4rd matrix for 4rd & 3rd vertex
            matrix = rotation_matrix(rotation_angle_end, u1)
            # rotate uperp1 around u1 vector
            rv = np.dot(matrix,uperp1)
            # formatting
            v1 = np.array(rv)
            v1 = v1[0]
            # add to verticies
            v1[np.abs(v1) < 1e-10] = 0.0
            # 3rd matrix for 3rd vertex
            vertices[:][vnum] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i+1]
            vnum += 1
            # 4th vertex
            vertices[:][vnum] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i]
            vnum += 1
    else: # fill with previous verts for smooth looking things
        for j in range(0,nfaces*4):
            print("j = " + str(j))
            if (j % 4) == 0: # if we are on a repeated face (new lower face) ("vertex 1")
                #vertices[:][vnum] = vertices[:][(vnum-nfaces*4) + j + 1]
                vertices[:][vnum+j] = vertices[:][(vnum-nfaces*4) + j]
                print("in first if")
            elif ((j+1) % 4) == 0: # if we are on a repeated face, 1 up ("vertex 4")
                #vertices[:][vnum+3] = vertices[:][(vnum-nfaces*4) + j - 1]
                vertices[:][vnum+3] = vertices[:][(vnum-nfaces*4) + j]
                print("in 2nd if")
            elif ((j-1) % 4) == 0: # if "vertex 2"
                rotation_angle_start = 360.0/nfaces*(j%4)
                matrix = rotation_matrix(rotation_angle_start, u1)
                # rotate uperp1 around u1 vector
                rv = np.dot(matrix,uperp1)
                # formatting
                v1 = np.array(rv)
                v1 = v1[0]
                # add to verticies
                # moar formating
                v1[np.abs(v1) < 1e-10] = 0.0
                vertices[:][vnum+1] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i+1]
                print("in 3rd f")
            elif ((j+2) % 4) == 0: # "vertex 3"
                rotation_angle_end = 360.0/nfaces*(j%4+1.0)
                matrix = rotation_matrix(rotation_angle_end, u1)
                # rotate uperp1 around u1 vector
                rv = np.dot(matrix,uperp1)
                # formatting
                v1 = np.array(rv)
                v1 = v1[0]
                # add to verticies
                v1[np.abs(v1) < 1e-10] = 0.0
                # 3rd matrix for 3rd vertex
                vertices[:][vnum+2] = v1/(v1[0]**2.+v1[1]**2.+v1[2]**2)**0.5*r_cyl + line_pts[i+1]
                print("in 4th if")
            vnum = vnum+4

meshname = 'mymesh'
#meshlocation = (vertices['x'][0], vertices['y'][0], vertices['z'][0])
meshlocation = (0, 0, 0)
plot_index = 0

# now, generate mesh... hopefully
deselect_all()
nftype = [("f1","int"),("f2","int"),("f3","int"),("f4","int")]
#newfaces = np.empty(int(len(vertices)/3.), dtype=nftype) # store sets of face colors
newfaces = np.empty(int(len(vertices)/4.), dtype=nftype)
cc = 0
for i in range(0,int(len(vertices)/4.)):
    newfaces[i] = (cc, cc+1, cc+2, cc+3)
    cc = cc+4

me = bpy.data.meshes.new(meshname+"Mesh")
ob = bpy.data.objects.new(meshname,me)
ob.location = meshlocation   # position object at 3d-cursor
bpy.context.scene.objects.link(ob)                # Link object to scene
# Fill the mesh with verts, edges, faces
me.from_pydata(vertices.tolist(),[],newfaces.tolist())
me.update(calc_edges=True)    # Update mesh with new data

# materials woop woop!
obj = bpy.data.objects[meshname]
bpy.context.scene.objects.active=obj
for i in range(0,len(colors)):
    mat = makeMaterial(meshname+str(i)+'_'+str(plot_index),
                       (colors[i][0],colors[i][1],colors[i][2]),
                       (1,1,1), 1.0, 0.0)
    setMaterial(obj,mat)

fnum = 0
for p in range(0,len(line_pts)-1):
    for i in range(0,nfaces):
        me.polygons[fnum].material_index = colorindex[p]
        fnum += 1

## now, do individual mat indecies
#for i in range(0,len(newfaces)):
#    me.polygons[i].material_index = colorindex[i]


from django.test import SimpleTestCase
from networks.models import parse_opt82

class Option82TestCase(SimpleTestCase):
    def test_parse_opt82_ok(self):
        circuit_id = b'\x00\x04\x00\x98\x00\x05'
        rem_id = b'\x00\x06\xec\x22\x80\x7f\xad\xb8'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertEqual(mac, 'ec:22:80:7f:ad:b8')
        self.assertEqual(port, 5)

    def test_parse_opt82_ok2(self):
        circuit_id = b'\x00\x74\x00\x07\x1d'
        rem_id = b'\x1c\x87\x79\x12\xe6\x1a'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertEqual(mac, '1c:87:79:12:e6:1a')
        self.assertEqual(port, 29)

    def test_parse_opt82_long_data(self):
        circuit_id = b'\x00\x74\x00\xff\x1d\xff\x01'
        rem_id = b'\xff\x12\x1c\x87\x79\x12\xe6\x1a'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertEqual(mac, '1c:87:79:12:e6:1a')
        self.assertEqual(port, 1)

    def test_parse_opt82_short_data(self):
        circuit_id = b'\x00\x74\x00\xff'
        rem_id = b'\x1c\x87\x79'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertIsNone(mac)
        self.assertEqual(port, 255)

    def test_parse_opt82_ok_zte(self):
        circuit_id = b'\x5a\x54\x45\x47\x43\x30\x32\x38\x38\x45\x37\x30'
        rem_id = b'\x34\x35\x3a\x34\x37\x3a\x63\x30\x3a\x32\x38\x3a\x38\x65\x3a\x37\x30'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertEqual(mac, '45:47:c0:28:8e:70')
        self.assertEqual(port, 0)

    def test_parse_opt82_ok_zte2(self):
        circuit_id = b'\x5a\x54\x45\x47\x43\x34\x30\x32\x35\x33\x42\x33'
        rem_id = b'\x34\x35\x3a\x34\x37\x3a\x63\x34\x3a\x32\x3a\x35\x33\x3a\x62\x33'
        mac, port = parse_opt82(
            remote_id=rem_id,
            circuit_id=circuit_id
        )
        self.assertEqual(mac, '45:47:c4:2:53:b3')
        self.assertEqual(port, 0)


#!/usr/bin/python

from pisi.actionsapi import get, pisitools, shelltools
import shutil

WorkDir = "."

def install():
    icon_sizes = [
            "16x16",
            "24x24",
            "32x32",
            "48x48",
            "128x128",
            "256x256",
            "512x512",
            "1024x1024"
            ]
    pisitools.insinto("/opt/hipchat", "hipchat4-4.29.4.1662/HipChat4/*")
    for i in icon_sizes:
        pisitools.domove(
                "hipchat4-4.29.4.1662/icons/hicolor/" + i + "/apps/*",
                "/usr/share/icons/hicolor/" + i + "/apps/"
                )
    pisitools.dosym("/opt/hipchat/bin/HipChat4", "/usr/bin/hipchat")


from odoo import api, fields, models, tools, _
import base64

class MedicalPatient(models.Model):
    _inherit = "medical.patient"

    digital_signature = fields.Binary(string='Signature')

    @api.multi
    def print_questionnaire(self):
        data = {'ids': self.ids}
        data, data_format = self.env.ref('patient_sign.report_sign2_pdf').render([1],data=data)
        att_id = self.env['ir.attachment'].create({
            'name': 'Questionnaire_'+self.updated_date,
            'type': 'binary',
            'datas': base64.encodestring(data),
            'datas_fname': self.name.name + '_Questionnaire.pdf',
            'res_model': 'medical.patient',
            'res_id': self.id,
            'mimetype': 'application/pdf'
        })

    @api.multi
    def action_approval_wizard(self):
        return {
            'name': _('Sign Form'),
            'type': 'ir.actions.act_window',
            'res_model': 'sign.wizard',
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new',
            'nodestroy': False,
            'context':{
                'default_patient_id': self.id,
                'default_receiving_treatment': self.receiving_treatment,
                'default_receiving_medicine': self.receiving_medicine,
                'default_medicine_yes': self.medicine_yes,
                'default_have_card': self.have_card,
                'default_card_yes': self.card_yes,
                'default_have_allergies': self.have_allergies,
                'default_allergies_yes': self.allergies_yes,
                'default_have_feaver': self.have_feaver,
                'default_have_ashtham': self.have_ashtham,
                'default_have_attacks': self.have_attacks,
                'default_attacks_yes': self.attacks_yes,
                'default_have_heart': self.have_heart,
                'default_heart_yes': self.heart_yes,
                'default_have_diabetic': self.have_diabetic,
                'default_have_arthritis': self.have_arthritis,
                'default_have_bleeding': self.have_bleeding,
                'default_bleeding_yes': self.bleeding_yes,
                'default_have_infectious': self.have_infectious,
                'default_infectious_yes': self.infectious_yes,
                'default_have_rheumatic': self.have_rheumatic,
                'default_have_liver': self.have_liver,
                'default_have_serious': self.have_serious,
                'default_have_reaction': self.have_reaction,
                'default_reaction_yes': self.reaction_yes,
                'default_have_surgery': self.have_surgery,
                'default_surgery_yes': self.surgery_yes,
                'default_have_tabacco': self.have_tabacco,
                'default_have_gutkha': self.have_gutkha,
                'default_have_medicine': self.have_medicine,
                'default_have_pregnant': self.have_pregnant,
                'default_pregnant_yes': self.pregnant_yes,
                'default_have_breastfeeding': self.have_breastfeeding,
            }
        }


from elasticsearch_dsl.query import Q

from .base import BaseSearchQueryBackend

__title__ = 'django_elasticsearch_dsl_drf.filter_backends.search.' \
            'query_backends.match'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2017-2019 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('MatchQueryBackend',)

class MatchQueryBackend(BaseSearchQueryBackend):
    """Match query backend."""

    query_type = 'match'

    @classmethod
    def construct_search(cls, request, view, search_backend):
        """Construct search.

        :param request:
        :param view:
        :param search_backend:
        :return:
        """
        query_params = search_backend.get_search_query_params(request)
        __queries = []
        for search_term in query_params:
            __values = search_backend.split_lookup_name(search_term, 1)
            __len_values = len(__values)
            if __len_values > 1:
                field, value = __values
                if field in view.search_fields:
                    # Initial kwargs for the match query
                    field_kwargs = {field: {'query': value}}
                    # In case if we deal with structure 2
                    if isinstance(view.search_fields, dict):
                        extra_field_kwargs = view.search_fields[field]
                        if extra_field_kwargs:
                            field_kwargs[field].update(extra_field_kwargs)
                    # The match query
                    __queries.append(
                        Q(cls.query_type, **field_kwargs)
                    )
            else:
                for field in view.search_fields:
                    # Initial kwargs for the match query
                    field_kwargs = {field: {'query': search_term}}

                    # In case if we deal with structure 2
                    if isinstance(view.search_fields, dict):
                        extra_field_kwargs = view.search_fields[field]
                        if extra_field_kwargs:
                            field_kwargs[field].update(extra_field_kwargs)

                    # The match query
                    __queries.append(
                        Q(cls.query_type, **field_kwargs)
                    )
        return __queries


#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Date: 2018/4/26

from collections import ChainMap

a = {"x": 1, "z": 3}

b = {"y": 2, "z": 4}

c = ChainMap(a, b)

a["a"] = 1

print(c)

b["b"] = 100

print(c)

c["a"] = 13

print(c)

del c["y"]

print(c)


number = input("enter the number: ")
print(number)

length = len(number)

# If all digits are 9, the simply add 2 to it and print
if length == number.count('9'):
    print(int(number)+2)
else:
    # If number is Even
    if length % 2 == 0:
        half = length // 2
        a = int(number[:half])
        new_number = int(str(a)+str(a)[::-1])
        new_number1 = int(str(a + 1) + str(a + 1)[::-1])
        a = abs(new_number - int(number))
        b = abs(new_number1 - int(number))
        if a < b:
            print(new_number)
        else:
            print(new_number1)
        '''
        if new_number < int(number):
            print(new_number)
        else:
            new_number = int(str(a+1) + str(a+1)[::-1])
            print(new_number)
        '''
    # If number is Odd
    else:
        half = (length // 2) + 1
        a = int(number[:half])
        new_number = int(str(a)[0:half-1] + str(a)[::-1])
        new_number1 = int(str(a + 1)[0:half - 1] + str(a + 1)[::-1])
        a = abs(new_number - int(number))
        b = abs(new_number1 - int(number))
        if a < b :
            print(new_number)
        else:
            print(new_number1)
        '''
        if new_number > int(number):
            print(new_number)
        else:
            new_number = int(str(a+1)[0:half-1] + str(a+1)[::-1])
            print(new_number)
        '''


from selenium import webdriver
from fpdf import FPDF
from PIL import Image
import sys

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, flowables

image_file = 'image.png'
pdf_file = 'output.pdf'
# print A4
# A4 = (1024, 768)
driver = webdriver.PhantomJS()
driver.set_window_size(*A4) # set the window size that you need
driver.get('http://facebook.com/')

driver.save_screenshot(image_file)

def drawPageFrame(canvas, doc):
    width, height = A4
    canvas.saveState()
    canvas.drawImage(
    image_file, 0, 0, height, width,
    preserveAspectRatio=True, anchor='c')
    canvas.restoreState()

def jpg2pdf(pdfname):
    width, height = A4
    # To make it landscape, pagesize is reversed
    # You can modify the code to add PDF metadata if you want
    doc = SimpleDocTemplate(pdfname, pagesize=(height, width))
    elem = []

    elem.append(flowables.Macro('canvas.saveState()'))
    elem.append(flowables.Macro('canvas.restoreState()'))

    doc.build(elem, onFirstPage=drawPageFrame)

jpg2pdf(pdf_file)


import argparse
import numpy as np
import tempfile

from typing import Any, Dict, List, Optional

from dnnv import logging
from dnnv.nn import OperationGraph
from dnnv.properties import Expression
from dnnv.verifiers.common import (
    SAT,
    UNSAT,
    UNKNOWN,
    CommandLineExecutor,
    HalfspacePolytope,
    HalfspacePolytopePropertyExtractor,
    HyperRectangle,
    Property,
    as_layers,
)

from .errors import NeurifyError, NeurifyTranslatorError
from .utils import to_neurify_inputs

def parse_args(args: Optional[List[str]] = None):
    parser = argparse.ArgumentParser()
    parser.add_argument("--neurify.max_depth", default=None, type=int, dest="max_depth")
    parser.add_argument(
        "--neurify.max_thread", default=None, type=int, dest="max_thread"
    )
    return parser.parse_known_args(args)

def parse_results(stdout: List[str], stderr: List[str]):
    if len(stdout) < 2:
        raise NeurifyError(f"Neurify terminated before producing expected output.")
    result = stdout[-2].strip()
    if result == "Falsified.":
        return SAT
    elif result == "Unknown.":
        return UNKNOWN
    elif result == "Proved.":
        return UNSAT
    raise NeurifyError(f"Unexpected verification result: {stdout[-1]}")

def validate_counter_example(prop: Property, stdout: List[str], stderr: List[str]):
    cex_found = False
    input_shape, input_dtype = prop.op_graph.input_details[0]
    for line in stdout:
        if cex_found:
            values = line.split(":")[-1][1:-1].split()
            cex = np.asarray([float(v) for v in values], dtype=input_dtype).reshape(
                input_shape
            )
            break
        if line.endswith("Solution:"):
            cex_found = True
    else:
        lb = prop.input_constraint.lower_bounds[0]
        ub = prop.input_constraint.upper_bounds[0]
        cex = ((lb + ub) / 2).astype(input_dtype)
    if not prop.input_constraint.validate(cex):
        raise NeurifyError("Invalid counter example found: input outside bounds.")
    output = prop.op_graph(cex)
    if not prop.output_constraint.validate(output):
        raise NeurifyError("Invalid counter example found: output outside bounds.")

def verify(dnn: OperationGraph, phi: Expression, **kwargs: Dict[str, Any]):
    logger = logging.getLogger(__name__)
    dnn = dnn.simplify()
    phi.networks[0].concretize(dnn)

    result = UNSAT
    property_extractor = HalfspacePolytopePropertyExtractor(
        HyperRectangle, HalfspacePolytope
    )
    with tempfile.TemporaryDirectory() as dirname:
        for prop in property_extractor.extract_from(~phi):
            if prop.input_constraint.num_variables > 1:
                raise NeurifyTranslatorError(
                    "Unsupported network: More than 1 input variable"
                )
            layers = as_layers(
                prop.suffixed_op_graph(), translator_error=NeurifyTranslatorError,
            )
            neurify_inputs = to_neurify_inputs(
                prop.input_constraint,
                layers,
                dirname=dirname,
                translator_error=NeurifyTranslatorError,
            )
            logger.debug("Running neurify")
            executor = CommandLineExecutor(
                "neurify",
                "-n",
                neurify_inputs["nnet_path"],
                "-x",
                neurify_inputs["input_path"],
                "-sl",
                "0.0000000000001",  # TODO: remove magic number
                "-I",
                neurify_inputs["input_interval_path"],
                "-v",
                *[f"--{k}={v}" for k, v in kwargs.items() if v is not None],
                verifier_error=NeurifyError,
            )
            out, err = executor.run()
            logger.debug("Parsing results")
            result |= parse_results(out, err)
            if result == SAT:
                logger.debug("SAT! Validating counter example.")
                validate_counter_example(prop, out, err)
            if result == SAT or result == UNKNOWN:
                return result

    return result


from django.urls import path
from . import views

app_name = "anasayfa"
urlpatterns = [
    path('', views.index, name='index'),
    path('bilgi/<str:seo_link>', views.custom_page, name='custom_page'),
    path('iletisim', views.contact, name='contact'),

    path('uye-girisi-yap', views.uyegirisipost, name='uye-girisi-post'),

    path('uye-ol', views.uyeolpost, name='uye-ol-post'),
    path('bayi-ol', views.uyeolpostbayi, name='bayi-ol-post'),

    path('giris-yap', views.loginpage, name='uye-girisi'),
    path('kayit-ol', views.register, name='kayit-ol'),

    # path('bayi-girisi-yap', views.uyegirisipostbayi, name='bayi-girisi-post'),
    # path('bayi-girisi', views.registerbayi, name='bayi-girisi'),

    path('cikis-yap', views.logout_user, name='cikis-yap'),
    path('iletisim-kaydet', views.contact_post, name='contact-post'),
    path('firma-onayla/<int:companyid>', views.approved_company, name='approved_company'),

    path('robots.txt', views.robots_txt, name='robots_txt'),
    path('sitemap.xml', views.sitemap_xml, name='sitemap_xml'),
    path('ac0bdba759c3.html', views.yandex_file, name='yandex_file'),
]


import numpy as np
import math

X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])
y = np.array([[0, 1, 1, 0]]).T
init_theta = 2*np.random.random((13,1)) - 1

def runForward(X, theta):
	theta1 = np.array(theta[:9]).reshape(3,3)
	theta2 = np.array(theta[9:]).reshape(4,1)
	h1 = sigmoid(np.dot(X, theta1)) #4x3 * 3x3 = 4x3
	h1_bias = np.insert(h1, 3, [1,1,1,1], axis=1)
	output = sigmoid(np.dot(h1_bias, theta2)) #4x4 * 4x1 = 4x1
	return output
	#4x3 * 3x1 = 4x1
def costFunction(X, y, theta):
	m = float(len(X))
	hThetaX = np.array(runForward(X, theta))
	return np.sum(np.abs(y - hThetaX))
def sigmoid(x): return 1 / (1 + np.exp(- x))

def demoRun():
	print("Random theta: \n%s\n" % (runForward(X, init_theta)))
	print("Cost: %s\n" % (costFunction(X,y, init_theta)))
	theta = np.array([[-10.27649069,-14.03, 10.45888659, 9.12 ,14.87, -21.50294038, 1.85, -13.28, -0.15360052, -11.21345025, 35.77912716, 11.05, -2.49589577]])
	print("Optimal theta: \n%s\n" % (runForward(X, theta)))
	print("Cost: %s\n" % (costFunction(X, y, theta)))


from collections_helper.collections_spec_constants import MetaCrudParams

spec = {
    # Scope/Collection ops params
    MetaCrudParams.COLLECTIONS_TO_FLUSH: 0,
    MetaCrudParams.COLLECTIONS_TO_DROP: 0,
    MetaCrudParams.DOC_GEN_TYPE: "employee",

    MetaCrudParams.SCOPES_TO_DROP: 0,
    MetaCrudParams.SCOPES_TO_ADD_PER_BUCKET: 0,
    MetaCrudParams.COLLECTIONS_TO_ADD_FOR_NEW_SCOPES: 0,

    MetaCrudParams.COLLECTIONS_TO_ADD_PER_BUCKET: 0,

    # Only dropped scope/collection will be created.
    # While scope recreated all prev collection will also be created
    # In both the collection creation case, previous maxTTL value of
    # individual collection is considered
    MetaCrudParams.SCOPES_TO_RECREATE: 0,
    MetaCrudParams.COLLECTIONS_TO_RECREATE: 0,

    # Applies only for the above listed scope/collection operations
    MetaCrudParams.BUCKET_CONSIDERED_FOR_OPS: "all",
    MetaCrudParams.SCOPES_CONSIDERED_FOR_OPS: "all",
    MetaCrudParams.COLLECTIONS_CONSIDERED_FOR_OPS: "all",

    # Doc loading params
    "doc_crud": {
        # This applies to all collections created during following steps:
        # COLLECTIONS_TO_ADD_FOR_NEW_SCOPES,
        # COLLECTIONS_TO_ADD_PER_BUCKET,
        # SCOPES_TO_RECREATE,
        # COLLECTIONS_TO_RECREATE
        MetaCrudParams.DocCrud.NUM_ITEMS_FOR_NEW_COLLECTIONS: 100,

        # Applies to all active collections selected as per the
        # COLLECTIONS_CONSIDERED_FOR_CRUD value
        MetaCrudParams.DocCrud.COMMON_DOC_KEY: "test_collections",

        MetaCrudParams.DocCrud.CREATE_PERCENTAGE_PER_COLLECTION: 100,
        MetaCrudParams.DocCrud.READ_PERCENTAGE_PER_COLLECTION: 0,
        MetaCrudParams.DocCrud.UPDATE_PERCENTAGE_PER_COLLECTION: 0,
        MetaCrudParams.DocCrud.REPLACE_PERCENTAGE_PER_COLLECTION: 0,
        MetaCrudParams.DocCrud.DELETE_PERCENTAGE_PER_COLLECTION: 0,
        MetaCrudParams.DocCrud.TOUCH_PERCENTAGE_PER_COLLECTION: 0,

        # Doc loading options supported (None as of now)
    },

    "subdoc_crud": {
        MetaCrudParams.SubDocCrud.XATTR_TEST: False,

        # Applies to all active collections selected as per the
        # COLLECTIONS_CONSIDERED_FOR_CRUD value
        MetaCrudParams.SubDocCrud.INSERT_PER_COLLECTION: 0,
        MetaCrudParams.SubDocCrud.UPSERT_PER_COLLECTION: 0,
        MetaCrudParams.SubDocCrud.REMOVE_PER_COLLECTION: 0,
        MetaCrudParams.SubDocCrud.LOOKUP_PER_COLLECTION: 0,

        # Sub-doc loading option supported (None as of now)
    },

    # Doc_loading task options
    MetaCrudParams.DOC_TTL: 0,
    MetaCrudParams.DURABILITY_LEVEL: "",
    MetaCrudParams.SDK_TIMEOUT: 60,
    MetaCrudParams.SDK_TIMEOUT_UNIT: "seconds",
    MetaCrudParams.TARGET_VBUCKETS: "all",
    MetaCrudParams.SKIP_READ_ON_ERROR: False,
    MetaCrudParams.SUPPRESS_ERROR_TABLE: False,

    MetaCrudParams.RETRY_EXCEPTIONS: [],
    MetaCrudParams.IGNORE_EXCEPTIONS: [],

    # Applies only for DocCrud / SubDocCrud operation
    MetaCrudParams.COLLECTIONS_CONSIDERED_FOR_CRUD: "all",
    MetaCrudParams.SCOPES_CONSIDERED_FOR_CRUD: "all",
    MetaCrudParams.BUCKETS_CONSIDERED_FOR_CRUD: "all"
}


def weight_on_planets():
   weight = input("What do you weigh on earth? ")
   weight=float(weight)
   weight1=weight*.38
   weight2=weight*2.34
   print("\nOn Mars you would weigh",weight1,"pounds.\nOn Jupiter you would weigh",weight2,"pounds.")

if __name__ == '__main__':
   weight_on_planets()


#!/usr/bin/env python

import rospy
import json

from nav_msgs.msg import Odometry
from std_msgs.msg import String

def change_light(msg=None):
  pub = rospy.Publisher('change_light', String, queue_size=10)
  if not msg:
    msg = {
        'id':'paintMixer01', # painting_machine, curing_machine, mixer-A, mixer-B, charging_station, itemInv01, paintInv01
        'state':'on'   # on, off
    }
  rate = rospy.Rate(10)
  while(pub.get_num_connections() == 0):
      rate.sleep()
  pub.publish(str(msg))

if __name__ == '__main__':
  try:
    test_topic()
  except rospy.ROSInterruptException:
    pass


import time
import rtmidi
from bidict import bidict
from collections import namedtuple

class NanoSync:
    def __init__(self):

        self.serial_number = ""
        self.firmware_version = ""

        # create midi objects fot communication

        self.nanosync_midi_out = rtmidi.MidiOut()
        self.nanosync_midi_in = rtmidi.MidiIn()

        self.midi_in_port = None
        self.midi_out_port = None
        self._select_correct_ports()

        # important commands used for NanoSyncs
        self.serial_message = [1]         # F0 2C 4E 53 01 F7 - command to return S/N of Nano Sync and firmware version
        self.query_current_Config = [3]   # F0 2C 4E 53 03 F7 - Queries the nano sync about its current setup

        # abstraction for the Nano sync control

        # <editor-fold desc="Settings dictionaries">
        self.video_ref                          = bidict({"internal": 1, "external pal": 2, "external ntsc": 3, "external tri": 4})
        self.video_standard                     = bidict({"ntsc": 1, "pal 25": 2, "pal 24": 3, "pal 23.98": 4})
        self.HD_standard                        = bidict({"1080i x2 fps": 1, "1080p x1 fps": 2, "1080p x2 fps": 3, "720p x1 fps": 4, "720p x2 fps": 5})
        self.FPS                                = bidict({"23.98 fps": 1, "24 fps": 2, "25 fps": 3, "29.97 fps": 4, "30 fps": 5})
        self.video_definition                   = bidict({"SD" : 1, "HD": 2})

        self.audio_ref                          = bidict({"follow video": 1, "external word clock": 2, "external word 1:1": 3, "LTC": 4})
        self.external_word_fs                   = bidict({"44.1 khz": 1, "48 khz": 2})
        self.external_word_fs_multiplier        = bidict({"x1": 1, "x2": 2})
        self.external_word_fs_modifier          = bidict({"1/1": 1, "+0.1%": 2})
        self.external_LTC_fps                   = bidict({"23.98 fps": 1, " 24 fps": 2, "25 fps": 3, "29.98 fps": 4, "30 fps": 5})
        self.audio_sample_rate                  = bidict({"48 khz": 1, "44.1 khz": 2})
        self.audio_sample_rate_modifier         = bidict({"x1": 1, "+4%": 2, "+0.1%": 3, "-0.1%": 4, "-4%": 5})
        self.word_multiplier_1_6                = bidict({"x1": 1, "x2": 2, "x4": 3})
        self.word_multiplier_7_8                = bidict({"x1": 1, "x2": 2, "x4": 4, "x256": 5})
        self.AES_multiplier                     = bidict({"x1": 1, "x2": 2})
        self.SPDIF_multiplier                   = bidict({"x1": 1, "x2": 2})
        # </editor-fold>

        # Setting some default values

        self.cursor_pos                             = 0 # Placeholder
        self.video_ref_setting                      = self.video_ref["internal"]
        self.video_standard_setting                 = self.video_standard["ntsc"]
        self.HD_standard_setting                    = self.HD_standard["1080p x2 fps"]
        self.FPS_setting                            = self.FPS["30 fps"]
        self.video_1_to_3_setting                   = self.video_definition["HD"]
        self.video_4_setting                        = self.video_definition["HD"]
        self.video_5_setting                        = self.video_definition["HD"]
        self.video_6_setting                        = self.video_definition["HD"]

        # audio stuff below, best to leave it all alone
        self.audio_ref_setting                      = self.audio_ref["follow video"]
        self.external_word_fs_setting               = self.external_word_fs["44.1 khz"]
        self.external_word_fs_multiplier_setting    = self.external_word_fs_multiplier["x1"]
        self.external_word_fs_modifier_setting      = self.external_word_fs_modifier["1/1"]
        self.external_LTC_fps_setting               = self.external_LTC_fps["23.98 fps"]
        self.audio_sample_rate_setting              = self.audio_sample_rate["48 khz"]
        self.audio_sample_rate_modifier_setting     = self.audio_sample_rate_modifier["x1"]
        self.word_multiplier_1_6_setting            = self.word_multiplier_1_6["x1"]
        self.word_multiplier_7_8_setting            = self.word_multiplier_7_8["x1"]
        self.AES_multiplier_setting                 = self.AES_multiplier["x1"]
        self.SPDIF_multiplier_setting               = self.SPDIF_multiplier["x1"]

        self.current_config = None  # Puts all the settings above into a list
        self._update_current_config()

        self._connect()
        self._get_current_config()

    def _update_current_config(self):
        """ Updates the current config list """

        self.current_config =   [self.cursor_pos,
                                self.video_ref_setting,
                                self.video_standard_setting,
                                self.HD_standard_setting,
                                self.FPS_setting,
                                self.video_1_to_3_setting,
                                self.video_4_setting,
                                self.video_5_setting,
                                self.video_6_setting,
                                self.audio_ref_setting,
                                self.external_word_fs_setting,
                                self.external_word_fs_multiplier_setting,
                                self.external_word_fs_modifier_setting,
                                self.external_LTC_fps_setting,
                                self.audio_sample_rate_setting,
                                self.audio_sample_rate_modifier_setting,
                                self.word_multiplier_1_6_setting,
                                self.word_multiplier_7_8_setting,
                                self.AES_multiplier_setting,
                                self.SPDIF_multiplier_setting]

    def _send_message(self, message, new_config=False):
        """Formats with the midi system exclusive header and footer then sends the command via the midi out"""

        if new_config is False:
            header = [240, 44, 78, 83]
            footer = [247]
        else: # if a new config is being sent the header needs to changed
            header = [240, 44, 78, 83, 15]
            footer = [247]

        message = header + message + footer
        self.nanosync_midi_out.send_message(message)

    def _receive_message(self):
        """Makes 5 attempts to read a message from the midi buffer - If nothing is found an IO error is raised"""

        for i in range(5):
            return_message = self.nanosync_midi_in.get_message()
            if return_message is not None:
                return return_message
            time.sleep(0.1)
        else:
            raise IOError("did not receive message from Nanosync after attempts")

    def _get_current_config(self):
        """Function sends the get current config command to nanosyncs, reads data back, formats it and then saves data
         to each variable """

        self._send_message(self.query_current_Config)
        received_config = self._receive_message()
        # midi library return a tuple containing meta data and a list of all the settings
        received_config = received_config[0]  # strip meta data
        received_config = received_config[5:-1]  # Strips the midi system exclusive header and footer

        self.cursor_pos                             = received_config[0]
        self.video_ref_setting                      = received_config[1]
        self.video_standard_setting                 = received_config[2]
        self.HD_standard_setting                    = received_config[3]
        self.FPS_setting                            = received_config[4]
        self.video_1_to_3_setting                   = received_config[5]
        self.video_4_setting                        = received_config[6]
        self.video_5_setting                        = received_config[7]
        self.video_6_setting                        = received_config[8]

        self.audio_ref_setting                      = received_config[9]
        self.external_word_fs_setting               = received_config[10]
        self.external_word_fs_multiplier_setting    = received_config[11]
        self.external_word_fs_modifier_setting      = received_config[12]
        self.external_LTC_fps_setting               = received_config[13]
        self.audio_sample_rate_setting              = received_config[14]
        self.audio_sample_rate_modifier_setting     = received_config[15]
        self.word_multiplier_1_6_setting            = received_config[16]
        self.word_multiplier_7_8_setting            = received_config[17]
        self.AES_multiplier_setting                 = received_config[18]
        self.SPDIF_multiplier_setting               = received_config[19]

        self._update_current_config()

    def _select_correct_ports(self):
        available_out_ports = self.nanosync_midi_out.get_ports()
        available_in_ports = self.nanosync_midi_in.get_ports()

        # rtpmidi returns a list of of midi devices that may be in different orders
        # function selects port based the name

        for i, elem in enumerate(available_in_ports):
            if 'NANOSYNCS' in elem:
                print("found nanosync in port on %i" % i)
                self.midi_in_port = i
                break
        else:
            raise IOError("unable to find nano sync midi out port")

        for i, elem in enumerate(available_out_ports):
            if 'NANOSYNCS' in elem:
                print("found nanosync out port on %i" % i)
                self.midi_out_port = i
                break
        else:
            raise IOError("unable to find nano sync midi in port")

    def _connect(self):

        # Should initialise the midi in and out ports
        # Should select the correct port from the list of devices
        # Should verify that the connection is successful by sending the return serial device

        self.nanosync_midi_in.ignore_types(sysex=False) # Allow system exclusive commands to be sent

        self.nanosync_midi_out.open_port(self.midi_out_port)
        self.nanosync_midi_in.open_port(self.midi_in_port)

        self._send_message(self.serial_message)
        info = self._receive_message()
        if info is None:
            raise IOError("failed to communicate with nanosync")

        else:
            info = info[0]  # midi library returns a tuple with extra data, this strips it
            info = info[5:-1]  # this strips the padding of the return message
            serial_number = info[:4]
            firmware = info[4:]

            for char in serial_number:
                self.serial_number += chr(char)
            for char in firmware:
                self.firmware_version += chr(char)
            self.firmware_version = self.firmware_version[:2] + '.' + self.firmware_version[2:]

            print("connected to NanoSync")
            print("serial number: %s" % self.serial_number)
            print("Firmware version %s" %  self.firmware_version)

    def disconnect(self):
        self.nanosync_midi_out.close_port()
        self.nanosync_midi_in.close_port()

    def print_current_config(self):
        """Prints the current config to console - used for user readability"""
        self._get_current_config()

        print("video ref: %s" %                   self.video_ref.inverse[self.video_ref_setting])
        print("video standard: %s" %              self.video_standard.inverse[self.video_standard_setting])
        print("HD standard: %s" %                 self.HD_standard.inverse[self.HD_standard_setting])
        print("FPS: %s" %                         self.FPS.inverse[self.FPS_setting])
        print("SDI out 1 to 3: %s" %              self.video_definition.inverse[self.video_1_to_3_setting])
        print("SDI out 4: %s" %                   self.video_definition.inverse[self.video_4_setting])
        print("SDI out 5: %s" %                   self.video_definition.inverse[self.video_5_setting])
        print("SDI out 6: %s" %                   self.video_definition.inverse[self.video_6_setting])

        print("Audio ref: %s" %                   self.audio_ref.inverse[self.audio_ref_setting])
        print("External word: %s" %               self.external_word_fs.inverse[self.external_word_fs_setting])
        print("External word multiplier: %s" %    self.external_word_fs_multiplier.inverse[self.external_word_fs_multiplier_setting])
        print("External word fs 1: %s" %          self.external_word_fs_modifier.inverse[self.external_word_fs_modifier_setting])
        print("External LTC fps: %s" %            self.external_LTC_fps.inverse[self.external_LTC_fps_setting])
        print("Audio sample rate: %s" %           self.audio_sample_rate.inverse[self.audio_sample_rate_setting])
        print("Sample rate pull factor: %s" %     self.audio_sample_rate_modifier.inverse[self.audio_sample_rate_modifier_setting])
        print("word mult 1 to 6: %s" %            self.word_multiplier_1_6.inverse[self.word_multiplier_1_6_setting])
        print("word mult 7 to 8: %s" %            self.word_multiplier_7_8.inverse[self.word_multiplier_7_8_setting])
        print("AES mult: %s" %                    self.AES_multiplier.inverse[self.AES_multiplier_setting])
        print("SPDIF mult: %s" %                  self.SPDIF_multiplier.inverse[self.SPDIF_multiplier_setting])

    def get_current_refresh_rate(self):
        """Returns a named tuple containing the numerator and denominator to calculate the refresh rate"""

        # self.HD_standard_setting = 1  # byte 3: 1080i x 2fps = 1, 1080p = 2, 1080p x 2fps = 3, 720p = 4, 720p x 2fps = 5
        # self.FPS_setting = 1          # byte 4: 23.98fps = 1, 24fps = 2, 25fps = 3, 29.97fps = 4, 30fps = 5

        fps_dict = {
            1: 23.98,
            2: 24,
            3: 25,
            4: 29.97,
            5: 30}

        tuple_dict = {

            60: ("60", "1"),
            50: ("50", "1"),
            48: ("48", "1"),
            30: ("30", "1"),
            25: ("25", "1"),
            24: ("24", "1"),

            59.94: ("60000", "1001"),
            47.96: ("48000", "1001"),
            29.97: ("30000", "1001"),
            23.98: ("24000", "1001")}

        self._get_current_config()  # Make sure we have the latest data

        refresh_rate = fps_dict.get(self.FPS_setting)

        if self.HD_standard_setting in [3, 5]:  # if HD standard is 1080p x 2fps or 720p x 2fps
            refresh_rate *= 2

            refresh_rate_ntp = namedtuple("refresh rate", "numerator denominator")
            return_tuple = refresh_rate_ntp(tuple_dict.get(refresh_rate))
            return return_tuple

        else:

            refresh_rate_ntp = namedtuple("refresh rate", "numerator denominator")
            return_tuple = refresh_rate_ntp(tuple_dict.get(refresh_rate))
            return return_tuple

    def send_new_config_raw(self, new_config):
        """Accepts a list of 20 values corresponding to each of the settings of the nanosyncs """

        # some basic parameter checking
        if type(new_config) is not list:
            print("Command has to be a list")
            return
        if len(new_config) is not 20:
            print("command has to 20 items long ")

        # gets the latest state of the nanosync configuration
        self._get_current_config()
        comparision = self.current_config

        if new_config == comparision:
            print("Nanosyncs already has identical config set, skipping sending the new config ")
        else:
            for i in range(5):
                self._send_message(new_config, new_config= True)
                self._get_current_config()
                if new_config == self.current_config:  # This may provide a false positive if the nanosync is pre-configured with the same setting being sent
                    print("successfully sent command")
                    break
            else:
                print("!!! warning - detected no change in the config of the Nanosync message send may of failed")
                time.sleep(0.1)

    def set_video_ref(self, setting):

        if setting in ["internal", "external pal", "external ntsc", "external tri"]:
            self.video_ref_setting = self.video_ref[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set video ref to %s " % setting)
        else:
            print("invalid setting has been given for video ref")

    def set_video_standard(self, setting):

        if setting in ["ntsc", "pal 25", "pal 24", "pal 23.98"]:
            self.video_standard_setting = self.video_standard[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set video standard to %s " % setting)
        else:
            print("invalid setting has been given for video standard")

    def set_hd_standard(self, setting):

        if setting in ["1080i x2 fps", "1080p x1 fps", "1080p x2 fps", "720p x1 fps", "720p x2 fps"]:
            self.HD_standard_setting = self.HD_standard[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)
            print("set HD standard to %s " % setting)
        else:
            print("invalid setting has been given for HD standard")

    def set_fps(self, setting):

        if setting in ["23.98 fps", "24 fps", "25 fps", "29.97 fps", "30 fps"]:
            self.FPS_setting = self.FPS[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set FPS to %s " % setting)
        else:
            print("invalid setting has been given for FPS")

    def set_sdi_out_1_to_3(self, setting):

        if setting in ["SD", "HD"]:
            self.video_1_to_3_setting = self.video_definition[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set set SDI out 1 to 3 to %s " % setting)
        else:
            print("invalid setting has been given for SDI out 1 to 3")

    def set_sdi_out_4(self, setting):

        if setting in ["SD", "HD"]:
            self.video_4_setting= self.video_definition[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set SDI out 4 to %s " % setting)
        else:
            print("invalid setting has been given for SDI out 4")

    def set_sdi_out_5(self, setting):

        if setting in ["SD", "HD"]:
            self.video_5_setting= self.video_definition[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set SDI out 5 to %s " % setting)
        else:
            print("invalid setting has been given for SDI out 5")

    def set_sdi_out_6(self, setting):

        if setting in ["SD", "HD"]:
            self.video_6_setting= self.video_definition[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set SDI out 5 to %s " % setting)
        else:
            print("invalid setting has been given for SDI out 5")

    def set_audio_reference(self, setting):

        if setting in ["follow video", "external word clock", "external word 1:1", "LTC"]:
            self.audio_ref_setting= self.audio_ref[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set audio reference to %s " % setting)
        else:
            print("invalid setting has been given for audio reference")

    def set_external_word_fs(self, setting):

        if setting in ["44.1 khz", "48 khz"]:
            self.external_word_fs_setting = self.external_word_fs[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set external word FS to %s " % setting)
        else:
            print("invalid setting has been given external word FS")

    def set_external_word_multiplier(self, setting):

        if setting in ["x1", "x2"]:
            self.external_word_fs_multiplier_setting = self.external_word_fs_multiplier[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set external word multiplier to %s " % setting)
        else:
            print("invalid setting has been given for external word multiplier")

    def set_external_word_modifier(self, setting):

        if setting in ["1/1", "+0.1%"]:
            self.external_word_fs_modifier_setting = self.external_word_fs_modifier[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set external word modifier to %s " % setting)
        else:
            print("invalid setting has been given for external word modifier")

    def set_external_ltc_fps(self, setting):

        if setting in ["23.98 fps", " 24 fps", "25 fps", "29.98 fps", "30 fps"]:
            self.external_LTC_fps_setting = self.external_LTC_fps[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set external LTC FPS to %s " % setting)
        else:
            print("invalid setting has been given for external LTC FPS")

    def set_audio_sample_rate(self, setting):

        if setting in ["48 khz", "44.1 khz"]:
            self.audio_sample_rate_setting = self.audio_sample_rate[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set audio sample rate to %s " % setting)
        else:
            print("invalid setting has been given for the audio sample rate")

    def set_audio_sample_rate_modifier(self, setting):

        if setting in ["x1", "+4%", "+0.1%", "-0.1%", "-4%"]:
            self.audio_sample_rate_modifier_setting = self.audio_sample_rate_modifier[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set audio sample rate modifier to %s " % setting)
        else:
            print("invalid setting has been given for audio sample rate modifier ")

    def set_word_multiplier_1_6(self, setting):

        if setting in ["x1", "x2", "x4"]:
            self.word_multiplier_1_6_setting = self.word_multiplier_1_6[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set word multiplier 1 to 6 to %s " % setting)
        else:
            print("invalid setting has been given word multiplier 1 to 6")

    def set_word_multiplier_7_8(self, setting):

        if setting in ["x1", "x2", "x4", "x256"]:
            self.word_multiplier_7_8_setting = self.word_multiplier_7_8[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)

            print("set word multiplier 7 to 8 to %s " % setting)
        else:
            print("invalid setting has been given for word multiplier 7 to 8")

    def set_AES_multiplier(self, setting):

        if setting in ["x1", "x2"]:
            self.AES_multiplier_setting = self.AES_multiplier[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)
            print("set AES multiplier to %s " % setting)
        else:
            print("invalid setting has been given for AES multiplier")

    def set_SPDIF_multiplier(self, setting):

        if setting in ["x1", "x2"]:
            self.SPDIF_multiplier_setting = self.SPDIF_multiplier[setting]
            self._update_current_config()
            self.send_new_config_raw(self.current_config)
            print("set SPDIF multiplier to %s " % setting)
        else:
            print("invalid setting has been given for SPDIF multiplier")

    def get_video_ref(self):
        return self.video_ref.inverse[self.video_ref_setting]

    def get_video_standard(self):
        return self.video_standard.inverse[self.video_standard_setting]

    def get_hd_standard(self):
        return self.HD_standard.inverse[self.HD_standard_setting]

    def get_fps(self):
        return self.FPS.inverse[self.FPS_setting]

    def get_sdi_out_1_to_3(self):
        return self.video_definition.inverse[self.video_1_to_3_setting]

    def get_sdi_out_4(self):
        return self.video_definition.inverse[self.video_4_setting]

    def get_sdi_out_5(self):
        return self.video_definition.inverse[self.video_5_setting]

    def get_sdi_out_6(self):
        return self.video_definition.inverse[self.video_6_setting]

    def get_audio_ref(self):
        return self.audio_ref.inverse[self.audio_ref_setting]

    def get_external_word_fs(self):
        return self.external_word_fs.inverse[self.external_word_fs_setting]

    def get_external_word_fs_multiplier(self):
        return self.external_word_fs_multiplier.inverse[self.external_word_fs_multiplier_setting]

    def get_external_word_fs_modifier(self):
        return self.external_word_fs_modifier.inverse[self.external_word_fs_modifier_setting]

    def get_external_LTC_fps(self):
        return self.external_LTC_fps.inverse[self.external_LTC_fps_setting]

    def get_audio_sample_rate(self):
        return self.audio_sample_rate.inverse[self.audio_sample_rate_setting]

    def get_audio_sample_rate_modifier(self):
        return self.audio_sample_rate_modifier.inverse[self.audio_sample_rate_modifier_setting]

    def get_word_multiplier_1_6(self):
        return self.word_multiplier_1_6.inverse[self.word_multiplier_1_6_setting]

    def get_word_multiplier_7_8(self):
        return self.word_multiplier_7_8.inverse[self.word_multiplier_7_8_setting]

    def get_AES_multiplier(self):
        return self.AES_multiplier.inverse[self.AES_multiplier_setting]

    def get_SPDIF_multiplier(self):
        return self.SPDIF_multiplier.inverse[self.SPDIF_multiplier_setting]


from vector import Vector
# from visual import graphics

alpha = 0.5
vectors = []
clusters = []
# answer = []
# listed_vectors = []

def read_data_and_set_first_center():
    for line in open("./input/iris.dat", "r"):
        vectors.append(Vector(list(map(float, line.split()[:-1]))))

    clusters.append(vectors[0])

def clustering():
    v_max, d_max = max(
        [min([(v, (v - c).len()) for c in clusters], key=lambda x: x[1]) for v in vectors],
        key=lambda x: x[1])

    distances_between_clusters_centers = []
    for i in range(len(clusters) - 1):
        for j in range(i + 1, len(clusters)):
            distances_between_clusters_centers.append((clusters[i] - clusters[j]).len())

    average_distance = 0 if len(clusters) < 2 else \
        alpha * sum(distances_between_clusters_centers) / len(distances_between_clusters_centers)

    if d_max > average_distance:
        clusters.append(v_max)
        clustering()

def set_vectors_to_clusters():
    # global answer, listed_vectors
    log = {i+1: [] for i, c in enumerate(clusters)}
    for vi, v in enumerate(vectors):
        index = min([(i + 1, (v - c).len()) for i, c in enumerate(clusters)], key=lambda x: x[1])[0]
        # answer.append(list(v)+[index])
        print("%s  %d" % (v, index))
        log[index].append(v)
    # for c in clusters:
    #     listed_vectors.append(list(c))

    print("\ncenters:")
    print("\n".join(map(str, clusters)))
    print()

    for k in log:
        print("cluster: %d, count: %d" % (k, len(log[k])))

read_data_and_set_first_center()
clustering()
set_vectors_to_clusters()
# graphics.__init__(answer, listed_vectors)


import configparser
from eeg_v2 import EEG
from utils import chunks, baseline_calc, butter_bandpass_filter
import numpy as np
from mne.preprocessing import ICA
import mne
from matplotlib import pyplot as plt
import json
import sys, os
import pandas as pd

# https://github.com/mne-tools/mne-python/issues/2404

config = configparser.ConfigParser()
config.read("config.ini")

_WINDOW = int(config["CHUNK"]["WINDOW"])

def _get_ica_map(ica, components=None):
    """Get ICA topomap for components"""
    fast_dot = np.dot
    if components is None:
        components = list(range(ica.n_components_))
    maps = fast_dot(ica.mixing_matrix_[:, components].T,
                    ica.pca_components_[:ica.n_components_])
    return maps

def main():

    path_edf="edf/1489/1489_alice/edf/A0001489.edf"
    path_stage="edf/1489/1489_alice/csv/STAGE.csv"

    eeg = EEG(path_edf=path_edf, path_stage=path_stage)

    # name = input("Subject no: ")

    _components = []

    N_SAMPLE = int(config["DEFAULT"]["N_SAMPLE_PER_SUBJECT"])
    counter = 0

    if not os.path.exists("csvs"):
        os.mkdir("csvs")

    for task in eeg.tasks:
        if counter > N_SAMPLE:
            break

        for idx, chunk in enumerate(chunks(task, _WINDOW)):

            if counter > N_SAMPLE:
                break

            _min, _max = np.min(chunk), np.max(chunk)

            # Getting sample data
            sample_raw = eeg.raw.copy().crop(_min, _max, include_tmax=False)

            # Apply baseline correction
            sample_raw_baseline = sample_raw.copy()
            sample_raw_baseline.apply_function(baseline_calc)

            # sample_raw.copy().pick('Fp1').plot()
            # sample_raw_baseline.copy().pick('Fp1').plot()

            # break

            # Apply bandpass filter
            sample_raw_bandpass = sample_raw_baseline.copy()
            sample_raw_bandpass.apply_function(butter_bandpass_filter)

            # Apply ICA
            sample_raw_train = sample_raw_bandpass.copy()
            sample_raw_corrected = sample_raw_bandpass.copy()

            # Train
            ica = ICA(method="extended-infomax", random_state=1)
            ica.fit(sample_raw_corrected)

            # Plot ICA component
            ica.plot_sources(inst=sample_raw_train)
            ica.plot_components(inst=sample_raw_train)

            while True:
                try:
                    list_of_eog = input("List of EOG components seperated by space: ")
                    list_of_eog = list(map(int, list_of_eog.split()))

                    list_of_ecg = input("List of ECG components seperated by space: ")
                    list_of_ecg = list(map(int, list_of_ecg.split()))

                    list_of_emg = input("List of EMG components seperated by space: ")
                    list_of_emg = list(map(int, list_of_emg.split()))

                    break
                except ValueError:
                    print("Try again..")

            if list_of_eog or list_of_ecg or list_of_emg:

                ica.exclude = [*list_of_eog, *list_of_emg, *list_of_ecg]
                # raw_ = eeg.raw.copy()
                # sample_raw_corrected = sample_raw_bandpass.copy()

                ica.apply(sample_raw_corrected)

                sample_raw.plot(title="RAW")
                sample_raw_corrected.plot(title="RAW_CORRECTED")

                to_save = input("Save this trunk? [y/n]: ")
                # print("=================================================================================")

                if to_save == 'y':
                    maps = _get_ica_map(ica).T
                    scalings = np.linalg.norm(maps, axis=0)
                    maps /= scalings[None, :]

                    # maps[:,0] = 0

                    components = ica.get_sources(inst=sample_raw_train).get_data()
                    components_name = ica.ch_names

                    def f(idx):
                        if idx in list_of_eog:
                            return "EOG"

                        if idx in list_of_ecg:
                            return "ECG"

                        if idx in list_of_emg:
                            return "EMG"

                        return "EEG"

                    for idx2, comp_name in enumerate(components_name):
                        tmp = {
                            "name" : "{comp_name}_{idx}".format(comp_name=comp_name, idx=idx),
                            # "component" : list(components[idx2, :]),
                            "map" : list(maps[:, idx2]),
                            "label" : f(idx2)
                        }
                        # Save file to csv
                        temp = pd.DataFrame(components[idx2,:].reshape(-1, len(components[idx2,: ])), columns = [f"comp_{i}" for i in range(len(components[idx2,:]))])

                        temp['name'] = "{comp_name}_{idx}".format(comp_name=comp_name, idx=idx)
                        temp['label'] = "EOG" if idx2 in list_of_eog else "Non-EOG"
                        # print(temp)
                        comp_map = list(maps[:, idx2])
                        # print(comp_map)
                        for i in range(len(comp_map)):
                            temp[f"map_component_{i}"] = comp_map[i]
                        temp.to_csv(f'./csvs/{name}_{idx}_{comp_name}.csv', index=False, header=True)
                        # print(maps[:, idx2].shape)
                        # sys.exit()
                        try:
                            _components.append(tmp)
                            counter+=1

                        except Exception as e:
                            break

    j = {
        "name" : name,
        "data" : _components
    }

    with open("data.json", "w") as f:
        json.dump(j, f)

if __name__ == "__main__":
    main()


'''
Convert a stack of images to numpy array
'''
import os
import numpy as np
from PIL import Image
import argparse
import cv2
import glob

parser = argparse.ArgumentParser(description="Segmentation using tensorflow")

parser.add_argument('-m','--mode', type=str, default='rgb', help='rgb or seg')
parser.add_argument('-sf','--split_file', type=str, default=None, help='a text file indicate training or testing images')
parser.add_argument('-i','--img_dir', type=str, help='directory to one image or a folder contains images')
parser.add_argument('-o','--out_dir', type=str, required=True, help='directory to the output numpy file')
parser.add_argument('-d','--dim_order', type=str, default='nhwc', help='order of the dimentions of image, nhwc or nwhc or ncwh or nchw')

args = parser.parse_args()
split_file = args.split_file
img_dir = args.img_dir
out_dir = args.out_dir
mode = args.mode

BAD_IMAGES = ['/media/DATA/UnrealLandingDataset/AirSimCollectData/label/513-0.png',
              '/media/DATA/UnrealLandingDataset/AirSimCollectData/label/3112-0.png',
              '/media/DATA/UnrealLandingDataset/AirSimCollectData/label/3792-0.png',
              '/media/DATA/UnrealLandingDataset/AirSimCollectData/label/3965-0.png']

if split_file is not None:
    with open(split_file, 'r') as f:
        all_image_files = f.read().splitlines()
        all_images = []
        for i, image_file in enumerate(all_image_files):
            if mode == 'rgb':
                try:
                    image = np.array(Image.open(image_file).convert('RGB'))
                    all_images.append(image)
                except:
                    print(image_file)
                    continue
            elif mode == 'seg':
                if image_file in BAD_IMAGES:
                    print(image_file)
                    continue
                image = np.array(Image.open(image_file))
                all_images.append(image)

elif os.path.isdir(img_dir):
        all_image_files = sorted(glob.glob(os.path.join(img_dir, '*')))
        all_images = []
        for image_file in all_image_files:
            try:
                image = np.array(Image.open(image_file).convert('RGB'))
                all_images.append(image)
            except:
                print(image_file)
                continue
            # img = Image.open(image_file).convert('RGB')
            # image = np.asarray(img)
            # img.close()

else:
    try:
        image = np.asarray(Image.open(image_file).convert('RGB'))
    except:
        raise NameError("indicated image doesn't exit!")

all_images = np.array(all_images)
print(all_images.shape)
np.save(out_dir, all_images)


# -*- coding: utf-8 -*-
# @Time    : 2018/10/28 11:08 AM
# @Author  : Muximus
# @Site    :
# @File    : custom_classifier.py
# @Software: PyCharm
import os
import sys

_root = os.path.normpath("%s/../.." % os.path.dirname(os.path.abspath(__file__)))
sys.path.append(_root)
import numpy as np
import abc
from keras.layers import *
from keras.models import Model
from keras.optimizers import *
from keras.initializers import *
from keras import models
from keras.losses import *
from keras.models import Sequential

class Classifier(abc.ABC):
    def __init__(self, *args):
        pass

    @abc.abstractmethod
    def build_model(self):
        pass

class BasicLSTMClassifier(Classifier):
    def __init__(self, embedding_matrix, labels_num, max_sequence_length=35, lstm_unit=550, hidden_unit=550,
                 drop_out=0.5):
        super(BasicLSTMClassifier, self).__init__()
        self.embedding_matrix = embedding_matrix
        self.labels_num = labels_num
        self.max_sequence_length = max_sequence_length
        self.lstm_unit = lstm_unit
        self.hidden_unit = hidden_unit
        self.drop_out = drop_out

    def build_model(self):
        inp = Input(shape=(self.max_sequence_length,))
        i = Embedding(self.embedding_matrix.shape[0],
                      self.embedding_matrix.shape[1],
                      weights=[self.embedding_matrix],
                      input_length=self.max_sequence_length,
                      trainable=True)(inp)
        i = Dropout(self.drop_out)(i)
        i = Bidirectional(LSTM(self.lstm_unit, return_sequences=True))(i)
        i = GlobalMaxPool1D()(i)
        i = Dense(550)(i)
        i = BatchNormalization()(i)
        i = ELU()(i)
        i = Dropout(0.5)(i)
        i = Dense(self.labels_num, activation='softmax')(i)
        model = Model(inputs=inp, outputs=i)
        opti = Adam(lr=1e-03, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss=sparse_categorical_crossentropy,
                      optimizer=opti,
                      metrics=['acc'])
        return model

class RNNCNNClassifier(Classifier):
    def __init__(self, embedding_matrix, labels_num, filters, max_sequence_length: int = 35,
                 num_filters: int = 128, lstm_units: int = 550):
        super(RNNCNNClassifier, self).__init__()
        self.embedding_matrix = embedding_matrix
        self.max_sequence_length = max_sequence_length
        self.labels_num = labels_num
        self.num_filters = num_filters
        self.filters = filters
        self.lstm_units = lstm_units

    def build_model(self):
        inp0 = Input(shape=(self.max_sequence_length,))
        embedding_layer = Embedding(self.embedding_matrix.shape[0],
                                    self.embedding_matrix.shape[1],
                                    weights=[self.embedding_matrix],
                                    input_length=self.max_sequence_length,
                                    trainable=True)(inp0)
        embedding_layer = Bidirectional(LSTM(self.lstm_units, return_sequences=True))(embedding_layer)
        embedding_layer = Dropout(0.5)(embedding_layer)
        conv_blocks = []
        for fz in self.filters:
            conv = Conv1D(filters=self.num_filters,
                          kernel_size=fz,
                          activation='relu',
                          padding='same',
                          strides=1)(embedding_layer)
            conv = GlobalMaxPooling1D()(conv)
            conv_blocks.append(conv)
        z = concatenate(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]
        z = Dense(550)(z)
        z = BatchNormalization()(z)
        z = Activation('relu')(z)
        z = Dropout(0.4)(z)
        z = Dense(self.labels_num, activation='softmax', activity_regularizer=regularizers.l2(1e-04))(z)
        model = Model(inputs=inp0, outputs=z)
        opti = Adam(lr=1e-03, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss=sparse_categorical_crossentropy,
                      optimizer=opti,
                      metrics=['acc'])
        return model

class MLPBasicClassifier(Classifier):
    def __init__(self, label_num: int, hidden_unit: int = 550,
                 input_dim: int = 550, hidden_layers: list = None):
        super(MLPBasicClassifier, self).__init__()
        self.label_num = label_num
        self.hidden_unit = hidden_unit
        self.input_dim = input_dim
        self.hidden_layers = hidden_layers

    def build_model(self):
        model = Sequential()
        model.add(Dense(self.hidden_unit, input_shape=(self.input_dim,)))
        model.add(BatchNormalization())
        model.add(ELU())
        model.add(Dropout(0.5))
        if self.hidden_layers:
            for param in self.hidden_layers:
                model.add(Dense(param['units']))
                model.add(BatchNormalization())
                model.add(ELU())
                model.add(Dropout(param['dropout']))
        model.add(
            Dense(self.label_num, activation='softmax', kernel_regularizer=regularizers.l2(1e-04),
                  activity_regularizer=regularizers.l2(1e-04)))
        opti = Adam(lr=1e-03, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss=sparse_categorical_crossentropy,
                      optimizer=opti,
                      metrics=['acc'])
        return model

def conv_block_origin(input_tensor, filters, kernel_size):
    x = BatchNormalization()(input_tensor)
    x = Activation('relu')(x)
    x = Conv1D(filters[0], kernel_size[0], padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv1D(filters[1], kernel_size[1], padding='same')(x)
    shortcut = Conv1D(filters[1], kernel_size=kernel_size[0], use_bias=False)(input_tensor)
    add = Add()([x, shortcut])
    return add

def identity_block_origin(input_tensor, filters, kernel_size):
    x = BatchNormalization()(input_tensor)
    x = Activation('relu')(x)
    x = Conv1D(filters[0], kernel_size[0], padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv1D(filters[1], kernel_size[1], padding='same')(x)
    add = Add()([x, input_tensor])
    return add

class ResidualConvClassifier(Classifier):
    def __init__(self, embedding_matrix, labels_num, max_sequence_length: int = 35, ):
        super(ResidualConvClassifier, self).__init__()
        self.embedding_matrix = embedding_matrix
        self.max_sequence_length = max_sequence_length
        self.labels_num = labels_num

    def build_model(self):
        inp = Input(shape=(self.max_sequence_length,))
        i_e = Embedding(self.embedding_matrix.shape[0],
                        self.embedding_matrix.shape[1],
                        weights=[self.embedding_matrix],
                        input_length=self.max_sequence_length,
                        trainable=True)(inp)
        # embedding_layer = Dropout(0.5)(i_e)
        # conv_blocks = []
        # for fz, ks in zip([64, 128, 256], [1, 2, 3]):
        #     conv = Conv1D(filters=fz,
        #                   kernel_size=ks,
        #                   padding='same',
        #                   activation="relu",
        #                   strides=1)(embedding_layer)
        #     conv = GlobalMaxPooling1D()(conv)
        #     conv_blocks.append(conv)
        # i = concatenate(conv_blocks)
        i0 = conv_block_origin(i_e, [64, 64, 64], [1, 2, 3])
        i1 = identity_block_origin(i0, [64, 64, 64], [1, 2, 3])
        i1 = identity_block_origin(i1, [64, 64, 64], [1, 2, 3])
        i2 = conv_block_origin(i1, [128, 128, 128], [1, 2, 3])
        i3 = identity_block_origin(i2, [128, 128, 128], [1, 2, 3])
        i3 = identity_block_origin(i3, [128, 128, 128], [1, 2, 3])
        i4 = conv_block_origin(i3, [256, 256, 256], [1, 2, 3])
        i5 = identity_block_origin(i4, [256, 256, 256], [1, 2, 3])
        i5 = identity_block_origin(i5, [256, 256, 256], [1, 2, 3])
        i = GlobalMaxPool1D()(i5)
        i = Dense(1024)(i)
        i = BatchNormalization()(i)
        i = ELU()(i)
        i = Dropout(0.5)(i)
        i = Dense(550)(i)
        i = BatchNormalization()(i)
        i = ELU()(i)
        i = Dropout(0.5)(i)
        i = Dense(self.labels_num, activation='softmax', kernel_regularizer=regularizers.l2(1e-04),
                  activity_regularizer=regularizers.l2(1e-04))(i)
        model = Model(inputs=inp, outputs=i)
        opti = Adam(lr=1e-03, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss=sparse_categorical_crossentropy,
                      optimizer=opti,
                      metrics=['acc'])
        return model

class TransferClassifier(Classifier):
    def __init__(self, trick, model_dir, new_model: Model):
        super(TransferClassifier, self).__init__()
        self.trick = trick
        self.model_dir = model_dir
        self.new_model = new_model

    def build_model(self):
        pred_model = models.load_model('{}'.format(self.model_dir))
        if 0 < self.trick < len(pred_model.layers):
            for index, layer in enumerate(pred_model.layers[:-self.trick]):
                if index > 1:
                    print(self.new_model.layers[index].name, pred_model.layers[index].name)
                    self.new_model.layers[index].set_weights(pred_model.layers[index].get_weights())
                    self.new_model.layers[index].trainable = True
        else:
            for index, layer in enumerate(pred_model.layers):
                self.new_model.layers[index].trainable = True
        return self.new_model

def cnn1d_for_text(inp, filters, num_filters, max_sequence_length, embedding_matrix):
    embedding_layer = Embedding(embedding_matrix.shape[0],
                                embedding_matrix.shape[1],
                                weights=[embedding_matrix],
                                input_length=max_sequence_length,
                                trainable=True)(inp)
    embedding_layer = Dropout(0.5)(embedding_layer)
    conv_blocks = []
    for fz in filters:
        conv = Conv1D(filters=num_filters,
                      kernel_size=fz,
                      padding='valid',
                      activation="relu",
                      strides=1)(embedding_layer)
        conv = GlobalMaxPooling1D()(conv)
        conv_blocks.append(conv)
    return concatenate(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]

class CNNBasic(Classifier):
    def __init__(self, embedding_matrix, labels_num, filters, max_sequence_length: int = 35,
                 num_filters: int = 128):
        super(CNNBasic, self).__init__()
        self.embedding_matrix = embedding_matrix
        self.max_sequence_length = max_sequence_length
        self.labels_num = labels_num
        self.num_filters = num_filters
        self.filters = filters

    def build_model(self):
        inpq0 = Input(shape=(self.max_sequence_length,))
        z = cnn1d_for_text(inpq0, self.filters, self.num_filters, self.max_sequence_length,
                           self.embedding_matrix)
        z = Dense(550)(z)
        z = BatchNormalization()(z)
        z = ELU()(z)
        z = Dropout(0.5)(z)
        z = Dense(self.labels_num, activation='softmax',
                  kernel_initializer=RandomUniform(-0.1, 0.1, seed=1400),
                  activity_regularizer=regularizers.l2(1e-04)
                  )(z)
        model = Model(inputs=inpq0, outputs=z)
        opti = Adam(lr=1e-03, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
        model.compile(loss=sparse_categorical_crossentropy,
                      optimizer=opti,
                      metrics=['acc'])
        return model

if __name__ == '__main__':
    from utils.batch_generator import BathGenBasic
    a = np.memmap('./data.mat', 'float32', shape=(20000,1),mode='w+')
    a[:] = np.random.randint(0,10,(20000,1))[:]
    l =  np.squeeze(a)
    m = MLPBasicClassifier(10,input_dim=1).build_model()