text:
"""
VARMAX Model

Author: Chad Fulton
License: Simplified-BSD
"""
from __future__ import division, absolute_import, print_function

import numpy as np
from .mlemodel import MLEMixin, MLEResultsMixin
try:
    from statsmodels.tsa.statespace import varmax
    from statsmodels.tsa.statespace import mlemodel
except ImportError:
    from .compat import mlemodel, varmax
import statsmodels.base.wrapper as wrap

class VARMAX(MLEMixin, varmax.VARMAX):
    r"""
    Vector Autoregressive Moving Average with eXogenous regressors model

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`, , shaped nobs x k_endog.
    exog : array_like, optional
        Array of exogenous regressors, shaped nobs x k.
    order : iterable
        The (p,q) order of the model for the number of AR and MA parameters to
        use.
    trend : {'nc', 'c'}, optional
        Parameter controlling the deterministic trend polynomial.
        Can be specified as a string where 'c' indicates a constant intercept
        and 'nc' indicates no intercept term.
    error_cov_type : {'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors). Default is
        "unstructured".
    measurement_error : boolean, optional
        Whether or not to assume the endogenous observations `endog` were
        measured with error. Default is False.
    enforce_stationarity : boolean, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    enforce_invertibility : boolean, optional
        Whether or not to transform the MA parameters to enforce invertibility
        in the moving average component of the model. Default is True.
    **kwargs
        Keyword arguments may be used to provide default values for state space
        matrices or for Kalman filtering options. See `Representation`, and
        `KalmanFilter` for more details.

    Attributes
    ----------
    order : iterable
        The (p,q) order of the model for the number of AR and MA parameters to
        use.
    trend : {'nc', 'c'}, optional
        Parameter controlling the deterministic trend polynomial.
        Can be specified as a string where 'c' indicates a constant intercept
        and 'nc' indicates no intercept term.
    error_cov_type : {'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors). Default is
        "unstructured".
    measurement_error : boolean, optional
        Whether or not to assume the endogenous observations `endog` were
        measured with error. Default is False.
    enforce_stationarity : boolean, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    enforce_invertibility : boolean, optional
        Whether or not to transform the MA parameters to enforce invertibility
        in the moving average component of the model. Default is True.

    Notes
    -----
    Generically, the VARMAX model is specified (see for example chapter 18 of
    [1]_):

    .. math::

        y_t = \nu + A_1 y_{t-1} + \dots + A_p y_{t-p} + B x_t + \epsilon_t +
        M_1 \epsilon_{t-1} + \dots M_q \epsilon_{t-q}

    where :math:`\epsilon_t \sim N(0, \Omega)`, and where :math:`y_t` is a
    `k_endog x 1` vector. Additionally, this model allows considering the case
    where the variables are measured with error.

    Note that in the full VARMA(p,q) case there is a fundamental identification
    problem in that the coefficient matrices :math:`\{A_i, M_j\}` are not
    generally unique, meaning that for a given time series process there may
    be multiple sets of matrices that equivalently represent it. See Chapter 12
    of [1]_ for more informationl. Although this class can be used to estimate
    VARMA(p,q) models, a warning is issued to remind users that no steps have
    been taken to ensure identification in this case.

    See Also
    --------
    dismalpy.ssm.mlemodel.MLEModel
    dismalpy.ssm.kalman_smoother.KalmanSmoother
    dismalpy.ssm.kalman_filter.KalmanFilter
    dismalpy.ssm.representation.Representation

    References
    ----------
    .. [1] Lutkepohl, Helmut. 2007.
       New Introduction to Multiple Time Series Analysis.
       Berlin: Springer.

    """
    def filter(self, params, transformed=True, cov_type=None, return_ssm=False,
               **kwargs):
        params = np.array(params, ndmin=1)

        # Transform parameters if necessary
        if not transformed:
            params = self.transform_params(params)
            transformed = True

        # Get the state space output
        results = super(VARMAX, self).filter(params, transformed, cov_type,
                                             return_ssm=True, **kwargs)

        # Wrap in a results object
        if not return_ssm:
            result_kwargs = {}
            if cov_type is not None:
                result_kwargs['cov_type'] = cov_type
            results = VARMAXResultsWrapper(
                VARMAXResults(self, params, results, **result_kwargs)
            )

        return results
    filter.__doc__ = MLEMixin.filter.__doc__

    def smooth(self, params, transformed=True, cov_type=None, return_ssm=False,
               **kwargs):
        params = np.array(params, ndmin=1)

        if not transformed:
            params = self.transform_params(params)
            transformed = True

        # Get the state space output
        results = super(VARMAX, self).smooth(params, transformed, cov_type,
                                             return_ssm=True, **kwargs)

        # Wrap in a results object
        if not return_ssm:
            result_kwargs = {}
            if cov_type is not None:
                result_kwargs['cov_type'] = cov_type
            results = VARMAXResultsWrapper(
                VARMAXResults(self, params, results, **result_kwargs)
            )

        return results
    smooth.__doc__ = MLEMixin.smooth.__doc__

class VARMAXResults(MLEResultsMixin, varmax.VARMAXResults):
    """
    Class to hold results from fitting an VARMAX model.

    Parameters
    ----------
    model : VARMAX instance
        The fitted model instance

    Attributes
    ----------
    specification : dictionary
        Dictionary including all attributes from the VARMAX model instance.
    coefficient_matrices_var : array
        Array containing autoregressive lag polynomial coefficient matrices,
        ordered from lowest degree to highest.
    coefficient_matrices_vma : array
        Array containing moving average lag polynomial coefficients,
        ordered from lowest degree to highest.

    See Also
    --------
    dismalpy.ssm.mlemodel.MLEResults
    dismalpy.ssm.kalman_smoother.SmootherResults
    dismalpy.ssm.kalman_filter.FilterResults
    dismalpy.ssm.representation.FrozenRepresentation
    """
    pass

class VARMAXResultsWrapper(mlemodel.MLEResultsWrapper):
    _attrs = {}
    _wrap_attrs = wrap.union_dicts(
        mlemodel.MLEResultsWrapper._wrap_attrs, _attrs)
    _methods = {}
    _wrap_methods = wrap.union_dicts(
        mlemodel.MLEResultsWrapper._wrap_methods, _methods)
wrap.populate_wrapper(VARMAXResultsWrapper, VARMAXResults)

max_stars_repo_name: dismalpy/dismalpy

max_issues_repo_name: dismalpy/dismalpy

max_forks_repo_name: dismalpy/dismalpy

reason: import numpy,import statsmodels,from statsmodels


text:
import argparse
import numpy as np
import logging
import random
import torch
import sys
from functools import partial
from dataclasses import dataclass, field
from typing import Optional, Union
from tqdm import tqdm
import json
from pathlib import Path

from datasets import load_dataset, load_metric

import transformers
from transformers import (
    AutoModelForSeq2SeqLM,
    AutoTokenizer,
    Seq2SeqTrainingArguments,
    HfArgumentParser,
    set_seed,
    PreTrainedTokenizerBase,
    PreTrainedModel,
    DataCollatorForSeq2Seq
)
from transformers.file_utils import PaddingStrategy

from custom_trainer import CustomTrainer

logger = logging.getLogger(__name__)

@dataclass
class DataAndModelArguments:
    """
    Arguments for setting up training/evaluation
    """
    model_name: str = field(
        metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    train_dset: str = field(
        default='squad', metadata={"help": "The name of the dataset to use (via the datasets library)."}
    )
    val_dset: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use for validation."}
    )
    predict_dset: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use for final generation."}
    )
    output_predictions: Optional[str] = field(
        default=None, metadata={"help": "The output file to store predictions"}
    )
    num_beams: Optional[int] = field(
        default=None,
        metadata={
            "help": "Number of beams to use for evaluation. This argument will be passed to ``model.generate``, "
                    "which is used during ``evaluate`` and ``predict``."
        },
    )
    should_log: bool = field(
        default=True,
        metadata={"help": "Whether or not to log"},
    )

@dataclass
class DataCollatorForSeq2SeqMinPadding:
    """
    Data collator that will dynamically pad the inputs received, as well as the labels.

    Args:
        tokenizer (:class:`~transformers.PreTrainedTokenizer` or :class:`~transformers.PreTrainedTokenizerFast`):
            The tokenizer used for encoding the data.
        model (:class:`~transformers.PreTrainedModel`):
            The model that is being trained. If set and has the `prepare_decoder_input_ids_from_labels`, use it to
            prepare the `decoder_input_ids`

            This is useful when using `label_smoothing` to avoid calculating loss twice.
        padding (:obj:`bool`, :obj:`str` or :class:`~transformers.file_utils.PaddingStrategy`, `optional`, defaults to :obj:`True`):
            Select a strategy to pad the returned sequences (according to the model's padding side and padding index)
            among:

            * :obj:`True` or :obj:`'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
              sequence is provided).
            * :obj:`'max_length'`: Pad to a maximum length specified with the argument :obj:`max_length` or to the
              maximum acceptable input length for the model if that argument is not provided.
            * :obj:`False` or :obj:`'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of
              different lengths).
        max_length (:obj:`int`, `optional`):
            Maximum length of the returned list and optionally padding length (see above).
        pad_to_multiple_of (:obj:`int`, `optional`):
            If set will pad the sequence to a multiple of the provided value.

            This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability >=
            7.5 (Volta).
        label_pad_token_id (:obj:`int`, `optional`, defaults to -100):
            The id to use when padding the labels (-100 will be automatically ignored by PyTorch loss functions).
    """

    tokenizer: PreTrainedTokenizerBase
    model: Optional[PreTrainedModel] = None
    padding: Union[bool, str, PaddingStrategy] = True
    max_length: Optional[int] = None
    pad_to_multiple_of: Optional[int] = None
    label_pad_token_id: int = -100

    def __call__(self, features):
        labels = [feature["labels"] for feature in features] if "labels" in features[0].keys() else None
        # We have to pad the labels before calling `tokenizer.pad` as this method won't pad them and needs them of the
        # same length to return tensors.
        if labels is not None:
            max_label_length = max(len(l) for l in labels)
            padding_side = self.tokenizer.padding_side
            for feature in features:
                remainder = [self.label_pad_token_id] * (max_label_length - len(feature["labels"]))
                feature["labels"] = (
                    feature["labels"] + remainder if padding_side == "right" else remainder + feature["labels"]
                )

        max_length = max(len(feature['input_ids']) for feature in features)
        features = self.tokenizer.pad(
            features,
            padding=self.padding,
            max_length=max_length,
            pad_to_multiple_of=self.pad_to_multiple_of,
            return_tensors="pt",
        )

        # prepare decoder_input_ids
        if self.model is not None and hasattr(self.model, "prepare_decoder_input_ids_from_labels"):
            decoder_input_ids = self.model.prepare_decoder_input_ids_from_labels(labels=features["labels"])
            features["decoder_input_ids"] = decoder_input_ids

        return features

