text:
import os
import numpy as np
import sys; ##sys.path.append( os.environ['AFQMCLAB_DIR']+"/scripts/pyscf" )
from molecule import *
from rhf import *
from model import *
from uhf import *

global NMO, N, Nup, Ndn, spin
NMO = 14
SMO = 2*NMO
N = 5
Nup = 3
Ndn = 2
spin = 1

def readFCIDUMPSOC():
	Tout = np.zeros(shape=(SMO,SMO),dtype=complex)
	Vout = np.zeros(shape=(SMO,SMO, SMO, SMO),dtype=float)
	P_upup = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	P_dndn = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	P_updn = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	EngShf = []
	SumEngShf = 0.0
	count = 0
	fin = open('./FCIDUMPSOC','r')
	while(True):
		line = fin.readline()
		if not line:
			break
		tmp = line.split()
		if len(tmp) == 6:
			real = float(tmp[0])
			imag = float(tmp[1])
			ind_i = int(tmp[2])
			ind_l = int(tmp[3])
			ind_j = int(tmp[4])
			ind_k = int(tmp[5])
			value = real+ 1j* imag
			if ind_i == 0 and ind_l == 0 and ind_j == 0 and ind_k == 0:
				EngShf[count] = real+ 1j* imag
				SumEngShf += EngShf[count]
				count = count+1
				continue
			if count == 0:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i][2*ind_l][2*ind_j][2*ind_k] = real
					P_upup[ind_i][ind_l][ind_j][ind_k] = real
			elif count == 1:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i+1][2*ind_l+1][2*ind_j+1][2*ind_k+1] = real
					P_dndn[ind_i][ind_l][ind_j][ind_k] = real
			elif count == 2:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i][2*ind_l][2*ind_j+1][2*ind_k+1] = real
					Vout[2*ind_i+1][2*ind_l+1][2*ind_j][2*ind_k] = real
					P_updn[ind_i][ind_l][ind_j][ind_k] = real
			elif count ==3:
				if abs(value) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					Tout[ind_i][ind_l] = value
			elif count ==4:
				if abs(value) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					Tout[ind_i+NMO][ind_l+NMO] = value
			elif count ==5:
				if abs(value) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					Tout[ind_i][ind_l+NMO] = value
					Tout[ind_i+NMO][ind_l] = value

	return Tout, Vout

def readFCIDUMP():
	Tout = np.zeros(shape=(SMO,SMO),dtype=complex)
	Vout = np.zeros(shape=(SMO,SMO, SMO, SMO),dtype=float)
	P_upup = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	P_dndn = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	P_updn = np.zeros(shape=(NMO,NMO, NMO, NMO),dtype=float)
	EngShf = []
	SumEngShf = 0.0
	count = 0
	fin = open('./FCIDUMPSOC','r')
	while(True):
		line = fin.readline()
		if not line:
			break
		tmp = line.split()
		if len(tmp) == 5:
			real = float(tmp[0])
			ind_i = int(tmp[1])
			ind_l = int(tmp[2])
			ind_j = int(tmp[3])
			ind_k = int(tmp[4])
			if ind_i == 0 and ind_l == 0 and ind_j == 0 and ind_k == 0:
				EngShf[count] = real
				SumEngShf += EngShf[count]
				count = count+1
				continue
			if count == 0:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i][2*ind_l][2*ind_j][2*ind_k] = real
					P_upup[ind_i][ind_l][ind_j][ind_k] = real
			elif count == 1:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i+1][2*ind_l+1][2*ind_j+1][2*ind_k+1] = real
					P_dndn[ind_i][ind_l][ind_j][ind_k] = real
			elif count == 2:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					ind_j = ind_j -1
					ind_k = ind_k -1
					Vout[2*ind_i][2*ind_l][2*ind_j+1][2*ind_k+1] = real
					Vout[2*ind_i+1][2*ind_l+1][2*ind_j][2*ind_k] = real
					P_updn[ind_i][ind_l][ind_j][ind_k] = real
			elif count ==3:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					Tout[ind_i][ind_l] = real
			elif count ==4:
				if abs(real) > 0.00000001:
					ind_i = ind_i -1
					ind_l = ind_l -1
					Tout[ind_i+NMO][ind_l+NMO] = real

	return Tout, Vout, P_upup, P_dndn, P_updn

if __name__ == '__main__':

	t,V,pUpUp, pDnDn, pUnDn = readFCIDUMP()
	V2 = np.transpose(V, (0,1,3,2))
	diff = V2-V
	print(diff)
	V = V.reshape( SMO*SMO, SMO*SMO )
	w, vec = np.linalg.eigh(V)

	eigenNum = NMO**2

	f = open("model_param", 'w')
	f.write( '{:16d} \n'.format(NMO) )
	f.write( '{:16d} \n'.format(N) )
	f.write( '{:16d} \n'.format(Nup) )
	f.write( '{:16d} \n'.format(Ndn) )
	f.write( '{:16d} \n'.format(eigenNum) )
	for i in range( 2*NMO ):
		for j in range( 2*NMO ):
			f.write( '{:26.18e} {:26.18e} \n'.format(t[j, i].real, t[j, i].imag))
	for i in range( 2*NMO ):
		for j in range( 2*NMO ):
			f.write( '{:26.18e} {:26.18e} \n'.format(K[j, i].real, K[j, i].imag))
	for i in range( 2*NMO ):
		for j in range( 2*NMO ):
			for l in range( eigenNum ):
			f.write( '{:26.18e} {:26.18e} \n'.format(V[l, j, i].real, V[l, j, i].imag))
	for i in range( eigenNum ):
		f.write( '{:26.18e} \n'.format(0.0) )
	f.close()

#	f = h5py.File("model_param", "w")
#	f.create_dataset("L",              (1,),                        data=[NMO],                 dtype='int')
#	f.create_dataset("N",              (1,),                        data=[N],                   dtype='int')
#	f.create_dataset("Nup",            (1,),                        data=[Nup],                 dtype='int')
#	f.create_dataset("Ndn",            (1,),                        data=[Ndn],                 dtype='int')
#	f.create_dataset("choleskyNumber", (1,),                        data=[choleskyNum],         dtype='int')
#	f.create_dataset("t",              (4*NMO**2,),                 data=t.ravel(),             dtype='complex')
#	f.create_dataset("K",              (4*NMO**2,),                 data=K.ravel(),             dtype='complex')
#	f.create_dataset("choleskyVecs",   (choleskyNum*NMO**2,),       data=V.ravel(),             dtype='float64')
#	f.create_dataset("choleskyBg",     (choleskyNum,),              data=np.zeros(choleskyNum), dtype='float64')
#	f.close()

#	V = readVMatrix()


text:
import torch
import numpy as np

from .energy.base import Energy
from .sampling.base import Sampler
from .distribution import CustomDistribution

__all__ = ["ProductEnergy", "ProductSampler", "ProductDistribution"]

class ProductEnergy(Energy):
    """Stack multiple energies together to form an energy on the product space.
    The energy on the product space is the sum of its independent components.

    Parameters
    ----------
    components : Sequence[Energy]
        The individual energies that form the direct product.
    cat_dim : int or None
        If None, the .energy function takes multiple tensors (one for each component).
        Otherwise, it expects one tensor that is then split along dimension `cat_dim`.

    Notes
    -----
    The underlying components have to be single-event energies.
    """
    def __init__(self, components, cat_dim=None, **kwargs):
        event_shapes, lengths = _stacked_event_shapes([c.event_shape for c in components], cat_dim)
        super().__init__(dim=event_shapes, **kwargs)
        self._components = torch.nn.ModuleList(components)
        self._cat_dim = cat_dim
        self._lengths = lengths

    def _energy(self, *xs):
        if self._cat_dim is None:
            assert len(xs) == len(self._components)
            energies = [dist.energy(x) for dist, x in zip(self._components, xs)]
        else:
            assert len(xs) == 1
            xs = xs[0].split(self._lengths, dim=self._cat_dim)
            energies = [dist.energy(x) for x, dist in zip(xs, self._components)]
        return torch.sum(torch.stack(energies, dim=-1), dim=-1)

    def __getitem__(self, index):
        return self._components[index]

    def __iter__(self):
        return self._components.__iter__()

    def __len__(self):
        return self._components.__len__()

class ProductSampler(Sampler):
    """Sampler on the product space.

    Parameters
    ----------
    components : Sequence[Sampler]
        The individual samplers that form the direct product.
    cat_dim : int or None
        If None, the .sample function generates multiple tensors (one for each component).
        Otherwise, it returns one tensor that is concatenated along dimension `cat_dim`.
    """
    def __init__(self, components, cat_dim=None, **kwargs):
        super().__init__(**kwargs)
        self._components = torch.nn.ModuleList(components)
        self._cat_dim = cat_dim

    def _sample(self, n_samples):
        samples = tuple(dist._sample(n_samples) for dist in self._components)
        if self._cat_dim is None:
            return samples
        else:
            return torch.cat(samples, dim=self._cat_dim)

    def _sample_with_temperature(self, n_samples, temperature=1.0):
        samples = tuple(dist._sample_with_temperature(n_samples, temperature) for dist in self._components)
        if self._cat_dim is None:
            return samples
        else:
            return torch.cat(samples, dim=self._cat_dim)

    def __getitem__(self, index):
        return self._components[index]

    def __iter__(self):
        return self._components.__iter__()

    def __len__(self):
        return self._components.__len__()

class ProductDistribution(CustomDistribution):
    """Distribution on a product space.
    Encapsulate multiple distributions in one object.

    Parameters
    ----------
    components : Iterable
        List of distributions.
    cat_dim : int or None
        The dimension along which samples from the individual components are concatenated.
        If None, don't concatenate.

    Notes
    -----
    The underlying components have to be single-event distributions.
    """

    def __init__(self, components, cat_dim=None):
        super().__init__(
            energy=ProductEnergy(components=components, cat_dim=cat_dim),
            sampler=ProductSampler(components=components, cat_dim=cat_dim)
        )

def _stacked_event_shapes(event_shapes, cat_dim):
    if cat_dim is None:
        return event_shapes, None
    else:
        lengths = [e[cat_dim] for e in event_shapes]
        shape = np.array(event_shapes[0])
        # assert that shapes are consistent
        for e in event_shapes:
            assert len(e) == len(shape)
            assert _shapes_consistent(e, shape, cat_dim)
        # concatenate events along dimensions `cat_dim`
        shape[cat_dim] = sum(s[cat_dim] for s in event_shapes)
        event_shapes = torch.Size(shape.tolist())
        return event_shapes, lengths

def _shapes_consistent(shape1, shape2, cat_dim):
    """check if shapes are the same in all dimensions but `cat_dim`"""
    diff = np.abs(np.array(shape1) - shape2)
    return diff.sum() == diff[cat_dim]

ext: py

max_stars_repo_name: michellab/bgflow

max_issues_repo_name: michellab/bgflow

max_forks_repo_name: michellab/bgflow

reason: import numpy


text:
# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
import random
from scipy import stats
import os
import core.interaction_mapper as um
from multiprocessing import Process

class TensorboardWriter(object):

    def __init__(self, config):
        self.cf = config

        def launchTensorBoard(cmd, path):
            os.system(cmd + path + "\"")
            return

        p = Process(target=launchTensorBoard, args=(self.cf.tb_command, self.cf.tb_dir))
        p.start()

        self.tensorboard_train_writer = tf.summary.FileWriter(self.cf.tb_dir + "/train")
        self.tensorboard_test_writer = tf.summary.FileWriter(self.cf.tb_dir + "/test")
        self.tensorboard_log_writer = tf.summary.FileWriter(self.cf.tb_dir + "/log")

    def log_info(self, sess, log_txt):
        log_tensor = sess.run(tf.summary.text('info', tf.convert_to_tensor(log_txt)))
        self.tensorboard_log_writer.add_summary(log_tensor)
        self.tensorboard_log_writer.flush()

    def log_scalar(self, y_val, x_val, tag=""):
        summary = tf.Summary(value=[tf.Summary.Value(tag=tag,
                                                     simple_value=y_val)])
        self.tensorboard_log_writer.add_summary(summary, x_val)
        # log_tensor = sess.run(tf.summary.scalar('info', tf.convert_to_tensor(val)))
        # self.tensorboard_log_writer.add_summary(log_tensor)

    def add_train_summary(self, train_output, counter):
        self.tensorboard_train_writer.add_summary(train_output, counter)

    def add_test_summary(self, test_output, counter):
        self.tensorboard_test_writer.add_summary(test_output, counter)

    def flush(self):
        self.tensorboard_train_writer.flush()
        self.tensorboard_test_writer.flush()

