text:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 15:41:39 2018

@author: elcok
"""

import geopandas as gpd
import pandas as pd
import os
import igraph as ig
import numpy as np
import sys

from vtra.utils import load_config,extract_value_from_gdf,get_nearest_node,gdf_clip,count_points_in_polygon
from vtra.transport_network_creation import province_shapefile_to_network, add_igraph_time_costs_province_roads

def netrev_edges(region_name,start_points,end_points,graph,save_edges = True,output_path ='',excel_writer =''):
	"""
	Assign net revenue to roads assets in Vietnam

	Inputs are:
	start_points - GeoDataFrame of start points for shortest path analysis.
	end_points - GeoDataFrame of potential end points for shorest path analysis.
	G - iGraph network of the province.
	save_edges -

	Outputs are:
	Shapefile with all edges and the total net reveneu transferred along each edge
	GeoDataFrame of total net revenue transferred along each edge
	"""
	save_paths = []
	path_index = 0
	for iter_,place in start_points.iterrows():
		try:
			closest_center = end_points.loc[end_points['OBJECTID']
			== place['NEAREST_C_CENTER']]['NEAREST_G_NODE'].values[0]

			pos0_i = graph.vs[node_dict[place['NEAREST_G_NODE']]]
			pos1_i = graph.vs[node_dict[closest_center]]

			if pos0_i != pos1_i:
				path = graph.get_shortest_paths(pos0_i,pos1_i,weights='min_cost',output="epath")
				get_od_pair = (place['NEAREST_G_NODE'],closest_center)
				get_path = [graph.es[n]['edge_id'] for n in path][0]
				get_dist = sum([graph.es[n]['length'] for n in path][0])
				get_time = sum([graph.es[n]['min_time'] for n in path][0])
				get_travel_cost = sum([graph.es[n]['min_cost'] for n in path][0])
				path_index += 1
				save_paths.append(('path_{}'.format(path_index),get_od_pair,get_path,place['netrev'],get_travel_cost,get_dist,get_time))
		except:
			print(iter_)

	save_paths_df = pd.DataFrame(save_paths,columns = ['path_index','od_nodes','edge_path','netrev','travel_cost','distance','time'])
	save_paths_df.to_excel(excel_writer,province_name,index = False)
	excel_writer.save()
	del save_paths_df

	all_edges = [x['edge_id'] for x in graph.es]
	all_edges_geom = [x['geometry'] for x in graph.es]

	gdf_edges = gpd.GeoDataFrame(pd.DataFrame([all_edges,all_edges_geom]).T,crs='epsg:4326')
	gdf_edges.columns = ['edge_id','geometry']

	gdf_edges['netrev'] = 0
	for path in save_paths:
		gdf_edges.loc[gdf_edges['edge_id'].isin(path[2]),'netrev'] += path[3]

	if save_edges == True:
		gdf_edges.to_file(os.path.join(output_path,'weighted_edges_district_center_flows_{}.shp'.format(region_name)))
	return gdf_edges

if __name__ == '__main__':

	data_path,calc_path,output_path = load_config()['paths']['data'],load_config()['paths']['calc'],load_config()['paths']['output']

	# provinces to consider
	province_list = ['Thanh Hoa','Binh Dinh','Lao Cai']
	# province_list = ['Thanh Hoa']
	district_committe_names = ['district_people_committee_points_thanh_hoa.shp','district_province_peoples_committee_point_binh_dinh.shp','district_people_committee_points_lao_cai.shp']

	shp_output_path = os.path.join(output_path,'flow_mapping_shapefiles')
	flow_output_excel = os.path.join(output_path,'flow_mapping_paths','province_roads_district_center_flow_paths.xlsx')
	excl_wrtr = pd.ExcelWriter(flow_output_excel)

	for prn in range(len(province_list)):
		province = province_list[prn]
		# set all paths for all input files we are going to use
		province_name = province.replace(' ','').lower()

		edges_in = os.path.join(data_path,'Roads','{}_roads'.format(province_name),'vietbando_{}_edges.shp'.format(province_name))
		nodes_in = os.path.join(data_path,'Roads','{}_roads'.format(province_name),'vietbando_{}_nodes.shp'.format(province_name))
		population_points_in = os.path.join(data_path,'Points_of_interest','population_points.shp')
		commune_center_in = os.path.join(data_path,'Points_of_interest',district_committe_names[prn])

		province_path = os.path.join(data_path,'Vietnam_boundaries','who_boundaries','who_provinces.shp')
		commune_path = os.path.join(data_path,'Vietnam_boundaries','boundaries_stats','commune_level_stats.shp')

		# path_width_table = os.path.join(data_path,'Roads','road_properties','road_properties.xlsx')

		# load provinces and get geometry of the right province
		provinces = gpd.read_file(province_path)
		provinces = provinces.to_crs({'init': 'epsg:4326'})
		province_geom = provinces.loc[provinces.NAME_ENG == province].geometry.values[0]

		#clip all to province
		prov_pop = gdf_clip(population_points_in,province_geom)
		prov_commune_center = gdf_clip(commune_center_in,province_geom)
		if 'OBJECTID' not in prov_commune_center.columns.values.tolist():
			prov_commune_center['OBJECTID'] = prov_commune_center.index

		print (prov_commune_center)
		prov_communes = gdf_clip(commune_path,province_geom)

		# load nodes and edges
		nodes = gpd.read_file(nodes_in)
		nodes = nodes.to_crs({'init': 'epsg:4326'})
		sindex_nodes = nodes.sindex

		# get revenue values for each village

		# first create sindex of all villages to count number of villages in commune
		prov_pop_sindex = prov_pop.sindex

		# create new column in prov_communes with amount of villages
		prov_communes['n_villages'] = prov_communes.geometry.apply(lambda x: count_points_in_polygon(x,prov_pop_sindex))
		prov_communes['netrev_village'] = (prov_communes['netrevenue']*prov_communes['nfirm'])/prov_communes['n_villages']

		commune_sindex = prov_communes.sindex
		# give each village a net revenue based on average per village in commune
		prov_pop['netrev'] = prov_pop.geometry.apply(lambda x: extract_value_from_gdf(x,commune_sindex,prov_communes,'netrev_village'))

		# and use average if commune has no stats
		# prov_pop.loc[prov_pop['netrev'] == 0,'netrev'] = prov_pop['netrev'].mean()

		# get nearest node in network for all start and end points
		prov_pop['NEAREST_G_NODE'] = prov_pop.geometry.apply(lambda x: get_nearest_node(x,sindex_nodes,nodes,'NODE_ID'))
		prov_commune_center['NEAREST_G_NODE'] = prov_commune_center.geometry.apply(lambda x: get_nearest_node(x,sindex_nodes,nodes,'NODE_ID'))

		# prepare for shortest path routing, we'll use the spatial index of the centers
		# to find the nearest center for each population point
		sindex_commune_center = prov_commune_center.sindex
		prov_pop['NEAREST_C_CENTER'] = prov_pop.geometry.apply(lambda x: get_nearest_node(x,sindex_commune_center,prov_commune_center,'OBJECTID'))

		# load network

		# G = province_shapefile_to_network(edges_in,path_width_table)
		G = province_shapefile_to_network(edges_in)
		G = add_igraph_time_costs_province_roads(G,0.019)

		nodes_name = np.asarray([x['name'] for x in G.vs])
		nodes_index = np.asarray([x.index for x in G.vs])
		node_dict = dict(zip(nodes_name,nodes_index))

		# get updated edges
		edges_updated = netrev_edges(province_name,prov_pop,prov_commune_center,G,save_edges = True,output_path = shp_output_path,excel_writer = excl_wrtr)

max_stars_repo_name: oi-analytics/oia-transport-archive

max_issues_repo_name: oi-analytics/oia-transport-archive

max_forks_repo_name: oi-analytics/oia-transport-archive


text:
#!/usr/bin/env python3

# Copyright (2021-) Shahruk Hossain <shahruk10@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import unittest
import numpy as np

from kaldi_tflite.lib.layers import StatsPooling
from kaldi_tflite.lib.testdata import RefStatsPooling

tolerance = 4e-6

class TestStatsPoolingLayer(unittest.TestCase):

    def calcErr(self, ref, val):
        return np.sqrt(np.mean(np.power(ref - val, 2.0)))

    def defaultLayerCfg(self):
        return {
            "left_context": 0,
            "right_context": 16,
            "input_period": 1,
            "output_period": 1,
            "include_std": True,
            "padding": "SAME",
            "reduce": False,
            "epsilon": 1e-10,
            "reduce_time_axis": False,
            "name": "stats",
        }

    def test_StatsPoolingReduce(self):
        cfg = self.defaultLayerCfg()
        cfg.update({"reduce_time_axis": True})
        stats = StatsPooling(**cfg)

        name = "stats_mean_std"
        inputs = RefStatsPooling.getInputs(name)
        wantOutputs = RefStatsPooling.getOutputs(name)[:, 0:1, :]
        gotOutputs = stats(inputs)

        err = self.calcErr(wantOutputs, gotOutputs)
        self.assertTrue(err <= tolerance, f"err={err}, tolerance={tolerance}")

    def test_StatsPooling(self):

        # Default config overrides.
        configs = {
            "stats_mean": {"include_std": False},
            "stats_mean_std": {},
            "stats_mean_std_windowed": {"right_context": 4},
            "stats_mean_std_only_left_context": {"left_context": -4, "right_context": 0},
            "stats_mean_std_both_left_right_context": {"left_context": -4, "right_context": 4},
            "stats_mean_std_asymmetrical_context": {"left_context": -4, "right_context": 2},
            "stats_mean_std_subsampling": {"input_period": 4, "output_period": 4},
            "stats_mean_std_windowed_subsampling": {
                "left_context": -4, "right_context": 4, "input_period": 4, "output_period": 4,
            },
        }

        for name, overrides in configs.items():
            with self.subTest(name=name, overrides=overrides):
                cfg = self.defaultLayerCfg()
                cfg.update(overrides)
                stats = StatsPooling(**cfg)

                inputs = RefStatsPooling.getInputs(name)
                wantOutputs = RefStatsPooling.getOutputs(name)
                gotOutputs = stats(inputs)

                err = self.calcErr(wantOutputs, gotOutputs)
                self.assertTrue(err <= tolerance, f"err={err}, tolerance={tolerance}")

if __name__ == "__main__":
    unittest.main()

ext: py

max_stars_repo_name: ishine/kaldi-tflite

max_issues_repo_name: ishine/kaldi-tflite

max_forks_repo_name: ishine/kaldi-tflite

reason: import numpy


text:
#%%
from dataclasses import dataclass
import numpy as np
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDRegressor, LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.neighbors import KNeighborsRegressor
from shared import (
    dataset_local_path,
    bootstrap_r2,
    simple_boxplot,
)
import random
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler

# start off by seeding random number generators:
RANDOM_SEED = 12345
random.seed(RANDOM_SEED)
np.random.seed(RANDOM_SEED)

# Load the AirQualityUCI Dataset:
df = pd.read_csv(dataset_local_path("AirQualityUCI.csv"), sep=";", decimal=",")
print(df.shape)
# drop empty columns:
df = df.dropna(how="all", axis="columns")
print(df.shape)

PREDICT_COL = "CO(GT)"

# select only the rows where our 'y' is present:
df = df[df[PREDICT_COL] > -200.0]
print(df.shape)

# delete Date/Time columns
df.pop("Date")
df.pop("Time")

print(df.head())

#%%
#  Now train/test split:
tv_f, test_f = train_test_split(df, test_size=0.25, random_state=RANDOM_SEED)
train_f, vali_f = train_test_split(tv_f, test_size=0.25, random_state=RANDOM_SEED)

y_train = train_f.pop(PREDICT_COL)
y_vali = vali_f.pop(PREDICT_COL)
y_test = test_f.pop(PREDICT_COL)

#%%
# Now process data:
# Note, we don't NEED DictVectorizer... why?

# Let's fix missing values;
fix_missing = SimpleImputer(missing_values=-200.0)

scaler = StandardScaler()

X_train = scaler.fit_transform(fix_missing.fit_transform(train_f))
X_vali = scaler.transform(fix_missing.transform(vali_f))
X_test = scaler.transform(fix_missing.transform(test_f))

#%% Now let's build our models!

def regress_eval(y_true, y_pred) -> float:
    return metrics.r2_score(y_true=y_true, y_pred=y_pred)

@dataclass
class LinearRegressionModel:
    # Managed to squeeze bias into this weights array by adding some +1s.
    weights: np.ndarray

    @staticmethod
    def random(D: int) -> "LinearRegressionModel":
        weights = np.random.randn(D + 1, 1)
        return LinearRegressionModel(weights)

    def improve_feature_weight(
        self,
        train_X: np.ndarray,
        train_y: np.ndarray,
        feature_id: int,
        real_change: float = 0.0001,
    ) -> bool:
        changes = []
        start_weights = np.copy(self.weights)
        start_score = self.score(train_X, train_y)
        # try zero:
        self.weights[feature_id] = 0.0
        changes.append((self.score(train_X, train_y), np.copy(self.weights)))
        for dir in [-1, +1]:  # try bigger and smaller
            for step in [0.001, 0.01, 0.1, 1, 2, 4, 8]:  # try a range of steps
                weight = start_weights[feature_id] + dir * step
                self.weights[feature_id] = weight
                now_score = self.score(train_X, train_y)
                changes.append(
                    (now_score, np.copy(self.weights))
                )  # score accuracy/auc/whatver
        (best_score, best_weights) = max(changes, key=lambda t: t[0])
        if (best_score - start_score) >= real_change:
            self.weights = best_weights
            return True
        else:
            self.weights = start_weights
            return False

    def predict(self, X: np.ndarray) -> np.ndarray:
        """ Compute the signed distance from the self.weights hyperplane. """
        (N, D) = X.shape
        assert self.weights[:D].shape == (D, 1)
        # Matrix multiplication; sprinkle transpose and assert to get the shapes you want (or remember Linear Algebra)... or both!
        output = np.dot(self.weights[:D].transpose(), X.transpose())
        assert output.shape == (1, N)
        return (output + self.weights[-1]).reshape((N,))

    def score(self, X: np.ndarray, y: np.ndarray) -> float:
        """ Take predictions and compute accuracy. """
        y_hat = self.predict(X)
        return metrics.r2_score(np.asarray(y), y_hat)  # type:ignore

(N, D) = X_train.shape

graphs = {}

best_score = 0.0
best_model = None
for i in range(1000):
    m = LinearRegressionModel.random(D)
    train_score = m.score(X_vali, y_vali)
    if train_score > best_score or best_model is None:
        best_score = train_score
        best_model = m
        print("rand[{}] = {:.3}".format(i, train_score))

print(["{:1.3f}".format(x[0]) for x in best_model.weights.tolist()])

graphs["Best-of-Random"] = bootstrap_r2(best_model, X_vali, y_vali)

# SGD uses derivatives to 'aim' it's search... faster than CA which explores every direction!
for i in range(20):
    sgd = SGDRegressor(random_state=i + RANDOM_SEED)
    sgd.fit(X_train, y_train)
    train_score = sgd.score(X_vali, y_vali)
    if train_score > best_score or best_model is None:
        best_score = train_score
        best_model = sgd
        print("sgd[{}] = {:.3}".format(i, train_score))

graphs["SGD"] = bootstrap_r2(best_model, X_vali, y_vali)

# Linear Regression solves for a solution to y = Ax; no randomness needed!
lr = LinearRegression()
lr.fit(X_train, y_train)
train_score = lr.score(X_vali, y_vali)
print("lr = {:.3}".format(train_score))
graphs["LR"] = bootstrap_r2(lr, X_vali, y_vali)

def ca_restart(loud=False):
    ## MINI-CA
    if loud:
        print("### Coordinate Ascent ###\n\n")
    ca = LinearRegressionModel.random(D)
    if loud:
        print("ca.start = {:.3}".format(ca.score(X_train, y_train)))
    dims = list(range(D + 1))

    # keep optimizing until we stop getting better!
    while True:
        any_better = False
        random.shuffle(dims)
        for d in dims:
            better = ca.improve_feature_weight(X_train, y_train, feature_id=d)
            if better:
                any_better = True
        if not any_better:
            break
        if loud:
            print("w+ = {:.3}".format(ca.score(X_train, y_train)))
    return ca

# Coordinate ascent; try them:
best_score = 0.0
best_model = None
for i in range(3):
    ca = ca_restart()
    train_score = ca.score(X_vali, y_vali)
    if train_score > best_score or best_model is None:
        best_score = train_score
        best_model = ca
    print("ca[{}] = {:.3}".format(i, train_score))

graphs["CoordinateAscent"] = bootstrap_r2(best_model, X_vali, y_vali)

# --- now try some nonlinear models --- #
rf = RandomForestRegressor()
rf.fit(X_train, y_train)
graphs["RF"] = bootstrap_r2(rf, X_vali, y_vali)
knn = KNeighborsRegressor()
knn.fit(X_train, y_train)
graphs["KNN"] = bootstrap_r2(knn, X_vali, y_vali)

# Graph everything:
simple_boxplot(graphs, "{} R**2".format(PREDICT_COL), save="graphs/p11-r2-score.png")

##
# TODO:
# 1. remove the 'best-of-random' graph, so you can see the other ones!
# 2. See if there's anything here that might help your project.


text:
from tests.unit import TestAPI
import itertools

import numpy.random
import pandas as pd
import networkx as nx

from causality.inference.search import IC
from causality.inference.independence_tests import RobustRegressionTest

TEST_SET_SIZE = 2000

class Test_IC(TestAPI):

    def setUp(self):
        x1 = numpy.random.normal(size=TEST_SET_SIZE)
        x2 = x1 + numpy.random.normal(size=TEST_SET_SIZE)
        x3 = x1 + numpy.random.normal(size=TEST_SET_SIZE)
        x4 = x2 + x3 + numpy.random.normal(size=TEST_SET_SIZE)
        x5 = x4 + numpy.random.normal(size=TEST_SET_SIZE)
        self.X = pd.DataFrame({'x1' : x1, 'x2' : x2, 'x3' : x3, 'x4' : x4, 'x5' : x5})
        self.variable_types = {'x1' : 'c', 'x2' : 'c', 'x3' : 'c', 'x4' : 'c', 'x5' : 'c'}
        self.true_neighbors = { 'x1' : set(['x2','x3']),
                                'x2' : set(['x1','x4']),
                                'x3' : set(['x1','x4']),
                                'x4' : set(['x2','x3','x5']),
                                'x5' : set(['x4'])}
        self.true_colliders = set([('x3','x4'), ('x2','x4')])
        self.true_marked = set([('x4','x5')])
        self.ic = IC(RobustRegressionTest)
        self.ic.search(self.X, self.variable_types)

    def test_build_g(self):
        self.ic._build_g(self.variable_types)
        V = len(self.X.columns)
        assert(len(self.ic._g.edges()) == (V-1)*V / 2)
        assert(set(self.ic._g.nodes()) == set(self.variable_types.keys()))
        for node, variable_type in self.variable_types.items():
            assert(self.ic._g.nodes[node]['type'] == variable_type)
        for i, j in self.ic._g.edges():
            assert(self.ic._g.get_edge_data(i, j)['marked'] == False)

    def test_find_skeleton(self):
        self.ic._build_g(self.variable_types)
        self.ic._find_skeleton(self.X, self.variable_types)
        for node, neighbors in self.true_neighbors.items():
            assert(set(self.ic._g.neighbors(node)) == neighbors)

    def test_orient_colliders(self):
        self.ic._build_g(self.variable_types)
        self.ic._find_skeleton(self.X, self.variable_types)
        self.ic._orient_colliders()
        for i, j in self.ic._g.edges():
            measured_colliders = self.ic._g.get_edge_data(i, j)['arrows']
            if len(measured_colliders) > 0:
                if j in measured_colliders:
                    assert((i,j) in self.true_colliders)
                else:
                    assert((j,i) in self.true_colliders)
            else:
                assert((i,j) not in self.true_colliders and (j,i) not in self.true_colliders)

    def test_separating_set(self):
        self.ic._build_g(self.variable_types)
        self.ic._find_skeleton(self.X, self.variable_types)
        for xi, xj in itertools.combinations(self.variable_types.keys(), 2):
            if not self.ic._g.has_edge(xi,xj):
                if (xi,xj) in self.ic.separating_sets:
                    z = self.ic.separating_sets[(xi,xj)]
                else:
                    z = self.ic.separating_sets[(xj,xi)]
                test = self.ic.independence_test([xj],[xi], list(z), self.X, self.ic.alpha)
                assert(test.independent())

    def test_marked_directed_path(self):
        marked_edges = [('a','b'),('b','c'),('c','d')]
        unmarked_edges = [('a','d')]
        nodes = ['a','b','c','d']
        g = nx.Graph()
        g.add_edges_from(marked_edges, marked=True)
        g.add_edges_from(unmarked_edges, marked=False)
        for i, j in (marked_edges + unmarked_edges):
            g.get_edge_data(i, j)['arrows'] = [j]
        self.ic._g = g
        assert(self.ic._marked_directed_path('a','d'))
        assert(not self.ic._marked_directed_path('d','a'))

    def test_recursion_rule_1(self):
        pass

    def test_recursion_rule_2(self):
        pass

    def test_search(self):
        self.ic.search(self.X, self.variable_types)
        for i, j in self.ic._g.edges():
            if self.ic._g.get_edge_data(i, j)['marked']:
                assert( (i,j) in self.true_marked or (j,i) in self.true_marked)
            else:
                assert( (i,j) not in self.true_marked and (j,i) not in self.true_marked)

max_stars_repo_name: vishalbelsare/causality

max_issues_repo_name: vishalbelsare/causality

max_forks_repo_name: vishalbelsare/causality

reason: import numpy,import networkx


text:
from single_qubit_classifier import generate_noisy_classification
import matplotlib.pyplot as plt
import numpy as np

import nisqai
from pyquil import get_qc, Program
from pyquil.api import WavefunctionSimulator
from _dense_angle_encoding import DenseAngleEncoding
from nisqai.encode import WaveFunctionEncoding
from nisqai.encode._feature_maps import FeatureMap
from nisqai.encode._encoders import angle_simple_linear
from _encoders import angle_param_linear
from nisqai.layer._base_ansatz import BaseAnsatz
from nisqai.layer._params import Parameters
from nisqai.data._cdata import CData, LabeledCData
from nisqai.cost._classical_costs import indicator
from pyquil.gates import *
from scipy.optimize import minimize
from collections import Counter
import random
from sklearn.datasets import make_circles, make_moons

from noise_models import add_noisy_gate_to_circ, estimate_meas_noise
from classifier_circuits import *
from data_gen import *
from single_qubit_classifier import train_classifier, train_classifier_encoding, ClassificationCircuit
from plots import plot_correct_classifications, scatter
from data_gen import generate_data, remove_zeros

"""
# Test/Learn optimal encodings in presence of certain noise models
"""

def main(train=False, encoding_choice='denseangle_param', retrain=False, data_choice='moons', noise=False):

    ### Firstly, generate for dataset:
    '''
    # We use the transpose of the (scaled to unit square) Moons dataset in order to see a non-linear decision boundary
    '''
    data_train, data_test, true_labels_train, true_labels_test = generate_data(data_choice, num_points=500, split=True)

    # data_train, true_labels_train   = remove_zeros(data_train, true_labels_train)
    # data_test, true_labels_test     = remove_zeros(data_test, true_labels_test)

    ### Next, generate correct classification parameters for dataset (perfect classification):
    '''
    # Define parameters of model. Start with DenseAngle encoding with fixed parameters.
    '''

    qc_name = '1q-qvm'
    qc = get_qc(qc_name)
    num_shots = 1024
    qubits = qc.qubits()
    init_params = np.random.rand(3)
    if encoding_choice.lower() == 'wavefunction_param':
        init_encoding_params = [ 0 ] # Generalized Wavefunction Encoding initialised to Wavefunction encoding
    else:
        init_encoding_params = [np.pi, 2*np.pi]

    if train:

        optimiser = 'Powell'
        params, result_unitary_param = train_classifier(qc, num_shots, init_params, encoding_choice, init_encoding_params, optimiser, data_train, true_labels_train)

        print('The optimised parameters are:', result_unitary_param.x)
        print('These give a cost of:', ClassificationCircuit(qubits, data_train).build_classifier(result_unitary_param.x, encoding_choice, init_encoding_params, num_shots, qc, true_labels_train))
        ideal_params =  result_unitary_param.x
    else:
        if data_choice.lower() == 'moons':
            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about 90 %
            '''
            # 90% Classification parameters for dense angle encoding
            '''
            if encoding_choice.lower() == 'denseangle_param': ideal_params= [ 2.19342064 , 1.32972029, -0.18308298]

            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about 75 %
            '''
            # 73% Classification parameters for superdense angle encoding
            '''
            if encoding_choice.lower() == 'superdenseangle_param': ideal_params =  [-0.27365492,  0.83278854,  3.00092961]

            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about  %
            '''
            # 85% Classification parameters for wavefunction encoding
            '''
            if encoding_choice.lower() == 'wavefunction': ideal_params = [0.81647273, 0.41996708, 2.20603541]
            if encoding_choice.lower() == 'wavefunction_param': ideal_params = [0.81647273, 0.41996708, 2.20603541]

        elif data_choice.lower() == 'random_vertical_boundary':
            if encoding_choice.lower() == 'superdenseangle_param': ideal_params = [1.606422245361118, 0.23401504261014927, 5.694226283697996]

        elif data_choice.lower() == 'random_diagonal_boundary':

            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about 90 %
            '''
            # 90% Classification parameters for dense angle encoding
            '''
            if encoding_choice.lower() == 'denseangle_param':   ideal_params = [0.8579214,  1.22952647, 4.99408074]

            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about  %
            '''
            # % Classification parameters for superdense angle encoding
            '''
            if encoding_choice.lower() == 'superdenseangle_param': ideal_params = [2.0101407,  1.05916291, 1.14570489]

            ### Define Ideal parameters for trained model. Simple model can acheieve classification of about  97%
            '''
            # 97% Classification parameters for wavefunction encoding
            '''
            if encoding_choice.lower() == 'wavefunction':           ideal_params = [0.69409285 0.0862859  0.42872711]
            if encoding_choice.lower() == 'wavefunction_param':     ideal_params =  [0.69409285 0.0862859  0.42872711]

    print('These give a cost of:', ClassificationCircuit(qubits, data_test).build_classifier(ideal_params, encoding_choice, init_encoding_params, num_shots, qc, true_labels_test))
    predicted_labels_ideal = ClassificationCircuit(qubits, data_test).make_predictions(ideal_params, encoding_choice, init_encoding_params, num_shots, qc)

    # nisqai.visual.scatter(data_test, true_labels_test, predicted_labels)

    ### Overlay decision bounday
    '''
    # Generate Grid of datapoints to determine and visualise ideal decision boundary
    '''
    num_points = 400
    data_grid, grid_true_labels = generate_data('full_vertical_boundary', num_points)
    data_grid, grid_true_labels = remove_zeros(data_grid, grid_true_labels)

    predicted_labels = ClassificationCircuit(qubits, data_test).make_predictions(ideal_params, encoding_choice, init_encoding_params, num_shots, qc)
    plot_params = {'colors': ['blue', 'orange'], 'alpha': 1}
    scatter(data_test, true_labels_test, predicted_labels, **plot_params)

    predicted_labels_grid = ClassificationCircuit(qubits, data_grid).make_predictions(ideal_params, encoding_choice, init_encoding_params, num_shots, qc)

    plot_params = {'colors': ['red', 'green'], 'alpha': 0.2}

    scatter(data_grid, predicted_labels_grid, **plot_params)
    plt.show()

    ## Define noise parameters
    '''
    # Define noise parameters to add to model to determine how classification is affected.
    '''
    if noise:
        noise_choice = 'amp_damp_before_measurement'
        noise_values = 0.3

    ### Add noise to circuit and classify
    '''
    # Add noise to circuit, and determine number of points classified differently (not mis-classified since we can't achieve perfect classification)
    '''
    if noise:
        noisy_predictions, number_classified_same = generate_noisy_classification(ideal_params, noise_choice, noise_values, encoding_choice, init_encoding_params, qc, num_shots, data_test, predicted_labels_ideal)
        print('The proportion classified differently after noise is:', 1- number_classified_same)

    ## Overlay decision boundary
    '''
    # Generate Grid of datapoints to determine and visualise ideal decision boundary WITH noise added
    '''

    if noise:
        print(noise_choice)
        predicted_labels = ClassificationCircuit(qubits, data_test, noise_choice, noise_values).make_predictions(ideal_params, encoding_choice, init_encoding_params, num_shots, qc)
        plot_params = {'colors': ['blue', 'orange'], 'alpha': 1}
        scatter(data_test, true_labels_test, predicted_labels, **plot_params)

        predicted_labels_grid = ClassificationCircuit(qubits, data_grid, noise_choice, noise_values).make_predictions(ideal_params, encoding_choice, init_encoding_params, num_shots, qc)
        plot_params = {'colors': ['red', 'green'], 'alpha': 0.2}
        scatter(data_grid, predicted_labels_grid, **plot_params)

        plt.show()

    ### Retrain circuit with noise
    '''
    # Given the noise in the circuit, train the parameters of encoding unitary to account for noise. Parameterised unitary parameters are fixed as the ideal ones learned.
    '''
    if retrain:

        if encoding_choice.lower() == 'wavefunction_param': optimiser = 'L-BFGS-B'
        else:                                               optimiser = 'Powell'

        if noise:
            encoding_params, result_encoding_param = train_classifier_encoding(qc, noise_choice, noise_values, num_shots, ideal_params, encoding_choice, init_encoding_params, optimiser, data_train, true_labels_train)
            print('The optimised encoding parameters with noise are:', result_encoding_param.x)
            ideal_encoding_params = result_encoding_param.x
        else:
            encoding_params, result_encoding_param = train_classifier_encoding(qc, None, None, num_shots, ideal_params, encoding_choice, init_encoding_params, optimiser, data_train, true_labels_train)
            print('The optimised encoding parameters without noise are:', result_encoding_param.x)
            ideal_encoding_params = result_encoding_param.x
    else:
        ### Define Ideal ENCODING parameters for trained model. Simple model can acheieve classification of about 90 with noise, 93% without noise %
        '''
        # 90% Classification parameters for dense angle encoding
        '''
        if data_choice.lower() == 'moons' and encoding_choice.lower() == 'denseangle_param' and noise:
            ideal_encoding_params = [2.23855329, 7.57781576]
            '''
            # 93% Classification parameters for dense angle encoding without noise
            '''
        elif data_choice.lower() == 'moons' and encoding_choice.lower() == 'denseangle_param':
            ideal_encoding_params =  [3.05615259, 7.61215138]  # No noise

        ### Define Ideal ENCODING parameters for trained model. Simple model can acheieve classification of about 90 %
        '''
        # NO NOISE  - 74-77% Classification parameters with training for superdense angle encoding
        # NOISE     - Classification parameters for superdense angle encoding (0.3 amp damp = 20% different classification - 69% accuracy with noise before encoding training)
        #             With learned encoding -
        '''
        if data_choice.lower() == 'moons' and encoding_choice.lower() == 'superdenseangle_param' and noise:
            ideal_encoding_params =  [3.31296568, 6.34142188]

        elif data_choice.lower() == 'moons' and encoding_choice.lower() == 'superdenseangle_param':
            ideal_encoding_params = [2.86603822, 6.14328274] # No noise

        ### Define Ideal ENCODING parameters for trained model. Simple model can acheieve classification of about 90 %
        '''
        # NO NOISE  - 82-84% Classification parameters with training for generalised wavefunction encoding
        # NOISE     - Classification parameters for superdense angle encoding (0.3 amp damp = 20% different classification - 78% accuracy with noise before encoding training)
        #             With learned encoding -
        '''
        print(data_choice.lower(), encoding_choice.lower())
        if data_choice.lower() == 'moons' and encoding_choice.lower() == 'wavefunction_param' and noise:
            ideal_encoding_params =  [0.02884417]
        elif data_choice.lower() == 'moons' and encoding_choice.lower() == 'wavefunction_param':
            ideal_encoding_params = [0.01582773] # No noise

    if noise:
        print('These give a cost with the noisy circuit of:',\
            ClassificationCircuit(qubits, data_test, noise_choice, noise_values).build_classifier(ideal_params, encoding_choice, ideal_encoding_params , num_shots, qc, true_labels_test) )
    else:
        print('These give a cost with the ideal circuit of:',\
            ClassificationCircuit(qubits, data_test).build_classifier(ideal_params, encoding_choice, ideal_encoding_params , num_shots, qc, true_labels_test) )

    ### Add noise to circuit and classify
    '''
    # Using learned encoding parameters, check again proportion misclassified
    '''
    if noise:
        noisy_predictions, number_classified_same = generate_noisy_classification(ideal_params, noise_choice, noise_values, encoding_choice, ideal_encoding_params, qc, num_shots, data_test, predicted_labels)
        print('The proportion classified differently after noise with learned encoding is:', 1 - number_classified_same)

    ## Overlay decision boundary
    '''
    # Generate Grid of datapoints to determine and visualise ideal decision boundary WITH/WITHOUT noise added
    '''
    if noise:
        predicted_labels = ClassificationCircuit(qubits, data_test, noise_choice, noise_values).make_predictions(ideal_params, encoding_choice, ideal_encoding_params, num_shots, qc)

        plot_params = {'colors': ['blue', 'orange'], 'alpha': 1}
        scatter(data_test,  true_labels_test, predicted_labels, **plot_params)

        predicted_labels_grid = ClassificationCircuit(qubits, data_grid, noise_choice, noise_values).make_predictions(ideal_params, encoding_choice, ideal_encoding_params, num_shots, qc)

        plot_params = {'colors': ['red', 'green'], 'alpha': 0.2}
        scatter(data_grid, predicted_labels_grid, **plot_params)
        plt.show()
    else:
        predicted_labels = ClassificationCircuit(qubits, data_test).make_predictions(ideal_params, encoding_choice, ideal_encoding_params, num_shots, qc)

        plot_params = {'colors': ['blue', 'orange'], 'alpha': 1}
        scatter(data_test, true_labels_test,  predicted_labels, **plot_params)

        predicted_labels_grid = ClassificationCircuit(qubits, data_grid).make_predictions(ideal_params, encoding_choice, ideal_encoding_params, num_shots, qc)

        plot_params = {'colors': ['red', 'green'], 'alpha': 0.2}
        scatter(data_grid, predicted_labels_grid, **plot_params)
        plt.show()

if __name__ == "__main__":
    main(train=False, encoding_choice='superdense_angle', retrain=False, data_choice='random_diagonal_boundary', noise=True)

'''
# IDEAL PARAMETERS FOR DATASETS
'''
# ideal_params_diagonal = [0.8268539878512121, -0.019890153474649244, 0.762840633753562]
# ideal_params = [1.552728382792277, 2.1669057463233097, -0.013736721729997667] #Vertical?

'''
NOISE PARAMETERS
'''
noise = "Decoherence_symmetric_ro"
# T1 = 30e-6
# T2 = 30e-6
# gate_time_1q = 50e-9
# gate_time_2q = 150e-09
# ro_fidelity = 0.95

# T1 = np.inf
# T2 = np.inf
# gate_time_1q = 0
# gate_time_2q = 0
# ro_fidelity = 0.95
# noise_values = [T1, T2, gate_time_1q, gate_time_2q, ro_fidelity]
# noise  ='measurement'
# noise_values = [0.95, 0.95]

max_stars_repo_name: BrianCoyle/NoiseRobustClassifier

max_issues_repo_name: BrianCoyle/NoiseRobustClassifier

max_forks_repo_name: BrianCoyle/NoiseRobustClassifier

reason: import numpy,from scipy


text:
# Demonstrates parameter estimation and Gibbs sampling for a multivariate Gaussian

from plmrf import *
import numpy as np

nsamp = 1000
x1 = np.random.normal(size=nsamp)
y1 = np.random.normal(loc=x1)

x2 = np.random.normal(size=nsamp)
y2 = np.random.normal(loc=x2)

var_defs = [
    VariableDef("x1", samples=x1),
    VariableDef("x2", samples=x2),
    VariableDef("y1", samples=y1),
    VariableDef("y2", samples=y2)
]

data = {
    "x1" : x1,
    "x2" : x2,
    "y1" : y1,
    "y2" : y2,
}

potentials = [
    GaussianPotential(["x1", "y1"], samples=data),
    GaussianPotential(["x2", "y2"], samples=data),
    GaussianPotential(["y1", "y2"], samples=data),
    GaussianPotential(["y1"], samples=data, location=0),
    GaussianPotential(["y2"], samples=data, location=0)
]

network = LogLinearMarkovNetwork(potentials, var_defs)
mple_result = network.fit(data)
print("MPLE optimization result:")
print(mple_result)

x1p, x2p, y1p, y2p = np.array(np.meshgrid(
    np.linspace(np.min(x1), np.max(x1), 5),
    np.linspace(np.min(x2), np.max(x2), 5),
    np.linspace(np.min(y1), np.max(y1), 5),
    np.linspace(np.min(y2), np.max(y2), 5))).reshape(4, -1)
new_data = {
    "x1" : x1p,
    "x2" : x2p,
    "y1" : y1p,
    "y2" : y2p
}

print("\n")
var_order = ["x1", "x2", "y1", "y2"]
cur_state = {"x1" : np.array([0]), "x2" : np.array([0]),
             "y1" : np.array([0]), "y2" : np.array([0])}
print("\nGibbs sampling from {0}".format([cur_state[v][0] for v in var_order]))
for i in range(20):
    cur_state = network.gibbs_sample(cur_state)
    print("Sample {0} state: {1}".format(i+1, [cur_state[v][0] for v in var_order]))

max_stars_repo_name: dgarant/kde-markov-network

max_issues_repo_name: dgarant/kde-markov-network

max_forks_repo_name: dgarant/kde-markov-network


text:
# -*- coding: utf-8 -*-
"""
Ashutosh Rattan (53004)
COE6
"""

import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
import sys

def handleMissing(filename):
    data=pd.DataFrame(pd.read_csv(filename))

    SI=SimpleImputer(missing_values=np.nan,strategy='mean') # strategy can be
    #assigned mean, median etc. Look the function signature for more info.

    data=pd.DataFrame(SI.fit_transform(data))# fit computes the 'strategy'
    #transform applies it on the data, fit_transform() combines these two steps.
    data.to_csv('dataNew.csv',index=False)
    print("New data is saved to file 'dataNew.csv' in this directory")

#handleMissing("data.csv")
if (__name__ == "__main__"):
    handleMissing(sys.argv[1])


text:
import numpy as np
import pywt

class CwtTimeSuiteBase(object):
    """
    Set-up for CWT timing.
    """
    params = ([32, 128, 512],
              ['cmor', 'cgau4', 'fbsp', 'gaus4', 'mexh', 'morl', 'shan'],
              [16, 64, 256])
    param_names = ('n', 'wavelet', 'max_scale')

    def setup(self, n, wavelet, max_scale):
        try:
            from pywt import cwt
        except ImportError:
            raise NotImplementedError("cwt not available")
        self.data = np.ones(n, dtype='float')
        self.scales = np.arange(1, max_scale+1)

class CwtTimeSuite(CwtTimeSuiteBase):
    def time_cwt(self, n, wavelet, max_scale):
        pywt.cwt(self.data, self.scales, wavelet)

max_stars_repo_name: lravindr/pywt

max_issues_repo_name: lravindr/pywt

max_forks_repo_name: lravindr/pywt


text:
#
# Python integration example using RiskPaths model
#   to analyze contribution of delayed union formations
#   versus decreased fertility on childlessness
#
# Input parameters:
#   AgeBaselineForm1: age baseline for first union formation
#   UnionStatusPreg1: relative risks of union status on first pregnancy
# Output value:
#   T05_CohortFertility: Cohort fertility, expression 1
#

# Prerequisite:
#
# download openM++ release from https://github.com/openmpp/main/releases/latest
# unpack it into any directory
# start oms web-service:
#   Windows:
#     cd C:\my-openmpp-release
#     bin\ompp_ui.bat
#   Linux:
#     cd ~/my-openmpp-release
#     bin/oms
#
# Script below is using openM++ web-service "oms"
# to run the model, modify parameters and read output values.

# Important:
#   Script below does not handle errors, please use try/except in production.

import time
import requests
import numpy as np
import matplotlib.pyplot as plt

# get default values for AgeBaselineForm1, UnionStatusPreg1 and SimulationCases parameters
# by reading it from first model run results
# assuming first run of the model done with default set of parameters
#
rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/status/first')
rsp.raise_for_status()
firstRunStatus = rsp.json()
firstRunDigest = rsp.json()['RunDigest']

rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/' + firstRunDigest + '/parameter/AgeBaselineForm1/value')
rsp.raise_for_status()
ageFirstUnion = rsp.json()

rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/' + firstRunDigest + '/parameter/UnionStatusPreg1/value')
rsp.raise_for_status()
unionStatusPreg = rsp.json()

rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/' + firstRunDigest + '/parameter/SimulationCases/value')
rsp.raise_for_status()
simulationCases = rsp.json()

# create new input data for our modelling task
#

# set number of simulation cases
simulationCases[0]['Value'] = 1000

# for AgeBaselineForm1 and UnionStatusPreg1 parameter values
# apply scale in range from 0.44 to 1.0
#
scaleStep = 0.08
scaleValues = [0.44 + i * scaleStep for i in range(1 + round((1.00 - 0.44) / scaleStep))]

ageValues = [x['Value'] for x in ageFirstUnion]
unionValues = [x['Value'] for x in unionStatusPreg]

inpSetLst = []
for scaleAgeBy in scaleValues:
    #
    print("Scale age by:", scaleAgeBy)
    for i in range(len(ageFirstUnion)):
        ageFirstUnion[i]['Value'] = ageValues[i] * scaleAgeBy

    for scaleUnionBy in scaleValues:
        #
        # scale first two values of unionStatusPreg vector
        unionStatusPreg[0]['Value'] = unionValues[0] * scaleUnionBy
        unionStatusPreg[1]['Value'] = unionValues[1] * scaleUnionBy
        #
        # create new set of input parameters
        # automatically generate unique names for each input set
        #
        inpSetRq = {
            'ModelName': 'RiskPaths',
            'Name': '',
            'BaseRunDigest': firstRunDigest,
            'IsReadonly': True,
            'Txt': [{
                'LangCode': 'EN',
                'Descr': 'Scale age: ' + str(scaleAgeBy) + ' union status: ' + str(scaleUnionBy)
            }],
            'Param': [
                {
                    'Name': 'AgeBaselineForm1',
                    'SubCount': 1,
                    'Value': ageFirstUnion,
                    'Txt': [{'LangCode': 'EN', 'Note': 'Age values scale by: ' + str(scaleAgeBy)}]
                },
                {
                    'Name': 'UnionStatusPreg1',
                    'SubCount': 1,
                    'Value': unionStatusPreg,
                    'Txt': [{'LangCode': 'EN', 'Note': 'Union Status values scale by: ' + str(scaleUnionBy)}]
                }
            ],
        }
        #
        # create new input set of model parameters
        # automatically generate unique name for that input set
        #
        rsp = requests.put('http://127.0.0.1:4040/api/workset-create', json=inpSetRq)
        rsp.raise_for_status()
        js = rsp.json()
        #
        inpSetName = js['Name']
        if inpSetName is None or inpSetName == '':
            raise Exception("Fail to create input set, scales:", scaleAgeBy, scaleUnionBy)
        #
        inpSetLst.append(inpSetName)

# create modeling task from all input sets
# automatically generate unique name for the task
#
inpLen = len(inpSetLst)
print("Create task from", inpLen, "input sets of parameters")

taskRq = {
    'ModelName': 'RiskPaths',
    'Name': '',
    'Set': inpSetLst,
    'Txt': [{
        'LangCode': 'EN',
        'Descr': 'Task to run RiskPaths ' + str(inpLen) + ' times',
        'Note': 'Task scales AgeBaselineForm1 and UnionStatusPreg1 parameters from 0.44 to 1.00 with step ' + str(scaleStep)
    }]
}
rsp = requests.put('http://127.0.0.1:4040/api/task-new', json=taskRq)
rsp.raise_for_status()
js = rsp.json()

taskName = js['Name']
if taskName is None or taskName == '':
    raise Exception("Error at create modeling task")

#
# submit request to web-service to run RiskPaths with modeling task
#
runModelRq = {
    'ModelName': 'RiskPaths',
    'Opts': {
        'OpenM.TaskName': taskName,
        'OpenM.ProgressPercent': '100'
    }
}
rsp = requests.post('http://127.0.0.1:4040/api/run', json=runModelRq)
rsp.raise_for_status()
js = rsp.json()
#
taskRunStamp = js['RunStamp']
if taskRunStamp is None or taskRunStamp == '':
    raise Exception('Model failed to start, task run stamp is empty')

print("Starting modeling task:", taskName)

# wait until modeling task completed
# and report the progress
#
# task status returned by web-service can be one of:
# i=initial p=in progress w=waiting s=success x=exit e=error(failed)
#
taskStatus = ''

while taskStatus in '' 'i' 'p' 'w':
    #
    time.sleep(1)
    #
    rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/task/' + taskName + '/run-status/run/' + taskRunStamp)
    rsp.raise_for_status()
    js = rsp.json()
    taskStatus = js['Status']
    #
    # if model not started to run the task yet check again after short sleep
    #
    if taskStatus in '' 'i':
        #
        print("Waiting for modeling task to start...")
        continue
    #
    # if task completed successfully then get pairs of {model run, inpur set name}
    #
    if taskStatus == 's':
        rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/task/' + taskName + '/runs')
        rsp.raise_for_status()
        js = rsp.json()
        taskRuns = js['TaskRun'][0]['TaskRunSet']   # use index=0 because this is first run of our task
        break
    #
    # if task still in progress then count completed model runs
    #
    if taskStatus in 'i' 'p' 'w':
        rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/' + taskRunStamp + '/status/list')
        rsp.raise_for_status()
        trs = rsp.json()
        #
        n = 0
        for r in trs:
            if r['Status'] == 's': n += 1
        #
        print("Completed", n, "model runs out of", inpLen)
        continue
    #
    # any other task run status considered as failure
    #
    raise Exception("Model run failed, task run stamp:", taskRunStamp, "status:", taskStatus)
    #

print("Modeling task completed, retriving results...")

# for each age and union status retrive output:
#   childlessness value: T05_CohortFertility.Expr1
#
# organize results into 2-dimensional array to plot 3d chart
#
childlessnessVals = np.zeros((len(scaleValues), len(scaleValues)))
runIdx = 0

for ageIdx in range(len(scaleValues)):
    for unionIdx in range(len(scaleValues)):
        #
        runDigest = taskRuns[runIdx]['Run']['RunDigest']
        #
        rsp = requests.get('http://127.0.0.1:4040/api/model/RiskPaths/run/' + runDigest + '/table/T05_CohortFertility/expr')
        rsp.raise_for_status()
        js = rsp.json()
        #
        childlessnessVals[ageIdx][unionIdx] = js[1]['Value']
        runIdx += 1

# display the results
#
ageVals, unionVals = np.meshgrid(scaleValues, scaleValues)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(ageVals, unionVals, childlessnessVals, color='black')
ax.set_xlabel('Decreased union formation')
ax.set_ylabel('Decreased fertility')
ax.set_zlabel('Childlessness')
ax.view_init(elev=45)
plt.show()

max_stars_repo_name: openmpp/python

max_issues_repo_name: openmpp/python

max_forks_repo_name: openmpp/python


text:
"""
Generate pulse sequence diagrams for demonstration purposes.

