text:
import gym
import numpy as np
import pickle
env = gym.make('CartPole-v0')

# There are four variable, use a linear model W*x + b so actually five params

# Given some param, run a full episode and compute reward
def run_episode(W, max_step=1000, render=False):
    observation = env.reset()
    total_reward = 0
    N_step = 0
    done = False
    while not done and N_step <= max_step:
        if render:
            env.render()
        b = np.append(observation, 1.0)
        action = 1 if np.dot(W, b) > 0 else 0
        observation, reward, done, info = env.step(action)
        total_reward += reward
        N_step += 1

    return total_reward, N_step

train_episode = 10000
save_model = 10
def train():
    best_W = []
    best_reward = 0
    for i in range(train_episode):
        # generate random param
        W = np.random.rand(5) * 2 - 1
        reward, t = run_episode(W)
        print("Episode {} finished after {} timesteps".format(i, t+1))
        if reward > best_reward:
            best_reward = reward
            best_W = W
        if i % save_model == 0:
            pickle.dump(best_W, open('random_model.p', 'wb'))

    return best_W, best_reward

load_model = True
def run():
    if load_model:
        W = pickle.load(open('random_model.p', 'rb'))
    else:
        W, best_reward = train()
        print ("Best model reward = {}".format(best_reward))

    for i in range(20):
        _, t = run_episode(W, max_step=1000, render=True)
        print("Episode {} finished after {} timesteps".format(i, t+1))

run()

max_stars_repo_name: iveney/openai-gym

max_issues_repo_name: iveney/openai-gym

max_forks_repo_name: iveney/openai-gym


text:
import os
import argparse
import json
import numpy as np
import pandas as pd
import torch
import shutil

from autoPyTorch import AutoNetImageClassificationMultipleDatasets

def evaluate_config(run_id, config_dir, config, logdir):
    torch.backends.cudnn.benchmark = True

    run_id = run_id
    logdir = os.path.join(logdir, "run_" + str(run_id))
    config_dir = config_dir

    autonet_config = {
        "min_workers": 1,
        "budget_type": "epochs",
        "default_dataset_download_dir": "./datasets/",
        "images_root_folders": ["./datasets/cifar10/"],
        "train_metric": "accuracy",
        "additional_metrics": ["cross_entropy"],
        "validation_split": 0.2,
        "use_tensorboard_logger": True,
        "networks": ['darts'],
        "images_shape": [3, 32, 32],
        "log_level": "info",
        "random_seed": 1,
        "run_id": str(run_id),
        "result_logger_dir": logdir,
        "dataloader_worker": 2}

    # Initialize
    autonet = AutoNetImageClassificationMultipleDatasets(**autonet_config)

    if config is None:
        # Read hyperparameter config
        with open(config_dir, "r") as f:
            hyperparameter_config = json.load(f)
    else:
        with open(config, "r") as f:
            hyperparameter_config = json.load(f)
        #hyperparameter_config = json.loads(config)

    # NOTE: 'budget' has to be set here according to the T_max value from the config
    hyperparameter_config["NetworkSelectorDatasetInfo:network"] = "darts"
    print(hyperparameter_config)
    print(autonet.get_hyperparameter_search_space())
    budget = hyperparameter_config['SimpleLearningrateSchedulerSelector:cosine_annealing:T_max']
    print('budget', budget)

    autonet_config = autonet.get_current_autonet_config()
    result = autonet.refit(X_train=np.array([os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                             "datasets/CIFAR10.csv")]), Y_train=np.array([0]),
                           X_valid=None, Y_valid=None,
                           hyperparameter_config=hyperparameter_config,
                           autonet_config=autonet_config,
                           budget=budget, budget_type="epochs")

    print("Done with refitting.")

    torch.cuda.empty_cache()

    # Score
    with torch.no_grad():
        df = pd.read_csv("./datasets/cifar10/CIFAR_32_test.csv",
                         header=None).values
        X_test = np.array([path for path in df[:, 0]])
        Y_test = np.array(df[:, 1]).astype(np.int32)
        score = autonet.score(X_test=X_test, Y_test=Y_test)
        result["test_accuracy"] = score

    # Dump
    with open(os.path.join(logdir, "final_output.json"), "w+") as f:
        json.dump(result, f)

    return result

def get_config_dir(config_parent_dir, run_id):
    config_dirs = [os.path.join(config_parent_dir, p) for p in os.listdir(config_parent_dir) if p.startswith("config_")]
    config_dirs.sort(key=lambda x: int(x.replace(".json", "").split("_")[-1]))
    return config_dirs[run_id-1]

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Fit a random config on CIFAR task')
    parser.add_argument("--run_id", type=int, help="An id for the run.")
    parser.add_argument("--config_parent_dir", type=str, help="Path to config.json", default="./configs")
    parser.add_argument("--config", type=str, help="Config as json string", default=None)
    parser.add_argument("--logdir", type=str, help="Directory the results are written to.", default="logs/darts_proxy")
    parser.add_argument("--offset", type=int, help="An id for the run.", default=0)
    args = parser.parse_args()
    run_id = args.run_id + args.offset
    config_dir = get_config_dir(args.config_parent_dir, run_id)
    print(config_dir)

    rundir = "run_"+str(run_id)

    if not os.path.exists(args.logdir):
        os.makedirs(args.logdir)

    if rundir in os.listdir(args.logdir):
        subdirs = os.listdir(os.path.join(args.logdir, rundir))
        if ("final_output.json" in subdirs) and ("bohb_status.json" in subdirs):
            pass
        else:
            shutil.rmtree(os.path.join(args.logdir, rundir))
            evaluate_config(run_id=run_id, config_dir=config_dir, config=args.config, logdir=args.logdir)
    else:
        evaluate_config(run_id=run_id, config_dir=config_dir, config=args.config, logdir=args.logdir)

ext: py

reason: import numpy


text:
import numpy as np
import sys
from tqdm import tqdm
import random
from functools import partial
from rdkit import Chem
from multiprocessing import Pool
from multiobj_rationale.properties import gsk3_model, jnk3_model
from multiobj_rationale.mcts import mcts
from MolEvol.common.fingerprint import get_fingerprint_as_array, select_atoms, extract_selected_subgraph, \
    extract_selected_subgraph_for_gcn
from MolEvol.common.parse_args import args
from multiobj_rationale.properties import qed_func, sa_func

# silence sklearn warnings
import warnings

warnings.filterwarnings('ignore')

class dual_gsk3_jnk3_model_4prop():
    """Scores based on an ECFP classifier for activity."""

    def __init__(self, combine_method='geo'):
        self.gsk3_model = gsk3_model()
        self.jnk3_model = jnk3_model()
        self.qed_model = qed_func()
        self.sa_model = sa_func()
        self.combine_method = combine_method

    def __call__(self, smiles_list):
        scores_gsk3 = self.gsk3_model(smiles_list)
        scores_jnk3 = self.jnk3_model(smiles_list)
        scores_sa = self.sa_model(smiles_list)
        scores_qed = self.qed_model(smiles_list)
        assert self.combine_method == 'geo'
        scores_final = np.float32(
            (scores_gsk3 * scores_jnk3 * scores_qed * (1.0 / scores_sa / 0.9 - 1.0 / 9)) ** (1 / 4))
        assert np.max(scores_final) <= 1, (scores_gsk3, scores_jnk3, scores_sa, scores_qed, scores_final)
        return scores_final

class dual_gsk3_jnk3_model():
    """Scores based on an ECFP classifier for activity."""

    def __init__(self, combine_method='geo'):
        self.gsk3_model = gsk3_model()
        self.jnk3_model = jnk3_model()
        self.combine_method = combine_method

    def __call__(self, smiles_list):
        scores_gsk3 = self.gsk3_model(smiles_list)
        scores_jnk3 = self.jnk3_model(smiles_list)

        if self.combine_method == 'geo':
            return np.float32(np.sqrt(scores_gsk3 * scores_jnk3))
        else:
            return np.float32((scores_jnk3 + scores_gsk3) / 2.0)

    def compute_gradient(self, smiles):
        mol = Chem.MolFromSmiles(smiles)
        if mol is None:
            return None

        fp, info = get_fingerprint_as_array(mol)

        fps = [fp]
        for onbit, subgraphs in info.items():
            step_fp = fp.copy()
            step_fp[0, onbit] = 0
            fps.append(step_fp)

        fps = np.concatenate(fps, axis=0)
        scores_gsk3 = np.float32(self.gsk3_model.clf.predict_proba(fps)[:, 1])
        scores_jnk3 = np.float32(self.jnk3_model.clf.predict_proba(fps)[:, 1])
        combined_scores = np.sqrt(scores_gsk3 * scores_jnk3)

        gradient = combined_scores[1:] - combined_scores[0]

        return info, gradient

