text:
#Group Bar Plot In MatPlotLib
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
        'pre_score': [4, 24, 31, 2, 3],
        'mid_score': [25, 94, 57, 62, 70],
        'post_score': [5, 43, 23, 23, 51]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'pre_score', 'mid_score', 'post_score'])

# Setting the positions and width for the bars
pos = list(range(len(df['pre_score'])))
width = 0.25

# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))

# Create a bar with pre_score data,
# in position pos,
plt.bar(pos,
        #using df['pre_score'] data,
        df['pre_score'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#EE3224',
        # with label the first value in first_name
        label=df['first_name'][0])

# Create a bar with mid_score data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos],
        #using df['mid_score'] data,
        df['mid_score'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#F78F1E',
        # with label the second value in first_name
        label=df['first_name'][1])

# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos],
        #using df['post_score'] data,
        df['post_score'],
        # of width
        width,
        # with alpha 0.5
        alpha=0.5,
        # with color
        color='#FFC222',
        # with label the third value in first_name
        label=df['first_name'][2])

# Set the y axis label
ax.set_ylabel('Score')

# Set the chart's title
ax.set_title('Test Subject Scores')

# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])

# Set the labels for the x ticks
ax.set_xticklabels(df['first_name'])

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df['pre_score'] + df['mid_score'] + df['post_score'])] )

# Adding the legend and showing the plot
plt.legend(['Pre Score', 'Mid Score', 'Post Score'], loc='upper left')
plt.grid()
plt.show()


text:
#This code finds an estimate for the Stefan Boltzmann (SB) constant using numerical integration.

import numpy as np
from scipy.integrate import quad

#Required constants defined below.
K=1.3806488*10**-23
h=1.054571726*10**-34
c=299792458
SB=5.670373*10**-8

def I(x):
    return (x**3)/(np.exp(x)-1) #expression to be integrated.

W=quad(I,0,710) #This function carries out the integration, arguments(integrand, lower limit, upper limit).
print W
sigma=((K**4)/(4*(np.pi)**2*c**2*h**3))*W[0] #Finds an estimate of SB constant.

error=((SB-sigma)/SB)*100 #finds the error on the calculation as a percentage of the true value.

print 'Result of integration=', W[0]
print 'Stefan Boltzmann Constant=',sigma,'Wm^-2K^-4'
print 'The relative error on this calculation is ',error,'%'

ext: py

max_stars_repo_name: FHomewood/ScientificComputing

max_issues_repo_name: FHomewood/ScientificComputing

max_forks_repo_name: FHomewood/ScientificComputing

reason: import numpy,from scipy


text:
"""Convenient generic type definitions (used in multiple places)."""
import pathlib
from typing import cast, NewType, Sequence, Union

import numpy as np
import pandas as pd

Pathlike = Union[pathlib.Path, str]  # Represents input paths

# Represents latent codes. Index should identify cells.
Latents = NewType("Latents", pd.DataFrame)

def create_latents(latent_codes: np.ndarray, index: Sequence) -> Latents:
    """A factory method for `Latents`.

    Args:
        latent_codes: numpy array, shape (n_cells, k),
            where k is the latent space dimension
        index: cell identifiers

    Returns:
        Latents
    """
    df = pd.DataFrame(latent_codes, index=index)
    return cast(Latents, df)

def write_latents(latent_codes: Latents, path: Pathlike) -> None:
    """Writes latent codes to the disk.

    Args:
        latent_codes: latent codes
        path: path where the latent codes should be dumped

    See Also:
        read_latents, used to read latent codes from the disk
    """
    latent_codes = cast(pd.DataFrame, latent_codes)
    latent_codes.to_csv(path)

def read_latents(path: Pathlike) -> Latents:
    """Reads latent codes from the disk.

    Args:
        path: path where

    Returns:
        latent codes

    See Also:
        write_latents, function used to dump the latent codes to the disk
    """
    codes = pd.read_csv(path, index_col=0)
    return cast(Latents, codes)

max_stars_repo_name: BoevaLab/scCanSig

max_issues_repo_name: BoevaLab/scCanSig

max_forks_repo_name: BoevaLab/scCanSig

reason: import numpy


text:
from micarraylib.datasets import starss2022_loader
from micarraylib.arraycoords.array_shapes_utils import _polar2cart
import numpy as np
import soundata
import pytest
import librosa

def test_starss2022_init():

    a = starss2022_loader.starss2022(download=False, data_home="~/")
    assert a.name == "starss2022"
    assert a.fs == 24000
    assert len(a.array_format) == 1
    assert a.array_format["Eigenmike"] == "A"
    assert len(a.capsule_coords["Eigenmike"]) == 4
    assert list(a.capsule_coords["Eigenmike"].keys()) == ["6", "10", "22", "26"]
    b = _polar2cart(a.capsule_coords["Eigenmike"], "radians")
    # TODO: analize why the atol is needed
    assert np.allclose(
        np.mean(np.array([c for c in b.values()]), axis=0), [0, 0, 0], atol=1e-4
    )
    assert (
        len(a.micarray_clip_ids["Eigenmike"])
        == len(soundata.initialize("starss2022", data_home="~/").clip_ids) / 2
    )  # because soundata has clip_ids for each format A and B
    assert (
        len(a.clips_list)
        == len(soundata.initialize("starss2022", data_home="~/").clip_ids) / 2
    )  # because soundata has clip_ids for each format A and B

def test_starss2022_get_audio_numpy():

    a = starss2022_loader.starss2022(
        download=False, data_home="tests/resources/datasets/starss2022"
    )
    A = a.get_audio_numpy("dev/dev-train-sony/fold3_room21_mix001")
    B = a.get_audio_numpy("dev/dev-train-sony/fold3_room21_mix001", fmt="B")

    Al = librosa.load(
        "tests/resources/datasets/starss2022/mic_dev/dev-train-sony/fold3_room21_mix001.wav",
        sr=24000,
        mono=False,
    )[0]
    Bl = librosa.load(
        "tests/resources/datasets/starss2022/foa_dev/dev-train-sony/fold3_room21_mix001.wav",
        sr=24000,
        mono=False,
    )[0]

    assert (B == Bl).all()
    assert (A == Al).all()

    with pytest.raises(ValueError):
        a.get_audio_numpy("a", "b")
    with pytest.raises(ValueError):
        a.get_audio_numpy("a", "Eigenmike")

def test_starss2022_get_audio_events():

    a = starss2022_loader.starss2022(
        download=False, data_home="tests/resources/datasets/starss2022"
    )
    A = a.get_audio_events("dev/dev-train-sony/fold3_room21_mix001")

    with pytest.raises(ValueError):
        a.get_audio_events("a")

max_stars_repo_name: micarraylib/micarraylib

max_issues_repo_name: micarraylib/micarraylib

max_forks_repo_name: micarraylib/micarraylib


text:
import subprocess
import os

import pytest
import warnings
import string
import numpy as np
import pandas as pd

from opendp.whitenoise.metadata import CollectionMetadata
from opendp.whitenoise.synthesizers.mwem import MWEMSynthesizer

git_root_dir = subprocess.check_output("git rev-parse --show-toplevel".split(" ")).decode("utf-8").strip()

meta_path = os.path.join(git_root_dir, os.path.join("service", "datasets", "PUMS.yaml"))
csv_path = os.path.join(git_root_dir, os.path.join("service", "datasets", "PUMS.csv"))

schema = CollectionMetadata.from_file(meta_path)
df = pd.read_csv(csv_path, index_col=0)
df = df.drop(["income"], axis=1)
nf = df.to_numpy().astype(int)

synth = MWEMSynthesizer(split_factor=3)

faux_synth = MWEMSynthesizer(split_factor=1)

test_data = np.array([[1,1,1],[2,2,2],[3,3,3]])

test_histogram = [[[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]],
        [[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]],
        [[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]]]

test_histogram_dims = (3,3,3)

class TestMWEM:
    def test_fit(self):
        synth.fit(nf)
        assert synth.histograms

    def test_sample(self):
        sample_size = nf.shape[0]
        synthetic = synth.sample(sample_size)
        assert synthetic.shape == nf.shape

    def test_initialize_A(self):
        h = synth._initialize_A(test_histogram,(3,3,3))
        assert int(np.sum(h)) == int(np.sum(test_histogram))

    def test_histogram_from_data_attributes(self):
        three_dims = synth._histogram_from_data_attributes(test_data,np.array([[0,1,2]]))
        one_dims = synth._histogram_from_data_attributes(test_data,np.array([np.array([0]),np.array([1]),np.array([2])]))
        assert three_dims[0][1] == [3,3,3]
        assert one_dims[0][1] == [3]

    def test_compose_arbitrary_slices(self):
        ss = synth._compose_arbitrary_slices(10, (3,3,3))
        assert np.array(ss).shape == (10,3)

    def test_evaluate(self):
        ss = synth._evaluate([slice(0, 2, None), slice(0, 2, None), slice(0, 3, None)],np.array(test_histogram))
        assert ss == 6.0

    def test_binary_replace_in_place_slice(self):
        b = synth._binary_replace_in_place_slice(np.array(test_histogram), [slice(0, 2, None), slice(0, 2, None), slice(0, 3, None)])
        assert (b == np.array([[[1., 1., 0.],
                            [1., 1., 0.],
                            [0., 0., 1.]],
                            [[1., 1., 0.],
                            [1., 1., 0.],
                            [0., 0., 1.]],
                            [[1., 1., 0.],
                            [1., 1., 0.],
                            [0., 0., 1.]]])).all()

    def test_reorder(self):
        original = np.array([[1,2,3,4,5,6], [6,7,8,9,10,11]])
        splits = np.array([[1,3,4],[0,2,5]])
        m1 = original[:, splits[0]]
        m2 = original[:, splits[1]]
        reordered = synth._reorder(splits)
        reconstructed = np.hstack((m1,m2))
        assert (original == reconstructed[:, reordered]).all()

    def test_generate_splits(self):
        assert (synth._generate_splits(3,3) == np.array([[0, 1, 2]])).all()

    # TODO: More split tests

    def test_faux_fit(self):
        pytest.warns(Warning, faux_synth.fit, test_data)
        assert faux_synth.histograms


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

import sys
import os
import gc
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from utils.rankings import get_score_rankings, rank_query, invert_rankings  # , get_candidate_score_rankings
import multileaving.SampleBasedProbabilisticMultileave as pm
import multileaving.candidatepreselection as cps
from algorithms.probmeanbandit import ProbMeanBandit
import numpy as np