def enforce_reproducibility(seed=1000):
    # Sets seed manually for both CPU and CUDA
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    # For atomic operations there is currently
    # no simple way to enforce determinism, as
    # the order of parallel operations is not known.
    # CUDNN
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False
    # System based
    random.seed(seed)
    np.random.seed(seed)
    set_seed(seed)

def data_preprocess(tokenizer, dset, examples):
    if dset == 'local':
        inputs = [ctx + ' ' + ans[0]['text'] for ctx, ans in zip(examples['context'], examples['answers'])]
    else:
        inputs = [ctx + ' ' + ans['text'][0] for ctx, ans in zip(examples['context'], examples['answers'])]
    targets = [q for i,q in enumerate(examples['question'])]
    model_inputs = tokenizer(inputs, max_length=tokenizer.model_max_length, truncation=True)

    # Setup the tokenizer for targets
    with tokenizer.as_target_tokenizer():
        labels = tokenizer(targets, max_length=tokenizer.model_max_length, truncation=True)

    # # If we are padding here, replace all tokenizer.pad_token_id in the labels by -100 when we want to ignore
    # # padding in the loss.
    # if padding == "max_length" and data_args.ignore_pad_token_for_loss:
    #     labels["input_ids"] = [
    #         [(l if l != tokenizer.pad_token_id else -100) for l in label] for label in labels["input_ids"]
    #     ]

    model_inputs["labels"] = labels["input_ids"]
    return model_inputs

def compute_metrics(tokenizer, metric, eval_preds):
    preds, labels = eval_preds
    if isinstance(preds, tuple):
        preds = preds[0]
    preds = np.where(preds != -100, preds, tokenizer.pad_token_id)
    decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
    labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
    decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
    # Some simple post-processing
    decoded_preds = [pred.strip() for pred in decoded_preds]
    decoded_labels = [lab.strip() for lab in decoded_labels]

    result = metric.compute(predictions=decoded_preds, references=decoded_labels)
    #result = {"rouge": result["score"]}
    result = {
        'rouge1_low_p': result['rouge1'].low.precision,
        'rouge1_low_r': result['rouge1'].low.recall,
        'rouge1_low_fmeasure': result['rouge1'].low.fmeasure,
        'rouge1_mid_p': result['rouge1'].mid.precision,
        'rouge1_mid_r': result['rouge1'].mid.recall,
        'rouge1_mid_fmeasure': result['rouge1'].mid.fmeasure,
        'rouge1_high_p': result['rouge1'].high.precision,
        'rouge1_high_r': result['rouge1'].high.recall,
        'rouge1_high_fmeasure': result['rouge1'].high.fmeasure,
        'rouge2_low_p': result['rouge2'].low.precision,
        'rouge2_low_r': result['rouge2'].low.recall,
        'rouge2_low_fmeasure': result['rouge2'].low.fmeasure,
        'rouge2_mid_p': result['rouge2'].mid.precision,
        'rouge2_mid_r': result['rouge2'].mid.recall,
        'rouge2_mid_fmeasure': result['rouge2'].mid.fmeasure,
        'rouge2_high_p': result['rouge2'].high.precision,
        'rouge2_high_r': result['rouge2'].high.recall,
        'rouge2_high_fmeasure': result['rouge2'].high.fmeasure,
        'rougeL_low_p': result['rougeL'].low.precision,
        'rougeL_low_r': result['rougeL'].low.recall,
        'rougeL_low_fmeasure': result['rougeL'].low.fmeasure,
        'rougeL_mid_p': result['rougeL'].mid.precision,
        'rougeL_mid_r': result['rougeL'].mid.recall,
        'rougeL_mid_fmeasure': result['rougeL'].mid.fmeasure,
        'rougeL_high_p': result['rougeL'].high.precision,
        'rougeL_high_r': result['rougeL'].high.recall,
        'rougeL_high_fmeasure': result['rougeL'].high.fmeasure,
        'rougeLsum_low_p': result['rougeLsum'].low.precision,
        'rougeLsum_low_r': result['rougeLsum'].low.recall,
        'rougeLsum_low_fmeasure': result['rougeLsum'].low.fmeasure,
        'rougeLsum_mid_p': result['rougeLsum'].mid.precision,
        'rougeLsum_mid_r': result['rougeLsum'].mid.recall,
        'rougeLsum_mid_fmeasure': result['rougeLsum'].mid.fmeasure,
        'rougeLsum_high_p': result['rougeLsum'].high.precision,
        'rougeLsum_high_r': result['rougeLsum'].high.recall,
        'rougeLsum_high_fmeasure': result['rougeLsum'].high.fmeasure,
    }

    #prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
    #result["gen_len"] = np.mean(prediction_lens)
    result = {k: round(v, 6) for k, v in result.items()}
    return result

if __name__ == '__main__':
    parser = HfArgumentParser((DataAndModelArguments, Seq2SeqTrainingArguments))
    dm_args, training_args = parser.parse_args_into_dataclasses()

    seed = training_args.seed
    model_name = dm_args.model_name
    n_gpu = training_args.n_gpu
    should_log = dm_args.should_log
    train_dset_name = dm_args.train_dset
    val_dset_name = dm_args.val_dset
    gen_dset_name = dm_args.predict_dset
    enforce_reproducibility(seed)

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        handlers=[logging.StreamHandler(sys.stdout)],
    )
    logger.setLevel(logging.INFO if should_log else logging.WARN)

    # See if CUDA available
    device = torch.device("cpu")
    if torch.cuda.is_available():
        print("Training on GPU")
        device = torch.device("cuda:0")

    # Set the verbosity to info of the Transformers logger (on main process only):
    if should_log:
        transformers.utils.logging.set_verbosity_info()
    logger.info(f"Training/evaluation parameters {training_args}")

    # Create the tokenizer and model
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

    train_dset = None
    if training_args.do_train:
        # Create dataset and processor
        if Path(train_dset_name).exists():
            preprocessor = partial(data_preprocess, tokenizer, 'local')
            # Load from local file
            train_dset_base = load_dataset('json', data_files=[train_dset_name])['train']
        else:
            preprocessor = partial(data_preprocess, tokenizer, train_dset_name)
            train_dset_base = load_dataset(train_dset_name, split='train')
        train_dset = train_dset_base.map(preprocessor, batched=True)

    val_dset = None
    if training_args.do_eval:
        if Path(val_dset_name).exists():
            preprocessor = partial(data_preprocess, tokenizer, 'local')
            val_dset_base = load_dataset('json', data_files=[val_dset_name])['train']
        else:
            preprocessor = partial(data_preprocess, tokenizer, val_dset_name)
            val_dset_base = load_dataset(val_dset_name, split='validation')
        val_dset = val_dset_base.map(preprocessor, batched=True)

    pred_dset = None
    if training_args.do_predict:
        preprocessor = partial(data_preprocess, tokenizer, 'local')
        gen_dset_base = load_dataset('json', data_files=gen_dset_name)
        # Filter missing NER
        gen_dset_base = gen_dset_base.filter(lambda example: len(example['answers']) > 0)
        gen_dset = gen_dset_base.map(preprocessor, batched=True)['train']

    metric = load_metric('rouge')

    data_collator = DataCollatorForSeq2Seq(
        tokenizer,
        model=model,
        label_pad_token_id=-100,
        padding='longest'
    )

    trainer = CustomTrainer(
        model=model,
        args=training_args,
        train_dataset=train_dset,
        eval_dataset=val_dset,
        tokenizer=tokenizer,
        data_collator=data_collator,
        compute_metrics=partial(compute_metrics, tokenizer, metric) if training_args.do_train or training_args.do_eval else None
    )

    if training_args.do_train:
        # Train
        train_result = trainer.train()
        trainer.save_model()
        metrics = train_result.metrics
        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    if training_args.do_eval:
        # Validation
        eval_metrics = trainer.evaluate(
            max_length=tokenizer.model_max_length,
            num_beams=dm_args.num_beams,
            metric_key_prefix='eval'
        )
        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    if training_args.do_predict:
        dl = trainer.get_test_dataloader(gen_dset)
        all_samples = []
        for b in tqdm(dl):
            input_ids = b['input_ids'].to(device)
            samples = model.generate(
                input_ids,
                num_beams=dm_args.num_beams,
                max_length=tokenizer.model_max_length,
                early_stopping=True
            )
            all_samples.extend(list(samples.detach().cpu().numpy()))
        print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in all_samples])
        with open(dm_args.output_predictions, 'wt') as f:
            for id,con,ans,q,citance,paper_id,evidence in zip(gen_dset['id'],gen_dset['context'], gen_dset['answers'], all_samples,gen_dset['citance'],gen_dset['paper_id'], gen_dset['evidence']):
                gen_question = tokenizer.decode(q, skip_special_tokens=True, clean_up_tokenization_spaces=False)
                f.write(json.dumps({'id': id, 'paper_id': paper_id, 'context': con, 'answer': ans[0], 'generated_question': gen_question, 'citance': citance, 'evidence': evidence}) + '\n')

ext: py

max_stars_repo_name: allenai/scientific-claim-generation

max_issues_repo_name: allenai/scientific-claim-generation

max_forks_repo_name: allenai/scientific-claim-generation

reason: import numpy


text:
import sys
import pytest
import numpy
import taurus_pyqtgraph as tpg
import pyqtgraph as pg
from taurus.external.qt import Qt

from .util import show_and_wait, get_sub_config  # noqa