DM = dual_gsk3_jnk3_model()
QM = dual_gsk3_jnk3_model_4prop()

def local_for_one_molecule(smiles_idx, num_rounds):
    smiles, idx = smiles_idx
    np.random.seed(idx)
    random.seed(idx)
    mol = Chem.MolFromSmiles(smiles)
    rationale_list = []
    if mol.GetNumAtoms() > 50:
        return smiles, [None]

    assert args.l2x_gcn
    if args.l2x_gcn:
        from MolEvol.explainer.explain import L2X_explain
        onbit_list, importance_list = L2X_explain(smiles)

    tot = sum(importance_list)
    importance_list = [x / tot for x in importance_list]

    min_len, max_len = 3, 5  # int(total_len * 0.3)
    candidate_len_population = [i for i in range(min_len, max_len + 1)]
    candidate_len_list = random.choices(candidate_len_population, k=num_rounds)

    rset = set()
    for num_info in candidate_len_list:
        num_info = min(num_info, len(onbit_list))
        selected_atoms = np.random.choice(onbit_list, num_info, p=importance_list, replace=False)
        selected_atoms = set(int(atom) for atom in selected_atoms)
        # print(selected_atoms)
        subgraph_smiles = extract_selected_subgraph_for_gcn(smiles, selected_atoms)
        minimum_smiles = subgraph_smiles
        if minimum_smiles is None:
            continue
        if minimum_smiles not in rset and mol.GetNumAtoms() > Chem.MolFromSmiles(minimum_smiles).GetNumAtoms():
            rationale_list.append(minimum_smiles)
            rset.add(minimum_smiles)

    return smiles, rationale_list

def do_local_search_disc(molecules, args):
    import time
    st = time.time()
    mol_id_list = [(mol, idx) for idx, mol in enumerate(molecules)]

    work_func = partial(
        local_for_one_molecule,
        num_rounds=args.num_local_rounds
    )

    pool = Pool(args.ncpu)
    results = pool.map(work_func, mol_id_list)
    pool.close()

    np.random.seed(args.seed)
    random.seed(args.seed)

    print('Time Elapsed:', time.time() - st)
    print('Collect Results...')
    sys.stdout.flush()
    gsls_tuples = []

    all_rationales = set()
    orig_dict = {}
    for orig_smiles, rationales in results:
        sr = set(rationales)
        all_rationales.update(sr)
        for r in sr:
            orig_dict[r] = orig_smiles

    print('Unique Results...')
    print('Time Elapsed:', time.time() - st)
    sys.stdout.flush()
    if args.prop4:
        eval_model = QM
    else:
        eval_model = DM

    if None in all_rationales:
        all_rationales.remove(None)
    len_limit = 500000
    total_len = min(len_limit, len(all_rationales))
    all_rationales_uniq = random.sample(all_rationales, k=total_len)
    scr_list = []
    interval = 50000
    for i in range(0, total_len, interval):
        print('Eval {} to {}...'.format(i, i + interval - 1))
        scr_list += list(eval_model(all_rationales_uniq[i:i + interval]).reshape(-1))
    assert len(scr_list) == total_len

    print('Length of Unique:', len(scr_list))
    print('Time Elapsed:', time.time() - st)
    sys.stdout.flush()
    for r_smiles, scr in (zip(tqdm(all_rationales_uniq), scr_list)):
        mol = Chem.MolFromSmiles(r_smiles)
        if mol.GetNumAtoms() <= args.max_atoms and scr >= args.scr_thres:
            gsls_tuples.append((orig_dict[r_smiles], r_smiles, mol.GetNumAtoms(), scr))
            # print(r_smiles, DM([r_smiles])[0])

    return gsls_tuples

if __name__ == '__main__':
    dual_model = dual_gsk3_jnk3_model()
    # dual_model.gsk3_model.feature_importances_

    smiles = 'C=C(C)CS(=O)c1ccc(Nc2nccc(-c3ccccc3)n2)cc1'
    info, gradient = dual_model.compute_gradient(smiles)

    # lower gradient matters
    for i, (onbit, _) in enumerate(info.items()):
        print(f'onbit={onbit}, gradient={gradient[i]}')

max_stars_repo_name: AspirinCode/MolEvol

max_issues_repo_name: AspirinCode/MolEvol

max_forks_repo_name: binghong-ml/MolEvol


text:
import argparse
import numpy as np
import time
import sys

import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.join(os.path.dirname(currentdir),'src')
sys.path.insert(0,parentdir)
from tasks.task_template import TaskTemplate

class DemoFSLTask(TaskTemplate):

    @staticmethod
    def get_parser(parser=argparse.ArgumentParser()):
        parser.add_argument('--support_idx', type=list, default=[],
                            help="List of idxs for supports of a task")
        parser.add_argument('--support_lbls', type=list, default=[],
                            help="List of labels for the supports of a task")
        parser.add_argument('--target_idx', type=list, default=[],
                            help="List of idxs for targets of a task")
        parser.add_argument('--target_lbls', type=list, default=[],
                            help="List of labels for the targets of a task")
        return parser

    @staticmethod
    def get_output_dim(args, dataset):
        return dataset.get_num_classes()

    def __init__(self, dataset, args, class_seed, sample_seed):
        """
        Few Shot Learning Task sampler for creating a single episode for a few-shot learning task
        """
        super().__init__(dataset, args, class_seed, sample_seed)
        self.support_idx = args.support_idx
        self.target_idx = args.target_idx
        self.support_lbls = args.support_lbls
        self.target_lbls = args.target_lbls

    def set_targarts(self, target_idx, target_lbls):
        self.target_idx = target_idx
        self.target_lbls = target_lbls

    def set_supports(self, support_idx, support_lbls):
        self.support_idx = support_idx
        self.support_lbls = support_lbls

    def __len__(self):
        return 1

    def __iter__(self):
        supports_x = self.support_idx
        supports_y = self.support_lbls
        targets_x = self.target_idx
        targets_y = self.target_lbls

        rng = np.random.RandomState(self.sample_seed)
        support_seeds = rng.randint(0, 999999999, len(supports_y))
        target_seeds = rng.randint(0, 999999999, len(targets_y))
        supports_y = zip(supports_y, support_seeds)
        targets_y = zip(targets_y, target_seeds)

        support_set = (supports_x, supports_y)
        target_set = (targets_x, targets_y)

        yield (support_set, target_set)


text:
# This script contains tools for the codebase.

import importlib
import math
import numpy as np

def get_module_class(d, c):
    """Load module m in directory d with the class name c."""

    m = importlib.import_module('.'.join([d, c]))

    return getattr(m, c)

def apply_trans(ts, modlist):
    """Apply a series of transformative modules."""

    for m in modlist:

        ts = m.apply(ts)

    return ts

def append_results(results, base, c, conf):
    """Append results before calculating metrics."""

    results.append({'truth name': base['name'],
                    'model name': c['name'],
                    'path': c['path'],
                    'location': conf['location'],
                    'var': c['var']}
                   )

    return results

def calculate_angle_diff(x_i, y_i):
    """Calculate angle difference between two wind directions.
    Converting wind direction angle into u and v unit vectors,
    to avoid 0-360 degree split.
    It only yields positive angle difference.
    """

    u_x = -math.sin(math.radians(x_i))
    v_x = -math.cos(math.radians(x_i))

    u_y = -math.sin(math.radians(y_i))
    v_y = -math.cos(math.radians(y_i))

    if u_x == u_y and v_x == v_y:
        angle = 0
    else:
        angle = math.degrees(math.acos((u_x*u_y + v_x*v_y)))

    return angle

def get_wd_angle_diff_series(x, y):
    """Convert x and y time series into one series of wind direction
    difference in degrees.
    """

    angle_series = np.empty(len(x))

    for i, (x_i, y_i) in enumerate(zip(x, y)):

        angle_series[i] = calculate_angle_diff(x_i, y_i)

    return angle_series


text:
import json
import uuid
from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union

import numpy as np
import pandas as pd
import xarray as xr
from semantic_version import Version
from sklearn.neighbors import NearestNeighbors
from slicedimage.io import resolve_path_or_url

from starfish.core.codebook._format import (
    CURRENT_VERSION,
    DocumentKeys,
    MAX_SUPPORTED_VERSION,
    MIN_SUPPORTED_VERSION,
)
from starfish.core.config import StarfishConfig
from starfish.core.imagestack import indexing_utils
from starfish.core.intensity_table.decoded_intensity_table import DecodedIntensityTable
from starfish.core.intensity_table.intensity_table import IntensityTable
from starfish.core.spacetx_format.util import CodebookValidator
from starfish.core.types import Axes, Features, Number