class EmbeddingBandit(ProbMeanBandit):

    def __init__(self, embedding_arguments, mgd_arguments, n_raw_features, k):

        # The ProbMeanBandit only sees the embedding features, thus feature_count = n_embedding_features
        self.n_raw_features = n_raw_features
        self.drop_prob = embedding_arguments.drop_probability
        self.drop_decay = embedding_arguments.drop_decay
        self.includes_linear_model = embedding_arguments.add_linear_model
        assert self.drop_decay < 1.
        if embedding_arguments.n_embedding_features > 0:
            self.n_embedding_features = embedding_arguments.n_embedding_features
        else:
            self.n_embedding_features = n_raw_features
        self.permanent_drop = embedding_arguments.permanent_drop
        self.min_updates_for_drop = embedding_arguments.min_updates_for_drop
        self.enable_drop = embedding_arguments.enable_drop
        self.min_embedding_features = embedding_arguments.min_embedding_features

        assert self.enable_drop or self.drop_prob == 0

        self.store_train_embeddings = self.drop_prob == 0
        self.new_features = np.ones(self.n_embedding_features)
        # method keeps track of embeddings so that we only change parts of embedding that have changed
        self.test_embeddings = None
        self.train_embeddings = None
        self.update_embedding = True

        self.setup_sampler()

        if self.includes_linear_model:
            ProbMeanBandit.__init__(self, mgd_arguments, self.n_embedding_features
                                    + n_raw_features, k, exploration=True)
        else:
            ProbMeanBandit.__init__(self, mgd_arguments, self.n_embedding_features, k,
                                    exploration=True)

        if self.enable_drop:
            self.add_message('N_DROP', 0)

    def clean(self):
        # explicitely clean out the embeddings
        del self.test_embeddings
        del self.train_embeddings
        del self.update_embedding
        gc.collect()
        ProbMeanBandit.clean(self)

    def setup_sampler(self):
        """
        Use this function if sampler needs to be setup before use.
        """
        pass

    def create_candidates(self):
        """
        Create candidates invokes the sample_candidates function and adds the current_best.
        """
        self.weights = np.zeros((self.feature_count, self.n_cand + 1))
        self.weights[:, :-1] = self.sample_candidates()
        self.weights[:, -1:] = self.current_best

    def sample_candidates(self):
        raise NotImplementedError('No Candidate sampling method implemented.')

    def generate_drop_weights(self, n_generate=None):
        assert not self.includes_linear_model or self.drop_prob == 0
        if n_generate is None:
            n_generate = self.n_generate
        vectors = np.random.randn(self.feature_count, n_generate)
        self.drop_mask = np.zeros((self.feature_count, n_generate))
        if self.feature_count > self.min_embedding_features:
            vectors, self.drop_mask = self.generate_drop_weights_rec(n_generate, vectors,
                    self.drop_mask, self.drop_prob)
        else:
            vectors, self.drop_mask = self.generate_drop_weights_rec(n_generate, vectors,
                    self.drop_mask, 0)
        weights = self.current_best[:, 0][:, None] + vectors * self.unit
        return weights

    def generate_drop_weights_rec(self, n_generate, vectors, full_drop_mask, drop_prob):
        """
        Vectors should be initialized with random.randn
        """
        if drop_prob == 0:
            vectors /= np.sum(vectors ** 2, axis=0) ** (1. / 2)
            return vectors, full_drop_mask
        else:
            drop_mask = np.random.rand(self.feature_count, n_generate) < drop_prob
            keep_mask = np.logical_not(drop_mask)

            if not np.any(keep_mask):
                return self.generate_drop_weights_rec(n_generate, vectors, full_drop_mask,
                        drop_prob * self.drop_decay)

            drop_v = np.tile(self.current_best[:, 0], (n_generate, 1)).T[drop_mask]
            vectors[drop_mask] = -drop_v / self.alpha / self.unit

            a = np.sum((vectors * drop_mask) ** 2, axis=0)
            b = np.sum((vectors * keep_mask) ** 2, axis=0)

            safe_bool = np.logical_and(b > 0, a <= 1)
            safe_ind = np.arange(n_generate)[safe_bool]
            squared = (1. - a[safe_ind]) / b[safe_ind]
            unsafe_ind = np.arange(n_generate)[np.logical_not(safe_bool)]
            n_unsafe = unsafe_ind.shape[0]

            # resample the setted weights
            vectors[:, unsafe_ind] = np.random.randn(self.feature_count, n_unsafe)
            # vectors[to_drop[:,unsafe_ind],unsafe_ind] = np.random.randn(n_drop,unsafe_ind.shape[0])

            norm_mask = np.empty((self.feature_count, safe_ind.shape[0]))
            # features to keep have to be normalized
            norm_mask[:, :] = squared[None, :] ** (1. / 2)
            # features to drop are not normalized
            norm_mask[drop_mask[:, safe_ind]] = 1
            vectors[:, safe_ind] *= norm_mask
            full_drop_mask[:, safe_ind] = drop_mask[:, safe_ind]

            if n_unsafe > 0:
                vectors[:, unsafe_ind], full_drop_mask[:, unsafe_ind] = \
                    self.generate_drop_weights_rec(n_unsafe, vectors[:, unsafe_ind],
                        full_drop_mask[:, unsafe_ind], drop_prob * self.drop_decay)

            return vectors, full_drop_mask

    def get_embedding(self, feat_matrix, output_matrix):
        raise NotImplementedError('No full embedding function implemented.')

    def compute_embedding(self, feature_matrix, index_range=None):
        if index_range is None:
            index_range = (0, feature_matrix.shape[1])
        if not self.includes_linear_model:
            embedding = np.empty((self.n_embedding_features, index_range[1] - index_range[0]))
            raw_features = feature_matrix[:, index_range[0]:index_range[1]]
            self.get_embedding(raw_features, embedding)
        else:
            embedding = np.empty((feature_matrix.shape[0] + self.n_embedding_features,
                                 index_range[1] - index_range[0]))
            embedding[:feature_matrix.shape[0], :] = feature_matrix[:, index_range[0]:index_range[1]]
            embedding[feature_matrix.shape[0]:, :] = self.get_embedding(embedding[:
                    feature_matrix.shape[0], :], embedding[feature_matrix.shape[0]:, :])
        return embedding

    def get_partial_embedding(self, feat_matrix, feat_indices):
        raise NotImplementedError('No partial embedding function implemented.')

    def get_test_rankings(self, test_feature_matrix, test_doc_ranges, inverted=True):

        if self.update_embedding:
            if self.test_embeddings is None:
                self.test_embeddings = self.compute_embedding(test_feature_matrix)
            else:
                update_indices = np.where(self.new_features)[0]
                self.test_embeddings[update_indices, :] = \
                    self.get_partial_embedding(test_feature_matrix, update_indices)
            assert self.test_embeddings.shape == (self.feature_count, test_doc_ranges[-1])
            self.update_embedding = False
            self.new_features[:] = 0

        # non interleaved rankings
        return ProbMeanBandit.get_test_rankings(self, self.test_embeddings, test_doc_ranges,
                                                inverted=inverted)

    def get_train_ranking(self, feature_matrix, doc_ranges, ranking_i):

        if not self.store_train_embeddings:
            embedding = self.compute_embedding(feature_matrix, doc_ranges[ranking_i:ranking_i + 2])
        else:
            if self.train_embeddings is None:
                self.train_embeddings = self.compute_embedding(feature_matrix)
            embedding = self.train_embeddings[:, doc_ranges[ranking_i]:doc_ranges[ranking_i + 1]]

        return ProbMeanBandit.get_train_ranking(self, embedding, [0, embedding.shape[1]], 0)

    def update_to_interaction(self, clicks):
        ProbMeanBandit.update_to_interaction(self, clicks)
        if self.enable_drop:
            self.drop_weights()

    def drop_weights(self):
        dropped = np.abs(self.current_best) < 10 ** -7
        if np.sum(dropped) > 0 and self.model_updates > self.min_updates_for_drop:
            drop_ind = np.where(dropped)[0]
            if self.feature_count - len(drop_ind) < self.min_embedding_features:
                np.random.shuffle(drop_ind)
                drop_ind = sorted(drop_ind[:self.feature_count - self.min_embedding_features])
            self.set_message('N_DROP', len(drop_ind))
            if self.permanent_drop:
                self.permanently_drop_weights(drop_ind)
            else:
                self.resample_weights(drop_ind)

    def resample_weights(self, emb_feature_ind):
        raise NotImplementedError('No resampling method implemented.')

    def permanently_drop_weights(self, emb_feature_ind):
        raise NotImplementedError('No permanent dropping of features implemented.')

max_stars_repo_name: Morgolt/BalancingSpeedQualityOLTR

max_issues_repo_name: Morgolt/BalancingSpeedQualityOLTR

max_forks_repo_name: Morgolt/BalancingSpeedQualityOLTR


text:
## @ingroup Components-Energy-Distributors
# Solar_Logic.py
#
# Created:  Jun 2014, E. Botero
# Modified: Jan 2016, T. MacDonald

# ----------------------------------------------------------------------
#  Imports
# ----------------------------------------------------------------------

# suave imports
import SUAVE

# package imports
import numpy as np
from SUAVE.Components.Energy.Energy_Component import Energy_Component

# ----------------------------------------------------------------------
#  Solar Logic Class
# ----------------------------------------------------------------------

