text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division
from __future__ import print_function

from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import cross_val_score

import itertools
import multiprocessing
from functools import partial

import timeit
from time import time as now

__author__  = ['Nico Curti']
__email__   = ['nico.curti2@unibo.it']

NUM_REPEATS = 3
NUMBER = 10

def couple_evaluation (couple, data, labels):
  f1, f2 = couple

  samples = data[:, [f1, f2]]
  score = cross_val_score(GaussianNB(), samples, labels,
                          cv=LeaveOneOut(), n_jobs=1).mean()
  return (f1, f2, score * 100.)

def couple_pooling (data, labels, nth):

  _, Nfeature = data.shape

  couples = itertools.combinations(range(0, Nfeature), 2)

  couple_eval = partial(couple_evaluation, data=data, labels=labels)

  with multiprocessing.Pool(nth) as pool:
    scores = zip(*pool.map(couple_eval, couples))

  scores = sorted(scores, key=lambda x : x[2], reverse=True)

  return scores

def python_version (Nsample, Nfeature, seed, nth):

  SETUP_CODE = '''
from __main__ import couple_pooling
from sklearn.datasets import make_classification

data, label = make_classification(n_samples={Nsample},
                                n_features={Nfeature},
                                n_classes=2,
                                n_clusters_per_class=1,
                                class_sep=1.,
                                scale=1.,
                                shuffle=False,
                                random_state={seed}
                                )
  '''.format(**{'Nsample':Nsample, 'Nfeature':Nfeature, 'seed':seed})

  TEST_CODE = '''
couple_pooling (data, label, {nth})
  '''.format(**{'nth':nth})

  times = timeit.repeat(setup=SETUP_CODE,
                        stmt=TEST_CODE,
                        repeat=NUM_REPEATS,
                        number=NUMBER)

  return times

def cpp_version (Nsample, Nfeature, seed, nth):

  SETUP_CODE = '''
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import LeaveOneOut
from DNetPRO import DNetPRO
from sklearn.datasets import make_classification

data, label = make_classification(n_samples={Nsample},
                                n_features={Nfeature},
                                n_classes=2,
                                n_clusters_per_class=1,
                                class_sep=1.,
                                scale=1.,
                                shuffle=False,
                                random_state={seed}
                                )

dnet = DNetPRO(estimator=GaussianNB(), cv=LeaveOneOut(), n_jobs={nth}, verbose=False)
  '''.format(**{'Nsample':Nsample, 'Nfeature':Nfeature, 'seed':seed, 'nth':nth})

  TEST_CODE = '''
dnet._evaluate_couples(data, label)
  '''

  times = timeit.repeat(setup=SETUP_CODE,
                        stmt=TEST_CODE,
                        repeat=NUM_REPEATS,
                        number=NUMBER)

  return times

if __name__ == '__main__':

  import numpy as np

  available_cores = multiprocessing.cpu_count()

  nths = range(2, available_cores + 2, 2)
  Nsamples = range(10, 100, 10)
  Nfeatures = range(10, 100, 10)

  parameters = itertools.product(nths, Nsamples, Nfeatures)

  with open('DNetPRO_timing.dat', 'w') as fp:

    row = 'num_repeats,number,samples,features,nth,py_mean,py_max,py_min,py_std,cpp_mean,cpp_max,cpp_min,cpp_std'
    fp.write(row + '\n')

    for seed, (nth, Nsample, Nfeature) in enumerate(parameters):

      print('Evaluating (sample={}, feature={}, nth={}) ...'.format(Nsample, Nfeature, nth),
           end='', flush=True)

      py_tic = now()

      times = python_version(Nsample, Nfeature, seed, nth)

      py_mean = np.mean(times)
      py_max  = np.max(times)
      py_min  = np.min(times)
      py_std  = np.std(times)

      py_toc = now()

      cpp_tic = now()

      times = cpp_version(Nsample, Nfeature, seed, nth)

      cpp_mean = np.mean(times)
      cpp_max  = np.max(times)
      cpp_min  = np.min(times)
      cpp_std  = np.std(times)

      cpp_toc = now()

      save = '{NUM_REPEATS},{NUMBER},{Nsample},{Nfeature},{NTH},\
{PY_MEAN:.3f},{PY_MAX:.3f},{PY_MIN:.3f},{PY_STD:.3f},\
{CPP_MEAN:.3f},{CPP_MAX:.3f},{CPP_MIN:.3f},{CPP_STD:.3f}'.format(
              **{'NUM_REPEATS':NUM_REPEATS, 'NUMBER':NUMBER,
                 'Nsample':Nsample, 'Nfeature':Nfeature, 'NTH':nth,
                 'PY_MEAN':py_mean, 'PY_MAX':py_max, 'PY_MIN':py_min, 'PY_STD':py_std,
                 'CPP_MEAN':cpp_mean, 'CPP_MAX':cpp_max, 'CPP_MIN':cpp_min, 'CPP_STD':cpp_std
                 })

      fp.write(save + '\n')

      print('  took {:.3f} seconds'.format(cpp_toc - py_tic), flush=True)

max_stars_repo_name: Nico-Curti/DNetPRO

max_issues_repo_name: Nico-Curti/DNetPRO

max_forks_repo_name: Nico-Curti/DNetPRO


text:
"""This module contains various linear policies.
"""

from policies import StochasticPolicy
import numpy as np
import modprop

class LinearPolicy(StochasticPolicy):
    """A policy class that computes a mean and covariance from linear products.

    The output is Gaussian-distributed with mean = A*x and covariance = np.diag(B*x)
    for vector state x

    Parameters
    ----------
    input_dim  : integer
        The input vector dimension
    output_dim : integer
        The output vector dimension

    Properties
    ----------
    A : numpy 2D-array
        Maps state to output mean
    B : numpy 2D-array
        Maps state to log variances
    offset : float or numpy 2D-array
        Offset to add to the covariance
    """

    def __init__(self, input_dim, output_dim, offset=1E-9):
        super(LinearPolicy, self).__init__(input_dim, output_dim)

        A = np.zeros((output_dim, input_dim))
        B = np.zeros((output_dim, input_dim))

        self._Amod = modprop.ConstantModule(A)
        self._Bmod = modprop.ConstantModule(B)
        self._state = modprop.ConstantModule(None)
        offset = np.atleast_2d(offset)
        if offset.shape != (output_dim, output_dim):
            offset = offset[0, 0] * np.identity(output_dim)
        self._covOffset = modprop.ConstantModule(offset)

        self._Ax = modprop.MatrixProductModule()
        modprop.link_ports(in_port=self._Ax.left_port,
                           out_port=self._Amod.out_port)
        modprop.link_ports(in_port=self._Ax.right_port,
                           out_port=self._state.out_port)

        self._Bx = modprop.MatrixProductModule()
        modprop.link_ports(in_port=self._Bx.left_port,
                           out_port=self._Bmod.out_port)
        modprop.link_ports(in_port=self._Bx.right_port,
                           out_port=self._state.out_port)

        self._expBx = modprop.ExponentialModule()
        modprop.link_ports(in_port=self._expBx.in_port,
                           out_port=self._Bx.out_port)

        self._expBxd = modprop.DiagonalReshapeModule()
        modprop.link_ports(in_port=self._expBxd.vec_in,
                           out_port=self._expBx.out_port)

        self._expBxd_off = modprop.AdditionModule()
        modprop.link_ports(in_port=self._expBxd_off.left_port,
                           out_port=self._expBxd.diag_out)
        modprop.link_ports(in_port=self._expBxd_off.right_port,
                           out_port=self._covOffset.out_port)

        self._act = modprop.ConstantModule(None)
        self._delAct = modprop.DifferenceModule()
        modprop.link_ports(in_port=self._delAct.left_port,
                           out_port=self._act.out_port)
        modprop.link_ports(in_port=self._delAct.right_port,
                           out_port=self._Ax.out_port)

        self._ll = modprop.LogLikelihoodModule()
        modprop.link_ports(in_port=self._ll.x_in,
                           out_port=self._delAct.out_port)
        modprop.link_ports(in_port=self._ll.S_in,
                           out_port=self._expBxd_off.out_port)

        self._llSink = modprop.SinkModule()
        modprop.link_ports(in_port=self._llSink.in_port,
                           out_port=self._ll.ll_out)

    def sample_action(self, state):
        self.__invalidate()
        self.__foreprop(state=state, action=None)
        return np.random.multivariate_normal(mean=self.mean,
                                             cov=self.cov)

    def logprob(self, state, action):
        self.__invalidate()
        self.__foreprop(state=state, action=action)
        return self._llSink.value

    def gradient(self, state, action):
        self.__invalidate()
        self.__foreprop(state=state, action=action)
        acc = modprop.AccumulatedBackprop(do_dx=np.identity(1))
        self._llSink.backprop_value = acc
        modprop.iterative_backprop(self._llSink)

        return np.hstack((self._Amod.backprop_value[0],
                          self._Bmod.backprop_value[0]))

    def __foreprop(self, state, action):
        self._act.value = np.atleast_1d(action)
        self._state.value = np.atleast_1d(state)
        modprop.iterative_foreprop(self._state)
        if action is not None:
            modprop.iterative_foreprop(self._act)
        modprop.iterative_foreprop(self._Amod)
        modprop.iterative_foreprop(self._Bmod)
        modprop.iterative_foreprop(self._covOffset)

    def __invalidate(self):
        modprop.iterative_invalidate(self._state)
        modprop.iterative_invalidate(self._act)
        modprop.iterative_invalidate(self._Amod)
        modprop.iterative_invalidate(self._Bmod)
        modprop.iterative_invalidate(self._covOffset)

    def get_theta(self):
        return np.hstack((self._Amod.value.flatten('F'),
                          self._Bmod.value.flatten('F')))

    def set_theta(self, t):
        n_A = len(self._Amod.value.flat)
        n_B = len(self._Bmod.value.flat)
        if n_A + n_B != len(t):
            raise ValueError('Parameter dimension mismatch!')
        self._Amod.value = np.reshape(
            t[:n_A], self._Amod.value.shape, order='F')
        self._Bmod.value = np.reshape(
            t[n_A:], self._Bmod.value.shape, order='F')

    @property
    def A(self):
        return self._Amod.value

    @A.setter
    def A(self, a):
        self._Amod.value = a

    @property
    def B(self):
        return self._Bmod.value

    @B.setter
    def B(self, b):
        self._Bmod.value = b

    @property
    def mean(self):
        return self._Ax.out_port.value

    @property
    def cov(self):
        return self._expBxd_off.out_port.value