NormalizedFeaturesArgtype = TypeVar("NormalizedFeaturesArgtype", "Codebook", IntensityTable)

class Codebook(xr.DataArray):
    """Codebook for an image-based transcriptomics experiment

    The codebook is a three dimensional tensor with shape :code:`(feature, round, channel)` whose
    values are the expected intensity of features (spots or pixels) that correspond to each target
    (gene or protein) in each of the image tiles of an experiment.

    This class supports the construction of synthetic codebooks for testing, and exposes decode
    methods to assign target identifiers to spots. This codebook provides an in-memory
    representation of the codebook defined in the SpaceTx format.

    The codebook is a subclass of xarray, and exposes the complete public API of that package in
    addition to the methods and constructors listed below.

    Examples
    --------
    Build a codebook using :py:meth:`Codebook.synthetic_one_hot_codebook`::

        >>> from starfish import Codebook
        >>> sd = Codebook.synthetic_one_hot_codebook(n_round=4, n_channel=3, n_codes=2)
        >>> sd.codebook()
        <xarray.Codebook (target: 2, r: 4, c: 3)>
        array([[[1, 0, 0],
                [0, 0, 1],
                [1, 0, 0],
                [0, 1, 0]],

               [[1, 0, 0],
                [1, 0, 0],
                [1, 0, 0],
                [1, 0, 0]]], dtype=uint8)
        Coordinates:
          * target   (target) object 6d7fff11-8905-4421-ab49-4f6d8ecdb4b7 \
    1f5f7087-0618-49fc-a6a5-82fee14360b3
          * r        (r) int64 0 1 2 3
          * c        (c) int64 0 1 2

    """

    __slots__ = ()

    @property
    def code_length(self) -> int:
        """return the length of codes in this codebook"""
        return int(np.dot(*self.shape[1:]))

    @classmethod
    def zeros(cls, code_names: Sequence[str], n_round: int, n_channel: int):
        """
        Create an empty codebook of shape (code_names, n_round, n_channel)

        Parameters
        ----------
        code_names : Sequence[str]
            The targets to be coded.
        n_round : int
            Number of imaging rounds used to build the codes.
        n_channel : int
            Number of channels used to build the codes.

        Examples
        --------
        Build an empty 2-round 3-channel codebook::

            >>> from starfish import Codebook
            >>> Codebook.zeros(['ACTA', 'ACTB'], n_round=2, n_channel=4)
            <xarray.Codebook (target: 2, r: 2, c: 4)>
            array([[[0, 0, 0, 0],
                    [0, 0, 0, 0]],

                   [[0, 0, 0, 0],
                    [0, 0, 0, 0]]], dtype=uint8)
            Coordinates:
              * target   (target) object 'ACTA' 'ACTB'
              * r        (r) int64 0 1
              * c        (c) int64 0 1 2 3

        Returns
        -------
        Codebook :
            codebook whose values are all zero

        """
        data = np.zeros((len(code_names), n_round, n_channel), dtype=np.uint8)
        return cls.from_numpy(code_names, n_round, n_channel, data)

    @classmethod
    def from_numpy(
            cls,
            code_names: Sequence[str],
            n_round: int,
            n_channel: int,
            data: np.ndarray,
    ) -> "Codebook":
        """create a codebook of shape (code_names, n_round, n_channel) from a 3-d numpy array

        Parameters
        ----------
        code_names : Sequence[str]
            the targets to be coded
        n_round : int
            number of imaging rounds used to build the codes
        n_channel : int
            number of channels used to build the codes
        data : np.ndarray
            array of unit8 values with len(code_names) x n_channel x n_round elements

        Examples
        --------
        Build a 3-round 4-channel codebook where :code:`ACTA` is specified by intensity in round 0,
        channel 1, and :code:`ACTB` is coded by fluorescence in channels 0, 1, and 2 of rounds 0,
        1, and 2.
        ::

            >>> import numpy as np
            >>> from starfish import Codebook
            >>> data = np.zeros((2,3,4), dtype=np.uint8)
            >>> data[0, 0, 1] = 1                 # ACTA
            >>> data[[1, 1, 1], [0, 1, 2], [0, 1, 2]] = 1  # ACTB
            >>> Codebook.from_numpy(['ACTA', 'ACTB'], n_channel=4, n_round=3, data=data)
            <xarray.Codebook (target: 2, r: 3, c: 4)>
            array([[[0, 1, 0, 0],
                    [0, 0, 0, 0],
                    [0, 0, 0, 0]],

                   [[1, 0, 0, 0],
                    [0, 1, 0, 0],
                    [0, 0, 1, 0]]], dtype=uint8)
            Coordinates:
              * target   (target) object 'ACTA' 'ACTB'
              * r        (r) int64 0 1 2
              * c        (c) int64 0 1 2 3

        Returns
        -------
        Codebook :
            codebook with filled values

        """
        return cls(
            data=data,
            coords=(
                pd.Index(code_names, name=Features.TARGET),
                pd.Index(np.arange(n_round), name=Axes.ROUND.value),
                pd.Index(np.arange(n_channel), name=Axes.CH.value),
            )
        )

    @classmethod
    def _verify_version(cls, semantic_version_str: str) -> None:
        version = Version(semantic_version_str)
        if not (MIN_SUPPORTED_VERSION <= version <= MAX_SUPPORTED_VERSION):
            raise ValueError(
                f"version {version} not supported.  This version of the starfish library only "
                f"supports codebook formats from {MIN_SUPPORTED_VERSION} to "
                f"{MAX_SUPPORTED_VERSION}")

    @classmethod
    def from_code_array(
            cls, code_array: List[Dict[Union[str, Any], Any]],
            n_round: Optional[int] = None, n_channel: Optional[int] = None) -> "Codebook":
        """
        Construct a codebook from a python list of SpaceTx-Format codewords.

        Note: Loading the SpaceTx-Format codebook with :py:meth:`json.load` will produce a code
        array that can be passed to this constructor.

        Parameters
        ----------
        code_array : List[Dict[str, Any]]
            Array of dictionaries, each containing a codeword and target
        n_round : Optional[int]
            The number of imaging rounds used in the codes. Will be inferred if not provided
        n_channel : Optional[int]
            The number of channels used in the codes. Will be inferred if not provided

        Examples
        --------
        Construct a codebook from some array data in python memory
        ::

            >>> from starfish.types import Axes, Features
            >>> from starfish import Codebook
            >>> codebook = [
            >>>     {
            >>>         Features.CODEWORD: [
            >>>             {Axes.ROUND.value: 0, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>             {Axes.ROUND.value: 1, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>         ],
            >>>         Features.TARGET: "ACTB_human"
            >>>     },
            >>>     {
            >>>         Features.CODEWORD: [
            >>>             {Axes.ROUND.value: 0, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>             {Axes.ROUND.value: 1, Axes.CH.value: 1, Features.CODE_VALUE: 1},
            >>>         ],
            >>>         Features.TARGET: "ACTB_mouse"
            >>>     },
            >>> ]
            >>> Codebook.from_code_array(codebook)
            <xarray.Codebook (target: 2, r: 2, c: 4)>
            array([[[0, 0, 0, 1],
                    [0, 0, 0, 1]],

                   [[0, 0, 0, 1],
                    [0, 1, 0, 0]]], dtype=uint8)
            Coordinates:
              * target   (target) object 'ACTB_human' 'ACTB_mouse'
              * r        (r) int64 0 1
              * c        (c) int64 0 1 2 3

            Returns
            -------
            Codebook :
                Codebook with shape (targets, channels, imaging_rounds)

        """

        # guess the max round and channel if not provided, otherwise check provided values are valid
        max_round, max_ch = 0, 0

        # verify codebook structure and fields
        for code in code_array:

            if not isinstance(code, dict):
                raise ValueError(f'codebook must be an array of dictionary codes. Found: {code}.')

            # verify all necessary fields are present
            required_fields = {Features.CODEWORD, Features.TARGET}
            missing_fields = required_fields.difference(code)
            if missing_fields:
                raise ValueError(
                    f'Each entry of codebook must contain {required_fields}. Missing fields: '
                    f'{missing_fields}')

            for entry in code[Features.CODEWORD]:
                if not isinstance(entry, dict):
                    raise TypeError("codeword entries should be dictionaries")

                required_codeword_fields = {Axes.ROUND.value, Axes.CH.value, Features.CODE_VALUE}
                missing_codeword_fields = required_codeword_fields.difference(entry)
                if missing_codeword_fields:
                    raise ValueError(
                        f"Each codeword entry must contain {required_codeword_fields}. Missing "
                        f"fields: {missing_codeword_fields}")

        for code in code_array:
            for entry in code[Features.CODEWORD]:
                max_round = max(max_round, entry[Axes.ROUND])
                max_ch = max(max_ch, entry[Axes.CH])

        # set n_channel and n_round if either were not provided
        n_round = n_round if n_round is not None else max_round + 1
        n_channel = n_channel if n_channel is not None else max_ch + 1

        # raise errors if provided n_round or n_channel are out of range
        if max_round + 1 > n_round:
            raise ValueError(
                f'code detected that requires an imaging round value ({max_round + 1}) that is '
                f'greater than provided n_round: {max_round}')
        if max_ch + 1 > n_channel:
            raise ValueError(
                f'code detected that requires a channel value ({max_ch + 1}) that is greater '
                f'than provided n_channel: {n_channel}')

        target_names = [w[Features.TARGET] for w in code_array]

        # fill the codebook
        data = np.zeros((len(target_names), n_round, n_channel), dtype=np.uint8)
        for i, code_dict in enumerate(code_array):
            for bit in code_dict[Features.CODEWORD]:
                ch = int(bit[Axes.CH])
                r = int(bit[Axes.ROUND])
                data[i, r, ch] = int(bit[Features.CODE_VALUE])
        return cls.from_numpy(target_names, n_round, n_channel, data)

    @classmethod
    def open_json(
            cls, json_codebook: str,
            n_round: Optional[int] = None,
            n_channel: Optional[int] = None,
    ) -> "Codebook":
        """
        Load a codebook from a SpaceTx Format json file or a url pointing to such a file.

        Parameters
        ----------
        json_codebook : str
            Path or url to json file containing a spaceTx codebook.
        n_round : Optional[int]
            The number of imaging rounds used in the codes. Will be inferred if not provided.
        n_channel : Optional[int]
            The number of channels used in the codes. Will be inferred if not provided.

        Examples
        --------
        Create a codebook from in-memory data
        ::

            >>> from starfish.types import Axes, Features
            >>> from starfish import Codebook
            >>> import tempfile
            >>> import json
            >>> import os
            >>> dir_ = tempfile.mkdtemp()
            >>> codebook = [
            >>>     {
            >>>         Features.CODEWORD: [
            >>>             {Axes.ROUND.value: 0, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>             {Axes.ROUND.value: 1, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>         ],
            >>>         Features.TARGET: "ACTB_human"
            >>>     },
            >>>     {
            >>>         Features.CODEWORD: [
            >>>             {Axes.ROUND.value: 0, Axes.CH.value: 3, Features.CODE_VALUE: 1},
            >>>             {Axes.ROUND.value: 1, Axes.CH.value: 1, Features.CODE_VALUE: 1},
            >>>         ],
            >>>         Features.TARGET: "ACTB_mouse"
            >>>     },
            >>> ]
            >>> # make a fake file
            >>> json_codebook = os.path.join(dir_, 'codebook.json')
            >>> with open(json_codebook, 'w') as f:
            >>>     json.dump(codebook, f)
            >>> # read codebook from file
            >>> Codebook.open_json(json_codebook)
            <xarray.Codebook (target: 2, r: 2, c: 4)>
            array([[[0, 0, 0, 1],
                    [0, 0, 0, 1]],

                   [[0, 0, 0, 1],
                    [0, 1, 0, 0]]], dtype=uint8)
            Coordinates:
              * target   (target) object 'ACTB_human' 'ACTB_mouse'
              * r        (r) int64 0 1
              * c        (c) int64 0 1 2 3

        Returns
        -------
        Codebook :
            Codebook with shape (targets, channels, imaging_rounds)

        """

        config = StarfishConfig()

        backend, name, _ = resolve_path_or_url(json_codebook, backend_config=config.slicedimage)
        with backend.read_contextmanager(name) as fh:
            codebook_doc = json.load(fh)

            if config.strict:
                codebook_validator = CodebookValidator(codebook_doc)
                if not codebook_validator.validate_object(codebook_doc):
                    raise Exception("validation failed")

        if isinstance(codebook_doc, list):
            raise ValueError(
                "codebook is a list and not an dictionary.  It is highly likely that you are using"
                "a codebook formatted for a previous version of starfish.")

        version_str = codebook_doc[DocumentKeys.VERSION_KEY]
        cls._verify_version(version_str)

        return cls.from_code_array(codebook_doc[DocumentKeys.MAPPINGS_KEY], n_round, n_channel)

    def get_partial(self, indexers: Mapping[Axes, Union[int, slice, Sequence]]):
        """
        Slice the codebook data according to the provided indexing parameters. Used in a composite
        codebook scenario.

        Parameters
        ----------
        indexers : Mapping[Axes, Union[int, Sequence]]
            A dictionary of dim:index where index is the value, values or range to index the
            dimension
        """
        selector = indexing_utils.convert_to_selector(indexers)
        return indexing_utils.index_keep_dimensions(self, indexers=selector)

    def to_json(self, filename: Union[str, Path]) -> None:
        """
        Save a codebook to json using SpaceTx Format.

        Parameters
        ----------
        filename : Union[str, Path]
            The name of the file in which to save the codebook.

        """
        code_array = []
        for target_index in range(self.sizes[Features.TARGET]):
            codeword = []
            target_codeword = self[{Features.TARGET: target_index}]
            nonzero_indices = np.nonzero(target_codeword.values)
            for round_index, ch_index in zip(*nonzero_indices):
                codeword.append(
                    {
                        Axes.ROUND.value: int(self.coords[Axes.ROUND.value][round_index]),
                        Axes.CH.value: int(self.coords[Axes.CH.value][ch_index]),
                        Features.CODE_VALUE: float(target_codeword[round_index, ch_index])
                    })
            target_cell: np.ndarray = self.coords[Features.TARGET][target_index].values
            assert len(target_cell.shape) == 0
            target_name = str(target_cell)
            code_array.append({
                Features.CODEWORD: codeword,
                Features.TARGET: target_name,
            })
        codebook_document = {
            DocumentKeys.VERSION_KEY: str(CURRENT_VERSION),
            DocumentKeys.MAPPINGS_KEY: code_array,
        }

        with open(filename, 'w') as f:
            json.dump(codebook_document, f)

    @staticmethod
    def _normalize_features(
            array: NormalizedFeaturesArgtype,
            norm_order: int,
    ) -> Tuple[NormalizedFeaturesArgtype, np.ndarray]:
        """Unit normalize each feature of array

        Parameters
        ----------
        array : Union[Codebook, IntensityTable]
            codebook or intensity table containing (ch, r) features to normalize
        norm_order : int
            the norm to apply to each feature

        Notes
        -----
        The available norms for this function can be found at the following link:
        :doc:`numpy:reference/generated/numpy.linalg.norm`

        Returns
        -------
        Union[IntensityTable, Codebook] :
            IntensityTable or Codebook containing normalized features
        np.ndarray :
            A 1 dimensional numpy array containing the feature norms

        """
        feature_traces = array.stack(traces=(Axes.CH.value, Axes.ROUND.value))
        norm = np.linalg.norm(feature_traces.values, ord=norm_order, axis=1)
        array = array / norm[:, None, None]

        # if a feature is all zero, the information should be spread across the channel
        n = array.sizes[Axes.CH.value] * array.sizes[Axes.ROUND.value]
        partitioned_intensity = np.linalg.norm(np.full(n, fill_value=1 / n), ord=norm_order) / n
        array.values[np.logical_not(np.isfinite(array.values))] = partitioned_intensity

        return array, norm

    @staticmethod
    def _approximate_nearest_code(
            norm_codes: "Codebook", norm_intensities: xr.DataArray, metric: str,
    ) -> Tuple[np.ndarray, np.ndarray]:
        """find the nearest code for each feature using the ball_tree approximate NN algorithm

        Parameters
        ----------
        norm_codes : Codebook
            codebook with each code normalized to unit length (sum = 1)
        norm_intensities : IntensityTable
            intensity table with each feature normalized to unit length (sum = 1)
        metric : str
            the sklearn metric string to pass to NearestNeighbors

        Returns
        -------
        np.ndarray : metric_output
            the output of metric applied to each feature closest code
        np.ndarray : targets
            the gene that corresponds to each matched code

        Notes
        -----
        This function does not verify that the intensities have been normalized.

        """
        linear_codes = norm_codes.stack(traces=(Axes.CH.value, Axes.ROUND.value)).values
        linear_features = norm_intensities.stack(
            traces=(Axes.CH.value, Axes.ROUND.value)).values

        # reshape into traces
        nn = NearestNeighbors(n_neighbors=1, algorithm='ball_tree', metric=metric).fit(linear_codes)
        metric_output, indices = nn.kneighbors(linear_features)
        gene_ids = np.ravel(norm_codes.indexes[Features.TARGET].values[indices])

        return np.ravel(metric_output), gene_ids

    def _validate_decode_intensity_input_matches_codebook_shape(
            self,
            intensities: IntensityTable,
    ):
        """verify that the shapes of the codebook and intensities match"""
        ch_match = intensities.sizes[Axes.CH] == self.sizes[Axes.CH]
        round_match = intensities.sizes[Axes.ROUND] == self.sizes[Axes.ROUND]
        if not (ch_match and round_match):
            raise ValueError(
                'Codebook and Intensities must have same number of channels and rounds')

    def decode_metric(
            self, intensities: IntensityTable, max_distance: Number, min_intensity: Number,
            norm_order: int, metric: str='euclidean', return_original_intensities: bool = False
    ) -> DecodedIntensityTable:
        """
        Assigns intensity patterns that have been extracted from an :py:class:`ImageStack` and
        stored in an :py:class:`IntensityTable` by a :py:class:`SpotFinder` to the gene targets that
        they encode.

        This method carries out the assignment by first normalizing both the codes and the
        recovered intensities to be unit magnitude using an L2 norm, and then finds the closest
        code for each feature according to a distance metric (default=euclidean).

        Features greater than :code:`max_distance` from their nearest code, or that have an average
        intensity below :code:`min_intensity` are not assigned to any feature.

        Parameters
        ----------
        intensities : IntensityTable
            features to be decoded
        max_distance : Number
            maximum distance between a feature and its closest code for which the coded target will
            be assigned.
        min_intensity : Number
            minimum intensity for a feature to receive a target annotation
        norm_order : int
            the scipy.linalg norm to apply to normalize codes and intensities
        metric : str
            the sklearn metric string to pass to NearestNeighbors
        return_original_intensities: bool
            If True returns original intensity values in the DecodedIntensityTable instead of
            normalized ones (default=False)

        Notes
        -----
        The available norms for this function can be found at the following link:
        :doc:`numpy:reference/generated/numpy.linalg.norm`

        The available metrics for this function can be found at the following link:
        :doc:`scipy:spatial.distance`

        Returns
        -------
        IntensityTable :
            Intensity table containing normalized intensities, target assignments, distances to
            the nearest code, and the filtering status of each feature.

        """

        self._validate_decode_intensity_input_matches_codebook_shape(intensities)

        # add empty metadata fields and return
        if intensities.sizes[Features.AXIS] == 0:
            return DecodedIntensityTable.from_intensity_table(
                intensities,
                targets=(Features.AXIS, np.empty(0, dtype='U')),
                distances=(Features.AXIS, np.empty(0, dtype=np.float64)),
                passes_threshold=(Features.AXIS, np.empty(0, dtype=bool)))

        # normalize both the intensities and the codebook
        norm_intensities, norms = self._normalize_features(intensities, norm_order=norm_order)
        norm_codes, _ = self._normalize_features(self, norm_order=norm_order)

        metric_outputs, targets = self._approximate_nearest_code(
            norm_codes, norm_intensities, metric=metric)

        # only targets with low distances and high intensities should be retained
        passes_filters = np.logical_and(
            norms >= min_intensity,
            metric_outputs <= max_distance,
            dtype=np.bool
        )

        return_intensities = intensities if return_original_intensities else norm_intensities
        # norm_intensities is a DataArray, make it back into an IntensityTable
        return DecodedIntensityTable.from_intensity_table(
            return_intensities,
            targets=(Features.AXIS, targets),
            distances=(Features.AXIS, metric_outputs),
            passes_threshold=(Features.AXIS, passes_filters))

    def decode_per_round_max(self, intensities: IntensityTable) -> DecodedIntensityTable:
        """
        Assigns intensity patterns that have been extracted from an :py:class:`ImageStack` and
        stored in an :py:class:`IntensityTable` by a :py:class:`SpotFinder` to the gene targets that
        they encode.

        This method carries out the assignment by identifying the maximum-intensity channel for each
        round, and assigning each spot to a code if the maximum-intensity pattern exists in the
        codebook.

        This method is only compatible with one-hot codebooks, where exactly one channel is expected
        to contain fluorescence in each imaging round. This is a common coding strategy for
        experiments that read out one DNA base with a distinct fluorophore in each imaging round.

        Notes
        -----
        - If no code matches the per-round maximum for a feature, it will be assigned 'nan' instead
          of a target value
        - Numpy's argmax breaks ties by picking the first of the tied values -- this can lead to
          unexpected results in low-precision images where some features with "tied" channels will
          decode, but others will be assigned 'nan'.

        Parameters
        ----------
        intensities : IntensityTable
            features to be decoded

        Returns
        -------
        IntensityTable :
            intensity table containing additional data variables for target assignments

        """

        def _view_row_as_element(array: np.ndarray) -> np.ndarray:
            """view an entire code as a single element

            This view allows vectors (codes) to be compared for equality without need for multiple
            comparisons by casting the data in each code to a structured dtype that registers as
            a single value

            Parameters
            ----------
            array : np.ndarray
                2-dimensional numpy array of shape (n_observations, (n_channel * n_round)) where
                observations may be either features or codes.

            Returns
            -------
            np.ndarray :
                1-dimensional vector of shape n_observations

            """
            nrows, ncols = array.shape
            dtype = {'names': ['f{}'.format(i) for i in range(ncols)],
                     'formats': ncols * [array.dtype]}
            return array.view(dtype)

        self._validate_decode_intensity_input_matches_codebook_shape(intensities)

        # add empty metadata fields and return
        if intensities.sizes[Features.AXIS] == 0:
            return DecodedIntensityTable.from_intensity_table(
                intensities,
                targets=(Features.AXIS, np.empty(0, dtype='U')),
                distances=(Features.AXIS, np.empty(0, dtype=np.float64)),
                passes_threshold=(Features.AXIS, np.empty(0, dtype=bool)))

        intensities_without_nans = intensities.fillna(0)
        max_channels = intensities_without_nans.argmax(Axes.CH.value)
        # this snippet of code finds all the (feature, round) spots that have uniform illumination,
        # and assigns them to a ch number that's one larger than max possible to ensure that such
        # spots decode to `NaN`.
        max_channels_max = intensities_without_nans.reduce(np.amax, Axes.CH.value)
        max_channels_min = intensities_without_nans.reduce(np.amin, Axes.CH.value)
        uniform_illumination_mask = (max_channels_max == max_channels_min).values

        max_channels.values[uniform_illumination_mask] = (  # type: ignore
            intensities.sizes[Axes.CH.value]
        )
        codes = self.argmax(Axes.CH.value)

        # TODO ambrosejcarr, dganguli: explore this quality score further
        # calculate distance scores by evaluating the fraction of signal in each round that is
        # found in the non-maximal channels.
        max_intensities = intensities.max(Axes.CH.value)
        round_intensities = intensities.sum(Axes.CH.value)
        distance = 1 - (max_intensities / round_intensities).mean(Axes.ROUND.value)

        a = _view_row_as_element(codes.values.reshape(self.shape[0], -1))  # type: ignore
        b = _view_row_as_element(
            max_channels.values.reshape(intensities.shape[0], -1)  # type: ignore
        )

        targets = np.full(intensities.shape[0], fill_value=np.nan, dtype=object)

        # decode the intensities
        for i in np.arange(codes.shape[0]):  # type: ignore
            targets[np.where(a[i] == b)[0]] = codes[Features.TARGET][i]

        # a code passes filters if it decodes successfully
        passes_filters = ~pd.isnull(targets)

        return DecodedIntensityTable.from_intensity_table(
            intensities,
            targets=(Features.AXIS, targets.astype('U')),
            distances=(Features.AXIS, distance),
            passes_threshold=(Features.AXIS, passes_filters))

    @classmethod
    def synthetic_one_hot_codebook(
            cls, n_round: int, n_channel: int, n_codes: int, target_names: Optional[Sequence]=None
    ) -> "Codebook":
        """Generate codes where one channel is "on" in each imaging round

        Parameters
        ----------
        n_round : int
            number of imaging rounds per code
        n_channel : int
            number of channels per code
        n_codes : int
            number of codes to generate
        target_names : Optional[List[str]]
            if provided, names for targets in codebook

        Examples
        --------
        Create a Codebook with 2 rounds, 3 channels, and 2 codes
        ::

            >>> from starfish import Codebook
            >>> sd = Codebook.synthetic_one_hot_codebook(n_round=4, n_channel=3, n_codes=2)
            >>> sd.codebook()
            <xarray.Codebook (target: 2, r: 4, c: 3)>
            array([[[1, 0, 0],
                    [0, 0, 1],
                    [1, 0, 0],
                    [0, 1, 0]],

                   [[1, 0, 0],
                    [1, 0, 0],
                    [1, 0, 0],
                    [1, 0, 0]]], dtype=uint8)
            Coordinates:
              * target   (target) object 6d7fff11-8905-4421-ab49-4f6d8ecdb4b7 \
        1f5f7087-0618-49fc-a6a5-82fee14360b3
              * r        (r) int64 0 1 2 3
              * c        (c) int64 0 1 2

        Returns
        -------
        List[Dict] :
            list of codewords

        """

        codes: Set = set()
        while len(codes) < n_codes:
            codes.add(tuple([np.random.randint(0, n_channel) for _ in np.arange(n_round)]))

        # construct codewords from code
        codewords = [
            [
                {
                    Axes.ROUND.value: h, Axes.CH.value: c, 'v': 1
                } for h, c in enumerate(code)
            ] for code in codes
        ]

        # make a codebook from codewords
        if target_names is None:
            # use a reverse-sorted list of integers as codewords
            target_names = [uuid.uuid4() for _ in range(n_codes)]
        if n_codes != len(target_names):
            raise ValueError(
                f"n_codes ({n_codes} does not match the number of targets ({len(target_names)})")

        codebook = [{Features.CODEWORD: w, Features.TARGET: g}
                    for w, g in zip(codewords, target_names)]

        return cls.from_code_array(codebook, n_round=n_round, n_channel=n_channel)

