text:
# with input in the DataFrame format
# update metadata of the dataset

import os, sys
import typing
import scipy.io
import numpy as np
from sklearn import preprocessing
from collections import OrderedDict
from typing import cast, Any, Dict, List, Union, Sequence, Optional, Tuple

#from common_primitives import utils
from d3m import container
from d3m.metadata import base as metadata_base
from d3m.metadata import hyperparams
from d3m.metadata import params
from d3m.primitive_interfaces.supervised_learning import SupervisedLearnerPrimitiveBase
from d3m.primitive_interfaces import base
from d3m.primitive_interfaces.base import CallResult
from rpi_d3m_primitives.structuredClassifier.structured_Classify_model import Model
from rpi_d3m_primitives.featSelect.RelationSet import RelationSet
import rpi_d3m_primitives
import time

Inputs = container.DataFrame
Outputs = container.DataFrame

__all__ = ('NaiveBayes_PointInf',)

class Params(params.Params):
    pass

class Hyperparams(hyperparams.Hyperparams):
    pass

class NaiveBayes_PointInf(SupervisedLearnerPrimitiveBase[Inputs, Outputs, Params, Hyperparams]):
    """
    A primitive which does naive bayes classification. MAP Point Inference is applied.
    """

    metadata = metadata_base.PrimitiveMetadata({
        'id': 'e38a1b20-0262-4720-abec-9262a1fc0cf9',
        'version': '2.1.5',
        'name': 'Naive Bayes Classifier',
        'keywords': ['Naive Bayes','MAP Point Inference','Classification'],
        'description': 'This algorithm is an implementation of Naive Bayes classification with MAP Point Inference',
        'source': {
            'name': rpi_d3m_primitives.__author__,
            'contact': 'mailto:cuiz3@rpi.edu',
            'uris': [
                'https://github.com/zijun-rpi/d3m-primitives/blob/master/NaiveBayes_PointInf.py',
                'https://github.com/zijun-rpi/d3m-primitives.git'
                ]
        },
        'installation':[
            {
                'type': metadata_base.PrimitiveInstallationType.PIP,
                'package': 'rpi_d3m_primitives',
	            'version': rpi_d3m_primitives.__version__
            }
        ],
        'python_path': 'd3m.primitives.classification.naive_bayes.PointInfRPI',
        'algorithm_types': [
            metadata_base.PrimitiveAlgorithmType.NAIVE_BAYES_CLASSIFIER],
        'primitive_family': metadata_base.PrimitiveFamily.CLASSIFICATION
    })

    def __init__(self, *, hyperparams: Hyperparams, random_seed: int = 0, docker_containers: typing.Union[typing.Dict[str, base.DockerContainer]] = None) -> None:
        super().__init__(hyperparams=hyperparams, random_seed=random_seed, docker_containers=docker_containers)
        self._index = None
        self._training_inputs = None
        self._training_outputs = None
        self._fitted = False
        self._target_columns_metadata: List[Dict] = None
        self._clf = Model('nb', bayesInf=1, PointInf=1)
        self._LEoutput = preprocessing.LabelEncoder()

    def _store_target_columns_metadata(self, outputs: Outputs) -> None:
        outputs_length = outputs.metadata.query((metadata_base.ALL_ELEMENTS,))['dimension']['length']

        target_columns_metadata: List[Dict] = []

        for column_index in range(outputs_length):
            column_metadata = OrderedDict(outputs.metadata.query_column(column_index))

            # Update semantic types and prepare it for predicted targets.
            semantic_types = list(column_metadata.get('semantic_types', []))
            if 'https://metadata.datadrivendiscovery.org/types/PredictedTarget' not in semantic_types:
                semantic_types.append('https://metadata.datadrivendiscovery.org/types/PredictedTarget')
            semantic_types = [semantic_type for semantic_type in semantic_types if semantic_type != 'https://metadata.datadrivendiscovery.org/types/TrueTarget']
            column_metadata['semantic_types'] = semantic_types

            target_columns_metadata.append(column_metadata)

        self._target_columns_metadata = target_columns_metadata

    def set_training_data(self, *, inputs: Inputs, outputs: Outputs) -> None:

        # Update semantic types and prepare it for predicted targets
        self._store_target_columns_metadata(outputs)

        # set training labels
        metadata = outputs.metadata
        column_metadata = metadata.query((metadata_base.ALL_ELEMENTS, 0))
        semantic_types = column_metadata.get('semantic_types', [])
        if 'https://metadata.datadrivendiscovery.org/types/CategoricalData' in semantic_types:
            self._LEoutput.fit(outputs)
            self._training_outputs = self._LEoutput.transform(outputs)

        # convert categorical values to numerical values in training data
        metadata = inputs.metadata
        [m,n] = inputs.shape
        self._training_inputs = np.zeros((m,n))
        for column_index in metadata.get_elements((metadata_base.ALL_ELEMENTS,)):
            if column_index is metadata_base.ALL_ELEMENTS:
                continue
            column_metadata = metadata.query((metadata_base.ALL_ELEMENTS, column_index))
            semantic_types = column_metadata.get('semantic_types', [])
            if 'https://metadata.datadrivendiscovery.org/types/CategoricalData' in semantic_types:
                LE = preprocessing.LabelEncoder()
                LE = LE.fit(inputs.iloc[:,column_index])
                self._training_inputs[:,column_index] = LE.transform(inputs.iloc[:,column_index])
            elif 'http://schema.org/Text' in semantic_types:
                pass
            else:
                temp = list(inputs.iloc[:, column_index].values)
                for i in np.arange(len(temp)):
                    if bool(temp[i]):
                        self._training_inputs[i,column_index] = float(temp[i])
                    else:
                        self._training_inputs[i,column_index] = 'nan'
        self._fitted = False

    def fit(self, *, timeout: float = None, iterations: int = None) -> None:
        if self._fitted:
            return CallResult(None)

        if self._training_inputs.any() == None or self._training_outputs.any() == None:
            raise ValueError('Missing training data, or missing values exist.')

        discTrainset = RelationSet(self._training_inputs, self._training_outputs.reshape(-1, 1))
        discTrainset.impute()
        discTrainset.discretize() #starting from 1
        # if original lable value is starting from 1, same label after discretization, prediction + 1
        # if original label value is starting from 0, label += 1
        discTrainset.remove()
        X_train = discTrainset.data - 1
        Y_train = discTrainset.labels - 1
        bins = discTrainset.NUM_STATES
        stateNo = np.append(bins, len(np.unique(Y_train)))
        t = time.time()
        self._clf.fit(X_train, Y_train, stateNo)
        self._time = time.time() - t
        self._fitted = True

        return CallResult(None)

    def produce(self, *, inputs: Inputs, timeout: float = None, iterations: int = None) -> base.CallResult[Outputs]:  # inputs: m x n numpy array
        if self._fitted:
            # get discrete bins from training data
            discTrainset = RelationSet(self._training_inputs, self._training_outputs.reshape(-1, 1))
            discTrainset.impute()
            discTrainset.discretize()
            discTrainset.remove()
            bins = discTrainset.NUM_STATES

            # convert categorical values to numerical values in testing data
            metadata = inputs.metadata
            [m, n] = inputs.shape
            X_test = np.zeros((m, n))
            for column_index in metadata.get_elements((metadata_base.ALL_ELEMENTS,)):
                if column_index is metadata_base.ALL_ELEMENTS:
                    continue
                column_metadata = metadata.query((metadata_base.ALL_ELEMENTS, column_index))
                semantic_types = column_metadata.get('semantic_types', [])
                if 'https://metadata.datadrivendiscovery.org/types/CategoricalData' in semantic_types:
                    LE = preprocessing.LabelEncoder()
                    LE = LE.fit(inputs.iloc[:, column_index])
                    X_test[:, column_index] = LE.transform(inputs.iloc[:, column_index])
                elif 'http://schema.org/Text' in semantic_types:
                    pass
                else:
                    temp = list(inputs.iloc[:, column_index].values)
                    for i in np.arange(len(temp)):
                        if bool(temp[i]):
                            X_test[i, column_index] = float(temp[i])
                        else:
                            X_test[i, column_index] = 'nan'
            discTestset = RelationSet(X_test, [])
            discTestset.impute()
            X_test = discTestset.data
            index_list = np.setdiff1d(np.arange(discTrainset.num_features),np.array(discTrainset.removeIdx))
            X_test = X_test[:, index_list]
            est = preprocessing.KBinsDiscretizer(n_bins=bins,encode='ordinal',strategy='uniform')
            est.fit(X_test)
            X_test = est.transform(X_test)
            t = time.time()
            output = self._clf.predict(X_test)
            self._time = time.time() - t + self._time
            if min(self._training_outputs) == 1:
                output = output + 1
            # label decode
            output = self._LEoutput.inverse_transform(output)

            # update metadata
            output = container.DataFrame(output, generate_metadata=False, source=self)
            output.metadata = inputs.metadata.clear(source=self, for_value=output, generate_metadata=True)

            for column_index, column_metadata in enumerate(self._target_columns_metadata):
                output.metadata = output.metadata.update_column(column_index, column_metadata, source=self)

            return CallResult(output)
        else:
            raise ValueError('Model should be fitted first.')

    def get_params(self) -> None:
        pass

    def set_params(self) -> None:
        pass

reason: import numpy,import scipy


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

import csv
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

sns.set()

best_all = []
mean_all = []
worst_all = []
std_all = []
for i in range(1, 11):
    with open('log-J-1000-2000-{0:02d}'.format(i), 'r') as f:
        best = []
        mean = []
        worst = []
        std = []
        reader = csv.reader(f)
        next(reader, None)
        for row in reader:
            if row[0][0] == '$':
                continue
            best.append(float(row[1]))
            mean.append(float(row[2]))
            worst.append(float(row[3]))
            std.append(float(row[4]))
        best_all.append(best)
        mean_all.append(mean)
        worst_all.append(worst)
        std_all.append(std)

y = []
y1 = []
y2 = []
for i in range(500):
    best_generation = [best_all[t][i] for t in range(len(best_all))]
    m = np.mean(best_generation)
    s = np.std(best_generation)
    y.append(m)
    y1.append(m-s)
    y2.append(m+s)

plt.plot(np.arange(len(y)), y, label="best score")
plt.fill_between(np.arange(len(y)), y1, y2, alpha=0.5)

y = []
y1 = []
y2 = []
for i in range(500):
    mean_generation = [mean_all[t][i] for t in range(len(best_all))]
    m = np.mean(mean_generation)
    s = np.std(mean_generation)
    y.append(m)
    y1.append(m-s)
    y2.append(m+s)

plt.plot(np.arange(len(y)), y, label="mean score")
plt.fill_between(np.arange(len(y)), y1, y2, alpha=0.5)

plt.xlabel("generation")
plt.ylabel("J score")
plt.legend(loc="lower right")

plt.savefig("J-score-generation.pdf")

ext: py

max_stars_repo_name: Birdlet/ChemGE

max_issues_repo_name: Birdlet/ChemGE

max_forks_repo_name: Birdlet/ChemGE

reason: import numpy


text:
from __future__ import division, unicode_literals, print_function, absolute_import
from labscript_utils import PY2
if PY2:
    str = unicode
import sys
import json
from base64 import b64encode, b64decode
if PY2:
    from collections import Iterable, Mapping
else:
    from collections.abc import Iterable, Mapping
import numpy as np
import h5py

vlenstring = h5py.special_dtype(vlen=str)

JSON_IDENTIFIER = 'Content-Type: application/json '
BASE64_IDENTIFIER = 'Content-Transfer-Encoding: base64 '

VALID_PROPERTY_LOCATIONS = {
    "connection_table_properties",
    "device_properties",
    "unit_conversion_parameters"
    }

if PY2:
    str = unicode

def _check_dicts(o):
    if isinstance(o, Mapping):
        if not all(isinstance(k, (str, bytes)) for k in o.keys()):
            raise TypeError("Cannot JSON encode dictionary with non-string keys")
        for item in o.values():
            _check_dicts(item)
    elif isinstance(o, Iterable) and not isinstance(o, (str, bytes)):
        for item in o:
            _check_dicts(item)