max_stars_repo_name: otto-de/crystal-gazer

max_issues_repo_name: otto-de/crystal-gazer

max_forks_repo_name: otto-de/crystal-gazer

reason: import numpy,from scipy


text:
from __future__ import unicode_literals, print_function, division

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
from data_util import config,data
from numpy import random
from allennlp.modules.elmo import  batch_to_ids,Elmo

options_file = "training_ptr_gen/config_files/elmo_2x4096_512_2048cnn_2xhighway_5.5B_options.json"
weight_file =  "training_ptr_gen/config_files/elmo_2x4096_512_2048cnn_2xhighway_5.5B_weights.hdf5"

use_cuda = config.use_gpu and torch.cuda.is_available()

random.seed(123)
torch.manual_seed(123)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(123)

def init_lstm_wt(lstm):
    for names in lstm._all_weights:
        for name in names:
            if name.startswith('weight_'):
                wt = getattr(lstm, name)
                wt.data.uniform_(-config.rand_unif_init_mag, config.rand_unif_init_mag)
            elif name.startswith('bias_'):
                # set forget bias to 1
                bias = getattr(lstm, name)
                n = bias.size(0)
                start, end = n // 4, n // 2
                bias.data.fill_(0.)
                bias.data[start:end].fill_(1.)

def init_linear_wt(linear):
    linear.weight.data.normal_(std=config.trunc_norm_init_std)
    if linear.bias is not None:
        linear.bias.data.normal_(std=config.trunc_norm_init_std)

def init_wt_normal(wt):
    wt.data.normal_(std=config.trunc_norm_init_std)

def init_wt_unif(wt):
    wt.data.uniform_(-config.rand_unif_init_mag, config.rand_unif_init_mag)

class Encoder(nn.Module):
    def __init__(self):
        super(Encoder, self).__init__()
        # Load elmo
        print(f'Loading Elmo with {1} layers')
        self.elmo_layer = Elmo(options_file, weight_file, 1, dropout=0.5)
        print('loaded')

        #self.embedding = nn.Embedding(config.vocab_size, config.emb_dim)# old embedding
        #init_wt_normal(self.embedding.weight)

        self.lstm = nn.LSTM(1024, config.hidden_dim, num_layers=1, batch_first=True, bidirectional=True)
        init_lstm_wt(self.lstm)

        self.W_h = nn.Linear(config.hidden_dim * 2, config.hidden_dim * 2, bias=False)

    #seq_lens should be in descending order
    def forward(self, input, seq_lens,vocab,batchartoovs=None):
        strings=[]
        for examp in input:
            copy=examp.clone().cpu().numpy().astype(int)
            converted=data.outputids2words(copy, vocab, (batchartoovs if config.pointer_gen else None))
            strings.append(converted)

        strings=batch_to_ids(strings).cuda()
        embedded=self.elmo_layer(strings)['elmo_representations']
        embedded=embedded[0]
        #[batch size, max enc steps, 1024]
        #embedded = self.embedding(input)

        packed = pack_padded_sequence(embedded, seq_lens, batch_first=True)
        output, hidden = self.lstm(packed)

        encoder_outputs, _ = pad_packed_sequence(output, batch_first=True)  # h dim = B x t_k x n
        encoder_outputs = encoder_outputs.contiguous()

        encoder_feature = encoder_outputs.view(-1, 2*config.hidden_dim)  # B * t_k x 2*hidden_dim
        encoder_feature = self.W_h(encoder_feature)

        return encoder_outputs, encoder_feature, hidden

class ReduceState(nn.Module):
    def __init__(self):
        super(ReduceState, self).__init__()

        self.reduce_h = nn.Linear(config.hidden_dim * 2, config.hidden_dim)
        init_linear_wt(self.reduce_h)
        self.reduce_c = nn.Linear(config.hidden_dim * 2, config.hidden_dim)
        init_linear_wt(self.reduce_c)

    def forward(self, hidden):
        h, c = hidden # h, c dim = 2 x b x hidden_dim
        h_in = h.transpose(0, 1).contiguous().view(-1, config.hidden_dim * 2)
        hidden_reduced_h = F.relu(self.reduce_h(h_in))
        c_in = c.transpose(0, 1).contiguous().view(-1, config.hidden_dim * 2)
        hidden_reduced_c = F.relu(self.reduce_c(c_in))

        return (hidden_reduced_h.unsqueeze(0), hidden_reduced_c.unsqueeze(0)) # h, c dim = 1 x b x hidden_dim

class Attention(nn.Module):
    def __init__(self):
        super(Attention, self).__init__()
        # attention
        if config.is_coverage:
            self.W_c = nn.Linear(1, config.hidden_dim * 2, bias=False)
        self.decode_proj = nn.Linear(config.hidden_dim * 2, config.hidden_dim * 2)
        self.v = nn.Linear(config.hidden_dim * 2, 1, bias=False)

    def forward(self, s_t_hat, encoder_outputs, encoder_feature, enc_padding_mask, coverage):
        b, t_k, n = list(encoder_outputs.size())

        dec_fea = self.decode_proj(s_t_hat) # B x 2*hidden_dim
        dec_fea_expanded = dec_fea.unsqueeze(1).expand(b, t_k, n).contiguous() # B x t_k x 2*hidden_dim
        dec_fea_expanded = dec_fea_expanded.view(-1, n)  # B * t_k x 2*hidden_dim

        att_features = encoder_feature + dec_fea_expanded # B * t_k x 2*hidden_dim
        if config.is_coverage:
            coverage_input = coverage.view(-1, 1)  # B * t_k x 1
            coverage_feature = self.W_c(coverage_input)  # B * t_k x 2*hidden_dim
            att_features = att_features + coverage_feature

        e = F.tanh(att_features) # B * t_k x 2*hidden_dim
        scores = self.v(e)  # B * t_k x 1
        scores = scores.view(-1, t_k)  # B x t_k

        attn_dist_ = F.softmax(scores, dim=1)*enc_padding_mask # B x t_k
        normalization_factor = attn_dist_.sum(1, keepdim=True)
        attn_dist = attn_dist_ / normalization_factor

        attn_dist = attn_dist.unsqueeze(1)  # B x 1 x t_k
        c_t = torch.bmm(attn_dist, encoder_outputs)  # B x 1 x n
        c_t = c_t.view(-1, config.hidden_dim * 2)  # B x 2*hidden_dim

        attn_dist = attn_dist.view(-1, t_k)  # B x t_k

        if config.is_coverage:
            coverage = coverage.view(-1, t_k)
            coverage = coverage + attn_dist

        return c_t, attn_dist, coverage

class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()
        self.attention_network = Attention()

        self.x_context = nn.Linear(config.hidden_dim * 2 + 1024, 1024)

        self.lstm = nn.LSTM(1024, config.hidden_dim, num_layers=1, batch_first=True, bidirectional=False)
        init_lstm_wt(self.lstm)

        if config.pointer_gen:
            self.p_gen_linear = nn.Linear(config.hidden_dim * 4 + 1024, 1)

        #p_vocab
        self.out1 = nn.Linear(config.hidden_dim * 3, config.hidden_dim)
        self.out2 = nn.Linear(config.hidden_dim, config.vocab_size)
        init_linear_wt(self.out2)

    def forward(self, y_t_1, s_t_1, encoder_outputs, encoder_feature, enc_padding_mask,
                c_t_1, extra_zeros, enc_batch_extend_vocab, coverage, step,vocab,batchartoovs):

        if not self.training and step == 0:
            h_decoder, c_decoder = s_t_1
            s_t_hat = torch.cat((h_decoder.view(-1, config.hidden_dim),
                                 c_decoder.view(-1, config.hidden_dim)), 1)  # B x 2*hidden_dim
            c_t, _, coverage_next = self.attention_network(s_t_hat, encoder_outputs, encoder_feature,
                                                              enc_padding_mask, coverage)
            coverage = coverage_next

        strings = []
        for examp in y_t_1:
            copy = [examp.clone().cpu().item()]
            converted = data.outputids2words(copy, vocab, (batchartoovs if config.pointer_gen else None))
            strings.append(converted)

        strings = batch_to_ids(strings).cuda()
        y_t_1_embd  = self.elmo_layer(strings)['elmo_representations'][0].squeeze_()  # only last layer

        #y_t_1_embd = self.embedding(y_t_1)
        x = self.x_context(torch.cat((c_t_1, y_t_1_embd ), 1))
        lstm_out, s_t = self.lstm(x.unsqueeze(1), s_t_1)

        h_decoder, c_decoder = s_t
        s_t_hat = torch.cat((h_decoder.view(-1, config.hidden_dim),
                             c_decoder.view(-1, config.hidden_dim)), 1)  # B x 2*hidden_dim
        c_t, attn_dist, coverage_next = self.attention_network(s_t_hat, encoder_outputs, encoder_feature,
                                                          enc_padding_mask, coverage)

        if self.training or step > 0:
            coverage = coverage_next

        p_gen = None
        if config.pointer_gen:
            p_gen_input = torch.cat((c_t, s_t_hat, x), 1)  # B x (2*2*hidden_dim + emb_dim)
            p_gen = self.p_gen_linear(p_gen_input)
            p_gen = F.sigmoid(p_gen)

        output = torch.cat((lstm_out.view(-1, config.hidden_dim), c_t), 1) # B x hidden_dim * 3
        output = self.out1(output) # B x hidden_dim

        #output = F.relu(output)

        output = self.out2(output) # B x vocab_size
        vocab_dist = F.softmax(output, dim=1)

        if config.pointer_gen:
            vocab_dist_ = p_gen * vocab_dist
            attn_dist_ = (1 - p_gen) * attn_dist

            if extra_zeros is not None:
                vocab_dist_ = torch.cat([vocab_dist_, extra_zeros], 1)

            final_dist = vocab_dist_.scatter_add(1, enc_batch_extend_vocab, attn_dist_)
        else:
            final_dist = vocab_dist

        return final_dist, s_t, c_t, attn_dist, p_gen, coverage

class Model(object):
    def __init__(self, model_file_path=None, is_eval=False):
        encoder = Encoder()
        decoder = Decoder()
        reduce_state = ReduceState()

        # shared the embedding between encoder and decoder
        decoder.elmo_layer=encoder.elmo_layer
        if is_eval:
            encoder = encoder.eval()
            decoder = decoder.eval()
            reduce_state = reduce_state.eval()

        if use_cuda:
            encoder = encoder.cuda()
            decoder = decoder.cuda()
            reduce_state = reduce_state.cuda()

        self.encoder = encoder
        self.decoder = decoder
        self.reduce_state = reduce_state

        if model_file_path is not None:
            state = torch.load(model_file_path, map_location= lambda storage, location: storage)
            self.encoder.load_state_dict(state['encoder_state_dict'])
            self.decoder.load_state_dict(state['decoder_state_dict'], strict=False)
            self.reduce_state.load_state_dict(state['reduce_state_dict'])

max_stars_repo_name: Rhuax/ELMo-Text-Summarization

max_issues_repo_name: Rhuax/ELMo-Text-Summarization

max_forks_repo_name: Rhuax/ELMo-Text-Summarization

reason: from numpy