@author Paul J. Ganssle
"""
import scipy.io;
from numpy import *;

from matplotlib import rcParams, rc
from matplotlib.pyplot import *;

from matplotlib.patches import Rectangle, FancyArrowPatch
from matplotlib.lines import Line2D

import matplotlib.gridspec as gridspec

import theme.colors as thc
import theme.figure_settings as tfs

tfs.setup_environment()

"""
Functions for creating figures
"""
def make_spin_echo_constant_time(ax,
    T2=2.0, T2s=0.125, t_max=2.0, tau=0.5, freq=10.0,
    exc_phase=0.0, ref_phase=0.0,
    pulse_width=0.05,
    pulse_margin=0.025,
    pulse_fc=thc.bc,
    pulse_ec='k',
    echo_color=thc.rc,
    pulse_lw=1.0,
    echo_lw=1.5,
    base_lw=2.0,
    fill_alpha=0.5,
    margins=[None, None, None, None],
    margin_bottom=None,
    margin_top=None,
    margin_left=None,
    margin_right=None,
    baseline_height=None,
    echo_h_frac=0.85,
    annotate_pulses=True,
    ShowPulses=True,
    ShowBaseline=True,
    ShowEchoes=True,
    AnnotatePulses=True):

    """
    Single spin echo sequence with constant time.
    """
    fig = ax.get_figure()
    trans = ax.transAxes
    ax.axis('off')

    exc_phase *= pi/180.0                           # Degrees to radians
    ref_phase *= pi/180.0

    # Margins can be specified either with a list [bot, top, left, right] or
    # with specific keyword arguments. The keyword arguments override the list items.
    if len(margins) < 4:
        for ii in range(len(margins), 4):
            margins.append(None)

    default_margins = [0.0, 0.0, 0.0, 0.0]
    for ii, margin in enumerate([margin_bottom, margin_top, margin_left, margin_right]):
        if margin is not None:
            margins[ii] = margin
        elif margins[ii] is None:
            margins[ii] = default_margins[ii]

    (margin_bottom, margin_top, margin_left, margin_right) = margins

    # Setup a spectrum for the initial FID and the echoes
    height_area = 1-margin_top-margin_bottom        # These two define the working area
    timespan_width = (1-2*pulse_margin-margin_left-margin_right)/(1-pulse_width/4)

    pulse_wr = (pulse_width)*timespan_width         # Refocusing pulse width.
    pulse_we = pulse_wr/2                           # Excitation pulse width

    pulse_te = pulse_we*t_max                       # Pulse duration (time)
    pulse_tr = pulse_wr*t_max

    np_f = 2**14
    np_e = 2**15+1

    t_f = linspace(pulse_te/2.0, tau-pulse_te/2.0, np_f)
    t_e = linspace(-tau+pulse_tr/2, t_max-2*tau, np_e)

    decay_curve = lambda t: exp(-abs(t)/T2s)*exp(-1j*(2*pi*freq*t+exc_phase))
    f_f = decay_curve(t_f)
    f_e = decay_curve(t_e)*exp(-abs(2*tau)/T2)

    # Adjust the phases here.
    #phase_offset = arctan2(imag(f_f[0]), real(f_f[0]))
    phase_offset = pi
    f_e *= exp(1j*(2*(ref_phase-exc_phase)-phase_offset))
    f_f *= exp(-1j*(phase_offset))

    f_e = real(f_e)
    f_f = real(f_f)

    threshhold = 0.0015
    lif = None
    for ii in reversed(range(0, len(f_f))):     # Find the last index above the threshhold
        if abs(f_f[ii]) > threshhold:
            lif = ii
            break

    fi = 0;     lie = -1
    for ii in range(0, len(f_e)):           # Find the first AND last index above the threshhold
        if abs(f_e[ii]) > threshhold:
            fie = ii
            break

    for ii in reversed(range(0, len(f_e))):
        if abs(f_e[ii]) > threshhold:
            lie = ii
            break

    ## Calculate the geometry involved, all in transAxes units.

    span_top = 1-margin_top
    span_bottom = margin_bottom
    span_left = margin_left
    span_right = 1-margin_right

    fid_max = max([max(f_e), max(f_f)])
    fid_min = min([0, min(f_e), min(f_f)])          # Fix the absolute minimum at zero.
    fid_range = fid_max-fid_min

    if baseline_height is None:
        if fid_min < 0:                                 # This is the height of the base line.
            baseline_height = span_top/(1+(fid_range/(echo_h_frac*abs(fid_min))))
        else:
            baseline_height = 0

        baseline_height *= height_area
        baseline_height += span_bottom

    pulse_height = span_top-baseline_height
    echo_height = pulse_height*echo_h_frac

    tau_width = (tau/t_max)*timespan_width          # Convert tau to width in axis units.

    fid_loc = pulse_margin+pulse_we                 # Where the excitation pulse ends
    exc_loc = fid_loc-pulse_we                      # Where the excitation pulse starts

    ref_loc = exc_loc+tau_width+pulse_we/2-pulse_wr/2.0     # Where the refocusing pulse starts
    echo_loc = ref_loc+pulse_wr                     # Where the echo starts.

    ip_span = ref_loc-fid_loc                       # Span between the excitation and refocus pulse
    ap_span = timespan_width-echo_loc               # Span the remainder of the figure

    ## Create the shapes
    # Keep track of all these zorders. Line is on the bottom, then FIDs, then pulses on top
    line_zorder = 1
    fid_zorder = 2
    pulse_zorder = 3

    if ShowBaseline:
        # Generate the pulses and baseline first.
        baseline = Line2D([span_left, span_right],
                          [baseline_height]*2,
                          lw=base_lw,
                          color='k',
                          zorder=line_zorder,
                          clip_on=False,
                          transform=trans)

    if ShowPulses:
        exc_pulse = Rectangle((exc_loc, baseline_height),
                              pulse_we,
                              pulse_height,
                              fc=pulse_fc,
                              ec=pulse_ec,
                              lw=pulse_lw,
                              zorder=pulse_zorder,
                              clip_on=False,
                              transform=trans)

        ref_pulse = Rectangle((ref_loc, baseline_height),
                              pulse_wr,
                              pulse_height,
                              fc=pulse_fc,
                              ec=pulse_ec,
                              lw=pulse_lw,
                              zorder=pulse_zorder,
                              clip_on=False,
                              transform=trans)

        # Now add them to the axis.
        ax.add_patch(exc_pulse)
        ax.add_patch(ref_pulse)
        ax.add_line(baseline)

    ## Now add in the echo decays
    # Convert the data into transAxes and scale them right.
    if ShowEchoes:
        f_f /= fid_range/echo_height                    # Normalize to echo height.
        f_f += baseline_height                          # Offset so the zero point is at the baseline

        f_e /= fid_range/echo_height
        f_e += baseline_height

        t_f = linspace(fid_loc, ref_loc, len(f_f))
        t_e_span = (max(t_e)-min(t_e))*timespan_width/t_max
        t_e = linspace(echo_loc, echo_loc+t_e_span, len(f_e))

        # Plot the FIDs now
        echo_ls = '-'
        echo_line = {
                     'lw':echo_lw,
                     'zorder':fid_zorder,
                     'color':echo_color,
                     'transform':trans,
                     'clip_on':False
                    }

        echo_fill = {
                     'lw' : 0,
                     'zorder':fid_zorder-1,
                     'color':echo_color,
                     'transform':trans,
                     'clip_on':False,
                     'alpha':fill_alpha
                     }

        # Remove the zeros
        threshhold = 0.005

        if lif is not None:
            t_f = t_f[0:lif]
            f_f = f_f[0:lif]

        t_e = t_e[fie:lie]
        f_e = f_e[fie:lie]

        plot(t_f, f_f, echo_ls, **echo_line)
        plot(t_e, f_e, echo_ls, **echo_line)
        fill_between(t_e, baseline_height, f_e, **echo_fill)
        fill_between(t_f, baseline_height, f_f, **echo_fill)

        xlim([0, 1])
        ylim([0, 1])

        ## Annotate the pulses
        if AnnotatePulses:
            pass

        return [baseline_height, exc_loc, ref_loc, pulse_we, pulse_wr, tau_width]

def make_single_echo_T2(n=3, file_loc=None, file_formats=['eps', 'pdf'], figsize=(7,3.5), dpi=150):
    """
    Creates demonstration figure for a spin-echo sequence.

    Provide a file location to file_loc if you want to save it.
    """
    [fig, fnum] = tfs.make_new_fig(fnum=1, close_old=True, return_fig=True,
                                   figsize=figsize, dpi=dpi)

    bh = None
    for ii in range(1, n+1):
        ax = subplot(n, 1, ii)
        bh, = make_spin_echo_constant_time(ax, T2s=0.15, T2=2.5, t_max=5,
                                     tau=0.75*ii,
                                     pulse_lw=1.0,
                                     base_lw=1.0,
                                     echo_lw=1.25,
                                     pulse_fc=thc.bc,
                                     echo_color=thc.rc,
                                     echo_h_frac=1.0,
                                     baseline_height=bh)

    '''
    subplots_adjust(left=0.02, bottom=0.04, right=0.99, top=0.99, hspace=0.15)
    '''
    tight_layout(pad=0.025)
    if file_loc is not None:
        tfs.save_figs(file_loc, formats=file_formats)

    show()
    return [fig, fnum]

"""
Create the figures
"""
fnum = 2
# Show a 3-up single-echo pulse sequence
#[fig, fnum] = make_single_echo_T2(n=3, file_loc='../relaxometry/SingleEchoT2-3up')

# Create a version with arrows and the like.
figsize=(7, 2.5);   dpi = 150
[fig, fnum] = tfs.make_new_fig(fnum=fnum, close_old=True, return_fig = True, figsize=figsize, dpi=dpi)
ax = subplot(1, 1, 1)
ax.axis('off')
tight_layout()

# Show an echo
bh = None
[bh, exc_loc, ref_loc, pulse_we, pulse_wr, tau_width] = make_spin_echo_constant_time(ax,
                                  T2s=0.15, T2 = 2.5, t_max=2.5,
                                  tau=0.85,
                                  pulse_lw=1.0,
                                  base_lw=1.0,
                                  echo_lw=1.25,
                                  pulse_fc=thc.bc,
                                  echo_color=thc.rc,
                                  echo_h_frac=1.0,
                                  baseline_height=bh,
                                  margin_top=0.15,
                                  exc_phase=90,
                                  ref_phase=45)

# Draw lines below it
ta2_ff = 0.00092235*0.375           # Fudge factor
ta1_s = exc_loc+pulse_we/2;     ta1_e = ta1_s + tau_width
ta2_s = ref_loc+pulse_wr/2;     ta2_e = ta2_s + tau_width + ta2_ff
ah = 0.95
arrow_ms = 30;          arrow_lw = 1.25
tau_arrow1 = FancyArrowPatch((ta1_s, ah), (ta1_e, ah), arrowstyle='<|-|>', fc='k',
                             transform=ax.transAxes, mutation_scale=arrow_ms, lw=arrow_lw)
tau_arrow2 = FancyArrowPatch((ta2_s, ah), (ta2_e, ah), arrowstyle='<|-|>', fc='k',
                             transform=ax.transAxes, mutation_scale=arrow_ms, lw=arrow_lw)

dash_y = [bh, 1.0]
dash_kwargs = {
    'lw':1.2,
    'color':'k',
    'clip_on':False,
    'dashes':[7.5,5,7.5,5],
    'transform':ax.transAxes
}
dashs = Line2D([ta1_s, ta1_s], dash_y, zorder=4, **dash_kwargs)
dashm = Line2D([ta2_s, ta2_s], dash_y, zorder=4, **dash_kwargs)
dashe = Line2D([ta2_e, ta2_e], dash_y, zorder=-1, **dash_kwargs)

ax.add_patch(tau_arrow1)
ax.add_patch(tau_arrow2)
ax.add_line(dashs)
ax.add_line(dashm)
ax.add_line(dashe)

# Annotate the arrows
txt1x = mean([ta1_s, ta1_e]);       txt2x = mean([ta2_s, ta2_e])
txth = ah-0.025
ptxth = bh-0.03

txtkwargs = {
    'fontsize':14,
    'va':'top',
    'ha':'center',
    'transform':ax.transAxes
}

text(txt1x, txth, r'$\tau$',**txtkwargs)
text(txt2x, txth, r'$\tau$', **txtkwargs)
text(exc_loc+pulse_we/2, ptxth, r'$\frac{\pi}{2}$', **txtkwargs)
text(ref_loc+pulse_wr/2, ptxth, r'$\pi$', **txtkwargs)

try:
    SaveFigs
except:
    SaveFigs = True

tfs.save_figs('../relaxometry/SingleEchoT2', formats=['pdf', 'eps'])

show()

reason: from numpy,import scipy


text:
# -*- coding: utf-8 -*-
"""Common test case for all message based resources.