max_stars_repo_name: haoxusci/starfish

max_issues_repo_name: haoxusci/starfish

max_forks_repo_name: haoxusci/starfish


text:
#!/usr/bin/env python

import rospy
import time
from barc.msg import ECU, Encoder, Z_KinBkMdl
from numpy import pi

# from encoder
v_meas      = 0.0
t0          = time.time()
ang_km1     = 0.0
ang_km2     = 0.0
n_FL        = 0.0
n_FR        = 0.0
n_BL        = 0.0
n_BR        = 0.0
r_tire      = 0.05 # radius of the tire

# pwm cmds
servo_pwm   = 1580.0
motor_pwm   = 1500.0
motor_pwm_offset = 1580.0
servo_pwm_offset = 1600.0

# state estimate
x_est = 0.0
y_est = 0.0

# lateral error
lateral_error = 0.0

# references
v_ref = 0.5 # reference speed, unit: m/s
x_center = 0.2774
y_center = 0.2935
radius = 9.7690

# lateral error calculation
def lateral_error_cal(x_est, y_est):
    global lateral_error
    x_ref = x_center + radius*(x_est - x_center)/sqrt((x_est-x_center)**2 + (y_est-y_center)**2)
    y_ref = y_center + radius*(y_est - y_center)/sqrt((x_est-x_center)**2 + (y_est-y_center)**2)
    lateral_error = sqrt((x_ref-x_center)**2 + (y_ref-y_center)**2) - radius