text:
from matplotlib import pyplot as plt
import os
import argparse
import glob
import numpy as np
import pickle
import csv
from tifffile import imread
import sys
sys.path.append(r'E:\NuclearSegmentationPipeline\DataGenerator')
from Classes.Image import Image
from Classes.Helper import Tools
from sklearn.metrics import accuracy_score, precision_score, recall_score
from Tools.Helper import calculateAggregatedJaccardIndex, objectBasedMeasures, objectBasedMeasures4, jaccardIndex_with_object, jaccardIndex_with_area, getMetrics, calculateSinglecellDiceJI
from Tools.StructuredEvaluation import StructuredEvaluation,TestImage,Architecture,Metric,Diagnosis, Preparation, ChallengeLevel, NaturepaperClasses
from skimage.measure import label

def main():
    tools = Tools()
    structuredEvaluation = StructuredEvaluation()
    parser = argparse.ArgumentParser(description='Train model.')
    parser.add_argument('--inputFolder', help='Select input folder.', default=None)
    parser.add_argument('--outputFolder', help='select output folder', default=None)
    parser.add_argument('--resultfile', help='select result file', default=r"E:\NuclearSegmentationPipeline\Results\results_scaled.csv")
    args = parser.parse_args()

    # Read csv file to get position
    mapping_images = dict()
    natureclass = dict()
    abs_index = 0
    mapping_class = dict()

    with open(args.resultfile) as csv_file:
        csv_reader = csv.reader(csv_file)
        for row in csv_reader:
            if int(row[2]) == 0:
                mapping_images[os.path.basename(row[0])] = abs_index
                abs_index = abs_index + 1
    # Read csv file to get naturepaperclass
    with open(r"E:\NuclearSegmentationPipeline\DataGenerator\image_description_final_revision.csv") as csv_file2:
        csv_reader2 = csv.reader(csv_file2)
        for row in csv_reader2:
            mapping_class[row[0].split(';')[0]] = row[0].split(';')[1]
            natureclass[row[0].split(';')[0]] = row[0].split(';')[4]
    rawimages = []
    groundtruth = []
    img_pathes = []
    base_path = r"E:\NuclearSegmentationPipeline\DataGenerator\dataset_singlecellgroundtruth"

    type_list = ["Ganglioneuroma", "Neuroblastoma", "normal"]

    abs_index = 0
    target = "masks"

    ids_predictions = glob.glob(os.path.join(r"E:\NuclearSegmentationPipeline\Results\\", '*.pkl'))
    predictions = []
    prediction_parameters = []
    print("Loading predictions...")
    for i in ids_predictions:
        prediction_parameters.append(os.path.basename(i).split('_reconstructed')[0])
        #predictions.append(pickle.load(open(i, "rb")))
        # Workaround for rwf pickles
        with open(i, "rb") as f:
            u = pickle._Unpickler(f)
            u.encoding = "latin1"
            predictions.append(u.load())

    for index,elem in enumerate(predictions):
        structuredEvaluation.addArchitecture(Architecture(name = prediction_parameters[index]))
    type_list_sorted = []
    img_name_list = []
    abs_index = 0
    print ("Reading images ...")
    for type in type_list:
        imgs = glob.glob(os.path.join(base_path, type, "images", "*.tif"))
        for index,img in enumerate(imgs):
            groundtruth.append(imread(img.replace('images','masks').replace('.tif','singlemask.tif')))
            img_name_list.append(os.path.basename(img))
            type_tmp = type
            structuredEvaluation.addTestImage(TestImage(position = abs_index, naturepaperclass=natureclass[os.path.basename(img).replace('.tif','')]))
            abs_index = abs_index + 1
    print("Start prediction")
    for index in range(0, groundtruth.__len__()):
        print("Calculating metrics for image number " + str(index) + " from " + str(groundtruth.__len__()) + " ...")
        for ix,prediction in enumerate(predictions):
            # Calculate metrics for Evi
            pred_index = mapping_images[img_name_list[index]]
            erg = calculateSinglecellDiceJI(groundtruth[index],label(prediction['masks'][pred_index].astype(np.float32)))
            structuredEvaluation.addMetric(Metric(dice=erg["DICE"], ji=erg["JI"]), image=index, architecture=prediction_parameters[ix])

    structuredEvaluation.printMetrics(r"E:\NuclearSegmentationPipeline\Results\test_singlecellannotation_GNB.csv", structuredEvaluation.calculateMetricsForDiagnosis(target='naturepaperclass', targetlist=[NaturepaperClasses.GNB_I]))
    structuredEvaluation.printMetrics(r"E:\NuclearSegmentationPipeline\Results\test_singlecellannotation__NB.csv", structuredEvaluation.calculateMetricsForDiagnosis(target='naturepaperclass', targetlist=[NaturepaperClasses.NB_IV]))
    structuredEvaluation.printMetrics(r"E:\NuclearSegmentationPipeline\Results\test_singlecellannotation__normal.csv", structuredEvaluation.calculateMetricsForDiagnosis(target='naturepaperclass', targetlist=[NaturepaperClasses.NC_I]))
    structuredEvaluation.printMetrics(r"E:\NuclearSegmentationPipeline\Results\test_singlecellannotation__alldiagnosis.csv", structuredEvaluation.calculateMetricsForDiagnosis(target='naturepaperclass', targetlist=[NaturepaperClasses.GNB_I, NaturepaperClasses.NB_IV, NaturepaperClasses.NC_I]))

if __name__ == '__main__':
    main()

max_stars_repo_name: SCCH-KVS/NuclearSegmentationPipeline

max_issues_repo_name: SCCH-KVS/NuclearSegmentationPipeline

max_forks_repo_name: SCCH-KVS/NuclearSegmentationPipeline


text:
import casadi as ca
import numpy as np
import numpy.linalg as la

import pdb

