text:
import numpy as np
np.warnings.filterwarnings('ignore')
import pathlib
import subprocess
import os
import collections
import shutil
import quaternion
import operator
import glob
import csv
import re
import CSVHelper
import SE3
import JSONHelper
import argparse
np.seterr(all='raise')
import argparse

# params
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True, choices=["scannet" ],help="choose dataset")
parser.add_argument('--projectdir', required=True, help="project directory")
opt = parser.parse_args()

# get top8 (most frequent) classes from annotations.
def get_top8_classes_scannet():
    top = collections.defaultdict(lambda : "other")
    top["03211117"] = "display"
    top["04379243"] = "table"
    top["02808440"] = "bathtub"
    top["02747177"] = "trashbin"
    top["04256520"] = "sofa"
    top["03001627"] = "chair"
    top["02933112"] = "cabinet"
    top["02871439"] = "bookshelf"
    return top

# helper function to calculate difference between two quaternions
def calc_rotation_diff(q, q00):
    rotation_dot = np.dot(quaternion.as_float_array(q00), quaternion.as_float_array(q))
    rotation_dot_abs = np.abs(rotation_dot)
    try:
        error_rotation_rad = 2 * np.arccos(rotation_dot_abs)
    except:
        return 0.0
    error_rotation_rad = 2 * np.arccos(rotation_dot_abs)
    error_rotation = np.rad2deg(error_rotation_rad)
    return error_rotation

def evaluate(projectdir, filename_cad_appearance, filename_annotations):
    appearances_cad = JSONHelper.read(filename_cad_appearance)

    benchmark_per_scan = collections.defaultdict(lambda : collections.defaultdict(lambda : 0)) # <-- benchmark_per_scan
    benchmark_per_class = collections.defaultdict(lambda : collections.defaultdict(lambda : 0)) # <-- benchmark_per_class
    if opt.dataset == "scannet":
        catid2catname = get_top8_classes_scannet()

    groundtruth = {}
    cad2info = {}
    idscan2trs = {}

    testscenes = [os.path.basename(f).split(".")[0] for f in glob.glob(projectdir + "/*.csv")]

    testscenes_gt = []
    for r in JSONHelper.read(filename_annotations):
        id_scan = r["id_scan"]
        # NOTE: remove this
        if id_scan not in testscenes:
            continue
        # <-
        testscenes_gt.append(id_scan)

        idscan2trs[id_scan] = r["trs"]

        for model in r["aligned_models"]:
            id_cad = model["id_cad"]
            catid_cad = model["catid_cad"]
            catname_cad = catid2catname[catid_cad]
            model["n_total"] = len(r["aligned_models"])
            groundtruth.setdefault((id_scan, catid_cad),[]).append(model)
            cad2info[(catid_cad, id_cad)] = {"sym" : model["sym"], "catname" : catname_cad}

            benchmark_per_class[catname_cad]["n_total"] += 1
            benchmark_per_scan[id_scan]["n_total"] += 1

    projectname = os.path.basename(os.path.normpath(projectdir))

    # Iterate through your alignments
    counter = 0
    for file0 in glob.glob(projectdir + "/*.csv"):
        alignments = CSVHelper.read(file0)
        id_scan = os.path.basename(file0.rsplit(".", 1)[0])
        if id_scan not in testscenes_gt:
            continue
        benchmark_per_scan[id_scan]["seen"] = 1

        appearance_counter = {}

        for alignment in alignments: # <- multiple alignments of same object in scene

            # -> read from .csv file
            catid_cad = alignment[0]
            id_cad = alignment[1]
            cadkey = catid_cad + "_" + id_cad
            #import pdb; pdb.set_trace()
            if cadkey in appearances_cad[id_scan]:
                n_appearances_allowed = appearances_cad[id_scan][cadkey] # maximum number of appearances allowed
            else:
                n_appearances_allowed = 0

            appearance_counter.setdefault(cadkey, 0)
            if appearance_counter[cadkey] >= n_appearances_allowed:
                continue
            appearance_counter[cadkey] += 1

            catname_cad = cad2info[(catid_cad, id_cad)]["catname"]
            sym = cad2info[(catid_cad, id_cad)]["sym"]
            t = np.asarray(alignment[2:5], dtype=np.float64)
            q0 = np.asarray(alignment[5:9], dtype=np.float64)
            q = np.quaternion(q0[0], q0[1], q0[2], q0[3])
            s = np.asarray(alignment[9:12], dtype=np.float64)
            # <-

            key = (id_scan, catid_cad) # <-- key to query the correct groundtruth models_old
            for idx, model_gt in enumerate(groundtruth[key]):

                is_same_class = model_gt["catid_cad"] == catid_cad # <-- is always true (because the way the 'groundtruth' was created
                if is_same_class: # <-- proceed only if candidate-model and gt-model are in same class

                    Mscan = SE3.compose_mat4(idscan2trs[id_scan]["translation"], idscan2trs[id_scan]["rotation"], idscan2trs[id_scan]["scale"])
                    Mcad = SE3.compose_mat4(model_gt["trs"]["translation"], model_gt["trs"]["rotation"], model_gt["trs"]["scale"], -np.array(model_gt["center"]))

                    t_gt, q_gt, s_gt = SE3.decompose_mat4(np.dot(np.linalg.inv(Mscan), Mcad))

                    error_translation = np.linalg.norm(t - t_gt, ord=2)
                    error_scale = 100.0*np.abs(np.mean(s/s_gt) - 1)

                    # --> resolve symmetry
                    if sym == "__SYM_ROTATE_UP_2":
                        m = 2
                        tmp = [calc_rotation_diff(q, q_gt*quaternion.from_rotation_vector([0, (i*2.0/m)*np.pi, 0])) for i in range(m)]
                        error_rotation = np.min(tmp)
                    elif sym == "__SYM_ROTATE_UP_4":
                        m = 4
                        tmp = [calc_rotation_diff(q, q_gt*quaternion.from_rotation_vector([0, (i*2.0/m)*np.pi, 0])) for i in range(m)]
                        error_rotation = np.min(tmp)
                    elif sym == "__SYM_ROTATE_UP_INF":
                        m = 36
                        tmp = [calc_rotation_diff(q, q_gt*quaternion.from_rotation_vector([0, (i*2.0/m)*np.pi, 0])) for i in range(m)]
                        error_rotation = np.min(tmp)
                    else:
                        error_rotation = calc_rotation_diff(q, q_gt)

                    # -> define Thresholds
                    threshold_translation = 0.2 # <-- in meter
                    threshold_rotation = 20 # <-- in deg
                    threshold_scale = 20 # <-- in %
                    # <-

                    is_valid_transformation = error_translation <= threshold_translation and error_rotation <= threshold_rotation and error_scale <= threshold_scale

                    counter += 1
                    if is_valid_transformation:
                        benchmark_per_scan[id_scan]["n_good"] += 1
                        benchmark_per_class[catname_cad]["n_good"] += 1
                        del groundtruth[key][idx]
                        break

    print("***********")
    benchmark_per_scan = sorted(benchmark_per_scan.items(), key=lambda x: x[1]["n_good"], reverse=True)
    total_accuracy = {"n_good" : 0, "n_total" : 0, "n_scans" : 0}
    for k, v in benchmark_per_scan:
        if "seen" in v:
            total_accuracy["n_good"] += v["n_good"]
            total_accuracy["n_total"] += v["n_total"]
            total_accuracy["n_scans"] += 1
            print("id-scan: {:>20s} \t n-cads-positive: {:>4d} \t n-cads-total: {:>4d} \t accuracy: {:>4.4f}".format(k,  v["n_good"], v["n_total"], float(v["n_good"])/v["n_total"]))
    instance_mean_accuracy = float(total_accuracy["n_good"])/total_accuracy["n_total"]
    print("instance-mean-accuracy: {:>4.4f} \t n-cads-positive: {:>4d} \t n-cads-total: {:>4d} \t n-total-scans: {:>4d}".format(instance_mean_accuracy, total_accuracy["n_good"], total_accuracy["n_total"], total_accuracy["n_scans"]))

    print("*********** PER CLASS **************************")

    accuracy_per_class = {}
    for k,v in benchmark_per_class.items():
        accuracy_per_class[k] = float(v["n_good"])/v["n_total"]
        print("category-name: {:>20s} \t n-cads-positive: {:>4d} \t n-cads-total: {:>4d} \t accuracy: {:>4.4f}".format(k,  v["n_good"], v["n_total"], float(v["n_good"])/v["n_total"]))

    class_mean_accuracy = np.mean([ v for k,v in accuracy_per_class.items()])
    print("class-mean-accuracy: {:>4.4f}".format(class_mean_accuracy))
    return instance_mean_accuracy, class_mean_accuracy

if __name__ == "__main__":
    evaluate(opt.projectdir, "./" + opt.dataset + "_cad_appearances.json", "./" + opt.dataset + "_full_annotations.json")
    #evaluate(opt.projectdir, "./cad_appearances_hidden_testset.json", "./full_annotations_hidden_testset.json")


text:
from __future__ import print_function
from conv_qsar.utils.parsing import input_to_bool
from conv_qsar.utils.parse_cfg import read_config
import conv_qsar.utils.reset_layers as reset_layers
import rdkit.Chem as Chem
import numpy as np
import datetime
import json
import sys
import os
import time

# from conv_qsar.main.core import build_model, train_model, save_model
from conv_qsar.main.test import test_model, test_embeddings_demo
from conv_qsar.main.data import get_data_full

if __name__ == '__main__':
	if len(sys.argv) < 2:
		print('Usage: {} "settings.cfg"'.format(sys.argv[0]))
		quit(1)

	# Load settings
	try:
		config = read_config(sys.argv[1])
	except:
		print('Could not read config file {}'.format(sys.argv[1]))
		quit(1)

	# Get model label
	try:
		fpath = config['IO']['model_fpath']
	except KeyError:
		print('Must specify model_fpath in IO in config')
		quit(1)

	###################################################################################
	### BUILD MODEL
	###################################################################################

	print('...building model')
	try:
		kwargs = config['ARCHITECTURE']
		if 'kernel' not in kwargs:
			kwargs['kernel'] = 'rbf' # default

		from sklearn.svm import SVR
		if kwargs['kernel'] in ['linear', 'rbf', 'poly']:
			model = SVR(kernel = kwargs['kernel'])
		elif kwargs['kernel'] in ['tanimoto']:
			def kernel(xa, xb):
				m = xa.shape[0]
				n = xb.shape[0]
				xa = xa.astype(bool)
				xb = xb.astype(bool)
				score = np.zeros((m, n))
				for i in range(m):
					for j in range(n):
						score[i, j] = float(np.sum(np.logical_and(xa[i], xb[j])))  / np.sum(np.logical_or(xa[i], xb[j]))
				#print(score)
				return score

			model = SVR(kernel = kernel)
		else:
			raise ValueError('Unknown kernel!')

		print('...built untrained model')
	except KeyboardInterrupt:
		print('User cancelled model building')
		quit(1)

	###################################################################################
	### DEFINE DATA
	###################################################################################

	data_kwargs = config['DATA']
	if '__name__' in data_kwargs:
		del data_kwargs['__name__'] #  from configparser
	if 'batch_size' in config['TRAINING']:
		data_kwargs['batch_size'] = int(config['TRAINING']['batch_size'])
	if 'use_fp' in config['ARCHITECTURE']:
		data_kwargs['use_fp'] = config['ARCHITECTURE']['use_fp']
	if 'shuffle_seed' in data_kwargs:
		data_kwargs['shuffle_seed'] = int(data_kwargs['shuffle_seed'])
	else:
		data_kwargs['shuffle_seed'] = int(time.time())
	if 'truncate_to' in data_kwargs:
		data_kwargs['truncate_to'] = int(data_kwargs['truncate_to'])
	if 'training_ratio' in data_kwargs:
		data_kwargs['training_ratio'] = float(data_kwargs['training_ratio'])

	if 'cv_folds' in data_kwargs:
		try:
			os.makedirs(os.path.dirname(fpath))
		except: # folder exists
			pass
		if '<this_fold>' in data_kwargs['cv_folds']:
			cv_folds = data_kwargs['cv_folds']
			total_folds = int(cv_folds.split('/')[1])
			all_cv_folds = ['{}/{}'.format(i + 1, total_folds) for i in range(total_folds)]
		else:
			all_cv_folds = [data_kwargs['cv_folds']]

	# Iterate through all folds
	ref_fpath = fpath
	for cv_fold in all_cv_folds:
		print('Using CV fold {}'.format(cv_fold))
		data_kwargs['cv_folds'] = cv_fold
		fpath = ref_fpath.replace('<this_fold>', cv_fold.split('/')[0])
		data = get_data_full(**data_kwargs)

		# Train model
		try:
			print('...training model')
			model.fit(data[0]['mols'], data[0]['y'])
			print('...trained model')
		except KeyboardInterrupt:
			pass

		###################################################################################
		### TEST MODEL
		###################################################################################

		print('...testing model')
		tstamp = datetime.datetime.utcnow().strftime('%m-%d-%Y_%H-%M')
		# Need to define predict_on_batch to be compatible
		model.predict_on_batch = model.predict
		data_withresiduals = test_model(model, data, fpath, tstamp = tstamp,
			batch_size = 1)
		print('...tested model')