# encoder measurement update
def enc_callback(data):
    global t0, v_meas
    global n_FL, n_FR, n_BL, n_BR
    global ang_km1, ang_km2

    n_FL = data.FL
    n_FR = data.FR
    n_BL = data.BL
    n_BR = data.BR

    # compute the average encoder measurement
    n_mean = (n_FL + n_FR + n_BL + n_BR)/4

    # transfer the encoder measurement to angular displacement
    ang_mean = n_mean*2*pi/8

    # compute time elapsed
    tf = time.time()
    dt = tf - t0

    # compute speed with second-order, backwards-finite-difference estimate
    v_meas    = r_tire*(3*ang_mean - 4*ang_km1 + ang_km2)/(2*dt)
    # rospy.logwarn("speed = {}".format(v_meas))

    # update old data
    ang_km1 = ang_mean
    ang_km2 = ang_km1
    t0      = time.time()

# state estimated from kalman filter
def state_callback(data):
    global x_est, y_est

    x_est = data.x
    y_est = data.y
    lateral_error = lateral_error_cal(x_est, y_est)

# Insert your PID longitudinal controller here: since you are asked to do longitudinal control,
# the steering angle d_f can always be set to zero. Therefore, the control output of your controller
# is essentially longitudinal acceleration acc.
# ===================================PID longitudinal controller================================#
class PID():
    def __init__(self, kp=1, ki=1, kd=1, integrator=0, derivator=0):
        self.kp = kp
        self.ki = ki
        self.kd = kd
        self.integrator = integrator
        self.derivator = derivator
        self.integrator_max = 10
        self.integrator_min = -10

    def acc_calculate(self, speed_reference, speed_current):
        self.error = speed_reference - speed_current

        # Propotional control
        self.P_effect = self.kp*self.error

        # Integral control
        self.integrator = self.integrator + self.error
        ## Anti windup
        if self.integrator >= self.integrator_max:
            self.integrator = self.integrator_max
        if self.integrator <= self.integrator_min:
            self.integrator = self.integrator_min
        self.I_effect = self.ki*self.integrator

        # Derivative control
        self.derivator = self.error - self.derivator
        self.D_effect = self.kd*self.derivator
        self.derivator = self.error

        acc = self.P_effect + self.I_effect + self.D_effect
        if acc <= 0:
            acc = 20
        return acc