class init_FTOCP(object):
    def __init__(self, Q, P, R, Rd, N, agent, x_refs=None):
        self.Q = Q
        self.P = P
        self.R = R
        self.N = N
        self.Rd = Rd

        self.agent = agent

        self.n_x = self.agent.n_x
        self.n_u = self.agent.n_u

        self.l_r = self.agent.l_r
        self.l_f = self.agent.l_f

        self.dt = self.agent.dt

        self.F, self.b = self.agent.get_state_constraints()
        self.H, self.g = self.agent.get_input_constraints()

        self.x_lim = self.agent.x_lim
        self.y_lim = self.agent.y_lim
        self.psi_lim = self.agent.psi_lim
        self.v_lim = self.agent.v_lim

        self.a_lim = self.agent.a_lim
        self.df_lim = self.agent.df_lim
        self.da_lim = self.agent.da_lim
        self.ddf_lim = self.agent.ddf_lim

        self.state_lb = []
        self.state_ub = []
        if self.x_lim is not None:
            self.state_lb += [self.x_lim[0]]
            self.state_ub += [self.x_lim[1]]
        else:
            self.state_lb += [-1000.0]
            self.state_ub += [1000.0]
        if self.y_lim is not None:
            self.state_lb += [self.y_lim[0]]
            self.state_ub += [self.y_lim[1]]
        else:
            self.state_lb += [-1000.0]
            self.state_ub += [1000.0]
        if self.psi_lim is not None:
            self.state_lb += [self.psi_lim[0]]
            self.state_ub += [self.psi_lim[1]]
        else:
            self.state_lb += [-1000.0]
            self.state_ub += [1000.0]
        if self.v_lim is not None:
            self.state_lb += [self.v_lim[0]]
            self.state_ub += [self.v_lim[1]]
        else:
            self.state_lb += [-1000.0]
            self.state_ub += [1000.0]

        self.input_lb = []
        self.input_ub = []
        if self.df_lim is not None:
            self.input_lb += [self.df_lim[0]]
            self.input_ub += [self.df_lim[1]]
        else:
            self.input_lb += [-1000.0]
            self.input_ub += [1000.0]
        if self.a_lim is not None:
            self.input_lb += [self.a_lim[0]]
            self.input_ub += [self.a_lim[1]]
        else:
            self.input_lb += [-1000.0]
            self.input_ub += [1000.0]

        self.input_rate_lb = []
        self.input_rate_ub = []
        if self.ddf_lim is not None:
            self.input_rate_lb += [self.ddf_lim[0]]
            self.input_rate_ub += [self.ddf_lim[1]]
        else:
            self.input_rate_lb += [-1000.0]
            self.input_rate_ub += [1000.0]
        if self.da_lim is not None:
            self.input_rate_lb += [self.da_lim[0]]
            self.input_rate_ub += [self.da_lim[1]]
        else:
            self.input_rate_lb += [-1000.0]
            self.input_rate_ub += [1000.0]

        self.x_refs_idx = 0
        if x_refs is not None:
            self.x_refs = x_refs
        else:
            self.x_refs = [np.zeros(self.n_x)]

        self.cost = None
        self.opti = None
        self.agt_x = []
        self.x_s = None
        self.x_f = None
        self.x = None
        self.u = None

        self.opti0 = None
        self.cost0 = None
        self.agt_x0 = []
        self.x_s0 = None
        self.x_f0 = None
        self.x0 = None
        self.u0 = None

    def build_opti_solver(self, agt_idx):
        self.opti = ca.Opti()

        self.x = self.opti.variable(self.n_x, self.N+1)
        self.u = self.opti.variable(self.n_u, self.N)
        self.x_s = self.opti.parameter(self.n_x)
        self.x_f = self.opti.parameter(self.n_x)
        self.last_u = self.opti.parameter(self.n_u)

        self.agt_x = []
        for i in range(agt_idx):
            self.agt_x.append(self.opti.parameter(self.n_x, self.N+1))

        self.opti.set_value(self.x_f, self.x_refs[self.x_refs_idx])

        da_lim = self.agent.da_lim
        ddf_lim = self.agent.ddf_lim
        r = self.agent.r

        self.opti.subject_to(self.x[:,0] == self.x_s)
        self.opti.subject_to(self.opti.bounded(ddf_lim[0]*self.dt, self.u[0,0]-self.last_u[0], ddf_lim[1]*self.dt))
        self.opti.subject_to(self.opti.bounded(da_lim[0]*self.dt, self.u[1,0]-self.last_u[1], da_lim[1]*self.dt))

        stage_cost = 0
        for i in range(self.N):
            stage_cost += ca.bilin(self.Q, self.x[:,i+1]-self.x_f, self.x[:,i+1]-self.x_f) + ca.bilin(self.R, self.u[:,i], self.u[:,i])

            beta = ca.atan2(self.l_r*ca.tan(self.u[0,i]), self.l_f+self.l_r)
            self.opti.subject_to(self.x[0,i+1] == self.x[0,i] + self.dt*self.x[3,i]*ca.cos(self.x[2,i] + beta))
            self.opti.subject_to(self.x[1,i+1] == self.x[1,i] + self.dt*self.x[3,i]*ca.sin(self.x[2,i] + beta))
            self.opti.subject_to(self.x[2,i+1] == self.x[2,i] + self.dt*self.x[3,i]*ca.sin(beta))
            self.opti.subject_to(self.x[3,i+1] == self.x[3,i] + self.dt*self.u[1,i])

            if self.F is not None:
                self.opti.subject_to(ca.mtimes(self.F, self.x[:,i+1]) <= self.b)
            if self.H is not None:
                self.opti.subject_to(ca.mtimes(self.H, self.u[:,i]) <= self.g)

            # Treat init and final states of agents before as obstacles
            for j in range(agt_idx):
                self.opti.subject_to(ca.bilin(np.eye(2), self.x[:2,i+1]-self.agt_x[j][:2,i+1], self.x[:2,i+1]-self.agt_x[j][:2,i+1]) >= (1.5*2*r)**2)

            if i < self.N-1:
                stage_cost += ca.bilin(self.Rd, self.u[:,i+1]-self.u[:,i], self.u[:,i+1]-self.u[:,i])
                self.opti.subject_to(self.opti.bounded(ddf_lim[0]*self.dt, self.u[0,i+1]-self.u[0,i], ddf_lim[1]*self.dt))
                self.opti.subject_to(self.opti.bounded(da_lim[0]*self.dt, self.u[1,i+1]-self.u[1,i], da_lim[1]*self.dt))

        self.cost = stage_cost
        self.opti.minimize(self.cost)

        solver_opts = {
            "mu_strategy" : "adaptive",
            "mu_init" : 1e-5,
            "mu_min" : 1e-15,
            "barrier_tol_factor" : 1,
            "print_level" : 0,
            "linear_solver" : "ma27"
            }
        # solver_opts = {}
        plugin_opts = {"verbose" : False, "print_time" : False, "print_out" : False}
        self.opti.solver('ipopt', plugin_opts, solver_opts)

    def build_opti0_solver(self, agt_idx):
        self.opti0 = ca.Opti()

        self.x0 = self.opti0.variable(self.n_x, self.N+1)
        self.u0 = self.opti0.variable(self.n_u, self.N)
        self.x_s0 = self.opti0.parameter(self.n_x)
        self.x_f0 = self.opti0.parameter(self.n_x)

        self.agt_x0 = []
        for i in range(agt_idx):
            self.agt_x0.append(self.opti0.parameter(self.n_x, self.N+1))

        self.opti0.set_value(self.x_f0, self.x_refs[self.x_refs_idx])

        da_lim = self.agent.da_lim
        ddf_lim = self.agent.ddf_lim
        r = self.agent.r

        self.opti0.subject_to(self.x0[0,0] == self.x_s0[0])
        self.opti0.subject_to(self.x0[1,0] == self.x_s0[1])
        # self.opti0.subject_to(self.opti0.bounded(-2*np.pi, self.x0[2,0], 2*np.pi))
        self.opti0.subject_to(self.x0[2,0] == self.x_s0[2])
        self.opti0.subject_to(self.x0[3,0] == self.x_s0[3])

        stage_cost = 0
        for i in range(self.N):
            stage_cost += ca.bilin(self.Q, self.x0[:,i+1]-self.x_f0, self.x0[:,i+1]-self.x_f0) + ca.bilin(self.R, self.u0[:,i], self.u0[:,i])

            beta = ca.atan2(self.l_r*ca.tan(self.u0[0,i]), self.l_f+self.l_r)
            self.opti0.subject_to(self.x0[0,i+1] == self.x0[0,i] + self.dt*self.x0[3,i]*ca.cos(self.x0[2,i] + beta))
            self.opti0.subject_to(self.x0[1,i+1] == self.x0[1,i] + self.dt*self.x0[3,i]*ca.sin(self.x0[2,i] + beta))
            self.opti0.subject_to(self.x0[2,i+1] == self.x0[2,i] + self.dt*self.x0[3,i]*ca.sin(beta))
            self.opti0.subject_to(self.x0[3,i+1] == self.x0[3,i] + self.dt*self.u0[1,i])

            if self.F is not None:
                self.opti0.subject_to(ca.mtimes(self.F, self.x0[:,i+1]) <= self.b)
            if self.H is not None:
                self.opti0.subject_to(ca.mtimes(self.H, self.u0[:,i]) <= self.g)

            # Treat init and final states of agents before as obstacles
            for j in range(agt_idx):
                self.opti0.subject_to(ca.bilin(np.eye(2), self.x0[:2,i+1]-self.agt_x0[j][:2,i+1], self.x0[:2,i+1]-self.agt_x0[j][:2,i+1]) >= (1.5*2*r)**2)

            if i < self.N-1:
                stage_cost += ca.bilin(self.Rd, self.u0[:,i+1]-self.u0[:,i], self.u0[:,i+1]-self.u0[:,i])
                self.opti0.subject_to(self.opti0.bounded(ddf_lim[0]*self.dt, self.u0[0,i+1]-self.u0[0,i], ddf_lim[1]*self.dt))
                self.opti0.subject_to(self.opti0.bounded(da_lim[0]*self.dt, self.u0[1,i+1]-self.u0[1,i], da_lim[1]*self.dt))

        self.cost0 = stage_cost
        self.opti0.minimize(self.cost0)

        solver_opts = {
            "mu_strategy" : "adaptive",
            "mu_init" : 1e-5,
            "mu_min" : 1e-15,
            "barrier_tol_factor" : 1,
            "print_level" : 0,
            "linear_solver" : "ma27"
            }
        # solver_opts = {}
        plugin_opts = {"verbose" : False, "print_time" : False, "print_out" : False}
        self.opti0.solver('ipopt', plugin_opts, solver_opts)

    def solve_opti(self, k, x_0, last_u, agt_trajs, x_guess=None, u_guess=None, verbose=False):
        if x_guess is not None:
            self.opti.set_initial(self.x, x_guess)
        if u_guess is not None:
            self.opti.set_initial(self.u, u_guess)

        self.opti.set_value(self.last_u, last_u)
        self.opti.set_value(self.x_s, x_0)

        for i in range(len(self.agt_x)):
            if k < agt_trajs[i].shape[1]-1:
                traj = agt_trajs[i][:,k:min(k+self.N+1,agt_trajs[i].shape[1])]
                if traj.shape[1] < self.N+1:
                    traj = np.hstack((traj, np.tile(traj[:,-1].reshape((-1,1)), (self.N+1-traj.shape[1]))))
            else:
                traj = np.tile(agt_trajs[i][:,-1].reshape((-1,1)), (self.N+1))
            self.opti.set_value(self.agt_x[i], traj)

        try:
            sol = self.opti.solve()
            feasible = True
            x_pred = sol.value(self.x)
            u_pred = sol.value(self.u)
            return x_pred, u_pred, feasible
        except:
            print(self.opti.stats()['return_status'])
            return None, None, False

        return x_pred, u_pred, feasible

    def solve_opti0(self, k, x_0, agt_trajs, x_guess=None, u_guess=None, verbose=False):
        if x_guess is not None:
            self.opti0.set_initial(self.x0, x_guess)
        if u_guess is not None:
            self.opti0.set_initial(self.u0, u_guess)

        self.opti0.set_value(self.x_s0, x_0)

        for i in range(len(self.agt_x0)):
            if k < agt_trajs[i].shape[1]-1:
                traj = agt_trajs[i][:,k:min(k+self.N+1,agt_trajs[i].shape[1])]
                if traj.shape[1] < self.N+1:
                    traj = np.hstack((traj, np.tile(agt_trajs[i][:,-1].reshape((-1,1)), (self.N+1-traj.shape[1]))))
            else:
                traj = np.tile(agt_trajs[i][:,-1].reshape((-1,1)), (self.N+1))
            self.opti0.set_value(self.agt_x0[i], traj)

        try:
            sol = self.opti0.solve()
            feasible = True
            x_pred = sol.value(self.x0)
            u_pred = sol.value(self.u0)
            return x_pred, u_pred, feasible
        except:
            print(self.opti0.stats()['return_status'])
            return None, None, False

        return x_pred, u_pred, feasible

    def update_x_refs(self, x_refs):
        self.x_refs = x_refs
        self.opti.set_value(self.x_f, self.x_refs[self.x_refs_idx])

    def get_x_refs(self):
        return self.x_refs

    def advance_reference_idx(self):
        self.x_refs_idx += 1
        self.opti.set_value(self.x_f, self.x_refs[self.x_refs_idx])

    def reset_reference_idx(self):
        self.x_refs_idx = 0
        self.opti.set_value(self.x_f, self.x_refs[self.x_refs_idx])

    def get_reference_idx(self):
        return self.x_refs_idx

max_stars_repo_name: KTH-SML/multi-agent-LMPC

max_issues_repo_name: KTH-SML/multi-agent-LMPC

max_forks_repo_name: KTH-SML/multi-agent-LMPC


text:
import torch
import torch.nn as nn
import os
import numpy as np
import time
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import argparse
import cv2
from model import ShapeRecognizer
import glob  as gb

def img_toTensor(p):

    '''
    Torchvision pre-trained models use an input tensor, which scaled between [0 1].
    And the normalize with mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225].

    This function reads an image and pre-process it for the task of counting shapes.
    '''

    img = cv2.imread(p)
    img = cv2.resize(img, (256,256))
    im = img[...,::-1]

    # scale [0 1] and normalize the input image. We are not that interested about color info
    mean = np.array([0.5, 0.5, 0.5])
    std = np.array([0.5, 0.5, 0.5])
    im = (im / 255 - mean) / std
    im = im.astype(np.float32)
    im = torch.from_numpy(im)

    im = im.permute(2,0,1).unsqueeze(0).cuda()
    return img, im

def run_folder(net, lista, out_path):

    '''
    A function to run the network through a folder of images. Results are written in ./data/demo_output .
    '''
    # loop over image paths
    for p in lista:

        start = time.time()
        # read one image from path p.
        img, im = img_toTensor(p)

        # run net
        # We restrict the model to predict numbers between 0-6.
        pred_squar, pred_circ, pred_trgle, sa, ca, ta = net(im)
        _, ps = torch.max(pred_squar, dim=-1)
        _, pc = torch.max(pred_circ, dim=-1)
        _, pt = torch.max(pred_trgle, dim=-1)

        # save on disk, input image along with the attention masks
        con_masks = torch.cat(( sa[0], ca[0], ta[0]), 2).data.cpu().numpy().transpose(1,2,0)*255
        con_masks = cv2.resize(con_masks, (256*3, 256)).astype(np.uint8)
        # apply opencv pseudo-color
        con_masks = cv2.applyColorMap(con_masks, 11)

        #
        concat = np.concatenate((img, con_masks), 1)

        # name comprehensive of the current epoch and followed by the output predictions
        name = p.split('/')[-1].split('.')[0]
        name = name + '___' + str(ps[0].item()) + str(pc[0].item()) + str(pt[0].item()) + '.jpg'
        cv2.imwrite(os.path.join(out_path, name), concat)

        tempo = round((time.time() - start), 4)
        print('Input image of size {}x{} and processing time of {} seconds'.format(img.shape[0], img.shape[1], tempo))
        print('The input image has {} squares, {} circles and {} triangles.'.format(ps.item(), pc.item(), pt.item()))

def run_image(net, img_path):
        '''
        A function to run the network through one single image.
        '''
        # read one image.
        _, im = img_toTensor(img_path)
        # run net
        # We restrict the model to predict numbers between 0-6.
        pred_squar, pred_circ, pred_trgle, sa, ca, ta = net(im)
        _, ps = torch.max(pred_squar, dim=-1)
        _, pc = torch.max(pred_circ, dim=-1)
        _, pt = torch.max(pred_trgle, dim=-1)

        print('The input image has {} squares, {} circles and {} triangles.'.format(ps.item(), pc.item(), pt.item()))

def main():

    '''
    Inference on images resised to 256x256. Reads jpg format images.
    '''

    # Collect arguments (if any)
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--model', type=str, default=None, help='path to the model')
    parser.add_argument('-i', '--image', type=str, default=None, help='run demo on a single image')
    parser.add_argument('-f', '--folder', type=str, default=None, help='run demo on a specific folder of images')
    args = parser.parse_args()

    # general settings
    root = os.getcwd()
    model_path =  os.path.join(root,  args.model)

    # network loading
    net = ShapeRecognizer()
    net.load_state_dict(torch.load(model_path))
    net = net.cuda()
    net.eval()

    # two options
    if args.folder != None:
        # directory to write results in case
        out_path = os.path.join(root, 'data', 'demo_output')
        if not os.path.isdir(out_path):
            os.mkdir(out_path)
        # directory where to read images
        path_imgs = os.path.join(root, args.folder)
        l = gb.glob(os.path.join(path_imgs, '*.jpg'))

        run_folder(net, l, out_path)
        # end results
        print('\n\n Output results saved to:  {}'.format(out_path))

    elif args.image != None:
        image_path = os.path.join(root, args.image)
        run_image(net, image_path)

    else:
        print('Provide an input image or a folder preceded by -i or -f respectively')

if __name__ == "__main__":
    main()


text:
from enum import Enum
from typing import List, Optional, Union

import anndata
import napari
import numpy as np
import pandas as pd
from napari.utils.events import EmitterGroup, Event

from ...explore.sample import sample_anndata, sample_pandas

class TableSource(Enum):
    ANNDATA = "anndata"
    LAYER_FEATURES = "layer_features"