ext: py

reason: import numpy


text:
#!/usr/bin/env python3

import pathlib

import matplotlib.pyplot as plt
import networkx as nx
import torch

from deepred_pytorch.models import ModelDAG
from deepred_pytorch.io import parse_data
from deepred_pytorch.io.data import parse_prot_go_map
from deepred_pytorch.io.utils import normalize_data

def main(
    go_obo_file,
    model_go_map_dir,
    model_dir,
    feature_vector_file,
    model_go_map_file,
    go_prot_map_train_dir,
    go_prot_map_test_dir,
):
    x_train, y_train = parse_data(
        feature_vector_file, model_go_map_file, go_prot_map_train_dir
    )
    x_train_tensor = torch.FloatTensor(x_train.values)
    # y_train_tensor = torch.FloatTensor(y_train.values)
    y_true_labels_train = parse_prot_go_map(go_prot_map_train_dir)
    x_train_tensor, scaler = normalize_data(x_train_tensor)
    x_test, y_test = parse_data(
        feature_vector_file, model_go_map_file, go_prot_map_test_dir
    )
    assert (y_train.columns == y_test.columns).all()
    x_test_tensor = torch.FloatTensor(x_test.values)
    # y_test_tensor = torch.FloatTensor(y_test.values)
    y_true_labels_test = parse_prot_go_map(go_prot_map_test_dir)
    y_true_labels = {**y_true_labels_train, **y_true_labels_test}
    x_test_tensor, _ = normalize_data(x_test_tensor, scaler=scaler)

    md = ModelDAG(go_obo_file, model_go_map_dir)
    md.load_models(model_dir)
    for ind in range(x_test_tensor.shape[0]):
        prot_name = x_test.index[ind]
        sample = x_test_tensor[ind, :]
        predicted_go_terms = md.predict(sample.view(1, sample.shape[0]), threshold=0.75)
        true_go_terms = y_true_labels[prot_name]
        print(f"True: {true_go_terms}, Predicted: {predicted_go_terms}")
        jacc_score = len(predicted_go_terms & true_go_terms) / len(
            predicted_go_terms | true_go_terms
        )
        print(f"Jaccard Score: {jacc_score}")

if __name__ == "__main__":
    GO_OBO_FILE = pathlib.Path("data/GO_db/go-basic.obo")
    MODEL_GO_MAP_DIR = pathlib.Path("data/model_go_map")
    MODEL_DIR = pathlib.Path("outputs/MF_CTriad")

    FEATURE_VECTOR_FILE = pathlib.Path("data/feature_vectors/CTriad_training_MF.csv")
    # all this does is limit the number of proteins in the train or test set
    # MODEL_GO_MAP_FILE = pathlib.Path("data/model_go_map_all.txt")
    MODEL_GO_MAP_FILE = pathlib.Path("data/model_go_map/MFGOTerms30_1_1001_2000_1.txt")
    GO_PROT_MAP_TRAIN_DIR = pathlib.Path("data/go_prot_map/training")
    GO_PROT_MAP_TEST_DIR = pathlib.Path("data/go_prot_map/testing")

    main(
        GO_OBO_FILE,
        MODEL_GO_MAP_DIR,
        MODEL_DIR,
        FEATURE_VECTOR_FILE,
        MODEL_GO_MAP_FILE,
        GO_PROT_MAP_TRAIN_DIR,
        GO_PROT_MAP_TEST_DIR,
    )

max_stars_repo_name: dileep-kishore/DEEPred-pytorch

max_issues_repo_name: dileep-kishore/DEEPred-pytorch

max_forks_repo_name: dileep-kishore/DEEPred-pytorch

reason: import networkx


text:
from baselines.common import Dataset, explained_variance, fmt_row, zipsame
from baselines import logger
import baselines.common.tf_util as U
import tensorflow as tf, numpy as np
import time
from baselines.common.mpi_adam import MpiAdam
from baselines.common.mpi_moments import mpi_moments
from mpi4py import MPI
from collections import deque
import os
from scipy.special import softmax

import cv2
import baselines.Termination_DEOC.cnn_policy as cnn
import itertools
import sys

def traj_segment_generator(pi, env, horizon, stochastic, num_options,saves,results,rewbuffer,dc):
    t = 0
    ac = env.action_space.sample() # not used, just so we have the datatype
    new = True # marks if we're on first timestep of an episode
    ob = env.reset()

    cur_ep_ret = 0 # return in current episode
    cur_ent_ret = 0 ## pseudo rewards in current episodes
    cur_op_ent_ret = 0 ## Intra option entropy in current episodes
    cur_ep_len = 0 # len of current episode
    cur_opt_switches = 0
    ep_rets = [] # returns of completed episodes in this segment
    ent_rets = []
    op_ent_rets=[]
    ep_lens = [] # lengths of ...
    opt_switches = []
    iters_so_far=0

    # Initialize history arrays
    obs = np.array([ob for _ in range(horizon)])
    rews = np.zeros(horizon, 'float32')
    realrews = np.zeros(horizon, 'float32')
    prews = np.zeros((horizon), 'float32')
    vpreds = np.zeros(horizon, 'float32')
    vpreds_ent = np.zeros(horizon, 'float32') #Entropy Value
    news = np.zeros(horizon, 'int32')
    opts = np.zeros(horizon, 'int32')
    acs = np.array([ac for _ in range(horizon)])
    prevacs = acs.copy()

    option = pi.get_option(ob)

    optpol_p=[]
    term_p=[]
    value_val=[]
    opt_duration = [[] for _ in range(num_options)]
    logstds = [[] for _ in range(num_options)]
    curr_opt_duration = 0.

    while True:
        prevac = ac
        if isinstance(pi, cnn.CnnPolicy):
            ac, vpred, vpred_ent, feats = pi.act(stochastic, ob, option)
            logstd=0
        else:
            ac, vpred, vpred_ent, feats,logstd = pi.act(stochastic, ob, option)
        logstds[option].append(logstd)
        # Slight weirdness here because we need value function at time T
        # before returning segment [0, T-1] so we get the correct
        # terminal value
        if t > 0 and t % horizon == 0:
            yield {"ob" : obs, "rew" : rews, "realrew": realrews, "prew": prews, "vpred" : vpreds, "vpred_ent" : vpreds_ent, "new" : news,
                    "ac" : acs, "opts" : opts, "prevac" : prevacs, "nextvpred": vpred * (1 - new), "nextvpred_ent": vpred_ent * (1 - new),
                    "ep_rets" : ep_rets, "ent_rets": ent_rets, "ep_lens" : ep_lens, 'term_p': term_p, 'value_val': value_val,
                     "opt_dur": opt_duration, "optpol_p":optpol_p, "logstds": logstds, "opt_switches":opt_switches}
            # Be careful!!! if you change the downstream algorithm to aggregate
            # several of these batches, then be sure to do a deepcopy
            ep_rets = []
            ent_rets = [] #### Storing pseudo rewards for episodes
            ep_lens = []
            term_p  = []
            value_val=[]
            opt_duration = [[] for _ in range(num_options)]
            logstds = [[] for _ in range(num_options)]
            curr_opt_duration = 0.
            iters_so_far +=1

            if iters_so_far == 100:
                if hasattr(env,'NAME'):
                    print("#################### Switching Goal ########################")
                    env.change()

        i = t % horizon
        obs[i] = ob
        vpreds[i] = vpred
        vpreds_ent[i] = vpred_ent
        news[i] = new
        opts[i] = option
        acs[i] = ac
        prevacs[i] = prevac

        ob, rew, new, _ = env.step(ac)
        # if not hasattr(env,'NAME'):
        #     if env.spec.id[:-3].lower()[:9] == "miniworld":
        #         env.text_label.text += "Option_" + str(option)
        # env.render()

        prew = gen_pseudo_reward(pi, ob, num_options, stochastic, cross=False)
        if hasattr(env,'NAME'):
            prew = prew*1e-2
        elif env.spec.id[:-3].lower()[:9] == "miniworld":
            prew = prew*1e-2
        else:
            prew = prew*1e-1  # Changing from 1e-2 while switching to softmax

        # rew = rew/10 if (env.spec.id[:-3].lower() == 'humanoid' or env.spec.id[:-3].lower() == 'humanoidstandup') else rew  ## Scaling reward down further for humanoid
        rew = rew/10 if num_options > 1 and not hasattr(env,'NAME') and not env.spec.id[:-3].lower()[:9] == "miniworld" else rew # To stabilize learning.

        prews[i] = prew
        rews[i] = rew
        realrews[i] = rew

        curr_opt_duration += 1

        ### Book-keeping
        t_p = []
        v_val = []
        for oopt in range(num_options):
            v_val.append(pi.get_vpred([ob],[oopt])[0][0])
            t_p.append(pi.get_tpred([ob],[oopt])[0][0])
        term_p.append(t_p)
        optpol_p.append(pi._get_op([ob])[0][0])
        value_val.append(v_val)
        term = pi.get_term([ob],[option])[0][0]
        ###

        if term:
            if num_options > 1:
                rews[i] -= dc
            opt_duration[option].append(curr_opt_duration)
            curr_opt_duration = 0.
            old_option = option
            option = pi.get_option(ob)
            if option != old_option:
                cur_opt_switches += 1

        # rew = rew*10 if (env.spec.id[:-3].lower() == 'humanoid' or env.spec.id[:-3].lower() == 'humanoidstandup') else rew
        cur_ep_ret += rew*10 if num_options > 1 and not hasattr(env,'NAME') and not env.spec.id[:-3].lower()[:9] == "miniworld" else rew
        cur_ent_ret += prew
        cur_ep_len += 1

        if new:
            ep_rets.append(cur_ep_ret)
            ent_rets.append(cur_ent_ret)
            ep_lens.append(cur_ep_len)
            opt_switches.append(cur_opt_switches)

            cur_opt_switches =0
            cur_ep_ret = 0
            cur_ent_ret =0
            cur_ep_len = 0
            ob = env.reset()
            option = pi.get_option(ob)
        t += 1

def gen_pseudo_reward(pi, ob, num_options, stochastic=True, cross=True):
    if num_options==1:
        return 0
    else:
        cum_entropy, joint_entropy = 0,0
        combinations = list(itertools.combinations(range(num_options), 2))
        for i in range(len(combinations)):
            sampled_op1 = combinations[i][0]
            sampled_op2 = combinations[i][1]
            if isinstance(pi, cnn.CnnPolicy):
                x1, _, _, _= pi.act(stochastic, ob, sampled_op1)
                x2, _, _, _ = pi.act(stochastic, ob, sampled_op2)
                logits_op1 = pi.get_logits(stochastic, ob, sampled_op1)[0]
                logits_op2 = pi.get_logits(stochastic, ob, sampled_op2)[0]
                pd_op1 = softmax(logits_op1)
                pd_op2 = softmax(logits_op2)
                pd_op1 = np.clip(pd_op1,1e-20, 1.0)
                pd_op2 = np.clip(pd_op2,1e-20, 1.0)
                if cross:
                    cum_entropy += -np.sum(pd_op1*np.log(pd_op2))/10*pd_op1.shape[0]
                else:
                    joint = np.multiply(pd_op1, pd_op2)
                    joint_entropy += -np.sum(joint*np.log(joint))/10*joint.shape[0]

            else:
                if cross:
                    x1, _, _, _,_ = pi.act(stochastic, ob, sampled_op1)
                    x2, _, _, _, _ = pi.act(stochastic, ob, sampled_op2)
                    x1 = softmax(x1)
                    x2 = softmax(x2)
                    x1 = np.clip(x1,1e-20, 1.0)
                    x2 = np.clip(x2,1e-20, 1.0)
                    cum_entropy += -np.sum(x1*np.log(x2))/x1.shape[0]
                else:
                    x1_prob, x2_prob = pi.get_action_prob(stochastic, ob, sampled_op1, sampled_op2)
                    x1_prob = np.clip(x1_prob,1e-20, 1.0)
                    x2_prob = np.clip(x2_prob, 1e-20, 1.0)
                    joint = np.multiply(x1_prob, x2_prob)
                    joint_entropy += np.clip(-np.sum(joint*np.log(joint))/joint.shape[0], a_min=1e-20, a_max=None)

        cum_entropy = cum_entropy/len(combinations)
        joint_entropy = joint_entropy/ len(combinations)
        return cum_entropy*2 if cross else joint_entropy*4

def add_vtarg_and_adv(seg, gamma, lam, deoc=False, tradeoff=0.1):
    """
    Compute target value using TD(lambda) estimator, and advantage with GAE(lambda)
    """
    T = len(seg["rew"])
    new = np.append(seg["new"], 0) # last element is only used for last vtarg, but we already zeroed it if last new = 1
    vpred = np.append(seg["vpred"], seg["nextvpred"])
    vpred_ent = (np.append(seg["vpred_ent"], seg["nextvpred_ent"]) if deoc else np.zeros((T+T), 'float32'))
    seg["adv"] = gaelam = np.empty(T, 'float32')
    seg["adv_ent"] = gaelam_ent = np.zeros(T, 'float32')
    rew = seg["rew"]
    prew = seg["prew"]
    lastgaelam = 0
    lastgaelam_ent =0
    for t in reversed(range(T)):
        nonterminal = 1-new[t+1]

        delta = rew[t] + gamma * vpred[t+1] * nonterminal - vpred[t]  # Vanilla TD Error

        # Changes for deoc
        if deoc:
            delta +=  (1-tradeoff)*rew[t] + (tradeoff)*prew[t] - rew[t]   # Compute changed TD_error
            delta_ent = (1-tradeoff)*rew[t] + gamma * vpred_ent[t+1] * nonterminal - vpred_ent[t]
            gaelam_ent[t] = lastgaelam_ent = delta_ent + gamma * lam * nonterminal * lastgaelam_ent + (tradeoff)*prew[t]

        gaelam[t] = lastgaelam = delta + gamma * lam * nonterminal * lastgaelam

    if deoc:
        seg["tdlamret_ent"] = seg["adv_ent"] + seg["vpred_ent"]
    else:
        seg["tdlamret_ent"] =  np.zeros(T, 'float32')

    seg["tdlamret"] = seg["adv"] + seg["vpred"]