# =====================================end of the controller====================================#

# state estimation node
def controller():
    global motor_pwm, servo_pwm, motor_pwm_offset, servo_pwm_offset
    global v_ref, v_meas
    global lateral_error, x_est, y_est
    # initialize node:
    rospy.init_node('lateral_control', anonymous=True)

    # topic subscriptions / publications
    rospy.Subscriber('encoder', Encoder, enc_callback)
    rospy.Subscriber('state_estimate', Z_KinBkMdl, state_callback)

    ecu_pub   = rospy.Publisher('ecu_pwm', ECU, queue_size = 10)

    # set node rate
    loop_rate   = 50
    rate        = rospy.Rate(loop_rate)

    # Initialize the PID controller
    longitudinal_control = PID(kp=200, ki=0, kd=0.0)
    lateral_control = PID(kp=50, ki=0, kd=0.0)

    while not rospy.is_shutdown():
        # acceleration calculated from PID controller.
        motor_pwm = longitudinal_control.acc_calculate(v_ref, v_meas) + motor_pwm_offset
        servo_pwm = lateral_control.acc_calculate(0, lateral_error) + servo_pwm_offset
        rospy.logwarn("pwm = {}".format(motor_pwm))
        # publish information
        ecu_pub.publish( ECU(motor_pwm, servo_pwm) )

        # wait
        rate.sleep()

if __name__ == '__main__':
    try:
       controller()
    except rospy.ROSInterruptException:
        pass

max_stars_repo_name: treyfortmuller/barc

max_issues_repo_name: treyfortmuller/barc

reason: from numpy


text:
import networkx as nx
from scipy.io import loadmat
import matplotlib.pyplot as plt

dataset = 'citeseer'
x = loadmat(dataset+'.mat')
x = x['network']
G = nx.from_scipy_sparse_matrix(x)
pos = nx.spring_layout(G)

plt.figure(num=None, figsize=(20, 20), dpi=80)
plt.axis('off')
fig = plt.figure(1)

#nx.draw_networkx_nodes(G,pos,node_size=weight)
nx.draw_networkx_nodes(G,pos)
nx.draw_networkx_edges(G,pos)

cut = 1.00
xmax = cut * max(xx for xx, yy in pos.values())
ymax = cut * max(yy for xx, yy in pos.values())
plt.xlim(0, xmax)
plt.ylim(0, ymax)

#plt.draw()
plt.savefig('a.png')
plt.gcf().clear()

max_stars_repo_name: yashchandak/GNN

max_issues_repo_name: yashchandak/GNN

max_forks_repo_name: yashchandak/GNN

reason: from scipy,import networkx


text:
#!/usr/bin/env python3
import setup_run_dir    # this import tricks script to run from 2 levels up
import json
import random
import logging
import torch
import numpy
from   amrlib.models.parse_spring.trainer import Trainer
from   amrlib.utils.logging import setup_logging, silence_penman, WARN

# See random generators for consistant results
random.seed(0)
torch.manual_seed(0)
numpy.random.seed(0)