class ClusterAnnotationModel:
    def __init__(self):
        self.events = EmitterGroup(
            source=self,
            annotation_classes=Event,
            annotating=Event,
            auto_advance=Event,
            layer=Event,
            sample_annotated=Event,
            selected_sample=Event,
        )

        self._layer = None
        self._table_source = TableSource.LAYER_FEATURES
        self._label_column = "label_value"
        self._sample_data = None
        self._annotating = False
        self._group_by = None
        self._n_samples_per_group = 10
        self._random_seed = 42
        self._auto_advance = True

        # index of the label in the label image for the selected observation
        self._selected_sample = 0

    @property
    def layer(self) -> Optional[napari.layers.Labels]:
        return self._layer

    @layer.setter
    def layer(self, layer: Optional[napari.layers.Layer]):
        self._layer = layer
        self.events.layer()

    @property
    def table_source(self) -> str:
        """The source of the features table from the selected layer.

        If anndata, the source is assumed to be from layer.metadata["adata"].
        If layer_features, the source is assuemd to be from layer.features
        """
        return self._table_source.value

    @table_source.setter
    def table_source(self, table_source: Union[TableSource, str]):
        table_source = TableSource(table_source)
        self._table_source = table_source

    @property
    def feature_table(self) -> Optional[anndata.AnnData]:
        if self._layer is None:
            return None

        if self._table_source == TableSource.ANNDATA:
            return self._layer.metadata["adata"]
        else:
            return self._layer.features

    @property
    def annotations(self) -> pd.Series:
        """The annotations that have been added to the sampled data"""
        if self._table_source == TableSource.ANNDATA:
            return self._sample_data.obs[self._label_column]
        else:
            return self._sample_data[self._label_column]

    @property
    def annotation_table(self) -> pd.DataFrame:
        """The full table to which the annotations are being added.

        If the feature table is AnnData, returns obs.
        If the feature table is a pandas DataFrame, returns the full dataframe.
        """

        if self._table_source == TableSource.ANNDATA:
            return self.feature_table.obs
        else:
            return self.feature_table

    @property
    def sample_annotation_table(self) -> pd.DataFrame:
        """The sample table to which the annotations are being added.

        If the feature table is AnnData, returns obs.
        If the feature table is a pandas DataFrame, returns the full dataframe.
        """

        if self._table_source == TableSource.ANNDATA:
            return self._sample_data.obs
        else:
            return self._sample_data

    @property
    def annotation_classes(self) -> List[Union[str, int]]:
        return self._annotation_classes

    @annotation_classes.setter
    def annotation_classes(self, annotation_classes: List[Union[str, int]]):
        self._annotation_classes = annotation_classes
        self.events.annotation_classes()

    @property
    def annotating(self) -> bool:
        """State variable that is set to True when annotating.

        Setting annotating to True/False sets up/tears down the annotation mode.
        """
        return self._annotating

    @annotating.setter
    def annotating(self, annotating: bool):
        if annotating == self.annotating:
            # if the value is not changing, return
            return
        if self.layer is None:
            self._annotating = False

        if annotating is True:
            self._initialize_annotation()
        else:
            self._teardown_annotation()
        self._annotating = annotating
        self.events.annotating()

    def _initialize_annotation(self):
        """Initialize the samples from the feature table for annotation."""
        if self.feature_table is None:
            return

        if self._label_column not in self.annotation_table:
            self.annotation_table[self._label_column] = np.nan

        if self._table_source == TableSource.ANNDATA:
            self._sample_data = sample_anndata(
                self.feature_table,
                group_by=self._group_by,
                n_samples_per_group=self._n_samples_per_group,
                random_seed=self._random_seed,
            )
        else:
            self._sample_data = sample_pandas(
                self.feature_table,
                group_by=self._group_by,
                n_samples_per_group=self._n_samples_per_group,
                random_seed=self._random_seed,
            )

    def _teardown_annotation(self):
        """After annotation has completed, add the annotations back into the original feature table."""
        self._update_table(
            self.annotation_table, self.sample_annotation_table[self._label_column]
        )
        self._sample_data = None

    @property
    def n_samples(self) -> Optional[int]:
        """The total number of samples.
        Returns 0 if no samples have been initailized.
        """
        if self._sample_data is None:
            return 0
        return len(self._sample_data)

    @property
    def selected_sample(self) -> int:
        return self._selected_sample

    @selected_sample.setter
    def selected_sample(self, selected_sample):
        if (self._layer is None) or (self._sample_data is None):
            return
        self._selected_sample = selected_sample
        selected_sample_row = self.selected_sample_row

        if self._table_source == TableSource.ANNDATA:
            label_value = selected_sample_row.obs["label"].values[0]
        else:
            label_value = selected_sample_row["label"]
        self._layer.selected_label = label_value

        self.events.selected_sample()

    def next_sample(self):
        """Incrememnt the selected sample"""
        if self._sample_data is None:
            # do nothing if the sample data hasn't been set
            return
        self.selected_sample = (self.selected_sample + 1) % self.n_samples

    def previous_sample(self):
        """Decrement the selected sample"""
        if self._sample_data is None:
            # do nothing if the sample data hasn't been set
            return
        self.selected_sample = (
            (self.selected_sample - 1) + self.n_samples
        ) % self.n_samples

    @property
    def selected_sample_row(self) -> Optional[anndata.AnnData]:
        """The AnnData row corresponded to the currently selected observation"""
        if self._sample_data is None:
            return None
        else:
            if self._table_source == TableSource.ANNDATA:
                return self._sample_data[self.selected_sample, :]
            else:
                return self._sample_data.iloc[self.selected_sample, :]

    @property
    def auto_advance(self) -> bool:
        """Flag set to true when the next sample is automatically selected after making an annotation."""
        return self._auto_advance

    @auto_advance.setter
    def auto_advance(self, auto_advance: bool):
        self._auto_advance = auto_advance
        self.events.auto_advance()

    def start_annotation(
        self,
        annotation_classes: List[str] = [
            "true_positive",
            "false_negative",
            "false_positive",
        ],
        group_by: Optional[str] = None,
        n_samples_per_group: int = 10,
        random_seed: int = 42,
    ):
        if self.layer is None:
            return

        if self.annotating is False:
            if not self._validate_n_samples(
                group_by=group_by, n_samples_per_group=n_samples_per_group
            ):
                raise ValueError(
                    "n_samples_per_group is greater than the number of observations"
                )
            # set the labels in the QtLabelSelectWidget
            self.annotation_classes = annotation_classes

            self._group_by = group_by
            self._n_samples_per_group = n_samples_per_group
            self._random_seed = random_seed

            self.annotating = True
            self.selected_sample = 0
        else:
            self.annotating = False

    def _update_table(self, df: pd.DataFrame, new_column: pd.Series):
        column_name = new_column.name
        for index, value in new_column.iteritems():
            df.at[index, column_name] = value

    def _get_group_by_keys(self, combo_widget=None) -> List[str]:
        """Get the valid columns to group the features table by.

        This is generally used by magicgui to determine the valid choices
        for the widget generated from self.start_annotation().

        Returns
        -------
        group_by_keys : List[str]
            A list of valid keys to group the features by.
        """
        if self._layer is None:
            return []

        return self.annotation_table.columns.tolist()

    def _validate_n_samples(
        self, group_by: Optional[str], n_samples_per_group: int
    ) -> bool:
        """Check if the number of samples works with the selected table.

        Note: returns False if self._layer is None.
        """
        if self._layer is None:
            return False

        if group_by is not None:
            max_counts = self.annotation_table[group_by].value_counts().min()
        else:
            max_counts = len(self.annotation_table)
        return n_samples_per_group <= max_counts

    def _annotate_selected_sample(self, annotation_value):
        """Set the currently selected observation to the specified annotation value"""
        self.sample_annotation_table.iat[
            self.selected_sample,
            self.sample_annotation_table.columns.get_loc(self._label_column),
        ] = annotation_value

        self.events.sample_annotated()
        if self.auto_advance is True:
            self.next_sample()

max_stars_repo_name: kevinyamauchi/morphometrics

max_issues_repo_name: kevinyamauchi/morphometrics

max_forks_repo_name: kevinyamauchi/morphometrics


text:
import pandas as pd
import numpy as np
sys.path.insert(0, '/NHL_Game_Prediction/Functions/')
from functions.app_functions import *

''' Returns specific season'''
def extract_season(game, season):
    return game[(game['season'] == season)]

''' Specialized function to convert game times to EST'''
def convert_to_EST(game):
    game[['date_time_GMT']] = pd.to_datetime(game['date_time_GMT'])
    game[['date_time_GMT']] = game['date_time_GMT'].apply(lambda x: convert_timezones(x))
    return game

''' Helper function to convert to EST'''
def convert_timezones(time):
    return time.astimezone('EST')

''' Formats years correctly'''
def add_year(date, season):
    if int(date) > 1000:
        return season[:2] + ':' + date[:2] + ':' + date[2:]
    else:
        return season[2:] + ':' + date[:2] + ':' + date[2:]

''' Convert the odds datetime column to the correct format'''
def convert_odds_date(odds, season):
    odds['season'] = season
    odds[['Date']] = odds['Date'].astype(str)
    odds[['Date']] = odds['Date'].str.zfill(4)
    odds['cvt_date'] = odds.apply(lambda x: add_year(x.Date, x.season), axis=1)
    odds['cvt_date'] = pd.to_datetime(odds['cvt_date'], format='%y:%m:%d')
    return odds

''' Preprocess the odds data'''
def preprocess_odds_dataframe(odds):
    away = odds.loc[(odds['VH']=='V')]
    home = odds.loc[(odds['VH']=='H')]
    home = home.iloc[1:].reset_index(drop=True)
    away = away.iloc[1:].reset_index(drop=True)
    even = odds.iloc[::2]
    odd = odds.iloc[1::2]

    indexes = np.arange(0,len(odds)/2)
    even['index'] = indexes
    odd['index'] = indexes
    combined = even.merge(odd, on='index')
    combined = combined[['Team_x','Team_y', 'Open_x','Open_y','index','cvt_date_y']]
    combined = combined.rename(columns={'Team_x':'away_team', 'Team_y':'home_team', 'Open_x':'away_odds', 'Open_y':'home_odds', 'cvt_date_y':'date_time'})
    combined = combined.sort_values(by=['date_time','away_team'])
    return combined

'''Cleans the names of teams to match the odds df'''
def clean_team_names(y_pred):

    y_pred['away_team'] = y_pred['away_team'].str.replace(" ","")
    y_pred['home_team'] = y_pred['home_team'].str.replace(" ","")

    y_pred['away_team'] = y_pred['away_team'].str.replace("StLouis","St.Louis")
    y_pred['home_team'] = y_pred['home_team'].str.replace("StLouis","St.Louis")

    return y_pred

'''Merges the odds dataframe with predictions made by the model'''
def merge_odds_predictions(y_pred, odds):

    y_pred['date_time'] = y_pred['date_time_GMT'].dt.date
    y_pred['date_time'] = pd.to_datetime(y_pred['date_time'])

    merged = y_pred.merge(odds, left_on=['date_time','home_team','away_team'], right_on=['date_time','home_team','away_team'])

    return merged

'''Matches the games bet on by the selective model to all the games in the test set. This helps identify
periods of time that the selective model didn't bet, and allows for later graphing and comparisons between
model'''
def process_selective_model(games_bet_on_selective, profit_time_selective, total_wager_time_selective, merged):

    games_bet_on_selective['profit'] = profit_time_selective
    games_bet_on_selective['total_wager_time'] = total_wager_time_selective
    games_bet_on_selective = games_bet_on_selective[['date_time', 'home_team','away_team','profit','total_wager_time']].sort_values(by='date_time')
    temp_merged = merged[['date_time','home_team','away_team']].sort_values(by='date_time')
    join = temp_merged.merge(games_bet_on_selective, on=['date_time','home_team','away_team'],how='outer')
    join['profit'][0] = 0
    join['total_wager_time'][0] = 0
    join = join.fillna(method='ffill')
    join['rate_of_return'] = join['profit']/join['total_wager_time']
    join = join.fillna(0)

    return join