## @ingroup Components-Energy-Distributors
class Solar_Logic(Energy_Component):
    """ The distributor is a component unique to a solar aircraft. This controls the flow of energy in to and from the battery.
        This includes the basic logic of the maximum power point tracker that modifies the voltage of the panels to
        extract maximum power.

        Assumptions:
        None

        Source:
        None
    """

    def __defaults__(self):
        """ This sets the default values.

            Assumptions:
            None

            Source:
            N/A

            Inputs:
            None

            Outputs:
            None

            Properties Used:
            None
        """

        self.MPPT_efficiency = 0.0
        self.system_voltage  = 0.0

    def voltage(self):
        """ The system voltage

            Assumptions:
                this function practically does nothing

            Source:
            N/A

            Inputs:
                self.system_voltage         [volts]

            Outputs:
                self.outputs.system_voltage [volts]

            Properties Used:
            None
        """
        volts = self.system_voltage

        self.outputs.system_voltage = volts

        return volts

    def logic(self,conditions,numerics):
        """ The power being sent to the battery

            Assumptions:
                the system voltage is constant
                the maximum power point is at a constant voltage

            Source:
            N/A

            Inputs:
                self.inputs:
                    powerin
                    pavionics
                    ppayload
                    volts_motor
                    currentesc
                numerics.time.integrate

            Outputs:
                self.outputs:
                    current
                    power_in
                    energy_transfer

            Properties Used:
                self.MPPT_efficiency

        """
        #Unpack
        pin         = self.inputs.powerin[:,0,None]
        pavionics   = self.inputs.pavionics
        ppayload    = self.inputs.ppayload
        volts_motor = self.inputs.volts_motor
        esccurrent  = self.inputs.currentesc
        volts       = self.voltage()
        I           = numerics.time.integrate

        pavail = pin*self.MPPT_efficiency

        plevel = pavail -pavionics -ppayload - volts_motor*esccurrent

        # Integrate the plevel over time to assess the energy consumption
        # or energy storage
        e = np.dot(I,plevel)

        # Send or take power out of the battery, Pack up
        self.outputs.current         = (plevel/volts)
        self.outputs.power_in        = plevel
        self.outputs.energy_transfer = e

        return


text:
#!/usr/bin/env python3

import numpy as np
import pandas as pd

import glob

import itertools
import os
import multiprocessing

from pyflash import FlashFile

from ulz import *

import numba

@numba.jit(nopython=True,target='cpu')
def powerspectrum(fekin):

    (Nx,Ny,Nz) = fekin.shape
    nws = np.zeros(int(np.sqrt(Nx**2+Ny**2+Nz**2)))
    pws = np.zeros(int(np.sqrt(Nx**2+Ny**2+Nz**2)))

    for i in range(Nx):
        for j in range(Ny):
            for k in range(Nz):
                I = i - Nx/2
                J = j - Ny/2
                K = k - Nz/2
                r = np.sqrt(I**2+J**2+K**2)
                pws[int(r)] += r**2 * fekin[i,j,k]
                nws[int(r)] += 1

    for i,n in enumerate(nws):
        if n > 0:
            pws[i] /= n

    return pws

def calcTimeEvolution(path):
    fpaths = sorted(glob.glob('%s/amr/flash_hdf5_chk_*' % path))
    reap = dict([(key,[]) for key in 'time step ekin power'.split()])

    for i,fp in enumerate(fpaths):
        flash = FlashFile(fp)

        # scalar
        time = flash.meta['real scalars']['time']
        step = flash.meta['integer scalars']['nstep']
        c_s  = flash.meta['real runtime']['c_ambient']
        rho0 = flash.meta['real runtime']['rho_ambient']
        Delta = flash.meta['spacing'][0]

        # 3d
        rho = flash.get_box('dens')
        vel = [flash.get_box('vel'+dim) for dim in 'x y z'.split()]

        vol  = rho.shape[0]*rho.shape[1]*rho.shape[2]
        ekin = rho/2 * (vel[0]**2 + vel[1]**2 + vel[2]**2) / rho0 / vol / c_s**2

        from numpy.fft import rfftn, fftshift
        fekin = fftshift(np.abs(rfftn(ekin)))**2

        pws = powerspectrum(fekin)

        # collect data we want
        reap['time'].append(time)
        reap['step'].append(step)
        reap['ekin'].append(ekin)
        reap['power'].append(pws)

        #if i % 20 == 0:
        #    print(i,end=" ",flush=True)

    return pd.DataFrame.from_dict(reap)

def gen_datafile(args):
    unit,grid,solv = args

    key     = gen_key(unit,grid,solv)
    srcpath = gen_amr_path(key) # source
    snkpath = gen_hdf5_path(key)  # sink

    df = calcTimeEvolution(srcpath)
    df.to_hdf(snkpath,'table_power',append=True)

    print(key,flush=True)

units = 'cgs'.split()
grids = [64]
solvs = '8w b3'.split()

params = itertools.product(units,grids,solvs)

pool = multiprocessing.Pool(6)
stat = pool.map(gen_datafile, params)
pool.close()

# for param in params:
#     print(gen_key(*param),end=' => ',flush=True)
#     gen_datafile(param)
#     print('')

max_stars_repo_name: jmark/turbubox

max_issues_repo_name: jmark/turbubox

max_forks_repo_name: jmark/turbubox

reason: import numpy,from numpy,import numba


text:
from __future__ import print_function
import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
from pyransac import features as ffs

# Generating an exponential function
start = 2
end = 10
num_points = 11

a = 1
b = 1
n = 3
x = np.linspace(a,10,11)
y = a*np.power(x,n)+b

# Select three points from the mathematical
# function
i = [0,3,5]
points = np.array([
                   pps for pps in zip(x[i],y[i])
                   ])

# Generate Exponential feature from the
# three points
exp = ffs.Exponential(points)

# Compare parameters
print("""
      Manual set parameters for exponential y=ax*n + b:\n
      a = {0}
      b = {1}
      k = {2}

      Detected feature parameters:

      a = {3}
      b = {4}
      k = {5}
      """.format(a,b,n,exp.a,exp.b,exp.k)
      )

# Print feature
xe,ye = exp.print_feature(num_points,start,end)
plt.plot(x,y)
plt.plot(x[i],y[i],'*')
plt.plot(xe,ye,'o-')
plt.show()

max_stars_repo_name: rubendibattista/tra-python-processer

max_issues_repo_name: rubendibattista/tra-python-processer

max_forks_repo_name: rubendibattista/tra-python-processer

reason: import numpy,import scipy


text:
# This file is part of QuTiP: Quantum Toolbox in Python.
#
#    Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
#    All rights reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions are
#    met:
#
#    1. Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#
#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
#       of its contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
"""Time-dependent Quantum Object (Qobj) class.
"""
__all__ = ['QobjEvo']

from qutip.qobj import Qobj
import qutip.settings as qset
from qutip.interpolate import Cubic_Spline
from scipy.interpolate import CubicSpline
from functools import partial, wraps
from types import FunctionType, BuiltinFunctionType
import numpy as np
from numbers import Number
from qutip.qobjevo_codegen import _compile_str_single, _compiled_coeffs
from qutip.cy.spmatfuncs import (cy_expect_rho_vec, cy_expect_psi, spmv, cy_spmm_tr)
from qutip.cy.cqobjevo import (CQobjCte, CQobjCteDense, CQobjEvoTd,
                                 CQobjEvoTdMatched, CQobjEvoTdDense)
from qutip.cy.cqobjevo_factor import (InterCoeffT, InterCoeffCte,
                                      InterpolateCoeff, StrCoeff)
import pickle
import sys
import scipy
import os

if qset.has_openmp:
    from qutip.cy.openmp.cqobjevo_omp import (CQobjCteOmp, CQobjEvoTdOmp,
                                              CQobjEvoTdMatchedOmp)

safePickle = False
if sys.platform == 'win32':
    safePickle = True

def proj(x):
    if np.isfinite(x):
        return (x)
    else:
        return np.inf+0j

str_env = {
    "sin": np.sin,
    "cos": np.cos,
    "tan": np.tan,
    "asin": np.arcsin,
    "acos": np.arccos,
    "atan": np.arctan,
    "pi": np.pi,
    "sinh": np.sinh,
    "cosh": np.cosh,
    "tanh": np.tanh,
    "asinh": np.arcsinh,
    "acosh": np.arccosh,
    "atanh": np.arctanh,
    "exp": np.exp,
    "log": np.log,
    "log10": np.log10,
    "erf": scipy.special.erf,
    "zerf": scipy.special.erf,
    "sqrt": np.sqrt,
    "real": np.real,
    "imag": np.imag,
    "conj": np.conj,
    "abs": np.abs,
    "norm": lambda x: np.abs(x)**2,
    "arg": np.angle,
    "proj": proj,
    "np": np,
    "spe": scipy.special}

class _file_list:
    """
    Contain temp a list .pyx to clean
    """
    def __init__(self):
        self.files = []

    def add(self, file_):
        self.files += [file_ + ".pyx"]

    def clean(self):
        for i, file_ in enumerate(self.files):
            try:
                os.remove(file_)
            except:
                pass
            if not os.path.isfile(file_):
                # Don't exist anymore
                del self.files[i]

    def __del__(self):
        self.clean()

coeff_files = _file_list()

class _StrWrapper:
    def __init__(self, code):
        self.code = "_out = " + code

    def __call__(self, t, args={}):
        env = {"t": t}
        env.update(args)
        exec(self.code, str_env, env)
        return env["_out"]

class _CubicSplineWrapper:
    # Using scipy's CubicSpline since Qutip's one
    # only accept linearly distributed tlist
    def __init__(self, tlist, coeff):
        self.coeff = coeff
        self.tlist = tlist
        self.func = CubicSpline(self.tlist, self.coeff)

    def __call__(self, t, args={}):
        return self.func([t])[0]

class _StateAsArgs:
    # old with state (f(t, psi, args)) to new (args["state"] = psi)
    def __init__(self, coeff_func):
        self.coeff_func = coeff_func

    def __call__(self, t, args={}):
        return self.coeff_func(t, args["_state_vec"], args)

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# object for each time dependent element of the QobjEvo
# qobj : the Qobj of element ([*Qobj*, f])
# get_coeff : a callable that take (t, args) and return the coeff at that t
# coeff : The coeff as a string, array or function as provided by the user.
# type : flag for the type of coeff
class EvoElement:
    def __init__(self, qobj, get_coeff, coeff, type):
        self.qobj = qobj
        self.get_coeff = get_coeff
        self.coeff = coeff
        self.type = type

    @classmethod
    def make(cls, list_):
        """self.qobj = list_[0]
        self.get_coeff = list_[1]
        self.coeff = list_[2]
        self.type = list_[3]"""
        return cls(*list_)

    def __getitem__(self, i):
        if i == 0:
            return self.qobj
        if i == 1:
            return self.get_coeff
        if i == 2:
            return self.coeff
        if i == 3:
            return self.type