def learn(env, policy_func, *,
        timesteps_per_batch, # timesteps per actor per update
        clip_param, entcoeff, # clipping parameter epsilon, entropy coeff
        optim_epochs, optim_stepsize, optim_batchsize,# optimization hypers
        gamma, lam, # advantage estimation
        max_timesteps=0, max_episodes=0, max_iters=0, max_seconds=0,  # time constraint
        callback=None, # you can do anything in the callback, since it takes locals(), globals()
        adam_epsilon=1e-5,
        schedule='constant', # annealing for stepsize parameters (epsilon and adam)
        num_options=1,
        app='',
        saves=False,
        wsaves=False,
        epoch=-1,
        seed=1,
        dc=0,
        render=False,
        caption='',
        deoc=False,
        tradeoff=0.1,
        term_mult=1.0,
        lr_mult=1.0,
        tdeoc = False
        ):

    optim_batchsize_ideal = optim_batchsize
    np.random.seed(seed)
    tf.set_random_seed(seed)
    env.seed(seed)

    ### Book-keeping
    if hasattr(env,'NAME'):
        gamename = env.NAME.lower()
    else:
        gamename = env.spec.id[:-3].lower()

    gamename += '-seed' + str(seed)
    gamename += app

    dirname = '{}_{}opts_saves/'.format(gamename,num_options)

    ### More book-kepping
    results=[]
    if tdeoc:
        results_name = caption + 'TDEOC_' + gamename + '_tradeoff'+ str(tradeoff) + '_dc' + str(dc)
    elif deoc:
        results_name = caption + 'DEOC_' + gamename + '_tradeoff'+ str(tradeoff) + '_dc' + str(dc)
    else:
        results_name = caption + 'Vanilla_' +  gamename  + '_dc' + str(dc)

    if epoch >= 0:
        results_name_file = results_name + '_epoch' + str(epoch)
        results_name_file += '_term_mult' + str(term_mult) + '_lr_mult' + str(lr_mult) +  '_'+str(num_options)+'opts' + '_results.csv'

    results_name += '_term_mult' + str(term_mult) + '_lr_mult' + str(lr_mult) +  '_'+str(num_options)+'opts' + '_results.csv'

    if (epoch<0 and os.path.exists(results_name)) or (epoch>=0 and os.path.exists(results_name_file)):
        print("Run already saved")
        sys.exit()
    if saves:
        if epoch >= 0:
            print(results_name_file)
            results = open(results_name_file,'w')
        else:
            print(results_name)
            results = open(results_name,'w')

        out = 'epoch,avg_reward,avg_entropy,switches'

        for opt in range(num_options): out += ',option {} dur'.format(opt)
        for opt in range(num_options): out += ',option {} steps'.format(opt)
        for opt in range(num_options): out += ',option {} std'.format(opt)
        for opt in range(num_options): out += ',option {} term'.format(opt)
        for opt in range(num_options): out += ',option {} adv'.format(opt)
        out+='\n'
        results.write(out)
        # results.write('epoch,avg_reward,option 1 dur, option 2 dur, option 1 term, option 2 term\n')
        results.flush()

    if wsaves:
        first=True
        if not os.path.exists(results_name+'_weights'):
            os.makedirs(results_name+'_weights')
            first = False
        # while os.path.exists(dirname) and first:
        #     dirname += '0'

        # files = ['pposgd_simple.py','mlp_policy.py','run_mujoco.py']
        # for i in range(len(files)):
        #     src = os.path.expanduser('') + files[i]
        #     dest = os.path.expanduser('') + results_name+'_weights'
        #     shutil.copy2(src,dest)
    ###

    # Setup losses and stuff
    # ----------------------------------------
    ob_space = env.observation_space
    ac_space = env.action_space
    pi = policy_func("pi", ob_space, ac_space) # Construct network for new policy
    oldpi = policy_func("oldpi", ob_space, ac_space) # Network for old policy
    atarg = tf.placeholder(dtype=tf.float32, shape=[None]) # Target advantage function (if applicable)
    atarg_ent = tf.placeholder(dtype=tf.float32, shape=[None]) # Target advantage with pseudo reward function (if applicable)
    ret = tf.placeholder(dtype=tf.float32, shape=[None]) # Empirical return
    ret_ent = tf.placeholder(dtype=tf.float32, shape=[None]) # Empirical return Entropy
    diversity = tf.placeholder(dtype=tf.float32, shape=[None])

    # option = tf.placeholder(dtype=tf.int32, shape=[None])

    lrmult = tf.placeholder(name='lrmult', dtype=tf.float32, shape=[]) # learning rate multiplier, updated with schedule
    clip_param = clip_param * lrmult # Annealed cliping parameter epislon

    # pdb.set_trace()
    ob = U.get_placeholder_cached(name="ob")
    option = U.get_placeholder_cached(name="option")
    term_adv = U.get_placeholder(name='term_adv', dtype=tf.float32, shape=[None])

    ac = pi.pdtype.sample_placeholder([None])

    kloldnew = oldpi.pd.kl(pi.pd)
    ent = pi.pd.entropy()
    meankl = U.mean(kloldnew)
    meanent = U.mean(ent)
    pol_entpen = (-entcoeff) * meanent

    ratio = tf.exp(pi.pd.logp(ac) - oldpi.pd.logp(ac)) # pnew / pold
    surr1 = ratio * (atarg_ent if deoc else atarg) # surrogate from conservative policy iteration
    surr2 = U.clip(ratio, 1.0 - clip_param, 1.0 + clip_param) * (atarg_ent if deoc else atarg) #
    pol_surr = - U.mean(tf.minimum(surr1, surr2)) # PPO's pessimistic surrogate (L^CLIP)  (Intra Option update)

    vf_loss = U.mean(tf.square(pi.vpred - ret)) ## Critic (Option critic)

    if deoc:
        vf_loss_ent = U.mean(tf.square(pi.vpred_ent - ret_ent))
        total_loss = pol_surr + pol_entpen + vf_loss + vf_loss_ent
        losses = [pol_surr, pol_entpen, vf_loss, vf_loss_ent, meankl, meanent]
        loss_names = ["pol_surr", "pol_entpen", "vf_loss", "vf_ent_loss", "kl", "ent"]
    else:
        total_loss = pol_surr + pol_entpen + vf_loss
        losses = [pol_surr, pol_entpen, vf_loss, meankl, meanent]
        loss_names = ["pol_surr", "pol_entpen", "vf_loss", "kl", "ent"]

    if tdeoc:
        term_loss = -pi.tpred * diversity ## Termination loss fn
    else:
        term_loss = pi.tpred * term_adv

    log_pi = tf.log(tf.clip_by_value(pi.op_pi, 1e-20, 1.0))
    entropy = -tf.reduce_sum(pi.op_pi * log_pi, reduction_indices=1)

    op_loss = - tf.reduce_sum( log_pi[0][option[0]] * atarg  + entropy * 0.1 ) #Policy over options

    total_loss += op_loss

    var_list = pi.get_trainable_variables()
    term_list = var_list[8:10]

    lossandgrad = U.function([ob, ac, atarg, atarg_ent, ret, ret_ent, lrmult,option], losses + [U.flatgrad(total_loss, var_list)])
    termloss = U.function(([ob, option, term_adv, diversity] if tdeoc else [ob, option, term_adv]), [U.flatgrad(term_loss, var_list)]) # Since we will use a different step size.
    adam = MpiAdam(var_list, epsilon=adam_epsilon)

    assign_old_eq_new = U.function([],[], updates=[tf.compat.v1.assign(oldv, newv)
        for (oldv, newv) in zipsame(oldpi.get_variables(), pi.get_variables())])
    compute_losses = U.function([ob, ac, atarg, atarg_ent, ret, ret_ent, lrmult, option], losses)

    U.initialize()
    adam.sync()

    saver = tf.compat.v1.train.Saver(max_to_keep=10000)
    dirname = results_name+'_weights/'

    if epoch >= 0:

        dirname = results_name+'_weights/'
        print("Loading weights from iteration: " + str(epoch))

        filename = dirname + '{}.ckpt'.format(epoch)
        saver.restore(U.get_session(),filename)
    ###

    episodes_so_far = 0
    timesteps_so_far = 0
    global iters_so_far
    iters_so_far = 0
    tstart = time.time()
    lenbuffer = deque(maxlen=100) # rolling buffer for episode lengths
    rewbuffer = deque(maxlen=100) # rolling buffer for episode rewards
    prewbuffer = deque(maxlen=100) # rolling buffer for episode pseudo rewards

    assert sum([max_iters>0, max_timesteps>0, max_episodes>0, max_seconds>0])==1, "Only one time constraint permitted"

    # Prepare for rollouts
    # ----------------------------------------
    seg_gen = traj_segment_generator(pi, env, timesteps_per_batch, stochastic=True, num_options=num_options,saves=saves,results=results,rewbuffer=rewbuffer,dc=dc)

    datas = [0 for _ in range(num_options)]

    while True:
        if callback: callback(locals(), globals())
        if max_timesteps and timesteps_so_far >= max_timesteps:
            break
        elif max_episodes and episodes_so_far >= max_episodes:
            break
        elif max_iters and iters_so_far >= max_iters:
            break
        elif max_seconds and time.time() - tstart >= max_seconds:
            break

        if schedule == 'constant':
            cur_lrmult = 1.0
        elif schedule == 'linear':
            cur_lrmult =  max(1.0 - float(timesteps_so_far) / max_timesteps, 0)
        else:
            raise NotImplementedError

        logger.log("********** Iteration %i ************"%iters_so_far)

        seg = seg_gen.__next__()
        add_vtarg_and_adv(seg, gamma, lam, deoc=deoc, tradeoff=tradeoff)

        opt_d = [0 for _ in range(num_options)]
        opt_steps =[] # Mean number of steps taken by an option before termination
        for i in range(num_options):
            dur = np.mean(seg['opt_dur'][i]) if len(seg['opt_dur'][i]) > 0 else 0.
            opt_steps.append(dur)

        std = []
        for i in range(num_options):
            logstd = np.mean(seg['logstds'][i]) if len(seg['logstds'][i]) > 0 else 0.
            std.append(np.exp(logstd))
        print("mean opt dur:", opt_steps)
        print("mean op pol:", np.mean(np.array(seg['optpol_p']),axis=0))
        print("mean term p:", np.mean(np.array(seg['term_p']),axis=0))
        print("mean value val:", np.mean(np.array(seg['value_val']),axis=0))

        ob, ac, opts, atarg, atarg_ent, tdlamret, tdlamret_ent, diversity = seg["ob"], seg["ac"], seg["opts"], seg["adv"], seg["adv_ent"], seg["tdlamret"], seg["tdlamret_ent"], seg["prew"]
        vpredbefore = seg["vpred"] # predicted value function before udpate
        atarg = (atarg - atarg.mean()) / atarg.std() # standardized advantage function estimate
        diversity = (diversity - diversity.mean()) / diversity.std()
        diversity = diversity*1e1 # if env.spec.id[:-3].lower()[:9] == "miniworld" else diversity*1e1
        atarg_ent = (atarg_ent if not deoc else (atarg_ent - atarg_ent.mean()) / atarg_ent.std())
        if hasattr(pi, "ob_rms"): pi.ob_rms.update(ob) # update running mean/std for policy
        assign_old_eq_new() # set old parameter values to new parameter values

        if iters_so_far % 5 == 0 and wsaves:
            print("weights are saved...")
            filename =  dirname + '.ckpt'
            save_path = saver.save(U.get_session(),filename)
            if iters_so_far % 500 == 0:
                filename =  dirname + '{}.ckpt'.format(iters_so_far)
                save_path = saver.save(U.get_session(),filename)

        min_batch= (160 if num_options<3 else 200) # Arbitrary
        t_advs = [[] for _ in range(num_options)]
        for opt in range(num_options):
            indices = np.where(opts==opt)[0]
            print("batch size:",indices.size)
            opt_d[opt] = indices.size
            if not indices.size:
                t_advs[opt].append(0.)
                continue

            ### This part is only necessasry when we use options. We proceed to these verifications in order not to discard any collected trajectories.
            if datas[opt] != 0:
                if (indices.size < min_batch and datas[opt].n > min_batch):
                    datas[opt] = Dataset(dict(ob=ob[indices], ac=ac[indices], atarg=atarg[indices], atarg_ent=atarg_ent[indices],  vtarg=tdlamret[indices], vtarg_ent=tdlamret_ent[indices], diversity=diversity[indices]), shuffle=not pi.recurrent)
                    t_advs[opt].append(0.)
                    continue

                elif indices.size + datas[opt].n < min_batch:
                    # pdb.set_trace()
                    oldmap = datas[opt].data_map

                    cat_ob = np.concatenate((oldmap['ob'],ob[indices]))
                    cat_ac = np.concatenate((oldmap['ac'],ac[indices]))
                    cat_atarg = np.concatenate((oldmap['atarg'],atarg[indices]))
                    cat_atarg_ent = np.concatenate((oldmap['atarg_ent'],atarg_ent[indices]))
                    cat_diversity = np.concatenate((oldmap['diversity'],diversity[indices]))
                    cat_vtarg = np.concatenate((oldmap['vtarg'],tdlamret[indices]))
                    cat_vtarg_ent = np.concatenate((oldmap['vtarg_ent'],tdlamret_ent[indices]))
                    datas[opt] = Dataset(dict(ob=cat_ob, ac=cat_ac, atarg=cat_atarg, atarg_ent=cat_atarg_ent, vtarg=cat_vtarg, vtarg_ent=cat_vtarg_ent, diversity=cat_diversity), shuffle=not pi.recurrent)
                    t_advs[opt].append(0.)
                    continue

                elif (indices.size + datas[opt].n > min_batch and datas[opt].n < min_batch) or (indices.size > min_batch and datas[opt].n < min_batch):

                    oldmap = datas[opt].data_map
                    cat_ob = np.concatenate((oldmap['ob'],ob[indices]))
                    cat_ac = np.concatenate((oldmap['ac'],ac[indices]))
                    cat_atarg = np.concatenate((oldmap['atarg'],atarg[indices]))
                    cat_atarg_ent = np.concatenate((oldmap['atarg_ent'],atarg_ent[indices]))
                    cat_diversity = np.concatenate((oldmap['diversity'],diversity[indices]))
                    cat_vtarg = np.concatenate((oldmap['vtarg'],tdlamret[indices]))
                    cat_vtarg_ent = np.concatenate((oldmap['vtarg_ent'],tdlamret_ent[indices]))
                    datas[opt] = d = Dataset(dict(ob=cat_ob, ac=cat_ac, atarg=cat_atarg, atarg_ent=cat_atarg_ent, vtarg=cat_vtarg, vtarg_ent=cat_vtarg_ent, diversity=cat_diversity), shuffle=not pi.recurrent)

                if (indices.size > min_batch and datas[opt].n > min_batch):
                    datas[opt] = d = Dataset(dict(ob=ob[indices], ac=ac[indices], atarg=atarg[indices], atarg_ent=atarg_ent[indices], vtarg=tdlamret[indices], vtarg_ent=tdlamret_ent[indices] , diversity=diversity[indices]), shuffle=not pi.recurrent)

            elif datas[opt] == 0:
                datas[opt] = d = Dataset(dict(ob=ob[indices], ac=ac[indices], atarg=atarg[indices], atarg_ent=atarg_ent[indices], vtarg=tdlamret[indices], vtarg_ent=tdlamret_ent[indices], diversity=diversity[indices]) , shuffle=not pi.recurrent)
            ###

            optim_batchsize = optim_batchsize or ob.shape[0]
            optim_epochs = np.clip(np.int(10 * (indices.size / (timesteps_per_batch/num_options))),10,10) if num_options > 1 else optim_epochs
            print("optim epochs:", optim_epochs)
            logger.log("Optimizing...")

            # Here we do a bunch of optimization epochs over the data
            for _ in range(optim_epochs):
                losses = [] # list of tuples, each of which gives the loss for a minibatch
                for batch in d.iterate_once(optim_batchsize):

                    tadv,nodc_adv = pi.get_term_adv(batch["ob"],[opt])
                    tadv = tadv if num_options > 1 else np.zeros_like(tadv)
                    t_advs[opt].append(nodc_adv)

                    *newlosses, grads = lossandgrad(batch["ob"], batch["ac"], batch["atarg"], batch["atarg_ent"], batch["vtarg"], batch["vtarg_ent"], cur_lrmult, [opt])
                    termg = termloss(batch["ob"], [opt], tadv, batch["diversity"]) if tdeoc else termloss(batch["ob"], [opt], tadv)
                    adam.update(termg[0], term_mult*5e-7 * cur_lrmult)
                    adam.update(grads, lr_mult*optim_stepsize * cur_lrmult)
                    losses.append(newlosses)

        # Record 3d simulations
        if iters_so_far%50 == 0 and render:
            record_behavior(env, pi, iteration=iters_so_far, stochastic=True, num_opts=num_options, frames=2052, dirname=results_name)

        # Record Trajectories with option distinction
        if iters_so_far%5==0 and render :
            if hasattr(env,'NAME'):
                record_tmaze(env, pi, iteration=iters_so_far, stochastic=True, num_opts=num_options, frames=2052, dirname=results_name, epoch=epoch)
            elif env.spec.id[:-3].lower() == "miniworld-oneroom" or env.spec.id[:-3].lower() == "miniworld-tmaze":
                record_oneroom(env, pi, iteration=iters_so_far, stochastic=True, num_opts=num_options, frames=2052, dirname=results_name, epoch=epoch)

        lrlocal = (seg["ep_lens"], seg["ep_rets"], seg["ent_rets"], seg["opt_switches"]) # local values
        listoflrpairs = MPI.COMM_WORLD.allgather(lrlocal) # list of tuples
        lens, rews, prew, switches = map(flatten_lists, zip(*listoflrpairs))
        lenbuffer.extend(lens)
        rewbuffer.extend(rews)
        prewbuffer.extend(prew)
        logger.record_tabular("OptSwitches", np.mean(switches))
        logger.record_tabular("EpLenMean", np.mean(lenbuffer))
        logger.record_tabular("EpRewMean", np.mean(rewbuffer))
        logger.record_tabular("EntropyMean", np.mean(prewbuffer))
        logger.record_tabular("EpThisIter", len(lens))
        episodes_so_far += len(lens)
        timesteps_so_far += sum(lens)
        iters_so_far += 1
        logger.record_tabular("EpisodesSoFar", episodes_so_far)
        logger.record_tabular("TimestepsSoFar", timesteps_so_far)
        logger.record_tabular("TimeElapsed", time.time() - tstart)
        if MPI.COMM_WORLD.Get_rank()==0:
            logger.dump_tabular()

        ### Book keeping
        if saves:
            out = "{},{},{},{}"
            for _ in range(num_options): out+=",{},{},{},{}"
            out+="\n"

            info = [iters_so_far, np.mean(rewbuffer), np.mean(prewbuffer), np.mean(seg["opt_switches"])]
            for i in range(num_options): info.append(opt_d[i])
            for i in range(num_options): info.append(opt_steps[i])
            for i in range(num_options): info.append(std[i])
            for i in range(num_options): info.append(np.mean(np.array(seg['term_p']),axis=0)[i])
            for i in range(num_options):
                info.append(np.mean(t_advs[i]))

            results.write(out.format(*info))
            results.flush()