def test_plot_model_setting(qtbot):

    w = tpg.TaurusPlot()
    qtbot.addWidget(w)

    assert len(w) == 0
    assert len(w._model_chooser_tool.getModelNames()) == 0

    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]

    w.setModel(models1)
    c0 = w[0]
    c1 = w[1]
    assert len(w) == 2
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
    ]

    # add a regular data item (non-taurus)
    c2 = pg.PlotDataItem(name="pg item", pen="b", fillLevel=0, brush="c")
    c2.setData(numpy.linspace(0, 20, 10))
    w.addItem(c2)
    assert w[:] == [c0, c1, c2]
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
    ]

    # add a taurus data item
    c3 = tpg.TaurusPlotDataItem(name="taurus item", pen="r", symbol="o")
    c3.setModel('eval:Quantity(rand(16),"m")')
    w.addItem(c3)
    assert w[:] == [c0, c1, c2, c3]
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
        c3.getFullModelNames(),
    ]

    # Add existing (c0) model again (it is ignored)
    w.addModels([models1[0]])
    assert w[:] == [c2, c0, c1, c3]  # there is reordering, non taurus first
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
        c3.getFullModelNames(),
    ]

    # set 1 model (not adding!, the non-taurus curve is kept)
    w.setModel([models1[1]])
    assert w[:] == [c2, c1]  # c1 **is** still the same object!
    assert w._model_chooser_tool.getModelNames() == [c1.getFullModelNames()]

    # set empty model (not adding!, the non taurus curve is kept)
    w.setModel([])
    assert w[:] == [c2]
    assert w._model_chooser_tool.getModelNames() == []

    # remove non-taurus curve
    w.removeItem(c2)
    assert w[:] == []
    assert w._model_chooser_tool.getModelNames() == []

    # Uncomment for visual checks
    # show_and_wait(qtbot, w, timeout=3600000)

def test_plot_model_setting_with_y2(qtbot):

    w = tpg.TaurusPlot()
    qtbot.addWidget(w)
    vb1 = w.getViewBox()
    vb2 = w._y2

    assert w[:] == []
    assert w._model_chooser_tool.getModelNames() == []
    assert vb1.addedItems == []
    assert vb2.addedItems == []

    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]

    w.setModel(models1)
    c0 = w[0]
    c1 = w[1]

    # move the first curve to Y2
    vb1.removeItem(c0)
    vb2.addItem(c0)
    assert vb1.addedItems == [c1]
    assert vb2.addedItems == [c0]

    assert w[:] == [c0, c1]
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
    ]

    # add a regular data item (non-taurus) to y2
    c2 = pg.PlotDataItem(name="pg item", pen="b", fillLevel=0, brush="c")
    c2.setData(numpy.linspace(0, 20, 10))
    vb2.addItem(c2)

    assert vb1.addedItems == [c1]
    assert vb2.addedItems == [c0, c2]

    assert len(w) == 3
    assert w[:] == [c0, c1, c2]
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
    ]

    # add a taurus data item to y2
    c3 = tpg.TaurusPlotDataItem(name="taurus item", pen="r", symbol="o")
    c3.setModel('eval:Quantity(rand(16),"m")')
    vb2.addItem(c3)
    assert vb1.addedItems == [c1]
    assert vb2.addedItems == [c0, c2, c3]
    assert w[:] == [c0, c1, c2, c3]
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
        c3.getFullModelNames(),
    ]

    # Add existing (c0) model again
    w.addModels([models1[0]])
    assert vb1.addedItems == [c1]
    assert vb2.addedItems == [c2, c0, c3]
    assert w[:] == [c2, c0, c1, c3]  # there is reordering, non taurus first
    assert w._model_chooser_tool.getModelNames() == [
        c0.getFullModelNames(),
        c1.getFullModelNames(),
        c3.getFullModelNames(),
    ]

    # set (not adding!) 1 model which is already on y2
    # only non-taurus curve and the set curve remain. Both stay in y2
    w.setModel([models1[0]])
    assert vb1.addedItems == []
    assert vb2.addedItems == [c2, c0]
    assert w[:] == [c2, c0]  # c0 **is** still the same object!
    assert w._model_chooser_tool.getModelNames() == [c0.getFullModelNames()]

    # set empty model (not adding!, only the non-taurus curve remains)
    w.setModel([])
    assert vb1.addedItems == []
    assert vb2.addedItems == [c2]
    assert w[:] == [c2]
    assert w._model_chooser_tool.getModelNames() == []

    # remove non-taurus curve
    c2.getViewBox().removeItem(c2)
    w.getPlotItem().removeItem(c2)
    assert vb1.addedItems == []
    assert vb2.addedItems == []
    assert w[:] == []
    assert w._model_chooser_tool.getModelNames() == []

    # Uncomment for visual checks
    # show_and_wait(qtbot, w, timeout=3600000)

def test_multiple_setModel(qtbot):
    """
    Check that repeated calls to setModel do not duplicate the items
    in the plot
    """
    w = tpg.TaurusPlot()
    qtbot.addWidget(w)
    for i in range(5):
        w.setModel(["eval:rand(2)"])
        assert w.getPlotItem().listDataItems() == [
            w[0]
        ], "Found duplicates after {} calls to setModel".format(i + 1)

def test_xymodelchooser_config(qtbot):

    w1 = tpg.TaurusPlot()
    qtbot.addWidget(w1)

    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]

    w1.setModel(models1)

    # test createConfig
    cfg = w1.createConfig()
    xymccfg1 = get_sub_config(cfg, "XYmodelchooser")
    modelscfg1 = get_sub_config(xymccfg1, "CurveInfo")
    assert modelscfg1[0] == (
        None,
        "eval://localhost/@DefaultEvaluator/1*rand(22)",
        "1*rand(22)",
    )
    assert modelscfg1[1] == (
        "eval://localhost/@DefaultEvaluator/linspace(-10,20,10)",
        "eval://localhost/@DefaultEvaluator/2*rand(10)",
        "2*rand(10)",
    )

    # test applyConfig
    w2 = tpg.TaurusPlot()
    qtbot.addWidget(w2)
    assert len(w2) == 0
    assert len(w2._model_chooser_tool.getModelNames()) == 0

    # add a model to w2
    w2.setModel("eval:9*rand(11)")
    assert len(w2) == 1
    assert len(w2._model_chooser_tool.getModelNames()) == 1

    # apply config (the previously added tauruscurve of w2 should be removed)
    w2.applyConfig(cfg)
    assert len(w2) == 2
    assert len(w2._model_chooser_tool.getModelNames()) == 2
    assert w2._model_chooser_tool._getCurveInfo() == modelscfg1

    # Uncomment for visual checks
    # show_and_wait(qtbot, w1, w2, timeout=3600000)

def test_y2_config(qtbot):

    # create a plot with 3 curves
    w1 = tpg.TaurusPlot()
    qtbot.addWidget(w1)

    models1 = [
        "eval:1*rand(11)",
        "eval:2*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]

    w1.setModel(models1)

    w1_vb1 = w1.getViewBox()
    w1_vb2 = w1._y2

    # check that the 3 curves are on Y1
    assert [c.getViewBox() for c in w1] == [w1_vb1] * 3
    assert len(w1_vb1.addedItems) == 3
    assert len(w1_vb2.addedItems) == 0

    # move the second curve to Y2
    c = w1[1]
    w1_vb1.removeItem(c)
    w1_vb2.addItem(c)

    # check that the move was ok and there are no duplicates
    assert [c.getViewBox() for c in w1] == [w1_vb1, w1_vb2, w1_vb1]
    assert w1_vb2._getCurvesNames() == [w1[1].getFullModelNames()]
    assert len(w1_vb1.addedItems) == 2
    assert len(w1_vb2.addedItems) == 1

    # test createConfig
    plot_cfg = w1.createConfig()
    y2_cfg = get_sub_config(plot_cfg, "Y2Axis")
    curvescfg1 = get_sub_config(y2_cfg, "Y2Curves")
    assert curvescfg1 == [w1[1].getFullModelNames()]

    # # Debugging
    # from pprint import pprint
    # pprint(plot_cfg)
    # pprint(curvescfg1)

    # create a second, empty plot
    w2 = tpg.TaurusPlot()
    qtbot.addWidget(w2)
    assert len(w2) == 0

    # check applyConfig on the new plot
    w2.applyConfig(plot_cfg)
    assert len(w2) == 3

    w2_vb1 = w2.getViewBox()
    w2_vb2 = w2._y2
    w1_all_names = [c.getFullModelNames() for c in w1]
    w2_all_names = [c.getFullModelNames() for c in w2]

    assert w1_all_names == w2_all_names
    assert len(w2_all_names) == 3
    assert len(w2_vb1.addedItems) == 2
    assert len(w2_vb2.addedItems) == 1
    assert w2_vb2._getCurvesNames() == w1_vb2._getCurvesNames()
    assert w2_vb2._getCurvesNames() == [w1[1].getFullModelNames()]

    # Uncomment for visual checks
    # show_and_wait(qtbot, w1, w2, timeout=3600000)

def test_curveproperties(qtbot):

    w = tpg.TaurusPlot()
    qtbot.addWidget(w)

    # add a regular data item (non-taurus) to y1
    c0 = pg.PlotDataItem(name="pg item1", pen="b", fillLevel=0, brush="c")
    c0.setData(numpy.linspace(0, 20, 10))
    w.addItem(c0)

    # add a regular data item (non-taurus) to y2
    c1 = pg.PlotDataItem(name="pg item2", pen="y", symbol="d", symbolBrush="r")
    c1.setData(20 - numpy.linspace(0, 20, 10))
    w._y2.addItem(c1)

    # add a taurus data item to y1
    c2 = tpg.TaurusPlotDataItem(name="taurus item1", pen="r", symbol="o")
    c2.setModel('eval:Quantity(rand(16),"m")')
    w.addItem(c2)

    # add a taurus data item to y2
    c3 = tpg.TaurusPlotDataItem(name="taurus item2", pen="y", symbol="s")
    c3.setModel('eval:Quantity(rand(20),"km")')
    w._y2.addItem(c3)

    # Add 2 more items using setModel
    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]
    w.addModels(models1)
    c4 = w[4]
    c5 = w[5]

    c4.setPen("g")

    c5.setPen(None)
    c5.setSymbol("t")
    c5.setSymbolSize(7)

    # move c5 to y2
    w._y2.addItem(c5)

    assert len(w) == 6
    assert w[:] == [c0, c1, c2, c3, c4, c5]
    assert len(w._cprop_tool.getModifiableItems()) == 6
    for c in [c0, c1, c2, c3, c4, c5]:
        assert c.name() in w._cprop_tool.getModifiableItems().keys()
        assert c in w._cprop_tool.getModifiableItems().values()
    assert w.getViewBox().addedItems == [c0, c2, c4]
    assert w._y2.addedItems == [c1, c3, c5]

    prop = w._cprop_tool._getCurveAppearanceProperties()

    # check lColor
    assert pg.mkColor(prop[c0.name()].lColor) == pg.mkColor("b")
    assert pg.mkColor(prop[c1.name()].lColor) == pg.mkColor("y")
    assert pg.mkColor(prop[c2.name()].lColor) == pg.mkColor("r")
    assert pg.mkColor(prop[c3.name()].lColor) == pg.mkColor("y")
    assert pg.mkColor(prop[c4.name()].lColor) == pg.mkColor("g")
    # assert pg.mkColor(prop[c5.name()].lColor) == pg.mkColor('b')

    # check lStyle
    for c in c0, c1, c2, c3, c4:
        assert prop[c.name()].lStyle == Qt.Qt.SolidLine
    assert prop[c5.name()].lStyle == Qt.Qt.NoPen

    # check y2
    for c in c0, c2, c4:
        assert prop[c.name()].y2 is False
    for c in c1, c3, c5:
        assert prop[c.name()].y2 is True

    # Uncomment for visual checks
    # show_and_wait(qtbot, w, timeout=3600000)