class QobjEvo:
    """A class for representing time-dependent quantum objects,
    such as quantum operators and states.

    The QobjEvo class is a representation of time-dependent Qutip quantum
    objects (Qobj). This class implements math operations :
        +,- : QobjEvo, Qobj
        * : Qobj, C-number
        / : C-number
    and some common linear operator/state operations. The QobjEvo
    are constructed from a nested list of Qobj with their time-dependent
    coefficients. The time-dependent coefficients are either a funciton, a
    string or a numpy array.

    For function format, the function signature must be f(t, args).
    *Examples*
        def f1_t(t, args):
            return np.exp(-1j * t * args["w1"])

        def f2_t(t, args):
            return np.cos(t * args["w2"])

        H = QobjEvo([H0, [H1, f1_t], [H2, f2_t]], args={"w1":1., "w2":2.})

    For string based coeffients, the string must be a compilable python code
    resulting in a complex. The following symbols are defined:
        sin cos tan asin acos atan pi
        sinh cosh tanh asinh acosh atanh
        exp log log10 erf zerf sqrt
        real imag conj abs norm arg proj
        numpy as np, and scipy.special as spe.
    *Examples*
        H = QobjEvo([H0, [H1, 'exp(-1j*w1*t)'], [H2, 'cos(w2*t)']],
                    args={"w1":1.,"w2":2.})

    For numpy array format, the array must be an 1d of dtype float or complex.
    A list of times (float64) at which the coeffients must be given (tlist).
    The coeffients array must have the same len as the tlist.
    The time of the tlist do not need to be equidistant, but must be sorted.
    *Examples*
        tlist = np.logspace(-5,0,100)
        H = QobjEvo([H0, [H1, np.exp(-1j*tlist)], [H2, np.cos(2.*tlist)]],
                    tlist=tlist)

    args is a dict of (name:object). The name must be a valid variables string.
    Some solvers support arguments that update at each call:
    sesolve, mesolve, mcsolve:
        state can be obtained with:
            name+"=vec":Qobj  => args[name] == state as 1D np.ndarray
            name+"=mat":Qobj  => args[name] == state as 2D np.ndarray
            name+"=Qobj":Qobj => args[name] == state as Qobj

            This Qobj is the initial value.

        expectation values:
            name+"=expect":O (Qobj/QobjEvo)  => args[name] == expect(O, state)
            expect is <phi|O|psi> or tr(state * O) depending on state dimensions

    mcsolve:
        collapse can be obtained with:
            name+"=collapse":list => args[name] == list of collapse
            each collapse will be appended to the list as (time, which c_ops)

    Mixing the formats is possible, but not recommended.
    Mixing tlist will cause problem.

    Parameters
    ----------
    QobjEvo(Q_object=[], args={}, tlist=None)

    Q_object : array_like
        Data for vector/matrix representation of the quantum object.

    args : dictionary that contain the arguments for

    tlist : array_like
        List of times at which the numpy-array coefficients are applied. Times
        must be equidistant and start from 0.

    Attributes
    ----------
    cte : Qobj
        Constant part of the QobjEvo

    ops : list
        List of Qobj and the coefficients.
        [(Qobj, coefficient as a function, original coefficient,
            type, local arguments ), ... ]
        type :
            1: function
            2: string
            3: np.array
            4: Cubic_Spline

    args : map
        arguments of the coefficients

    tlist : array_like
        List of times at which the numpy-array coefficients are applied.

    compiled : int
        Has the cython version of the QobjEvo been created

    compiled_qobjevo : cy_qobj (CQobjCte or CQobjEvoTd)
        Cython version of the QobjEvo

    dummy_cte : bool
        is self.cte a dummy Qobj

    const : bool
        Indicates if quantum object is Constant

    type : int
        information about the type of coefficient
            "string", "func", "array",
            "spline", "mixed_callable", "mixed_compilable"

    num_obj : int
        number of Qobj in the QobjEvo : len(ops) + (1 if not dummy_cte)

    Methods
    -------
    copy() :
        Create copy of Qobj

    arguments(new_args):
        Update the args of the object

    Math:
        +/- QobjEvo, Qobj, scalar:
            Addition is possible between QobjEvo and with Qobj or scalar
        -:
            Negation operator
        * Qobj, scalar:
            Product is possible with Qobj or scalar
        / scalar:
            It is possible to divide by scalar only
    conj()
        Return the conjugate of quantum object.

    dag()
        Return the adjoint (dagger) of quantum object.

    trans()
        Return the transpose of quantum object.

    norm()
        Return self.dag() * self.
        Only possible if num_obj == 1

    permute(order)
        Returns composite qobj with indices reordered.

    ptrace(sel)
        Returns quantum object for selected dimensions after performing
        partial trace.

    apply(f, *args, **kw_args)
        Apply the function f to every Qobj. f(Qobj) -> Qobj
        Return a modified QobjEvo and let the original one untouched

    apply_decorator(decorator, *args, str_mod=None,
                    inplace_np=False, **kw_args):
        Apply the decorator to each function of the ops.
        The *args and **kw_args are passed to the decorator.
        new_coeff_function = decorator(coeff_function, *args, **kw_args)
        str_mod : list of 2 elements
            replace the string : str_mod[0] + original_string + str_mod[1]
            *exemple: str_mod = ["exp(",")"]
        inplace_np:
            Change the numpy array instead of applying the decorator to the
            function reading the array. Some decorators create incorrect array.
            Transformations f'(t) = f(g(t)) create a missmatch between the
            array and the associated time list.

    tidyup(atol=1e-12)
        Removes small elements from quantum object.

    compress():
        Merge ops which are based on the same quantum object and coeff type.

    compile(code=False, matched=False, dense=False, omp=0):
        Create the associated cython object for faster usage.
        code: return the code generated for compilation of the strings.
        matched: the compiled object use sparse matrix with matching indices.
                    (experimental, no real advantage)
        dense: the compiled object use dense matrix.
        omp: (int) number of thread: the compiled object use spmvpy_openmp.

    __call__(t, data=False, state=None, args={}):
        Return the Qobj at time t.
        *Faster after compilation

    mul_mat(t, mat):
        Product of this at t time with the dense matrix mat.
        *Faster after compilation

    mul_vec(t, psi):
        Apply the quantum object (if operator, no check) to psi.
        More generaly, return the product of the object at t with psi.
        *Faster after compilation

    expect(t, psi, herm=False):
        Calculates the expectation value for the quantum object (if operator,
            no check) and state psi.
        Return only the real part if herm.
        *Faster after compilation

    to_list():
        Return the time-dependent quantum object as a list
    """

    def __init__(self, Q_object=[], args={}, tlist=None, copy=True):
        if isinstance(Q_object, QobjEvo):
            if copy:
                self._inplace_copy(Q_object)
            else:
                self.__dict__ = Q_object.__dict__
            if args:
                self.arguments(args)
            return

        self.const = False
        self.dummy_cte = False
        self.args = args.copy() if copy else args
        self.dynamics_args = []
        self.cte = None
        self.tlist = tlist
        self.compiled = ""
        self.compiled_qobjevo = None
        self.compiled_ptr = None
        self.coeff_get = None
        self.type = "none"
        self.omp = 0

        if isinstance(Q_object, list) and len(Q_object) == 2:
            if isinstance(Q_object[0], Qobj) and not isinstance(Q_object[1],
                                                                (Qobj, list)):
                # The format is [Qobj, f/str]
                Q_object = [Q_object]

        op_type = self._td_format_check_single(Q_object, tlist)
        self.ops = []

        if isinstance(op_type, int):
            if op_type == 0:
                self.cte = Q_object
                self.const = True
                self.type = "cte"
            elif op_type == 1:
                raise Exception("The Qobj must not already be a function")
            elif op_type == -1:
                pass
        else:
            op_type_count = [0, 0, 0, 0]
            for type_, op in zip(op_type, Q_object):
                if type_ == 0:
                    if self.cte is None:
                        self.cte = op
                    else:
                        self.cte += op
                elif type_ == 1:
                    op_type_count[0] += 1
                    self.ops.append(EvoElement(op[0], op[1], op[1], "func"))
                elif type_ == 2:
                    op_type_count[1] += 1
                    self.ops.append(EvoElement(op[0], _StrWrapper(op[1]),
                                    op[1], "string"))
                elif type_ == 3:
                    op_type_count[2] += 1
                    self.ops.append(EvoElement(op[0], _CubicSplineWrapper(tlist, op[1]),
                                     op[1].copy(), "array"))
                elif type_ == 4:
                    op_type_count[3] += 1
                    self.ops.append(EvoElement(op[0], op[1], op[1], "spline"))

            nops = sum(op_type_count)
            if op_type_count[0] == nops:
                self.type = "func"
            elif op_type_count[1] == nops:
                self.type = "string"
            elif op_type_count[2] == nops:
                self.type = "array"
            elif op_type_count[3] == nops:
                self.type = "spline"
            elif op_type_count[0]:
                self.type = "mixed_callable"
            else:
                self.type = "mixed_compilable"

            try:
                if not self.cte:
                    self.cte = self.ops[0].qobj
                    # test is all qobj are compatible (shape, dims)
                    for op in self.ops[1:]:
                        self.cte += op.qobj
                    self.cte *= 0.
                    self.dummy_cte = True
                else:
                    cte_copy = self.cte.copy()
                    # test is all qobj are compatible (shape, dims)
                    for op in self.ops:
                        cte_copy += op.qobj
            except Exception as e:
                raise TypeError("Qobj not compatible.") from e

            if not self.ops:
                self.const = True
        self.num_obj = (len(self.ops) if self.dummy_cte else len(self.ops) + 1)
        self._args_checks()

    def _td_format_check_single(self, Q_object, tlist=None):
        op_type = []

        if isinstance(Q_object, Qobj):
            op_type = 0
        elif isinstance(Q_object, (FunctionType,
                                   BuiltinFunctionType, partial)):
            op_type = 1
        elif isinstance(Q_object, list):
            if (len(Q_object) == 0):
                op_type = -1
            for op_k in Q_object:
                if isinstance(op_k, Qobj):
                    op_type.append(0)
                elif isinstance(op_k, list):
                    if not isinstance(op_k[0], Qobj):
                        raise TypeError("Incorrect Q_object specification")
                    elif len(op_k) == 2:
                        if isinstance(op_k[1], Cubic_Spline):
                            op_type.append(4)
                        elif callable(op_k[1]):
                            op_type.append(1)
                        elif isinstance(op_k[1], str):
                            op_type.append(2)
                        elif isinstance(op_k[1], np.ndarray):
                            if not isinstance(tlist, np.ndarray) or not \
                                        len(op_k[1]) == len(tlist):
                                raise TypeError("Time list does not match")
                            op_type.append(3)
                        else:
                            raise TypeError("Incorrect Q_object specification")
                    else:
                        raise TypeError("Incorrect Q_object specification")
        else:
            raise TypeError("Incorrect Q_object specification")
        return op_type

    def _args_checks(self, update=False):
        to_remove = []
        to_add = {}
        for key in self.args:
            if "=" in key:
                name, what = key.split("=")
                if what in ["Qobj", "vec", "mat"]:
                    # state first, expect last
                    if not update:
                        self.dynamics_args = [(name, what, None)] + self.dynamics_args
                        if name not in self.args:
                            if isinstance(self.args[key], Qobj):
                                if what == "Qobj":
                                    to_add[name] = self.args[key]
                                elif what == "mat":
                                    to_add[name] = self.args[key].full()
                                else:
                                    to_add[name] = self.args[key].full().ravel("F")
                            else:
                                if what == "Qobj":
                                    to_add[name] = Qobj(dims=[self.cte.dims[1],[1]])
                                elif what == "mat":
                                    to_add[name] = np.zeros((self.cte.shape[1],1))
                                else:
                                    to_add[name] = np.zeros((self.cte.shape[1]))

                elif what == "expect":
                    if isinstance(self.args[key], QobjEvo):
                        expect_op = self.args[key]
                    else:
                        expect_op = QobjEvo(self.args[key], copy=False)
                    if update:
                        for ops in self.dynamics_args:
                            if ops[0] == name:
                                ops = (name, what, expect_op)
                    else:
                        self.dynamics_args += [(name, what, expect_op)]
                        if name not in self.args:
                            to_add[name] = 0.
                else:
                    raise Exception("Could not understand dynamics args: " +
                                    what + "\nSupported dynamics args: "
                                    "Qobj, csr, vec, mat, expect")
                to_remove.append(key)

        for key in to_remove:
            del self.args[key]

        self.args.update(to_add)

    def _check_old_with_state(self):
        add_vec = False
        for op in self.ops:
            if op.type == "func":
                try:
                    op.get_coeff(0., self.args)
                except TypeError as e:
                    nfunc = _StateAsArgs(self.coeff)
                    op = EvoElement((op.qobj, nfunc, nfunc, "func"))
                    add_vec = True
        if add_vec:
            self.dynamics_args += [("_state_vec", "vec", None)]

    def __del__(self):
        # sometime not called
        coeff_files.clean()

    def __call__(self, t, data=False, state=None, args={}):
        try:
            t = float(t)
        except Exception as e:
            raise TypeError("t should be a real scalar.") from e

        if state is not None:
            self._dynamics_args_update(t, state)

        if args:
            if not isinstance(args, dict):
                raise TypeError("The new args must be in a dict")
            old_args = self.args.copy()
            old_compiled = self.compiled
            self.compiled = False
            self.args.update(args)
            op_t = self.__call__(t, data=data)
            self.args = old_args
            self.compiled = old_compiled
        elif self.const:
            if data:
                op_t = self.cte.data.copy()
            else:
                op_t = self.cte.copy()
        elif self.compiled and self.compiled.split()[0] != "dense":
            op_t = self.compiled_qobjevo.call(t, data)
        elif data:
            op_t = self.cte.data.copy()
            for part in self.ops:
                op_t += part.qobj.data * part.get_coeff(t, self.args)
        else:
            op_t = self.cte.copy()
            for part in self.ops:
                op_t += part.qobj * part.get_coeff(t, self.args)

        return op_t

    def _dynamics_args_update(self, t, state):
        if isinstance(state, Qobj):
            for name, what, op in self.dynamics_args:
                if what == "vec":
                    self.args[name] = state.full().ravel("F")
                elif what == "mat":
                    self.args[name] = state.full()
                elif what == "Qobj":
                    self.args[name] = state
                elif what == "expect":
                    self.args[name] = op.expect(t, state)

        elif isinstance(state, np.ndarray) and state.ndim == 1:
            s1 = self.cte.shape[1]
            for name, what, op in self.dynamics_args:
                if what == "vec":
                    self.args[name] = state
                elif what == "expect":
                    self.args[name] = op.expect(t, state)
                elif state.shape[0] == s1 and self.cte.issuper:
                    new_l = int(np.sqrt(s1))
                    mat = state.reshape((new_l, new_l), order="F")
                    if what == "mat":
                        self.args[name] = mat
                    elif what == "Qobj":
                        self.args[name] = Qobj(mat, dims=self.cte.dims[1])
                elif state.shape[0] == s1:
                    mat = state.reshape((-1,1))
                    if what == "mat":
                        self.args[name] = mat
                    elif what == "Qobj":
                        self.args[name] = Qobj(mat, dims=[self.cte.dims[1],[1]])
                elif state.shape[0] == s1*s1:
                    new_l = int(np.sqrt(s1))
                    mat = state.reshape((new_l, new_l), order="F")
                    if what == "mat":
                        self.args[name] = mat
                    elif what == "Qobj":
                        self.args[name] = Qobj(mat, dims=[self.cte.dims[1], self.cte.dims[1]])

        elif isinstance(state, np.ndarray) and state.ndim == 2:
            s1 = self.cte.shape[1]
            new_l = int(np.sqrt(s1))
            for name, what, op in self.dynamics_args:
                if what == "vec":
                    self.args[name] = state.ravel("F")
                elif what == "mat":
                    self.args[name] = state
                elif what == "expect":
                    self.args[name] = op.expect(t, state)
                elif state.shape[1] == 1:
                    self.args[name] = Qobj(state, dims=[self.cte.dims[1],[1]])
                elif state.shape[1] == s1:
                    self.args[name] = Qobj(state, dims=self.cte.dims)
                else:
                    self.args[name] = Qobj(state)

        else:
            raise TypeError("state must be a Qobj or np.ndarray")

    def copy(self):
        new = QobjEvo(self.cte.copy())
        new.const = self.const
        new.args = self.args.copy()
        new.dynamics_args = self.dynamics_args.copy()
        new.tlist = self.tlist
        new.dummy_cte = self.dummy_cte
        new.num_obj = self.num_obj
        new.type = self.type
        new.compiled = False
        new.compiled_qobjevo = None
        new.compiled_ptr = None
        new.coeff_get = None
        new.coeff_files = []

        for op in self.ops:
            if op.type == "array":
                new_coeff = op.coeff.copy()
            else:
                new_coeff = op.coeff
            new.ops.append(EvoElement(op.qobj.copy(), op.get_coeff,
                                      new_coeff, op.type))

        return new

    def _inplace_copy(self, other):
        self.cte = other.cte
        self.const = other.const
        self.args = other.args.copy()
        self.dynamics_args = other.dynamics_args
        self.tlist = other.tlist
        self.dummy_cte = other.dummy_cte
        self.num_obj = other.num_obj
        self.type = other.type
        self.compiled = ""
        self.compiled_qobjevo = None
        self.compiled_ptr = None
        self.coeff_get = None
        self.ops = []

        for op in other.ops:
            if op.type == "array":
                new_coeff = op.coeff.copy()
            else:
                new_coeff = op.coeff
            self.ops.append(EvoElement(op.qobj.copy(), op.get_coeff,
                                       new_coeff, op.type))

    def arguments(self, args):
        if not isinstance(args, dict):
            raise TypeError("The new args must be in a dict")
        self.args.update(args)
        self._args_checks(True)
        if self.compiled and self.compiled.split()[2] is not "cte":
            if isinstance(self.coeff_get, StrCoeff):
                self.coeff_get.set_args(self.args)
                self.coeff_get._set_dyn_args(self.dynamics_args)
            elif isinstance(self.coeff_get, _UnitedFuncCaller):
                self.coeff_get.set_args(self.args, self.dynamics_args)
            else:
                pass

    def to_list(self):
        list_qobj = []
        if not self.dummy_cte:
            list_qobj.append(self.cte)
        for op in self.ops:
            list_qobj.append([op.qobj, op.coeff])
        return list_qobj

    # Math function
    def __add__(self, other):
        res = self.copy()
        res += other
        return res

    def __radd__(self, other):
        res = self.copy()
        res += other
        return res

    def __iadd__(self, other):
        if isinstance(other, QobjEvo):
            self.cte += other.cte
            l = len(self.ops)
            for op in other.ops:
                if op.type == "array":
                    new_coeff = op.coeff.copy()
                else:
                    new_coeff = op.coeff
                self.ops.append(EvoElement(op.qobj.copy(), op.get_coeff,
                                           new_coeff, op.type))
                l += 1
            self.args.update(**other.args)
            self.dynamics_args += other.dynamics_args
            self.const = self.const and other.const
            self.dummy_cte = self.dummy_cte and other.dummy_cte
            if self.type != other.type:
                if self.type in ["func", "mixed_callable"] or \
                        other.type in ["func", "mixed_callable"]:
                    self.type = "mixed_callable"
                else:
                    self.type = "mixed_compilable"
            self.compiled = ""
            self.compiled_qobjevo = None
            self.compiled_ptr = None
            self.coeff_get = None

            if self.tlist is None:
                self.tlist = other.tlist
            else:
                if other.tlist is None:
                    pass
                elif len(other.tlist) != len(self.tlist) or \
                        other.tlist[-1] != self.tlist[-1]:
                    raise Exception("tlist are not compatible")
        else:
            self.cte += other
            self.dummy_cte = False

        self.num_obj = (len(self.ops) if self.dummy_cte else len(self.ops) + 1)
        self._reset_type()
        return self

    def __sub__(self, other):
        res = self.copy()
        res -= other
        return res

    def __rsub__(self, other):
        res = -self.copy()
        res += other
        return res

    def __isub__(self, other):
        self += (-other)
        return self

    def __mul__(self, other):
        res = self.copy()
        res *= other
        return res

    def __rmul__(self, other):
        res = self.copy()
        if isinstance(other, Qobj):
            res.cte = other * res.cte
            for op in res.ops:
                op.qobj = other * op.qobj
            return res
        else:
            res *= other
            return res

    def __imul__(self, other):
        if isinstance(other, Qobj) or isinstance(other, Number):
            self.cte *= other
            for op in self.ops:
                op.qobj *= other
        elif isinstance(other, QobjEvo):
            if other.const:
                self.cte *= other.cte
                for op in self.ops:
                    op.qobj *= other.cte
            elif self.const:
                cte = self.cte.copy()
                self = other.copy()
                self.cte = cte * self.cte
                for op in self.ops:
                    op.qobj = cte*op.qobj
            else:
                cte = self.cte.copy()
                self.cte *= other.cte
                new_terms = []
                old_ops = self.ops
                if not other.dummy_cte:
                    for op in old_ops:
                        new_terms.append(self._ops_mul_cte(op, other.cte, "R"))
                if not self.dummy_cte:
                    for op in other.ops:
                        new_terms.append(self._ops_mul_cte(op, cte, "L"))

                for op_left in old_ops:
                    for op_right in other.ops:
                        new_terms.append(self._ops_mul_(op_left,
                                                        op_right))
                self.ops = new_terms
                self.args.update(other.args)
                self.dynamics_args += other.dynamics_args
                self.dummy_cte = self.dummy_cte and other.dummy_cte
                self.num_obj = (len(self.ops) if
                              self.dummy_cte else len(self.ops) + 1)
            self._reset_type()

        else:
            raise TypeError("QobjEvo can only be multiplied"
                            " with QobjEvo, Qobj or numbers")
        return self

    def __div__(self, other):
        if isinstance(other, (int, float, complex,
                              np.integer, np.floating, np.complexfloating)):
            res = self.copy()
            res *= other**(-1)
            return res
        else:
            raise TypeError('Incompatible object for division')

    def __idiv__(self, other):
        if isinstance(other, (int, float, complex,
                              np.integer, np.floating, np.complexfloating)):
            self *= other**(-1)
        else:
            raise TypeError('Incompatible object for division')
        return self

    def __truediv__(self, other):
        return self.__div__(other)

    def __neg__(self):
        res = self.copy()
        res.cte = -res.cte
        for op in res.ops:
            op.qobj = -op.qobj
        return res

    def _ops_mul_(self, opL, opR):
        new_f = _Prod(opL.get_coeff, opR.get_coeff)
        new_op = [opL.qobj*opR.qobj, new_f, None, 0]
        if opL.type == opR.type and opL.type == "string":
            new_op[2] = "(" + opL.coeff + ") * (" + opR.coeff + ")"
            new_op[3] = "string"
        elif opL[3] == opR[3] and opL[3] == "array":
            new_op[2] = opL[2]*opR[2]
            new_op[3] = "array"
        else:
            new_op[2] = new_f
            new_op[3] = "func"
            if self.type not in ["func", "mixed_callable"]:
                self.type = "mixed_callable"
        return EvoElement.make(new_op)

    def _ops_mul_cte(self, op, cte, side):
        new_op = [None, op.get_coeff, op.coeff, op.type]
        if side == "R":
            new_op[0] = op.qobj * cte
        if side == "L":
            new_op[0] = cte * op.qobj
        return EvoElement.make(new_op)

    # Transformations
    def trans(self):
        res = self.copy()
        res.cte = res.cte.trans()
        for op in res.ops:
            op.qobj = op.qobj.trans()
        return res

    def conj(self):
        res = self.copy()
        res.cte = res.cte.conj()
        for op in res.ops:
            op.qobj = op.qobj.conj()
        res._f_conj()
        return res

    def dag(self):
        res = self.copy()
        res.cte = res.cte.dag()
        for op in res.ops:
            op.qobj = op.qobj.dag()
        res._f_conj()
        return res

    def _cdc(self):
        """return a.dag * a """
        if not self.num_obj == 1:
            res = self.dag()
            res *= self
        else:
            res = self.copy()
            res.cte = res.cte.dag() * res.cte
            for op in res.ops:
                op.qobj = op.qobj.dag() * op.qobj
            res._f_norm2()
        return res

    # Unitary function of Qobj
    def tidyup(self, atol=1e-12):
        self.cte = self.cte.tidyup(atol)
        for op in self.ops:
            op.qobj = op.qobj.tidyup(atol)
        return self

    def _compress_make_set(self):
        sets = []
        callable_flags = ["func", "spline"]
        for i, op1 in enumerate(self.ops):
            already_matched = False
            for _set in sets:
                already_matched = already_matched or i in _set
            if not already_matched:
                this_set = [i]
                for j, op2 in enumerate(self.ops[i+1:]):
                    if op1.qobj == op2.qobj:
                        same_flag = op1.type == op2.type
                        callable_1 = op1.type in callable_flags
                        callable_2 = op2.type in callable_flags
                        if (same_flag or (callable_1 and callable_2)):
                            this_set.append(j+i+1)
                sets.append(this_set)

        fsets = []
        for i, op1 in enumerate(self.ops):
            already_matched = False
            for _set in fsets:
                already_matched = already_matched or i in _set
            if not already_matched:
                this_set = [i]
                for j, op2 in enumerate(self.ops[i+1:]):
                    if op1.type != op2.type:
                        pass
                    elif op1.type is "array":
                        if np.allclose(op1.coeff, op2.coeff):
                            this_set.append(j+i+1)
                    else:
                        if op1.coeff is op2.coeff:
                            this_set.append(j+i+1)
                fsets.append(this_set)
        return sets, fsets

    def _compress_merge_qobj(self, sets):
        callable_flags = ["func", "spline"]
        new_ops = []
        for _set in sets:
            if len(_set) == 1:
                new_ops.append(self.ops[_set[0]])

            elif self.ops[_set[0]].type in callable_flags:
                new_op = [self.ops[_set[0]].qobj, None, None, "func"]
                fs = []
                for i in _set:
                    fs += [self.ops[i].get_coeff]
                new_op[1] = _Add(fs)
                new_op[2] = new_op[1]
                new_ops.append(EvoElement.make(new_op))

            elif self.ops[_set[0]].type is "string":
                new_op = [self.ops[_set[0]].qobj, None, None, "string"]
                new_str = "(" + self.ops[_set[0]].coeff + ")"
                for i in _set[1:]:
                    new_str += " + (" + self.ops[i].coeff + ")"
                new_op[1] = _StrWrapper(new_str)
                new_op[2] = new_str
                new_ops.append(EvoElement.make(new_op))

            elif self.ops[_set[0]].type is "array":
                new_op = [self.ops[_set[0]].qobj, None, None, "array"]
                new_array = (self.ops[_set[0]].coeff).copy()
                for i in _set[1:]:
                    new_array += self.ops[i].coeff
                new_op[2] = new_array
                new_op[1] = _CubicSplineWrapper(self.tlist, new_array)
                new_ops.append(EvoElement.make(new_op))

        self.ops = new_ops

    def _compress_merge_func(self, fsets):
        new_ops = []
        for _set in fsets:
            base = self.ops[_set[0]]
            new_op = [None, base.get_coeff, base.coeff, base.type]
            if len(_set) == 1:
                new_op[0] = base.qobj
            else:
                new_op[0] = base.qobj.copy()
                for i in _set[1:]:
                    new_op[0] += self.ops[i].qobj
            new_ops.append(EvoElement.make(new_op))
        self.ops = new_ops

    def compress(self):
        self.tidyup()
        sets, fsets = self._compress_make_set()
        N_sets = len(sets)
        N_fsets = len(fsets)
        num_ops = len(self.ops)

        if N_sets < num_ops and N_fsets < num_ops:
            # Both could be better
            self.compiled = ""
            self.compiled_qobjevo = None
            self.coeff_get = None
            if N_sets < N_fsets:
                self._compress_merge_qobj(sets)
            else:
                self._compress_merge_func(fsets)
            sets, fsets = self._compress_make_set()
            N_sets = len(sets)
            N_fsets = len(fsets)
            num_ops = len(self.ops)

        if N_sets < num_ops:
            self.compiled = ""
            self.compiled_qobjevo = None
            self.coeff_get = None
            self._compress_merge_qobj(sets)
        elif N_fsets < num_ops:
            self.compiled = ""
            self.compiled_qobjevo = None
            self.coeff_get = None
            self._compress_merge_func(fsets)
        self._reset_type()

    def _reset_type(self):
        op_type_count = [0, 0, 0, 0]
        for op in self.ops:
            if op.type == "func":
                op_type_count[0] += 1
            elif op.type == "string":
                op_type_count[1] += 1
            elif op.type == "array":
                op_type_count[2] += 1
            elif op.type == "spline":
                op_type_count[3] += 1

        nops = sum(op_type_count)
        if op_type_count[0] == nops:
            self.type = "func"
        elif op_type_count[1] == nops:
            self.type = "string"
        elif op_type_count[2] == nops:
            self.type = "array"
        elif op_type_count[3] == nops:
            self.type = "spline"
        elif op_type_count[0]:
            self.type = "mixed_callable"
        else:
            self.type = "mixed_compilable"

        self.num_obj = (len(self.ops) if self.dummy_cte else len(self.ops) + 1)

    def permute(self, order):
        res = self.copy()
        res.cte = res.cte.permute(order)
        for op in res.ops:
            op.qobj = op.qobj.permute(order)
        return res

    def ptrace(self, sel):
        res = self.copy()
        res.cte = res.cte.ptrace(sel)
        for op in res.ops:
            op.qobj = op.qobj.ptrace(sel)
        return res

    # function to apply custom transformations
    def apply(self, function, *args, **kw_args):
        self.compiled = ""
        res = self.copy()
        cte_res = function(res.cte, *args, **kw_args)
        if not isinstance(cte_res, Qobj):
            raise TypeError("The function must return a Qobj")
        res.cte = cte_res
        for op in res.ops:
            op.qobj = function(op.qobj, *args, **kw_args)
        return res

    def apply_decorator(self, function, *args, **kw_args):
        if "str_mod" in kw_args:
            str_mod = kw_args["str_mod"]
            del kw_args["str_mod"]
        else:
            str_mod = None
        if "inplace_np" in kw_args:
            inplace_np = kw_args["inplace_np"]
            del kw_args["inplace_np"]
        else:
            inplace_np = None
        res = self.copy()
        for op in res.ops:
            op.get_coeff = function(op.get_coeff, *args, **kw_args)
            if op.type == ["func", "spline"]:
                op.coeff = op.get_coeff
                op.type = "func"
            elif op.type == "string":
                if str_mod is None:
                    op.coeff = op.get_coeff
                    op.type = "func"
                else:
                    op.coeff = str_mod[0] + op.coeff + str_mod[1]
            elif op.type == "array":
                if inplace_np:
                    # keep the original function, change the array
                    def f(a):
                        return a
                    ff = function(f, *args, **kw_args)
                    for i, v in enumerate(op.coeff):
                        op.coeff[i] = ff(v)
                    op.get_coeff = _CubicSplineWrapper(self.tlist, op.coeff)
                else:
                    op.coeff = op.get_coeff
                    op.type = "func"
        if self.type == "string" and str_mod is None:
            res.type = "mixed_callable"
        elif self.type == "array" and not inplace_np:
            res.type = "mixed_callable"
        elif self.type == "spline":
            res.type = "mixed_callable"
        elif self.type == "mixed_compilable":
            for op in res.ops:
                if op.type == "func":
                    res.type = "mixed_callable"
        return res

    def _f_norm2(self):
        new_ops = []
        for op in self.ops:
            new_op = [op.qobj, None, None, op.type]
            if op.type == "func":
                new_op[1] = _Norm2(op.get_coeff)
                new_op[2] = new_op[1]
            elif op.type == "string":
                new_op[2] = "norm(" + op.coeff + ")"
                new_op[1] = _StrWrapper(new_op[2])
            elif op.type == "array":
                new_op[2] = np.abs(op.coeff)**2
                new_op[1] = _CubicSplineWrapper(self.tlist, new_op[2])
            elif op.type == "spline":
                new_op[1] = _Norm2(op.get_coeff)
                new_op[2] = new_op[1]
                new_op[3] = "func"
                self.type = "mixed_callable"
            new_ops.append(EvoElement.make(new_op))
        self.ops = new_ops
        return self

    def _f_conj(self):
        new_ops = []
        for op in self.ops:
            new_op = [op.qobj, None, None, op.type]
            if op.type == "func":
                new_op[1] = _Conj(op.get_coeff)
                new_op[2] = new_op[1]
            elif op.type == "string":
                new_op[2] = "conj(" + op.coeff + ")"
                new_op[1] = _StrWrapper(new_op[2])
            elif op.type == "array":
                new_op[2] = np.conj(op.coeff)
                new_op[1] = _CubicSplineWrapper(self.tlist, new_op[2])
            elif op.type == "spline":
                new_op[1] = _Conj(op.get_coeff)
                new_op[2] = new_op[1]
                new_op[3] = "func"
                self.type = "mixed_callable"
            new_ops.append(EvoElement.make(new_op))
        self.ops = new_ops
        return self

    def expect(self, t, state, herm=0):
        if not isinstance(t, (int, float)):
            raise TypeError("The time need to be a real scalar")
        if isinstance(state, Qobj):
            if self.cte.dims[1] == state.dims[0]:
                vec = state.full().ravel("F")
            elif self.cte.dims[1] == state.dims:
                vec = state.full().ravel("F")
            else:
                raise Exception("Dimensions do not fit")
        elif isinstance(state, np.ndarray):
            vec = state.reshape((-1))
        else:
            raise TypeError("The vector must be an array or Qobj")

        if vec.shape[0] == self.cte.shape[1]:
            if self.compiled:
                exp = self.compiled_qobjevo.expect(t, vec)
            elif self.cte.issuper:
                self._dynamics_args_update(t, state)
                exp = cy_expect_rho_vec(self.__call__(t, data=True), vec, 0)
            else:
                self._dynamics_args_update(t, state)
                exp = cy_expect_psi(self.__call__(t, data=True), vec, 0)
        elif vec.shape[0] == self.cte.shape[1]**2:
            if self.compiled:
                exp = self.compiled_qobjevo.overlapse(t, vec)
            else:
                self._dynamics_args_update(t, state)
                exp = (self.__call__(t, data=True) *
                       vec.reshape((self.cte.shape[1],
                                    self.cte.shape[1]))).trace()
        else:
            raise Exception("The shapes do not match")

        if herm:
            return exp.real
        else:
            return exp

    def mul_vec(self, t, vec):
        was_Qobj = False
        if not isinstance(t, (int, float)):
            raise TypeError("the time need to be a real scalar")
        if isinstance(vec, Qobj):
            if self.cte.dims[1] != vec.dims[0]:
                raise Exception("Dimensions do not fit")
            was_Qobj = True
            dims = vec.dims
            vec = vec.full().ravel()
        elif not isinstance(vec, np.ndarray):
            raise TypeError("The vector must be an array or Qobj")
        if vec.ndim != 1:
            raise Exception("The vector must be 1d")
        if vec.shape[0] != self.cte.shape[1]:
            raise Exception("The length do not match")

        if self.compiled:
            out = self.compiled_qobjevo.mul_vec(t, vec)
        else:
            self._dynamics_args_update(t, vec)
            out = spmv(self.__call__(t, data=True), vec)

        if was_Qobj:
            return Qobj(out, dims=dims)
        else:
            return out

    def mul_mat(self, t, mat):
        was_Qobj = False
        if not isinstance(t, (int, float)):
            raise TypeError("the time need to be a real scalar")
        if isinstance(mat, Qobj):
            if self.cte.dims[1] != mat.dims[0]:
                raise Exception("Dimensions do not fit")
            was_Qobj = True
            dims = mat.dims
            mat = mat.full()
        if not isinstance(mat, np.ndarray):
            raise TypeError("The vector must be an array or Qobj")
        if mat.ndim != 2:
            raise Exception("The matrice must be 2d")
        if mat.shape[0] != self.cte.shape[1]:
            raise Exception("The length do not match")

        if self.compiled:
            out = self.compiled_qobjevo.mul_mat(t, mat)
        else:
            self._dynamics_args_update(t, mat)
            out = self.__call__(t, data=True) * mat

        if was_Qobj:
            return Qobj(out, dims=dims)
        else:
            return out

    def compile(self, code=False, matched=False, dense=False, omp=0):
        self.tidyup()
        Code = None
        if self.compiled:
            return
        for _, _, op in self.dynamics_args:
            if isinstance(op, QobjEvo):
                op.compile(code, matched, dense, omp)
        if not qset.has_openmp:
            omp = 0
        if omp:
            nnz = [self.cte.data.nnz]
            for part in self.ops:
                nnz += [part.qobj.data.nnz]
            if all(qset.openmp_thresh < nz for nz in nnz):
                omp = 0
        if self.const:
            if dense:
                self.compiled_qobjevo = CQobjCteDense()
                self.compiled = "dense single cte"
            elif omp:
                self.compiled_qobjevo = CQobjCteOmp()
                self.compiled = "csr omp cte"
                self.compiled_qobjevo.set_threads(omp)
                self.omp = omp
            else:
                self.compiled_qobjevo = CQobjCte()
                self.compiled = "csr single cte"
            self.compiled_qobjevo.set_data(self.cte)
        else:
            if matched:
                if omp:
                    self.compiled_qobjevo = CQobjEvoTdMatchedOmp()
                    self.compiled = "matched omp "
                    self.compiled_qobjevo.set_threads(omp)
                    self.omp = omp
                else:
                    self.compiled_qobjevo = CQobjEvoTdMatched()
                    self.compiled = "matched single "
            elif dense:
                self.compiled_qobjevo = CQobjEvoTdDense()
                self.compiled = "dense single "
            elif omp:
                self.compiled_qobjevo = CQobjEvoTdOmp()
                self.compiled = "csr omp "
                self.compiled_qobjevo.set_threads(omp)
                self.omp = omp
            else:
                self.compiled_qobjevo = CQobjEvoTd()
                self.compiled = "csr single "
            self.compiled_qobjevo.set_data(self.cte, self.ops)
            self.compiled_qobjevo.has_dyn_args(bool(self.dynamics_args))

            if self.type in ["func"]:
                funclist = []
                for part in self.ops:
                    funclist.append(part.get_coeff)
                self.coeff_get = _UnitedFuncCaller(funclist, self.args,
                                                   self.dynamics_args, self.cte)
                self.compiled += "pyfunc"
                self.compiled_qobjevo.set_factor(func=self.coeff_get)
            elif self.type in ["mixed_callable"]:
                funclist = []
                for part in self.ops:
                    if isinstance(part.get_coeff, _StrWrapper):
                        part.get_coeff, file = _compile_str_single(part.coeff, self.args)
                        coeff_files.add(file)
                    funclist.append(part.get_coeff)
                self.coeff_get = _UnitedFuncCaller(funclist, self.args,
                                                   self.dynamics_args, self.cte)
                self.compiled += "pyfunc"
                self.compiled_qobjevo.set_factor(func=self.coeff_get)
            elif self.type in ["string", "mixed_compilable"]:
                # All factor can be compiled
                self.coeff_get, Code, file = _compiled_coeffs(self.ops,
                                                              self.args,
                                                              self.dynamics_args,
                                                              self.tlist)
                coeff_files.add(file)
                self.compiled_qobjevo.set_factor(obj=self.coeff_get)
                self.compiled += "cyfactor"
            elif self.type == "array":
                if np.allclose(np.diff(self.tlist),
                               self.tlist[1] - self.tlist[0]):
                    self.coeff_get = InterCoeffCte(self.ops, None,
                                                     self.tlist)
                else:
                    self.coeff_get = InterCoeffT(self.ops, None, self.tlist)
                self.compiled += "cyfactor"
                self.compiled_qobjevo.set_factor(obj=self.coeff_get)
            elif self.type == "spline":
                self.coeff_get = InterpolateCoeff(self.ops, None, None)
                self.compiled += "cyfactor"
                self.compiled_qobjevo.set_factor(obj=self.coeff_get)
            else:
                pass
            coeff_files.clean()
            if code:
                return Code

    def _get_coeff(self, t):
        out = []
        for part in self.ops:
            out.append(part.get_coeff(t, self.args))
        return out

    def __getstate__(self):
        _dict_ = {key: self.__dict__[key]
                  for key in self.__dict__ if key is not "compiled_qobjevo"}
        if self.compiled:
            return (_dict_, self.compiled_qobjevo.__getstate__())
        else:
            return (_dict_,)

    def __setstate__(self, state):
        self.__dict__ = state[0]
        self.compiled_qobjevo = None
        if self.compiled:
            mat_type, threading, td =  self.compiled.split()
            if mat_type == "csr":
                if safePickle:
                    # __getstate__ and __setstate__ of compiled_qobjevo pass pointers
                    # In 'safe' mod, these pointers are not used.
                    if td == "cte":
                        if threading == "single":
                            self.compiled_qobjevo = CQobjCte()
                            self.compiled_qobjevo.set_data(self.cte)
                        elif threading == "omp":
                            self.compiled_qobjevo = CQobjCteOmp()
                            self.compiled_qobjevo.set_data(self.cte)
                            self.compiled_qobjevo.set_threads(self.omp)
                    else:
                        # time dependence is pyfunc or cyfactor
                        if threading == "single":
                            self.compiled_qobjevo = CQobjEvoTd()
                            self.compiled_qobjevo.set_data(self.cte, self.ops)
                        elif threading == "omp":
                            self.compiled_qobjevo = CQobjEvoTdOmp()
                            self.compiled_qobjevo.set_data(self.cte, self.ops)
                            self.compiled_qobjevo.set_threads(self.omp)

                        if td == "pyfunc":
                            self.compiled_qobjevo.set_factor(obj=self.coeff_get)
                        elif td == "cyfactor":
                            self.compiled_qobjevo.set_factor(func=self.coeff_get)
                else:
                    if td == "cte":
                        if threading == "single":
                            self.compiled_qobjevo = CQobjCte.__new__(CQobjCte)
                        elif threading == "omp":
                            self.compiled_qobjevo = CQobjCteOmp.__new__(CQobjCteOmp)
                            self.compiled_qobjevo.set_threads(self.omp)
                    else:
                        # time dependence is pyfunc or cyfactor
                        if threading == "single":
                            self.compiled_qobjevo = CQobjEvoTd.__new__(CQobjEvoTd)
                        elif threading == "omp":
                            self.compiled_qobjevo = CQobjEvoTdOmp.__new__(CQobjEvoTdOmp)
                            self.compiled_qobjevo.set_threads(self.omp)
                    self.compiled_qobjevo.__setstate__(state[1])

            elif mat_type == "dense":
                if td == "cte":
                    self.compiled_qobjevo = \
                        CQobjCteDense.__new__(CQobjCteDense)
                else:
                    CQobjEvoTdDense.__new__(CQobjEvoTdDense)
                self.compiled_qobjevo.__setstate__(state[1])

            elif mat_type == "matched":
                if threading == "single":
                    self.compiled_qobjevo = \
                        CQobjEvoTdMatched.__new__(CQobjEvoTdMatched)
                elif threading == "omp":
                    self.compiled_qobjevo = \
                        CQobjEvoTdMatchedOmp.__new__(CQobjEvoTdMatchedOmp)
                    self.compiled_qobjevo.set_threads(self.omp)
                self.compiled_qobjevo.__setstate__(state[1])