'''Makes baseline predictions - bets on the favourite of every game'''
def predict_baseline(predictions):

    predictions['baseline_pred'] = predictions.apply(lambda x: baseline_helper(x.away_odds, x.home_odds), axis=1)

    profit = 0
    profit_time = []
    total_bet_time = []
    total_bet = 0
    games_bet_on = []

    for i in range(len(predictions)):

        game = predictions.iloc[i]
        result = game['target']
        guess = game['baseline_pred']

        if guess == 0:

            odds = game['home_odds']

        else:

            odds = game['away_odds']

        if (result == guess):

            if odds > 0:

                total_bet += 100
                profit += odds

            else:

                total_bet += abs(odds)
                profit += 100

        else:

            if odds > 0:

                total_bet += 100
                profit -= 100

            else:

                total_bet += abs(odds)
                profit += odds

        profit_time.append(profit)
        total_bet_time.append(total_bet)
        games_bet_on.append(game)

    return profit, total_bet, profit_time, total_bet_time, games_bet_on

'''Helper function for the baseline'''
def baseline_helper(away_odds, home_odds):
    if home_odds < away_odds:
        return 0
    else:
        return 1

'''Makes predictions based on the model. Bets on every game but based on the models predictions'''
def make_money(predictions):

    profit = 0
    profit_time = []
    total_bet_time = []
    total_bet = 0
    games_bet_on = []

    for i in range(len(predictions)):

        game = predictions.iloc[i]
        result = game['target']
        guess = game['pred']

        if guess == 0:

            odds = game['home_odds']

        else:

            odds = game['away_odds']

        if (result == guess):

            if odds > 0:

                total_bet += 100
                profit += odds

            else:

                total_bet += abs(odds)
                profit += 100

        else:

            if odds > 0:

                total_bet += 100
                profit -= 100

            else:

                total_bet += abs(odds)
                profit += odds

        profit_time.append(profit)
        total_bet_time.append(total_bet)
        games_bet_on.append(game)

    return profit, total_bet, profit_time, total_bet_time, games_bet_on

'''Helper function to determine game days'''
def get_betting_dates(merged):
    merged['date_time'] = merged['date_time_GMT'].dt.date.astype('str')
    dates = list(merged['date_time'].astype('str').unique())

    return dates

'''Selective betting predictions. Bets on 1 game each day there are games, selecting the highest Expected Profit.
This is computed based on the model's confidence combined with the odds'''
def selective_betting(merged, dates):
    total_wager = 0
    profit = 0
    profit_time = []
    bets_placed = []

    games_bet_on = pd.DataFrame()

    for day in dates:
        games = merged[(merged['date_time'] == day)]
        games = games.reset_index(drop=True)
        profit_games, bad_games, to_bet = get_positive_profit(games)

        if to_bet == False:
            continue
        game_list = list(profit_games.index)
        profit_games = profit_games.reset_index()
        profit_games = profit_games.rename(columns={'level_0':'Unnamed: 0'})
        #print(day)

        confidence = abs(profit_games['home_prob'][0] - profit_games['away_prob'][0])

        if confidence > 0.2:
            bet = 200

        else:
            bet = 100

        wager = calc_bets(bet, len(profit_games), profit_games, game_list)

        if wager['pred'][0] == wager['target'][0]:

            profit += wager['win_profit'][0]

        else:

            profit -= 100

        profit_time.append(profit)
        total_wager += bet
        bets_placed.append(total_wager)
        games_bet_on = games_bet_on.append(wager)

    return profit, profit_time, bets_placed, total_wager, games_bet_on


text:
# -*- coding: utf-8 -*-
"""Opinion_mining.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/10FDuEHaj0sM7bTxQqaK7jENJa5dFTXeY

# Análisis de sentimiento
"""

!pip install beautifulsoup4
!pip install google-search-results-serpwow
!pip install google-search-results-serpwow --upgrade

# Commented out IPython magic to ensure Python compatibility.
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# %matplotlib inline
from scipy import stats
import pandas as pd
import multiprocessing
import random

from matplotlib import rc
from sklearn.preprocessing import scale
from sklearn.preprocessing import OneHotEncoder
import timeit
import itertools
import seaborn as sns
from collections import Counter
import operator

import requests
import json
import csv

from google.colab import drive
drive.mount('/content/drive')

review_results = []

urls = [
          #Tiradores: Tirador para muebles, cajón, armario
        'https://www.amazon.es/Emuca-9160951-Furniture-Handle-Nickel/product-reviews/B075YV44YJ/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.it/Emuca-9160951-Furniture-Handle-Nickel/product-reviews/B075YV44YJ/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.fr/Emuca-9160951-Furniture-Handle-Nickel/product-reviews/B075YV44YJ/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.co.uk/Emuca-9160951-Furniture-Handle-Nickel/product-reviews/B075YV44YJ/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',

          #Cajones: Kit de cajón para cocina y baño
        'https://www.amazon.es/Emuca-3018712-Caj%C3%B3n-Cierre-Blanco/product-reviews/B079H95LXG/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.it/Emuca-3018712-Caj%C3%B3n-Cierre-Blanco/product-reviews/B079H95LXG/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.fr/Emuca-3018712-Caj%C3%B3n-Cierre-Blanco/product-reviews/B079H95LXG/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.co.uk/Emuca-3018712-Caj%C3%B3n-Cierre-Blanco/product-reviews/B079H95LXG/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',

          #Armarios: Barra de armario abatible
        'https://www.amazon.es/Emuca-7086311-basculante-regulable-830-1150mm/dp/B00XKV5CWW/ref=cm_cr_arp_d_product_top?ie=UTF8',
        'https://www.amazon.it/Emuca-7086311-basculante-regulable-830-1150mm/dp/B00XKV5CWW/ref=cm_cr_arp_d_product_top?ie=UTF8',
        'https://www.amazon.fr/Emuca-7086311-basculante-regulable-830-1150mm/dp/B00XKV5CWW/ref=cm_cr_arp_d_product_top?ie=UTF8',
        'https://www.amazon.co.uk/Emuca-7086311-basculante-regulable-830-1150mm/dp/B00XKV5CWW/ref=cm_cr_arp_d_product_top?ie=UTF8',

          #Bases: Ruedas
        'https://www.amazon.es/2037121-ruedas-pivotantes-mueble-rodamientos/product-reviews/B01DDE7GHE/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.it/2037121-ruedas-pivotantes-mueble-rodamientos/product-reviews/B01DDE7GHE/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.fr/2037121-ruedas-pivotantes-mueble-rodamientos/product-reviews/B01DDE7GHE/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.co.uk/2037121-ruedas-pivotantes-mueble-rodamientos/product-reviews/B01DDE7GHE/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',

          #Equipamiento: Escurridor de cocina
        'https://www.amazon.es/product-reviews/B01N4SFEM5/ref=acr_dpx_hist_1?ie=UTF8&filterByStar=one_star&reviewerType=all_reviews',
        'https://www.amazon.it/product-reviews/B01N4SFEM5/ref=acr_dpx_hist_1?ie=UTF8&filterByStar=one_star&reviewerType=all_reviews',
        'https://www.amazon.fr/product-reviews/B01N4SFEM5/ref=acr_dpx_hist_1?ie=UTF8&filterByStar=one_star&reviewerType=all_reviews',
        'https://www.amazon.co.uk/product-reviews/B01N4SFEM5/ref=acr_dpx_hist_1?ie=UTF8&filterByStar=one_star&reviewerType=all_reviews',

          #Iluminación: Multienchufe
        'https://www.amazon.es/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.it/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.fr/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.co.uk/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews'

          #Montaje: Paragolpe
        'https://www.amazon.es/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.it/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.fr/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.co.uk/Emuca-5070125-multienchufe-retr%C3%A1ctil-3500W-16A/product-reviews/B01GV0PE26/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews'

          #Montaje: Sistema push para puerta de mueble
        'https://www.amazon.es/Emuca-1275221-Sistema-puerta-amortiguador/product-reviews/B0771STHRB/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.es/Emuca-1275221-Sistema-puerta-amortiguador/product-reviews/B0771STHRB/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.es/Emuca-1275221-Sistema-puerta-amortiguador/product-reviews/B0771STHRB/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews',
        'https://www.amazon.es/Emuca-1275221-Sistema-puerta-amortiguador/product-reviews/B0771STHRB/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews'

]

for url in urls:
  params = {
    'api_key': '9A7BE26DBE9D44A3924748F7F8CC1C35',
    'type': 'reviews',
    'url': url,
    'output': 'json'
    }

  api_result = requests.get('https://api.rainforestapi.com/request', params).json()

  if 'reviews' in api_result:
    review_results += api_result['reviews']

review_results

with open('opinion_mining.csv', 'w', encoding='utf-8') as file:
    fields = ['title', 'body', 'rating', 'review_country', 'verified_purchase', 'id']
    writer = csv.DictWriter(file, fieldnames=fields)
    writer.writeheader()
    for review in review_results:
        writer.writerow({
            'title' : review['title'] if 'title' in review else None,
            'body': review['body'] if 'body' in review else None,
            'rating': review['rating'] if 'rating' in review else None,
            'review_country': review['review_country'] if 'review_country' in review else None,
            'verified_purchase': review['verified_purchase'] if 'verified_purchase' in review else None,
            'id': review['id'] if 'id' in review else None
            })

df = pd.read_csv("/content/opinion_mining.csv")
df

df.groupby(by='review_country').size()

df.groupby(by='rating').size()

df.to_csv('/content/drive/My Drive/TFM/03_DATASETS/opinion_mining_all_reviews.csv',sep=';',decimal=',')

"""# Split by country and language"""

df_es=df[df['review_country']=='es']
df_it=df[df['review_country']=='it']
df_fr=df[df['review_country']=='fr']
df_gb=df[df['review_country']=='gb']
df_gb.sort_values(by='rating',ascending=True).head(15)

"""### Import packages"""

#Import packages
import re, string
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from textblob import TextBlob

nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')

"""### Defining functions

We remove all capital letters, punctuations, emojis, links, etc. Basically, removing all that is not words or numbers.
"""

#The function
def clean_text(text):
    text = text.lower()
    text = re.sub('@', '', text)
    text = re.sub('\[.*?\]', '', text)
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    text = re.sub(r"[^a-zA-Z ]+", "", text)

    #Tokenize the data (into words, which means breaking up every comment into a group of individual words.)
    text = nltk.word_tokenize(text)
    #Remove stopwords (which are words that don’t add value to a comment, like “the”, “a”, “and”, etc.)
    text = [w for w in text if w not in sw]
    return text

"""We lemmatize the text, which is the process of grouping together the inflected forms of a word so they can be analyzed as a single item, since they have a similar meaning (walking becomes walk, officers becomes officer, etc.)."""

#Lemmatizer
lemmatizer = WordNetLemmatizer()
def lem(text):
    text = [lemmatizer.lemmatize(t) for t in text]
    text = [lemmatizer.lemmatize(t, 'v') for t in text]
    return text

"""### Reviews UK

We define the text language we will analyze
"""

sw = stopwords.words('english')

"""We apply the function to the data"""

df_gb['body'] = df_gb['body'].apply(lambda x: clean_text(x))

"""We lemmatize the text, which is the process of grouping together the inflected forms of a word so they can be analyzed as a single item, since they have a similar meaning (walking becomes walk, officers becomes officer, etc.)."""

df_gb['body'] = df_gb['body'].apply(lambda x: lem(x))

"""We remove all empty comments from the data (some people just comment emojis, punctuations or things like that)."""

df_gb.reset_index(inplace=True, drop=True)

len(df_gb)

#Remove all empty comments
empty_comment = ""
for i in range(len(df_gb)):
    if df_gb['body'] [i]==empty_comment:
        df_gb=df_gb.drop(i)
df_gb=df_gb.reset_index(drop=True)

"""**Word Frequency** Let’s begin by looking at the word frequency, i.e. what words are repeated most often in the comments, using the FreqDist function from nltk."""

#From lists of comments to a single list containing all words
all_words=[]
for i in range(len(df_gb)):
  all_words = all_words + df_gb['body'][i]
#Get word frequency
nlp_words = nltk.FreqDist(all_words)
plot1 = nlp_words.plot(20, color='salmon', title='Word Frequency')