class FixedLinearPolicy(StochasticPolicy):
    """A policy class that computes a mean with fixed covariance from linear products.

    The output is Gaussian-distributed with mean = A*x and covariance = np.diag(b)
    for vector state x

    Parameters
    ----------
    input_dim  : integer
        The input vector dimension
    output_dim : integer
        The output vector dimension

    Properties
    ----------
    A : numpy 2D-array
        Maps state to output mean
    b : numpy 1D-array
        Constant log variances
    offset : float or numpy 2D-array
        Offset to add to the covariance
    """

    def __init__(self, input_dim, output_dim, offset=1E-9):
        super(FixedLinearPolicy, self).__init__(input_dim, output_dim)

        A = np.zeros((output_dim, input_dim))
        b = np.zeros(output_dim)

        self._Amod = modprop.ConstantModule(A)
        self._Bmod = modprop.ConstantModule(b)
        self._state = modprop.ConstantModule(None)
        offset = np.atleast_2d(offset)
        if offset.shape != (output_dim, output_dim):
            offset = offset[0, 0] * np.identity(output_dim)
        self._covOffset = modprop.ConstantModule(offset)

        self._Ax = modprop.MatrixProductModule()
        modprop.link_ports(in_port=self._Ax.left_port,
                           out_port=self._Amod.out_port)
        modprop.link_ports(in_port=self._Ax.right_port,
                           out_port=self._state.out_port)

        self._expBx = modprop.ExponentialModule()
        modprop.link_ports(in_port=self._expBx.in_port,
                           out_port=self._Bmod.out_port)

        self._expBxd = modprop.DiagonalReshapeModule()
        modprop.link_ports(in_port=self._expBxd.vec_in,
                           out_port=self._expBx.out_port)

        self._expBxd_off = modprop.AdditionModule()
        modprop.link_ports(in_port=self._expBxd_off.left_port,
                           out_port=self._expBxd.diag_out)
        modprop.link_ports(in_port=self._expBxd_off.right_port,
                           out_port=self._covOffset.out_port)

        self._act = modprop.ConstantModule(None)
        self._delAct = modprop.DifferenceModule()
        modprop.link_ports(in_port=self._delAct.left_port,
                           out_port=self._act.out_port)
        modprop.link_ports(in_port=self._delAct.right_port,
                           out_port=self._Ax.out_port)

        self._ll = modprop.LogLikelihoodModule()
        modprop.link_ports(in_port=self._ll.x_in,
                           out_port=self._delAct.out_port)
        modprop.link_ports(in_port=self._ll.S_in,
                           out_port=self._expBxd_off.out_port)

        self._llSink = modprop.SinkModule()
        modprop.link_ports(in_port=self._llSink.in_port,
                           out_port=self._ll.ll_out)

    def sample_action(self, state):
        self.__invalidate()
        self.__foreprop(state=state, action=None)
        return np.random.multivariate_normal(mean=self.mean,
                                             cov=self.cov)

    def logprob(self, state, action):
        self.__invalidate()
        self.__foreprop(state=state, action=action)
        return self._llSink.value

    def gradient(self, state, action):
        self.__invalidate()
        self.__foreprop(state=state, action=action)
        acc = modprop.AccumulatedBackprop(do_dx=np.identity(1))
        self._llSink.backprop_value = acc
        modprop.iterative_backprop(self._llSink)

        return np.hstack((self._Amod.backprop_value[0],
                          self._Bmod.backprop_value[0]))

    def __foreprop(self, state, action):
        self._act.value = np.atleast_1d(action)
        self._state.value = np.atleast_1d(state)
        modprop.iterative_foreprop(self._state)
        if action is not None:
            modprop.iterative_foreprop(self._act)
        modprop.iterative_foreprop(self._Amod)
        modprop.iterative_foreprop(self._Bmod)
        modprop.iterative_foreprop(self._covOffset)

    def __invalidate(self):
        modprop.iterative_invalidate(self._state)
        modprop.iterative_invalidate(self._act)
        modprop.iterative_invalidate(self._Amod)
        modprop.iterative_invalidate(self._Bmod)
        modprop.iterative_invalidate(self._covOffset)

    def get_theta(self):
        return np.hstack((self._Amod.value.flatten('F'),
                          self._Bmod.value))

    def set_theta(self, t):
        n_A = len(self._Amod.value.flat)
        n_b = len(self._Bmod.value)
        if n_A + n_b != len(t):
            raise ValueError('Parameter dimension mismatch!')
        self._Amod.value = np.reshape(
            t[:n_A], self._Amod.value.shape, order='F')
        self._Bmod.value = t[n_A:]

    @property
    def A(self):
        return self._Amod.value

    @A.setter
    def A(self, a):
        self._Amod.value = a

    @property
    def B(self):
        return self._Bmod.value

    @B.setter
    def B(self, bv):
        self._Bmod.value = bv

    @property
    def mean(self):
        return self._Ax.out_port.value

    @property
    def cov(self):
        return self._expBxd_off.out_port.value

class DeterministicLinearPolicy(StochasticPolicy):
    """A non-stochastic linear policy that outputs
    action = A * state.
    """
    def __init__(self, input_dim, output_dim):
        super(DeterministicLinearPolicy, self).__init__(indim=input_dim,
                                                        outdim=output_dim)
        self.A = np.zeros((output_dim, input_dim))

    def sample_action(self, state):
        return np.dot(self.A, state)

    def logprob(self, state, action):
        a = self.sample_action(state)
        if np.any(a != action):
            return float('-inf')
        else:
            return 0

    def gradient(self, state, action):
        return np.zeros(len(self.get_theta()))

    def get_theta(self):
        return self.A.flatten()

    def set_theta(self, a):
        self.A = np.reshape(a, self.A.shape)

ext: py

max_stars_repo_name: Humhu/percepto

max_issues_repo_name: Humhu/percepto

max_forks_repo_name: Humhu/percepto

reason: import numpy


text:
import requests
import json
import numpy as np
import time

arr_old=[2,3]
m=0
while 1:
  response = requests.get("https://api.shodan.io/shodan/host/search?key=##SHODANKEYHERE##&query=WHATUPTIME"
              ).text
  data = json.loads(response)

  arr_new=[]

  for i in range(100):
    arr_new.append(np.array(data['matches'][i]['ip_str']))

  print(arr_new)

  def Diff(li1, li2):
      li_dif = [i for i in li1 + li2 if i not in li1 or i not in li2]
      return li_dif

  print(Diff(arr_new, arr_old))
  arr_old=arr_new
  print("Loop")
  print(m)
  m=m+1
  time.sleep(600)

max_stars_repo_name: MayThirtyOne/Shodan-RDP-Exploit

max_issues_repo_name: MayThirtyOne/Shodan-RDP-Exploit

max_forks_repo_name: MayThirtyOne/Shodan-RDP-Exploit


text:
import numpy as np
import astropy.units as u
from ..interpu import interpu

def test_interpu():
    x = np.arange(20) * u.km
    y = x**3 - 3*x**2*u.km + 5*u.km**3

    points = np.random.rand(10)*20.

    result0 = np.interp(points, x.value, y.value)

    points1 = points*u.km
    points1 = points1.to(u.imperial.mi)
    result1 = interpu(points1, x, y)

    assert np.all(np.abs(result0 - result1.value) < 1e-8)

max_stars_repo_name: mburger-stsci/mathMB

max_issues_repo_name: mburger-stsci/mathMB

max_forks_repo_name: mburger-stsci/mathMB

reason: import numpy,import astropy


text:
#!/usr/bin/python3
#https://github.com/eriklindernoren/Keras-GAN#gan
#https://towardsdatascience.com/gan-by-example-using-keras-on-tensorflow-backend-1a6d515a60d0

import matplotlib.pyplot
import numpy
import keras

#Import Data
from keras.datasets import mnist
(X, _), (_, _) = mnist.load_data()						# Import data
X = X / 127.5 - 1.								# Make all tensor values between -1 and 1
X = numpy.expand_dims(X, axis=3)						# Add an extra dimention for a channel
img_shape = (28, 28, 1)								# Identify shape of data
latent_dim = 100								# Size of noise
batch_size = 32
epochs = 30000

#Discriminator
D = keras.models.Sequential()
D.add(keras.layers.Flatten(input_shape=img_shape))				# For am image flatten the tensor into a vector
D.add(keras.layers.Dense(512))
D.add(keras.layers.advanced_activations.LeakyReLU(alpha=0.2))
D.add(keras.layers.Dense(256))
D.add(keras.layers.advanced_activations.LeakyReLU(alpha=0.2))
D.add(keras.layers.Dense(1, activation='sigmoid'))				# Output is either True or False
D.summary()