# Function defined inside another function cannot be pickled,
# Using class instead
class _UnitedFuncCaller:
    def __init__(self, funclist, args, dynamics_args, cte):
        self.funclist = funclist
        self.args = args
        self.dynamics_args = dynamics_args
        self.dims = cte.dims
        self.shape = cte.shape

    def set_args(self, args, dynamics_args):
        self.args = args
        self.dynamics_args = dynamics_args

    def dyn_args(self, t, state, shape):
        # 1d array are to F ordered
        mat = state.reshape(shape, order="F")
        for name, what, op in self.dynamics_args:
            if what == "vec":
                self.args[name] = state
            elif what == "mat":
                self.args[name] = mat
            elif what == "Qobj":
                if self.shape[1] == shape[1]:  # oper
                    self.args[name] = Qobj(mat, dims=self.dims)
                elif shape[1] == 1:
                    self.args[name] = Qobj(mat, dims=[self.dims[1],[1]])
                else:  # rho
                    self.args[name] = Qobj(mat, dims=self.dims[1])
            elif what == "expect":  # ket
                if shape[1] == op.cte.shape[1]: # same shape as object
                    self.args[name] = op.mul_mat(t, mat).trace()
                else:
                    self.args[name] = op.expect(t, state)

    def __call__(self, t, args=None):
        if args:
            now_args = self.args.copy()
            now_args.update(args)
        else:
            now_args = self.args
        out = []
        for func in self.funclist:
            out.append(func(t, now_args))
        return out

    def get_args(self):
        return self.args