def _encode_bytestrings(o):
    """Encode all bytestring values (not keys) to base64 with a prefix"""
    if isinstance(o, Mapping):
        return {key: _encode_bytestrings(value) for key, value in o.items()}
    elif isinstance(o, Iterable) and not isinstance(o, (str, bytes)):
        return list([_encode_bytestrings(value) for value in o])
    elif isinstance(o, bytes):
        return BASE64_IDENTIFIER + str(b64encode(o).decode())
    else:
        return o

def _decode_bytestrings(o):
    """Decode all base64-encoded values (not keys) to bytestrings"""
    if isinstance(o, Mapping):
        return {key: _decode_bytestrings(value) for key, value in o.items()}
    elif isinstance(o, Iterable) and not isinstance(o, (str, bytes)):
        return list([_decode_bytestrings(value) for value in o])
    elif isinstance(o, str) and o.startswith(BASE64_IDENTIFIER):
        return b64decode(o[len(BASE64_IDENTIFIER):])
    else:
        return o

def is_json(value):
    if isinstance(value, bytes):
        return value[:len(JSON_IDENTIFIER)] == JSON_IDENTIFIER.encode('utf8')
    elif isinstance(value, str):
        return value.startswith(JSON_IDENTIFIER)
    return False

def serialise(value):
    _check_dicts(value)
    if not PY2:
        value = _encode_bytestrings(value)
    json_string = json.dumps(value)
    return JSON_IDENTIFIER + json_string

def deserialise(value):
    assert is_json(value)
    return _decode_bytestrings(json.loads(value[len(JSON_IDENTIFIER):]))

def set_device_properties(h5_file, device_name, properties):
    gp = h5_file['devices/' + device_name]
    for key, val in properties.items():
        try:
            # Workaround for h5py not supporting None but not raising a TypeError:
            if val is None:
                raise TypeError('has no native HDF5 equivalent')
            gp.attrs[key] = val
        except TypeError as e:
            # If type not supported by HDF5, store as JSON
            if 'has no native HDF5 equivalent' in str(e):
                json_string = serialise(val)
                gp.attrs[key] = json_string
            else:
                raise

def _get_device_properties(h5_file, device_name):
    gp = h5_file['devices/' + device_name]
    properties = {}
    for key, val in gp.attrs.items():
        # Deserialize values if stored as JSON
        if is_json(val):
            properties[key] = deserialise(val)
        else:
            properties[key] = val
    return properties

def _get_con_table_properties(h5_file, device_name):
    dataset = h5_file['connection table']

    # Compare with the name in the connection table
    # whether it is np.bytes_ or vlenstr:
    namecol_dtype = dataset['name'].dtype
    if namecol_dtype.type is np.bytes_:
        device_name = device_name.encode('utf8')
    elif namecol_dtype is vlenstring:
        pass
    else:
        raise TypeError(namecol_dtype)

    row = dataset[dataset['name'] == device_name][0]
    json_string = row['properties']
    return deserialise(json_string)

def _get_unit_conversion_parameters(h5_file, device_name):
    dataset = h5_file['connection table']

    # Compare with the name in the connection table
    # whether it is np.bytes_ or vlenstr:
    namecol_dtype = dataset['name'].dtype
    if namecol_dtype.type is np.bytes_:
        device_name = device_name.encode('utf8')
    elif namecol_dtype is vlenstring:
        pass
    else:
        raise TypeError(namecol_dtype)

    row = dataset[dataset['name'] == device_name][0]
    json_string = row['unit conversion params']
    return deserialise(json_string)

def get(h5_file, device_name, location):
    if location == 'device_properties':
        return _get_device_properties(h5_file, device_name)
    elif location == 'connection_table_properties':
        return _get_con_table_properties(h5_file, device_name)
    elif location == 'unit_conversion_parameters':
        return _get_unit_conversion_parameters(h5_file, device_name)
    else:
        raise ValueError('location must be one of %s'%str(VALID_PROPERTY_LOCATIONS))


text:
# Copyright 2022 san kim
#
# 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 argparse

import os
import time
import json
import shutil
import logging
import functools

import numpy as np
import tqdm

import torch
from torch.utils.data import DataLoader
from torch.nn import CrossEntropyLoss
import torch.nn.functional as F
from torch import optim

import torch.distributed as dist
import torch.utils.data
import torch.utils.data.distributed
import torch.nn.parallel
from torch.nn.parallel import DistributedDataParallel as DDP

from transformers import AutoTokenizer, ViTFeatureExtractor
from torch.utils.tensorboard import SummaryWriter

from data_utils import DatasetForImages
from modeling_encoder import (
    VisionT5SimpleBiEncoder,
    VisionT5MeanBiEncoder,
    VisionT5SimpleBiEncoderHN,
    VisionT5MeanBiEncoderHN,
)

from training_retriever import (
    create_directory_info,
    get_env_var,
    MODEL_CLS,
)

from index_scorer import (
    write_fvecs,
    write_fvecs_append
)

logger = logging.getLogger(__name__)

def main():
    parser = argparse.ArgumentParser()

    # data
    parser.add_argument("--data_path",
                        default="cc12m_filtered.tsv", type=str)
    parser.add_argument("--fvecs_dir",
                        default=None, type=str)
    parser.add_argument("--image_root_dir",
                        default=None, type=str)

    # model
    parser.add_argument("--vision_model",
                        default="google/vit-base-patch16-384", type=str)
    parser.add_argument("--language_model",
                        default="KETI-AIR/ke-t5-base", type=str)

    parser.add_argument("--model_cls", default="VisionT5MeanBiEncoder",
                        choices=["VisionT5SimpleBiEncoder",
                                "VisionT5MeanBiEncoder",
                                "VisionT5SimpleBiEncoderHN",
                                "VisionT5MeanBiEncoderHN",
                                ],
                        type=str, help="model class")
    parser.add_argument("--dir_suffix",
                        default=None, type=str)
    parser.add_argument("--output_dir",
                        default="output", type=str)

    # resume
    parser.add_argument("--hf_path", default=None, type=str,
                        help="path to score huggingface model")

    # default settings for training, evaluation
    parser.add_argument("--batch_size", default=16,
                        type=int, help="mini batch size")
    parser.add_argument("--num_workers", default=0, type=int,
                        help="number of workers")

    # distributed setting
    parser.add_argument("--local_rank", type=int, default=-1,
                        help="local_rank for distributed training on gpus")
    parser.add_argument("--local_world_size", type=int, default=1,
                        help="The size of the local worker group.")
    parser.add_argument("--rank", type=int, default=0,
                        help="The rank of the worker within a worker group.")
    parser.add_argument("--world_size", type=int, default=1,
                        help="world size. (num_nodes*num_dev_per_node)")
    parser.add_argument("--distributed", action='store_true',
                        help="is distributed training")

    parser.add_argument("--batch_write", action='store_true',
                        help="write fvecs for each iteration")

    args = parser.parse_args()

    args.local_rank = get_env_var('LOCAL_RANK', int, args.local_rank)
    args.local_world_size = get_env_var('LOCAL_WORLD_SIZE', int, args.local_world_size)
    args.rank = get_env_var('RANK', int, args.rank)
    args.world_size = get_env_var('WORLD_SIZE', int, args.world_size)

    # check world size
    args.distributed = False
    if 'WORLD_SIZE' in os.environ:
        args.distributed = int(os.environ['WORLD_SIZE']) > 1
        args.local_rank = int(os.environ['LOCAL_RANK'])

    path_info = create_directory_info(args, create_dir=False)

    # if the world size is bigger than 1, init process group(sync)
    if args.distributed:
        torch.cuda.set_device(args.local_rank)
        torch.distributed.init_process_group(backend='nccl',
                                             init_method='env://')
        args.world_size = torch.distributed.get_world_size()

    if args.fvecs_dir is None:
        args.fvecs_dir = os.path.join(path_info["model_dir"], "fvecs")
    if args.distributed or args.local_rank == 0:
        if not os.path.isdir(args.fvecs_dir):
            os.makedirs(args.fvecs_dir, exist_ok=True)

    if args.hf_path.lower()=='default':
        args.hf_path = os.path.join(path_info["model_dir"], "hf")

    # device
    device = torch.device('cuda')

    # get model class
    model_cls_cfg = MODEL_CLS[args.model_cls]
    model_cls = model_cls_cfg["model_cls"]

    # create model
    model = model_cls.from_pretrained(args.hf_path)
    logger.info("weights were loaded from pre_trained: {}".format(args.hf_path))
    model = model.cuda()

    # get tokenizer
    image_tokenizer = ViTFeatureExtractor.from_pretrained(args.vision_model)

    logger.info("loading dataset: {}".format(args.data_path))
    # create dataset
    dataset = DatasetForImages(
        args.data_path,
        image_tokenizer=image_tokenizer,
        shard_idx=args.rank,
        num_shards=args.world_size,
        image_root_dir=args.image_root_dir)

    collate_fn = dataset.get_collate_fn()

    # create data loader
    data_loader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            shuffle=False,
                            num_workers=args.num_workers,
                            sampler=None,
                            collate_fn=collate_fn)

    # wrap model using DDP
    model_ptr = model
    if args.distributed:
        model = DDP(model,
                    device_ids=[args.local_rank],
                    output_device=args.local_rank)
        model_ptr = model.module

    # run

    target_path = os.path.join(args.fvecs_dir, "{:03d}.fvecs".format(args.rank))
    if os.path.isfile(target_path):
        os.remove(target_path)

    np_container = []
    with torch.no_grad():
        for example in tqdm.tqdm(data_loader, desc="conv..."):
            # fvecs: [bsz, model_dim]
            fvecs = model_ptr.encode_image({
                "pixel_values":example["pixel_values"].to(device)})
            fvecs_np = fvecs.cpu().numpy()

            if args.batch_write:
                write_fvecs_append(target_path, fvecs_np)
            else:
                np_container.append(fvecs_np)

        if not args.batch_write:
            np_arr = np.concatenate(tuple(np_container), axis=0)
            print("np array({}) to file {}".format(np_arr.shape, target_path))
            write_fvecs(target_path, np_arr)

if __name__ == "__main__":
    logging.basicConfig(level = logging.INFO)
    main()

# CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7" python -m torch.distributed.launch --nproc_per_node=8 create_index4retriever.py \
# --hf_path default \
# --dir_suffix od1

# CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7" python -m torch.distributed.launch --nproc_per_node=8 create_index4retriever.py \
# --data_path ../downloaded_data/cc12m/cc12m_filtered_new.tsv \
# --image_root_dir ../downloaded_data/cc12m/images_384 \
# --fvecs_dir fvecs_cc12m_freeze_lm \
# --hf_path default \
# --dir_suffix freeze_lm \
# --batch_size 64

# CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7" python -m torch.distributed.launch --nproc_per_node=8 create_index4retriever.py \
# --data_path ../downloaded_data/cc12m/cc12m_filtered_new.tsv \
# --image_root_dir ../downloaded_data/cc12m/images_384 \
# --fvecs_dir fvecs_cc12m_hn \
# --hf_path output/VisionT5MeanBiEncoderHN-google_vit-base-patch16-384-KETI-AIR_ke-t5-base/hf \
# --batch_size 64


text:
from main import run_main
import numpy as np

#datasets = ['DREAM5_ecoli_expression_data']
datasets = ['gasch_GSE102475','gasch_GSE102475']
#ground_truths = ['DREAM5_yeast_ground_truth']
ground_truths = ['yeast_chipunion_KDUnion_intersect','yeast_chipunion_KDUnion_intersect']
epochs = [5,5]
leap_path = 'logs/leap/MAC_symmetric_gasch_example.csv'
#leap_path = 'logs/leap/MAC_symmetric_dream5_ecoli.csv'
runs = range(2)

granpy, pearson, random, leap = ([] for i in range(4))
for dataset, ground_truth, epoch in zip(datasets, ground_truths, epochs):
    for run in runs:
        granpy_scores, pearson_scores, random_scores, leap_scores = run_main(dataset, ground_truth, epoch, leap_path)
        granpy.append(granpy_scores)
        pearson.append(pearson_scores)
        random.append(random_scores)
        leap.append(leap_scores)