@pytest.mark.xfail(
    sys.version_info[:2] == (3, 6),
    reason="flaky behaviour observed in CI for py 3.6 in this test",
)
def test_curveproperties_config(qtbot):
    w1 = tpg.TaurusPlot()
    qtbot.addWidget(w1)

    # add a regular data item (non-taurus) to y1
    c0 = pg.PlotDataItem(name="pg item1", pen="m", fillLevel=0, brush="c")
    c0.setData(numpy.linspace(0, 20, 10) / 20.0)
    w1.addItem(c0)

    # add a regular data item (non-taurus) to y2
    c1 = pg.PlotDataItem(name="pg item2", pen="y", symbol="d", symbolBrush="r")
    c1.setData(1 - numpy.linspace(0, 20, 10) / 20.0)
    w1._y2.addItem(c1)

    # add a taurus data item to y1
    c2 = tpg.TaurusPlotDataItem(name="taurus item1", pen="r", symbol="o")
    c2.setModel('eval:Quantity(rand(16),"m")')
    w1.addItem(c2)

    # add a taurus data item to y2
    c3 = tpg.TaurusPlotDataItem(name="taurus item2", pen="y", symbol="s")
    c3.setModel('eval:Quantity(rand(20),"km")')
    w1._y2.addItem(c3)

    # Add 2 more items using setModel
    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]
    w1.addModels(models1)
    c4 = w1[4]
    c5 = w1[5]

    c4.setPen("g")

    c5.setPen(None)
    c5.setSymbol("t")
    c5.setSymbolSize(7)
    c5.setSymbolBrush("r")

    # move c5 to y2
    w1._y2.addItem(c5)

    assert len(w1) == 6
    assert w1[:] == [c0, c1, c2, c3, c4, c5]
    w1_mod_items = w1._cprop_tool.getModifiableItems()
    for c in [c0, c1, c2, c3, c4, c5]:
        assert c0 in w1_mod_items.values()
    assert w1.getViewBox().addedItems == [c0, c2, c4]
    assert w1._y2.addedItems == [c1, c3, c5]

    # test createConfig
    cfg = w1.createConfig()
    propcfg = get_sub_config(cfg, "CurvePropertiesTool")
    _ = get_sub_config(propcfg, "CurveProperties")

    # # Debugging
    # from pprint import pprint
    # # pprint(cfg)
    # print("-" * 80)
    # pprint(curvescfg)

    # test applyConfig
    w2 = tpg.TaurusPlot()
    qtbot.addWidget(w2)
    # assert len(w2._model_chooser_tool.getModelNames()) == 0
    assert len(w2._cprop_tool._getCurveAppearanceProperties()) == 0

    w2.applyConfig(cfg)
    # assert len(w2._model_chooser_tool.getModelNames()) == 2
    # assert w2._model_chooser_tool.getModelNames() == modelscfg1

    w1_props = w1._cprop_tool._getCurveAppearanceProperties()
    w2_props = w2._cprop_tool._getCurveAppearanceProperties()

    assert len(w1_props) == 6
    assert len(w2_props) == 4
    for k, p_aft in w2_props.items():
        assert k in w1_props
        p_ini = w1_props[k]
        conflicts = p_ini.conflictsWith(p_aft, strict=True)
        msg = "Mismatch in saved/restored curve properties for '{}':".format(k)
        msg += "\n\t Saved:    {}".format(p_ini)
        msg += "\n\t Restored: {}".format(p_aft)
        assert conflicts == [], msg

    # test applyConfig
    w3 = tpg.TaurusPlot()
    qtbot.addWidget(w3)

    # Manually add regular data items matching the names used in w1
    # but do not match the properties, which should be updated by applyConfig
    c0_w3 = pg.PlotDataItem(
        name="pg item1", y=numpy.linspace(0, 20, 10) / 20.0
    )
    w3.addItem(c0_w3)
    c1_w3 = pg.PlotDataItem(name="pg item2", y=numpy.zeros(15))
    w3._y2.addItem(c1_w3)

    assert len(w3._cprop_tool._getCurveAppearanceProperties()) == 2

    w3.applyConfig(cfg)

    w3_props = w3._cprop_tool._getCurveAppearanceProperties()

    assert len(w3_props) == 6
    for k, p_aft in w3_props.items():
        assert k in w1_props
        p_ini = w1_props[k]
        conflicts = p_ini.conflictsWith(p_aft, strict=True)
        msg = "Mismatch in saved/restored curve properties for '{}':".format(k)
        msg += "\n\t Saved:    {}".format(p_ini)
        msg += "\n\t Restored: {}".format(p_aft)
        assert conflicts == [], msg

    # Uncomment for visual checks
    # show_and_wait(qtbot, w1, w2, w3, timeout=3600000)

def test_curveproperties_configfile(qtbot, tmp_path):
    w1 = tpg.TaurusPlot()
    qtbot.addWidget(w1)
    w1.setBackground("y")

    # add a regular data item (non-taurus) to y1
    c0 = pg.PlotDataItem(name="pg item1", pen="m", fillLevel=0, brush="c")
    c0.setData(numpy.linspace(0, 20, 10) / 20.0)
    w1.addItem(c0)

    # add a regular data item (non-taurus) to y2
    c1 = pg.PlotDataItem(name="pg item2", pen="y", symbol="d", symbolBrush="r")
    c1.setData(1 - numpy.linspace(0, 20, 10) / 20.0)
    w1._y2.addItem(c1)

    # add a taurus data item to y1
    c2 = tpg.TaurusPlotDataItem(name="taurus item1", pen="r", symbol="o")
    c2.setModel('eval:Quantity(rand(16),"m")')
    w1.addItem(c2)

    # add a taurus data item to y2
    c3 = tpg.TaurusPlotDataItem(name="taurus item2", pen="y", symbol="s")
    c3.setModel('eval:Quantity(rand(20),"km")')
    w1._y2.addItem(c3)

    # Add 2 more items using setModel
    models1 = [
        "eval:1*rand(22)",
        ("eval:linspace(-10,20,10)", "eval:2*rand(10)"),
    ]
    w1.addModels(models1)
    c4 = w1[4]
    c5 = w1[5]

    c4.setPen("g")

    c5.setPen(None)
    c5.setSymbol("t")
    c5.setSymbolSize(7)
    c5.setSymbolBrush("r")

    # move c5 to y2
    w1._y2.addItem(c5)

    assert len(w1) == 6
    assert w1[:] == [c0, c1, c2, c3, c4, c5]
    w1_mod_items = w1._cprop_tool.getModifiableItems()
    for c in [c0, c1, c2, c3, c4, c5]:
        assert c0 in w1_mod_items.values()
    assert w1.getViewBox().addedItems == [c0, c2, c4]
    assert w1._y2.addedItems == [c1, c3, c5]

    # test saveConfigFile
    f = tmp_path / "plot.pck"
    with open(str(f), "wb") as ofile:
        w1.saveConfigFile(ofile=ofile)
    assert f.exists()
    assert len(list(tmp_path.iterdir())) == 1

    # test loadConfigFile
    w2 = tpg.TaurusPlot()
    qtbot.addWidget(w2)
    with open(str(f), "rb") as ifile:
        w2.loadConfigFile(ifile=ifile)

    w1_props = w1._cprop_tool._getCurveAppearanceProperties()
    w2_props = w2._cprop_tool._getCurveAppearanceProperties()

    assert len(w1_props) == 6
    assert len(w2_props) == 4
    for k, p_aft in w2_props.items():
        assert k in w1_props
        p_ini = w1_props[k]
        conflicts = p_ini.conflictsWith(p_aft, strict=True)
        msg = "Mismatch in saved/restored curve properties for '{}':".format(k)
        msg += "\n\t Saved:    {}".format(p_ini)
        msg += "\n\t Restored: {}".format(p_aft)
        assert conflicts == [], msg

    # Uncomment for visual checks
    # show_and_wait(qtbot, w1, w2, timeout=3600000)

# def test_save_config_action(qtbot, tmp_path):
#     w1 = tpg.TaurusPlot()
#     qtbot.addWidget(w1)
#
#     w2 = tpg.TaurusPlot()
#     qtbot.addWidget(w2)
#
#     menu = w1.getPlotItem().getViewBox().menu
#     save_action = None
#     for a in menu.actions():
#         # print(a.text())
#         if a.text() == "Save configuration":
#             save_action = a
#             break
#     assert save_action is not None
#     save_action.trigger()
#     # TODO: handle the modal dialog
#     menu = w2.getPlotItem().getViewBox().menu
#     load_action = None
#     for a in menu.actions():
#         # print(a.text())
#         if a.text() == "Retrieve saved configuration":
#             load_action = a
#             break
#     assert load_action is not None
#     load_action.trigger()
#     # TODO: handle the modal dialog

def test_duplicate_curve_titles(qtbot):
    """
    Check that curves with same title are changed to have unique titles
    """
    w = tpg.TaurusPlot()
    qtbot.addWidget(w)
    w.setModel(
        [
            "eval:@foo/a=0+rand(3);a",
            "eval:@bar/a=1+rand(3);a",
            (None, "eval:2+rand(3)", "a"),
            "eval:3+rand(3)",
        ]
    )

    assert len(w[:]) == 4
    mod_items = w._cprop_tool.getModifiableItems()
    assert len(mod_items) == 4, "{}".format(mod_items)
    assert list(sorted(mod_items.keys())) == [
        "3+rand(3)",
        "a",
        "a (2)",
        "a (3)",
    ]
    # show_and_wait(qtbot, w)


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