def record_behavior(env, pi, iteration, stochastic=True, num_opts=1, frames=200, dirname = "",epoch=-1):
    episodes = 0
    counter = 0
    iteration = epoch if epoch>0 and iteration==0 else iteration
    dir_name = "Option_Behaviors/" + str(dirname) + "/" + str(iteration)
    dir_name_run = "Recorded_run/" + str(dirname) + "/" + str(iteration)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    if not os.path.exists(dir_name_run):
        os.makedirs(dir_name_run)

    outs = [cv2.VideoWriter(os.path.join(dir_name, "Option_{}.avi".format(i)),cv2.VideoWriter_fourcc(*'XVID'), 50, (500,500)) for i in range(num_opts)]
    out_run = cv2.VideoWriter(os.path.join(dir_name_run, "Run.avi"),cv2.VideoWriter_fourcc(*'XVID'), 50, (500,500))
    ob = env.reset()
    option = pi.get_option(ob)
    for frame in range(frames):
        counter += 1
        if isinstance(pi, cnn.CnnPolicy):
            ac, _, _, _  = pi.act(stochastic, ob, option)
        else:
            ac, _, _, _ ,_ = pi.act(stochastic, ob, option)
        ob, rew, new, _ = env.step(ac)
        div = gen_pseudo_reward(pi, ob, num_opts, stochastic)
        term = pi.get_term([ob],[option])[0][0]

        if not hasattr(env,'NAME'):
            if env.spec.id[:-3].lower()[:9] == "miniworld":
                env.set_usertxt(option)
        frame = env.render(mode='rgb_array')
        frame = cv2.resize(frame, (500,500))
        font = cv2.FONT_HERSHEY_SIMPLEX
        frame = cv2.putText(frame,'Option: {}'.format(option),(25,50), font, 0.5, (200,255,255), 1, cv2.LINE_AA)
        # frame = cv2.putText(frame,'Div: {0:.2f}'.format(div),(25,75), font, 0.5, (200,255,255), 1, cv2.LINE_AA)
        frame = cv2.putText(frame,'Term: {0:.2f}'.format(term),(25,100), font, 0.5, (200,255,255), 1, cv2.LINE_AA)
        outs[option].write(frame)
        out_run.write(frame)

        if term:
            option = pi.get_option(ob)
        if new:
            episodes += 1
            counter = 1
            ob = env.reset()
            option = pi.get_option(ob)
    print("episodes={}".format(episodes))
    [out.release() for out in outs]
    out_run.release()

def record_tmaze(env, pi, iteration, stochastic=True, num_opts=1, frames=200, dirname = "", epoch=-1):
    import matplotlib.pyplot as plt
    iteration = epoch if epoch>0 and iteration==0 else iteration
    episodes = 0
    counter = 0
    xs = np.arange(-.4,0.4,0.01)
    ys = np.arange(0.45,-0.2,-0.01)
    dir_name = "Trajectories/" + str(dirname) + "/" + str(iteration)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

    ob = env.reset()
    option = pi.get_option(ob)

    # Generate trajectories sample
    for _ in range(15):
        options = [[],[]]
        while True:
            ac, _, _, _ ,_ = pi.act(stochastic, ob, option)
            ob, rew, new, _ = env.step(ac)
            options[option].append(ob)
            term = pi.get_term([ob],[option])[0][0]

            if term:
                option = pi.get_option(ob)
            if new:
                episodes += 1
                counter = 1
                ob = env.reset()
                option = pi.get_option(ob)
                break

        # Plot Trajectories
        fig,ax = plt.subplots(1,1)
        ax.set_facecolor('white')
        ax.axvspan(-0.1, 0.1, facecolor='black',zorder=1)
        ax.axhspan(0.2,0.45,  facecolor='black',zorder=1)
        if options[0]:
            ax.scatter(np.array(options[0])[:,0],np.array(options[0])[:,1],s=15,c='red',zorder=2)#, 'r*', zorder=2)     #color='xkcd:red',linestyle=':', zorder=2)
        if options[1]:
            ax.scatter(np.array(options[1])[:,0],np.array(options[1])[:,1],s=15,c='yellow',zorder=2)#, 'y*', zorder=2)  #color='xkcd:fuchsia',linestyle=':', zorder=2)

        # Draw maze
        # ax.plot(np.arange(-0.1,0.1,0.001), np.full((200,),-0.2) ,c='black', linewidth=4)
        # ax.plot(np.full((400,),0.1), np.arange(-0.2,0.2,0.001),c='black', linewidth=4)
        # ax.plot(np.full((400,),-0.1), np.arange(-0.2,0.2,0.001),c='black', linewidth=4)
        #
        # ax.plot(np.arange(0.1,0.4,0.001), np.full((301,),0.2) ,c='black', linewidth=4)
        # ax.plot(np.arange(-0.4,-0.1,0.001), np.full((301,),0.2) ,c='black', linewidth=4)
        #
        # ax.plot(np.full((650,),0.4), np.arange(-0.2,0.45,0.001),c='black', linewidth=4)
        # ax.plot(np.full((650,),-0.4), np.arange(-0.2,0.45,0.001),c='black', linewidth=4)
        #
        # ax.plot(np.arange(-0.4,0.4,0.001), np.full((800,),0.45),c='black' , linewidth=4)

        ax.scatter(0.3,0.3, s=1000, c='lime',zorder=2)
        ax.scatter(-0.3,0.3, s=1000, c='lime',zorder=2)

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xlim(xmin=-.4,xmax=.4)
        ax.set_ylim(ymin=-.2,ymax=.45)
        # ax.set_facecolor('white')
        # ax.axvspan(-0.1, 0.1, facecolor='black')
        # ax.axhspan(0.2,0.45,  facecolor='black')
        plt.savefig(os.path.join(dir_name, "Ep{}.png".format(episodes)),bbox_inches='tight')
        plt.clf();plt.close()

    #######################  Plot teriminations ####################################
    import matplotlib.patches as patches
    dir_name = "Terminations/" + str(dirname) + "/" + str(iteration)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    vinput= []
    for y in ys:
        for x in xs:
            vinput.append([x,y])
    vinput = np.array(vinput).squeeze()
    terms = [[],[]]
    for op in range(len(options)):
        for i in range(len(vinput)):
            terms[op].append(pi.get_tpred([vinput[i]],[op])[0][0])
    # terms = np.array(terms)

    for i in range(len(options)):
        fig,ax = plt.subplots(1,1)
        # for a in ax:
        #     a.axvspan(-0.1, 0.1, facecolor='black',zorder=1)
        #     a.axhspan(0.2,0.45,  facecolor='black',zorder=1)
        heatmap_op1 = ax.imshow(np.array(terms[i]).reshape(len(ys),len(xs)), extent=[-.4,.4,-.2,.45], interpolation='nearest', alpha=1.,zorder=1)
        # heatmap_op2 = ax.imshow(np.array(terms[1]).reshape(len(ys),len(xs)), extent=[-.4,.4,-.2,.45], interpolation='nearest', alpha=1.,zorder=1)
        # for a in ax:
        ax.scatter(0.3,0.3, s=1000, c='lime',zorder=3)
        ax.scatter(-0.3,0.3, s=1000, c='lime',zorder=3)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.add_patch(patches.Rectangle((-0.398,-0.2),0.3,0.4, color='white',zorder=4))
        ax.add_patch(patches.Rectangle((0.1,-0.2),0.3,0.4, color='white',zorder=4))
        plt.colorbar(heatmap_op1, ax=ax)

        plt.savefig(os.path.join(dir_name, "Op{}.png".format(i)),bbox_inches='tight')
        plt.clf();plt.close()

    ################# Plot terminations during trajectories ##########################
    dir_name = "Terminations_Trajectories/" + str(dirname) + "/" + str(iteration)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    for _ in range(5):
        options = [[],[]]
        while True:
            ac, _, _, _ ,_ = pi.act(stochastic, ob, option)
            ob, rew, new, _ = env.step(ac)
            # options[option].append(ob)
            term = pi.get_term([ob],[option])[0][0]

            if term:
                options[option].append(ob)
                option = pi.get_option(ob)
            if new:
                episodes += 1
                counter = 1
                ob = env.reset()
                option = pi.get_option(ob)
                break

        # Plot Trajectories
        fig,ax = plt.subplots(1,1)
        ax.set_facecolor('white')
        ax.axvspan(-0.1, 0.1, facecolor='black',zorder=1)
        ax.axhspan(0.2,0.45,  facecolor='black',zorder=1)
        if options[0]:
            ax.scatter(np.array(options[0])[:,0],np.array(options[0])[:,1],s=15,c='red',zorder=2)#, 'r*', zorder=2)     #color='xkcd:red',linestyle=':', zorder=2)
        if options[1]:
            ax.scatter(np.array(options[1])[:,0],np.array(options[1])[:,1],s=15,c='yellow',zorder=2)#, 'y*', zorder=2)  #color='xkcd:fuchsia',linestyle=':', zorder=2)

        ax.scatter(0.3,0.3, s=1000, c='lime',zorder=2)
        ax.scatter(-0.3,0.3, s=1000, c='lime',zorder=2)

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xlim(xmin=-.4,xmax=.4)
        ax.set_ylim(ymin=-.2,ymax=.45)
        # ax.set_facecolor('white')
        # ax.axvspan(-0.1, 0.1, facecolor='black')
        # ax.axhspan(0.2,0.45,  facecolor='black')
        plt.savefig(os.path.join(dir_name, "Ep{}.png".format(episodes)),bbox_inches='tight')
        plt.clf();plt.close()