# For bart-large
#   There are ~16068 batches in the training data for batch_size = 500
#   On a Titan X (12GB, fp32=6.7 TFlops) training takes 80 minutes/epoch including
#   about  6 minutes for prediction/smatch testing.
if __name__ == '__main__':
    logging.getLogger('transformers.tokenization_utils_base').setLevel(logging.ERROR)   # skip tokenizer warning
    setup_logging(logfname='logs/train_parse_spring.log', level=WARN)
    silence_penman()

    # Paths
    config_fn  = 'configs/model_parse_spring.json'
    #checkpoint = 'data/model_parse_spring/checkpoint_epoch_08_smatch_8422.pt'
    checkpoint = None   # start from scratch

    # Load the config file
    with open(config_fn) as f:
        config = json.load(f)

    # Setup the training data locations
    config['train'] = 'amrlib/data/amr_annotation_3.0/data/amrs/split/training/*.txt'
    config['dev']   = 'amrlib/data/amr_annotation_3.0/data/amrs/split/dev/*.txt'

    # Run the training
    trainer = Trainer(config)
    trainer.train(checkpoint=checkpoint)

max_stars_repo_name: MeghaTiya/amrlib

max_issues_repo_name: MeghaTiya/amrlib

max_forks_repo_name: MeghaTiya/amrlib


text:
import altair as alt
from numpy import row_stack
import pandas as pd

def generate_line_plot(data: pd.DataFrame, ycol: str):
    """"
    Generates the line chart for the dashboard

    Parameters
    ----------
    data : pandas dataframe
        The dataframe that contains the data to plot.

    ycol : string
        The column to plot on the y-axis. This must be
        a column from the `data` dataframe.

    Returns
    -------
    chart : html of altair Chart
        The generated line chart converted to html
    """
    # Set up dynamic axis labels
    if ycol == "averageRating":
        label = "Average Rating (/10)"
    if ycol == "runtimeMinutes":
        label = "Average Runtime (minutes)"

    ycol = f"mean({ycol})"

    chart = alt.Chart(data).mark_line().encode(
        x=alt.X("startYear",
                axis=alt.Axis(title="Year",
                              grid=False,
                              format='.0f'),
                scale=alt.Scale(domain=(2011, 2020))),
        y=alt.Y(ycol,
                axis=alt.Axis(title=label)),
        color=alt.Color("genres",
                        legend=alt.Legend(title="",
                                          orient='none',
                                          columns=5,
                                          legendX=20,
                                          legendY=240,
                                          direction='horizontal')),
    )

    chart = chart + chart.mark_circle()

    chart = chart.interactive()

    return chart.configure_axisLeft(
                labelColor='#DBA506',
                titleColor='#DBA506'
            ).configure_view(
                strokeWidth=0
            ).configure_axisBottom(
                labelColor='#DBA506',
                titleColor='#DBA506'
            ).configure_legend(
                labelColor='#DBA506',
                titleColor='#DBA506'
            ).properties(
                height=200,
                width=315,
                background='#222222'
            ).to_html()


text:
# Siamese Architecture for face recognition

import random
import numpy as np
import time
import tensorflow as tf
import math
import pdb
import sys
import scipy.io as sio
from sklearn import *
import matplotlib.pyplot as plt
import os

def Plot_ROC_Fn(label,distance,save_path):

    fpr, tpr, thresholds = metrics.roc_curve(label, distance, pos_label=1)
    AUC = metrics.roc_auc_score(label, distance, average='macro', sample_weight=None)
    # AP = metrics.average_precision_score(label, -distance, average='macro', sample_weight=None)

    # Calculating EER
    intersect_x = fpr[np.abs(fpr - (1 - tpr)).argmin(0)]
    EER = intersect_x
    print("EER = ", float(("{0:.%ie}" % 1).format(intersect_x)))

    # AUC(area under the curve) calculation
    print("AUC = ", float(("{0:.%ie}" % 1).format(AUC)))

    # # AP(average precision) calculation.
    # # This score corresponds to the area under the precision-recall curve.
    # print("AP = ", float(("{0:.%ie}" % 1).format(AP)))

    # Plot the ROC
    fig = plt.figure()
    ax = fig.gca()
    lines = plt.plot(fpr, tpr, label='ROC Curve')
    plt.setp(lines, linewidth=2, color='r')
    ax.set_xticks(np.arange(0, 1.1, 0.1))
    ax.set_yticks(np.arange(0, 1.1, 0.1))
    plt.title('ROC.jpg')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')

    # # Cutting the floating number
    # AUC = '%.2f' % AUC，
    # EER = '%.2f' % EER
    # # AP = '%.2f' % AP
    #
    # # Setting text to plot
    # # plt.text(0.5, 0.6, 'AP = ' + str(AP), fontdict=None)
    # plt.text(0.5, 0.5, 'AUC = ' + str(AUC), fontdict=None)
    # plt.text(0.5, 0.4, 'EER = ' + str(EER), fontdict=None)
    plt.grid()
    plt.show()
    fig.savefig(save_path)

if __name__ == '__main__':

    tf.app.flags.DEFINE_string(
    'evaluation_dir', '../../results/ROC',
    'Directory where checkpoints and event logs are written to.')

    tf.app.flags.DEFINE_string(
    'plot_dir', '../../results/PLOTS',
    'Directory where plots are saved to.')

    # Store all elemnts in FLAG structure!
    FLAGS = tf.app.flags.FLAGS

    # Loading scores and labels
    score = np.load(os.path.join(FLAGS.evaluation_dir,'score_vector.npy'))
    label = np.load(os.path.join(FLAGS.evaluation_dir,'target_label_vector.npy'))
    save_path = os.path.join(FLAGS.plot_dir,'ROC.PNG')

    # Creating the path
    if not os.path.exists(FLAGS.plot_dir):
            os.makedirs(FLAGS.plot_dir)

    Plot_ROC_Fn(label,score,save_path)

reason: import numpy,import scipy


text:
""" Computation of pairwise distances between 3D points

    Adapted from Jake VanderPlas's blog post at
    http://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/
"""

import timeit

import numpy as np
X = np.random.random((1500, 3))

def pairwise_numpy(X):
  return np.sqrt(((X[:, None, :] - X) ** 2).sum(-1))

def pairwise_python(X):
  M = X.shape[0]
  N = X.shape[1]
  D = np.empty((M, M), dtype=np.float64)
  for i in range(M):
    for j in range(M):
      d = 0.0
      for k in range(N):
        tmp = X[i, k] - X[j, k]
        d += tmp * tmp
      D[i, j] = np.sqrt(d)
  return D

import numba
use_signature = False
if use_signature:
  pairwise_numba = numba.jit(pairwise_python)
  # Make sure numba compiles the function with the type or argument we are
  # going to use in the benchmark, so as to get a fair comparison with Cython.
  pairwise_numba(np.array([(1., 1., 1.)]*10))
else:
  # Alternative: specify the signature, as numba will infer
  # it at the call point and then compile just-in-time the appropriate variant.
  # However, for our simple-minded test, this would give numba an unfair
  # disadvantage compared to Cython which pre-compiled the variant
  # corresponding to the specified signature.
  pairwise_numba = numba.jit('float64[:,:](float64[:,:])')(pairwise_python)

import pyximport
pyximport.install()
from cy_pairwise_distance import pairwise_cython

def mytimeit(func, repeats=5):
  t = timeit.repeat(stmt="{}(X)".format(func),
                    setup="from __main__ import {}, X".format(func),
                    number=repeats)
  return sum(sorted(t)[:3])/3

base_is_python = False
if base_is_python:
  t0 = mytimeit("pairwise_python", repeats=1)
  print("Python: {:.2f} s".format(t0))
  print("Numpy: x {:.1f} speedup".format(t0/mytimeit("pairwise_numpy")))
else:
  t0 = mytimeit("pairwise_numpy")
  print("Numpy: {:.2f} s".format(t0))
print("Numba: x {:.1f} speedup".format(t0/mytimeit("pairwise_numba")))
print("Cython: x {:.1f} speedup".format(t0/mytimeit("pairwise_cython")))

max_stars_repo_name: luc-j-bourhis/Cython-vs-Numba

max_issues_repo_name: luc-j-bourhis/Cython-vs-Numba

max_forks_repo_name: luc-j-bourhis/Cython-vs-Numba

reason: import numpy,import numba


text:
import numpy as np
from ..util.backend_functions import backend as bd
from .diffractive_element import DOE
from ..util.scaled_FT import scaled_fourier_transform