scores = [granpy, pearson, random, leap]
for score in scores:
    print(score)
    acc, ap, roc, f1 = zip(*score)
    for idx, dataset in enumerate(datasets):
        start = idx*(len(score)/len(datasets))
        end = (idx+1)*(len(score)/len(datasets))
        print(start)
        print(end)
        print(acc)
        print(acc[start:end])
        print('mean acc ' + str(np.mean(acc[start:end])) + ' with a sd of ' + str(np.std(acc[start:end])))
        print('mean ap ' + str(np.mean(ap[start:end])) + ' with a sd of ' + str(np.std(ap[start:end])))
        print('mean roc ' + str(np.mean(roc[start:end])) + ' with a sd of ' + str(np.std(roc[start:end])))
        print('mean f1 ' + str(np.mean(f1[start:end])) + ' with sd a of ' + str(np.std(f1[start:end])))

if len(granpy) != 0:
    print(pearson)
    print(granpy)
    #acc, ap, roc = zip(*pearson)
    #print('unzipped pearson acc for 2 runs')
    #print(acc)
    #print('average acc of the 2 runs')
    #print(np.mean(acc))
    #print(ap)
    #print(np.mean(granpy_scores))

max_stars_repo_name: reiamt/GRANpy

max_issues_repo_name: reiamt/GRANpy

max_forks_repo_name: reiamt/GRANpy


text:
###############
# @file ldac.py
# @author Douglas Applegate & Thomas Erben
# @date 27/11/2013
#
# @brief Utilities to make accessing LDAC cats easier within Python
###############
# HISTORY INFORMATION:
# ====================
#
# 01.09.2010:
# I included treatment of slices through vectorkeys in
# LDACCat.__getitem__
#
# 09.09.2010:
# I made the module more robust against the non-existence
# of necessary libraries
#
# 28.09.2010:
# Bug fix: The from ... import statement must appear on the
# top of the file.
#
# 20.11.2013:
# significant extensions to the code; first proper implementations
# of LDAC tables and LDAC catalogue objects.
#
# 25.11.2013:
# - In the LDACCat class the header of the original catalogue is
#   preserved when storing the catalogue to a file.
# - I add a method to add HISTORY keywords to the image header of
#   the catalogue
#
# 27.11.2013:
# In the LDACTable I replaced the test for an empty LDAC table. Now
# it is done by testing the data element for 'None'. Before it was
# done by the 'size' method on the HDU. It seems that this size element
# of HDU changed from a method to a simple 'int' in different pyfits
# versions. Hence it is useless for code that should be compatible
# to different versions of pyfits.
#
# 28.02.2014:
# In the LDACTable 'setitem' method we do no longer create a new table
# immediatly after a new column was added. This new table creation
# takes long for big catalogues and it leads to necessary operations
# if we add many columns without doing any operations in between. We
# therefore only update column definitions and raise a flag that this
# table needs an 'update'. The update then is done whenever necessary.
#
# 28.03.2014:
# - I corrected function '__setitem__' in LDACCat. The previous
#   implementation was untested and did not work at all!
# - I transfered Dominiks implementation of of the 'add' function in the
#   LDACTable. It enables concatenation of two tables with the same
#   keys and key types.
#
# 24.07.2015:
# - The pyfits module was replaced by the equivalent 'astrpy.io.fits'.
#   pyfits will not be supported anymore at some point in the future.
# - We substituted the depreceated astropy function 'new_table' with
#   the new BinTableHDU.from_columns. The former will no longer
#   be supported in a future version of astropy.
# - We substituted constructs such as 'self.hdu.data == None' to
#   'self.hdu.data is None' to avoid a 'FutureWarning: comparison to
#   `None` will result in an elementwise object comparison in the future.'
#   warning meesage. Although the expected future change of the '=='
#   operator would have no effect in the special cases the warning message
#   is confusing!
# - We do no longer provide detailed error messages if the numpy or astropy
#   (former pyfits) modules are missing. They are quite standard and
#   sufficiently known by now.
#
# 16.08.2015:
# - Bug fix: The LDACcat saveas function did not check whether tables
#   in the catalogue not to be updated before writing the whole catalogue
#   to a file - fixed!
# - In the LDACTable class I had to change the name of the private
#   function '__update()' to '_update()' to be able to use it within
#   the LDACCat class.

"""
Wrapper module to work with LDAC catalogues and tables
"""

from __future__ import with_statement

# standard-library includes:
import sys
import astropy.io.fits as aif
import numpy

class LDACCat(object):
    """
    Class to represent an LDAC catalogue
    """

    def __init__(self, cat=None):
        """
        An LDAC catalogue can be instantiated either as an empty catalogue
        or with an existing catalogue on disk.

        >>> a = ldac.LDACCat('mag.cat') # reads the catalogue 'mag.cat' into
                                        # the variable 'a'.
        """

        # The LDACCcat object contains a list of LDAC tables.  We
        # internally also keep the header of the PrimaryHDU. It is
        # reused when the catalogue is saved to a file.

        # for an empty catalogue this list is empty:
        self.ldactables = []
        self.header = None

        if cat != None:
            # read tables from a catalogue on disk:
            if type(cat) == type("a"):
                hdulist = aif.open(cat)

                for hdu in hdulist:
                    if isinstance(hdu, aif.PrimaryHDU) == True:
                        self.header = hdu.header
                    if isinstance(hdu, aif.BinTableHDU) == True:
                        self.ldactables.append(LDACTable(hdu))

    def __len__(self):
        """
        return the number of LDAC tables in this catalogue

        >>> b = len(a)  # number of LDAC tables in catalogue 'a'.
        """

        return len(self.ldactables)

    def __getitem__(self, tablename):
        """
        returns the named LDAC table. Returns 'None' if the table does
        not exist.

        Example:
        >>> b = a['OBJECTS'] # returns in 'b' the LDAC table with name
                             # 'OBJECTS' from catalogue 'a'
        """

        result = None
        for table in self.ldactables:
            if table.hdu.name == tablename:
                result = table

        return result

    def __setitem__(self, name, table):
        """
        adds or replaces an LDAC table in this catalogue

        >>> a['NEW_TABLE'] = b['OBJECTS'] # adds the new table 'NEW_TABLE' in
                                          # 'a' from table 'OBJECTS' in 'b'.
        """

        if isinstance(table, LDACTable):
            # check whether a table with name exists already:
            exists = False

            for i in xrange(len(self.ldactables)):
                if self.ldactables[i].hdu.name == name:
                    self.ldactables[i] = table
                    exists = True

            if exists == False:
                table.setname(name)
                self.ldactables.append(table)

    def tables(self):
        """
        returns the names of the contained LDAC tables

        >>> c = a.tables()  # gives a list of table names in catalogue 'a'
        """

        tablenames = []

        for table in self.ldactables:
            tablenames.append(table.hdu.name)

        return tablenames

    def __iter__(self):
        return self.ldactables.__iter__()

    def __contains__(self, tablename):
        """
        check whether a table with name 'tablename' is present
        in this catalogue
        """

        return tablename in self.tables()

    def __add__(a, b):
        """
        Appends table b to table a and returns a new LDAC table.
        Tables 'a' and 'b' must be identical (keys and key types).

        >>> c = a + b   # appends table b to a and saves it
                        # as a LDAC table again
        """

        # First check if both tables have the same number of
        # columns:
        if len(a.keys()) != len(b.keys()):
            print( "Tables do not have the same number of columns / keywords!" )
            print( "First table has " + str(len(a.keys())) + \
                " colums / keywords." )
            print( "Second table has " + str(len(b.keys())) + \
                " colums / keywords." )
            return None

        # Now let's check if all kewords from the first table are also
        # present in the second table and also at the same place!
        for i in range(len(a.keys())):
            if b.has_key(a.keys()[i]) == False:
                print( "Key " + str(a.keys()[i]) + \
                    " is not present in the second table!" )
                return None

        arows = a.hdu.data.shape[0]
        brows = b.hdu.data.shape[0]
        nrows = arows + brows
        hdu = aif.BinTableHDU.from_columns(a.hdu.columns, nrows=nrows)

        for i in a.keys():
            hdu.data.field(i)[:arows] = a.hdu.data.field(i)
            hdu.data.field(i)[arows:] = b.hdu.data.field(i)

        hdu.header = a.hdu.header
        hdu.header.update('NAXIS2', nrows)
        hdu.columns = a.hdu.columns
        hdu.name = a.hdu.name

        return LDACTable(hdu)

    def has_table(self, tablename):
        """
        check whether a table with name 'tablename' is present
        in this catalogue

        >>> c = a.has_table('OBJECTS') # returns 'True' if a table named
                                       # 'OBJECTS' is in catalogue 'a'
        """

        return self.__contains__(tablename)

    def add_history(self, keyvalue):
        """
        add a history keyword to the header of the catalogue

        >>> a.add_history('Catalogue created on 01/02/2013')
        """

        # create an empty header if necessary
        if self.header is None:
            self.header = aif.Header()

        # just delegate the work to an astropy method:
        self.header.add_history('') # empty line for separation from other
                                    # comment/history lines
        self.header.add_history(keyvalue)

    def saveas(self, file, clobber=False):
        """
        save the LDAC catalogue to a file.

        if clobber=True an existing file is overwritten.

        >>> a.saveas('test.cat') # saves LDAC catalogue 'a' with all its
                                 # tables to file 'test.cat'
        """

        primaryHDU = aif.PrimaryHDU(header=self.header)
        hdulist = aif.HDUList([primaryHDU])

        for table in self.ldactables:
            if table.update == 1:
                table._update()

            hdulist.append(table.hdu)

        hdulist.writeto(file, clobber=clobber)