#Generator
G = keras.models.Sequential()
G.add(keras.layers.Dense(256, input_dim=latent_dim))				# Input noise
G.add(keras.layers.advanced_activations.LeakyReLU(alpha=0.2))
G.add(keras.layers.BatchNormalization(momentum=0.8))
G.add(keras.layers.Dense(512))
G.add(keras.layers.advanced_activations.LeakyReLU(alpha=0.2))
G.add(keras.layers.BatchNormalization(momentum=0.8))
G.add(keras.layers.Dense(1024))
G.add(keras.layers.advanced_activations.LeakyReLU(alpha=0.2))
G.add(keras.layers.BatchNormalization(momentum=0.8))
G.add(keras.layers.Dense(numpy.prod(img_shape), activation='tanh'))		# Output nodes for each data item in data vector
G.add(keras.layers.Reshape(img_shape))						# Reshape output vector to shape of image
G.summary()

#Discriminator Model
DM = keras.models.Sequential()
DM.add(D)
DM.compile(loss='binary_crossentropy', optimizer=keras.optimizers.RMSprop(lr=0.0008, clipvalue=1.0, decay=6e-8), metrics=['accuracy'])

#Adversarial Model
AM = keras.models.Sequential()
AM.add(G)
AM.add(D)
AM.compile(loss='binary_crossentropy', optimizer=keras.optimizers.RMSprop(lr=0.0004, clipvalue=1.0, decay=3e-8), metrics=['accuracy'])

#Training
for epoch in range(epochs):
	#Generate a fake image
	images_train = X[numpy.random.randint(0, X.shape[0], size=batch_size)]	# Generate a real image tensor with shape (batch size, row, column, channel)
	noise = numpy.random.uniform(-1.0, 1.0, size=[batch_size, 100])		# Generate a noise tensor with shape (Batch size, length of noise vector[100]) and fill it with random numbers between -1 and 1
	images_fake = G.predict(noise)						# Use noise tensor to generate a prediction (batch size of fake images) from the Generator neural network, the output shape of course will be (batch size, row, columns, channel)
	#Train discriminator
	x = numpy.concatenate((images_train, images_fake))			# Combine the real image tensor (from the dataset) with the fake images tensor (from the generator) to generate a dataset with real images and fake images
	y = numpy.ones([2*batch_size, 1])					# Generate the labels tensor with twice the size of the batch (because we combined two batches real and fake) and give them all the label of 1 (True)
	y[batch_size:, :] = 0							# For real images keep 1 (True) but for fake images replace 1 with 0 (False)
	d_loss = DM.train_on_batch(x, y)					# Feed the training (real+fake) and label sets into the Discriminator neural network to train it to find the difference between fake and real data
	#Train adversarial
	y = numpy.ones([batch_size, 1])						# After the Discriminator is trained, now generate a new labels tensor and give it all the label of 1 (True)
	a_loss = AM.train_on_batch(noise, y)					# Train Adversarial neural network, can the discriminator find the fake image from the noise or not? if yes update weights of generator to become better at faking images
	D_loss = round(float(d_loss[0]), 3)
	D_accu = round(float(d_loss[1]), 3)
	A_loss = round(float(a_loss[0]), 3)
	print ('{} [D loss: {}, accuracy: {}] [G loss: {}]'.format(epoch, D_loss, D_accu, A_loss))

#Save Model
G.save_weights('GAN.h5')
#Load model and weights
#G.load_weights('GAN.h5')

#Generate 25 Images
r, c = 5, 5									# Rows, columns number of images (5 images by 5 images = 25 images)
noise = numpy.random.normal(0, 1, (r * c, 100))					# Generate a noise matrix, with a shape of (number of images , number of noise items per image), and values between -1 and 1, because loc = 0 (center of distribution) and scale = 1 (standard deviation, i.e: +1 and -1 from 0), the size will be 25 images (5*5) each with 100 items of noise
gen_imgs = G.predict(noise)							# Generate an image (predition) from the neural network using the noise matrix as input (number of images , rows of each image , columns of each image , channels)
gen_imgs = 0.5 * gen_imgs + 0.5							# Rescale images from -1.0-1.0 to 0.0-1.0
fig, axs = matplotlib.pyplot.subplots(r, c)					# Setup a subplot [25 plots (images) within a plot]
cnt = 0										# Count images
for i in range(r):								# For number of row images
	for j in range(c):							# For number of column images
		axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')		# Plot tensors
		axs[i,j].axis('off')						# Remove plot axis
		cnt += 1							# Add count for image number
matplotlib.pyplot.show()							# Show the plot with the images
#fig.savefig('generated.png')							# Save plot as a .png image file
matplotlib.pyplot.close()							# Close plot
#from IPython.display import Image ; Image("generated.png")			# Show Image in Jupyter Notebook
'''
#Generate an Image
noise = numpy.random.normal(0, 1, (1, 100))					# Generate a noise matrix, with a shape of (number of images , number of noise items per image), and values between -1 and 1, because loc = 0 (center of distribution) and scale = 1 (standard deviation, i.e: +1 and -1 from 0), the size will be 1 image with 100 items of noise
gen_imgs = G.predict(noise)							# Generate an image (predition) from the neural network using the noise matrix as input (number of images , rows of each image , columns of each image , channels)
gen_imgs = 0.5 * gen_imgs + 0.5							# Rescale images from -1.0-1.0 to 0.0-1.0
gen_imgs = gen_imgs.reshape([28, 28])						# remove batch number and channels (only keep rows and columns)
matplotlib.pyplot.imshow(gen_imgs, cmap='gray')					# Plot tensor with gray scale rather than colours
matplotlib.pyplot.axis('off')							# Remove plot axis
matplotlib.pyplot.show()							# Show the plot with the image
#matplotlib.pyplot.savefig('generated.png')					# Save plot as a .png image file
'''

''' # For RamaNet
real = X[np.random.randint(0, X.shape[0], size=batchs)]					# Real data from dataset, taking random sections from the dataset, shape is (batch size - number of examples, amino acid size, channels)
noise = np.random.uniform(0.0, 1.0, size=[batchs, 100])					# Noise data between 0 and 1 (because dataset is normalised at 0-1), shape is (batch size, noise size)
fake = G.predict(noise)													# Take noise and generate a tensor that looks like real data, shape is same as real data (batch size, amino acids size, channels)
# Now I have real data and fake data
x = np.concatenate((real, fake))										# Combine both real and fake data into 1 tensor, thus a dataset that contains both real and fake data
y = np.ones([2*batchs, 1])												# Label all real data with 1 (true)
y[batchs:, :] = 0														# Label all fake data with 0 (false)
# Now I have a labeled real and fake data
d_loss = DM.train_on_batch(x, y)										# Train Discriminator to learn what is real and what is fake data
# After training the D now it is time to update the G
y = np.ones([batchs, 1])												# Make a new label of all true, indipendent of previous y label
a_loss = AM.train_on_batch(noise, y)									# Train both D and G: use the generated noise with True label and train the generator
'''

max_stars_repo_path: Machine Learning/GAN-1.py

max_stars_repo_name: WenyueDai/Notes

max_issues_repo_path: Machine Learning/GAN-1.py

max_issues_repo_name: WenyueDai/Notes

max_forks_repo_path: Machine Learning/GAN-1.py

max_forks_repo_name: WenyueDai/Notes


text:
'''An implement of Kuhn Poker game.

We have two players. We have three cards: Jack, Queen, King, where King beats Queen and Queen beats
Jack. In this game, each player gets a random card. On a turn, a player can either

* pass: do nothing. The opponent wins all chip in the pot.
* bet: place a chip in the pot.

After two passes or two bets (not necessarily consecutive), the game ends. Player with the highest card wins.

The Maximum depth of the game tree is 3. The game tree is as follows.
Game tree::

                [chance]
                   |
              assign cards
                   |
                [player1]
                /      \\

            [pass]      [bet]
             |             |
          [player2]       [player2]
            /   \\         /   \\
          pass    bet   pass     bet
           |      |       |         |
           (h)  [player1]   (1)       (h)
                /   \\

              pass   bet
                |     |
                (2)   (h)

Leaf nodes notation:
    * (h): player has highest card wins!
    * (1): player 1 wins! (player 2 chickens out)
    * (2): player 2 wins! (player 1 chickens out)

In this game, we have 12 info-sets. Each info-set has 2 states since the opponent can have any of the
two cards that I'm not already holding. We have 4 decision nodes (excluding the chance's action),
and a player's private information consists of their own card, which takes 3 possible values so we have 3 * 4 = 12 info sets.

Note:
    There's a subtle difference between the number of info sets and the number of nodes per info set
    (the size of the info set). The size of the info set tells us how many worlds are consistent with the player's observations so far.
    The number of info sets is more "meta". A player, without entering into a game / trajectory,
    can reason ahead about the different info sets that they can be in once some partial observations are available to them when
    the game starts.
'''

from enum import Enum
from enum import IntEnum
from typing import Sequence

import numpy as np

from imperfecto.algos.player import Player
from imperfecto.games.game import ExtensiveFormGame
from imperfecto.misc.utils import lessVerboseEnum

class KUHN_POKER_ACTIONS(lessVerboseEnum, IntEnum):
    """Available actions in Kuhn Poker."""
    PASS = 0
    BET = 1

class KUHN_POKER_CHANCE_ACTIONS(lessVerboseEnum, IntEnum):
    """Availble chance actions in Kuhn Poker.

    We have 3 cards J, Q, K dealt randomly to 2 players.
    """
    JQ = 0  # player 2 wins
    JK = 1  # player 2 wins
    QK = 2  # player 2 wins
    QJ = 3  # player 1 wins
    KJ = 4  # player 1 wins
    KQ = 5  # player 1 wins