import mindspore.nn as nn
from mindspore import Tensor
import mindspore.context as context
from mindspore.ops import operations as P
from mindspore.ops.operations import _inner_ops as inner

class Net(nn.Cell):
    def __init__(self, keep_prob):
        super(Net, self).__init__()
        self.drop = P.Dropout(keep_prob)

    def construct(self, x_):
        return self.drop(x_)

@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard
def test_dropout():
    x_shape = [32, 16, 2, 5]
    x = np.ones(x_shape).astype(np.float32)
    keep_prob = 0.4
    dropout = Net(keep_prob)
    tx = Tensor(x)
    output, mask = dropout(tx)
    # check output
    output_np = output.asnumpy()
    elem_count = x.size
    nonzero_count = np.count_nonzero(output_np)
    assert (elem_count * (keep_prob - 0.1)) < nonzero_count < (elem_count * (keep_prob + 0.1))
    output_sum = np.sum(output_np)
    x_sum = np.sum(x)
    assert abs(output_sum - x_sum)/x_sum < 0.1
    # check mask
    mask_np = mask.asnumpy()
    mask_sum = np.sum(mask_np)
    assert np.count_nonzero(mask_np) == nonzero_count
    assert abs(mask_sum - nonzero_count)/nonzero_count < 0.1

class DropoutDynamic(nn.Cell):
    def __init__(self, keep_prob):
        super(DropoutDynamic, self).__init__()
        self.test_dynamic = inner.GpuConvertToDynamicShape()
        self.drop = P.Dropout(keep_prob)

    def construct(self, x):
        x = self.test_dynamic(x)
        return self.drop(x)

@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard
def test_dropout_dynamic():
    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
    x_1 = np.ones([32, 16, 2, 5]).astype(np.float32)
    x_2 = np.ones([32, 16, 2, 5, 6]).astype(np.float32)
    keep_prob = 0.4
    net = DropoutDynamic(keep_prob)

    output_1, mask_1 = net(Tensor(x_1))
    elem_count_1 = x_1.size
    nonzero_count_1 = np.count_nonzero(output_1.asnumpy())
    assert (elem_count_1 * (keep_prob - 0.1)) < nonzero_count_1 < (elem_count_1 * (keep_prob + 0.1))
    output_sum_1 = np.sum(output_1.asnumpy())
    x_sum_1 = np.sum(x_1)
    assert abs(output_sum_1 - x_sum_1)/x_sum_1 < 0.1
    mask_sum_1 = np.sum(mask_1.asnumpy())
    assert np.count_nonzero(mask_1.asnumpy()) == nonzero_count_1
    assert abs(mask_sum_1 - nonzero_count_1)/nonzero_count_1 < 0.1

    output_2, mask_2 = net(Tensor(x_2))
    elem_count_2 = x_2.size
    nonzero_count_2 = np.count_nonzero(output_2.asnumpy())
    assert (elem_count_2 * (keep_prob - 0.1)) < nonzero_count_2 < (elem_count_2 * (keep_prob + 0.1))
    output_sum_2 = np.sum(output_2.asnumpy())
    x_sum_2 = np.sum(x_2)
    assert abs(output_sum_2 - x_sum_2)/x_sum_2 < 0.1
    mask_sum_2 = np.sum(mask_2.asnumpy())
    assert np.count_nonzero(mask_2.asnumpy()) == nonzero_count_2
    assert abs(mask_sum_2 - nonzero_count_2)/nonzero_count_2 < 0.1

max_stars_repo_name: kungfu-team/mindspore-bert

max_issues_repo_name: peixinhou/mindspore

max_forks_repo_name: peixinhou/mindspore


text:
import numpy as np

#Définition des constantes
G = 6.67408 * 10**(-11)
dt = 1
masse_terre = 5.9722*(10)**24
rayon_terre = 6378.137 *(10)**3
t = 0

###############################################
#      Définition d'une classe planète        #
###############################################
class Planet:

    #Définir les différents attributs
    def __init__(self,mass,rayon,x,y,vx,vy,nom,x0=0,y0=0,init=False):
        self.mass = mass
        self.rayon = rayon
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.nom = nom
        self.x0 = x0
        self.y0 = y0
        self.init = init

    #Définir les différentes méthodes
    #Distance
    def distance(self, autre_planete):
        d = np.sqrt((autre_planete.x-self.x)**2 + (autre_planete.y-self.y)**2)
        return d

    #Calcul de l'accélération résultante
    def acceleration(self,liste_planetes,G=6.67408 * 10**(-11)):
        ax = 0
        ay = 0
        for planets in liste_planetes:
            if planets is self or self.x == planets.x:
                pass
            else:
                d = self.distance(planets)
                ax += (G * planets.mass)/(d**2) * (planets.x - self.x)/d
                ay += (G * planets.mass)/(d**2) * (planets.y - self.y)/d

        return ax, ay

    #Actualiser la vitesse de la planète
    def actualiser_sys(self,ax,ay,dt):
        if not self.init:
            self.init = True
            #Change the speed with the acceleration using Euler method
            self.vx = self.vx + ax * dt
            self.vy = self.vy + ay * dt

            #Compute the position using the spedd and Euler's method
            x = self.x + self.vx * dt + (1/2) * ax * dt**2
            y = self.y + self.vy * dt + (1/2) * ay * dt**2

            #Update the values of the position (x,y) and old position (x0,y0)
            # 1) Old position
            self.x0 = self.x
            self.y0 = self.y

            # 2) New position
            self.x = x
            self.y = y

            #Else, use verlet integration method to actualize position and speed
        else:
            #Defining the new position
            x_t2 = 2 * self.x - self.x0 + ax * dt**2
            y_t2 = 2 * self.y - self.y0 + ay * dt**2

            #Defining the new speed with the new position
            self.vx = ((x_t2 - self.x0) / (2*dt))
            self.vy = ((y_t2 - self.y0) / (2*dt))

            #Updating the position
            # 1) Old positions
            self.x0 = self.x
            self.y0 = self.y

            # 2) New positions
            self.x = x_t2
            self.y = y_t2

    #Énergie cinétique
    def ECin(self):
        T = 0.5 * self.mass * (self.vx**2 + self.vy**2)
        return T

    #Énergie potentielle gravitationnelle entre deux planètes
    def EGrav(self, autre_planete):
        U = - (G * self.mass * autre_planete.mass) / self.distance(autre_planete)
        return U

max_stars_repo_name: FelixDesrochers/Gravitation

max_issues_repo_name: FelixDesrochers/Gravitation

max_forks_repo_name: FelixDesrochers/Gravitation


text:
# Copyright (C) 2020-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""Python native tests."""

import numpy as np

import openfl.native as fx

def one_hot(labels, classes):
    """
    One Hot encode a vector.

    Args:
        labels (list):  List of labels to onehot encode
        classes (int): Total number of categorical classes

    Returns:
        np.array: Matrix of one-hot encoded labels
    """
    return np.eye(classes)[labels]

def build_model(input_shape,
                num_classes,
                conv_kernel_size=(4, 4),
                conv_strides=(2, 2),
                conv1_channels_out=16,
                conv2_channels_out=32,
                final_dense_inputsize=100,
                **kwargs):
    """
    Define the model architecture.

    Args:
        input_shape (numpy.ndarray): The shape of the data
        num_classes (int): The number of classes of the dataset

    Returns:
        tensorflow.python.keras.engine.sequential.Sequential: The model defined in Keras

    """
    import tensorflow as tf # NOQA
    import tensorflow.keras as ke # NOQA

    from tensorflow.keras import Sequential # NOQA
    from tensorflow.keras.layers import Conv2D, Flatten, Dense # NOQA
    config = tf.compat.v1.ConfigProto()
    config.gpu_options.allow_growth = True
    config.intra_op_parallelism_threads = 112
    config.inter_op_parallelism_threads = 1
    sess = tf.compat.v1.Session(config=config)
    model = Sequential()

    model.add(Conv2D(conv1_channels_out,
                     kernel_size=conv_kernel_size,
                     strides=conv_strides,
                     activation='relu',
                     input_shape=input_shape))

    model.add(Conv2D(conv2_channels_out,
                     kernel_size=conv_kernel_size,
                     strides=conv_strides,
                     activation='relu'))

    model.add(Flatten())

    model.add(Dense(final_dense_inputsize, activation='relu'))

    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss=ke.losses.categorical_crossentropy,
                  optimizer=ke.optimizers.Adam(),
                  metrics=['accuracy'])

    # initialize the optimizer variables
    opt_vars = model.optimizer.variables()

    for v in opt_vars:
        v.initializer.run(session=sess)

    return model

if __name__ == '__main__':
    fx.init('keras_cnn_mnist')
    from openfl.federated import FederatedModel, FederatedDataSet
    from tensorflow.python.keras.utils.data_utils import get_file

    origin_folder = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/'
    path = get_file('mnist.npz',
                    origin=origin_folder + 'mnist.npz',
                    file_hash='731c5ac602752760c8e48fbffcf8c3b850d9dc2a2aedcf2cc48468fc17b673d1')

    with np.load(path) as f:
        # get all of mnist
        X_train = f['x_train']
        y_train = f['y_train']

        X_valid = f['x_test']
        y_valid = f['y_test']
    img_rows, img_cols = 28, 28
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
    X_valid = X_valid.reshape(X_valid.shape[0], img_rows, img_cols, 1)
    X_train = X_train.astype('float32')
    X_valid = X_valid.astype('float32')
    X_train /= 255
    X_valid /= 255

    classes = 10
    y_train = one_hot(y_train, classes)
    y_valid = one_hot(y_valid, classes)

    feature_shape = X_train.shape[1]

    fl_data = FederatedDataSet(X_train, y_train, X_valid, y_valid,
                               batch_size=32, num_classes=classes)
    fl_model = FederatedModel(build_model=build_model, data_loader=fl_data)
    collaborator_models = fl_model.setup(num_collaborators=2)
    collaborators = {'one': collaborator_models[0], 'two': collaborator_models[1]}
    print(f'Original training data size: {len(X_train)}')
    print(f'Original validation data size: {len(X_valid)}\n')

    # Collaborator one's data
    print(f'Collaborator one\'s training data size: \
            {len(collaborator_models[0].data_loader.X_train)}')
    print(f'Collaborator one\'s validation data size: \
            {len(collaborator_models[0].data_loader.X_valid)}\n')

    # Collaborator two's data
    print(f'Collaborator two\'s training data size: \
            {len(collaborator_models[1].data_loader.X_train)}')
    print(f'Collaborator two\'s validation data size: \
            {len(collaborator_models[1].data_loader.X_valid)}\n')

    print(fx.get_plan())
    final_fl_model = fx.run_experiment(collaborators, {'aggregator.settings.rounds_to_train': 5})
    final_fl_model.save_native('final_pytorch_model.h5')