class LDACTable(object):
    """
    Class to represent an LDAC table
    """

    def __init__(self, hdu=None):
        """
        An LDAC table can be instantiated either as am empty table
        or with an astropy BinaryTable HDU (existing table).
        """

        if hdu is None:
            self.hdu = aif.BinTableHDU()
            self.hdu.data = None

            # We make sure that the table has 'some' proper name:
            self.hdu.name = "DEFAULT"
        else:
            self.hdu = hdu

        self.update = 0 # does the table need an update (e.g. when
                        # new columns were added?

    def __len__(self):
        """
        return the number of table entries (objects)
        """

        if self.update == 1:
            self._update()

        # 'self.hdu.data' leads to an exception for an empty catalogue.
        # Hence we check for this first:
        if self.hdu.size() == 0:
            return 0
        else:
            return len(self.hdu.data)

    def __getitem__(self, key):
        """
        returns the contents of an existing LDAC key as numpy array

        Example:
        >>> b = a['Xpos'] # store in 'b' the contents (numpy array)
                          # of key 'Xpos' from table 'a'.
        """

        if self.update == 1:
            self._update()

        if type(key) == type(5) or \
                type(key) == type(slice(5)):
            return self.hdu.data[key]

        if type(key) == type("a"):
            # we need to deal with slices through vector keys
            # such as 'MAG_APER(2)'
            startind = key.find("(")
            endind = key.find(")")

            if startind > 0 and endind > 0:
                keyname = key[:startind]
                keyindex = int(key[startind + 1:endind]) - 1

                try:
                   return self.hdu.data.field(keyname)[:,keyindex]
                except AttributeError:
                   raise KeyError(key)
            else:
                try:
                    return self.hdu.data.field(key)
                except AttributeError:
                    raise KeyError(key)

        raise TypeError

    def __setitem__(self, key, val):
        """
        set values of an LDAC table

        a['Xpos'] = b # sets the key 'Xpos' in the table 'a' to the
                      # values in numpy array 'b'. If the key does
                      # not yet exist it is created.
        """
        # VERY uncomplete implementation for the moment!
        # - we only treat scalars for the moment!
        # - we do not check whether the key types match
        #   when an existing key is overwritten

        # sanity checks: the column name must be a string and
        # the value arrays length must match the table data
        # dimension:
        if type(key) == type("a"):
            # The first condition applies to an empty table:
            if self.hdu.data is None or len(val) == self.hdu.data.size:
                # If necessary add a new column to the table
                if self.__contains__(key) == True:
                    # quite some things might go wrong here
                    # (same data type, etc.)

                    # The following construct of '....(key)[:]' ensures
                    # a 'deep' copy of array element which we need here:
                    self.hdu.data.field(key)[:] = val
                else:
                    # determine format for the new column:
                    colformat=""
                    if numpy.issubdtype(val.dtype, float) == True:
                        colformat="1E"

                    if numpy.issubdtype(val.dtype, int) == True:
                        colformat="1I"

                    # now create the new column and create a 'new' table
                    # with the old plus the new column (I did not find a
                    # way to just append a new column to an existing
                    # table!):
                    newcolumn = aif.Column(name=key, format=colformat,
                                              array=val)
                    self.hdu.columns = self.hdu.columns + \
                        aif.ColDefs([newcolumn])

                    self.update = 1

        #raise NotImplementedError

    def __delitem__(self, key):
        raise NotImplementedError

    def _update(self):
        # update the table if necessary:
        newtabhdu = aif.BinTableHDU.from_columns(self.hdu.columns)
        newtabhdu.name = self.hdu.name
        self.hdu = newtabhdu
        self.update = 0

    def keys(self):
        """
        returns the names of the keys contained in this table

        >>> b = a.keys() # store a list of keynames of table 'a' in
                         # 'b'.
        """

        if self.update == 1:
            self._update()

        return self.hdu.columns.names

    def __iter__(self):
        if self.update == 1:
            self._update()

        return self.hdu.data.__iter__()

    def __contains__(self, item):
        if self.update == 1:
            self._update()

        return item in self.keys()

    def has_key(self, key):
        """
        tests whether the table contains a certain key.

        >>> b = a.haskey('Xpos') # returns 'True' if table 'a' contains
                                 # a key with name 'Xpos'.
        """

        return self.__contains__(key)

    def filter(self, mask):
        if self.update == 1:
            self._update()

        return LDACTable(aif.BinTableHDU(data=self.hdu.data[mask],
                                          header=self.hdu.header))

    def setname(self, name):
        """
        set/change the name of the LDAC table.

        >>> a.name = 'TESTTABLE' # set/change the name of the LDAC table
                                 # in 'a' to 'TESTTABLE'.
        """

        self.hdu.name = name

    def saveas(self, file, clobber=False):
        """
        save the LDAC table as a catalogue. The produced
        catalogue will only consist of this table!

        clobber=True overwrites an existing file with the
        new catalogue

        >>> a.saveas('table.cat') # saves the LDAC table in 'a'
                                  # to file 'table.cat'
        """

        if self.update == 1:
            self._update()

        self.hdu.writeto(file, clobber=clobber)

def openObjects(hdulist, table='OBJECTS'):
    tablehdu = None
    for hdu in hdulist:
        # In a regular LDAC catalogue the primary header
        # does not have an EXTNAME keyword and 'hdu.header['EXTNAME']'
        # leads to a KeyError exception which we just ignore:
        try:
            if table == hdu.header['EXTNAME']:
                tablehdu = hdu
        except KeyError:
            pass

    if tablehdu is None:
        print( "Table %s not present in catalogue %s" % (table,
                                                        hdulist.filename()) )
        print( "Creating an empty LDAC table" )

    return LDACTable(tablehdu)

def openObjectFile(filename, table='OBJECTS'):
    hdulist = aif.open(filename)
    if hdulist is None:
        return None

    return openObjects(hdulist, table)

reason: import numpy,import astropy


text:
import pandas as pd
import numpy as np
import seaborn as sns

def series_to_list(data,columns=None):
    if columns == None:
        columns = data.columns

    return list(data[columns].values)

def find_outliers_df(data,method="std"):
    if method == 'std':
        outliers = dict()
        for column in data.columns:
            outliers[column] = data[data[column].std > 3]
    return outliers

def find_outliers_series(series):
    outliers = series[series>(series.std()*3)]

    return outliers


text:
#!/usr/bin/env python3
import ROOT
import os, sys
import copy
import yaml
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axisartist.axislines import SubplotZero
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

def getBinsAndErrors(h_data):
  bins = []
  errors = [[], []]
  for aBin in range(1,h_data.GetNbinsX()+1):
    bins.append(h_data.GetBinContent(aBin))

    if h_data.GetBinContent(aBin)+ h_data.GetBinError(aBin)>1:
      errors[1].append(1-h_data.GetBinContent(aBin))
      errors[0].append(h_data.GetBinError(aBin))
    else:
      errors[1].append(h_data.GetBinError(aBin))
      errors[0].append(h_data.GetBinError(aBin))
  return bins, errors

def getWidth(th1):
  NbinsX = th1.GetNbinsX()
  width=[]
  for aBin in range(1,NbinsX+1):
    width.append(th1.GetBinWidth(aBin)/2)
  return width

def getPos(th1):
  NbinsX = th1.GetNbinsX()
  pos = []
  for aBin in range(1,NbinsX+1):
    pos.append(th1.GetBinCenter(aBin))
  return pos

def getMargin(th1):
  NbinsX = th1.GetNbinsX()
  margin = []
  for aBin in range(1,NbinsX+2):
    margin.append(th1.GetBinLowEdge(aBin))
    if aBin==NbinsX+2:
      margin.append(th1.GetBinLowEdge(aBin)+th1.GetBinWidth(aBin))
  return margin

def saveAllHistos():
  histos = {}
  for i in range(0,f1.GetListOfKeys().Capacity()):
    histoNames.append(f1.GetListOfKeys().At(i).GetName())

def main():
  plotDirectory = os.getcwd()+"/outputs/"+label+"/"+year+"/plots"
  for aPdf in pdfs:
    subDirectory = plotDirectory + "/" + aPdf
    f_data = ROOT.TFile(subDirectory+"/eff_data.root")
    f_mc   = ROOT.TFile(subDirectory+"/eff_DY.root")
    histos = {}
    for i in range(0,f_data.GetListOfKeys().Capacity()):
      histos[f_data.GetListOfKeys().At(i).GetName()] = copy.deepcopy(f_data.Get(f_data.GetListOfKeys().At(i).GetName()))
    for i in range(0,f_mc.GetListOfKeys().Capacity()):
      histos[f_mc.GetListOfKeys().At(i).GetName()] = copy.deepcopy(f_mc.Get(f_mc.GetListOfKeys().At(i).GetName()))
    histos_class = {}
    for i in range(0,f_data.GetListOfKeys().Capacity()):
      histos_class[f_data.GetListOfKeys().At(i).GetName()[5:]] = f_data.GetListOfKeys().At(i).GetClassName()
    print(histos_class)

    for aName in histos_class:
      if not histos_class[aName] == "TH1F":
        continue
      fig, ax = plt.subplots(2,1)
      data_bins, data_errors = getBinsAndErrors(histos["data_"+aName])
      mc_bins  , mc_errors   = getBinsAndErrors(histos["DY_" +aName])
      ax[0].errorbar(getPos(histos["data_"+aName]),
        data_bins,
        fmt='',
        #color = colorDict[aRun],
        ms=3,
        marker='o' ,
        ls='none',
        xerr=getWidth(histos["data_"+aName]),
        yerr=data_errors,
        label= "Data")
      ax[0].errorbar(getPos(histos["DY_"+aName]),
        mc_bins,
        fmt='',
        #color = colorDict[aRun],
        ms=3,
        marker='o' ,
        ls='none',
        xerr=getWidth(histos["DY_"+aName]),
        yerr=mc_errors,
        label= "MC")
      ax[0].set_xticks(getMargin(histos["data_"+aName]))
      ax[1].set_xticks(getMargin(histos["data_"+aName]))
      for i in [0,1]:
        if 'pt' in aName:
          ax[i].xaxis.set_major_formatter(FormatStrFormatter('%5.0f'))
        elif 'eta' in aName:
          ax[i].xaxis.set_major_formatter(FormatStrFormatter('%5.1f'))
        else:
            ax[i].xaxis.set_major_formatter(FormatStrFormatter('%5.0f'))
      ax[0].set_xlim(getMargin(histos["data_"+aName])[0],getMargin(histos["data_"+aName])[-1] )
      ax[0].set_ylim(min(min(min(data_bins),min(mc_bins)),0.8), 1.1)
      ax[0].set_title(aName.split("_")[-1])
      ax[0].set_ylabel('efficiency')
      ratios = []
      for i in range(0, len(data_bins)):
        ratios.append(data_bins[i]/mc_bins[i])
      ax[1].errorbar(getPos(histos["data_"+aName]), ratios, fmt='', ms=3, marker='o' , ls='none', xerr=getWidth(histos["data_"+aName]))
      ax[1].set_ylim(min(min(ratios),0.95),max(max(ratios),1.05))
      ax[1].set_xlim(getMargin(histos["data_"+aName])[0],getMargin(histos["data_"+aName])[-1] )
      ax[1].set_ylabel('Data/MC')
      ax[1].set_xlabel(aName.split("_")[1])
      ax[0].grid(True)
      ax[1].grid(True)
      ax[0].legend(loc='lower right',frameon=False)
      plt.savefig(subDirectory+"/eff1D_"+aName+'.png')
      plt.cla()
      plt.close()
    del f_data
    del f_mc

if __name__ == '__main__':
  files = os.listdir(os.getcwd()+"/config")
  #labels = []
  for aFile in files:
    f = open("config/"+aFile)
    config = yaml.load(f)
    yaml.dump(config)
    label = str(config['label'])
    year = str(config['year'])
    samples = config['sample']
    if not 'data' in samples:
      samples['data'] = ''
    pdfs = config['pdfs']
    obs = config['observable']
    obs2D = config['observable2D']
    flags = config['flags']
    main()


text:
import torch
from torch.autograd import Variable
from torch.autograd import Function
from torchvision import models
import torchvision.transforms as transforms
from torchvision import utils
import torch.utils.data as Data
import cv2
import sys
import numpy as np
import argparse
import os
sys.path.append('%s/../../' % os.path.dirname(os.path.realpath(__file__)))
import PIL.Image as Image
import importlib

class NeuronVisualizer:
    def __init__(self, model, model_name, layer_list, img_size):
        self.model = model
        self.model_name = model_name
        self.module_name = []
        self.features_out_hook = []
        self.hook_handle_list = []
        self.layer_list = layer_list
        self.img_size = img_size
        self.segment_size = img_size
        self.margin = 3
        self.max_knum = 5
        self.k_rate = 0.1
        self.threshold_scale = 0.25

        self.output_folder = 'result_segments/%s' % self.model_name
        self.input = None
        self.cln_xs = None

    def _for_hook(self, module, fea_in, fea_out):
        self.features_out_hook.append(np.squeeze(fea_out.data.cpu().numpy()))

    def _handle_hook(self):
        for name, module in self.model._modules.items():
            self.module_name.append(name)
            handle = module.register_forward_hook(self._for_hook)
            self.hook_handle_list.append(handle)

    def _release_hook(self):
        for handle in self.hook_handle_list:
            handle.remove()

    def _predict(self, input):
        with torch.no_grad():
            pred = self.model(input)

    def _save_results(self):
        if not os.path.exists(self.output_folder):
            os.makedirs(self.output_folder + '/image')

    def _calculate_features(self):
        for layer_id, key in enumerate(self.features_out_hook):
            # 输入层与输出层排除
            if "conv" in self.module_name[layer_id].lower() or len(key.shape) < 3:
                continue
            # 若指定layer_list，则仅输出指定层输出
            if len(self.layer_list) > 0 and self.module_name[layer_id].lower() not in self.layer_list:
                continue
            num_units = self.features_out_hook[layer_id].shape[1]
            for inputID in range(len(self.cln_img)):
                # 取出前 k 激活的神经元
                feature_map = self.features_out_hook[layer_id][inputID]
                knum = min(self.max_knum, max(1, int(len(feature_map) * self.k_rate)))
                topk = np.argsort(np.mean(np.mean(feature_map, 1), 1))
                topk = topk[-knum:]
                whole_layer_output = []
                for unitID in range(knum):
                    index = topk[unitID]
                    feature_unit = feature_map[index] / np.max(feature_map[index])
                    # 生成 mask
                    mask = cv2.resize(feature_unit, self.segment_size)
                    mask[mask < self.threshold_scale] = 0.0 # binarize the mask
                    mask[mask > self.threshold_scale] = 1.0

                    img = self.cln_img[inputID].numpy()
                    img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
                    img = cv2.resize(img, self.segment_size)
                    img = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
                    img_mask = np.multiply(img, mask[:,:, np.newaxis])
                    img_mask = np.uint8(img_mask * 255)
                    output_unit = []
                    output_unit.append(img_mask)
                    # whole_layer_output.append(img_mask)
                    # whole_layer_output.append(np.uint8(np.ones((self.segment_size[0], self.margin, 3))*255))

                    montage_unit = np.concatenate(output_unit, axis=1)
                    cv2.imwrite(os.path.join(self.output_folder, 'image', 'Pic%d-%s-%d.jpg'%(inputID, self.module_name[layer_id], unitID)), montage_unit)

                # montage_unit = np.concatenate(output_unit, axis=1)
                # cv2.imwrite(os.path.join(self.output_folder, 'image', 'Pic%d-%s-%d.jpg'%(inputID, self.module_name[layer_id], unitID)), montage_unit)
    def __call__(self, cln_img, input):
        # 添加挂锁
        self._handle_hook()

        # 执行预测过程
        self._predict(input)

        # 取消挂锁
        self._release_hook()

        self.input = input
        self.cln_img = cln_img

        self._save_results()
        self._calculate_features()