class _Norm2():
    def __init__(self, f):
        self.func = f

    def __call__(self, t, args):
        return self.func(t, args)*np.conj(self.func(t, args))

class _Conj():
    def __init__(self, f):
        self.func = f

    def __call__(self, t, args):
        return np.conj(self.func(t, args))

class _Prod():
    def __init__(self, f, g):
        self.func_1 = f
        self.func_2 = g

    def __call__(self, t, args):
        return self.func_1(t, args)*self.func_2(t, args)

class _Add():
    def __init__(self, fs):
        self.funcs = fs

    def __call__(self, t, args):
        return np.sum([f(t, args) for f in self.funcs])

from qutip.superoperator import vec2mat

max_stars_repo_name: goerz-forks/qutip

reason: import numpy,import scipy,from scipy


text:
import scipy.io
import numpy as np
import matplotlib.pyplot as plt
import sys

if len(sys.argv) > 1:
    filename = sys.argv[1]
else:
    filename = 'corrsData.npz'

data = np.load(filename)
nTraces = data['nTraces']
maxCorrs = data['maxCorrs']

figure = plt.figure(1)
plt.plot(nTraces, maxCorrs, '0.8')
plt.plot(nTraces, maxCorrs[:,0xE0])

plt.axis([50, 500, 0, 0.035])
plt.ylabel('Correlation Coefficient')
plt.xlabel('Number of traces')