def record_oneroom(env, pi, iteration, stochastic=True, num_opts=1, frames=200, dirname = "", epoch=-1):
    import matplotlib.pyplot as plt
    episodes = 0
    counter = 0
    iteration = epoch if epoch>0 and iteration==0 else iteration
    xs = np.arange(-.4,0.4,0.01)
    ys = np.arange(0.45,-0.2,-0.01)
    dir_name = "Trajectories/" + str(dirname) + "/" + str(iteration)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

    ob = env.reset()
    option = pi.get_option(ob)

    # Generate trajectories sample
    for _ in range(10):
        options = [[],[]]
        frame2 = env.render_top_view()
        frame2 = cv2.resize(frame2, (520,394))
        fig,ax = plt.subplots(1,1)
        goal_pos = [env.box.pos[0], env.box.pos[2]]
        start_pos = [env.agent.pos[0], env.agent.pos[2]]
        # ax.imshow(frame2, zorder=1)
        while True:
            ac, _, _, _  = pi.act(stochastic, ob, option)
            ob, rew, new, _ = env.step(ac)
            pos = [env.agent.pos[0], env.agent.pos[2]]
            options[option].append(pos)
            term = pi.get_term([ob],[option])[0][0]

            if term:
                option = pi.get_option(ob)
            if new:
                episodes += 1
                counter = 1
                ob = env.reset()
                option = pi.get_option(ob)
                break

        if options[0]:
            ax.scatter(np.array(options[0])[:,0],np.array(options[0])[:,1], marker='o',zorder=2, c='red')#, 'r*', zorder=2)     #color='xkcd:red',linestyle=':', zorder=2)
        if options[1]:
            ax.scatter(np.array(options[1])[:,0],np.array(options[1])[:,1], marker='o',zorder=2, c='yellow')#, 'y*', zorder=2)  #color='xkcd:fuchsia',linestyle=':', zorder=2)
        ax.scatter(goal_pos[0], goal_pos[1], s=100, marker='s', c='green')
        ax.scatter(start_pos[0], start_pos[1], s=60, marker='X', c='blue')

        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_facecolor('black')
        plt.savefig(os.path.join(dir_name, "Ep{}.png".format(episodes)),bbox_inches='tight')
        plt.clf();plt.close()

def flatten_lists(listoflists):
    return [el for list_ in listoflists for el in list_]

reason: from scipy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Usage: python3 format_ground_truth.py data_set

import os.path
import cv2 as cv
import numpy as np
import sys

with open('v.txt') as f:
    content = f.readlines()
filenames = [x.strip() for x in content]

data_set = sys.argv[1] # 'test' or 'val'

if data_set == 'test':
   sys.exit('No test set annotations.')

vlist = np.loadtxt(data_set + '.txt')
vlist = vlist.astype('int').tolist()

sys.stdout.write('\033[K')

for fileno in vlist:
    print(fileno, end = '\r')
    fn = (filenames[fileno]).split('.')[0]

    res = np.loadtxt('../../../../../utils/ground_truth/headbox_wearer_speaker/' + fn + '.txt', delimiter=' ')
    res = res.astype('int')
    if len(res.shape) == 1:
        res = np.expand_dims(res, axis=0)

    box = {};
    for n in range(res.shape[0]):
        if not (res[n][0] in box):
           box[res[n][0]] = []
        box[res[n][0]].append([res[n][1], res[n][2], res[n][3], res[n][4], res[n][5], res[n][6]]);

    for frame_num in range(9000):
        asp = []
        if frame_num in box:
           b = box[frame_num]
           for k in range(len(b)):
               pid = int(b[k][0])
               x1 = int(b[k][1])
               y1 = int(b[k][2])
               x2 = int(b[k][3])
               y2 = int(b[k][4])
               speak = int(b[k][5]);
               if speak == 1:
                  asp.append([1, x1, y1, x2, y2])

        np.savetxt('ground-truth/' + str(fileno) + '_' + str(frame_num) + '.txt', asp, fmt='%d', delimiter=' ')


text:
"""
A file for executing math functions.
"""

import random
import numpy as np
from typing import Union  # gives ability to define types in python

def euler(n=10, maxitr=1000):

    """
    This function computes :math:`e` via a Taylor series.

    .. math::

        e = 1 + \\sum_n^\\infty \\frac{1}{n!}

    Parameters
    ----------
    n : int
        Level of precision.

    Returns
    -------
    e_value : float
        The computed value of Euler's number.
    """

    if n < 0:
        raise ValueError("Only positive integers are allowed")

    euler_tmp = 0
    euler = 0
    for i in range(maxitr):
        denominator = 1
        for j in range(1, i + 1):
            denominator = denominator * j
        euler_tmp += 1 / denominator

        if i == 0:
            euler = round(euler_tmp, n)
        else:
            if euler == round(euler_tmp, n):
                break
            else:
                euler = round(euler_tmp, n)
    print("Euler's number up to %g digits is: %f" % (n, euler))
    return euler

def find_pi(maxitr=1e+20):

    """
    This function computes :math:`\\pi` heuristically.

    Parameters
    ----------
    maxitr : int
        Number of random coordinates used in calculation.

    Returns
    -------
    pi_value : float
        The computed value of pi according to the number of points requested.
    """

    ncircle, ntotal = [0, 0]
    for i in range(int(maxitr)):

        # Generate coordinates
        coord = np.array([random.random() for x in range(2)])
        # Determine if it is within circle and add to counts
        ntotal += 1
        if np.sqrt(np.sum(coord**2)) < 1:
            ncircle += 1
        # Calculate pi
        pi_tmp = ncircle / ntotal * 4

    print("Pi is: %f" % (pi_tmp))
    return pi_tmp


text:
"""
montecarloSurface.py

Created by Luca Camerani at 13/09/2020, University of Milano-Bicocca.
(l.camerani@campus.unimib.it)
All rights reserved.

This file is part of the EcoFin-Library (https://github.com/LucaCamerani/EcoFin-Library),
and is released under the "BSD Open Source License".
"""

import matplotlib.pyplot as plt
import numpy as np

from EcoFin.math.stochasticProcess.itoProcess import ItoProcess
from EcoFin.stat.montecarloDistribution import MonteCarlo

t = 20
T = 100
dt = 1

n = 300

ito = ItoProcess(T, dt, t=t, x0=100, n=n)

simulation = None

# ---[GEOMETRIC BROWNIAN MOTION]----------------------------------------------
simulation = ito.geometricBrownianMotion(0.01, 0.02)
# ----------------------------------------------------------------------------

MC = MonteCarlo(simulation)
output = []

for i in MC.getIndexSpace():
    EPDF = MC.getEmpiricalDist(index=i, bandwidth=10, fullSpace=True)
    output.append(EPDF.probabilities)

X, Y = np.meshgrid(MC.getIndexSpace(), MC.getValues())
Z = np.array(output).transpose()

fig = plt.figure(figsize=(15, 8))
ax = fig.add_subplot(111, projection='3d')
fig.suptitle('Montecarlo Distribution Evolution')
surf = ax.plot_surface(X, Y, Z, alpha=.6)

for i in range(0, n):
    plt.plot(MC.getIndexSpace(), simulation[i], alpha=.6)

ax.set_xlabel('Index (time)')
ax.set_ylabel('Value')
ax.set_zlabel('Probability')
plt.show()

max_stars_repo_name: LucaCamerani/EcoFin-library

max_issues_repo_name: LucaCamerani/EcoFin-library

max_forks_repo_name: LucaCamerani/EcoFin-library


text:
# LSTM小例子，虽然我什么都不懂，但是能跑出结果就很开兴了
# 一个一个开始搞吧
# 呜呜呜，感觉被骗了，matlab 有相应的LSTM版本,但是没有LSTM回归的，就感觉很气啊，哼，希望matlab 2018b能有吧
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

# 前面都是一堆的包，没甚么好注释的
dataframe = read_csv('AirPassengers.csv', usecols=[1],
                     engine='python', skipfooter=3)
# 注意这个地方啊，usecols=[1]，就是说只使用第一行(从第0行开始数的);
# 这个数据只有 144个
dataset = dataframe.values  # dtype=int64
# 其实就是时间序列的值了
# 将整型变为float
dataset = dataset.astype('float32')  # 类型转换

plt.plot(dataset)  # 想想自己傻的很，原来这样也可以，可以不要自变量的取值范围的
plt.show()  # 显示图形

# X is the number of passengers at a given time (t) and Y is the number of passengers at the next time (t + 1).

# convert an array of values into a dataset matrix
# 这个地方我也比较迷
def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset) - look_back - 1):
        a = dataset[i:(i + look_back), 0]
        dataX.append(a)
        dataY.append(dataset[i + look_back, 0])
    return numpy.array(dataX), numpy.array(dataY)

# fix random seed for reproducibility
numpy.random.seed(7)
# 正则化数据
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)  # 这个地方把所有的都变了，包括test,所以后面要把test再变回来

# split into train and test sets
train_size = int(len(dataset) * 0.67)  # %67的是训练数据
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
# 这个地方是将训练数据和测试数据分开，trainX是0：93号数据，trainY是1：94号数据
# use this function to prepare the train and test datasets for modeling
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]，变换维度
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

# create and fit the LSTM network,创建LSTM模型
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)  # 转换回来 94个 [124.275 129.42 (94,1)
trainY = scaler.inverse_transform([trainY])  # 转换回来   94个[118 ,132
testPredict = scaler.inverse_transform(testPredict)  # [314.82578,353.67865... 46
testY = scaler.inverse_transform([testY])  # [301 356.....]  46

trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:, 0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:, 0]))
print('Test Score: %.2f RMSE' % (testScore))
# shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict) + look_back, :] = trainPredict
# 1-94号元素赋值了
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict) + (look_back * 2) + 1:len(dataset) - 1, :] = testPredict
# 97-142号元素赋值了
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))  # 转换回来
plt.plot(trainPredictPlot)  # 画图像
plt.plot(testPredictPlot)  # 画图像
plt.show()