def init_model(model_path = '', pth_path = ''):
    model_lib = importlib.import_module(model_path)
    model = model_lib.getModel()
    model.load_state_dict(torch.load(pth_path, map_location=torch.device('cpu')))
    return model

if __name__ == '__main__':
    """
    1. Loads an image.
    2. transform it and converts to a pytorch variable.
    3. Makes a forward pass to get the neuron outputs.
    4. Makes the visualization. """
    model = init_model('Models.UserModel.ResNet2', '../../Models/weights/resnet20_cifar10_clean_new.pt')
    model = model.eval()

    visualizer = NeuronVisualizer(model = model, model_name = "ResNet2", layer_list = [], img_size = (32, 32))
    dataset = []
    cln_xs = []
    transform = transforms.Compose(
        [transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])])
    for i in range(2):
        im = cv2.imread('../../Datasets/cifar_test/flip_data/' + str(i) + '.jpg')
        im = np.ascontiguousarray(im[:,:,::-1])
        cln_xs.append(im)
        im = transform(im)
        dataset.append(np.array(im))
    cln_xs = np.array(cln_xs)
    cln_xs = torch.from_numpy(cln_xs)
    dataset = np.array(dataset)
    dataset = torch.from_numpy(dataset)
    visualizer(cln_xs, dataset)


text:
# Code adapted from https://github.com/ClementPinard/SfmLearner-Pytorch/blob/master/inverse_warp.py

from __future__ import division
from pytorch3d.ops.knn import knn_points
import torch
import torch.nn.functional as F
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import helper_functions
FLOAT_EPS = np.finfo(np.float).eps
pixel_coords = None
import kornia

from scipy.spatial.transform import Rotation as R

def preprocess_depth_output_2_point_cloud_all(depth_maps, masks, intrinsics):
    '''
    Pre process data for pose network

    Function mean subtracts the point cloud to bring it to origin and downsamples it to 2048 points
    '''

    batch_size, num_views, height, width  = depth_maps.size()

    depth_maps = helper_functions.sigmoid_2_depth(depth_maps)

    point_cloud_list_all_views = []
    rotated_point_cloud_list_all_views = []

    for view in range(num_views):

        src_camera_coords = pixel2cam(depth_maps[:, view].unsqueeze(0), intrinsics.inverse())
        src_camera_coords = src_camera_coords.reshape(batch_size, 3, height*width) # [B 3 H*W]

        if torch.cuda.is_available():
            random_rotation = torch.from_numpy(R.random(batch_size, random_state=1024).as_matrix()).cuda().float() # [B 3 3]
        else:
            random_rotation = torch.from_numpy(R.random(batch_size, random_state=1024).as_matrix()).float() # [B 3 3]

        point_cloud_list = []
        rotated_point_cloud_list = []
        masks_batch = masks[:, view]

        for i in range(batch_size):

            src_camera_coords_view = src_camera_coords[i] # [3 H*W]
            mask = masks_batch[i] # [H W]
            mask = mask.reshape(1, -1).squeeze() # [H*W]

            # Extracting the points only within mask region
            src_camera_coords_view = src_camera_coords_view[:, (mask == 1.0)]

            # Mean center value
            src_camera_coords_view = src_camera_coords_view - src_camera_coords_view.mean(axis = 1).unsqueeze(1).repeat(1, src_camera_coords_view.size(1)) #[3 masksize]

            # Downsample to 2048 points
            src_camera_coords_view = torch.nn.functional.interpolate(src_camera_coords_view.unsqueeze(0), size = 2048).squeeze(0)

            point_cloud_list.append(src_camera_coords_view)

        src_camera_coords_downsampled = torch.stack(point_cloud_list) # [B 3 2048]
        rot_src_camera_coords = random_rotation @ src_camera_coords_downsampled # [B 3 2048]

        point_cloud_list_all_views.append(src_camera_coords_downsampled)
        rotated_point_cloud_list_all_views.append(rot_src_camera_coords)

    camera_point_clouds_downsampled = torch.stack(point_cloud_list_all_views, dim = 1) # [B views 2048]
    rotated_camera_point_clouds_downsampled = torch.stack(rotated_point_cloud_list_all_views, dim = 1) # [B views 2048]

    return camera_point_clouds_downsampled, rotated_camera_point_clouds_downsampled

def preprocess_depth_output_2_point_cloud(depth_maps, masks_batch, intrinsics):
    '''
    Pre process data for pose network

    Function mean subtracts the point cloud to bring it to origin and downsamples it to 2048 points
    '''

    batch_size, _, height, width  = depth_maps.size()

    depth_maps = helper_functions.sigmoid_2_depth(depth_maps)
    src_camera_coords = pixel2cam(depth_maps[:, 0].unsqueeze(0), intrinsics.inverse())
    src_camera_coords = src_camera_coords.reshape(batch_size, 3, height*width) # [B 3 H*W]

    if torch.cuda.is_available():
        random_rotation = torch.from_numpy(R.random(batch_size, random_state=1024).as_matrix()).cuda().float() # [B 3 3]
    else:
        random_rotation = torch.from_numpy(R.random(batch_size, random_state=1024).as_matrix()).float() # [B 3 3]

    point_cloud_list = []
    rotated_point_cloud_list = []

    for i in range(batch_size):

        src_camera_coords_view = src_camera_coords[i] # [3 H*W]
        mask = masks_batch[i] # [H W]
        mask = mask.reshape(1, -1).squeeze() # [H*W]

        # Extracting the points only within mask region
        src_camera_coords_view = src_camera_coords_view[:, (mask == 1.0)]

        # mean center value
        src_camera_coords_view = src_camera_coords_view - src_camera_coords_view.mean(axis = 1).unsqueeze(1).repeat(1, src_camera_coords_view.size(1)) #[3 masksize]

        # Downsample to 2048 points
        src_camera_coords_view = torch.nn.functional.interpolate(src_camera_coords_view.unsqueeze(0), size = 2048).squeeze(0)

        point_cloud_list.append(src_camera_coords_view)

    src_camera_coords_downsampled = torch.stack(point_cloud_list) # [B 3 2048]
    rot_src_camera_coords = random_rotation @ src_camera_coords_downsampled # [B 3 2048]

    return src_camera_coords_downsampled, rot_src_camera_coords

def depth_decode(depth_image):

    # # first 16 bits (first 2 channels) are 16-bit depth
    # R is the 8 LSB and G are the others
    depth_image_16 = depth_image[:,:,[1, 0]]
    # B are 8-bit version
    depth_image_8 = depth_image[:,:,2]

    # last 8 are empty
    depth_single_channel = np.zeros((depth_image_16.shape[0], depth_image_16.shape[1]))
    # convert 16 bit to actual depth values
    for i in range(depth_single_channel.shape[0]):
        for j in range(depth_single_channel.shape[1]):
            bit_str = '{0:08b}'.format(depth_image_16[i, j, 0]) + '{0:08b}'.format(depth_image_16[i, j, 1])
            depth_single_channel[i, j] = int(bit_str, 2)

    return depth_single_channel

def set_id_grid(depth):
    global pixel_coords
    b, _, h, w = depth.size()
    i_range = torch.arange(0, h).view(1, h, 1).expand(1,h,w).type_as(depth)  # [1, H, W]
    j_range = torch.arange(0, w).view(1, 1, w).expand(1,h,w).type_as(depth)  # [1, H, W]
    ones = torch.ones(1,h,w).type_as(depth)
    #print("i_range",i_range.device)
    #print("j_range",j_range.device)
    #print("ones",ones.device)

    pixel_coords = torch.stack((j_range, i_range, ones), dim=1).type_as(depth)  # [1, 3, H, W]
    pixel_coords.to(depth.device)

def cam2pixel(cam_coords, proj_c2p_rot, proj_c2p_tr):
    b, _, h, w = cam_coords.size()
    cam_coords_flat = cam_coords.reshape(b, 3, -1)  # [B, 3, H*W]

    if proj_c2p_rot is not None:
        pcoords = proj_c2p_rot.float() @ cam_coords_flat
    else:
        pcoords = cam_coords_flat

    if proj_c2p_tr is not None:
        pcoords = pcoords + proj_c2p_tr.float()  # [B, 3, H*W]
    X = pcoords[:, 0]
    Y = pcoords[:, 1]
    Z = pcoords[:, 2].clamp(min=1e-4)

    X_norm = 2*(X / Z)/(w-1) - 1  # Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1) [B, H*W]
    Y_norm = 2*(Y / Z)/(h-1) - 1  # Idem [B, H*W]

    pixel_coords = torch.stack([X_norm, Y_norm], dim=2)  # [B, H*W, 2]
    # print(pixel_coords.reshape(b,h,w,2).shape)
    return pixel_coords.reshape(b,h,w,2)

def pixel2cam(depth, intrinsics_inv):
    global pixel_coords
    b, _, h, w = depth.size()
    if (pixel_coords is None) or pixel_coords.size(2) < h:
        set_id_grid(depth)
    pixel_coords = pixel_coords.to(depth.device)
    current_pixel_coords = pixel_coords[:,:,:h,:w].expand(b,3,h,w).reshape(b, 3, -1)  # [B, 3, H*W]
    #print("-"*10)
    #print("Pixel", pixel_coords.device)
    #print("Depth", depth.device)
    #print("intrinsics_inv",intrinsics_inv.device)
    #print("current_pixel_coords",current_pixel_coords.device)
    #print("-"*10)
    cam_coords = (intrinsics_inv.float() @ current_pixel_coords.float())
    cam_coords = cam_coords.reshape(b, 3, h, w)
    return cam_coords * depth.clamp(min=1e-1)

def quat2mat(quat):

    x, y, z, w = quat[:,0], quat[:,1], quat[:,2], quat[:,3]

    B = quat.size(0)
    w2, x2, y2, z2 = w.pow(2), x.pow(2), y.pow(2), z.pow(2)
    n = w2 + x2 + y2 + z2
    x = x / n
    y = y / n
    z = z / n
    w = w / n
    w2, x2, y2, z2 = w.pow(2), x.pow(2), y.pow(2), z.pow(2)
    wx, wy, wz = w*x, w*y, w*z
    xy, xz, yz = x*y, x*z, y*z

    rotMat = torch.stack([1 - 2*y2 - 2*z2, 2*xy - 2*wz, 2*wy + 2*xz,
                          2*wz + 2*xy, 1 - 2*x2 - 2*z2, 2*yz - 2*wx,
                          2*xz - 2*wy, 2*wx + 2*yz, 1 - 2*x2 - 2*y2], dim=1).reshape(B, 3, 3)
    return rotMat