class KuhnPokerGame(ExtensiveFormGame):
    """A Kuhn Poker (https://en.wikipedia.org/wiki/Kuhn_poker) game class.

    Payoff:
        The winner wins whatever is in the pot. A player can win either in showdown by having the
        highest card or win by having their opponent chickens out.

    Nash equilibrium:
        There are infinitely many Nash equilibria. Player one can choose any probability ``alpha``
        in [0, 1/3] to bet when having Jack, and pass when the other play bets. They should bet with
        probability ``3 * alpha`` when having a King, and if the other player bets, they should bet.
        They should always pass when having a Queen, and if the other player bets, they should pass.
        The second player should always bet when having a King; when having a Queen, check if possible
        otherwise call with probability 1/3. When they have a Jack, they should never call and bet with
        probability 1/3. The first player's expected payoff under Nash equilibria is -1/18.
    """
    actions = KUHN_POKER_ACTIONS
    chance_actions = KUHN_POKER_CHANCE_ACTIONS
    n_players = 2
    has_chance_player = True

    def is_terminal(self, history: Sequence[IntEnum]):
        history_str = self.history_to_str(history[1:])  # ignore chance node
        terminal_nodes = ["PASS-PASS", "BET-BET", "BET-PASS",
                          "PASS-BET-PASS", "PASS-BET-BET"]
        return history_str in terminal_nodes

    def get_active_player(self, history: Sequence[IntEnum]) -> Player:
        return self.players[(len(history)-1) % 2]

    def get_winner(self, chance_action: KUHN_POKER_CHANCE_ACTIONS) -> int:
        """Return the winner of the game given a chance action.

        Args:
            chance_action: the chance action

        Returns:
            the winner (player_id) of the game
        """
        if chance_action.value < 3:
            return 1
        return 0

    def showdown(self, history) -> np.ndarray:
        """
        Return the payoff value of the game in the showdown case given a history.

        Winner is the player with the highest card. We don't take into account the amount of chips
        in the pot. Winner gets +1 point while loser gets -1 point.

        Args:
            history: the history of the game

        Returns:
            the payoff value of the game in the showdown case
        """
        winner = self.get_winner(history[0])
        return np.array([1, -1]) if winner == 0 else np.array([-1, 1])

    def get_payoffs(self, history):
        assert self.is_terminal(history)
        match '-'.join(map(str, history[1:])):
            case "PASS-PASS":
                return 1 * self.showdown(history)  # wins 2 chips
            case "BET-BET":
                return 2 * self.showdown(history)  # wins 4 chips
            case "BET-PASS":
                return np.array([1, -1])  # player 2 forfeits, player 1 wins
            case "PASS-BET-PASS":
                return np.array([-1, 1])  # player 1 forfeits, player 2 wins
            case "PASS-BET-BET":
                return 2 * self.showdown(history)  # wins 4 chips
        raise Exception("Invalid history")

    def get_card(self, chance_action: KUHN_POKER_CHANCE_ACTIONS, player_id: int) -> str:
        """Return the card that a chance action has dealt to a player.

        Args:
            chance_action: a chance action
            player_id: the player who receives the card

        Returns:
            the card that the chance action has dealt to the player
        """
        return chance_action.name[player_id]

    def get_infostate(self, history: Sequence[Enum]) -> str:
        player_id = self.get_active_player(history).player_id  # type: ignore
        my_card = self.get_card(history[0], player_id)  # type: ignore
        return my_card + "-" + "-".join(map(str, history[1:]))

    def chance_action(self) -> KUHN_POKER_CHANCE_ACTIONS:
        """Return a chance action.

        We have 3 cards J, Q, K dealt randomly to 2 players. This function handles the card dealing.

        Returns:
            a chance action
        """
        return KUHN_POKER_CHANCE_ACTIONS(np.random.choice(range(len(KUHN_POKER_CHANCE_ACTIONS))))

    @staticmethod
    def shorten_history(history_str: str) -> str:
        """Shorten history string. For example, "KJ-PASS-BET" -> "KJPB".
        Args:
            history_str: history string

        Returns:
            A shortened history string
        """
        s = ''.join(str(a) for a in history_str)
        replacements = {'-': '', 'PASS': 'P', 'BET': 'B'}
        for k, v in replacements.items():
            s = s.replace(k, v)
        return s + ' ' * (5-len(s))

max_stars_repo_name: vlongle/Imperfecto

max_issues_repo_name: vlongle/Imperfecto

max_forks_repo_name: vlongle/Imperfecto


text:
# mpcformat/tests/test_obs80.py

# Third-party imports
import numpy
import astropy.coordinates

# Import the specific package/module/function we are testing
import mpcformat.obs80 as obs80