max_stars_repo_name: chaiwenda/ImgLabel

max_issues_repo_name: chaiwenda/ImgLabel

max_forks_repo_name: chaiwenda/ImgLabel


text:
import cytest # from ./lib; must be first
from copy import copy
from curry.backends.py import sprite
from curry.common import LEFT, RIGHT, UNDETERMINED
import itertools, unittest

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

BASIC_SIZE = sprite.Fingerprint.BASIC_SIZE()
BRANCHING_FACTOR = sprite.Fingerprint.BRANCHING_FACTOR()

class Fingerprint(cytest.TestCase):
  def testTreeShape(self):
    # The fingerprint is an efficient data structure for storing choice states.
    #
    # It is implemented as a tree consisting of nodes.  A node is a union of a
    # pointer-to-branch or block (leaf).  Branches are shared.  Each choice
    # uses two bits to encode a choice state (left, right, or undetermined).
    # Since the space of one pointer is available (due to the branch), a block
    # on a 64-bit system contains 32 choices.  The fingerprint automatically
    # resizes itself based on the largest index ever used.  To expand, it adds
    # a level of branches.  The branching factor can be adjusted in the source,
    # but these diagrams are all drawn assuming a branching factor of four.

    zero = [0] * BASIC_SIZE # A zero block.
    allzero = lambda blocks: all(block.values() == zero for block in blocks)
    assertAllZero = lambda *args: self.assertTrue(allzero(itertools.chain(*args)))

    # Depth=0.
    # There is simply one block.
    fp = sprite.Fingerprint()
    self.assertEqual(fp.capacity, BASIC_SIZE)
    self.assertEqual(fp.depth, 0)
    self.assertEqual([int(fp[i]) for i in range(8)], [0]*8)
    self.assertEqual(fp.capacity, BASIC_SIZE)
    self.assertEqual(fp.depth, 0)
    assertAllZero([fp.tree()])

    # Depth=1.
    # All blocks are unique.
    #            B
    #          /| |\
    #         / | | \
    #        a  b c  d
    #
    # Increase the size.
    self.assertEqual(fp[BASIC_SIZE], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR)
    self.assertEqual(fp.depth, 1)
    # Accessing the last available element does not changne the capacity.
    self.assertEqual(fp[fp.capacity - 1], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR)
    self.assertEqual(fp.depth, 1)

    tree = fp.tree()
    self.assertEqual(len(set(id(x) for x in tree)), BRANCHING_FACTOR) # all different addresses
    assertAllZero(tree)

    # Depth=2.
    #                 o        (w,x,y,z)
    #               /   \\\
    #             /       \\\
    #            o          o
    #          /| |\      /| |\
    #         / | | \    / | | \
    #        a  b c  d  e  f g  h
    #
    # Increase the size.
    self.assertEqual(fp[fp.capacity], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR ** 2)
    self.assertEqual(fp.depth, 2)
    # Accessing the last available element does not changne the capacity.
    self.assertEqual(fp[fp.capacity - 1], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR ** 2)
    self.assertEqual(fp.depth, 2)

    tree = fp.tree()
    ids = [id(x) for x in tree]
    self.assertFalse(any(ids[0]==_ for _ in ids[1:]))
    self.assertTrue(ids[1:-1] == ids[2:])
    assertAllZero(tree[0], tree[1])

    # Depth=3.
    #
    #                          o (u,v,w,x)
    #                         / \\\
    #                       /     \\\
    #                     /         \\\
    #                   /             o (q,r,s,t)
    #                 o (m,n,o,p)    ||||
    #               /   \\\          ||||
    #             /       \\\        ||||
    #            o          o         o
    #          /| |\      /| |\     /| |\
    #         / | | \    / | | \   / | | \
    #        a  b c  d  e  f g  h i  j k  l
    #
    # Increase the size.
    self.assertEqual(fp[fp.capacity], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR ** 3)
    self.assertEqual(fp.depth, 3)
    # Accessing the last available element does not changne the capacity.
    self.assertEqual(fp[fp.capacity - 1], UNDETERMINED)
    self.assertEqual(fp.capacity, BASIC_SIZE * BRANCHING_FACTOR ** 3)
    self.assertEqual(fp.depth, 3)

    tree = fp.tree()
    abcd = [id(x) for x in tree[0][0]]
    efgh = [id(x) for x in tree[0][1]]
    ijkl = [id(x) for x in tree[1][0]]
    mnop = [id(x) for x in tree[0]]
    qrst = [id(x) for x in tree[1]]
    uvwx = [id(x) for x in tree]
    self.assertFalse(any(uvwx[0]==_ for _ in uvwx[1:]))
    self.assertTrue(uvwx[1:-1] == uvwx[2:])
    self.assertFalse(any(mnop[0]==_ for _ in mnop[1:]))
    self.assertTrue(mnop[1:-1] == mnop[2:])
    self.assertTrue(qrst[:-1] == qrst[1:])
    assertAllZero(tree[0][0], tree[0][1], tree[1][0])

    # Now, set a choice in one of the shared branches to check the
    # copy-on-write semantic.  The path below will clone an instance of "j".
    blockid = lambda path: sum(
        i * BRANCHING_FACTOR ** n
            for i,n in zip(path, reversed(range(len(path)-1)))
      )
    choiceid = lambda path: BASIC_SIZE * blockid(path) + path[-1]
    path = [2,0,1,17]
    cid = choiceid(path)
    fp[cid] = LEFT
    tree = fp.tree()
    #
    uvwx = [id(x) for x in tree]
    self.assertTrue(len(set(uvwx)) == 3)
    self.assertTrue(all(uvwx[1] == _ for _ in uvwx[3:]))
    assertAllZero(tree[0][0], tree[0][1], tree[1][0])
    qrst = [id(x) for x in tree[2]]
    self.assertTrue(qrst[0] != qrst[1] and qrst[1:-1] == qrst[2:])
    assertAllZero(tree[2][1])
    ijkl = [id(x) for x in tree[2][0]]
    assertAllZero([tree[2][0][_] for _ in [x for x in range(BRANCHING_FACTOR) if x != 1]])
    self.assertEqual(
        tree[2][0][1].values()
      , [-1 if _ == 17 else 0 for _ in range(BASIC_SIZE)]
      )

    # One last check: set one more arbitrary element and then sweep through all
    # indices to check.
    cidR = choiceid([0,3,0,5])
    fp[cidR] = RIGHT
    self.assertTrue(all(
        fp[i] == (LEFT if i == cid else RIGHT if i == cidR else UNDETERMINED)
            for i in range(fp.capacity)
      ))

  def testGetSetItem(self):
    fp = sprite.Fingerprint()
    self.assertEqual(fp[3], sprite.UNDETERMINED)
    with self.assertRaisesRegex(ValueError, 'expected LEFT or RIGHT'):
      fp[3] = sprite.UNDETERMINED
    for value in [15, None, 'left', -1, 0, 1]:
      with self.assertRaises(TypeError):
        fp[3] = value
    fp[3] = LEFT
    self.assertEqual(fp[3], LEFT)
    fp[3] = RIGHT
    self.assertEqual(fp[3], RIGHT)

  @unittest.skipIf(np is None, 'NumPy not available')
  def testRandom(self):
    '''Set and test random choice IDs, checking for consistent behavior.'''
    choice = np.random.choice
    def choose_dataset(data):
      n = choice(len(data))
      return data[n]
    def commit(data, maxindex):
      '''Commit a random choice in a random fingerprint.'''
      fp,memory = choose_dataset(data)
      while True:
        cid = choice(maxindex)
        if cid not in memory:
          break
      lr = LEFT if np.random.random() < 0.5 else RIGHT
      fp[cid] = memory[cid] = lr
    def clone(data, maxindex):
      fp,memory = choose_dataset(data)
      data.append((copy(fp), dict(memory)))
    def test(data, maxindex):
      fp,memory = choose_dataset(data)
      if not memory or np.random.random() < 0.5:
        # Test a value not already determined.
        while True:
          cid = choice(maxindex)
          if cid not in memory:
            break
        self.assertEqual(fp[cid], UNDETERMINED)
      else:
        # Test a value already determined.
        cid = choice(list(memory.keys()))
        self.assertEqual(fp[cid], memory[cid])
    ITERATIONS = 200
    NUM_ACTIONS = 100
    MAXINDEX = [int(x) for x in [1e2, 1e4, 1e6, 1e8, 1e10]]
    ACTIONS = [commit, test, clone]
    WEIGHTS = [
        [0.50, 0.00, 0.50] # commit and test only.
      , [0.25, 0.25, 0.50] # half testing, the other half commit or clone.
      , [0.10, 0.80, 0.10] # mostly cloning.
      , [0.80, 0.10, 0.10] # mostly committing.
      , [0.10, 0.10, 0.80] # mostly testing.
      ]
    for _ in range(ITERATIONS):
      # The first element (data) is a list of pairs of a fingerprint and a
      # dict.  Choice are remembered in the dict for cross-checking.
      args = [(sprite.Fingerprint(), {})], choice(MAXINDEX)
      p = WEIGHTS[choice(len(WEIGHTS))]
      for _ in range(NUM_ACTIONS):
        choice(ACTIONS, p=p)(*args)

max_stars_repo_name: andyjost/Sprite

max_issues_repo_name: andyjost/Sprite

max_forks_repo_name: andyjost/Sprite


text:
from nltk.tree import Tree
import numpy as np
import os
import pandas as pd
import pytest
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import string
import tempfile
import torch.nn as nn
import utils

import np_sgd_classifier
import np_shallow_neural_classifier
import np_rnn_classifier
import np_autoencoder
import np_tree_nn

import torch_shallow_neural_classifier
import torch_rnn_classifier
import torch_autoencoder
import torch_tree_nn

__author__ = "Christopher Potts"
__version__ = "CS224u, Stanford, Spring 2020"

utils.fix_random_seeds()

@pytest.fixture
def XOR():
    dataset = [
        ([1.,1.], True),
        ([1.,0.], False),
        ([0.,1.], False),
        ([0.,0.], True)]
    X, y = zip(*dataset)
    X = np.array(X)
    y = list(y)
    return X, y

@pytest.fixture
def X_sequence():
    vocab = ['a', 'b', '$UNK']
    train = [
        [list('ab'), 'good'],
        [list('aab'), 'good'],
        [list('abb'), 'good'],
        [list('aabb'), 'good'],
        [list('ba'), 'bad'],
        [list('baa'), 'bad'],
        [list('bba'), 'bad'],
        [list('bbaa'), 'bad']]
    test = [
        [list('aaab'), 'good'],
        [list('baaa'), 'bad']]
    return train, test, vocab

@pytest.fixture
def X_tree():
    vocab = ["1", "+", "2", "$UNK"]
    train = [
        "(odd 1)",
        "(even 2)",
        "(odd (pdd 1))",
        "(even (even 2))",
        "(even (odd 1) (neutral (neutral +) (odd 1)))",
        "(odd (odd 1) (neutral (neutral +) (even 2)))",
        "(odd (even 2) (neutral (neutral +) (odd 1)))",
        "(even (even 2) (neutral (neutral +) (even 2)))",
        "(even (odd 1) (neutralB (neutral +) (odd (odd 1) (neutral (neutral +) (even 2)))))"]
    X_train = [Tree.fromstring(x) for x in train]
    return X_train, vocab

@pytest.fixture
def cheese_disease_dataset():
    X = []
    y = []
    src_filename = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "cheeseDisease.train.txt")
    with open(src_filename, encoding='utf8') as f:
        for line in f:
            label, ex = line.split("\t", 1)
            label = "cheese" if label.strip() == "1" else "disease"
            ex = list(ex.lower().strip())
            X.append(ex)
            y.append(label)
    vocab = list(string.ascii_lowercase) + ["$UNK"]
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.33, random_state=42)
    return {'X_train': X_train, 'X_test': X_test,
            'y_train': y_train, 'y_test': y_test,
            'vocab': vocab}

def test_np_shallow_neural_classifier(XOR):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    X, y = XOR
    model = np_shallow_neural_classifier.ShallowNeuralClassifier(
        hidden_dim=4,
        hidden_activation=np.tanh,
        d_hidden_activation=utils.d_tanh,
        eta=0.05,
        tol=1.5e-8,
        display_progress=True,
        max_iter=100)
    model.fit(X, y)
    model.predict(X)

def test_np_shallow_neural_classifier_simple_example():
    acc = np_shallow_neural_classifier.simple_example()
    assert acc >= 0.88

def test_torch_shallow_neural_classifier(XOR):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    X, y = XOR
    model = torch_shallow_neural_classifier.TorchShallowNeuralClassifier(
        hidden_dim=4,
        hidden_activation=nn.ReLU(),
        max_iter=100,
        eta=0.01)
    model.fit(X, y)
    model.predict(X)
    model.predict_proba(X)

def test_torch_shallow_neural_classifier_simple_example():
    acc = torch_shallow_neural_classifier.simple_example()
    assert acc >= 0.90

def test_torch_shallow_neural_classifier_incremental(XOR):
    X, y = XOR
    model = torch_shallow_neural_classifier.TorchShallowNeuralClassifier(
        hidden_dim=4,
        hidden_activation=nn.ReLU(),
        max_iter=100,
        eta=0.01)
    model.fit(X, y, X_dev=X, dev_iter=1)
    epochs = list(model.dev_predictions.keys())
    assert epochs == list(range(1, 101))
    assert all(len(v)==len(X) for v in model.dev_predictions.values())