def pose_vec2mat(vec):

    size_list = list(vec.size())

    if len(size_list) == 3:
        # if dimension is [B 4 4] for multiview blender dataset

        return vec
    else:
        # If dimension is [B 7] for multiview nocs dataset
        b = vec.size(0)
        translation = vec[:, :3].unsqueeze(-1)  # [B, 3, 1]
        rot = vec[:,3:]
        rot_mat = quat2mat(rot)  # [B, 3, 3]

        invert_mat = torch.eye(4)
        invert_mat[0, 0] *= -1
        invert_mat[1, 1] *= -1

        # Adding 0.5 offset for dataset
        transform_mat = torch.cat([rot_mat,   (translation) + 0.5], dim=2)  # [B, 3, 4]
        transform_mat = torch.cat([transform_mat, torch.tensor([[0,0,0,1]]).unsqueeze(0).expand(1,1,4).type_as(transform_mat).repeat(b, 1, 1)], dim=1) # [B, 4, 4]
        return transform_mat @ invert_mat.type_as(transform_mat)

def inverse_warp(tgt_image, depth, intrinsics, src_pose, tgt_pose):

    src_camera_coords = pixel2cam(depth, intrinsics.inverse())
    src_pose_mat = pose_vec2mat(src_pose)
    tgt_pose_mat = pose_vec2mat(tgt_pose)

    src_cam_to_tgt_cam = tgt_pose_mat.inverse() @ src_pose_mat
    tgt_cam_2_proj = intrinsics @ src_cam_to_tgt_cam[:, :3, :] # Bx3x3 Bx3x4
    rot, tr = tgt_cam_2_proj[:,:,:3], tgt_cam_2_proj[:,:,-1:]
    tgt_pix_coords = cam2pixel(src_camera_coords, rot, tr)

    tgt_image = tgt_image.type_as(tgt_pix_coords)
    projected_img = F.grid_sample(tgt_image, tgt_pix_coords, padding_mode='zeros', align_corners=False)
    valid_points = tgt_pix_coords.abs().max(dim=-1)[0] <= 1

    return projected_img, valid_points

def inverse_warp_2(tgt_image, depth, intrinsics, src_pose, tgt_pose):

    '''
    Inverse warp function using Kornia
    '''

    src_pose_mat = pose_vec2mat(src_pose)
    tgt_pose_mat = pose_vec2mat(tgt_pose)

    b = tgt_image.size(0)
    h = torch.tensor(tgt_image.size(2)).repeat(b)
    w = torch.tensor(tgt_image.size(3)).repeat(b)

    intrinsics = torch.cat([intrinsics.float(), torch.tensor([[0, 0, 0]]).unsqueeze(2).expand(1, 3, 1).type_as(intrinsics).repeat(b, 1, 1).float()], dim = 2)
    intrinsics = torch.cat([intrinsics, torch.tensor([[0, 0, 0, 1]]).expand(1, 1, 4).type_as(intrinsics).repeat(b, 1, 1).float() ], dim = 1)

    pinhole_tgt = kornia.geometry.PinholeCamera(intrinsics, tgt_pose_mat.float(), h, w)
    pinhole_src = kornia.geometry.PinholeCamera(intrinsics, src_pose_mat.float(), h, w)

    image_src = kornia.geometry.depth_warp(pinhole_tgt, pinhole_src, depth.float(), tgt_image.float(), tgt_image.size(2), tgt_image.size(3))

    return image_src, image_src

def project_depth_point_cloud(depth, intrinsics, src_pose, tgt_pose):

    '''
    Project point cloud from src to tgt pose

    '''

    src_camera_coords = pixel2cam(depth, intrinsics.inverse()) # [B, 3, H, W]
    b, _, h, w = src_camera_coords.size()
    src_pose_mat = pose_vec2mat(src_pose)
    tgt_pose_mat = pose_vec2mat(tgt_pose)

    # source camera coordinates
    src_camera_coords = src_camera_coords.reshape(b, 3, h*w)
    src_cam_to_tgt_cam = tgt_pose_mat.inverse() @ src_pose_mat
    ones = torch.ones((b, 1, h*w), device=src_camera_coords.device)
    #print("ones",ones.device)
    #print("src_camera_coords",src_camera_coords.device)
    src_camera_coords_homogeneous = torch.cat([src_camera_coords, ones], dim = 1) # [B, 4, H*W]

    # destination camera coordinates
    projected_coords = src_cam_to_tgt_cam.float() @ src_camera_coords_homogeneous.float() # [B, 4, H*W]
    projected_coords = projected_coords[:, :3, :]

    return src_camera_coords, projected_coords

def NOCS_map_2_point_cloud(nocs_image_tensor, mask):
    '''
    Convert NOCS maps to point cloud

    Input:
        nocs_image_tensor - [B, 3, H, W] - torch tensor
        mask              - [B, H, W] - torch tensor
    Returns:
        nocs_point_cloud_list   - B element list - [3, masked dims]
        indices_list            - B element list - [2, masked dims]
    '''

    indices_list = []
    nocs_point_cloud_list = []

    B, views, H, W = nocs_image_tensor.shape

    for i in range(nocs_image_tensor.shape[0]):

        ind = torch.from_numpy(((mask[i, :, :] > 0.5).nonzero().cpu()).numpy())
        h = ind[:, 0]
        w = ind[:, 1]

        #torch.sigmoid((mask[i, :, :] - 0.5)* 100)

        #h = h.detach()
        #w = w.detach()
        #print(h.max(), w.max(), h.min(), w.min())
        nocs_point_cloud = nocs_image_tensor[i, :, h, w] # [3, mask]
        nocs_point_cloud.detach_()
        nocs_point_cloud_list.append(nocs_point_cloud)
        indices_list.append(torch.stack([h, w]).detach())  # [2, mask]

    return nocs_point_cloud_list, indices_list

def get_NOCS_correspondences(nocs_image_tensor_source, mask_source, nocs_image_tensor_target, mask_target):
    '''
    Get NOCS correspondences
    Input:
        nocs_image_tensor_source - [B, 3, H, W]
        mask_source              - [B, H, W]
        nocs_image_tensor_target - [B, 3, H, W]
        mask_target              - [B, H, W]

    Returns:
        indices_depth_list       - list of tensors with indices of shape [2, masked_dim]
    '''

    B, views, H, W = nocs_image_tensor_source.shape
    indices_depth_list_target = []
    indices_depth_list_source = []

    for i in range(B):

        nocs_point_cloud_list_source, indices_list_source = NOCS_map_2_point_cloud(nocs_image_tensor_source[i, :, :, :].unsqueeze(0), mask_source[i, 0, :, :].unsqueeze(0))

        nocs_point_cloud_list_target, indices_list_target = NOCS_map_2_point_cloud(nocs_image_tensor_target[i, :, :, :].unsqueeze(0), mask_target[i, 0, :, :].unsqueeze(0))

        pc_1, ind_1 = nocs_point_cloud_list_source[0], indices_list_source[0] # [3, mask_size], [2, mask_size]
        pc_2, ind_2 = nocs_point_cloud_list_target[0], indices_list_target[0] # [3, mask_size]

        # Perform NOCS KNN matching
        out = knn_points(pc_1.transpose(0, 1).unsqueeze(0), pc_2.transpose(0, 1).unsqueeze(0)) # [1, masked_dim, 3]
        corresponding_idx = out.idx[0, :, 0] # [masked_dim]
        corresponding_idx = ind_2[:, corresponding_idx]

        indices_depth_list_source.append(ind_1)
        indices_depth_list_target.append(corresponding_idx)

    return indices_depth_list_source, indices_depth_list_target

if __name__ == "__main__":

    src_pose = torch.tensor([[1663.45703125, 46.258087158203128, -2127.346435546875, 0.008096654899418354, -0.3257482051849365, 0.0027897413820028307, 0.9454177618026733]])
    tgt_pose = torch.tensor([[1889.214599609375, 221.49795532226563, -1699.667724609375, 0.039696164429187778, -0.4065377712249756, 0.01768353208899498, 0.9125999212265015]])
    src_pose_2 = torch.tensor([[2011.62060546875, 374.8108215332031, -1255.8643798828125,0.06847226619720459, -0.48349833488464358, 0.03797297552227974, 0.8718366026878357]])

    depth = Image.open('./test-images/depth.png')
    depth = np.array(depth)
    depth = depth_decode(depth)

    depth = torch.tensor(depth).unsqueeze(0).unsqueeze(1).float()

    # print(depth)
    # plt.imshow(depth[0][0])
    # plt.show()
    tgt_image = cv2.imread('./test-images/rgb.png')
    tgt_image = torch.tensor(tgt_image).unsqueeze(0).permute(0, 3, 1, 2).float() / 255.0
    intrinsics = torch.tensor([
        [617.1,0.0,320.0],
        [0.0,617.1,240.0],
        [0.0,0.0,1.0],
    ])

    scale_factor = 1
    src_pose[0, :3] = src_pose[0, :3] / scale_factor
    tgt_pose[0, :3] = tgt_pose[0, :3] / scale_factor
    src_pose_2[0, :3] = src_pose_2[0, :3] / scale_factor

    x_factor = -1
    src_pose[0, 0] = src_pose[0, 0] * x_factor
    tgt_pose[0, 0] = tgt_pose[0, 0] * x_factor
    src_pose_2[0, 0] = src_pose_2[0, 0] * x_factor

    src_pose[0, 4:6] = src_pose[0, 4:6] * -1
    tgt_pose[0, 4:6] = tgt_pose[0, 4:6] * -1
    src_pose_2[0, 4:6] = src_pose_2[0, 4:6] * -1

    intrinsics = intrinsics.unsqueeze(0)

    warp=inverse_warp(tgt_image, depth, intrinsics, tgt_pose, src_pose)
    warp=warp[0].permute(0,2,3,1)
    plt.imshow(warp[0])
    plt.show()

reason: import numpy,from scipy


text:
import numpy as np
import matplotlib.pyplot as plt
import csv

plt.rcParams.update({'figure.figsize': [6.5, 5.25]})
plt.rcParams.update({'font.family': 'serif'})
plt.rcParams.update({'font.size': 14})