"""
import gc
import logging
import time
import unittest

from pyvisa import constants, errors

from .resource_utils import ResourceTestCase

try:
    import numpy as np
except ImportError:
    np = None

class EventHandler:
    """Event handler.

    """

    def __init__(self):
        self.event_success = False
        self.srq_success = False
        self.io_completed = False

    def handle_event(self, instr, event_type, event, handle=None):
        """Event handler

        """
        if (event_type == constants.EventType.service_request):
            self.event_success = True
            self.srq_success = True
            return 0
        if (event_type == constants.EventType.io_completion):
            self.event_success = True
            self.io_completed = True
            return 0
        else:
            self.event_success = True
            return 0

class MessagebasedResourceTestCase(ResourceTestCase):
    """Base test case for all message based resources.

    """

    #: Type of resource being tested in this test case.
    #: See RESOURCE_ADDRESSES in the __init__.py file of this package for
    #: acceptable values
    RESOURCE_TYPE = ''

    # Any test involving communication involve to first write to glider the
    # data then request it to send it back

    def setUp(self):
        """Create a resource using the address matching the type.

        """
        super().setUp()
        self.instr.write_termination = '\n'
        self.instr.read_termination = '\n'
        self.instr.timeout = 100

    def test_encoding(self):
        """Tets setting the string encoding.

        """
        self.assertEqual(self.instr.encoding, 'ascii')
        self.instr.encoding = 'utf-8'

        with self.assertRaises(LookupError):
            self.instr.encoding = 'test'

    def test_termchars(self):
        """Test modifying the termchars.

        """
        # Write termination
        self.instr.write_termination = "\r\n"
        self.assertEqual(self.instr.write_termination, "\r\n")

        self.instr.read_termination = "\r\0"
        self.assertEqual(self.instr.read_termination, "\r\0")
        # self.assertEqual(self.instr.VI_ATTR_TERMCHAR, ord("\0"))
        self.assertEqual(
            self.instr.get_visa_attribute(constants.VI_ATTR_TERMCHAR),
            ord("\0")
        )
        # self.assertTrue(self.instr.VI_ATTR_TERMCHAR_EN)
        self.assertTrue(
            self.instr.get_visa_attribute(constants.VI_ATTR_TERMCHAR_EN),
        )

        # Disable read termination
        self.instr.read_termination = None
        # self.assertEqual(self.instr.VI_ATTR_TERMCHAR, ord("\n"))
        self.assertEqual(
            self.instr.get_visa_attribute(constants.VI_ATTR_TERMCHAR),
            ord("\n")
        )
        # self.assertFalse(self.instr.VI_ATTR_TERMCHAR_EN)
        self.assertFalse(
            self.instr.get_visa_attribute(constants.VI_ATTR_TERMCHAR_EN),
        )

        # Ban repeated term chars
        with self.assertRaises(ValueError):
            self.instr.read_termination = "\n\n"

    def test_write_raw_read_bytes(self):
        """Test writing raw data and reading a specific number of bytes.

        """
        # Reading all bytes at once
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        count = self.instr.write_raw(b"SEND\n")
        self.assertEqual(count, 5)
        self.instr.flush(constants.VI_READ_BUF)
        self.assertEqual(self.instr.read_bytes(5, chunk_size=2), b"test\n")

        # Reading one byte at a time
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        for ch in b"test\n":
            self.assertEqual(self.instr.read_bytes(1), ch.to_bytes(1, 'little'))

        # Breaking on termchar
        self.instr.read_termination = "\r"
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"te\rst\r\n")
        self.instr.write_raw(b"SEND\n")
        self.assertEqual(self.instr.read_bytes(100, break_on_termchar=True), b"te\r")
        self.assertEqual(self.instr.read_bytes(100, break_on_termchar=True), b"st\r")
        self.assertEqual(self.instr.read_bytes(1), b"\n")

    def test_handling_exception_in_read_bytes(self):
        """Test handling exception in read_bytes (monkeypatching)

        """
        def false_read(session, size):
            raise errors.VisaIOError(constants.VI_ERROR_ABORT)

        read = self.instr.visalib.read
        self.instr.visalib.read = false_read
        with self.assertLogs(level="DEBUG") as cm:
            try:
                self.instr.read_bytes(1)
            except errors.VisaIOError:
                pass
            finally:
             self.instr.visalib.read = read
        self.assertIn("- exception while reading:", cm.output[1])

    def test_write_raw_read_raw(self):
        """Test writing raw data and reading an answer.

        """
        self.instr.write_raw(b"RECEIVE\n")
        self.instr.write_raw(b"test\n")
        self.instr.write_raw(b"SEND\n")
        self.assertEqual(self.instr.read_raw(size=2), b"test\n")

    def test_write_read(self):
        """Test writing and reading.

        """
        self.instr.write_termination = "\n"
        self.instr.read_termination = "\r\n"
        self.instr.write("RECEIVE")
        with self.assertWarns(UserWarning):
            self.instr.write("test\r\n")
        count = self.instr.write("SEND")
        self.assertEqual(count, 5)
        self.assertEqual(self.instr.read(), "test")

        # Missing termination chars
        self.instr.read_termination = "\r\n"
        self.instr.write("RECEIVE")
        self.instr.write("test")
        self.instr.write("SEND")
        with self.assertWarns(Warning):
            self.assertEqual(self.instr.read(), "test\n")

        # Dynamic termination
        self.instr.write_termination = "\r"
        self.instr.write("RECEIVE\n", termination=False)
        self.instr.write("test\r", termination="\n")
        self.instr.write("SEND", termination="\n")
        self.assertEqual(self.instr.read(termination="\r"), "test")

        # Test query
        self.instr.write_termination = "\n"
        self.instr.write("RECEIVE")
        self.instr.write("test\r")
        tic = time.time()
        self.assertEqual(self.instr.query("SEND", delay=0.5), "test")
        self.assertGreater(time.time()-tic, 0.5)

        # Test handling repeated term char
        self.instr.read_termination = "\n"
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with self.assertWarns(Warning):
                self.instr.write("test\r", termination=char)
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            self.assertEqual(self.instr.read(), "test\r\r")

        # XXX not sure how to test encoding

    def test_handling_exception_in_read_raw(self):
        """Test handling exception in read_bytes (monkeypatching)

        """
        def false_read(session, size):
            raise errors.VisaIOError(constants.VI_ERROR_ABORT)

        read = self.instr.visalib.read
        self.instr.visalib.read = false_read
        with self.assertLogs(level="DEBUG") as cm:
            try:
                self.instr.read()
            except errors.VisaIOError:
                pass
            finally:
             self.instr.visalib.read = read

    def test_write_ascii_values(self):
        """Test writing ascii values.

        """
        # Standard separator
        l = [1, 2, 3, 4, 5]
        self.instr.write("RECEIVE")
        count = self.instr.write_ascii_values("", l, "d")
        self.assertEqual(count, 10)
        self.instr.write("SEND")
        self.assertEqual(self.instr.read(), "1,2,3,4,5")

        # Non standard separator and termination
        self.instr.write_termination = "\r"
        self.instr.write("RECEIVE", termination="\n")
        self.instr.write_ascii_values("", l, "d", separator=";",
                                      termination=False)
        self.instr.write("", termination="\n")
        self.instr.write("SEND", termination="\n")
        self.assertEqual(self.instr.read(), "1;2;3;4;5")

        # Test handling repeated term char
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with self.assertWarns(Warning):
                l = [1, 2, 3, 4, 5]
                self.instr.write_ascii_values("\r", l, "s", separator=";",
                                              termination=char)
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            self.assertEqual(self.instr.read(), "\r1;2;3;4;5\r")

    def test_write_binary_values(self):
        """Test writing binary data.

        """
        for hfmt, prefix in zip(("ieee", "hp", "empty"),
                                (b"#210", b"#A\n\x00", b"")):
            self.subTest(hfmt)

            l = [1, 2, 3, 4, 5]
            self.instr.write_termination = "\n"
            self.instr.write("RECEIVE")
            count = self.instr.write_binary_values("", l, "h", header_fmt=hfmt)
            # Each interger encoded as h uses 2 bytes
            self.assertEqual(count, len(prefix) + 10 + 1)
            self.instr.write("SEND")
            self.assertEqual(self.instr.read_bytes(11 + len(prefix)),
                             prefix + b"\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\n")

            if hfmt == "hp":
                prefix = prefix[0:2] + prefix[-2::][::-1]
            self.instr.write_termination = "\r"
            self.instr.write("RECEIVE", termination="\n")
            self.instr.write_binary_values("", l, "h", is_big_endian=True,
                                           termination=False, header_fmt=hfmt)
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            self.assertEqual(self.instr.read_bytes(11 + len(prefix)),
                             prefix + b"\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\n")

        # Test handling repeated term char
        self.subTest("Repeated term char")
        for char in ("\r", None):
            self.instr.write_termination = "\n" if char else "\r"
            self.instr.write("RECEIVE", termination="\n")
            with self.assertWarns(Warning):
                self.instr.write_binary_values("\r", l, "h", header_fmt=hfmt,
                                               termination=char)
            self.instr.write("", termination="\n")
            self.instr.write("SEND", termination="\n")
            self.assertEqual(self.instr.read(),
                             "\r\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\r")

        # Wrong header format
        with self.assertRaises(ValueError):
            self.instr.write_binary_values("", l, "h", header_fmt="zxz")

    def test_read_ascii_values(self):
        """Test reading ascii values.

        """
        # Standard separator
        self.instr.write("RECEIVE")
        l = [1, 2, 3, 4, 5]
        self.instr.write("1,2,3,4,5")
        self.instr.write("SEND")
        values = self.instr.read_ascii_values()
        self.assertIs(type(values[0]), float)
        self.assertEqual(values, [1.0, 2.0, 3.0, 4.0, 5.0])

        # Non standard separator and termination
        self.instr.write("RECEIVE")
        self.instr.write("1;2;3;4;5")
        tic = time.time()
        values = self.instr.query_ascii_values("SEND", converter='d',
                                               separator=';', delay=0.5)
        self.assertGreater(time.time()-tic, 0.5)
        self.assertIs(type(values[0]), int)
        self.assertEqual(values, [1, 2, 3, 4, 5])

        # Numpy container
        if np:
            self.instr.write("RECEIVE")
            self.instr.write("1,2,3,4,5")
            self.instr.write("SEND")
            values = self.instr.read_ascii_values(container=np.array)
            expected = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
            self.assertIs(values.dtype, expected.dtype)
            np.testing.assert_array_equal(values, expected)

    def test_read_binary_values(self):
        """Test reading binary data.

        """
        # XXX test handling binary decoding issue (troublesome)
        self.instr.read_termination = '\r'
        # 3328 in binary short is \x00\r this way we can interrupt the
        # transmission midway to test some corner cases
        data = [1, 2, 3328, 3, 4, 5, 6, 7]
        for hfmt in ("ieee", "hp"):
            print(hfmt)
            self.subTest(hfmt)

            self.instr.write("RECEIVE")
            self.instr.write_binary_values("", data, "h", header_fmt=hfmt,
                                           termination='\r\n')
            self.instr.write("SEND")
            new = self.instr.read_binary_values(datatype='h',
                                                is_big_endian=False,
                                                header_fmt=hfmt,
                                                expect_termination=True,
                                                chunk_size=8,
                                                )
            self.instr.read_bytes(1)
            self.assertEqual(data, new)

            self.instr.write("RECEIVE")
            self.instr.write_binary_values("", data, "h", header_fmt=hfmt,
                                           is_big_endian=True)

            new = self.instr.query_binary_values("SEND",
                                                 datatype='h',
                                                 header_fmt=hfmt,
                                                 is_big_endian=True,
                                                 expect_termination=False,
                                                 chunk_size=8,
                                                 container= np.array if np else list,
                                                 )
            self.instr.read_bytes(1)
            if np:
                np.testing.assert_array_equal(new,
                                              np.array(data, dtype=np.int16))
            else:
                self.assertEqual(data, new)

    def test_read_query_binary_values_invalid_header(self):
        """Test we properly handle an invalid header.

        """
        data = [1, 2, 3328, 3, 4, 5, 6, 7]
        self.instr.write("RECEIVE")
        self.instr.write_binary_values("", data, "h", header_fmt="ieee",
                                        is_big_endian=True)
        self.instr.write("SEND")
        with self.assertRaises(ValueError) as e:
            self.instr.read_binary_values(datatype='h',
                                          is_big_endian=False,
                                          header_fmt="invalid",
                                          expect_termination=True,
                                          chunk_size=8,
                                          )

        self.instr.write("RECEIVE")
        self.instr.write_binary_values("", data, "h", header_fmt="ieee",
                                        is_big_endian=True)
        with self.assertRaises(ValueError) as e:
            self.instr.query_binary_values("*IDN",
                                           datatype='h',
                                           is_big_endian=False,
                                           header_fmt="invalid",
                                           expect_termination=True,
                                           chunk_size=8,
                                           )

    def test_handling_malformed_binary(self):
        """

        """
        pass

    def test_read_binary_values_unreported_length(self):
        """Test reading binary data.

        """
        self.instr.read_termination = '\r'
        # 3328 in binary short is \x00\r this way we can interrupt the
        # transmission midway to test some corner cases
        data = [1, 2, 3328, 3, 4, 5]
        for hfmt, header in zip(("ieee", "hp", "empty"),
                                ("#10", "#A\x00\x00", "")):
            print(hfmt)
            self.subTest(hfmt)

            self.instr.write("RECEIVE")
            self.instr.write(header + "\x01\x00\x02\x00\x00\r\x03\x00\x04\x00\x05\x00",
                             termination='\r\n')
            self.instr.write("SEND")
            new = self.instr.read_binary_values(datatype='h',
                                                is_big_endian=False,
                                                header_fmt=hfmt,
                                                expect_termination=True,
                                                chunk_size=6,
                                                data_points=6)
            self.instr.read_bytes(1)
            self.assertEqual(data, new)

            self.instr.write("RECEIVE")
            self.instr.write(header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
                             termination='\r\n')
            new = self.instr.query_binary_values("SEND",
                                                 datatype='h',
                                                 header_fmt=hfmt,
                                                 is_big_endian=True,
                                                 expect_termination=False,
                                                 chunk_size=6,
                                                 container= np.array if np else list,
                                                 data_points=6)
            self.instr.read_bytes(1)
            if np:
                np.testing.assert_array_equal(new,
                                              np.array(data, dtype=np.int16))
            else:
                self.assertEqual(data, new)

            # Check we do error on unreported/unspecified length
            self.instr.write("RECEIVE")
            self.instr.write(header + "\x01\x00\x02\x00\x00\r\x03\x00\x04\x00\x05\x00",
                             termination='\r\n')
            self.instr.write("SEND")
            with self.assertRaises(ValueError):
                self.instr.read_binary_values(datatype='h',
                                              is_big_endian=False,
                                              header_fmt=hfmt,
                                              expect_termination=True,
                                              chunk_size=6,
                                              )

    def test_delay_in_query_ascii(self):
        """Test handling of the delay argument in query_ascii_values.

        """
        # Test using the instrument wide delay
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        l = [1, 2, 3, 4, 5]
        self.instr.write("1,2,3,4,5")
        tic = time.perf_counter()
        values = self.instr.query_ascii_values("SEND")
        self.assertGreater(time.perf_counter() - tic, 1.0)
        self.assertIs(type(values[0]), float)
        self.assertEqual(values, [1.0, 2.0, 3.0, 4.0, 5.0])

        # Test specifying the delay
        self.instr.query_delay = 0.0
        self.instr.write("RECEIVE")
        l = [1, 2, 3, 4, 5]
        self.instr.write("1,2,3,4,5")
        tic = time.perf_counter()
        values = self.instr.query_ascii_values("SEND", delay=1.0)
        self.assertGreater(time.perf_counter() - tic, 1.0)
        self.assertIs(type(values[0]), float)
        self.assertEqual(values, [1.0, 2.0, 3.0, 4.0, 5.0])

    def test_delay_in_query_binary(self):
        """Test handling of the delay argument in query_ascii_values.

        """
        header = "#10"
        data = [1, 2, 3328, 3, 4, 5]
        # Test using the instrument wide delay
        self.instr.query_delay = 1.0
        self.instr.write("RECEIVE")
        self.instr.write(header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
                         termination='\r\n')
        tic = time.perf_counter()
        new = self.instr.query_binary_values("SEND",
                                             datatype='h',
                                             header_fmt="ieee",
                                             is_big_endian=True,
                                             expect_termination=False,
                                             chunk_size=6,
                                             data_points=6)

        self.assertGreater(time.perf_counter() - tic, 1.0)
        self.assertEqual(data, new)

        # Test specifying the delay
        self.instr.query_delay = 0.0
        self.instr.write("RECEIVE")
        self.instr.write(header + "\x00\x01\x00\x02\r\x00\x00\x03\x00\x04\x00\x05",
                         termination='\r\n')
        tic = time.perf_counter()
        new = self.instr.query_binary_values("SEND",
                                             datatype='h',
                                             header_fmt="ieee",
                                             is_big_endian=True,
                                             expect_termination=False,
                                             chunk_size=6,
                                             data_points=6,
                                             delay=1.0)

        self.assertGreater(time.perf_counter() - tic, 1.0)
        self.assertEqual(data, new)

    def test_stb(self):
        """Test reading the status byte.

        """
        self.assertTrue(0 <= self.instr.stb <= 256)
        self.assertTrue(0 <= self.instr.read_stb() <= 256)

    def test_wait_on_event(self):
        """Test waiting on a VISA event.

        """
        event_type = constants.EventType.service_request
        event_mech = constants.EventMechanism.queue
        wait_time = 2000 # set time that program waits to receive event
        self.instr.enable_event(event_type, event_mech, None)
        self.instr.write("RCVSLOWSRQ")
        self.instr.write("1")
        self.instr.write("SENDSLOWSRQ")
        try:
            response = self.instr.wait_on_event(event_type, wait_time)
        finally:
            self.instr.disable_event(event_type, event_mech)
        self.assertFalse(response.timed_out)
        self.assertEqual(response.event_type, constants.EventType.service_request)
        self.assertEqual(self.instr.read(), "1")

    def test_wait_on_event_timeout(self):
        """Test waiting on a VISA event.

        """
        event_type = constants.EventType.service_request
        event_mech = constants.EventMechanism.queue
        # Emit a clear to avoid dealing with previous requests
        self.instr.clear()
        self.instr.enable_event(event_type, event_mech, None)
        try:
            response = self.instr.wait_on_event(event_type, 10,
                                                capture_timeout=True)
        finally:
            self.instr.disable_event(event_type, event_mech)
        self.assertTrue(response.timed_out)
        self.assertEqual(response.event_type, event_type)

        with self.assertRaises(errors.VisaIOError):
            self.instr.enable_event(event_type, event_mech, None)
            try:
                response = self.instr.wait_on_event(event_type, 10)
            finally:
                self.instr.disable_event(event_type, event_mech)

    def test_managing_visa_handler(self):
        """Test using visa handlers.

        """
        def _test(handle):
            handler = EventHandler()
            event_type = constants.EventType.service_request
            event_mech = constants.EventMechanism.handler
            user_handle = self.instr.install_handler(event_type, handler.handle_event,
                                                    user_handle=handle)
            self.instr.enable_event(event_type, event_mech, None)
            self.instr.write("RCVSLOWSRQ")
            self.instr.write("1")
            self.instr.write("SENDSLOWSRQ")

            try:
                t1 = time.time()
                while not handler.event_success:
                    if (time.time() - t1) > 2:
                        break
                    time.sleep(0.1)
            finally:
                self.instr.disable_event(event_type, event_mech)
                self.instr.uninstall_handler(event_type, handler.handle_event,
                                             user_handle)

            self.assertTrue(handler.srq_success)
            self.assertEqual(self.instr.read(), "1")

        for handle in (1, 1.0, "1", [1]):
            _test(handle)

    def test_handling_invalid_handler(self):
        """Test handling an error related to a wrong handler type.

        """
        with self.assertRaises(errors.VisaTypeError):
            event_type = constants.EventType.service_request
            event_mech = constants.EventMechanism.handler
            self.instr.install_handler(event_type, 1, object())

    def test_uninstalling_missing_visa_handler(self):
        """Test uninstalling a visa handler that was not registered.

        """
        handler1 = EventHandler()
        handler2 = EventHandler()
        event_type = constants.EventType.service_request
        event_mech = constants.EventMechanism.handler
        self.instr.install_handler(event_type, handler1.handle_event)
        with self.assertRaises(errors.UnknownHandler):
            self.instr.uninstall_handler(event_type, handler2.handle_event)

        self.instr.uninstall_handler(event_type, handler1.handle_event)

        with self.assertRaises(errors.UnknownHandler):
            self.instr.uninstall_handler(event_type, handler2.handle_event)

    def test_handler_clean_up_on_resource_del(self):
        """Test that handlers are properly cleaned when a resource is deleted.

        """
        handler = EventHandler()
        event_type = constants.EventType.service_request
        event_mech = constants.EventMechanism.handler
        self.instr.install_handler(event_type, handler.handle_event)

        self.instr = None
        gc.collect()
        self.assertFalse(self.rm.visalib.handlers)

    def test_uninstall_all_handlers(self):
        """Test uninstall all handlers from all sessions.

        """
        handler = EventHandler()
        event_type = constants.EventType.service_request
        event_mech = constants.EventMechanism.handler
        self.instr.install_handler(event_type, handler.handle_event)

        self.rm.visalib.uninstall_all_visa_handlers(None)
        self.assertFalse(self.rm.visalib.handlers)

    def test_shared_locking(self):
        """Test locking/unlocking a resource.

        """
        instr2 = self.rm.open_resource(str(self.rname))
        instr3 = self.rm.open_resource(str(self.rname))

        key = self.instr.lock()
        instr2.lock(requested_key=key)

        self.assertTrue(self.instr.query("*IDN?"))
        self.assertTrue(instr2.query("*IDN?"))
        with self.assertRaises(errors.VisaIOError):
            instr3.query("*IDN?")

        # Share the lock for a limited time
        with instr3.lock_context(requested_key=key) as key2:
            self.assertTrue(instr3.query("*IDN?"))

        # Stop sharing the lock
        instr2.unlock()

        with self.assertRaises(errors.VisaIOError):
            instr2.query("*IDN?")
        with self.assertRaises(errors.VisaIOError):
            instr3.query("*IDN?")

        self.instr.unlock()

        self.assertTrue(instr3.query("*IDN?"))

    def test_exclusive_locking(self):
        """Test locking/unlocking a resource.

        """
        instr2 = self.rm.open_resource(str(self.rname))

        self.instr.lock_excl()
        with self.assertRaises(errors.VisaIOError):
            instr2.query("*IDN?")

        self.instr.unlock()

        self.assertTrue(instr2.query("*IDN?"))

        # Share the lock for a limited time
        with self.instr.lock_context(requested_key='exclusive') as key:
            self.assertIsNone(key)
            with self.assertRaises(errors.VisaIOError):
                instr2.query("*IDN?")

max_stars_repo_name: KOLANICH/pyvisa

max_issues_repo_name: daytonb/pyvisa

max_forks_repo_name: daytonb/pyvisa


text:
import os

import pytest
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Activation, Flatten, Dense

from jina.executors.metas import get_default_metas
from jina.executors import BaseExecutor
from jina.excepts import PretrainedModelFileDoesNotExist

from .. import CustomKerasImageEncoder

class TestNet:
    def __init__(self):
        self.model = None
        self.input_shape = (224, 224, 3)
        self.conv = Conv2D(32, (3, 3), padding='same', name='conv1', input_shape=self.input_shape)
        self.activation_relu = Activation('relu')
        self.flatten = Flatten()
        self.dense = Dense(10, name='dense')
        self.activation_softmax = Activation('softmax')

    def create_model(self):
        self.model = Sequential()
        self.model.add(self.conv)
        self.model.add(self.activation_relu)
        self.model.add(self.flatten)
        self.model.add(self.dense)
        self.model.add(self.activation_softmax)
        return self.model

@pytest.fixture(scope='function', autouse=True)
def metas(tmpdir):
    metas = get_default_metas()
    if 'JINA_TEST_GPU' in os.environ:
        metas['on_gpu'] = True
    metas['workspace'] = tmpdir
    yield metas

def test_encoder(metas):
    path = os.path.join(metas['workspace'], 'model.pth')
    model = TestNet().create_model()
    model.save(path)
    return CustomKerasImageEncoder(channel_axis=1, model_path=path, layer_name='dense', metas=metas)

def default_encoder(metas):
    return CustomKerasImageEncoder(metas=metas)

def test_encoding_results(metas):
    target_output_dim = 10
    input_dim = 224
    encoder = test_encoder(metas)
    test_data = np.random.rand(2, 3, input_dim, input_dim).astype('float32')
    encoded_data = encoder.encode(test_data)
    assert encoded_data.shape == (2, target_output_dim)

def test_encoding_results_default(metas):
    target_output_dim = 1280
    input_dim = 224
    encoder = default_encoder(metas)
    test_data = np.random.rand(2, 3, input_dim, input_dim).astype('float32')
    encoded_data = encoder.encode(test_data)
    assert encoded_data.shape == (2, target_output_dim)

def test_save_and_load(metas):
    input_dim = 224
    encoder = test_encoder(metas)
    test_data = np.random.rand(2, 3, input_dim, input_dim).astype('float32')
    encoded_data_control = encoder.encode(test_data)
    encoder.touch()
    encoder.save()
    assert os.path.exists(encoder.save_abspath)
    encoder_loaded = BaseExecutor.load(encoder.save_abspath)
    encoded_data_test = encoder_loaded.encode(test_data)
    assert encoder_loaded.channel_axis == encoder.channel_axis
    np.testing.assert_array_equal(encoded_data_control, encoded_data_test)

def test_raise_exception():
    with pytest.raises(PretrainedModelFileDoesNotExist):
        CustomKerasImageEncoder(model_path=None)

max_stars_repo_name: tadej-redstone/jina-hub

max_issues_repo_name: tadej-redstone/jina-hub

max_forks_repo_name: tadej-redstone/jina-hub