max_stars_repo_name: igor-davidyuk/openfl

max_issues_repo_name: igor-davidyuk/openfl

max_forks_repo_name: igor-davidyuk/openfl


text:
import numpy as np

class OU_noise(object):
    def __init__(self, num_actions, action_low_bound, action_high_bound, dt,
                 mu=0.0, theta=0.15, max_sigma=2.0, min_sigma=0.1):
        self.mu = mu  # 0.0
        self.theta = theta  # 0.15
        self.sigma = max_sigma  # 0.3
        self.max_sigma = max_sigma  # 0.3
        self.min_sigma = min_sigma  # 0.1
        self.dt = dt  # 0.001
        self.num_actions = num_actions  # 1
        self.action_low = action_low_bound  # -2
        self.action_high = action_high_bound  # 2
        self.reset()

    def reset(self):
        self.state = np.zeros(self.num_actions)

    # self.state = np.zeros(self.num_actions)
    def state_update(self):
        x = self.state
        dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.num_actions)  # np.random.randn()生成0,1的随机数
        self.state = x + dx

    def add_noise(self, action):
        self.state_update()
        state = self.state
        self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, self.dt)
        return np.clip(action + state, self.action_low, self.action_high)

max_stars_repo_name: kaixindelele/DRL

max_issues_repo_name: kaixindelele/DRL

max_forks_repo_name: kaixindelele/DRL


text:
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
from load_data import train_iter, val_iter, id2vocab, PAD_IDX
from model import Encoder, Decoder, Transformer

device = "cuda" if torch.cuda.is_available() else 'cpu'
INPUT_DIM = len(id2vocab)
OUTPUT_DIM = len(id2vocab)
HID_DIM = 512
ENC_LAYERS = 6
DEC_LAYERS = 6
ENC_HEADS = 8
DEC_HEADS = 8
ENC_PF_DIM = 2048
DEC_PF_DIM = 2048
ENC_DROPOUT = 0.1
DEC_DROPOUT = 0.1
N_EPOCHS = 10
CLIP = 1

enc = Encoder(INPUT_DIM, HID_DIM, ENC_LAYERS, ENC_HEADS, ENC_PF_DIM, ENC_DROPOUT, device)
dec = Decoder(OUTPUT_DIM, HID_DIM, DEC_LAYERS, DEC_HEADS, DEC_PF_DIM, DEC_DROPOUT, device)

model = Transformer(enc, dec, PAD_IDX, device).to(device)

def initialize_weights(m):
    if hasattr(m, 'weight') and m.weight.dim() > 1:
        nn.init.xavier_uniform_(m.weight.data)
model.apply(initialize_weights)

optimizer = optim.Adam(model.parameters(), lr=5e-5)
#we ignore the loss whenever the target token is a padding token.
criterion = nn.CrossEntropyLoss(ignore_index = PAD_IDX)

loss_vals = []
loss_vals_eval = []
for epoch in range(N_EPOCHS):
    model.train()
    epoch_loss= []
    pbar = tqdm(train_iter)
    pbar.set_description("[Train Epoch {}]".format(epoch))
    for batch in pbar:
        trg, src = batch.trg.to(device), batch.src.to(device)
        model.zero_grad()
        output, _ = model(src, trg[:,:-1])
        #trg = [batch size, trg len]
        #output = [batch size, trg len-1, output dim]
        output_dim = output.shape[-1]
        output = output.contiguous().view(-1, output_dim)
        trg = trg[:,1:].contiguous().view(-1)
        #trg = [(trg len - 1) * batch size]
        #output = [(trg len - 1) * batch size, output dim]
        loss = criterion(output, trg)
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), CLIP)
        epoch_loss.append(loss.item())
        optimizer.step()
        pbar.set_postfix(loss=loss.item())
    loss_vals.append(np.mean(epoch_loss))

    model.eval()
    epoch_loss_eval= []
    pbar = tqdm(val_iter)
    pbar.set_description("[Eval Epoch {}]".format(epoch))
    for batch in pbar:
        trg, src = batch.trg.to(device), batch.src.to(device)
        model.zero_grad()
        output, _ = model(src, trg[:,:-1])
        #trg = [batch size, trg len]
        #output = [batch size, trg len-1, output dim]
        output_dim = output.shape[-1]
        output = output.contiguous().view(-1, output_dim)
        trg = trg[:,1:].contiguous().view(-1)
        #trg = [(trg len - 1) * batch size]
        #output = [(trg len - 1) * batch size, output dim]
        loss = criterion(output, trg)
        epoch_loss_eval.append(loss.item())
        pbar.set_postfix(loss=loss.item())
    loss_vals_eval.append(np.mean(epoch_loss_eval))

torch.save(model.state_dict(), 'model.pt')

l1, = plt.plot(np.linspace(1, N_EPOCHS, N_EPOCHS).astype(int), loss_vals)
l2, = plt.plot(np.linspace(1, N_EPOCHS, N_EPOCHS).astype(int), loss_vals_eval)
plt.legend(handles=[l1,l2],labels=['Train loss','Eval loss'],loc='best')

max_stars_repo_name: techthiyanes/nlp-notebook

max_issues_repo_name: techthiyanes/nlp-notebook

max_forks_repo_name: techthiyanes/nlp-notebook


text:
#!/usr/bin/env python3

from mendeleev import element # For looking up atomic numbers, etc
import numpy as np

from base_pot import PotProvider

# This is a map of pairs to parameters
# Parameters are in order {epsilon, sigma}
# Epsilon in eV, sigma in A
LJ_Params = {}

# Here are some example parameters
LJ_Params['Au-Au'] = [0.4129, 2.646] # https://doi.org/10.1063/1.4921075
LJ_Params['Ag-Ag'] = [0.3506, 2.648] # https://doi.org/10.1063/1.4921075
LJ_Params['Au-Ag'] = [0.3848, 2.647] # https://doi.org/10.1063/1.4921075

# fundamental charge squared in our units
esqr = 14.398

class LJ(PotProvider):

    def V_r(self, A, B, r):
        pair = A+'-'+B
        if not pair in DE_Params:
            pair = B+'-'+A
        if not pair in DE_Params:
            print("Unknown Pair {}".format(pair))
            exit()
        params = LJ_Params[pair]
        epsilon = params[0]
        sigma = params[1]

        A = 4 * epsilon * sigma**12
        B = 4 * epsilon * sigma**6

        return (A/(r**12)) - (B/(r**6))

    def dV_dr(self, A, B, r):
        pair = A+'-'+B
        if not pair in DE_Params:
            pair = B+'-'+A
        if not pair in DE_Params:
            print("Unknown Pair {}".format(pair))
            exit()
        params = LJ_Params[pair]
        epsilon = params[0]
        sigma = params[1]

        A = -48 * epsilon * sigma**12
        B = -24 * epsilon * sigma**6

        return - (A/(r**13)) + (B/(r**7))

    def name(self):
        return "Lennard Jones"

    def abbrv(self):
        return "LJ"

max_stars_repo_name: SINS-Lab/SEA-SAFARI

max_issues_repo_name: SINS-Lab/SEA-SAFARI

max_forks_repo_name: SINS-Lab/SEA-SAFARI


text:
import numpy as np
import os.path as osp
from setuptools import setup, find_packages
from distutils.extension import Extension
from Cython.Build import cythonize

def readme():
    # with open('README.rst') as f:
        # content = f.read()
    return ''

def find_version():
    version_file = 'torchreid/__init__.py'
    with open(version_file, 'r') as f:
        exec(compile(f.read(), version_file, 'exec'))
    return locals()['__version__']

def numpy_include():
    try:
        numpy_include = np.get_include()
    except AttributeError:
        numpy_include = np.get_numpy_include()
    return numpy_include

ext_modules = [
    Extension(
        'torchreid.metrics.rank_cylib.rank_cy',
        ['torchreid/metrics/rank_cylib/rank_cy.pyx'],
        include_dirs=[numpy_include()],
    )
]

def get_requirements(filename='requirements.txt'):
    here = osp.dirname(osp.realpath(__file__))
    with open(osp.join(here, filename), 'r') as f:
        requires = [line.replace('\n', '') for line in f.readlines()]
    return requires

setup(
    name='torchreid',
    version=find_version(),
    description='A library for deep learning person re-ID in PyTorch',
    author='Kaiyang Zhou',
    license='MIT',
    long_description=readme(),
    url='https://github.com/KaiyangZhou/deep-person-reid',
    packages=find_packages(),
    install_requires=get_requirements(),
    keywords=['Person Re-Identification', 'Deep Learning', 'Computer Vision'],
    ext_modules=cythonize(ext_modules)
)


text:
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import numpy as np

from extensions.ops.rank import Rank
from extensions.ops.size import Size
from mo.front.extractor import FrontExtractorOp
from mo.front.tf.extractors.utils import tf_int_list, tf_dtype_extractor
from mo.graph.graph import Node
from mo.ops.reshape import Reshape
from mo.ops.shape import Shape
from mo.ops.squeeze import Squeeze

class RankFrontExtractor(FrontExtractorOp):
    op = 'Rank'
    enabled = True

    @classmethod
    def extract(cls, node: Node):
        Rank.update_node_stat(node, {'output_type': np.int32})
        return cls.enabled

class ReshapeExtractor(FrontExtractorOp):
    op = 'Reshape'
    enabled = True

    @classmethod
    def extract(cls, node: Node):
        Reshape.update_node_stat(node, {'special_zero': False})
        return cls.enabled

class ShapeExtractor(FrontExtractorOp):
    op = 'Shape'
    enabled = True

    @classmethod
    def extract(cls, node: Node):
        Shape.update_node_stat(node, {'output_type': tf_dtype_extractor(node.pb.attr['out_type'].type, np.int32)})
        return cls.enabled