plt.savefig('figure-corrs.png')
plt.savefig('figure-corrs.pdf', format='pdf')
plt.show()


text:
# Kontsioti, Maskell, Dutta & Pirmohamed, A reference set of clinically relevant
# adverse drug-drug interactions (2021)
# Code to extract drug-drug interaction (DDI) data from BNF website

from bs4 import BeautifulSoup
import urllib
import os, csv
import numpy as np
import pandas as pd
from tqdm import tqdm
from nltk import word_tokenize
from nltk.util import ngrams
import string
import re
from nltk.tokenize.treebank import TreebankWordDetokenizer as Detok

detokenizer = Detok()

URL_BEGINNING = "https://bnf.nice.org.uk/interaction/"

print("beginning scrape...")

# Fetch the HTML containing the full list of Active Pharmaceutical Ingredients
# (APIs) with links for the interaction section.
r = urllib.request.urlopen(URL_BEGINNING).read()
soup1 = BeautifulSoup(r, "lxml")

# Extract the full URL list for interactions.
URL_list = []
for s in soup1.find_all("div", {"class": "span11"}):
    # a = soup1.find_all('div', {'class': 'span11'}).ul
    for ai in s(href=True):
        temp = URL_BEGINNING + ai["href"]
        URL_list.append(temp)