"""Let’s look for the most frequent bigrams, which means the most frequent pair of words that are next to each other in a comment."""

#Bigrams
bigrm = list(nltk.bigrams(all_words))
words_2 = nltk.FreqDist(bigrm)
words_2.plot(20, color='salmon', title='Bigram Frequency')

"""**Polarity**
Using Textblob’s sentiment function, we can look at the polarity of a comment, which is a float number ranging between -1 and 1 that is meant to represent if a comment is positive (1) or negative (-1). For instance, the sentence “Textblob is great” has a polarity of 0.4.
Here is how to get polarity for every comment and the distribution of said polarity:
"""

#Get sentiment from comments
df_gb['body'] = [str(thing) for thing in df_gb['body']]
sentiment_gb = []
for i in range(len(df_gb)):
    blob = TextBlob(df_gb['body'][i])
    for sentence in blob.sentences:
        sentiment_gb.append(sentence.sentiment.polarity)
df_gb['sentiment']=sentiment_gb
#Plot
df_gb['sentiment'].plot.hist(color='salmon', title='Comments Polarity English')

"""### Reviews Spain"""

sw = stopwords.words('spanish')

#The function
def clean_text(text):
    text = text.lower()
    text = re.sub('@', '', text)
    text = re.sub('\[.*?\]', '', text)
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    text = re.sub(r"[^a-zA-Z ]+", "", text)

    #Tokenize the data (into words, which means breaking up every comment into a group of individual words.)
    text = nltk.word_tokenize(text)
    #Remove stopwords (which are words that don’t add value to a comment, like “the”, “a”, “and”, etc.)
    text = [w for w in text if w not in sw]
    return text

df_es['body'] = df_es['body'].apply(lambda x: clean_text(x))

df_es['body'] = df_es['body'].apply(lambda x: lem(x))

df_es.reset_index(inplace=True, drop=True)

#Remove all empty comments
empty_comment = ""
for i in range(len(df_es)):
  if df_es['body'][i]==empty_comment:
    df_es=df_es.drop(i)
df_es=df_es.reset_index(drop=True)

#From lists of comments to a single list containing all words
all_words=[]
for i in range(len(df_es)):
  all_words = all_words + df_es['body'][i]
#Get word frequency
nlp_words = nltk.FreqDist(all_words)
plot1 = nlp_words.plot(20, color='salmon', title='Word Frequency')

#Bigrams
bigrm = list(nltk.bigrams(all_words))
words_2 = nltk.FreqDist(bigrm)
words_2.plot(20, color='salmon', title='Bigram Frequency')

#Get sentiment from comments
df_es['body'] = [str(thing) for thing in df_es['body']]
sentiment_es = []
for i in range(len(df_es)):
    blob = TextBlob(df_es['body'][i])
    for sentence in blob.sentences:
        sentiment_es.append(sentence.sentiment.polarity)
df_es['sentiment']=sentiment_es
#Plot
df_es['sentiment'].plot.hist(color='salmon', title='Comments Polarity Spanish')

"""### Reviews France"""

sw = stopwords.words('french')

#The function
def clean_text(text):
    text = text.lower()
    text = re.sub('@', '', text)
    text = re.sub('\[.*?\]', '', text)
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    text = re.sub(r"[^a-zA-Z ]+", "", text)

    #Tokenize the data (into words, which means breaking up every comment into a group of individual words.)
    text = nltk.word_tokenize(text)
    #Remove stopwords (which are words that don’t add value to a comment, like “the”, “a”, “and”, etc.)
    text = [w for w in text if w not in sw]
    return text

df_fr['body'] = df_fr['body'].apply(lambda x: clean_text(x))

df_fr['body'] = df_fr['body'].apply(lambda x: lem(x))

df_fr.reset_index(inplace=True, drop=True)

#Remove all empty comments
empty_comment = ""
for i in range(len(df_fr)):
  if df_fr['body'][i]==empty_comment:
    df_fr=df_fr.drop(i)
df_fr=df_fr.reset_index(drop=True)

#From lists of comments to a single list containing all words
all_words=[]
for i in range(len(df_fr)):
  all_words = all_words + df_fr['body'][i]
#Get word frequency
nlp_words = nltk.FreqDist(all_words)
plot1 = nlp_words.plot(20, color='salmon', title='Word Frequency')

#Bigrams
bigrm = list(nltk.bigrams(all_words))
words_2 = nltk.FreqDist(bigrm)
words_2.plot(20, color='salmon', title='Bigram Frequency')

#Get sentiment from comments
df_fr['body'] = [str(thing) for thing in df_fr['body']]
sentiment_fr = []
for i in range(len(df_fr)):
    blob = TextBlob(df_fr['body'][i])
    for sentence in blob.sentences:
        sentiment_fr.append(sentence.sentiment.polarity)
df_fr['sentiment']=sentiment_fr
#Plot
df_fr['sentiment'].plot.hist(color='salmon', title='Comments Polarity French')

"""### Reviews Italy"""

sw = stopwords.words('italian')