def test_o1():
    o = obs80.parseOpt('00433         A1893 10 29.4132  06 08 59.32 +53 39 04.2                 HA053802')  # noqa E501
    assert o.num == '00433'
    assert o.desig == ''
    assert o.disc == ''
    assert o.note1 == ''
    assert o.note2 == 'A'
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 1893
    assert t.month == 10
    assert t.day == 29
    assert numpy.isclose(.4132, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 6
    assert m == 8
    assert numpy.isclose(s, 59.32)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == 1
    assert d == 53
    assert m == 39
    assert numpy.isclose(s, 4.2)
    assert o.mag is None
    assert o.band == ''
    assert o.cod == "802"

def test_o2():
    """negative dec"""
    o = obs80.parseOpt('00433         A1894 02 17.1174  07 36 21.35 -00 38 13.2                 HA053802')  # noqa E501
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == -1
    assert d == 0
    assert m == 38
    assert numpy.isclose(s, 13.2)

def test_o3():
    """decimal minutes in RA, missing seconds in dec, mag, no band"""
    o = obs80.parseOpt('00433         A1898 08 13.89338 21 33.8     -05 58               10.0   AN147020')  # noqa E501
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 21
    assert numpy.isclose(m + s / 60., 33.8)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == -1
    assert d == 5
    assert m == 58
    assert numpy.isclose(s, 0)
    assert o.mag == 10
    assert o.band == ''

def test_o4():
    """one decimal place in sec of ra, no decimal places in sec dec"""
    o = obs80.parseOpt('00433         A1898 08 14.96732 21 31 50.8  -05 57 43            11.0   AN147537')  # noqa E501
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 21
    assert m == 31
    assert numpy.isclose(s, 50.8)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == -1
    assert d == 5
    assert m == 57
    assert numpy.isclose(s, 43)

def test_o5():
    """empty note2"""
    o = obs80.parseOpt('00433          1898 09 20.88897 20 42 53.05 -05 59 42.4                m24429000')  # noqa E501
    assert o.note2 == ''

def test_o6():
    """both perm and prov, disc *, very coarse time, ra, dec, one dp in mag"""
    o = obs80.parseOpt('00433J56P00C* A1956 08 07.14    19 43.6     -20 47               14.4   M1581760')  # noqa E501
    assert o.num == '00433'
    assert o.desig == 'J56P00C'
    assert o.disc == '*'
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 1956
    assert t.month == 8
    assert t.day == 7
    assert numpy.isclose(.14, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 19
    assert numpy.isclose(m + s / 60., 43.6)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == -1
    assert d == 20
    assert m == 47
    assert numpy.isclose(s, 0)
    assert o.mag == 14.4

def test_o7():
    """note2"""
    o = obs80.parseOpt('00433         E1975 01 24.01889007 44 23.906+24 23 59.87                v4566244')  # noqa E501
    assert o.note2 == 'E'

def test_o8():
    """note1"""
    o = obs80.parseOpt('00433        4A1979 08 27.24896 21 15 51.51 -05 15 55.5                 M5026688')  # noqa E501
    assert o.note1 == '4'

def test_o9():
    """note2, extra precision, band"""
    o = obs80.parseOpt('00433         T1988 10 05.04698200 52 27.557+38 16 05.91         10.74V CMC05950')  # noqa E501
    assert o.note2 == 'T'
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 1988
    assert t.month == 10
    assert t.day == 5
    assert numpy.isclose(.046982, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 0
    assert m == 52
    assert numpy.isclose(s, 27.557)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == 1
    assert d == 38
    assert m == 16
    assert numpy.isclose(s, 5.91)
    assert o.mag == 10.74
    assert o.band == 'V'

def test_o10():
    """note2"""
    o = obs80.parseOpt('00433         C1993 09 29.84946 19 43 43.22 -14 20 51.0                z22620107')  # noqa E501
    assert o.note2 == 'C'

def test_s():
    o = obs80.parseSat('00433         S2016 05 18.90150 22 08 22.93 -13 53 27.3          16   RL~1tbGC51', '00433         s2016 05 18.90150 1 + 6328.2817 - 2371.9854 - 1216.2699   ~1tbGC51')  # noqa E501
    assert o.num == '00433'
    assert o.desig == ''
    assert o.disc == ''
    assert o.note1 == ''
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 2016
    assert t.month == 5
    assert t.day == 18
    assert numpy.isclose(.90150, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 22
    assert m == 8
    assert numpy.isclose(s, 22.93)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == -1
    assert d == 13
    assert m == 53
    assert numpy.isclose(s, 27.3)
    assert o.mag == 16
    assert o.band == 'R'
    assert o.cod == 'C51'
    assert numpy.isclose(o.x, 6328.2817 * 1e3 / astropy.constants.au.value)
    assert numpy.isclose(o.y, -2371.9854 * 1e3 / astropy.constants.au.value)
    assert numpy.isclose(o.z, -1216.2699 * 1e3 / astropy.constants.au.value)

def test_r1():
    o = obs80.parseRad('00433         R1975 01 22.187500  15088536000                   430 251 JPLRS251', '00433         r1975 01 22.187500C        15000                      251 JPLRS251')  # noqa E501
    assert o.num == '00433'
    assert o.desig == ''
    assert o.note1 == ''
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 1975
    assert t.month == 1
    assert t.day == 22
    assert numpy.isclose(.187500, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    assert o.delay == 150885360.00
    assert o.doppler is None
    assert o.freq == 430
    assert o.txcod == '251'
    assert o.rxcod == '251'
    assert o.ret == 'C'
    assert o.udelay == 15
    assert o.udoppler is None

def test_r2():
    o = obs80.parseRad('00433         R1975 01 23.184028               -         130    430 251 JPLRS251', '00433         r1975 01 23.184028S                        2000       251 JPLRS251')  # noqa E501
    assert o.delay is None
    assert o.doppler == -1.3
    assert o.udelay is None
    assert o.udoppler == 2

def test_v():
    o = obs80.parseRov('     K03U11V  V2003 10 23.48631 01 37 13.60 +07 20 04.3          15.4 Rci8327247', '     K03U11V  v2003 10 23.48631 1 139.6119   +35.7803      70          ci8327247')  # noqa E501
    assert o.num == ''
    assert o.desig == 'K03U11V'
    assert o.disc == ''
    assert o.note1 == ''
    t = astropy.time.Time(o.jdutc, format='jd').datetime
    assert t.year == 2003
    assert t.month == 10
    assert t.day == 23
    assert numpy.isclose(.48631, (t.hour + (t.minute + (t.second +
        t.microsecond / 1e6) / 60) / 60) / 24)
    h, m, s = astropy.coordinates.Angle(o.ra, unit='h').hms
    assert h == 1
    assert m == 37
    assert numpy.isclose(s, 13.60)
    sign, d, m, s = astropy.coordinates.Angle(o.dec, unit='deg').signed_dms
    assert sign == 1
    assert d == 7
    assert m == 20
    assert numpy.isclose(s, 4.3)
    assert o.mag == 15.4
    assert o.band == 'R'
    assert o.lon == 139.6119
    assert o.lat == 35.7803
    assert o.alt == 70

max_stars_repo_name: matthewjohnpayne/MPCFormat

max_issues_repo_name: matthewjohnpayne/MPCFormat

max_forks_repo_name: matthewjohnpayne/MPCFormat


text:
# -*- coding: utf-8 -*-

# Import base utils
from mimetypes import init
import torch
from torch import nn
import numpy as np
import pathlib
import sys

# In order to import learn_KKL we need to add the working dir to the system path
working_path = str(pathlib.Path().resolve())
sys.path.append(working_path)

# Import KKL observer
from learn_KKL.learner import Learner
from learn_KKL.system import QuanserQubeServo2
from learn_KKL.luenberger_observer_jointly import LuenbergerObserverJointly
from learn_KKL.utils import generate_mesh

# Import learner utils
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger
from sklearn.model_selection import train_test_split
import torch.optim as optim
import sys

if __name__ == "__main__":

    ##########################################################################
    # Setup observer #########################################################
    ##########################################################################
    # Learning method
    learning_method = "Autoencoder_jointly"
    num_hl = 20
    size_hl = 100
    activation = nn.ReLU()
    recon_lambda = 0.2

    # Define system
    system = QuanserQubeServo2()

    # Define data params
    x_limits = np.array([[-0.5, 0.5], [0., 6.], [-30., 30.], [-16., 16.]])
    num_samples = 80000
    init_wc = 2.

    # Create the observer (autoencoder design)
    observer = LuenbergerObserverJointly(
        dim_x=system.dim_x, dim_y=system.dim_y, method=learning_method,
        wc=init_wc, recon_lambda=recon_lambda
    )
    observer.set_dynamics(system)

    # Generate the data
    data = generate_mesh(x_limits, num_samples, method="LHS")
    data, val_data = train_test_split(data, test_size=0.2, shuffle=True)

    ##########################################################################
    # Setup learner ##########################################################
    ##########################################################################

    # Trainer options
    num_epochs = 30
    trainer_options = {"max_epochs": num_epochs}
    batch_size = 100
    init_learning_rate = 1e-3

    # Optim options
    optim_method = optim.Adam
    optimizer_options = {"weight_decay": 1e-8}

    # Scheduler options
    scheduler_method = optim.lr_scheduler.ReduceLROnPlateau
    scheduler_options = {
        "mode": "min",
        "factor": 0.1,
        "patience": 1,
        "threshold": 1e-4,
        "verbose": True,
    }
    stopper = pl.callbacks.early_stopping.EarlyStopping(
        monitor="val_loss", min_delta=5e-4, patience=3, verbose=False,
        mode="min"
    )

    # Instantiate learner
    learner = Learner(
        observer=observer,
        system=system,
        training_data=data,
        validation_data=val_data,
        method=learning_method,
        batch_size=batch_size,
        lr=init_learning_rate,
        optimizer=optim_method,
        optimizer_options=optimizer_options,
        scheduler=scheduler_method,
        scheduler_options=scheduler_options,
    )

    # Define logger and checkpointing
    logger = TensorBoardLogger(save_dir=learner.results_folder + "/tb_logs")
    checkpoint_callback = ModelCheckpoint(monitor="val_loss")
    trainer = pl.Trainer(
        callbacks=[stopper, checkpoint_callback],
        **trainer_options,
        logger=logger,
        log_every_n_steps=1,
        check_val_every_n_epoch=3,
    )

    # To see logger in tensorboard, copy the following output name_of_folder
    print(f"Logs stored in {learner.results_folder}/tb_logs")
    # which should be similar to jupyter_notebooks/runs/method/exp_0/tb_logs/
    # Then type this in terminal:
    # tensorboard --logdir=name_of_folder

    # Train and save results
    trainer.fit(learner)

    ##########################################################################
    # Generate plots #########################################################
    ##########################################################################

    learner.save_results(
         limits=np.array([[-2.5, 2.5], [-2.5, 2.5]]),
         nb_trajs=10,
         tsim=(0, 20),
         dt=1e-2,
         fast=True,
         checkpoint_path=checkpoint_callback.best_model_path,
     )


text:
import os
import zlib
import StringIO
import base64
import numpy as np;
import json

path = 'database.json'
with open(path) as dbfile:
     database = json.loads(dbfile.read())

index = 0
for label in database:
    print label['name'], label['index']
    label['index'] = index
    index += 1

# save the database
with open(path, 'w') as outfile:
     json.dump(database, outfile)


text:
import pandas as pd
import numpy as np
import sys
sys.path.insert(1, '/')
import pdm_viz.EDA.EDA as eda
sys.path.insert(1, '/app')
import plotly.graph_objects as go
import datetime

tagList = ['MV26_PDI-1535A-02.PV_mean', 'MV26_PDI-1535B-02.PV_mean']

df = pd.read_csv('/app/data/preprocessed/temp.csv').set_index('datetime')

def plotSeries(train, hr=5):
    df = pd.read_csv('/app/data/preprocessed/temp.csv').set_index('datetime')
    lastReg = datetime.datetime.strptime(np.max(df.index), '%Y-%m-%d %H:%M:%S')
    print(lastReg)
    preds = pd.read_csv('/app/backend/simulator/data/predictions/differential.csv', index_col=0)
    preds.index = pd.to_datetime(preds.index)
    preds = preds.loc[preds.index <= (lastReg + datetime.timedelta(hours=hr+1)), :]
    if train == 'A':
        plotDf = df.loc[:, [tagList[0]]]
        predDf = preds.loc[:, [tagList[0]]]
        titles = ['DP Train A']
    elif train == 'B':
        plotDf = df.loc[:, [tagList[1]]]
        predDf = preds.loc[:, [tagList[1]]]
        titles = ['DP Train B']

    ts = eda.timeSeries(plotDf, yAxisTitles=titles, logo=False)
    ts.fig.add_trace(go.Scatter(x=predDf.index, y=predDf.iloc[:, 0], line=dict(width=1, color='#DA1A5D'), mode='lines', name='Predicted'),
                                row=1, col=1)
    ts.fig.update_layout(
        title=None,
    # {'text':'<b>MV26</b>',
    #            'x': 0.5,
    #            'y': 0.9,
    #            'xanchor': 'center'},
        margin={'t': 10},
        font_family="Roboto",
        font_color="#FFFFFF",
        title_font_family="Roboto",
        title_font_color="#FFFFFF",
        title_font_size=20,
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)'
    )
    return ts.fig

def plotMEx(train):
    idx = df.index
    if train == 'A':
        plotDf = df.loc[:, [tagList[0]]].sort_values(by=tagList[0], ascending=False)
        titles = ['Remaining Usefull Life (Hours)']
    elif train == 'B':
        plotDf = df.loc[:, [tagList[1]]].sort_values(by=tagList[1], ascending=False)
        titles = ['Remaining Usefull Life (Hours)']
    plotDf.index = df.index = idx
    ts = eda.timeSeries(plotDf, yAxisTitles=titles, logo=False)
    ts.fig.update_layout(
        title=None,
        margin={'t': 10},
        font_family="Roboto",
        font_color="#FFFFFF",
        title_font_family="Roboto",
        title_font_color="#FFFFFF",
        title_font_size=20,
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)'
    )
    return ts.fig
    return

max_stars_repo_name: lucasgiutavares/docker-dash

max_issues_repo_name: lucasgiutavares/docker-dash

max_forks_repo_name: lucasgiutavares/docker-dash


text:
# Generate two SSP tables, plot spectra at selected age, met to compare

import numpy as np
import pylab as plt
from caesar.pyloser.pyloser import get_ssp_spectrum,generate_ssp_table_bpass,generate_ssp_table_bc03

colors = ['b','c','g','r']
#generate_ssp_table_bpass('../BPASS_Chab100.hdf5')
generate_ssp_table_bc03('../BC03_Chab_Padova94.hdf5')
wave = [2000,10000]  # wavelength range to plot
icolor = 0
for age in [9.0,10.0]:  # log(age/yr)
    for met in [-1.3]:  # log(Z_mass_fraction)
        wave_fsps,spec_fsps = get_ssp_spectrum('../FSPS_Chab_EL.hdf5',age,met)
        wave_bpass,spec_bpass = get_ssp_spectrum('../BC03_Chab_Padova94.hdf5',age,met)
        sel_fsps = (wave_fsps>wave[0]) & (wave_fsps<wave[1])
        sel_bpass = (wave_bpass>wave[0]) & (wave_bpass<wave[1])
        plt.plot(wave_fsps[sel_fsps],np.log10(spec_fsps[sel_fsps]),'-',c=colors[icolor],label='FSPS %g %g'%(age,met))
        icolor += 1
        plt.plot(wave_bpass[sel_bpass],np.log10(spec_bpass[sel_bpass]),':',c=colors[icolor],label='BPASS %g %g'%(age,met))
        icolor += 1

plt.xlabel('Wavelength (AA)')
plt.ylabel('Flux (Lsun/Hz)')
plt.legend()
plt.show()

max_stars_repo_name: christopherlovell/caesar

max_issues_repo_name: christopherlovell/caesar

max_forks_repo_name: christopherlovell/caesar


text:
import pickle
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import mannwhitneyu

with open("./data/jar/stage_labels.pickle", 'rb') as pickle_in:
    stage_labels = pickle.load(pickle_in)
with open('./data/jar/ng.pickle', 'rb') as pickle_in:
    ng_data = pickle.load(pickle_in)
#stage specific
ss = []
#not stage specific
non_ss = []
for dhs in ng_data.keys():
    if stage_labels[dhs] == 1:
        ss.append(ng_data[dhs])
    elif stage_labels[dhs] == 0:
        non_ss.append(ng_data[dhs])

print(stats.kruskal(ss, non_ss))
w, p = mannwhitneyu(ss, non_ss)
print("Rank: " + str(w))
print("Mann-Whitney p-value: " + str(p))
ax = plt.axes()
ax.set_title("Nearest gene distance Non-Highly Stage Specific")
plt.style.use('ggplot')
plt.xlim(0, 25000)
plt.hist(non_ss, bins=50)
plt.show()

reason: from scipy


text:
# encoding: utf-8
'''
@author: Minghao Guo
@contact: mh.guo0111@gmail.com
@software: nef
@file: trans_listmode.py
@date: 5/13/2019
@desc:
'''

import numpy as np

from srfnef.functions import ListmodeCompress
from srfnef.data import Lors, Listmode
from srfnef.geometry import PetEcatScanner

def trans_listmode(path: str, scanner: PetEcatScanner) -> Listmode:
    ''' to translate gate output lors data to trans listmode data'''
    lors_data = np.load(path)
    if lors_data.shape[1] > 6:
        print('the width of lors data is', lors_data.shape[1], ', take care')
    lors = Lors(lors_data[:, :6])
    listmode = Listmode(np.ones((lors.length,), dtype = np.float32), lors)
    listmode1 = ListmodeCompress(scanner)(listmode)
    return listmode1


text:
from sympy import zeros, eye
from .extra_dyn import frictionforce, driveinertiaterm
from ..utils import sym_skew as skew
from ..utils import identity

def Adj(G, g):
    R = G[0:3, 0:3]
    p = G[0:3, 3]
    return (R.row_join(zeros(3))).col_join(
        (skew(p) * R).row_join(R)) * g

def Adjdual(G, g):
    R = G[0:3, 0:3]
    p = G[0:3, 3]
    return ((R.row_join(zeros(3))).col_join(
        (skew(p) * R).row_join(R))).transpose() * g

def adj(g, h):
    wg = g[0:3, 0]
    vg = g[3:6, 0]
    return (skew(wg).row_join(zeros(3))).col_join(
        (skew(vg)).row_join(skew(wg))) * h

def adjdual(g, h):
    wg = g[0:3, 0]
    vg = g[3:6, 0]
    return ((skew(wg).row_join(zeros(3))).col_join(
        (skew(vg)).row_join(skew(wg)))).transpose() * h

def rne_park_forward(rbtdef, geom, ifunc=None):
    '''RNE forward pass.'''

    if not ifunc:
        ifunc = identity

    V = list(range(0, rbtdef.dof + 1))
    dV = list(range(0, rbtdef.dof + 1))

    V[-1] = zeros(6, 1)
    dV[-1] = - zeros(3, 1).col_join(rbtdef.gravityacc)

    # Forward
    for i in range(rbtdef.dof):

        V[i] = ifunc(Adj(geom.Tdh_inv[i], V[i - 1])) + \
            ifunc(geom.S[i] * rbtdef.dq[i])
        V[i] = ifunc(V[i])

        dV[i] = ifunc(geom.S[i] * rbtdef.ddq[i]) + \
            ifunc(Adj(geom.Tdh_inv[i], dV[i - 1])) + \
            ifunc(adj(ifunc(Adj(geom.Tdh_inv[i], V[i - 1])),
                  ifunc(geom.S[i] * rbtdef.dq[i])))
        dV[i] = ifunc(dV[i])

    return V, dV

def rne_park_backward(rbtdef, geom, fw_results, ifunc=None):
    '''RNE backward pass.'''

    V, dV = fw_results

    if not ifunc:
        ifunc = identity

    # extend Tdh_inv so that Tdh_inv[dof] return identity
    Tdh_inv = geom.Tdh_inv + [eye(4)]

    F = list(range(rbtdef.dof + 1))
    F[rbtdef.dof] = zeros(6, 1)

    tau = zeros(rbtdef.dof, 1)

    fric = frictionforce(rbtdef)
    Idrive = driveinertiaterm(rbtdef)

    # Backward
    for i in range(rbtdef.dof - 1, -1, -1):

        Llm = (rbtdef.L[i].row_join(skew(rbtdef.l[i]))).col_join(
            (-skew(rbtdef.l[i])).row_join(eye(3) * rbtdef.m[i]))

        F[i] = Adjdual(Tdh_inv[i + 1], F[i + 1]) + \
            Llm * dV[i] - adjdual(V[i],  Llm * V[i])
        F[i] = ifunc(F[i])

        tau[i] = ifunc((geom.S[i].transpose() * F[i])[0] + fric[i] + Idrive[i])

    return tau

reason: from sympy


text:
# =============================================================================
# Standard Python modules
# =============================================================================
import os
import copy

# =============================================================================
# External Python modules
# =============================================================================
import numpy
import unittest
from mpi4py import MPI
from parameterized import parameterized_class

# =============================================================================
# Extension modules
# =============================================================================
from idwarp import USMesh, USMesh_C
from baseclasses import BaseRegTest

baseDir = os.path.dirname(os.path.abspath(__file__))  # Path to current folder

def eval_warp(handler, test_name, meshOptions, iscomplex):
    # --- Create warping object ---
    if iscomplex is True:
        # Checking if the complex verision of the code has been built:
        try:
            from idwarp import idwarp_cs  # noqa: F401

            h = 1e-40
            mesh = USMesh_C(options=meshOptions, debug=True)
        except ImportError:
            raise unittest.SkipTest("Skipping because you do not have complex idwarp compiled")
    else:
        mesh = USMesh(options=meshOptions)

    # --- Extract Surface Coordinates ---
    coords0 = mesh.getSurfaceCoordinates()

    vCoords = mesh.getCommonGrid()
    val = MPI.COMM_WORLD.reduce(numpy.sum(vCoords.flatten()), op=MPI.SUM)
    handler.root_add_val(f"{test_name} - Sum of vCoords Inital:", val, tol=1e-8)

    new_coords = coords0.copy()

    # --- Setting the coordinates displacement for surface mesh ---
    if test_name == "Test_inflate_cube":
        # This specific test displaces the surface in every direction
        for i in range(len(coords0)):
            new_coords[i, 0] *= 1.1
            new_coords[i, 1] *= 1.2
            new_coords[i, 2] *= 1.3
    else:
        # Do a shearing sweep deflection:
        for i in range(len(coords0)):
            span = coords0[i, 2]
            new_coords[i, 0] += 0.05 * span

    # --- Reset the newly computed surface coordiantes
    mesh.setSurfaceCoordinates(new_coords)
    mesh.warpMesh()

    # --- Get the sum of the warped coordinates ---
    vCoords = mesh.getWarpGrid()

    val = MPI.COMM_WORLD.reduce(numpy.sum(vCoords.flatten()), op=MPI.SUM)
    handler.root_add_val(f"{test_name} - Sum of vCoords Warped:", val, tol=1e-8)

    # --- Create a dXv vector to do test the mesh warping with ---
    dXv_warp = numpy.linspace(0, 1.0, mesh.warp.griddata.warpmeshdof)

    if not iscomplex:
        # --- Computing Warp Derivatives ---
        mesh.warpDeriv(dXv_warp, solverVec=False)
        dXs = mesh.getdXs()

        val = MPI.COMM_WORLD.reduce(numpy.sum(dXs.flatten()), op=MPI.SUM)

        # --- Verifying Warp Deriv ---
        mesh.verifyWarpDeriv(dXv_warp, solverVec=False, dofStart=0, dofEnd=5)

    else:
        # add a complex perturbation on all surface nodes simultaneously:
        for i in range(len(coords0)):
            new_coords[i, :] += h * 1j

        # Reset the newly computed surface coordiantes
        mesh.setSurfaceCoordinates(new_coords)
        mesh.warpMesh()

        vCoords = mesh.getWarpGrid()
        deriv = numpy.imag(vCoords) / h
        deriv = numpy.dot(dXv_warp, deriv)
        val = MPI.COMM_WORLD.reduce(numpy.sum(deriv), op=MPI.SUM)

    handler.root_add_val(f"{test_name} - Sum of dxs:", val, tol=1e-8)

# --- Set up variables to be overridden at every parametrized_class loop ---
test_params = [
    {"N_PROCS": 1, "iscomplex": False},
    {"N_PROCS": 2, "name": "parallel", "iscomplex": False},
    {"N_PROCS": 1, "name": "complex", "iscomplex": True},
    {"N_PROCS": 2, "name": "parallel_complex", "iscomplex": True},
]

@parameterized_class(test_params)
class Test_USmesh(unittest.TestCase):
    """
    Defines the tests for IDwarp.
    'train_' methods are not run by default by testflo.
    Use `$ testflo ./ -m "train_*"` to re-generate the .ref files used as benchmark.
    Note that, in the current state, you will get a few FAIL messages when training - that's due to an issue with parameterize, the .ref files will be generated correctly.

    Also note that the `handler` BaseRegTest class will automatically take care to benchmark or train the tests (including file IO)
    """

    N_PROCS = 1

    def setUp(self):
        # --- Reference options to be used by every test ---
        self.defOpts = {
            "gridFile": None,
            "fileType": "CGNS",
            "aExp": 3.0,
            "bExp": 5.0,
            "LdefFact": 1.0,
            "alpha": 0.25,
            "errTol": 0.0005,
            "evalMode": "fast",
            "symmTol": 1e-6,
            "useRotations": True,
            "bucketSize": 8,
        }
        # --- Setting the ref file for parallel tests ---
        self.ref_app = ""

        if self.N_PROCS > 1:
            # Appendix for the parallel reference files - appended to every ref_file instance
            self.ref_app = "_par"

    def train_comesh(self, train=True):
        try:
            self.test_comesh(train=train)
        except AttributeError:
            raise unittest.SkipTest()

    def test_comesh(self, train=False):
        ref_file = os.path.join(baseDir, f"ref/test_comesh{self.ref_app}.ref")
        with BaseRegTest(ref_file, train=train) as handler:
            # Test the C-mesh in the input files
            test_name = "Test_co_mesh"
            file_name = os.path.join(baseDir, "../input_files/co_mesh.cgns")

            meshOptions = copy.deepcopy(self.defOpts)

            meshOptions.update({"gridFile": file_name})

            # --- Call shared computation --
            eval_warp(handler=handler, test_name=test_name, meshOptions=meshOptions, iscomplex=self.iscomplex)

    def train_omesh(self, train=True):
        try:
            self.test_omesh(train=train)
        except AttributeError:
            raise unittest.SkipTest()

    def test_omesh(self, train=False):
        ref_file = os.path.join(baseDir, f"ref/test_omesh{self.ref_app}.ref")
        with BaseRegTest(ref_file, train=train) as handler:
            # --- Test the O-mesh ---
            test_name = "Test_o_mesh"
            file_name = os.path.join(baseDir, "../input_files/o_mesh.cgns")

            meshOptions = copy.deepcopy(self.defOpts)

            meshOptions.update({"gridFile": file_name})

            # --- Call shared computation ---
            eval_warp(handler=handler, test_name=test_name, meshOptions=meshOptions, iscomplex=self.iscomplex)

    def train_sym_mesh(self, train=True):
        try:
            self.test_sym_mesh(train=train)
        except AttributeError:
            raise unittest.SkipTest()

    def test_sym_mesh(self, train=False):
        ref_file = os.path.join(baseDir, f"ref/test_sym_mesh{self.ref_app}.ref")
        with BaseRegTest(ref_file, train=train) as handler:
            # Test the symmetric mesh
            test_name = "Test_sym_mesh"
            file_name = os.path.join(baseDir, "../input_files/mdo_tutorial_face_bcs.cgns")

            meshOptions = copy.deepcopy(self.defOpts)

            meshOptions.update(
                {
                    "gridFile": file_name,
                    "symmetryPlanes": [[[0, 0, 0], [0, 0, 1]]],
                }
            )

            # --- Call shared computation --
            eval_warp(handler=handler, test_name=test_name, meshOptions=meshOptions, iscomplex=self.iscomplex)

    def train_inflate_cube(self, train=True):
        try:
            self.test_inflate_cube(train=train)
        except AttributeError:
            raise unittest.SkipTest()

    def test_inflate_cube(self, train=False):
        ref_file = os.path.join(baseDir, f"ref/test_inflate_cube{self.ref_app}.ref")
        with BaseRegTest(ref_file, train=train) as handler:
            # Test the "cube" mesh
            test_name = "Test_inflate_cube"
            file_name = os.path.join(baseDir, "../input_files/symm_block.cgns")

            meshOptions = copy.deepcopy(self.defOpts)

            meshOptions.update({"gridFile": file_name})

            # --- Call shared computation --
            eval_warp(handler=handler, test_name=test_name, meshOptions=meshOptions, iscomplex=self.iscomplex)

max_stars_repo_name: anilyil/idwarp

max_issues_repo_name: anilyil/idwarp

max_forks_repo_name: anilyil/idwarp


text:
from frbpoppy import hist
from goodness_of_fit import GoodnessOfFit
from matplotlib.lines import Line2D
from scipy.optimize import curve_fit
from tests.convenience import plot_aa_style, rel_path
import matplotlib.pyplot as plt
import numpy as np

class Plot():
    """Plot runs."""

    def __init__(self):
        plot_aa_style()
        plt.rcParams['figure.figsize'] = (5.75373, (5.75373/3)*4)
        plt.rcParams['font.size'] = 9
        # plt.rcParams['xtick.major.pad'] = 10
        # plt.rcParams['ytick.major.pad'] = 10
        # plt.rcParams['axes.titlepad'] = 10
        self.fig, self.axes = plt.subplots(4, 3, sharey='row')
        self.colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
        self.gf = GoodnessOfFit()
        self.df = self.gf.so.df

        # Calculate global maximums
        self.gm = {}
        for run in self.df.run.unique():
            self.gm[run] = self.gf.calc_global_max(run)
        print(self.gm)

        # Plot various subplots
        self.alpha()
        self.si()
        self.li()
        self.li_2()
        self.lum_min()
        self.lum_max()
        self.w_int_mean()
        self.w_int_std()
        self.legend()
        self.dm_igm_slope()
        self.dm_host()
        self.axes[3, 2].set_axis_off()

        # Plot run rectangles
        # self.runs()

        # Save plot
        plt.tight_layout()  # rect=[0, 0, 0.98, 1])
        plt.subplots_adjust(wspace=0.1)
        plt.savefig(rel_path('./plots/mc/mc.pdf'))

    def alpha(self):
        ax = self.axes[0, 0]
        parm = 'alpha'
        label = r'$\alpha$'
        runs = [1, 5, 8]

        ax.set_yscale('log', nonposy='clip')
        ax.set_ylabel(r'GoF')
        ax.set_xlabel(label)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i==1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.1f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def si(self):
        ax = self.axes[0, 1]
        parm = 'si'
        label = r'\text{si}'
        runs = [1, 5, 8]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.1f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def li(self):
        ax = self.axes[0, 2]
        parm = 'li'
        # label = r'\text{lum$_{\text{i}}$}'
        label = 'li'
        runs = [1, 5, 8]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label)
        ax_right = ax.twinx()
        ax_right.set_ylabel('Set 1', labelpad=10)
        ax_right.tick_params(axis='y', which='both', right=False, labelright=False)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.1f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def li_2(self):
        ax = self.axes[1, 0]
        ax.set_ylabel(r'GoF')
        parm = 'li'
        label = 'li'
        runs = [2]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

        # if not np.isnan(best_value):
        #     title = fr'{label}=${best_value:.1f}$'
        #     ax.set_title(title, fontsize=10, color=self.colors[i])

    def lum_min(self):
        ax = self.axes[1, 1]
        ax.set_xscale('log')
        label = r'\text{lum$_{\text{min}}$}'
        parm = 'lum_min'
        runs = [2]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label + r' (erg s$^{-1}$)')
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

        # if not np.isnan(best_value):
        #     title = fr'{label}=${best_value:.1e}$'
        #     ax.set_title(title, fontsize=10, color=self.colors[i])

    def lum_max(self):
        ax = self.axes[1, 2]
        ax.set_xscale('log')
        label = r'\text{lum$_{\text{max}}$}'
        parm = 'lum_max'
        runs = [2]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label + r' (erg s$^{-1}$)')
        ax_right = ax.twinx()
        ax_right.set_ylabel('Set 2', labelpad=10)
        ax_right.tick_params(axis='y', which='both', right=False, labelright=False)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

        # if not np.isnan(best_value):
        #     title = fr'{label}=${best_value:.1e}$'
        #     ax.set_title(title, fontsize=10, color=self.colors[i])

    def w_int_mean(self):
        ax = self.axes[2, 0]
        ax.set_xscale('log')
        ax.set_ylabel(r'GoF')
        label = r'\text{w$_{\text{int, mean}}$}'
        parm = 'w_mean'
        runs = [3, 6, 9]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(fr'{label} (ms)')
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.1e}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def w_int_std(self):
        ax = self.axes[2, 1]
        label = r'\text{w$_{\text{int, std}}$}'
        parm = 'w_std'
        runs = [3, 6, 9]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(fr'{label} (ms)')
        ax_right = ax.twinx()
        ax_right.set_ylabel('Set 3', labelpad=10)
        ax_right.tick_params(axis='y', which='both', right=False, labelright=False)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.1f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def dm_igm_slope(self):
        ax = self.axes[3, 0]
        ax.set_ylabel(r'GoF')
        label = r'\text{DM$_{\text{IGM, slope}}$}'
        parm = 'dm_igm_slope'
        runs = [4, 7, 10]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label + r' ($\textrm{pc}\ \textrm{cm}^{-3}$)')
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.0f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def dm_host(self):
        ax = self.axes[3, 1]
        label = r'\text{DM$_{\text{Host}}$}'
        parm = 'dm_host'
        runs = [4, 7, 10]

        ax.set_yscale('log', nonposy='clip')
        ax.set_xlabel(label + r' ($\textrm{pc}\ \textrm{cm}^{-3}$)')
        ax_right = ax.twinx()
        ax_right.set_ylabel('Set 4', labelpad=10)
        ax_right.tick_params(axis='y', which='both', right=False, labelright=False)
        best_value = np.nan

        # Plot runs
        for i, run in enumerate(runs):
            bins, gofs = self.get_data(run, parm)
            ax.step(bins, gofs, where='mid')

            # Plot global maximum
            try:
                best_value, best_gof = self.gm[run][parm]
                ax.plot([best_value]*2, [1e-1, best_gof], color=self.colors[i], linestyle='--')
                ax.scatter([best_value], [best_gof], marker='x', color=self.colors[i])
            except KeyError:
                i -= 1
                continue

            if i == 1 and not np.isnan(best_value):
                title = fr'{label}=${best_value:.0f}$'
                ax.set_title(title, fontsize=10, color=self.colors[i])

    def legend(self):
        ax = self.axes[2, 2]

        # Add legend elements
        elements = []

        line = Line2D([0], [0], color=self.colors[0])
        elements.append((line, r'Cycle 1'))

        line = Line2D([0], [0], color=self.colors[1])
        elements.append((line, r'Cycle 2'))

        line = Line2D([0], [0], color=self.colors[2])
        elements.append((line, r'Cycle 3'))

        line = Line2D([0], [0], color='grey', linestyle='--')
        label = r'Max GoF'
        elements.append((line, label))

        lines, labels = zip(*elements)
        self.fig.legend(lines, labels, bbox_to_anchor=(0.84, 0.4),
                        loc='center')

        ax.set_axis_off()

    def get_data(self, run_number, par):
        df = self.df[self.df.run == run_number]
        if df.empty:
            return [np.nan], [np.nan]
        gofs = []
        bins = []
        for bin_val, group in df.groupby(par):
            gof = self.gf.weighted_median(group)
            gofs.append(gof)
            bins.append(bin_val)
        bins = np.array(bins)
        gofs = np.array(gofs)
        diff = np.diff(bins)
        bin_type = 'lin'
        if not np.isclose(diff[0], diff[1]):
            bin_type = 'log'
        bins, gofs = self.gf.add_edges_to_hist(bins, gofs, bin_type=bin_type)
        gofs[np.isnan(gofs)] = 1e-1
        return bins, gofs