print(URL_list)

scraped_API_count = 0
# Create an empty dataframe to store the extracted data for each API.
scraped_API = pd.DataFrame(
    np.nan,
    index=range(0, 160000),
    columns=["API", "Interactant", "Description"],
    dtype=str,
)
row_count = 0

HIGHEST_API_ID = len(URL_list)

for id in tqdm(range(0, HIGHEST_API_ID)):

    # Try to fetch the HTML for each API.
    try:
        l = urllib.request.urlopen(URL_list[id]).read()
    # If the page returns a 404 error, skip this id.
    except urllib.error.HTTPError as e:
        if e.getcode() == 404:
            continue
        raise

    # Add one to the count of succesfully scraped products.
    scraped_API_count += 1

    soup2 = BeautifulSoup(l, "lxml")

    # Extract the relevant information to a dataframe.
    for n, m in zip(
        soup2.find_all("h5", {"class": "interactant"}),
        soup2.find_all("div", {"class": "span9 interaction-messages"}),
    ):
        API = (
            soup2.find("h1", {"class": "interaction-heading"})
            .span.getText()
            .lstrip(" ")
        )
        interactant_name = n.getText().strip("\n")
        # To avoid duplicate entries (i.e. same drug pair and related description
        # with reversed positions of API and Interactant), we only extract
        # the interactants that come after each API in alphabetical order.
        if interactant_name > API:
            for i in m.select('div[class*="interaction-message"]'):
                scraped_API.at[row_count, "API"] = API
                scraped_API.at[row_count, "Interactant"] = interactant_name
                description = i.getText().strip("\n").strip()
                scraped_API.at[row_count, "Description"] = description
                row_count += 1

print("BNF interactions succesfully scraped.")

print("begin scraped data postprocessing...")

# Remove empty rows from the dataframe that contains the extracted data.
scraped_API_dropna = scraped_API[~scraped_API.isin(["n"]).any(axis=1)]
# Remove spaces at the beginning and at the end of the text.
scraped_API_dropna["API"] = scraped_API_dropna["API"].str.strip()
scraped_API_dropna["Interactant"] = scraped_API_dropna["Interactant"].str.strip()
scraped_API_dropna["Description"] = scraped_API_dropna["Description"].str.strip()

# Remove severity and evidence information (if available) and put them in separate columns
scraped_API_dropna["Description_short"] = ""
scraped_API_dropna["Severity"] = ""
scraped_API_dropna["Evidence"] = ""
sev = "Severity of interaction"
evid = "Evidence for interaction"

for index, row in scraped_API_dropna.iterrows():
    # Takes the description of the selected row
    descr = row["Description"]
    # Split to tokens
    tokens = word_tokenize(descr)
    # Take trigrams (3 consecutive tokens)
    trigrams = ngrams(tokens, 3)
    # Include all trigrams in a list
    trigram_list = [" ".join(grams) for grams in trigrams]
    # If the severity description is contained in the description
    if sev in descr:
        # Take the position of the trigram that contains the level of severity
        sev_level_index = trigram_list.index(sev) + 2
        # Take the token describing the level of severity
        sev_level = trigram_list[sev_level_index].split()[2]
        # Write the level of severity to the dataframe
        scraped_API_dropna.iloc[index]["Severity"] = sev_level
        # Shorten the description - remove any severity and evidence information
        text = detokenizer.detokenize(tokens[: trigram_list.index(sev)])
        text = re.sub("\s*,\s*", ", ", text)
        text = re.sub("\s*\.\s*", ". ", text)
        text = re.sub("\s*\?\s*", "? ", text)
        row["Description_short"] = text
    else:
        row["Description_short"] = row["Description"]
    # If the evidence description is contained in the description
    if evid in descr:
        # Take the position of the trigram that contains the level of evidence
        evid_level_index = trigram_list.index(evid) + 2
        # Take the token describing the level of evidence
        evid_level = trigram_list[evid_level_index].split()[2]
        # Write the level of evidence to the dataframe
        scraped_API_dropna.iloc[index]["Evidence"] = evid_level

# Drop the original description column
scraped_API_dropna.drop("Description", axis=1, inplace=True)

FILE_NAME = "data_extraction/output/bnf_ddi_data.csv"

print("saving to file...")
# Save the dataset to a csv file.
scraped_API_dropna.to_csv(FILE_NAME, sep=",", index=False, encoding="utf-8")

max_stars_repo_name: elpidakon/CRESCENDDI

max_issues_repo_name: elpidakon/CRESCENDDI

max_forks_repo_name: elpidakon/CRESCENDDI