#The function
def clean_text(text):
    text = text.lower()
    text = re.sub('@', '', text)
    text = re.sub('\[.*?\]', '', text)
    text = re.sub('https?://\S+|www\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    text = re.sub(r"[^a-zA-Z ]+", "", text)

    #Tokenize the data (into words, which means breaking up every comment into a group of individual words.)
    text = nltk.word_tokenize(text)
    #Remove stopwords (which are words that don’t add value to a comment, like “the”, “a”, “and”, etc.)
    text = [w for w in text if w not in sw]
    return text

df_it['body'] = df_it['body'].apply(lambda x: clean_text(x))

df_it['body'] = df_it['body'].apply(lambda x: lem(x))

df_it.reset_index(inplace=True, drop=True)

#Remove all empty comments
empty_comment = ""
for i in range(len(df_it)):
  if df_it['body'][i]==empty_comment:
    df_it=df_it.drop(i)
df_it=df_it.reset_index(drop=True)

#From lists of comments to a single list containing all words
all_words=[]
for i in range(len(df_it)):
  all_words = all_words + df_it['body'][i]
#Get word frequency
nlp_words = nltk.FreqDist(all_words)
plot1 = nlp_words.plot(20, color='salmon', title='Word Frequency')

#Bigrams
bigrm = list(nltk.bigrams(all_words))
words_2 = nltk.FreqDist(bigrm)
words_2.plot(20, color='salmon', title='Bigram Frequency')

#Get sentiment from comments
df_it['body'] = [str(thing) for thing in df_it['body']]
sentiment_it = []
for i in range(len(df_it)):
    blob = TextBlob(df_it['body'][i])
    for sentence in blob.sentences:
        sentiment_it.append(sentence.sentiment.polarity)
df_it['sentiment']=sentiment_it
#Plot
df_it['sentiment'].plot.hist(color='salmon', title='Comments Polarity Italian')

"""### Summary"""

df_gb_mean = df_gb['sentiment'].mean()
df_fr_mean = df_fr['sentiment'].mean()
df_it_mean = df_it['sentiment'].mean()
df_es_mean = df_es['sentiment'].mean()

opinion_mining = {'country': ['Great Britain','France','Italy','Spain'],
        'comments_polarity': [df_gb_mean,df_fr_mean,df_it_mean,df_es_mean]
        }

df_opinion_mining = pd.DataFrame(opinion_mining, columns = ['country', 'comments_polarity'])
df_opinion_mining

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
word_string=" ".join(df['title'].str.lower())
wordcloud = WordCloud(stopwords=STOPWORDS,
                          background_color='white',
                      max_words=300
                         ).generate(word_string)

plt.clf()
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
plt.figure(figsize=(20, 6),dpi=500)


text:
import logging
import os

import matplotlib as mpl
import numpy as np

mpl.use('Agg')

import matplotlib.pyplot as plt

from motifscan.io.utils import replace_special_char

logger = logging.getLogger(__name__)

def have_same_region_length(regions):
    length = None
    for region in regions:
        if not length:
            length = region.end - region.start
        else:
            if region.end - region.start != length:
                return False
    return True

def have_value_attr(regions):
    for region in regions:
        if region.score is None:
            return False
    return True

def smooth(x, window_len=11):
    if len(x) <= window_len:
        return x
    s = np.r_[x[window_len - 1:0:-1], x, x[-2:-window_len - 1:-1]]
    w = np.hanning(window_len)
    y = np.convolve(w / w.sum(), s, mode='same')
    return y[window_len - 1:-window_len + 1]

def plot_motif_sites_dist(output_dir, regions, pwms, motif_sites,
                          window_size):
    if window_size <= 0:
        if len(regions) == 0:
            logger.error("No regions found for plotting")
            return
        if not have_same_region_length(regions):
            logger.error("Unable to plot when the scanning length is "
                         "different across regions")
            return

    output_dir = os.path.join(output_dir, 'plots')
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    if window_size <= 0:
        window_size = regions[0].end - regions[0].start
    extend = window_size // 2

    for pwm, sites in zip(pwms, motif_sites):
        logger.debug(f"Plotting for {pwm.matrix_id + ',' + pwm.name}")
        distances = []
        for idx, region in enumerate(regions):
            for site in sites[idx]:
                distances.append(site.start + pwm.length / 2 - region.summit)
        bin_edges = np.arange(-extend - 5, extend + 6, 10)
        freq, _ = np.histogram(distances, bins=bin_edges)
        if len(distances) > 0:
            freq = smooth(freq / len(distances))
        x = []
        for i in range(len(freq)):
            x.append((bin_edges[i] + bin_edges[i + 1]) // 2)
        fig = plt.figure(figsize=(4, 3.5))
        ax = fig.gca()
        ax.bar(x, freq, width=10, color='#4169E1',
               label=pwm.matrix_id + ',' + pwm.name)
        ax.legend(loc='upper right', fontsize=8, frameon=False)
        ax.set_xlabel('Distance to Center/Summit', fontsize=8)
        ax.set_ylabel('Fraction', fontsize=8)
        ax.set_xlim(-extend - 5, extend + 5)
        if len(distances) > 0:
            ax.set_ylim(0, 1.2 * max(freq))
        else:
            ax.set_ylim(0, 0.1)
        ax.tick_params(axis='both', which='major', labelsize=8)
        fig.subplots_adjust(left=0.15, right=0.98, bottom=0.15, top=0.95)
        name = replace_special_char(pwm.matrix_id + '_' + pwm.name)
        path = os.path.join(output_dir, f"{name}_sites_distributions.pdf")
        fig.savefig(path)
        plt.close()

def plot_motif_sites_enrich(output_dir, regions, pwms, motif_sites,
                            motif_sites_control):
    if not have_value_attr(regions):
        logger.error("Unable to plot when some regions have no scores set for "
                     "sorting")
        return
    n_regions_input = len(regions)
    if len(str(n_regions_input)) < 2:
        logger.error(
            f"Too few regions to plot: {n_regions_input}")
        return

    output_dir = os.path.join(output_dir, 'plots')
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    # sort regions by score
    ranked_idx = sorted(range(n_regions_input),
                        key=lambda x: regions[x].score, reverse=True)
    flanking_size = n_regions_input // 100

    for pwm, sites_input, sites_control in zip(pwms, motif_sites,
                                               motif_sites_control):
        logger.debug(f"Plotting for {pwm.matrix_id + ',' + pwm.name}")
        n_regions_control = len(sites_control)
        n_control = sum([len(sites) > 0 for sites in sites_control])
        ratio_control = n_control / n_regions_control
        if ratio_control == 0:
            ratio_control = 1

        sites_input = np.asarray(sites_input)[ranked_idx]
        fold_changes = []
        has_site_flag = [len(sites) > 0 for sites in sites_input]
        for idx in range(n_regions_input):
            head = max(0, idx - flanking_size)
            tail = min(idx + flanking_size, n_regions_input)
            ratio_input = sum(has_site_flag[head:tail]) / (tail - head)
            fold_changes.append(ratio_input / ratio_control)
        fold_changes = smooth(fold_changes)

        fig = plt.figure(figsize=(4, 3.5))
        ax = fig.gca()
        ax.bar(range(1, n_regions_input + 1), fold_changes, width=1,
               color='#4169E1', label=pwm.matrix_id + ',' + pwm.name)
        ax.legend(loc='upper right', fontsize=8, frameon=False)
        ax.set_xlabel('Regions Ranked by Score (Descending)', fontsize=8)
        ax.set_ylabel('Fold Change', fontsize=8)
        ax.set_xlim(0, n_regions_input)
        y_max = max(fold_changes)
        if y_max > 0:
            ax.set_ylim(0, 1.2 * y_max)
        else:
            ax.set_ylim(0, 0.1)
        ax.tick_params(axis='both', which='major', labelsize=8)
        fig.subplots_adjust(left=0.15, right=0.98, bottom=0.15, top=0.95)
        name = replace_special_char(pwm.matrix_id + '_' + pwm.name)
        path = os.path.join(output_dir, f"{name}_sites_enrichment.pdf")
        fig.savefig(path)
        plt.close()

max_stars_repo_name: shao-lab/Motifscan

max_issues_repo_name: shao-lab/Motifscan

max_forks_repo_name: shao-lab/Motifscan


text:
#!/usr/bin/env python
#------------------------------------------------------------------------------
# Name:        colorMapCreator.py
# Purpose:     Generate reasonable diverging colormaps using the technique
#              presented in "Diverging Color Maps for Scientific Visualization
#              (Expanded)" by Kenneth Moreland.
#
# Author:      Carlo Barth
#
# Created:     22.10.2013
# Copyright:   (c) 2013
#------------------------------------------------------------------------------

# Imports
import numpy as np

# =============================================================================
# ====================== The Class ColorMapCreator ============================
# =============================================================================

class ColorMapCreator:
    """
    Class ColorMapCreator:
    Create diverging colormaps from RGB1 to RGB2 using the method of Moreland
    or a simple CIELAB-interpolation. numColors controls the number of color
    values to output (odd number) and divide gives the possibility to output
    RGB-values from 0.0-1.0 instead of 0-255. If a filename different than
    "" is given, the colormap will be saved to this file, otherwise a simple
    output using print will be given.
    """

    # ======================== Global Variables ===============================

    # Reference white-point D65
    Xn, Yn, Zn = [95.047, 100.0, 108.883] # from Adobe Cookbook

    # Transfer-matrix for the conversion of RGB to XYZ color space
    transM = np.array([[0.4124564, 0.2126729, 0.0193339],
                        [0.3575761, 0.7151522, 0.1191920],
                        [0.1804375, 0.0721750, 0.9503041]])

    # ============================= Functions =================================

    def __init__(self, RGB1, RGB2, numColors = 33., divide = 255.,
                  method = "moreland", filename = ""):

        # create a class variable for the number of colors
        self.numColors = numColors

        # assert an odd number of points
        assert np.mod(numColors,2) == 1, \
            "For diverging colormaps odd numbers of colors are desireable!"

        # assert a known method was specified
        knownMethods = ["moreland", "lab"]
        assert method in knownMethods, "Unknown method was specified!"

        if method == knownMethods[0]:
            #generate the Msh diverging colormap
            colorMap = self.generateColorMap(RGB1, RGB2, divide)
        elif method == knownMethods[1]:
            # generate the Lab diverging colormap
            colorMap = self.generateColorMapLab(RGB1, RGB2, divide)

        # print out the colormap of save it to file named filename
        if filename == "":
            for c in colorMap:
                    print "{0}, {1}, {2}".format(c[0], c[1], c[2])
        else:
            with open(filename, "w") as f:
                for c in colorMap:
                    f.write("{0}, {1}, {2}\n".format(c[0], c[1], c[2]))
    #-

    def rgblinear(self, RGB):
        """
        Conversion from the sRGB components to RGB components with physically
        linear properties.
        """

        # initialize the linear RGB array
        RGBlinear = np.zeros((3,))

        #  calculate the linear RGB values
        for i,value in enumerate(RGB):
            value = float(value) / 255.
            if value > 0.04045 :
                value = ( ( value + 0.055 ) / 1.055 ) ** 2.4
            else :
                value = value / 12.92
            RGBlinear[i] = value * 100.
        return RGBlinear
    #-

    def sRGB(self, RGBlinear):
        """
        Back conversion from linear RGB to sRGB.
        """

        # initialize the sRGB array
        RGB = np.zeros((3,))

        #  calculate the sRGB values
        for i,value in enumerate(RGBlinear):
            value = float(value) / 100.

            if value > 0.00313080495356037152:
                value = (1.055 * np.power(value,1./2.4) ) - 0.055
            else :
                value = value * 12.92

            RGB[i] = round(value * 255.)
        return RGB
    #-

    def rgb2xyz(self, RGB):
        """
        Conversion of RGB to XYZ using the transfer-matrix
        """
        return np.dot(self.rgblinear(RGB), self.transM)
    #-

    def xyz2rgb(self, XYZ):
        """
        Conversion of RGB to XYZ using the transfer-matrix
        """
        #return np.round(np.dot(XYZ, np.array(np.matrix(transM).I)))
        return self.sRGB(np.dot(XYZ, np.array(np.matrix(self.transM).I)))
    #-

    def rgb2Lab(self, RGB):
        """
        Conversion of RGB to CIELAB
        """

        # convert RGB to XYZ
        X, Y, Z = (self.rgb2xyz(RGB)).tolist()

        # helper function
        def f(x):
            limit = 0.008856
            if x> limit:
                return np.power(x, 1./3.)
            else:
                return 7.787*x + 16./116.

        # calculation of L, a and b
        L = 116. * ( f(Y/self.Yn) - (16./116.) )
        a = 500. * ( f(X/self.Xn) - f(Y/self.Yn) )
        b = 200. * ( f(Y/self.Yn) - f(Z/self.Zn) )
        return np.array([L, a, b])
    #-

    def Lab2rgb(self, Lab):
        """
        Conversion of CIELAB to RGB
        """

        # unpack the Lab-array
        L, a, b = Lab.tolist()

        # helper function
        def finverse(x):
            xlim = 0.008856
            a = 7.787
            b = 16./116.
            ylim = a*xlim+b
            if x > ylim:
                return np.power(x, 3)
            else:
                return ( x - b ) / a

        # calculation of X, Y and Z
        X = self.Xn * finverse( (a/500.) + (L+16.)/116. )
        Y = self.Yn * finverse( (L+16.)/116. )
        Z = self.Zn * finverse( (L+16.)/116. - (b/200.) )

        # conversion of XYZ to RGB
        return self.xyz2rgb(np.array([X,Y,Z]))
    #-

    def Lab2Msh(self, Lab):
        """
        Conversion of CIELAB to Msh
        """

        # unpack the Lab-array
        L, a, b = Lab.tolist()

        # calculation of M, s and h
        M = np.sqrt(np.sum(np.power(Lab, 2)))
        s = np.arccos(L/M)
        h = np.arctan2(b,a)
        return np.array([M,s,h])
    #-

    def Msh2Lab(self, Msh):
        """
        Conversion of Msh to CIELAB
        """

        # unpack the Msh-array
        M, s, h = Msh.tolist()

        # calculation of L, a and b
        L = M*np.cos(s)
        a = M*np.sin(s)*np.cos(h)
        b = M*np.sin(s)*np.sin(h)
        return np.array([L,a,b])
    #-

    def rgb2Msh(self, RGB):
        """ Direct conversion of RGB to Msh. """
        return self.Lab2Msh(self.rgb2Lab(RGB))
    #-

    def Msh2rgb(self, Msh):
        """ Direct conversion of Msh to RGB. """
        return self.Lab2rgb(self.Msh2Lab(Msh))
    #-

    def adjustHue(self, MshSat, Munsat):
        """
        Function to provide an adjusted hue when interpolating to an
        unsaturated color in Msh space.
        """

        # unpack the saturated Msh-array
        Msat, ssat, hsat = MshSat.tolist()

        if Msat >= Munsat:
            return hsat
        else:
            hSpin = ssat * np.sqrt(Munsat**2 - Msat**2) / \
                    (Msat * np.sin(ssat))
            if hsat > -np.pi/3:
                return hsat + hSpin
            else:
                return hsat - hSpin
    #-

    def interpolateColor(self, RGB1, RGB2, interp):
        """
        Interpolation algorithm to automatically create continuous diverging
        color maps.
        """

        # convert RGB to Msh and unpack
        Msh1 = self.rgb2Msh(RGB1)
        M1, s1, h1 = Msh1.tolist()
        Msh2 = self.rgb2Msh(RGB2)
        M2, s2, h2 = Msh2.tolist()

        # If points saturated and distinct, place white in middle
        if (s1>0.05) and (s2>0.05) and ( np.abs(h1-h2) > np.pi/3. ):
            Mmid = max([M1, M2, 88.])
            if interp < 0.5:
                M2 = Mmid
                s2 = 0.
                h2 = 0.
                interp = 2*interp
            else:
                M1 = Mmid
                s1 = 0.
                h1 = 0.
                interp = 2*interp-1.

        # Adjust hue of unsaturated colors
        if (s1 < 0.05) and (s2 > 0.05):
            h1 = self.adjustHue(np.array([M2,s2,h2]), M1)
        elif (s2 < 0.05) and (s1 > 0.05):
            h2 = self.adjustHue(np.array([M1,s1,h1]), M2)

        # Linear interpolation on adjusted control points
        MshMid = (1-interp)*np.array([M1,s1,h1]) + \
                 interp*np.array([M2,s2,h2])

        return self.Msh2rgb(MshMid)
    #-

    def generateColorMap(self, RGB1, RGB2, divide):
        """
        Generate the complete diverging color map using the Moreland-technique
        from RGB1 to RGB2, placing "white" in the middle. The number of points
        given by "numPoints" controls the resolution of the colormap. The
        optional parameter "divide" gives the possibility to scale the whole
        colormap, for example to have float values from 0 to 1.
        """

        # calculate
        scalars = np.linspace(0., 1., self.numColors)
        RGBs = np.zeros((self.numColors, 3))
        for i,s in enumerate(scalars):
            RGBs[i,:] = self.interpolateColor(RGB1, RGB2, s)
        return RGBs/divide
    #-

    def generateColorMapLab(self, RGB1, RGB2, divide):
        """
        Generate the complete diverging color map using a transition from
        Lab1 to Lab2, transitioning true RGB-white. The number of points
        given by "numPoints" controls the resolution of the colormap. The
        optional parameter "divide" gives the possibility to scale the whole
        colormap, for example to have float values from 0 to 1.
        """

        # convert to Lab-space
        Lab1 = self.rgb2Lab(RGB1)
        Lab2 = self.rgb2Lab(RGB2)
        LabWhite = np.array([100., 0., 0.])

        # initialize the resulting arrays
        Lab = np.zeros((self.numColors ,3))
        RGBs = np.zeros((self.numColors ,3))
        N2 = np.floor(self.numColors/2.)

        # calculate
        for i in range(3):
            Lab[0:N2+1, i] = np.linspace(Lab1[i], LabWhite[i], N2+1)
            Lab[N2:, i] = np.linspace(LabWhite[i], Lab2[i], N2+1)
        for i,l in enumerate(Lab):
            RGBs[i,:] = self.Lab2rgb(l)
        return RGBs/divide
    #-

# =============================================================================
# ========================== The Main-Function ================================
# =============================================================================

def main():

    # define the initial and final RGB-colors (low and high end of the diverging
    # colormap
    RGB1 = np.array([59, 76, 192])
    RGB2 = np.array([180, 4, 38])

    # create a new instance of the ColorMapCreator-class using the desired
    # options

    # simple printout
    colormap = ColorMapCreator(RGB1, RGB2)

    # save to specific file
##    import os
##    current_directory = os.getcwd()
##    fname = "RedBlueColormap.dat"
##    filename = os.path.join(current_directory, fname)
##    colormap = ColorMapCreator(RGB1, RGB2, filename = filename)

if __name__ == '__main__':
    main()

max_stars_repo_name: iesus/thesis-production-models