class SizeFrontExtractor(FrontExtractorOp):
    op = 'Size'
    enabled = True

    @classmethod
    def extract(cls, node):
        Size.update_node_stat(node, {'output_type': tf_dtype_extractor(node.pb.attr['out_type'].type, np.int32)})
        return cls.enabled

class SqueezeExtractor(FrontExtractorOp):
    op = 'Squeeze'
    enabled = True

    @classmethod
    def extract(cls, node: Node):
        Squeeze.update_node_stat(node, {'squeeze_dims': tf_int_list(node.pb.attr['squeeze_dims'].list)})
        return cls.enabled

max_stars_repo_name: monroid/openvino

max_issues_repo_name: thomas-yanxin/openvino

max_forks_repo_name: thomas-yanxin/openvino


text:
#!/usr/bin/env python3

"""
Phys2bids is a python3 library meant to set physiological files in BIDS
standard.

It was born for Acqknowledge files (BIOPAC), and at the moment it supports
``.acq`` files and ``.txt`` files obtained by labchart (ADInstruments).

It requires python 3.6 or above, as well as the modules:
- `numpy`
- `matplotlib`

In order to process ``.acq`` files, it needs `bioread`, an excellent module
that can be found at `this link`_

The project is under development.

At the very moment, it assumes:
-  the input file is from one individual scan, not one session with multiple scans.

.. _this link:
   https://github.com/uwmadison-chm/bioread

Copyright 2019, The Phys2BIDS community.
Please scroll to bottom to read full license.

"""

import os
import logging
import datetime

from copy import deepcopy
from numpy import savetxt
from pathlib import Path

from phys2bids import utils, viz, _version
from phys2bids.cli.run import _get_parser
from phys2bids.physio_obj import BlueprintOutput

LGR = logging.getLogger(__name__)

def print_summary(filename, ntp_expected, ntp_found, samp_freq, time_offset, outfile):
    """
    Prints a summary onscreen and in file with informations on the files.

    Parameters
    ----------
    filename: str
        Name of the input of phys2bids.
    ntp_expected: int
        Number of expected timepoints, as defined by user.
    ntp_found: int
        Number of timepoints found with the automatic process.
    samp_freq: float
        Frequency of sampling for the output file.
    time_offset: float
        Difference between beginning of file and first TR.
    outfile: str or path
        Fullpath to output file.

    Notes
    -----
    Outcome:
    summary: str
        Prints the summary on screen
    outfile: .log file
        File containing summary
    """
    start_time = -time_offset
    summary = (f'\n------------------------------------------------\n'
               f'Filename:            {filename}\n'
               f'\n'
               f'Timepoints expected: {ntp_expected}\n'
               f'Timepoints found:    {ntp_found}\n'
               f'Sampling Frequency:  {samp_freq} Hz\n'
               f'Sampling started at: {start_time} s\n'
               f'Tip: Time 0 is the time of first trigger\n'
               f'------------------------------------------------\n')
    LGR.info(summary)
    utils.writefile(outfile, '.log', summary)

def print_json(outfile, samp_freq, time_offset, ch_name):
    """
    Prints the json required by BIDS format.

    Parameters
    ----------
    outfile: str or path
        Fullpath to output file.
    samp_freq: float
        Frequency of sampling for the output file.
    time_offset: float
        Difference between beginning of file and first TR.
    ch_name: list of str
        List of channel names, as specified by BIDS format.

    Notes
    -----
    Outcome:
    outfile: .json file
        File containing information for BIDS.
    """
    start_time = -time_offset
    summary = dict(SamplingFrequency=samp_freq,
                   StartTime=start_time,
                   Columns=ch_name)
    utils.writejson(outfile, summary, indent=4, sort_keys=False)

def use_heuristic(heur_file, sub, ses, filename, outdir, record_label=''):
    utils.check_file_exists(heur_file)
    """
    Import the heuristic file specified by the user and uses its output
    to rename the file.

    Parameters
    ----------
    heur_file: path
        Fullpath to heuristic file.
    sub: str or int
        Name of subject.
    ses: str or int or None
        Name of session.
    filename: path
        Name of the input of phys2bids.
    outdir: str or path
        Path to the directory that will become the "site" folder
        ("root" folder of BIDS database).
    record_label: str
        Optional label for the "record" entry of BIDS.

    Returns
    -------
    heurpath: str or path
        Returned fullpath to tsv.gz new file (post BIDS formatting).
    """

    if sub[:4] != 'sub-':
        name = f'sub-{sub}'
    else:
        name = sub

    fldr = os.path.join(outdir, name)

    if ses:
        if ses[:4] != 'ses-':
            ses = f'ses-{ses}'

        fldr = os.path.join(fldr, ses)
        name = f'{name}_{ses}'

    fldr = os.path.join(fldr, 'func')
    utils.path_exists_or_make_it(fldr)

    cwd = os.getcwd()
    os.chdir(outdir)

    heur = utils.load_heuristic(heur_file)
    name = heur.heur(Path(filename).stem, name)

    recording = ''
    if record_label:
        recording = f'_recording-{record_label}'

    heurpath = os.path.join(fldr, f'{name}{recording}_physio')
    # for ext in ['.tsv.gz', '.json', '.log']:
    #     move_file(outfile, heurpath, ext)
    os.chdir(cwd)

    return heurpath

def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
              sub=None, ses=None, chtrig=0, chsel=None, num_timepoints_expected=0,
              tr=1, thr=None, ch_name=[], chplot='', debug=False, quiet=False):
    """
    Main workflow of phys2bids.
    Runs the parser, does some checks on input, then imports
    the right interface file to read the input. If only info is required,
    it returns a summary onscreen.
    Otherwise, it operates on the input to return a .tsv.gz file, possibily
    in BIDS format.

    Raises
    ------
    NotImplementedError
        If the file extension is not supported yet.

    """
    # Check options to make them internally coherent pt. I
    # #!# This can probably be done while parsing?
    outdir = utils.check_input_dir(outdir)
    utils.path_exists_or_make_it(outdir)

    # Create logfile name
    basename = 'phys2bids_'
    extension = 'tsv'
    isotime = datetime.datetime.now().strftime('%Y-%m-%dT%H%M%S')
    logname = os.path.join(outdir, (basename + isotime + '.' + extension))

    # Set logging format
    log_formatter = logging.Formatter(
        '%(asctime)s\t%(name)-12s\t%(levelname)-8s\t%(message)s',
        datefmt='%Y-%m-%dT%H:%M:%S')

    # Set up logging file and open it for writing
    log_handler = logging.FileHandler(logname)
    log_handler.setFormatter(log_formatter)
    sh = logging.StreamHandler()

    if quiet:
        logging.basicConfig(level=logging.WARNING,
                            handlers=[log_handler, sh])
    elif debug:
        logging.basicConfig(level=logging.DEBUG,
                            handlers=[log_handler, sh])
    else:
        logging.basicConfig(level=logging.INFO,
                            handlers=[log_handler, sh])

    version_number = _version.get_versions()['version']
    LGR.info(f'Currently running phys2bids version {version_number}')
    LGR.info(f'Input file is {filename}')

    # Check options to make them internally coherent pt. II
    # #!# This can probably be done while parsing?
    indir = utils.check_input_dir(indir)
    filename, ftype = utils.check_input_type(filename,
                                             indir)

    if heur_file:
        heur_file = utils.check_input_ext(heur_file, '.py')
        utils.check_file_exists(heur_file)

    infile = os.path.join(indir, filename)
    utils.check_file_exists(infile)

    # Read file!
    if ftype == 'acq':
        from phys2bids.interfaces.acq import populate_phys_input
    elif ftype == 'txt':
        from phys2bids.interfaces.txt import populate_phys_input
    else:
        # #!# We should add a logger here.
        raise NotImplementedError('Currently unsupported file type.')

    LGR.info(f'Reading the file {infile}')
    phys_in = populate_phys_input(infile, chtrig)
    LGR.info('Reading infos')
    phys_in.print_info(filename)
    # #!# Here the function viz.plot_channel should be called
    if chplot != '' or info:
        viz.plot_all(phys_in.ch_name, phys_in.timeseries, phys_in.units,
                     phys_in.freq, infile, chplot)
    # If only info were asked, end here.
    if info:
        return

    # Create trigger plot. If possible, to have multiple outputs in the same
    # place, adds sub and ses label.
    if tr != 0 and num_timepoints_expected != 0:
        # Run analysis on trigger channel to get first timepoint and the time offset.
        # #!# Get option of no trigger! (which is wrong practice or Respiract)
        phys_in.check_trigger_amount(chtrig, thr, num_timepoints_expected, tr)
        LGR.info('Plot trigger')
        plot_path = os.path.join(outdir,
                                 os.path.splitext(os.path.basename(filename))[0])
        if sub:
            plot_path += f'_sub-{sub}'
        if ses:
            plot_path += f'_ses-{ses}'
        viz.plot_trigger(phys_in.timeseries[0], phys_in.timeseries[chtrig],
                         plot_path, tr, phys_in.thr, num_timepoints_expected, filename)
    else:
        LGR.warning('Skipping trigger pulse count. If you want to run it, '
                    'call phys2bids using "-ntp" and "-tr" arguments')

    # The next few lines remove the undesired channels from phys_in.
    if chsel:
        LGR.info('Dropping unselected channels')
        for i in reversed(range(0, phys_in.ch_amount)):
            if i not in chsel:
                phys_in.delete_at_index(i)

    # If requested, change channel names.
    if ch_name:
        LGR.info('Renaming channels with given names')
        phys_in.rename_channels(ch_name)

    # The next few lines create a dictionary of different BlueprintInput
    # objects, one for each unique frequency in phys_in
    uniq_freq_list = set(phys_in.freq)
    output_amount = len(uniq_freq_list)
    if output_amount > 1:
        LGR.warning(f'Found {output_amount} different frequencies in input!')

    LGR.info(f'Preparing {output_amount} output files.')
    phys_out = {}  # create phys_out dict that will have a
    # blueprint object per frequency
    # for each different frequency
    for uniq_freq in uniq_freq_list:
        # copy the phys_in object to the new dict entry
        phys_out[uniq_freq] = deepcopy(phys_in)
        # this counter will take into account how many channels are eliminated
        count = 0
        # for each channel in the original phys_in object
        # take the frequency
        for idx, i in enumerate(phys_in.freq):
            # if that frequency is different than the frequency of the phys_obj entry
            if i != uniq_freq:
                # eliminate that channel from the dict since we only want channels
                # with the same frequency
                phys_out[uniq_freq].delete_at_index(idx - count)
                # take into acount the elimination so in the next eliminated channel we
                # eliminate correctly
                count += 1
        # Also create a BlueprintOutput object for each unique frequency found.
        # Populate it with the corresponding blueprint input and replace it
        # in the dictionary.
        phys_out[uniq_freq] = BlueprintOutput.init_from_blueprint(phys_out[uniq_freq])

    if heur_file and sub:
        LGR.info(f'Preparing BIDS output using {heur_file}')
    elif heur_file and not sub:
        LGR.warning(f'While "-heur" was specified, option "-sub" was not.\n'
                    f'Skipping BIDS formatting.')

    # Preparing output parameters: name and folder.
    for uniq_freq in uniq_freq_list:
        # If possible, prepare bids renaming.
        if heur_file and sub:
            if output_amount > 1:
                # Add "recording-freq" to filename if more than one freq
                outfile = use_heuristic(heur_file, sub, ses, filename,
                                        outdir, uniq_freq)
            else:
                outfile = use_heuristic(heur_file, sub, ses, filename, outdir)

        else:
            outfile = os.path.join(outdir,
                                   os.path.splitext(os.path.basename(filename))[0])
            if output_amount > 1:
                # Append "freq" to filename if more than one freq
                outfile = f'{outfile}_{uniq_freq}'

        LGR.info(f'Exporting files for freq {uniq_freq}')
        savetxt(outfile + '.tsv.gz', phys_out[uniq_freq].timeseries,
                fmt='%.8e', delimiter='\t')
        print_json(outfile, phys_out[uniq_freq].freq,
                   phys_out[uniq_freq].start_time,
                   phys_out[uniq_freq].ch_name)
        print_summary(filename, num_timepoints_expected,
                      phys_in.num_timepoints_found, uniq_freq,
                      phys_out[uniq_freq].start_time, outfile)