if __name__ == '__main__':
    Plot()

max_stars_repo_name: macrocosme/frbpoppy

max_issues_repo_name: macrocosme/frbpoppy

max_forks_repo_name: macrocosme/frbpoppy

reason: import numpy,from scipy


text:
"""Support for compensation sensor."""
import logging

import numpy as np

from homeassistant.const import (
    ATTR_ENTITY_ID,
    ATTR_UNIT_OF_MEASUREMENT,
    CONF_ATTRIBUTE,
    CONF_ENTITY_ID,
    CONF_NAME,
    CONF_UNIT_OF_MEASUREMENT,
    STATE_UNKNOWN,
)
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_state_change_event

from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR
from .const import CONF_COMPENSATION, CONF_POLYNOMIAL, CONF_PRECISION, DATA_COMPENSATION, CONF_TRACKED_ENTITY_ID, CONF_DATAPOINTS, CONF_MQTT_TOPIC

_LOGGER = logging.getLogger(__name__)

ATTR_ATTRIBUTE = "attribute"
ATTR_COEFFICIENTS = "coefficients"
ATTR_BASE_SENSOR = "base_sensor"

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Set up the Compensation sensor."""
    if discovery_info is not None:
        entity_id = discovery_info.get(CONF_COMPENSATION)
        conf = hass.data[DATA_COMPENSATION][entity_id]

        async_add_entities(
            [
                CompensationSensor(
                    hass,
                    f"{ DOMAIN_SENSOR }.{ entity_id }",
                    conf[CONF_TRACKED_ENTITY_ID],
                    conf.get(CONF_NAME),
                    conf.get(CONF_ATTRIBUTE),
                    conf[CONF_PRECISION],
                    conf[CONF_POLYNOMIAL],
                    conf.get(CONF_UNIT_OF_MEASUREMENT),
                )
            ]
        )

async def async_setup_entry(hass, entry, async_add_entries):
    """Set up the sensor platform."""
    conf = hass.data[DATA_COMPENSATION][entry.unique_id]

    entity = CompensationSensor(
        hass,
        conf[CONF_ENTITY_ID],
        conf[CONF_TRACKED_ENTITY_ID],
        conf.get(CONF_NAME),
        conf.get(CONF_MQTT_TOPIC),
        conf.get(CONF_ATTRIBUTE),
        conf[CONF_PRECISION],
        conf.get(CONF_POLYNOMIAL, np.poly1d(1)),
        conf.get(CONF_DATAPOINTS),
        conf.get(CONF_UNIT_OF_MEASUREMENT),
    )

    async_add_entries([entity])

class CompensationSensor(Entity):
    """Representation of a Compensation sensor."""

    def __init__(
        self,
        hass,
        entity_id,
        tracked_entity_id,
        name,
        mqtt_topic,
        attribute,
        precision,
        polynomial,
        datapoints,
        unit_of_measurement,
    ):
        """Initialize the Compensation sensor."""
        self._entity_id = entity_id
        self._tracked_entity_id = tracked_entity_id
        self._name = name
        self._mqtt_topic = mqtt_topic
        self._precision = precision
        self._attribute = attribute
        self._attributes = {}
        self._unit_of_measurement = unit_of_measurement
        self._poly = polynomial
        self._datapoints = datapoints
        self._coefficients = polynomial.coefficients.tolist()

        if hass.states.get(self._tracked_entity_id):
            self.calc_state(hass.states.get(self._tracked_entity_id))
        else:
            self._state = STATE_UNKNOWN

        @callback
        def async_compensation_sensor_state_listener(event):
            """Handle sensor state changes."""
            new_state = event.data.get("new_state")
            self.calc_state(new_state)
            self.async_write_ha_state()

        async_track_state_change_event(
            hass, [tracked_entity_id], async_compensation_sensor_state_listener
        )

    def calc_state(self, new_state):
        if new_state is None:
            return

        self._attributes.update(new_state.attributes)

        if self._unit_of_measurement is None and self._attribute is None:
            self._unit_of_measurement = new_state.attributes.get(
                ATTR_UNIT_OF_MEASUREMENT
            )

        try:
            if self._attribute:
                value = float(new_state.attributes.get(self._attribute))
            else:
                value = (
                    None
                    if new_state.state == STATE_UNKNOWN
                    else float(new_state.state)
                )
            # Calculate the result
            self._state = round(self._poly(value), self._precision)

        except (ValueError, TypeError):
            self._state = STATE_UNKNOWN
            if self._attribute:
                _LOGGER.warning(
                    "%s attribute %s is not numerical",
                    self._entity_id,
                    self._attribute,
                )
            else:
                _LOGGER.warning("%s state is not numerical", self._entity_id)

    @property
    def entity_id(self):
        """Return the entity_id of the sensor."""
        return self._entity_id
    @entity_id.setter
    def entity_id(self, ent_id):
        self._entity_id = ent_id

    @property
    def tracked_entity_id(self):
        """Return the tracked entity_id of the sensor."""
        return self._tracked_entity_id

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def should_poll(self):
        """No polling needed."""
        return False

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def device_state_attributes(self):
        """Return the state attributes of the sensor."""
        ret = {
            ATTR_ENTITY_ID: self._entity_id,
            ATTR_COEFFICIENTS: self._coefficients,
            CONF_TRACKED_ENTITY_ID: self._tracked_entity_id,
            ATTR_BASE_SENSOR: self._entity_id.replace("_calibrated", ""),
            CONF_MQTT_TOPIC: self._mqtt_topic,
            CONF_DATAPOINTS: self._datapoints,
        }
        if self._attribute:
            ret[ATTR_ATTRIBUTE] = self._attribute
        if self._attributes:
            ret.update(self._attributes)
        return ret

    @property
    def unit_of_measurement(self):
        """Return the unit the value is expressed in."""
        return self._unit_of_measurement

max_stars_repo_name: SciFiFarms/AllThing-Home-Assistant

max_issues_repo_name: SciFiFarms/AllThing-Home-Assistant

max_forks_repo_name: SciFiFarms/AllThing-Home-Assistant