def test_np_rnn_classifier(X_sequence):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    train, test, vocab = X_sequence
    embedding = np.array([utils.randvec(10) for _ in vocab])
    mod = np_rnn_classifier.RNNClassifier(
        vocab=vocab,
        embedding=embedding,
        hidden_dim=20,
        max_iter=100)
    X, y = zip(*train)
    X_test, _ = zip(*test)
    mod.fit(X, y)
    mod.predict(X_test)
    mod.predict_proba(X_test)
    mod.predict_one(X_test[0])
    mod.predict_one_proba(X_test[0])

@pytest.mark.parametrize("initial_embedding, use_embedding",[
    [True, False],
    [True, True],
    [False, False],
    [False, True]
])
def test_np_rnn_classifier_simple_example(initial_embedding, use_embedding):
    np_rnn_classifier.simple_example()

def test_torch_rnn_classifier(X_sequence):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    train, test, vocab = X_sequence
    mod = torch_rnn_classifier.TorchRNNClassifier(
        vocab=vocab, max_iter=100)
    X, y = zip(*train)
    X_test, _ = zip(*test)
    mod.fit(X, y)
    mod.predict(X_test)
    mod.predict_proba(X_test)

def test_torch_rnn_classifier_incremental(X_sequence):
    train, test, vocab = X_sequence
    model = torch_rnn_classifier.TorchRNNClassifier(
        vocab=vocab, max_iter=100)
    X, y = zip(*train)
    X_test, _ = zip(*test)
    model.fit(X, y, X_dev=X_test, dev_iter=20)
    epochs = list(model.dev_predictions.keys())
    assert epochs == list(range(20, 101, 20))
    assert all(len(v)==len(X_test) for v in model.dev_predictions.values())

def test_torch_rnn_classifier_cheese_disease(cheese_disease_dataset):
    mod = torch_rnn_classifier.TorchRNNClassifier(
        vocab=cheese_disease_dataset['vocab'],
        embed_dim=20,
        hidden_dim=20,
        max_iter=20)
    mod.fit(cheese_disease_dataset['X_train'], cheese_disease_dataset['y_train'])
    pred = mod.predict(cheese_disease_dataset['X_test'])
    assert accuracy_score(cheese_disease_dataset['y_test'], pred) > 0.80

@pytest.mark.parametrize("initial_embedding, use_embedding",[
    [True, False],
    [True, True],
    [False, False],
    [False, True]
])
def test_torch_rnn_classifier_simple_example(initial_embedding, use_embedding):
    torch_rnn_classifier.simple_example(initial_embedding)

@pytest.mark.parametrize("pandas", [True, False])
def test_np_autoencoder(pandas):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    X = utils.randmatrix(20, 50)
    if pandas:
        X = pd.DataFrame(X)
    ae = np_autoencoder.Autoencoder(hidden_dim=5, max_iter=100)
    H = ae.fit(X)
    ae.predict(X)
    H_is_pandas = isinstance(H, pd.DataFrame)
    assert H_is_pandas == pandas

@pytest.mark.parametrize("pandas", [True, False])
def test_torch_autoencoder(pandas):
    """Just makes sure that this code will run; it doesn't check that
    it is creating good models.
    """
    X = utils.randmatrix(20, 50)
    if pandas:
        X = pd.DataFrame(X)
    ae = torch_autoencoder.TorchAutoencoder(hidden_dim=5, max_iter=100)
    H = ae.fit(X)
    ae.predict(X)
    H_is_pandas = isinstance(H, pd.DataFrame)
    assert H_is_pandas == pandas

def test_np_autoencoder_simple_example():
    mse = np_autoencoder.simple_example()
    assert mse < 0.003

def test_torch_autoencoder_simple_example():
    mse = torch_autoencoder.simple_example()
    assert mse < 0.0001

@pytest.mark.parametrize("initial_embedding, separate_y", [
    [True, True],
    [True, False],
    [False, True],
    [False, False]
])
def test_np_tree_nn_simple_example(initial_embedding, separate_y):
    np_tree_nn.simple_example(initial_embedding, separate_y)

@pytest.mark.parametrize("initial_embedding, separate_y", [
    [True, True],
    [True, False],
    [False, True],
    [False, False]
])
def test_torch_tree_nn_simple_example(initial_embedding, separate_y):
    torch_tree_nn.simple_example(initial_embedding, separate_y)

def test_torch_tree_nn_incremental(X_tree):
    X, vocab = X_tree
    model = torch_tree_nn.TorchTreeNN(
        vocab,
        embed_dim=50,
        hidden_dim=50,
        max_iter=100,
        embedding=None)
    model.fit(X, X_dev=X, dev_iter=20)
    epochs = list(model.dev_predictions.keys())
    assert epochs == list(range(20, 101, 20))
    assert all(len(v)==len(X) for v in model.dev_predictions.values())

def test_sgd_classifier():
    acc = np_sgd_classifier.simple_example()
    assert acc >= 0.89

@pytest.mark.parametrize("model, params", [
    [
        np_sgd_classifier.BasicSGDClassifier(max_iter=10, eta=0.1),
        {'max_iter': 100, 'eta': 1.0}
    ],
    [
        np_rnn_classifier.RNNClassifier(
            vocab=[], max_iter=10, hidden_dim=5, eta=0.1),
        {'hidden_dim': 10, 'eta': 1.0, 'max_iter': 100}
    ],
    [
        torch_rnn_classifier.TorchRNNClassifier(
            vocab=[], max_iter=10, hidden_dim=5, eta=0.1),
        {
            'hidden_dim': 10,
            'eta': 1.0,
            'max_iter': 100,
            'l2_strength': 0.01,
            'embed_dim': 100,
            'bidirectional': False
        }
    ],
    [
        np_tree_nn.TreeNN(
            vocab=[], max_iter=10, hidden_dim=5, eta=0.1),
        {'embed_dim': 5, 'hidden_dim': 10, 'eta': 1.0, 'max_iter': 100}
    ],
    [
        torch_tree_nn.TorchTreeNN(
            vocab=[], max_iter=10, hidden_dim=5, eta=0.1),
        {
            'embed_dim': 5,
            'hidden_dim': 10,
            'hidden_activation': nn.ReLU(),
            'eta': 1.0,
            'max_iter': 100,
            'l2_strength': 0.01
        }
    ],
    [
        np_shallow_neural_classifier.ShallowNeuralClassifier(
            hidden_dim=5, max_iter=1, eta=1.0),
        {
            'hidden_dim': 10,
            # Reset to ReLU:
            'hidden_activation': lambda z: np.maximum(0, z),
            'd_hidden_activation': lambda z: np.where(z > 0, 1, 0),
            'max_iter': 10,
            'eta': 0.1
        }
    ],
    [
        torch_shallow_neural_classifier.TorchShallowNeuralClassifier(
            hidden_dim=5, hidden_activation=nn.ReLU(), max_iter=1, eta=1.0),
        {
            'hidden_dim': 10,
            'hidden_activation': nn.ReLU(),
            'max_iter': 10,
            'eta': 0.1
        }
    ],
    [
        np_autoencoder.Autoencoder(hidden_dim=5, max_iter=1, eta=1.0),
        {'hidden_dim': 10, 'max_iter': 10, 'eta': 0.1}
    ],
    [
        torch_autoencoder.TorchAutoencoder(
            hidden_dim=5, hidden_activation=nn.ReLU(), max_iter=1, eta=1.0),
        {
            'hidden_dim': 10,
            'hidden_activation': nn.ReLU(),
            'max_iter': 10,
            'eta': 0.1
        }
    ]
])
def test_parameter_setting(model, params):
    model.set_params(**params)
    for p, val in params.items():
        assert getattr(model, p) == val

@pytest.mark.parametrize("model_class", [
    np_rnn_classifier.RNNClassifier,
    torch_rnn_classifier.TorchRNNClassifier
])
def test_rnn_classifier_cross_validation(model_class, X_sequence):
    train, test, vocab = X_sequence
    mod = model_class(vocab, max_iter=2)
    X, y = zip(*train)
    best_mod = utils.fit_classifier_with_crossvalidation(
        X, y, mod, cv=2, param_grid={'hidden_dim': [10, 20]})

def test_torch_shallow_neural_classifier_save_load(XOR):
    X, y = XOR
    mod = torch_shallow_neural_classifier.TorchShallowNeuralClassifier(
        hidden_dim=4,
        hidden_activation=nn.ReLU(),
        max_iter=100,
        eta=0.01)
    mod.fit(X, y)
    mod.predict(X)
    with tempfile.NamedTemporaryFile(mode='wb') as f:
        name = f.name
        mod.to_pickle(name)
        mod2 = torch_shallow_neural_classifier.TorchShallowNeuralClassifier.from_pickle(name)
        mod2.predict(X)
        mod2.fit(X, y)

def test_torch_autoencoder_save_load():
    X = utils.randmatrix(20, 50)
    mod = torch_autoencoder.TorchAutoencoder(hidden_dim=5, max_iter=2)
    mod.fit(X)
    mod.predict(X)
    with tempfile.NamedTemporaryFile(mode='wb') as f:
        name = f.name
        mod.to_pickle(name)
        mod2 = torch_autoencoder.TorchAutoencoder.from_pickle(name)
        mod2.predict(X)
        mod2.fit(X)

def test_torch_rnn_classifier_save_load(X_sequence):
    train, test, vocab = X_sequence
    mod = torch_rnn_classifier.TorchRNNClassifier(
        vocab=vocab, max_iter=2)
    X, y = zip(*train)
    X_test, _ = zip(*test)
    mod.fit(X, y)
    mod.predict(X)
    with tempfile.NamedTemporaryFile(mode='wb') as f:
        name = f.name
        mod.to_pickle(name)
        mod2 = torch_rnn_classifier.TorchRNNClassifier.from_pickle(name)
        mod2.predict(X_test)
        mod2.fit(X, y)

def test_torch_tree_nn_save_load(X_tree):
    X, vocab = X_tree
    mod = torch_tree_nn.TorchTreeNN(
        vocab,
        embed_dim=50,
        hidden_dim=50,
        max_iter=100,
        embedding=None)
    mod.fit(X)
    mod.predict(X)
    with tempfile.NamedTemporaryFile(mode='wb') as f:
        name = f.name
        mod.to_pickle(name)
        mod2 = torch_tree_nn.TorchTreeNN.from_pickle(name)
        mod2.predict(X)
        mod2.fit(X)


text:
import numpy as np
from queue import Queue
import logging
import networkx as nx
from graphviz import Digraph
from retro_star.alg.mol_node import MolNode
from retro_star.alg.reaction_node import ReactionNode
from retro_star.alg.syn_route import SynRoute

class MolTree:
    def __init__(self, target_mol, known_mols, value_fn, zero_known_value=True):
        self.target_mol = target_mol
        self.known_mols = known_mols
        self.value_fn = value_fn
        self.zero_known_value = zero_known_value
        self.mol_nodes = []
        self.reaction_nodes = []

        self.root = self._add_mol_node(target_mol, None)
        self.succ = target_mol in known_mols
        self.search_status = 0

        if self.succ:
            logging.info('Synthesis route found: target in starting molecules')

    def _add_mol_node(self, mol, parent):
        is_known = mol in self.known_mols

        init_value = self.value_fn(mol)

        mol_node = MolNode(
            mol=mol,
            init_value=init_value,
            parent=parent,
            is_known=is_known,
            zero_known_value=self.zero_known_value
        )
        self.mol_nodes.append(mol_node)
        mol_node.id = len(self.mol_nodes)

        return mol_node

    def _add_reaction_and_mol_nodes(self, cost, mols, parent, template, ancestors):
        assert cost >= 0

        for mol in mols:
            if mol in ancestors:
                return

        reaction_node = ReactionNode(parent, cost, template)
        for mol in mols:
            self._add_mol_node(mol, reaction_node)
        reaction_node.init_values()
        self.reaction_nodes.append(reaction_node)
        reaction_node.id = len(self.reaction_nodes)

        return reaction_node

    def expand(self, mol_node, reactant_lists, costs, templates):
        assert not mol_node.is_known and not mol_node.children

        if costs is None:      # No expansion results
            assert mol_node.init_values(no_child=True) == np.inf
            if mol_node.parent:
                mol_node.parent.backup(np.inf, from_mol=mol_node.mol)
            return self.succ

        assert mol_node.open
        ancestors = mol_node.get_ancestors()
        for i in range(len(costs)):
            self._add_reaction_and_mol_nodes(costs[i], reactant_lists[i],
                                             mol_node, templates[i], ancestors)

        if len(mol_node.children) == 0:      # No valid expansion results
            assert mol_node.init_values(no_child=True) == np.inf
            if mol_node.parent:
                mol_node.parent.backup(np.inf, from_mol=mol_node.mol)
            return self.succ

        v_delta = mol_node.init_values()
        if mol_node.parent:
            mol_node.parent.backup(v_delta, from_mol=mol_node.mol)

        if not self.succ and self.root.succ:
            logging.info('Synthesis route found!')
            self.succ = True

        return self.succ

    def get_best_route(self):
        if not self.succ:
            return None,[]

        syn_route = SynRoute(
            target_mol=self.root.mol,
            succ_value=self.root.succ_value,
            search_status=self.search_status
        )

        mol_queue = Queue()
        best_reactions=[]
        mol_queue.put(self.root)
        full_reactions = []
        while not mol_queue.empty():
            mol = mol_queue.get()
            if mol.is_known:
                syn_route.set_value(mol.mol, mol.succ_value)
                continue

            best_reaction = None
            for reaction in mol.children:
                if reaction.succ:
                    if best_reaction is None or \
                            reaction.succ_value < best_reaction.succ_value:
                        best_reaction = reaction
            assert best_reaction.succ_value == mol.succ_value

            reactants = []
            for reactant in best_reaction.children:
                mol_queue.put(reactant)
                reactants.append(reactant.mol)

            syn_route.add_reaction(
                mol=mol.mol,
                value=mol.succ_value,
                template=best_reaction.template,
                reactants=reactants,
                cost=best_reaction.cost
            )
            str_product = mol.mol
            str_template = best_reaction.template
            str_reactants = ".".join(reactants)
            str_reaction = "*".join([str_product, str_reactants, str_template])
            full_reactions.append(str_reaction)
        #print(full_reactions)

        return syn_route, full_reactions

    def viz_search_tree(self, viz_file):
        G = Digraph('G', filename=viz_file)
        G.attr(rankdir='LR')
        G.attr('node', shape='box')
        G.format = 'pdf'

        node_queue = Queue()
        node_queue.put((self.root, None))
        while not node_queue.empty():
            node, parent = node_queue.get()

            if node.open:
                color = 'lightgrey'
            else:
                color = 'aquamarine'

            if hasattr(node, 'mol'):
                shape = 'box'
            else:
                shape = 'rarrow'

            if node.succ:
                color = 'lightblue'
                if hasattr(node, 'mol') and node.is_known:
                    color = 'lightyellow'

            G.node(node.serialize(), shape=shape, color=color, style='filled')

            label = ''
            if hasattr(parent, 'mol'):
                label = '%.3f' % node.cost
            if parent is not None:
                G.edge(parent.serialize(), node.serialize(), label=label)

            if node.children is not None:
                for c in node.children:
                    node_queue.put((c, node))

        G.render()