class Lens(DOE):
    def __init__(self,f, radius = None, aberration = None):
        """
        Creates a thin lens with a focal length equal to f
        """
        global bd
        from ..util.backend_functions import backend as bd

        self.f = f
        self.aberration = aberration
        self.radius = radius

    def get_transmittance(self, xx, yy, λ):

        t = 1
        if self.aberration != None:
            t = t*bd.exp(2*bd.pi * 1j *aberration(xx, yy))

        if self.radius != None:
            t = bd.where((xx**2 + yy**2) < self.radius**2, t, bd.zeros_like(xx))

        self.t = t

        t = self.t * bd.exp(-1j*bd.pi/(λ*self.f) * (xx**2 + yy**2))
        return t

    def get_coherent_PSF(self,  xx, yy, z, λ):
        """
        Get the coherent point spread function (PSF) of the lens pupil.
        Exactly, this method returns the result of the following integral:

        PSF(x,y) = 1 / (z*λ)**2 * ∫∫  t(u, v) * exp(-1j*pi/ (z*λ) *(u*x + v*y)) * du*dv
        """

        if (self.aberration != None) and (self.radius != None):

            if bd == np:
                from scipy import special
            else:
                from cupyx.scipy import special

            # we use an analytical solution:

            rr = bd.sqrt(xx**2 + yy**2)
            tmp = 2*bd.pi*self.radius*rr/(λ*z)
            tmp = bd.where(tmp < 1e-9, 1e-9, tmp) #avoid division by 0

            PSF = 2 * bd.pi * self.radius**2 * (special.j1(tmp))/ tmp
            PSF = 1 / (z*λ)**2 * PSF

            return PSF

        else:
            t = bd.ones_like(xx)

            if self.aberration != None:
                t = t*bd.exp(2*bd.pi * 1j *aberration(xx, yy))

            if self.radius != None:
                t = t*bd.where((xx**2 + yy**2) < self.radius**2, t, bd.zeros_like(xx))

        xx, yy, PSF = scaled_fourier_transform(xx, yy, t, λ = λ,z =z, scale_factor = 1, mesh = True)
        PSF = 1 / (z*λ)**2 * PSF

        return PSF

max_stars_repo_name: rafael-fuente/diffractsim

max_issues_repo_name: rafael-fuente/diffractsim

max_forks_repo_name: rafael-fuente/diffractsim

reason: import numpy,from scipy,from cupy


text:
"""
Functions for loading the Assistments data.  Originally from
https://sites.google.com/site/assistmentsdata/home/assistment-2009-2010-data/skill-builder-data-2009-2010
"""
import pickle

import logging

import numpy as np
import pandas as pd

from .constants import (ITEM_IDX_KEY, TEMPLATE_IDX_KEY, CONCEPT_IDX_KEY, USER_IDX_KEY,
                        TIME_IDX_KEY, CORRECT_KEY, SINGLE)

SKILL_ID_KEY = 'skill_id'
PROBLEM_ID_KEY = 'problem_id'
TEMPLATE_ID_KEY = 'template_id'
USER_ID_KEY = 'user_id'

LOGGER = logging.getLogger(__name__)

def load_data(file_path, item_id_col=SKILL_ID_KEY, template_id_col=None, concept_id_col=None,
              remove_nan_skill_ids=False, max_interactions_per_user=None,
              drop_duplicates=False, min_interactions_per_user=2):
    """ Load the Assistments dataset as a pandas dataframe, filter out students with only a single
    interaction, and optionally truncate student histories.  The columns used for item and concept
    identifiers can be specified in the input arguments.

    Note that multiple skill ids associated with an interaction will result in the first skill
    name lexicographically being retained.

    :param str file_path: path to the skill builder file
    :param str item_id_col: indicates column of csv file to use for item ids
    :param str template_id_col: Set a particular column to represent a template id for hierarchical
        IRT. If 'single', assumes a dummy single hierarchical level; if None, no column is retained
        for templates.
    :param str concept_id_col: indicates column of csv file to use for concept ids. If 'single',
        assumes a dummy single concept.  If None, concept column is not retained.
    :param bool remove_nan_skill_ids: whether to filter out interactions with NaN skill ids
    :param int max_interactions_per_user: number of interactions to keep per user (default is to
        keep all)
    :param int min_interactions_per_user: The minimum amount of history that is required to retain a
        student history.
    :param bool drop_duplicates: Whether to keep only the first of rows with duplicate order_id
        fields
    :return: processed data, student ids corresponding to the student indices, item ids
        corresponding to the item indices, template ids corresponding to the template indices, and
        concept ids corresponding to the concept indices
    :rtype: (pd.DataFrame, np.ndarray[int], np.ndarray[int], np.ndarray[int])
    """
    data = pd.DataFrame.from_csv(file_path)
    LOGGER.info("Read {:3,d} rows from file".format(len(data)))

    # Get the time index
    data[TIME_IDX_KEY] = data.index.values

    # fix up skill ids
    if data[SKILL_ID_KEY].dtype == 'object':
        # In this case, we have a string of skill ids like '1,54,3'
        # Keep only the first skill for now
        data[SKILL_ID_KEY] = data[SKILL_ID_KEY].apply(
            lambda x: sorted(map(int, x.split(',')))[0]).values
    nan_skill = data[SKILL_ID_KEY].apply(np.isnan)
    if remove_nan_skill_ids:
        data = data[~nan_skill]
        LOGGER.info("Removed {:3,d} rows with NaN skill_id".format(np.sum(nan_skill)))
    else:
        data.loc[nan_skill, SKILL_ID_KEY] = -1
        data[SKILL_ID_KEY] = data[SKILL_ID_KEY].astype(int)

    # sort by user, time, item, and concept id (if available)
    sort_keys = [USER_ID_KEY, TIME_IDX_KEY, item_id_col, SKILL_ID_KEY]
    data.sort(columns=sort_keys, inplace=True)

    if drop_duplicates:
        old_data_len = len(data)
        data = data.groupby(data.index).head(1)
        LOGGER.info("Removed {:3,d} duplicate rows ({:3,d} rows remaining)".format(
            old_data_len - len(data), len(data)))

    # filter for students with >= min_history_length interactions;
    # must be done after removing nan skillz
    data = data.groupby(USER_ID_KEY).filter(lambda x: len(x) >= min_interactions_per_user)
    LOGGER.info("Removed students with <{} interactions ({:3,d} rows remaining)".format(
        min_interactions_per_user, len(data)))

    # limit to first `max_interactions_per_user`
    if max_interactions_per_user is not None:
        old_data_len = len(data)
        data = data.groupby([USER_ID_KEY]).head(max_interactions_per_user)
        LOGGER.info("Filtered for {} max interactions per student ({:3,d} rows removed)".format(
            max_interactions_per_user, old_data_len - len(data)))

    # to be safe, sort again
    data.sort(columns=sort_keys, inplace=True)

    # attach question index
    item_ids, data[ITEM_IDX_KEY] = np.unique(data[item_id_col], return_inverse=True)
    user_ids, data[USER_IDX_KEY] = np.unique(data[USER_ID_KEY], return_inverse=True)

    # To store the ID mapping between the ID (for items and users) and the index
    output_file = open('item_id_mapping.pickle', 'wb')
    pickle.dump(item_ids, output_file)
    output_file.close()
    output_file = open('user_id_mapping.pickle', 'wb')
    pickle.dump(user_ids, output_file)
    output_file.close()

    cols_to_keep = [USER_IDX_KEY, ITEM_IDX_KEY, CORRECT_KEY, TIME_IDX_KEY]
    if concept_id_col is None:
        LOGGER.info('concept_id_col not supplied, not using concepts')
        concept_ids = None
    else:
        if concept_id_col == SINGLE:
            LOGGER.info('Using dummy single concept.')
            data[concept_id_col] = '0'
        elif concept_id_col not in data:
            raise ValueError('concept_id_col %s not found in data columns %s' % (concept_id_col,
                                                                                data.columns))
        concept_ids, data[CONCEPT_IDX_KEY] = np.unique(data[concept_id_col], return_inverse=True)
        cols_to_keep.append(CONCEPT_IDX_KEY)

    if template_id_col is None:
        LOGGER.info('template_id_col not supplied, not using templates')
        template_ids = None
    else:
        if template_id_col == SINGLE:
            LOGGER.info('Using dummy single template.')
            data[template_id_col] = '0'
        elif template_id_col not in data:
            raise ValueError('template_id_col %s not found', template_id_col)
        template_ids, data[TEMPLATE_IDX_KEY] = np.unique(data[template_id_col], return_inverse=True)
        cols_to_keep.append(TEMPLATE_IDX_KEY)

    LOGGER.info("Processed data: {:3,d} interactions, {:3,d} students; {:3,d} items, "
                "{:3,d} templates, {:3,d} concepts"
                .format(len(data), len(user_ids), len(item_ids),
                        len(template_ids) if template_ids is not None else 0,
                        len(concept_ids) if concept_ids is not None else 0))

    # To store the dataframe as they use it (after filtering, sorting, etc.).
    # This enables us to accurately compare the effectiveness of their algorithm with other approaches
    data[cols_to_keep].to_csv('filtered_dataframe_assistments.csv')

    return data[cols_to_keep], user_ids, item_ids, template_ids, concept_ids