plt.figure()
plt.grid(True)
plt.xlabel('Runtime per 1000 coarse-grid (32 x 32) steps (in seconds)')
plt.ylabel('Mean Absolute Error (MAE)')
plt.title('Accuracy-speed tradeoff')
for resolution in [64, 128]:
  with open(f'stats_{resolution}.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
      markersize = 14
      color = 'red'
      if resolution == 64:
        color = 'green'
      plt.semilogx(float(row[0]), float(row[1]), color=color, marker='o', markersize=markersize, label=f'Baseline {resolution} x {resolution}')
for stencil_size in [3, 5]:
  for num_layers in [4, 6, 8]:
    for filters in [64, 128]:
      with open(f'stats_{stencil_size}-{num_layers}-{filters}.txt') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
          label = ''
          if stencil_size == 3 and num_layers == 4 and filters == 64:
            label = 'Neural net 32 x 32\n(different hyperparameters)'
          plt.semilogx(float(row[0]), float(row[1]), 'b*', label=label)
plt.legend()
plt.savefig('mae_runtime.png')

max_stars_repo_name: lucianodrozda/data-driven-pdes-google

max_issues_repo_name: luciano-drozda/data-driven-pdes-google

max_forks_repo_name: luciano-drozda/data-driven-pdes-google


text:
from carla_utils import carla

import numpy as np
from typing import List
import cv2
import copy
import time

from .. import basic
from ..augment import ActorVertices, cvt
from ..agents import BaseAgent, BaseAgentObstacle

from .town_map_image import TownMapImage
from .town_map_image import COLOR_CHAMELEON_0, COLOR_CHOCOLATE_1, COLOR_WHITE

def d2_local(obstacle: BaseAgentObstacle, agent: BaseAgent, expand=carla.Vector2D(0.0,0.0)):
    dx, dy = obstacle.bounding_box.x +expand.x, obstacle.bounding_box.y +expand.y

    state = obstacle.get_state().world2local(agent.get_state())
    center_x, center_y, theta = state.x, state.y, state.theta

    l, n = np.array([np.cos(theta), np.sin(theta)]), np.array([np.cos(theta+np.pi/2), np.sin(theta+np.pi/2)])
    vertices = np.expand_dims(np.array([center_x, center_y]), axis=0).repeat(4, axis=0)

    vertices[0] +=  l*dx + n*dy
    vertices[1] += -l*dx + n*dy
    vertices[2] += -l*dx - n*dy
    vertices[3] +=  l*dx - n*dy
    return vertices

def sub_image(image, center, theta, width, height):
	"""Extract a rectangle from the source image.

	image - source image
	center - (x,y) tuple for the centre point.
	theta - angle of rectangle.
	width, height - rectangle dimensions.
	"""

	if 45 < theta <= 90:
		theta = theta - 90
		width, height = height, width

	# theta *= np.pi / 180 # convert to rad
	v_x = (np.cos(theta), np.sin(theta))
	v_y = (-np.sin(theta), np.cos(theta))
	s_x = center[0] - v_x[0] * (width / 2) - v_y[0] * (height / 2)
	s_y = center[1] - v_x[1] * (width / 2) - v_y[1] * (height / 2)
	mapping = np.array([[v_x[0],v_y[0], s_x], [v_x[1],v_y[1], s_y]])

	return cv2.warpAffine(image, mapping, (width, height), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_REPLICATE)

class BirdEyeView(object):
    def __init__(self, core, dim_state, range_min=None, range_max=None):
        '''
            range_min < 0, range_max > 0

        '''

        self.core = core
        self.dim_state = dim_state
        if range_min == None: range_min = carla.Vector2D(x=-25, y=-30)
        if range_max == None: range_max = carla.Vector2D(x=45, y=30)

        self.resolution = 0.1
        self.town_map_image = TownMapImage(core, self.resolution)

        assert range_max.x >= -range_min.x
        assert range_max.y + range_min.y == 0
        self.range_x_min, self.range_x_max = range_min.x, range_max.x
        self.range_y_max = range_max.y
        self.radius = 0.5 * (self.range_x_max - self.range_x_min) * np.sqrt(2)
        self.offset = np.array([self.range_x_min, -self.range_y_max])

        self.height, self.width = int(2*self.range_y_max / self.resolution), int((self.range_x_max - self.range_x_min) / self.resolution)
        self.channels = 3

    def run_step(self, agent: BaseAgent, obstacles: List[BaseAgentObstacle]):
        ### crop global image
        state_ego = agent.get_state()

        center = np.array([[state_ego.x, state_ego.y]])
        ##### find image center, maybe not center of vehicle
        center += 0.5*(self.range_x_max+self.range_x_min) *np.array([[np.cos(state_ego.theta), np.sin(state_ego.theta)]])
        center_pix = (center - self.town_map_image.world_offset) / self.town_map_image.resolution  ## world to image
        center_pix = center_pix[0].astype(np.int64)

        image_bev = sub_image(self.town_map_image.image_cv2, center_pix, state_ego.theta, self.width, self.height)

        ### route
        waypoints, _ = agent.global_path.remaining_waypoints(agent.get_transform())
        waypoints = waypoints[:150:15]   ## TODO change

        corners = []
        for p in waypoints:
            t = p.transform
            s = cvt.CUAState.carla_transform(t).world2local(state_ego)
            corners.append([s.x, s.y])

        pts = (np.array(corners) - self.offset) / self.resolution
        color = (0,0,255)
        cv2.polylines(image_bev, [pts.astype(np.int64)], False, color, 8)

        ### ego
        vertices_ego = d2_local(agent, agent)
        boundary_ego = (vertices_ego - self.offset) / self.resolution

        cv2.fillPoly(image_bev, pts=[boundary_ego.astype(np.int64)], color=COLOR_CHAMELEON_0)

        ### obs
        for obs in obstacles:
            state_obs = obs.get_state()
            if state_ego.distance_xy(state_obs) > self.radius:
                continue

            vertices_obs = d2_local(obs, agent)
            boundary_obs = (vertices_obs - self.offset) / self.resolution
            cv2.fillPoly(image_bev, pts=[boundary_obs.astype(np.int64)], color=COLOR_CHOCOLATE_1)

        return image_bev

class BridEyeViewChannelWise(BirdEyeView):
    def __init__(self, core, dim_state, range_min=None, range_max=None):
        super().__init__(core, dim_state, range_min, range_max)
        self.channels = 5

    def run_step(self, agent: BaseAgent, obstacles: List[BaseAgentObstacle]):

        ### crop global image
        state_ego = agent.get_state()

        center = np.array([[state_ego.x, state_ego.y]])
        ##### find image center, maybe not center of vehicle
        center += 0.5*(self.range_x_max+self.range_x_min) *np.array([[np.cos(state_ego.theta), np.sin(state_ego.theta)]])
        center_pix = (center - self.town_map_image.world_offset) / self.town_map_image.resolution  ## world to image
        center_pix = center_pix[0].astype(np.int64)

        image_background = sub_image(self.town_map_image.image_cv2, center_pix, state_ego.theta, self.width, self.height)
        image_background = np.expand_dims(cv2.cvtColor(image_background, cv2.COLOR_RGB2GRAY), 2)   ### reduce channel

        ### route
        image_route = np.zeros((self.height, self.width, 1), dtype=np.uint8)
        waypoints, _ = agent.global_path.remaining_waypoints(agent.get_transform())
        waypoints = waypoints[:150:15]   ## TODO change

        corners = []
        for p in waypoints:
            t = p.transform
            s = cvt.CUAState.carla_transform(t).world2local(state_ego)
            corners.append([s.x, s.y])

        pts = (np.array(corners) - self.offset) / self.resolution
        cv2.polylines(image_route, [pts.astype(np.int64)], False, COLOR_WHITE, 20)

        ### ego
        image_ego = np.zeros((self.height, self.width, 1), dtype=np.uint8)
        vertices_ego = d2_local(agent, agent)
        boundary_ego = (vertices_ego - self.offset) / self.resolution

        cv2.fillPoly(image_ego, pts=[boundary_ego.astype(np.int64)], color=COLOR_WHITE)

        ### obs
        image_obs = np.zeros((self.height, self.width, 1), dtype=np.uint8)
        image_obs_v = np.zeros((self.height, self.width, 1), dtype=np.uint8)
        for obs in obstacles:
            state_obs = obs.get_state()
            if state_ego.distance_xy(state_obs) > self.radius:
                continue

            vertices_obs = d2_local(obs, agent)
            boundary_obs = (vertices_obs - self.offset) / self.resolution
            cv2.fillPoly(image_obs, pts=[boundary_obs.astype(np.int64)], color=COLOR_WHITE)
            color_v = int((obs.get_current_v() / agent.max_velocity / 1.1) * 255)
            color_v = int(np.clip(color_v, 0, 255))
            # color_v = 0 ### ! warning
            cv2.fillPoly(image_obs_v, pts=[boundary_obs.astype(np.int64)], color=color_v)

        image = np.concatenate([image_background, image_route, image_ego, image_obs, image_obs_v], axis=2)
        image = cv2.resize(image, self.dim_state)
        return image

    def viz(self, image):
        image_background = image[:,:,0]
        image_route = image[:,:,1]
        image_ego = image[:,:,2]
        image_obs = image[:,:,3]

        iib = cv2.cvtColor(image_background, cv2.COLOR_GRAY2BGR)
        iii = np.hstack([iib, np.stack([image_route, image_ego, image_obs], axis=2)])
        cv2.imshow('haha', iii)
        cv2.waitKey(1)
        # cv2.imwrite('./results/tmp/' + str(time.time()) + '.png', iii)

max_stars_repo_name: IamWangYunKai/DG-TrajGen


text:
from copy import copy

import numpy as np

from abc import ABC
from typing import List

from advertising.data_structure.Campaign import Campaign
from advertising.optimizers.CampaignOptimizer import CampaignOptimizer
from advertising.regressors.DiscreteRegressor import DiscreteRegressor
from bandit.IBandit import IBandit

class CombinatorialBandit(IBandit, ABC):
    """
    General class for the combinatorial bandit for a specific campaign with a cumulative daily budget and
    each budget of every sub-campaign can assume values from [0, cumulative daily budget] with a uniform discretization
    """

    def __init__(self, campaign: Campaign, model_list: List[DiscreteRegressor]):
        """
        :param campaign: campaign that will be optimized
        :param model_list: regressors that will be used to estimate the quantity in each campaign. There is a regressor
        for each campaign
        """
        assert len(model_list) == campaign.get_n_sub_campaigns()

        self.campaign: Campaign = campaign
        self.t: int = 0
        self.collected_rewards: List[float] = []
        self.pulled_superarm_list: List[List] = []
        self.model_list: List[DiscreteRegressor] = model_list

        self.collected_rewards_sub_campaign: List[List] = [[] for _ in range(campaign.get_n_sub_campaigns())]
        self.pulled_arm_sub_campaign: List[List] = [[] for _ in range(campaign.get_n_sub_campaigns())]

        for sub_index, model in enumerate(self.model_list):
            sub_campaign_values = self.model_list[sub_index].sample_distribution()
            self.campaign.set_sub_campaign_values(sub_index, sub_campaign_values)

    def pull_arm(self, value_per_clicks: List[float] = None) -> List[int]:
        """
        Find the best allocation of budgets by optimizing the combinatorial problem of the campaign and then return
        the indices of the best budgets.
        The combinatorial problem is optimized given estimates provided only by the last data (amount specified
        by the sliding window)

        :return: the indices of the best budgets given the actual campaign
        """
        if value_per_clicks is None:
            value_per_clicks = np.ones(shape=self.campaign.get_n_sub_campaigns())

        temp_campaign: Campaign = copy(self.campaign)
        temp_campaign.multiply_sub_campaign_values(value_per_clicks)
        max_clicks, best_budgets = CampaignOptimizer.find_best_budgets(temp_campaign)
        return [np.where(temp_campaign.get_budgets() == budget)[0][0] for budget in best_budgets]


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.
# ============================================================================
"""Generate the summary event which conform to proto format."""
import io
import platform
import time

import numpy as np
from PIL import Image

from mindspore import log as logger
from mindspore import context
from mindspore.communication.management import get_rank
from mindspore.communication.management import GlobalComm

from ..._checkparam import Validator
from ..anf_ir_pb2 import DataType, ModelProto
from ..summary_pb2 import Event

# define the MindSpore image format
MS_IMAGE_TENSOR_FORMAT = 'NCHW'
# Set the Event mark
EVENT_FILE_NAME_MARK = ".out.events.summary."
# Set the init event of version and mark
EVENT_FILE_INIT_VERSION_MARK = "MindSpore.Event:"
EVENT_FILE_INIT_VERSION = 1

F32_MIN, F32_MAX = np.finfo(np.float32).min, np.finfo(np.float32).max

def get_event_file_name(prefix, suffix, time_second):
    """
    Create file name: file_prefix + EVENT_FILE_NAME_MARK + time(seconds) + "." + Hostname + file_suffix.

    Args:
        prefix (str): The prefix of file name.
        suffix (str): The suffix of file name.
        time_second (str): The time stamp of file name.

    Returns:
        String, the name of event log file.
    """
    Validator.check_str_by_regular(prefix)
    Validator.check_str_by_regular(suffix)
    file_name = ""
    hostname = platform.node()

    device_num = context.get_auto_parallel_context('device_num')
    device_id = context.get_context('device_id')
    if device_num > 1 or GlobalComm.WORLD_COMM_GROUP == 'nccl_world_group':
        # Notice:
        # In GPU distribute training scene, get_context('device_id') will not work,
        # so we use get_rank instead of get_context.
        device_id = get_rank()

    file_name = f'{file_name}{EVENT_FILE_NAME_MARK}{time_second}.{device_id}.{hostname}'

    if prefix is not None:
        file_name = prefix + file_name

    if suffix is not None:
        file_name = file_name + suffix

    return file_name

def package_init_event():
    """Package the summary init event."""
    init_event = Event()
    init_event.wall_time = time.time()
    version = EVENT_FILE_INIT_VERSION_MARK + str(EVENT_FILE_INIT_VERSION)
    init_event.version = version
    return init_event

def package_graph_event(data):
    """
    Package the summary graph event.

    Args:
        data (Bytes): Graph bytes string.

    Returns:
        Event, event log object.
    """
    graph_event = Event()
    graph_event.wall_time = time.time()
    modelp = ModelProto()
    modelp.ParseFromString(data)
    graph_event.graph_def.CopyFrom(modelp.graph)
    return graph_event

def package_summary_event(data_list, step, wall_time):
    """
    Package the summary to event protobuffer.

    Args:
        data_list (list): Summary data list.
        step (Number): The recode step index.
        wall_time (float): The wall time.

    Returns:
        Summary, the summary event.
    """
    # create the event of summary
    summary_event = Event()
    summary = summary_event.summary
    summary_event.wall_time = wall_time
    summary_event.step = int(step)

    for value in data_list:
        summary_type = value["_type"]
        data = value["data"]
        tag = value["name"]

        logger.debug(f"Now process {summary_type} summary, tag = {tag}")

        summary_value = summary.value.add()
        summary_value.tag = tag
        # get the summary type and parse the tag
        if summary_type == 'Scalar':
            if not _fill_scalar_summary(tag, data, summary_value):
                del summary.value[-1]
        elif summary_type == 'Tensor':
            _fill_tensor_summary(tag, data, summary_value.tensor)
        elif summary_type == 'Image':
            if not _fill_image_summary(tag, data, summary_value.image, MS_IMAGE_TENSOR_FORMAT):
                del summary.value[-1]
        elif summary_type == 'Histogram':
            _fill_histogram_summary(tag, data, summary_value.histogram)
        elif summary_type == 'Landscape':
            summary_value.loss_landscape.ParseFromString(data)
        else:
            # The data is invalid ,jump the data
            logger.error(f"Summary type({summary_type}) is error, tag = {tag}")
            del summary.value[-1]

    return summary_event

def _nptype_to_prototype(np_value):
    """
    Transform the np type to proto type.

    Args:
        np_value (Type): Numpy data type.

    Returns:
        Type, proto data type.
    """
    np2pt_tbl = {
        np.bool_: 'DT_BOOL',
        np.int8: 'DT_INT8',
        np.int16: 'DT_INT16',
        np.int32: 'DT_INT32',
        np.int64: 'DT_INT64',
        np.uint8: 'DT_UINT8',
        np.uint16: 'DT_UINT16',
        np.uint32: 'DT_UINT32',
        np.uint64: 'DT_UINT64',
        np.float16: 'DT_FLOAT16',
        np.float: 'DT_FLOAT64',
        np.float32: 'DT_FLOAT32',
        np.float64: 'DT_FLOAT64',
        None: 'DT_UNDEFINED'
    }
    np_type = None
    if np_value is None:
        logger.error("The numpy value in Summary is none")
    else:
        np_type = np_value.dtype.type

    proto = np2pt_tbl.get(np_type, None)
    if proto is None:
        raise TypeError("Transform numpy type failed in Summary, expect numpy type is one of ['np.bool_', 'np.int8', "
                        "'np.int16', 'np.int32', 'np.int64', 'np.uint8', 'np.uint16', 'np.uint32', 'np.uint64', "
                        "'np.float16', 'np.float', 'np.float64'].")

    return proto

def _fill_scalar_summary(tag: str, np_value, summary):
    """
    Package the scalar summary.

    Args:
        tag (str): Summary tag describe.
        np_value (Object): Scalary object.

    Returns:
        Summary, return scalar summary content.
    """
    logger.debug(f"Set({tag}) the scalar summary value")
    if np_value.size == 1:
        # is scalar
        summary.scalar_value = np_value.item()
        return True
    if np_value.size > 1:
        logger.info(
            f"The tensor is not a single scalar, tag = {tag}, ndim = {np_value.ndim}, shape = {np_value.shape}")
        summary.scalar_value = next(np_value.flat).item()
        return True
    logger.error(f"The size of Summary tensor should greater than 1, "
                 f"but got size = {np_value.size}, this means has no values inside tensor, ")
    return False

def _fill_tensor_summary(tag: str, np_value, summary_tensor):
    """
    Package the tensor summary.

    Args:
        tag (str): Summary tag describe.
        np_value (Type): Summary data type.
        summary_tensor (Tensor): The tensor of summary.

    Returns:
        Summary, return tensor summary content.
    """
    logger.debug(f"Set({tag}) the tensor summary value")
    # get tensor dtype
    tensor_dtype = _nptype_to_prototype(np_value)
    summary_tensor.data_type = DataType.Value(tensor_dtype)

    # get the value list
    tensor_value_list = np_value.reshape(-1).tolist()
    summary_tensor.float_data.extend(tensor_value_list)

    # get the tensor dim
    for v in np_value.shape:
        summary_tensor.dims.append(v)

    return summary_tensor

def _calc_histogram_bins(count):
    """
    Calculates experience-based optimal bins number for histogram.

    There should be enough number in each bin. So we calc bin numbers according to count. For very small count(1 -
    10), we assign carefully chosen number. For large count, we tried to make sure there are 9-10 numbers in each
    bucket on average. Too many bins will slow down performance, so we set max number of bins to 90.

    Args:
        count (int): Valid number count for the tensor.

    Returns:
        int, number of histogram bins.
    """
    max_bins, max_per_bin = 90, 10

    if not count:
        return 1
    if count <= 5:
        return 2
    if count <= 10:
        return 3
    if count <= 880:
        # note that math.ceil(881/10) + 1 equals 90
        return count // max_per_bin + 1

    return max_bins

def _fill_histogram_summary(tag: str, np_value: np.ndarray, summary) -> None:
    """
    Package the histogram summary.

    Args:
        tag (str): Summary tag describe.
        np_value (np.ndarray): Summary data.
        summary (summary_pb2.Summary.Histogram): Summary histogram data.
    """
    logger.debug(f"Set({tag}) the histogram summary value")
    # Default bucket for tensor with no valid data.
    ma_value = np.ma.masked_invalid(np_value)
    total, valid = np_value.size, ma_value.count()
    invalids = []
    for isfn in np.isnan, np.isposinf, np.isneginf:
        if total - valid > sum(invalids):
            invalids.append(np.count_nonzero(isfn(np_value)))
        else:
            invalids.append(0)

    summary.count = total
    summary.nan_count, summary.pos_inf_count, summary.neg_inf_count = invalids
    if not valid:
        logger.warning(f'There are no valid values in the ndarray(size={total}, shape={np_value.shape})')
        # summary.{min, max, sum} are 0s by default, no need to explicitly set
    else:
        # BUG: max of a masked array with dtype np.float16 returns inf
        # See numpy issue#15077
        if issubclass(np_value.dtype.type, np.floating):
            summary.min = ma_value.min(fill_value=np.PINF)
            summary.max = ma_value.max(fill_value=np.NINF)
            if summary.min < F32_MIN or summary.max > F32_MAX:
                logger.warning(f'Values({summary.min}, {summary.max}) are too large, '
                               f'you may encounter some undefined behaviours hereafter.')
        else:
            summary.min = ma_value.min()
            summary.max = ma_value.max()
        summary.sum = ma_value.sum(dtype=np.float64)
        _fill_bucket(valid, np_value, summary)

def _fill_bucket(valid, np_value, summary):
    """
    Fill the bucket.

    Args:
        valid (int): The count of valid data.
        np_value (np.ndarray): Summary data.
        summary (summary_pb2.Summary.Histogram): Summary histogram data.
    """
    bins = _calc_histogram_bins(valid)
    first_edge, last_edge = summary.min, summary.max

    if not first_edge < last_edge:
        first_edge -= 0.5
        last_edge += 0.5

    bins = np.linspace(first_edge, last_edge, bins + 1, dtype=np_value.dtype)
    hists, edges = np.histogram(np_value, bins=bins)

    for hist, edge1, edge2 in zip(hists, edges, edges[1:]):
        bucket = summary.buckets.add()
        bucket.width = edge2 - edge1
        bucket.count = hist
        bucket.left = edge1

def _fill_image_summary(tag: str, np_value, summary_image, input_format='NCHW'):
    """
    Package the image summary.

    Args:
        tag (str): Summary tag describe.
        np_value (Type): Summary data type.
        summary_image (Tensor): The tensor of summary.
        input_format (str): Data sort order index. Default: 'NCHW'.

    Returns:
        Summary, return image summary content.
    """
    logger.debug(f"Set({tag}) the image summary value")
    if np_value.ndim != 4 or np_value.shape[1] not in (1, 3):
        logger.error(f"The dimension of Summary tensor should be 4 or second dimension should be 1 or 3, "
                     f"but got tag = {tag}, ndim = {np_value.ndim}, shape={np_value.shape}, "
                     f"which means Summary tensor is not Image.")
        return False

    if np_value.ndim != len(input_format):
        logger.error(
            f"The tensor with dimension({np_value.ndim}) can't convert the format({input_format}) "
            f"because dimension not same, the dimension should be {len(input_format)}.")
        return False

    if 0 in np_value.shape:
        logger.error(
            f"The tensor with shape({np_value.shape}) is not a valid image because the shape contains zero.")
        return False

    # convert the tensor format
    tensor = _convert_image_format(np_value, input_format)

    # convert the tensor dtype
    # Do not assume that user passes in values in [0, 255], use data type to detect
    scale_factor = 1
    if tensor.dtype == np.uint8:
        scale_factor = 1
    elif np.max(tensor) <= 1 and np.min(tensor) >= 0:
        scale_factor = 255
    tensor = tensor.astype(np.float32)
    tensor = (tensor * scale_factor).astype(np.uint8)

    # create the image summary
    height, width, channel, image_string = _make_image(tensor)
    summary_image.height = height
    summary_image.width = width
    summary_image.colorspace = channel
    summary_image.encoded_image = image_string
    return True

def _make_image(tensor, rescale=1):
    """
    Convert a numpy representation of an image to Image protobuf.

    Args:
        tensor (Tensor): The image data.
        rescale (Number): The rescale value. Default: 1.

    Returns:
        (Number, Number, Number, Bytes), return the height, width, channel, image string .
    """
    height, width, channel = tensor.shape
    scaled_height = int(height * rescale)
    scaled_width = int(width * rescale)
    image = Image.fromarray(tensor)
    image = image.resize((scaled_width, scaled_height), Image.ANTIALIAS)
    output = io.BytesIO()
    image.save(output, format='PNG')
    image_string = output.getvalue()
    output.close()
    return height, width, channel, image_string

def _convert_image_format(np_tensor, input_format, out_format='HWC'):
    """
    Convert the image format.

    Args:
        np_tensor (Tensor): The image data.
        input_format (str): Input data format.
        out_format (str): The output data format. Default: 'HWC'.

    Returns:
        Tensor, return format image.
    """
    input_format = input_format.upper()

    # convert the NCHW
    if input_format != 'NCHW':
        index = [input_format.find(c) for c in 'NCHW']
        tensor_nchw = np_tensor.transpose(index)
    else:
        tensor_nchw = np_tensor

    # make grid to expand N
    tensor_chw = _make_canvas_for_imgs(tensor_nchw)

    # convert to out format
    out_index = ['CHW'.find(c) for c in out_format]
    out_tensor = tensor_chw.transpose(out_index)
    return out_tensor

def _make_canvas_for_imgs(tensor, col_imgs=8):
    """
    Expand the N, show imgs on a canvas.

    Args:
        tensor (Tensor): The canvas value.
        col_imgs (Number): The image colume number. Default: 8.

    Returns:
        Tensor, return canvas of image.
    """
    # expand the N1HW to N3HW
    if tensor.shape[1] == 1:
        tensor = np.concatenate([tensor, tensor, tensor], 1)

    # expand the N
    n = tensor.shape[0]
    h = tensor.shape[2]
    w = tensor.shape[3]
    cols = min(n, col_imgs)
    rows = int(np.ceil(float(n) / cols))

    # create the canvas: expand the n
    out_canvas = np.zeros((3, h * rows, w * cols))
    i = 0
    for y in range(rows):
        for x in range(cols):
            if i >= n:
                break
            out_canvas[:, y * h:(y + 1) * h, x * w:(x + 1) * w] = tensor[i]
            i = i + 1
    return out_canvas

max_stars_repo_name: httpsgithu/mindspore