def _main(argv=None):
    options = _get_parser().parse_args(argv)
    phys2bids(**vars(options))

if __name__ == '__main__':
    _main()

"""
Copyright 2019, The Phys2BIDS community.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

reason: from numpy


text:
import os
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from config import basedir

def preprocess(train, test, continuous_vars):
    for col in continuous_vars:
        # Create bins based on training set
        train[col], bins = pd.qcut(train[col], q=10, retbins=True, labels=False)
        bins = np.concatenate(([-np.inf], bins[1:-1], [np.inf]))

        # Apply bins from training set to test set
        test[col] = pd.cut(test[col], bins=bins, labels=False, include_lowest=True)
    return train, test

def one_hot_encoder(X_train, X_test, target_var):
    X_train_enc = None
    X_test_enc = None

    for col in X_train.columns:

        if col == target_var:
            if X_train_enc is not None and X_test_enc is not None:
                X_train_enc = pd.concat([X_train_enc, X_train[[target_var]]], axis=1)
                X_test_enc = pd.concat([X_test_enc, X_test[[target_var]]], axis=1)
            else:
                X_train_enc = X_train[[target_var]]
                X_test_enc = X_test[[target_var]]

        else:

            # Fit encoder to training data
            encoder = OneHotEncoder()
            enc_train = encoder.fit_transform(X_train[[col]])
            enc_train = pd.DataFrame(enc_train.toarray(), columns=encoder.categories_)
            enc_train.columns = [col + ":" + str(new_col[0]) for new_col in enc_train.columns]

            # Apply fitted encoder to test data
            enc_test = encoder.transform(X_test[[col]])
            enc_test = pd.DataFrame(enc_test.toarray(), columns=encoder.categories_)
            enc_test.columns = [col + ":" + str(new_col[0]) for new_col in enc_test.columns]

            if X_train_enc is not None and X_test_enc is not None:
                X_train_enc = pd.concat([X_train_enc, enc_train], axis=1)
                X_test_enc = pd.concat([X_test_enc, enc_test], axis=1)
            else:
                X_train_enc = enc_train
                X_test_enc = enc_test
    return X_train_enc, X_test_enc

if __name__ == "__main__":
    # Read the datasets
    df_train = pd.read_csv(os.path.join(basedir, 'data', 'temp', 'dataset_train.csv'))
    df_test = pd.read_csv(os.path.join(basedir, 'data', 'temp', 'dataset_test.csv'))

    df_train, df_test = preprocess(df_train, df_test, continuous_vars=["loan_amnt", "int_rate", "annual_inc", "dti",
                                                                       "fico_range_low", "income_to_installment"])

    print(df_train[:5].to_string())
    print(df_test[:5].to_string())

    input_vars = ["loan_amnt",
                  "term",
                  "int_rate",
                  "income_to_installment",
                  "grade",
                  "emp_length",
                  "home_ownership",
                  "annual_inc",
                  "purpose",
                  "addr_state",
                  "dti",
                  "fico_range_low",
                  "good_bad"]

    PD_train = df_train[input_vars]
    PD_test = df_test[input_vars]

    PD_train, PD_test = one_hot_encoder(PD_train, PD_test, target_var="good_bad")

    print(PD_train[:5].to_string())

    PD_train.to_csv(os.path.join(basedir, 'data', 'processed', 'PD_train_onehot.csv'), index=False, sep=";")
    PD_test.to_csv(os.path.join(basedir, 'data', 'processed', 'PD_test_onehot.csv'), index=False, sep=";")


text:
from datetime import datetime
import pickle

import numpy as np
import pytest

from data.external_data.yahoo import Yahoo
from data.local_data.data_generator import DataGenerator
from data.local_data.parameters_generator import SigmaStatesGenerator, TransitionCoefficientsGenerator

with open('tests/data/external_data/ora.pickle', 'rb') as f:
    ora: Yahoo = pickle.load(f)

@pytest.fixture
def data_generator():
    return DataGenerator(ora, 2, 3, SigmaStatesGenerator(), TransitionCoefficientsGenerator())

def test_y_train_generator(data_generator: DataGenerator):
    y_train = data_generator.generate_y_train()
    assert y_train.shape == (2, 9)

def test_x_train_generator(data_generator: DataGenerator):
    x_train = data_generator.generate_x_train()
    assert x_train.shape == (2, 34)

def test_number_of_calls(data_generator: DataGenerator):
    number_of_calls = data_generator.number_of_calls
    assert number_of_calls == 34

def test_y_train_with_data_size_equal_one():
    y_train = DataGenerator(ora, 1, 3, SigmaStatesGenerator(
    ), TransitionCoefficientsGenerator()).generate_y_train()
    assert y_train.shape == (9,)

def test_x_real(data_generator: DataGenerator):
    x_real = data_generator.generate_x_real()
    assert x_real.shape == (34, )


text:
"""

Test how to get data and plot from GAIA

"""
import matplotlib.pyplot as plt
import astropy.units as u
import astropy.coordinates as coord
from astroquery.vizier import Vizier

def gaia_query(ra_deg, dec_deg, rad_deg, maxmag=18,
               maxsources=10000):
    """
    Query Gaia DR1 @ VizieR using astroquery.vizier
    :param ra_deg: RA in degrees
    :param dec_deg: Declination in degrees
    :param rad_deg: field radius in degrees
    :param maxmag: upper limit G magnitude (optional)
    :param maxsources: maximum number of sources
    :return: astropy.table object
    """
    vquery = Vizier(columns=['Source', 'RA_ICRS', 'DE_ICRS',
                             'Gmag',"Teff","RPlx"],
                    column_filters={"Gmag":
                                    ("<%f" % maxmag)},
                    row_limit = maxsources)

    field = coord.SkyCoord(ra=ra_deg, dec=dec_deg,
                           unit=(u.deg, u.deg),
                           frame='icrs')

    return vquery.query_region(field,
                               width=("%fd" % rad_deg),
                               catalog="I/345/gaia2")[0]

def plot_gaia(ra,dec,data,ax,TMin=1000,TMax=11000):

    dmax,dmin = max(data["Gmag"]),min(data["Gmag"])

    for i in data:
        vis = 0.1+(dmax - i["Gmag"])/(dmax-dmin)*0.8
        siz = 3 +(dmax - i["Gmag"])/(dmax-dmin)*15
        """
        #vis = 0.1+(dmax - i["Gmag"])/(dmax-dmin)*0.8
        c = "w"
        if str(i["Teff"]) == "--":
            vis = 0.1
        elif float(i["Teff"]) > TMax:
            vis = 1
            c = "b"
        elif float(i["Teff"]) < TMin:
            vis = 0.1
            c = "r"
        else:
            vis = 0.1+(TMax - i["Teff"])/(TMax-TMin)*0.8
            c = "k"
        if str(i["Teff"]) != "--":
            print(float(i["Teff"]),TMax,float(i["Teff"]) > TMax)
        else:
            continue

        plt.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz+1, color="r",alpha = 1)
        plt.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz, color="w",alpha = 1)

        plt.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz, color=c,alpha = vis)
        """
        ax.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz+1, color="r",alpha = 1)
        ax.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz, color="w",alpha = 1)

        ax.plot(i["RA_ICRS"],i["DE_ICRS"],"o",markersize = siz, color="k",alpha = 1-vis)

    ax.plot(ra,dec, "*",markersize = 5,color="r",alpha=0.8)
    ax.plot(ra+0.008,dec+0.008, "*",markersize = 0.1,color="k",alpha=0.1)
    ax.plot(ra-0.008,dec-0.008, "*",markersize = 0.1,color="k",alpha=0.1)

if __name__ == "__main__":

    ra = 84.893
    dec = -25.148

    data = gaia_query(ra,dec,0.01)

    print(data)

    ax = plt.subplot()
    plot_gaia(ra,dec,data,ax,TMin=1000,TMax=11000)

    """
    ra = 7.19516
    dec = -67.86237
    """
    """
    ra = 317.63150
    dec = -27.18311

    ax = plt.subplot()

    plot_gaia(ra,dec,ax,0.1,4000.,10000.)

    plt.show()
    """

max_stars_repo_name: zhuchangzhan/TVOI

max_issues_repo_name: zhuchangzhan/TVOI

max_forks_repo_name: zhuchangzhan/TVOI

reason: import astropy