max_stars_repo_name: tsudalab/rxngenerator

max_issues_repo_name: tsudalab/rxngenerator

max_forks_repo_name: tsudalab/rxngenerator

reason: import numpy,import networkx


text:
import os
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def warmupExercise():
	A=np.eye(4)
	print(A)
	return A
warmupExercise()
data = np.loadtxt(os.path.join('C:/Users/mulla/Desktop/values.txt'),delimiter=',')
X, y=data[:,0],data[:,1]
m=y.size
def plotdata(x,y):
	plt.plot(x,y,'ro',ms=10,mec='k')
	plt.xlabel('the profit 10,000$s')
	plt.ylabel('population in 10,000s')
	plt.show()
plotdata(X,y)
X = np.stack([np.ones(m), X], axis=1)
def computecost(x,y,theta):
	n= len(x)
	y_predc=np.dot(X, theta)
	temp = np.power((y-y_predc), 2)
	jtheta=np.sum(temp) / (2 * n)
	return jtheta
jtheta =computecost(X,y,theta=np.array([0.0, 0.0]))
print('With theta = [0, 0] \nCost computed = %.2f' % jtheta)
print('Expected cost value (approximately) 32.07\n')
jtheta = computecost(X, y, theta=np.array([-1, 2]))
print('With theta = [-1, 2]\nCost computed = %.2fr' % jtheta)
print('Expected cost value (approximately) 54.24')
def gradientdecsent(x,y,theta,alpha):
	iterations=1500
	m = len(X)
	theta = theta.copy()
	J_history = []
	for i in range(iterations):
		theta = theta - (alpha / m) * (np.dot(X, theta) - y).dot(X)
		J_history.append(computecost(X, y, theta))
	return theta, J_history
theta=np.zeros(2)
alpha=0.01
theta,J_history=gradientdecsent(X,y,theta,alpha)
print('Theta found by gradient descent: {:.4f}, {:.4f}'.format(*theta))
print('Expected theta values (approximately): [-3.6303, 1.1664]')
plotdata(X[:,1],y)
plt.plot(X[:,1],np.dot(X, theta),'-')
plt.legend(['Training data','Lining Regression'])
fprediction=np.dot([1,3.5],theta)
print('the predicted profit for 35,000 people is {:.2f}\n'.format(fprediction*10000))
sprediction=np.dot([1,7],theta)
print('the predicted profit for 70000 people is {:.2f}\n'.format(sprediction*10000))
theta0_vals = np.linspace(-10, 10, 100)
theta1_vals = np.linspace(-1, 4, 100)
J_vals = np.zeros((theta0_vals.shape[0], theta1_vals.shape[0]))
for i, theta0 in enumerate(theta0_vals):
    for j, theta1 in enumerate(theta1_vals):
        J_vals[i, j] = computecost(X, y, [theta0, theta1])
J_vals = J_vals.T
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(121, projection='3d')
ax.plot_surface(theta0_vals, theta1_vals, J_vals, cmap='viridis')
plt.xlabel('theta0')
plt.ylabel('theta1')
plt.title('Surface')
ax = plt.subplot(122)
plt.contour(theta0_vals, theta1_vals, J_vals, linewidths=2, cmap='viridis', levels=np.logspace(-2, 3, 20))
plt.xlabel('theta0')
plt.ylabel('theta1')
plt.plot(theta[0], theta[1], 'ro', ms=10, lw=2)
plt.title('Contour, showing minimum')
plt.show()
data = np.loadtxt(os.path.join('C:/Users/mulla/Desktop/values2.txt'),delimiter=',')
X, y=data[:,:2],data[:,2]
m = y.size
print('{:>8s}{:>8s}{:>10s}'.format('X[:,0]', 'X[:, 1]', 'y'))
print('-'*26)
for i in range(10):
    print('{:8.0f}{:8.0f}{:10.0f}'.format(X[i, 0], X[i, 1], y[i]))
def featurenormalization(X):
    mean = np.mean(X, axis=0)
    stddev = np.std(X, axis=0)
    result = (X - mean) / stddev
    return result, mean , stddev
result, mean, stddev = featurenormalization(X)
print('Computed mean of features:',mean)
print('computed standard deviation of features:',stddev)
X= np.concatenate([np.ones((m, 1)),result], axis=1)
def computecostmulti(X, y, theta):
    m=len(X)
    jtheta=0
    y_predc=np.dot(X,theta)
    jtheta=(1/2*m)*np.sum(np.square(np.dot(X,theta)-y))
    return jtheta
def gradientdescentmulti(X, y,theta,alpha):
	iterations=1500
	m = len(X)
	theta = theta.copy()
	J_history = []
	for i in range(iterations):
		theta = theta - (alpha / m) * (np.dot(X, theta) - y).dot(X)
		J_history.append(computecost(X, y, theta))
	return theta, J_history
data = np.loadtxt(os.path.join('C:/Users/mulla/Desktop/values2.txt'),delimiter=',')
X = data[:, :2]
y = data[:, 2]
m = y.size
X = np.concatenate([np.ones((m, 1)), X], axis=1)
def normaleq(X,y):
	m=y.size
	theta=np.dot(np.dot(np.linalg.inv(np.dot(X.T,X)),X.T),y)
	return theta
theta=normaleq(X,y)
X_array=[1,1650,3]
X_array[1:3] = (X_array[1:3] - mean) / stddev
price=np.dot(X_array,theta)
print('Predicted price of a 1650 sq-ft, 3 br house (using normal equations): ${:.0f}'.format(price))
print(X_array[1:3])
print(theta)

max_stars_repo_name: rahulrenegade/Python-Course-Results

max_issues_repo_name: rahulrenegade/Python-Course-Results

max_forks_repo_name: rahulrenegade/Python-Course-Results


text:
""" I/O for MHW Systems"""
import os
import datetime
import numpy as np

import pandas
import h5py

from cf_units import Unit
import iris

from oceanpy.sst import utils as sst_utils

from IPython import embed

def grab_mhw_sys_file(vary=False):
    if vary:
        mhw_sys_file = os.path.join(os.getenv('MHW'), 'db', 'MHW_systems_vary.hdf')
    else:
        mhw_sys_file = os.path.join(os.getenv('MHW'), 'db', 'MHW_systems.hdf')
    return mhw_sys_file

def grab_mhwsys_mask_file(vary=False):
    if vary:
        mhwsys_mask_file = os.path.join(os.getenv('MHW'), 'db', 'MHW_mask_vary.hdf')
    else:
        mhwsys_mask_file = os.path.join(os.getenv('MHW'), 'db', 'MHW_mask.hdf')
    return mhwsys_mask_file

def load_systems(mhw_sys_file=None, vary=False):
    """
    Load up the MHW Systems into a pandas table

    Args:
        mhw_sys_file:
        vary (bool, optional):
            If True, load up the MHW Systems with varying climate threshold

    Returns:
        pandas.DataFrame:

    """
    if mhw_sys_file is None:
        mhw_sys_file = grab_mhw_sys_file(vary=vary)
    # Read
    print("Loading systems from {}".format(mhw_sys_file))
    mhw_sys = pandas.read_hdf(mhw_sys_file)
    # Return
    return mhw_sys

def load_mask_from_dates(ymd_start, ymd_end,
                         mhw_mask_file=None, mask_start=(1982,1,1),
                         vary=False):
    """

    Args:
        ymd_start (tuple):
        ymd_end (tuple):
        mhw_mask_file:
        mask_start:

    Returns:

    """
    # Convert ymd to indices
    t0 = datetime.date(mask_start[0], mask_start[1], mask_start[2]).toordinal()

    ts = datetime.date(ymd_start[0], ymd_start[1], ymd_start[2]).toordinal()
    te = datetime.date(ymd_end[0], ymd_end[1], ymd_end[2]).toordinal()

    i0 = ts-t0
    i1 = te-t0+1  # Make it inclusive

    # Load + return
    return maskcube_from_slice(i0, i1, mhw_mask_file=mhw_mask_file, mask_start=mask_start,
                               vary=vary)

def load_mask_from_system(mhw_system,
                          mhw_mask_file=None, mask_start = (1982, 1, 1), vary=False):
    # Load + return
    print("Loading mask from system: \n {}".format(mhw_system))
    return maskcube_from_slice(mhw_system.zboxmin, mhw_system.zboxmax,
                               mhw_mask_file=mhw_mask_file, mask_start=mask_start,
                               vary=vary)

def maskcube_from_slice(i0,i1,
                        mhw_mask_file=None, mask_start=(1982, 1, 1),
                        vary=False):
    """

    Parameters
    ----------
    i0 : int
    i1 : int
    mhw_mask_file : str, optional
    mask_start : tuple, optional

    Returns
    -------
    mcube : iris.cube.Cube

    """
    if mhw_mask_file is None:
        mhw_mask_file = grab_mhwsys_mask_file(vary=vary)

    t0 = datetime.date(mask_start[0], mask_start[1], mask_start[2]).toordinal()
    ts = t0 + i0
    te = t0 + i1

    # Load from HDF
    print("Loading mask from {}".format(mhw_mask_file))
    f = h5py.File(mhw_mask_file, mode='r')
    mask = f['mask'][:,:,i0:i1+1]
    f.close()

    # Convert to IRIS
    tunit = Unit('days since 01-01-01 00:00:00', calendar='gregorian')
    times = np.arange(ts, te+1)
    time_coord = iris.coords.DimCoord(times, standard_name='time', units=tunit)

    # Space
    lat_coord, lon_coord = sst_utils.noaa_oi_coords(as_iris_coord=True)

    # Iris
    mcube = iris.cube.Cube(mask, var_name='Mask',
                           dim_coords_and_dims=[
                               (lat_coord, 0),
                               (lon_coord, 1),
                               (time_coord, 2), ])
    # Return
    return mcube


text:
from __future__ import division
import pytest
import os
import numpy as np
from qupy import Qubits
from qupy.operator import X, rx, swap

dtype = getattr(np, os.environ.get('QUPY_DTYPE', 'complex128'))
device = int(os.environ.get('QUPY_GPU', -1))

if device >= 0:
    import cupy
    cupy.cuda.Device(device).use()
    xp = cupy
else:
    xp = np

def _allclose(x0, x1):
    if device >= 0:
        return np.allclose(xp.asnumpy(x0), xp.asnumpy(x1))
    return np.allclose(x0, x1)

# famous formula
def test_swap_is_3cnot():
    q = Qubits(2)
    q.gate(rx(0.1), target=0)
    q.gate(rx(0.1), target=1, control=0)
    q.gate(swap, target=(0, 1))
    psi1 = q.state

    q = Qubits(2)
    q.gate(rx(0.1), target=0)
    q.gate(rx(0.1), target=1, control=0)
    q.gate(X, target=0, control=1)
    q.gate(X, target=1, control=0)
    q.gate(X, target=0, control=1)
    psi2 = q.state

    assert _allclose(psi1, psi2)

max_stars_repo_name: ken-nakanishi/qupy

max_issues_repo_name: ken-nakanishi/qupy

max_forks_repo_name: ken-nakanishi/qupy

reason: import numpy,import cupy