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

import os
import copy
import numpy as np

from mmcv.utils.registry import build_from_cfg
from mmcls.datasets.builder import DATASETS, PIPELINES
from mmcls.datasets.base_dataset import BaseDataset
from mmcls.datasets.pipelines import Compose

from mpa.utils.logger import get_logger

logger = get_logger()

@DATASETS.register_module()
class ClsDirDataset(BaseDataset):
    """Classification dataset for Finetune, Incremental Learning, Semi-SL
        assumes the data for classification is divided by folders (classes)

    Args:
        data_dir (str): the prefix of data path
        pipeline (list): a list of dict, where each element represents
            a operation defined in `mmcls.datasets.pipelines`
        classes (list): List of classes to be used for training,
            If empty, use all classes in the folder list
            Also, if there is new_classes (incremental learning),
            classes are used as a list of old classes
        new_classes (list): List of final classes to be used for incremental learning
        use_labels (bool): dataset with labels or unlabels
    """

    def __init__(self, data_dir, pipeline=[], classes=[], new_classes=[], use_labels=True, **kwargs):
        self.data_dir = data_dir
        self._samples_per_gpu = kwargs.pop('samples_per_gpu', 1)
        self._workers_per_gpu = kwargs.pop('workers_per_gpu', 1)
        self.use_labels = use_labels
        self.img_indices = dict(old=[], new=[])
        self.class_acc = False

        self.new_classes = new_classes
        if not classes:
            self.CLASSES = self.get_classes_from_dir(self.data_dir)
        else:
            self.CLASSES = self.get_classes(classes)
        if isinstance(self.CLASSES, list):
            self.CLASSES.sort()
        self.num_classes = len(self.CLASSES)

        # Pipeline Settings
        if isinstance(pipeline, dict):
            self.pipeline = {}
            for k, v in pipeline.items():
                _pipeline = [dict(type='LoadImageFromFile'), *v]
                _pipeline = [build_from_cfg(p, PIPELINES) for p in _pipeline]
                self.pipeline[k] = Compose(_pipeline)
            self.num_pipes = len(pipeline)
        elif isinstance(pipeline, list):
            self.num_pipes = 1
            _pipeline = [dict(type='LoadImageFromFile'), *pipeline]
            self.pipeline = Compose([build_from_cfg(p, PIPELINES) for p in _pipeline])

        self.data_infos = self.load_annotations()
        self.statistics()

    def statistics(self):
        logger.info(f'ClsDirDataset - {self.num_classes} classes from {self.data_dir}')
        logger.info(f'- Classes: {self.CLASSES}')
        if self.new_classes:
            logger.info(f'- New Classes: {self.new_classes}')
            old_data_length = len(self.img_indices['old'])
            new_data_length = len(self.img_indices['new'])
            logger.info(f'- # of old classes images: {old_data_length}')
            logger.info(f'- # of New classes images: {new_data_length}')
        logger.info(f'- # of images: {len(self)}')

    def _read_dir(self):
        img_path, img_class, img_prefix = [], [], []
        if self.use_labels:
            cls_dir_list = os.listdir(self.data_dir)
            for cls_name in cls_dir_list:
                if cls_name not in self.CLASSES:
                    continue
                cls_dir = os.path.join(self.data_dir, cls_name)
                if not os.path.isdir(cls_dir):
                    continue

                cls_img_path = os.listdir(cls_dir)
                img_path += cls_img_path
                img_prefix += [cls_dir] * len(cls_img_path)
                img_class += [cls_name] * len(cls_img_path)
        else:
            path_list = os.listdir(self.data_dir)
            for p in path_list:
                current_path = os.path.join(self.data_dir, p)
                if os.path.isdir(current_path):
                    cls_img_path = os.listdir(current_path)
                    img_path += cls_img_path
                    img_prefix += [current_path] * len(cls_img_path)
                    img_class += [self.CLASSES[0]] * len(cls_img_path)
                else:
                    img_path += [p]
                    img_prefix += [self.data_dir]
                    img_class += [self.CLASSES[0]]

        return img_path, img_class, img_prefix

    def load_annotations(self):
        img_path_list, img_class_list, img_prefix_list = self._read_dir()
        data_infos = []
        for i, (img_path, img_cls, img_prefix) in enumerate(zip(img_path_list, img_class_list, img_prefix_list)):
            if self.use_labels:
                gt_label = np.array(self.class_to_idx[img_cls])
            else:
                gt_label = np.array([-1])
            info = {'img_prefix': img_prefix, 'img_info': {'filename': img_path}, 'gt_label': gt_label}
            data_infos.append(info)
            if img_cls in self.new_classes:
                self.img_indices['new'].append(i)
            else:
                self.img_indices['old'].append(i)
        return data_infos

    def get_classes_from_dir(self, root):
        if not self.use_labels:
            return [os.path.basename(root)]
        # classes = self.get_classes(classes)
        # print(classes)
        # if classes is None:
        classes = []
        path_list = os.listdir(root)
        for p in path_list:
            if os.path.isdir(os.path.join(root, p)):
                if p not in classes:
                    classes.append(p)
            else:
                raise ValueError("This folder structure is not suitable for label data")
        return classes

    def __len__(self):
        return len(self.data_infos)

    def __getitem__(self, idx):
        if self.pipeline is None:
            return self.data_infos[idx]

        data_infos = [
            copy.deepcopy(self.data_infos[idx]) for _ in range(self.num_pipes)
        ]
        if isinstance(self.pipeline, dict):
            results = {}
            for i, (k, v) in enumerate(self.pipeline.items()):
                results[k] = self.pipeline[k](data_infos[i])
        else:
            results = self.pipeline(data_infos[0])

        return results

    def evaluate(self,
                 results,
                 metric='accuracy',
                 metric_options=None,
                 logger=None):
        """Evaluate the dataset with new metric 'class_accuracy'

        Args:
            results (list): Testing results of the dataset.
            metric (str | list[str]): Metrics to be evaluated.
                Default value is `accuracy`.
                'accuracy', 'precision', 'recall', 'f1_score', 'support', 'class_accuracy'
            metric_options (dict, optional): Options for calculating metrics.
                Allowed keys are 'topk', 'thrs' and 'average_mode'.
                Defaults to None.
            logger (logging.Logger | str, optional): Logger used for printing
                related information during evaluation. Defaults to None.
        Returns:
            dict: evaluation results
        """
        if metric_options is None:
            metric_options = {'topk': (1, 5) if self.num_classes >= 5 else (1, )}

        if isinstance(metric, str):
            metrics = [metric]
        else:
            metrics = metric

        if 'class_accuracy' in metrics:
            metrics.remove('class_accuracy')
            self.class_acc = True

        eval_results = super().evaluate(results, metrics, metric_options, logger)

        # Add Evaluation Accuracy score per Class
        if self.class_acc:
            results = np.vstack(results)
            gt_labels = self.get_gt_labels()
            accuracies = self.class_accuracy(results, gt_labels)
            eval_results.update({f'{c} accuracy': a for c, a in zip(self.CLASSES, accuracies)})
            eval_results.update({'mean accuracy': np.mean(accuracies)})

        return eval_results

    def class_accuracy(self, results, gt_labels):
        accracies = []
        pred_label = results.argsort(axis=1)[:, -1:][:, ::-1]
        for i in range(self.num_classes):
            cls_pred = pred_label == i
            cls_pred = cls_pred[gt_labels == i]
            cls_acc = np.sum(cls_pred) / len(cls_pred)
            accracies.append(cls_acc)
        return accracies

    @property
    def samples_per_gpu(self):
        return self._samples_per_gpu

    @property
    def workers_per_gpu(self):
        return self._workers_per_gpu


text:
# --------------------------------------------------------
# Deep Iterative Matching Network
# Licensed under The Apache-2.0 License [see LICENSE for details]
# Written by Gu Wang, Yi Li
# --------------------------------------------------------
"""
generate rendered from rendered poses
generate (observed rendered) pair list file for training (or test)
"""
from __future__ import print_function, division
import numpy as np
import os
import sys

cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(1, os.path.join(cur_path, ".."))

from lib.utils.mkdir_if_missing import mkdir_if_missing
from lib.render_glumpy.render_py import Render_Py
import lib.pair_matching.RT_transform as se3
import cv2
import random

random.seed(2333)
np.random.seed(2333)
from tqdm import tqdm

LM6d_occ_dsm_root = os.path.join(
    cur_path, "..", "data/LINEMOD_6D/LM6d_converted/LM6d_occ_dsm"
)
image_set_dir = os.path.join(LM6d_occ_dsm_root, "image_set")
observed_set_dir = os.path.join(LM6d_occ_dsm_root, "image_set/observed")
idx2class = {
    1: "ape",
    5: "can",
    6: "cat",
    8: "driller",
    9: "duck",
    10: "eggbox",
    11: "glue",
    12: "holepuncher",
}
classes = idx2class.values()
classes = sorted(classes)

def class2idx(class_name, idx2class=idx2class):
    for k, v in idx2class.items():
        if v == class_name:
            return k

rendered_pose_dir = os.path.join(LM6d_occ_dsm_root, "ds_rendered_poses")
# output dir
rendered_root_dir = os.path.join(LM6d_occ_dsm_root, "data/rendered")
mkdir_if_missing(rendered_root_dir)

# config for renderer
width = 640
height = 480
K = np.array([[572.4114, 0, 325.2611], [0, 573.57043, 242.04899], [0, 0, 1]])
ZNEAR = 0.25
ZFAR = 6.0
depth_factor = 1000

# output_path
version = "v1"

def main():
    gen_images = True
    for class_idx, class_name in enumerate(tqdm(classes)):
        train_pair = []
        print("start ", class_name)
        if class_name in ["__back_ground__"]:
            continue

        if gen_images:
            # init render machine
            model_dir = os.path.join(
                cur_path,
                "../data/LINEMOD_6D/LM6d_converted/models/{}".format(class_name),
            )
            render_machine = Render_Py(model_dir, K, width, height, ZNEAR, ZFAR)

        for set_type in ["NDtrain"]:
            # observed index list
            observed_list_path = os.path.join(
                observed_set_dir, "NDtrain_observed_{}.txt".format(class_name)
            )
            with open(observed_list_path, "r") as f:
                observed_list = [x.strip() for x in f.readlines()]

            # rendered poses
            rendered_pose_path = os.path.join(
                rendered_pose_dir,
                "LM6d_occ_dsm_{}_NDtrain_rendered_pose_{}.txt".format(
                    version, class_name
                ),
            )
            with open(rendered_pose_path, "r") as f:
                str_rendered_pose_list = [x.strip().split(" ") for x in f.readlines()]
            rendered_pose_list = np.array(
                [[float(x) for x in each_pose] for each_pose in str_rendered_pose_list]
            )

            rendered_per_observed = 1
            assert len(rendered_pose_list) == 1 * len(observed_list), "{} vs {}".format(
                len(rendered_pose_list), len(observed_list)
            )

            for idx, observed_index in enumerate(tqdm(observed_list)):
                video_name, observed_prefix = observed_index.split("/")  # ./prefix
                rendered_dir = os.path.join(rendered_root_dir, video_name)
                mkdir_if_missing(rendered_dir)
                rendered_dir = os.path.join(rendered_dir, class_name)
                mkdir_if_missing(rendered_dir)

                for inner_idx in range(rendered_per_observed):
                    if gen_images:
                        image_file = os.path.join(
                            rendered_dir,
                            "{}_{}-color.png".format(observed_prefix, inner_idx),
                        )
                        depth_file = os.path.join(
                            rendered_dir,
                            "{}_{}-depth.png".format(observed_prefix, inner_idx),
                        )
                        # if os.path.exists(image_file) and os.path.exists(depth_file):
                        #     continue
                        rendered_idx = idx * rendered_per_observed + inner_idx
                        pose_rendered_q = rendered_pose_list[rendered_idx]

                        rgb_gl, depth_gl = render_machine.render(
                            pose_rendered_q[:4], pose_rendered_q[4:]
                        )
                        rgb_gl = rgb_gl.astype("uint8")

                        depth_gl = (depth_gl * depth_factor).astype(np.uint16)

                        cv2.imwrite(image_file, rgb_gl)
                        cv2.imwrite(depth_file, depth_gl)

                        pose_rendered_file = os.path.join(
                            rendered_dir,
                            "{}_{}-pose.txt".format(observed_prefix, inner_idx),
                        )
                        text_file = open(pose_rendered_file, "w")
                        text_file.write("{}\n".format(class2idx(class_name)))
                        pose_rendered_m = np.zeros((3, 4))
                        pose_rendered_m[:, :3] = se3.quat2mat(pose_rendered_q[:4])
                        pose_rendered_m[:, 3] = pose_rendered_q[4:]
                        pose_ori_m = pose_rendered_m
                        pose_str = "{} {} {} {}\n{} {} {} {}\n{} {} {} {}".format(
                            pose_ori_m[0, 0],
                            pose_ori_m[0, 1],
                            pose_ori_m[0, 2],
                            pose_ori_m[0, 3],
                            pose_ori_m[1, 0],
                            pose_ori_m[1, 1],
                            pose_ori_m[1, 2],
                            pose_ori_m[1, 3],
                            pose_ori_m[2, 0],
                            pose_ori_m[2, 1],
                            pose_ori_m[2, 2],
                            pose_ori_m[2, 3],
                        )
                        text_file.write(pose_str)

                    train_pair.append(
                        "{} {}/{}_{}".format(
                            observed_index, class_name, observed_prefix, inner_idx
                        )
                    )

            pair_set_file = os.path.join(
                image_set_dir, "train_{}.txt".format(class_name)
            )
            train_pair = sorted(train_pair)
            with open(pair_set_file, "w") as text_file:
                for x in train_pair:
                    text_file.write("{}\n".format(x))

        print(class_name, " done")

def check_observed_rendered():
    from lib.utils.utils import read_img
    import matplotlib.pyplot as plt

    observed_dir = os.path.join(LM6d_occ_dsm_root, "data/observed")

    for class_idx, class_name in enumerate(tqdm(classes)):
        if class_name != "driller":
            continue
        print(class_name)
        observed_list_path = os.path.join(
            observed_set_dir, "NDtrain_observed_{}.txt".format(class_name)
        )
        with open(observed_list_path, "r") as f:
            observed_list = [x.strip() for x in f.readlines()]
        for idx, observed_index in enumerate(observed_list):
            print(observed_index)
            prefix = observed_index.split("/")[1]
            color_observed = read_img(
                os.path.join(observed_dir, prefix + "-color.png"), 3
            )
            color_rendered = read_img(
                os.path.join(rendered_root_dir, class_name, prefix + "_0-color.png"), 3
            )
            fig = plt.figure()
            plt.axis("off")
            plt.subplot(1, 2, 1)
            plt.imshow(color_observed[:, :, [2, 1, 0]])
            plt.axis("off")

            plt.subplot(1, 2, 2)
            plt.imshow(color_rendered[:, :, [2, 1, 0]])
            plt.axis("off")

            plt.show()

if __name__ == "__main__":
    main()
    # check_observed_rendered()

ext: py

reason: import numpy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import io
from numpy.testing import assert_approx_equal, assert_allclose, assert_array_equal
from nose.tools import assert_equal, assert_true, assert_false, assert_greater, assert_less
import cv2
from cv_algorithms.contours import *
import numpy as np

class TestContours(object):
    def testAreaFilter(self):
        "Test contour area filter"
        cnts = [np.asarray([[0,0],[1,0],[1,1],[0,1],[0,0]]), # area 1
                np.asarray([[0,0],[2,0],[2,2],[0,2],[0,0]]), # area 4
                ]
        # Min area
        assert_array_equal(cnts, filter_min_area(cnts, 0.5))
        assert_array_equal(cnts, filter_min_area(cnts, 1.0))
        assert_array_equal([cnts[1]], filter_min_area(cnts, 1.1))
        assert_array_equal([cnts[1]], filter_min_area(cnts, 2.0))
        assert_array_equal([cnts[1]], filter_min_area(cnts, 3.0))
        assert_array_equal([cnts[1]], filter_min_area(cnts, 4.0))
        assert_array_equal([], filter_min_area(cnts, 5.0))
        # Max area
        assert_array_equal([], filter_max_area(cnts, 0.5))
        assert_array_equal([cnts[0]], filter_max_area(cnts, 1.0))
        assert_array_equal([cnts[0]], filter_max_area(cnts, 1.1))
        assert_array_equal([cnts[0]], filter_max_area(cnts, 2.0))
        assert_array_equal([cnts[0]], filter_max_area(cnts, 3.0))
        assert_array_equal(cnts, filter_max_area(cnts, 4.0))
        assert_array_equal(cnts, filter_max_area(cnts, 5.0))

    def test_contour_mask(self):
        cnts = [np.asarray([[0,0],[1,0],[1,1],[0,1],[0,0]]), # area 1
                np.asarray([[0,0],[2,0],[2,2],[0,2],[0,0]]), # area 4
                ]
        # Test return shape
        img = np.zeros((10,10,3), dtype=np.uint8)
        assert_equal((10, 10), contour_mask(img, []).shape)
        assert_equal((10, 10), contour_mask(img, cnts[0]).shape)
        assert_equal((10, 10), contour_mask(img, cnts).shape)
        assert_equal((10, 10), contour_mask((10,10), []).shape)
        assert_equal((10, 10), contour_mask((10,10,3), []).shape)
        # Test return values
        res = contour_mask(img, cnts)
        assert_false((res == 255).all())
        assert_false((res == 0).all())
        # Test no contours
        res = contour_mask(img, [])
        assert_false((res == 255).all())
        assert_true((res == 0).all())

reason: import numpy,from numpy


text:
# 3p
import numpy as np
import torch
# project
from .wrappers import wrapper

class Env:
    def __init__(self):
        self.action_space = None

    def reset(self):
        pass

    def step(self):
        pass

class GymEnv(Env):
    def __init__(self, env, terminal_on_life_loss=True, noop_max=30, frame_skip=4,
                 num_stack=4, screen_size=(84, 84), grayscale=True, clip_reward=True):
        self.env = env
        self.one_dim_state = (len(self.env.observation_space.shape) == 1)
        if self.one_dim_state:
            self.env = wrapper(env, terminal_on_life_loss=False, noop_max=0, frame_skip=0,
                               num_stack=num_stack, screen_size=None, grayscale=False, clip_reward=False)
        else:
            self.env = wrapper(env, terminal_on_life_loss=terminal_on_life_loss, noop_max=noop_max, frame_skip=frame_skip,
                               num_stack=num_stack, screen_size=screen_size, grayscale=grayscale, clip_reward=clip_reward)
        self.action_space = env.action_space
        self.observation_space = env.observation_space

    def reset(self):
        self.env.close()
        state = self.env.reset()
        if self.one_dim_state:
            state = torch.from_numpy(np.array(state)).float()
        else:
            state = torch.from_numpy(np.array(state)[:, :, :, 0]).float()
        return state

    def step(self, action):
        next_state, reward, is_terminal, _ = self.env.step(action)
        if self.one_dim_state:
            next_state = torch.from_numpy(np.array(next_state)).float()
        else:
            next_state = torch.from_numpy(np.array(next_state)[:, :, :, 0]).float()
        return next_state, reward, is_terminal, _

    def render(self,  mode='human'):
        self.env.render(mode=mode)

    def __del__(self):
        self.env.close()

max_stars_repo_name: pvnieo/beating-atari

max_issues_repo_name: pvnieo/beating-atari

max_forks_repo_name: pvnieo/beating-atari


text:
import cv2
import numpy as np

#Setting up the webcam

wc = cv2.VideoCapture(0)
wc.open(0)
wc.set(3,640)       #width
wc.set(4,480)       #height
wc.set(10,150)

def empty(n):
    pass

#Creating a trackbar
cv2.namedWindow('Trackbars')
cv2.resizeWindow('Trackbars', 640, 240)

cv2.createTrackbar('Hue min', 'Trackbars', 0, 179, empty)
cv2.createTrackbar('sat min', 'Trackbars', 0, 255, empty)
cv2.createTrackbar('val min', 'Trackbars', 0, 255, empty)
cv2.createTrackbar('Hue max', 'Trackbars', 179, 179, empty)
cv2.createTrackbar('sat max', 'Trackbars', 255, 255, empty)
cv2.createTrackbar('val max', 'Trackbars', 255, 255, empty)

#Reading the trackbar values

while(wc.isOpened()):
  # Capture frame-by-frame
  ret, img = wc.read()

  if ret == True:
    img = cv2.flip(img,1)
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)                       #hue saturation value model, make it easy to make color adjustments
    h_min = cv2.getTrackbarPos('Hue min', 'Trackbars')
    h_max = cv2.getTrackbarPos('Hue max', 'Trackbars')
    s_min = cv2.getTrackbarPos('sat min', 'Trackbars')
    s_max = cv2.getTrackbarPos('sat max', 'Trackbars')
    v_min = cv2.getTrackbarPos('val min', 'Trackbars')
    v_max = cv2.getTrackbarPos('val max', 'Trackbars')

    lower = np.array([h_min, s_min, v_min])
    upper = np.array([h_max, s_max, v_max])
    mask = cv2.inRange(imgHSV, lower, upper)

    #to get the color
    imgResult = cv2.bitwise_and(img, img, mask = mask)

    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
    h_stack = np.hstack([img, mask, imgResult])

    cv2.imshow('Horizontal stacked', h_stack)

    # Press Q on keyboard to  exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break

  # Break the loop
  else:
    break

# When everything done, release the video capture object
wc.release()
# Closes all the frames
cv2.destroyAllWindows()

max_stars_repo_path: Invisible-Cloak/Color Picker.py

max_stars_repo_name: Shilpa-prog/Invisible-cloak

max_issues_repo_path: Invisible-Cloak/Color Picker.py

max_issues_repo_name: Shilpa-prog/Invisible-cloak

max_forks_repo_path: Invisible-Cloak/Color Picker.py

max_forks_repo_name: Shilpa-prog/Invisible-cloak


text:
import datetime
import numpy as np

from pynwb import NWBHDF5IO, NWBFile
from pynwb.core import DynamicTableRegion
from pynwb.device import Device
from pynwb.ecephys import ElectrodeGroup
from pynwb.file import ElectrodeTable as get_electrode_table
from pynwb.testing import TestCase, remove_test_file, AcquisitionH5IOMixin

from {{ cookiecutter.py_pkg_name }} import TetrodeSeries

def set_up_nwbfile():
    nwbfile = NWBFile(
        session_description='session_description',
        identifier='identifier',
        session_start_time=datetime.datetime.now(datetime.timezone.utc)
    )

    device = nwbfile.create_device(
        name='device_name'
    )

    electrode_group = nwbfile.create_electrode_group(
        name='electrode_group',
        description='description',
        location='location',
        device=device
    )

    for i in np.arange(10.):
        nwbfile.add_electrode(
            x=i,
            y=i,
            z=i,
            imp=np.nan,
            location='location',
            filtering='filtering',
            group=electrode_group
        )

    return nwbfile

class TestTetrodeSeriesConstructor(TestCase):

    def setUp(self):
        """Set up an NWB file. Necessary because TetrodeSeries requires references to electrodes."""
        self.nwbfile = set_up_nwbfile()

    def test_constructor(self):
        """Test that the constructor for TetrodeSeries sets values as expected."""
        all_electrodes = self.nwbfile.create_electrode_table_region(
            region=list(range(0, 10)),
            description='all the electrodes'
        )

        data = np.random.rand(100, 3)
        tetrode_series = TetrodeSeries(
            name='name',
            description='description',
            data=data,
            rate=1000.,
            electrodes=all_electrodes,
            trode_id=1
        )

        self.assertEqual(tetrode_series.name, 'name')
        self.assertEqual(tetrode_series.description, 'description')
        np.testing.assert_array_equal(tetrode_series.data, data)
        self.assertEqual(tetrode_series.rate, 1000.)
        self.assertEqual(tetrode_series.starting_time, 0)
        self.assertEqual(tetrode_series.electrodes, all_electrodes)
        self.assertEqual(tetrode_series.trode_id, 1)

class TestTetrodeSeriesRoundtrip(TestCase):
    """Simple roundtrip test for TetrodeSeries."""

    def setUp(self):
        self.nwbfile = set_up_nwbfile()
        self.path = 'test.nwb'

    def tearDown(self):
        remove_test_file(self.path)

    def test_roundtrip(self):
        """
        Add a TetrodeSeries to an NWBFile, write it to file, read the file, and test that the TetrodeSeries from the
        file matches the original TetrodeSeries.
        """
        all_electrodes = self.nwbfile.create_electrode_table_region(
            region=list(range(0, 10)),
            description='all the electrodes'
        )

        data = np.random.rand(100, 3)
        tetrode_series = TetrodeSeries(
            name='TetrodeSeries',
            description='description',
            data=data,
            rate=1000.,
            electrodes=all_electrodes,
            trode_id=1
        )

        self.nwbfile.add_acquisition(tetrode_series)

        with NWBHDF5IO(self.path, mode='w') as io:
            io.write(self.nwbfile)

        with NWBHDF5IO(self.path, mode='r', load_namespaces=True) as io:
            read_nwbfile = io.read()
            self.assertContainerEqual(tetrode_series, read_nwbfile.acquisition['TetrodeSeries'])

class TestTetrodeSeriesRoundtripPyNWB(AcquisitionH5IOMixin, TestCase):
    """Complex, more complete roundtrip test for TetrodeSeries using pynwb.testing infrastructure."""

    def setUpContainer(self):
        """ Return the test TetrodeSeries to read/write """
        self.device = Device(
            name='device_name'
        )

        self.group = ElectrodeGroup(
            name='electrode_group',
            description='description',
            location='location',
            device=self.device
        )

        self.table = get_electrode_table()  # manually create a table of electrodes
        for i in range(10):
            self.table.add_row(
                x=i,
                y=i,
                z=i,
                imp=np.nan,
                location='location',
                filtering='filtering',
                group=self.group,
                group_name='electrode_group'
            )

        all_electrodes = DynamicTableRegion(
            data=list(range(0, 10)),
            description='all the electrodes',
            name='electrodes',
            table=self.table
        )

        data = np.random.rand(100, 3)
        tetrode_series = TetrodeSeries(
            name='name',
            description='description',
            data=data,
            rate=1000.,
            electrodes=all_electrodes,
            trode_id=1
        )
        return tetrode_series

    def addContainer(self, nwbfile):
        """Add the test TetrodeSeries and related objects to the given NWBFile."""
        nwbfile.add_device(self.device)
        nwbfile.add_electrode_group(self.group)
        nwbfile.set_electrode_table(self.table)
        nwbfile.add_acquisition(self.container)

max_stars_repo_name: TheChymera/ndx-template

max_issues_repo_name: TheChymera/ndx-template

max_forks_repo_name: TheChymera/ndx-template


text:
import numpy as np

def readArray(filename):
    '''
    lees een door saveArray opgeslagen array weer uit

    arugmenten:
        filename: filenaam waar de data staat
    '''
    data = np.loadtxt(filename, unpack=False, delimiter=',')
    return data

if __name__ == '__main__':
    # module wordt uitgevoerd
    import matplotlib.pyplot as plt

    # data van 1 kanaal weergeven
    fn = 'meting_test1_1603898691.txt'
    data = readArray(fn)
    time = data[:, 0]
    plt.plot(time, data[:, 1])
    plt.show()

max_stars_repo_name: ddland/TIS-TN-python

max_issues_repo_name: ddland/TIS-TN-python

max_forks_repo_name: ddland/TIS-TN-python


text:
## Copyright (c) 2009, Scott D. Peckham
## January 7, 2009  (Converted from IDL)

#----------------------------------------------------------------------------

from numpy import *
import numpy

import idl_func
import tf_utils

#----------------------------------------------------------------------------
class rt3_file:
    def Open(RT3_file, nz=None, n_stacks=None, \
             READ=False, WRITE=False, VERBOSE=False, \
             nx=None, ny=None, RTI_file=None):

        #------------------------------------------------------------
        # NOTE:  nx and ny keywords are used only to return values.
        #------------------------------------------------------------
        n_params = 4 - [nz, n_stacks].count(None)
        unit = None
        _opt = (nz, n_stacks)
        def _ret():
            _optrv = zip(_opt, [nz, n_stacks])
            _rv = [unit]
            _rv += [_o[1] for _o in _optrv if _o[0] is not None]
            return tuple(_rv)

        READ = (READ or not(WRITE))

        #-------------------------------------
        # Construct RTI filename, if missing
        #-------------------------------------
        if (RTI_file is None):
            RTI_file = Get_RTI_Filename(RT3_file)

        if (VERBOSE):
            TF_Print('RT3_file = ' + RT3_file)
            TF_Print('Reading RTI_file: ' + RTI_file)

        #--------------------------
        # Read info from RTI file
        #--------------------------
        info = Read_RTI_File(RTI_file)
        nx = info.ncols
        ny = info.nrows
        byte_order = info.byte_order
        self.SWAP_ENDIAN = Not_Same_Byte_Order(byte_order)  ########

        #---------------------------------
        # Open RT3 file to read or write
        #---------------------------------
        if (WRITE):
            file_unit = open(RT3_file, 'wb')
        else:
            file_unit = open(RT3_file, 'rb')

        #-------------------------------------
        # How many (nx x ny x nz) stacks in
        # file assuming data type is FLOAT ?
        #-------------------------------------
        if (READ):
            temp = idl_func.fstat(file_unit)
            filesize = temp.size
            n_stacks = (filesize / (int32(4) * int32(nx) * int32(ny) * int32(nz)))
        else:
            filesize = int32(0)
            n_stacks = int32(1)

        return _ret()

    #  Open
    #-------------------------------------------------------------------

max_stars_repo_name: mintproject/topoflow

max_issues_repo_name: mintproject/topoflow

max_forks_repo_name: mintproject/topoflow


text:
from sklearn import svm
import numpy as np
import random

data_violent = range(1,130)
data_nonviolent = range(1,130)

def strong_shuffle(numbers):
	random.shuffle(numbers)
	random.shuffle(numbers)
	random.shuffle(numbers)

strong_shuffle(data_violent)
strong_shuffle(data_nonviolent)

X_train = []
Y_train = []

X_test = []
Y_test = []

for i in data_nonviolent[0:80]:
    try:
        file_name = 'violent_features_NON_VIOLENT/nonvio_'+str(i)+'.txt'
        file_obj = open(file_name,'r')
        vif = np.loadtxt(file_obj)
        if vif.shape[0] == 336:# avoiding hd videos
            continue
        X_train.append(vif)
        Y_train.append(0)
        file_obj.close()
    except:
        continue
for i in data_violent[0:80]:
    try:
        file_name = 'violent_features_VIOLENT/vio_'+str(i)+'.txt'
        file_obj = open(file_name,'r')
        vif = np.loadtxt(file_obj)
        if vif.shape[0] == 336:# avoiding hd videos
            continue
        X_train.append(vif)
        Y_train.append(1)
        file_obj.close()
    except:
        continue
for i in data_nonviolent[81:]:
    try:
        file_name = 'violent_features_NON_VIOLENT/nonvio_'+str(i)+'.txt'
        file_obj = open(file_name,'r')
        vif = np.loadtxt(file_obj)
        if vif.shape[0] == 336:# avoiding hd videos
            continue
        X_test.append(vif)
        Y_test.append(0)
        file_obj.close()
    except:
        continue
for i in data_violent[81:]:
    try:
        file_name = 'violent_features_VIOLENT/vio_'+str(i)+'.txt'
        file_obj = open(file_name,'r')
        vif = np.loadtxt(file_obj)
        if vif.shape[0] == 336:# avoiding hd videos
            continue
        X_test.append(vif)
        Y_test.append(1)
        file_obj.close()
    except:
        continue
		# train contains the training set and test contains the testing set
classifiers = {}
for each_feature in range(0,252):
    # generating classifier for each feature
    train_data_X = []
    train_data_Y = []
    i = 0
    for each_set in X_train:
        train_data_X.append(each_set[each_feature])
        train_data_Y.append(Y_train[i])
        i += 1
    clf = svm.SVC(kernel = 'linear')
    clf.fit(np.array(train_data_X).reshape(-1,1),np.array(train_data_Y).reshape(-1,1))
    classifiers[each_feature] = clf

#calculate error for each classifier

w = [float(1)/(2*80)]*252
for t in range(0,10):
	# normalize weights
    w = [x/sum(w) for x in w]
    errors = [0.0]*252
    for each_feature in range(0,252):
        clf = classifiers[each_feature]
        i = 0
        preds = []
        for each_set in X_test:
			test_data_X = each_set[each_feature]
			test_data_Y = Y_test[i]
			i += 1
			preds.append(clf.predict(test_data_X.reshape(1,-1))[0])
        i = 0
        for each_pred in preds:
            errors[each_feature] += w[each_feature] * abs(each_pred-Y_test[i])
            i += 1

    min_error_classifier_index = np.where(errors == np.amin(errors))

    min_error_classifier_index = min_error_classifier_index[0][0]
    beta_change = errors[min_error_classifier_index]/(1.0 - errors[min_error_classifier_index])
    # calculate predictions for further use
    for each_feature in range(0,252):
        clf = classifiers[each_feature]
        i = 0
        preds = []
        for each_set in X_test:
            test_data_X = each_set[each_feature]
            test_data_Y = Y_test[i]
            i += 1
            preds.append(clf.predict(test_data_X.reshape(1,-1))[0])
        i = 0
    # update weights
        for each_set in X_test:
			print w[each_feature]
			w[each_feature] = w[each_feature] * (beta_change ** (1 - (abs(preds[i]-Y_test[i]))))
			i += 1
			print w[each_feature]

max_stars_repo_name: sekharkaredla/ASAGS


text:
import pickle
import csv
import numpy as np
import networkx as nx
from multiprocessing import Pool
from matplotlib import pyplot as plt
from matplotlib_venn import venn2, venn3, venn2_circles, venn3_circles

from altProts_in_communities.utils import is_alt

def get_bioplex_network(baits=None):
    G_b = nx.Graph()
    with open('BioPlex_interactionList_v4a.tsv', 'r') as f:
        for n,l in enumerate(f):
            ls = l.strip().split('\t')
            if n==0:
                keys = ls
                continue
            line = dict(zip(keys, ls))
            if baits is not None:
                if line['SymbolA'] in baits or line['SymbolB'] in baits:
                    G_b.add_edge(line['SymbolA'], line['SymbolB'])
            else:
                G_b.add_edge(line['SymbolA'], line['SymbolB'])

    G_b_edges = set()
    for e in G_b.edges:
        G_b_edges.add(frozenset(e))
    return G_b, G_b_edges

G_b, G_b_edges = get_bioplex_network()

def get_BP3_network(file_path):
    bp3_net = set()
    with open(file_path, 'r') as f:
        reader = csv.reader(f, delimiter='\t', quotechar='"')
        for n,l in enumerate(reader):
            if n==0:
                keys = l
                continue
            line = dict(zip(keys, l))
            bp3_net.add(frozenset((line['SymbolA'], line['SymbolB'])))
    return bp3_net

bp3_net = get_BP3_network('BioPlex_293T_Network_10K_Dec_2019.tsv')

class ThresholdSelect:
    def __init__(self, features_pkl_path, BP_edges, thresholds):
        self.predictions = pickle.load(open(features_pkl_path, 'rb'))
        self.BP_edges = BP_edges # set of frozensets
        self.thresholds = thresholds

    def draw(self):
        scores = [x for x in self.scores if type(x)==tuple]
        jacc, recall, precision, fscore = zip(*self.scores)
        thresholds = self.thresholds[:len(self.scores)]

        fig, ax = plt.subplots(figsize=(5,5))
        ax.plot(thresholds, fscore, label='Fscore', color='#7A3350')
        ax.plot(thresholds, precision,  label='precision', color='k')
        ax.plot(thresholds, recall, label='recall', color='#093E63')
        ax.plot(thresholds, jacc, label='Jaccard', color='#E4A737')
        ax.set_xlabel('NB score threshold')
        ax.set_ylabel('scores')
        plt.axvline(0.045, c='gray', linestyle='dashed')
        plt.legend()
        plt.show()

    def explore_thresholds(self):
        scores = []
        with Pool(8) as p:
            scores = p.map(self.compute_metrics_at_thres, self.thresholds)
        self.scores = scores

    def compute_recall(self, HCIP, ref_edges_set):
        TP = HCIP.intersection(ref_edges_set)
        FN = ref_edges_set.difference(HCIP)
        return len(TP)/(len(TP) + len(FN))

    def compute_precision(self, HCIP, ref_edges_set):
        TP = HCIP.intersection(ref_edges_set)
        FP = HCIP.difference(ref_edges_set)
        if len(TP) + len(FP) == 0:
            return 0.
        return len(TP)/(len(TP) + len(FP))

    def compute_f1(self, HCIP, ref_edges_set):
        recall    = self.compute_recall(HCIP, ref_edges_set)
        precision = self.compute_precision(HCIP, ref_edges_set)
        if recall + precision == 0 :
            return 0.
        return (2 * recall * precision) / (recall + precision)

    def compute_metrics_at_thres(self, thres):
        HCIP = []
        for pred in self.predictions:
            batch, bait, prey, target, pnoint_pint = pred
            if bait==prey:continue
            if pnoint_pint[1] < thres: continue # select only interactions over pint threshold of input thres
            HCIP.append(pred)

        HCIP_net = []
        for batch, bait, prey, target, pnoint_pint in HCIP:
            HCIP_net.append((bait, prey, pnoint_pint[1]))

        G_o_noalt = nx.Graph()
        G_o_noalt.add_edges_from([(x[0], x[1]) for x in HCIP_net if not is_alt(x[1])])
        G_o_noalt.remove_edges_from(nx.selfloop_edges(G_o_noalt))
        G_o_edges = set(frozenset(e) for e in G_o_noalt.edges)
        intersection = G_o_edges.intersection(self.BP_edges)
        union = G_o_edges.union(self.BP_edges)

        jacc      = len(intersection)/len(union)
        recall    = self.compute_recall(G_o_edges, self.BP_edges)
        precision = self.compute_precision(G_o_edges, self.BP_edges)
        fscore    = self.compute_f1(G_o_edges, self.BP_edges)

        return jacc, recall, precision, fscore

def tt_type_node_attrs(G, HCIP_baits):
    node_attrs = {}
    for n in G.nodes():
        node_attrs[n] = {}
        if is_alt(n):
            node_attrs[n]['tt_type'] = 'alt'
            node_attrs[n]['node_type'] = 'prey'
        else:
            node_attrs[n]['tt_type'] = 'ref'
            if n in HCIP_baits:
                node_attrs[n]['node_type'] = 'bait'
            else:
                node_attrs[n]['node_type'] = 'prey'

    return node_attrs

reason: import numpy,import networkx


text:
import html2text
import re
import sys
# import numpy as np
import pandas as pd

from training_description.database_queries import DatabaseQueries
from training_description.cleaning import TrainingDescriptionCleaner

if __name__ == '__main__':
    USER_NAME = 'eduardo oliveira'
    queries = DatabaseQueries()
    raw_descriptions, planner = queries.describe_descriptions_by_name(USER_NAME)
    training_description_cleaner = TrainingDescriptionCleaner()
    cleaned_description = training_description_cleaner.clean_training_descriptions(raw_descriptions, planner)
    # print('cleaned_description: ', cleaned_description)


text:
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

def tsne(matrix,
      perplexity = 60, n_iter = 2000, n_components = 3, ):
      tsne = TSNE(
            n_components= n_components, verbose=1, perplexity=perplexity, n_iter=n_iter)
      return tsne.fit_transform(matrix)

def pca(data, pca_dim = 128 ):
      from sklearn.decomposition import PCA
      pca = PCA(n_components=pca_dim)
      X_pca = pca.fit_transform(data)
      return X_pca

import plotly.plotly as py
import plotly.graph_objs as go
import plotly
import random

def plotly_scatter(plot_name, labels, latent_representation, take_first_n = 10000):
      plotly.tools.set_credentials_file(username='mrnood', api_key='2o0NGhHfo4RXCJudQ3Mi')

      #latent_representation = np.array(latent_representation)
      #latent_representation = tsne(latent_representation)

      unique_labels = sorted(list(set(labels)))
      large_gene_symbol = []
      for l in unique_labels:
            all_l_indices = [i for i,x in enumerate(labels) if x == l]
            large_gene_symbol.append(all_l_indices)

      l = []
      N= len(unique_labels)
      c= ['hsl('+str(h)+',50%'+',40%)' for h in np.linspace(0, 360, N)]
      c2 = ['hsl('+str(h)+',50%'+',30%)' for h in np.linspace(0, 360, N)]
      c3 = ['hsl('+str(h)+',40%'+',60%)' for h in np.linspace(0, 360, N)]

      palette = [c2, c, c3]
      c = 0
      for unique_label, all_l_indices in zip(unique_labels, large_gene_symbol):
            cur_palette = c % 3
            print(palette[cur_palette][c])
            trace0 = go.Scatter3d(
                  x=latent_representation[all_l_indices, 0],
                  y=latent_representation[all_l_indices, 1],
                  z=latent_representation[all_l_indices, 2],
                  mode='markers',
                  marker=dict(size=6,
                              #colorscale='jet',  # 'Viridis',
                              #line= dict(width=1),
                              color=palette[cur_palette][c],
                              #[unique_labels_filtered.index(unique_label)],
                              #opacity=0.75,
                              ), name=unique_label,)
            #text= unique_label) # The hover text goes here...
            l.append(trace0)
            c += 1
      layout = go.Layout(
                    scene = dict(
                    xaxis = dict(
                        #nticks=4,
                        #gridwidth=4,
                        title = '',
                        showgrid=False,
                        showline=False,
                        #zeroline=False,
                        showticklabels=False,
                         ),
                    yaxis = dict(
                        #nticks=4,
                        title = '',
                        showgrid=False,
                        #gridwidth=2,
                        showline=False,
                        #zeroline=False,
                        showticklabels=False,
                         ),
                    zaxis = dict(
                        #nticks=4,
                        title = '',
                        gridwidth=2,
                        showgrid = False,
                        showline=False,
                        #zeroline=False,
                        showticklabels=False,
                        ),
                        ),
                  height = 1000,
                  width=1200,
                  margin=dict(
                  r=0, l=0,
                  b=0, t=0),
                  showlegend= True,
                  )

      fig= go.Figure(data=l, layout=layout)
      py.iplot(fig)

def plot3d(plot_name, labels, latent_representation, take_first_n = 10000,
      perplexity = 40, n_iter = 2000, n_components = 2):

      ''' list int, str labels: color point according to this color
          np.ndarray tsne_projection: [ Num of sequences X Dim of Latent Rep. ]
          take_first_n: only first n row will show in tsne projection
      '''
      assert len(labels) == latent_representation.shape[0], \
      '%s != %s ' %(len(labels),  latent_representation.shape[0])

      if take_first_n > len(labels):
            take_first_n = len(labels)

      labels = labels[:take_first_n]
      latent_representation = latent_representation[:take_first_n, :]

      tsne_projection = tsne(latent_representation)

      # color by label,
      unique_labels = sorted(list(set(labels)))
      colors = [ unique_labels.index(l) for l in labels]

      # plot only gene symbols with >40 RRMs
      large_gene_symbol, unique_labels_filtered = [], []
      for l in unique_labels:
            all_l_indices = [i for i,x in enumerate(labels) if x == l]
            if len(all_l_indices) < 20:
                  continue
            large_gene_symbol.append(all_l_indices)
            unique_labels_filtered.append(l)

      fig = plt.figure(figsize=(10, 8))
      ax = fig.add_subplot(111, projection='3d')

      colors = iter(cm.jet(np.linspace(0, 1, len(large_gene_symbol))))
      for l, all_l_indices in zip(unique_labels_filtered, large_gene_symbol):
            cur_color = next(colors)
            ax.scatter(
                  tsne_projection[all_l_indices,0],
                  tsne_projection[all_l_indices,1],
                  tsne_projection[all_l_indices,2],
                  c= cur_color, label=l, s = 30)

      ax.legend(fontsize=15)
      #ax = plt.gca()
      #legend = ax.get_legend()
      # ax.set_zlim(-10, 10)
      # ax.set_ylim(-10, 10)
      plt.title(plot_name, fontsize=18)
      # ax.set_xlim(-10, 10)

      plt.savefig(plot_name, bbox_inches='tight')
      plt.show()
      return fig

def hist(hist_name, array, bins = 100, show = True, ):
      plt.hist(np.array(array), bins)
      plt.title(hist_name)

      if show:
            plt.show()
            return

      plt.savefig('./CharAE_Cho/activations/%s' %(hist_name))
      plt.clf()

def plot(plot_name, labels, latent_representation, take_first_n = 10000,
      n_iters=2000, perplexity=40):

      ''' list int, str labels: color point according to this color
          np.ndarray tsne_projection: [ Num of sequences X Dim of Latent Rep. ]
          take_first_n: only first n row will show in tsne projection
      '''
      from mpl_toolkits.mplot3d import Axes3D
      assert len(labels) == latent_representation.shape[0], \
      '%s != %s ' %(len(labels),  latent_representation.shape[0])

      if take_first_n > len(labels):
            take_first_n = len(labels)

      labels = labels[:take_first_n]
      latent_representation = latent_representation[:take_first_n, :]

      tsne_projection = tsne(latent_representation, n_components = 3)

      # color by label,
      unique_labels = sorted(list(set(labels)))
      colors = [ unique_labels.index(l) for l in labels]

      large_gene_symbol = []
      unique_labels_filtered = []
      for l in unique_labels:
            all_l_indices = [i for i,x in enumerate(labels) if x == l]
            if len(all_l_indices) < 20:
                  continue
            large_gene_symbol.append(all_l_indices)
            unique_labels_filtered.append(l)

      fig = plt.figure(figsize=(20, 20))
      ax = fig.add_subplot(111)
      colors = iter(cm.jet(np.linspace(0, 1, len(large_gene_symbol))))
      for l, all_l_indices in zip(unique_labels_filtered, large_gene_symbol):

            cur_color = next(colors)
            plt.scatter(
                  tsne_projection[all_l_indices,0],
                  tsne_projection[all_l_indices,1],
                  c= cur_color, label=l, s = 50)

      ax.legend(fontsize=16)
      plt.title(plot_name, fontsize=25)
      #legend = ax.get_legend()

      plt.savefig(plot_name, bbox_inches='tight')
      plt.show()

max_stars_repo_name: ostwind/ProteinLatentRep

max_issues_repo_name: ostwind/ProteinLatentRep

max_forks_repo_name: ostwind/ProteinLatentRep


text:
from networks import VggDepthEstimator
from LKVOLearner import FlipLR
import torch
from torch.autograd import Variable
import os
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import argparse

"""
CUDA_VISIBLE_DEVICES=1 nice -10 python3 testKITTI.py --dataset_root /newfoundland/chaoyang/kitti --ckpt_file /home/chaoyang/LKVOLearner/checkpoints/checkpoints_19_416_scratch/9_model.pth --test_file_list
"""
parser = argparse.ArgumentParser()

parser.add_argument("--dataset_root", type=str, default="/newfoundland/chaoyang/kitti", help="dataset root path")
parser.add_argument("--test_file_list", type=str, default="/newfoundland/chaoyang/SfMLearner/data/kitti/test_files_eigen.txt", help="test file list")
parser.add_argument("--ckpt_file", type=str, default=None, help="checkpoint file")
parser.add_argument("--output_path", type=str, default="pred_depths", help="output path")
parser.add_argument("--use_pp", default=False, action="store_true", help='use post processing')

FLAGS = parser.parse_args()

# dataset_root = "/newfoundland/chaoyang/kitti"
# model_path = "/home/chaoyang/LKVOLearner/checkpoints_new/12_model.pth"
# test_file_list = "/newfoundland/chaoyang/SfMLearner/data/kitti/test_files_eigen.txt"
dataset_root = FLAGS.dataset_root
model_path = FLAGS.ckpt_file
test_file_list = FLAGS.test_file_list
output_path = FLAGS.output_path

img_size = [128, 416]
vgg_depth_net = VggDepthEstimator(img_size)
vgg_depth_net.load_state_dict(torch.load(model_path))
vgg_depth_net.cuda()

fliplr = FlipLR(imW=img_size[1], dim_w=2).cuda()

def read_text_lines(file_path):
    f = open(file_path, 'r')
    lines = f.readlines()
    f.close()
    lines = [l.rstrip() for l in lines]
    return lines

test_files = read_text_lines(test_file_list)
pred_depths = []
i = 0
for filename in test_files:
    print(i)
    filename = filename.split()[0]
    im_path = os.path.join(dataset_root, filename)
    img_pil = Image.open(im_path).resize((img_size[1], img_size[0]), Image.ANTIALIAS)
    # img_pil.save('kitti_test_images/%04d.png'%(i))
    img = np.array(img_pil)
    # print(img.shape)
    img = img.transpose(2, 0, 1)
    # print(img.shape)
    print(filename)
    img_var = Variable(torch.from_numpy(img).float().cuda(), volatile=True)

    if FLAGS.use_pp:
        # flip image
        img_vars = (torch.cat((fliplr(img_var).unsqueeze(0), img_var.unsqueeze(0)), 0)-127)/127
        pred_depth_pyramid = vgg_depth_net.forward(img_vars)
        depth = pred_depth_pyramid[0]
        print(depth.size())
        depth_mean = (fliplr(depth[0:1, :, :]) + depth[1:2, :, :])*.5
        depth_mean.save(output_path + '/%4d.png'%(i))
        pred_depths.append(depth_mean.data.cpu().squeeze().numpy())

        # compute mean
    else:
        pred_depth_pyramid = vgg_depth_net.forward((img_var.unsqueeze(0)-127)/127)
        pred_depth_pyramid[0].save(output_path + '/%4d.png' % (i))
        pred_depths.append(pred_depth_pyramid[0].data.cpu().squeeze().numpy())
    i = i+1
    # if i==3:
    #     break
pred_depths = np.asarray(pred_depths)
print(pred_depths.shape)
np.save(output_path, 1/pred_depths)
import scipy.io as sio
sio.savemat(output_path, {'D': pred_depths})
    # print(pred_depth_pyramid[0].size())
    # plt.imshow(pred_depth_pyramid[0].data.cpu().squeeze().numpy())
    # plt.show()

reason: import numpy,import scipy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 6 09:19:41 2021

@author: Naveen GOUTHAM
"""
import numpy as np

class CRPS:
    '''
    A class to compute the continuous ranked probability score (crps) (Matheson and Winkler, 1976; Hersbach, 2000), the fair-crps (fcrps) (Ferro et al., 2008), and the adjusted-crps (acrps) (Ferro et al., 2008) given an ensemble prediction and an observation.

    The CRPS is a negatively oriented score that is used to compare the empirical distribution of an ensemble prediction to a scalar observation.

    References:
        [1] Matheson, J. E. & Winkler, R. L. Scoring Rules for Continuous Probability Distributions. Management Science 22, 1087–1096 (1976).
        [2] Hersbach, H. Decomposition of the Continuous Ranked Probability Score for Ensemble Prediction Systems. Wea. Forecasting 15, 559–570 (2000).
        [3] Ferro, C. A. T., Richardson, D. S. & Weigel, A. P. On the effect of ensemble size on the discrete and continuous ranked probability scores. Meteorological Applications 15, 19–24 (2008).

    ----------

    Parameters:
        ensemble_members: numpy.ndarray
            The predicted ensemble members. They will be sorted in ascending order automatically.
            Ex: np.array([2.1,3.5,4.7,1.2,1.3,5.2,5.3,4.2,3.1,1.7])

        observation: float
            The observed value.
            Ex: 5.4

        adjusted_ensemble_size: int, optional
            The size the ensemble needs to be adjusted to before computing the Adjusted Continuous Ranked Probability Score.
            The default is 200.
            Note: The crps becomes equal to acrps when adjusted_ensemble_size equals the length of the ensemble_members.

    ----------

    Method(s):

        compute():
            Computes the continuous ranked probability score (crps), the fair-crps (fcrps), and the adjusted-crps (acrps).

    ----------

    Attributes:
        crps: Continuous Ranked Probability Score
            It is the integral of the squared difference between the CDF of the forecast ensemble and the observation.

            .. math::
            \mathrm{crps = \int_{-\infty}^{\infty} [F(y) - F_{o}(y)]^2 dy}

        fcrps: Fair-Continuous Ranked Probability Score
            It is the crps computed assuming an infinite ensemble size.

            .. math::
            \mathrm{fcrps = crps - \int_{-\infty}^{\infty} [F(y) (1 - F(y))/(m-1)] dy},

            where m is the current ensemble size (here: length of ensemble_members)

        acrps: Adjusted-Continuous Ranked Probability Score
            It is the crps computed assuming an ensemble size of M.

            .. math::
            \mathrm{acrps = crps - \int_{-\infty}^{\infty} [(1 - m/M) F(y) (1 - F(y))/(m-1)] dy},

            where M is the adjusted_ensemble_size

    ----------

    Demonstration:

    import numpy as np
	import CRPS.CRPS as pscore

	Example - 1:
	In [1]: pscore(np.arange(1,5),3.5).compute()
	Out[1]: (0.625, 0.4166666666666667, 0.42083333333333334)

	Example - 2:
	In [2]: crps,fcrps,acrps = pscore(np.arange(1,11),8.3,50).compute()
	In [3]: crps
	Out[3]: 1.6300000000000003
	In [4]: fcrps
	Out[4]: 1.446666666666667
	In [5]: acrps
	Out[5]: 1.4833333333333336

    '''
    def __init__(self,ensemble_members,observation,adjusted_ensemble_size=200):
        '''
        Parameters:
            ensemble_members: numpy.ndarray
                The predicted ensemble members.
                Ex: np.array([2.1,3.5,4.7,1.2,1.3,5.2,5.3,4.2,3.1,1.7])
            observation: float
                The observed value.
                Ex: 5.4
            adjusted_ensemble_size: int, optional
                The size the ensemble needs to be adjusted to before computing the Adjusted Continuous Ranked Probability Score.
                The default is 200.
                Note: The crps becomes equal to acrps when adjusted_ensemble_size equals the length of the ensemble_members.

        Returns
        -------
        None

        '''
        self.fc = np.sort(np.unique(ensemble_members))
        self.ob = observation
        self.__m = len(self.fc)
        self.M = int(adjusted_ensemble_size)
        self.__cdf_fc = None
        self.__cdf_ob = None
        self.__delta_fc = None
        self.crps = None
        self.fcrps = None
        self.acrps = None

    def __str__(self):
        "Kindly refer to the __doc__ method for documentation. i.e. print(CRPS.__doc__)."

    def compute(self):
        '''
        Returns
        -------
        crps, fair-crps, adjusted-crps

        '''
        if self.ob < self.fc[0]:
            self.__cdf_fc = np.linspace(0,(self.__m - 1)/self.__m,self.__m)
            self.__cdf_ob = np.ones(self.__m)
            all_mem = np.array([self.ob] + list(self.fc), dtype = object)
            self.__delta_fc = np.array([all_mem[n+1] - all_mem[n] for n in range(len(all_mem)-1)], dtype=object)

        elif self.ob > self.fc[-1]:
            self.__cdf_fc = np.linspace(1/self.__m,1,self.__m)
            self.__cdf_ob = np.zeros(self.__m)
            all_mem = np.array(list(self.fc) + [self.ob], dtype = object)
            self.__delta_fc = np.array([all_mem[n+1] - all_mem[n] for n in range(len(all_mem)-1)], dtype=object)

        elif self.ob in self.fc:
            self.__cdf_fc = np.linspace(1/self.__m,1,self.__m)
            self.__cdf_ob = (self.fc >= self.ob)
            all_mem = self.fc
            self.__delta_fc = np.array([all_mem[n+1] - all_mem[n] for n in range(len(all_mem)-1)] + list(np.zeros(1)), dtype=object)

        else:
            cdf_fc = []
            cdf_ob = []
            delta_fc = []
            for f in range(len(self.fc)-1):
                if (self.fc[f] < self.ob) and (self.fc[f+1] < self.ob):
                    cdf_fc.append((f+1)*1/self.__m)
                    cdf_ob.append(0)
                    delta_fc.append(self.fc[f+1] - self.fc[f])
                elif (self.fc[f] < self.ob) and (self.fc[f+1] > self.ob):
                    cdf_fc.append((f+1)*1/self.__m)
                    cdf_fc.append((f+1)*1/self.__m)
                    cdf_ob.append(0)
                    cdf_ob.append(1)
                    delta_fc.append(self.ob - self.fc[f])
                    delta_fc.append(self.fc[f+1] - self.ob)
                else:
                    cdf_fc.append((f+1)*1/self.__m)
                    cdf_ob.append(1)
                    delta_fc.append(self.fc[f+1] - self.fc[f])
            self.__cdf_fc = np.array(cdf_fc)
            self.__cdf_ob = np.array(cdf_ob)
            self.__delta_fc = np.array(delta_fc)

        self.crps = np.sum(np.array((self.__cdf_fc - self.__cdf_ob) ** 2)*self.__delta_fc)
        if self.__m == 1:
            self.fcrps = self.acrps = 'Not defined'
        else:
            self.fcrps = self.crps - np.sum(np.array(((self.__cdf_fc * (1 - self.__cdf_fc))/(self.__m-1))*self.__delta_fc))
            self.acrps = self.crps - np.sum(np.array((((1 - (self.__m/self.M)) * self.__cdf_fc * (1 - self.__cdf_fc))/(self.__m-1))*self.__delta_fc))
        return self.crps, self.fcrps, self.acrps


text:
"""
   Copyright 2019 Islam Elnabarawy

   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.
"""
# References:
# [1] R. Xu and D. C. Wunsch II, "BARTMAP: A viable structure for biclustering,"
#     Neural Networks, vol. 24, no. 7, pp. 709-716, 2011.
# [2] I. Elnabarawy, D. C. Wunsch II, and A. M. Abdelbar, "Biclustering ARTMAP
#     Collaborative Filtering Recommender System," in Proceedings of the 2016 International
#     Joint Conference on Neural Networks (IJCNN ’16), 2016, pp. 2986-2991.

import multiprocessing
import random

import numpy as np
from sklearn import preprocessing

from nuart.common.linear_algebra import fuzzy_and, max_norm

__author__ = 'Islam Elnabarawy'

class FuzzyARTModule(object):
    def __init__(self, rho, alpha, beta, num_features):
        self.rho = rho
        self.alpha = alpha
        self.beta = beta

        self.num_clusters = 0
        self.num_features = num_features
        self.w = np.ones((self.num_clusters, self.num_features * 2))

    def train_dataset(self, dataset, max_epochs=np.inf, shuffle=False, random_seed=None):
        # complement-code the data
        dataset = np.concatenate((dataset, 1 - dataset), axis=1)

        # initialize variables
        labels = np.zeros(dataset.shape[0])
        iterations = 0
        w_old = None

        indices = list(range(dataset.shape[0]))

        if shuffle:
            if random_seed is not None:
                random.seed(random_seed)
            random.shuffle(indices)

        while not np.array_equal(self.w, w_old) and iterations < max_epochs:
            w_old = self.w
            for ix in indices:
                labels[ix] = self.train_pattern(dataset[ix, :])
            iterations += 1

        return labels, iterations

    def train_pattern(self, pattern):
        # evaluate the pattern to get the winning category
        winner = self.eval_pattern(pattern)

        # commit the pattern to the winning category
        self.commit_pattern(pattern, winner)

        return winner

    def commit_pattern(self, pattern, category):
        # check if the uncommitted node was the winner
        if (category + 1) > self.num_clusters:
            self.num_clusters += 1
            self.w = np.concatenate((self.w, np.ones((1, self.w.shape[1]))))

        # update the weight of the winning neuron
        self.w[category, :] = self.weight_update(pattern, self.w[category, :], self.beta)

    def eval_pattern(self, pattern):
        # initialize variables
        matches = np.zeros(self.num_clusters)

        # calculate the category match values
        for jx in range(self.num_clusters):
            matches[jx] = self.category_choice(pattern, self.w[jx, :], self.alpha)

        # pick the winning category
        match_attempts = 0

        while match_attempts < self.num_clusters:
            # winner-take-all selection
            winner = np.argmax(matches)

            # vigilance test
            if self.vigilance_check(pattern, self.w[winner, :], self.rho):
                # the winning category passed the vigilance test
                return winner
            else:
                # shut off this category from further testing
                matches[winner] = 0
                match_attempts += 1

        return self.num_clusters

    @staticmethod
    def category_choice(pattern, category_w, alpha):
        return max_norm(fuzzy_and(pattern, category_w)) / (alpha + max_norm(category_w))

    @staticmethod
    def vigilance_check(pattern, category_w, rho):
        return max_norm(fuzzy_and(pattern, category_w)) >= rho * max_norm(pattern)

    @staticmethod
    def weight_update(pattern, category_w, beta):
        return beta * fuzzy_and(pattern, category_w) + (1 - beta) * category_w

class BARTMAP(object):
    def __init__(self, arta_settings, artb_settings, corr_thresh, step_size):
        """
        Create a Biclustering ARTMAP object

        :param artb_settings: A 3-tuple containing the rho, alpha, and beta parameters of ARTa
        :param arta_settings: A 3-tuple containing the rho, alpha, and beta parameters of ARTb
        :param corr_thresh: A float specifying the correlation threshold to use for BARTMAP's inter-ART module
        :param step_size: The step size parameter for BARTMAP's inter-ART module
        """
        super(BARTMAP, self).__init__()

        self.arta_settings = arta_settings
        self.artb_settings = artb_settings
        self.corr_thresh = corr_thresh
        self.step_size = step_size

        self.num_samples = None
        self.num_features = None

        self.ARTa = None
        self.ARTb = None

        self.sample_labels = None
        self.num_sample_labels = 0

        self.feature_labels = None
        self.num_feature_labels = 0

        self.map = map

    def train(self, data):
        sample_data = preprocessing.MinMaxScaler().fit_transform(data)
        feature_data = preprocessing.MinMaxScaler().fit_transform(data.transpose())

        return self.train_preprocessed(sample_data, feature_data)

    def train_preprocessed(self, sample_data, feature_data):

        pool = multiprocessing.Pool()
        self.map = pool.map

        self.num_samples, self.num_features = sample_data.shape
        self.ARTa = FuzzyARTModule(*self.arta_settings, self.num_features)
        self.ARTb = FuzzyARTModule(*self.artb_settings, self.num_samples)

        self.feature_labels, _ = self.ARTb.train_dataset(feature_data)
        self.num_feature_labels = self.ARTb.num_clusters

        self.sample_labels = np.zeros(self.num_samples, dtype=np.int32)
        self.num_sample_labels = 0

        for ix in range(self.num_samples):
            # re-initialize the ARTa vigilance parameter for each sample
            self.ARTa.rho = self.arta_settings[0]

            sample = np.concatenate([sample_data[ix, :], 1 - sample_data[ix, :]], axis=0)

            while True:
                sample_category = self.ARTa.eval_pattern(sample)

                if sample_category == self.ARTa.num_clusters:
                    # new cluster created; always allow new clusters
                    self.ARTa.commit_pattern(sample, sample_category)
                    self.sample_labels[ix] = sample_category
                    self.num_sample_labels += 1
                    break
                else:
                    # the sample was assigned to an existing cluster; check correlation threshold
                    sample_cluster = sample_data[np.nonzero(self.sample_labels[:ix] == sample_category)]
                    correlations = np.array([
                        self.get_bicluster_correlations(jx, sample, sample_cluster)
                        for jx in range(self.num_feature_labels)
                    ])

                    # check the correlations against the threshold
                    if np.any(correlations > self.corr_thresh) or self.ARTa.rho >= 1:
                        # allow this sample to be committed into the bicluster
                        self.ARTa.commit_pattern(sample, sample_category)
                        self.sample_labels[ix] = sample_category
                        break
                    else:
                        # increase the ARTa vigilance threshold and try again
                        self.ARTa.rho += self.step_size
                        if self.ARTa.rho > 1:
                            self.ARTa.rho = 1

        pool.close()
        self.map = map

    def get_bicluster_correlations(self, jx, sample, sample_cluster):
        feature_ix = np.nonzero(self.feature_labels == jx)
        return self.get_average_correlation(sample_cluster[:, feature_ix], sample[feature_ix])

    def get_average_correlation(self, bicluster, sample):
        # compute the average of the correlation between each pair of samples
        return np.array(list(self.map(BARTMAP.get_correlation_args, [(row, sample) for row in bicluster]))).mean()

    @staticmethod
    def get_correlation_args(args):
        return BARTMAP.get_correlation(*args)

    @staticmethod
    def get_correlation(x, y):
        # compute the terms for all the item values
        terms1 = x - np.mean(x)
        terms2 = y - np.mean(y)
        # compute the sums to find the pairwise correlation
        numerator = np.sum(np.multiply(terms1, terms2))
        root1 = np.sqrt(np.sum(np.multiply(terms1, terms1)))
        root2 = np.sqrt(np.sum(np.multiply(terms2, terms2)))
        return numerator / (root1 * root2) if root1 != 0 and root2 != 0 else 0

max_stars_repo_name: ACIL-Group/NuART-Py

max_issues_repo_name: ACIL-Group/NuART-Py

max_forks_repo_name: ACIL-Group/NuART-Py


text:
"""Example for the usage of the qr_composition package."""

import numpy as np

from qr_decomposition import qr_decomposition

# Set print options (optional)
np.set_printoptions(precision=4, suppress=True)

# Input matrix
A = np.array([[6, 5, 0], [5, 1, 4], [0, 4, 3]])

# Print input matrix
print(A)

# Compute QR decomposition using Givens rotation
(Q, R) = qr_decomposition.givens_rotation(A)

# Print orthogonal matrix Q
print(Q)

# Print upper triangular matrix R
print(R)


text:
import os
import cv2
import base64
import numpy as np
import onnxruntime as ort

file_path = os.path.abspath(__file__)
file_dir = os.path.dirname(file_path)
package_dir = os.path.dirname(file_dir)

clp_det_model = os.path.join(
    package_dir,
    'resources',
    'pretrained_models',
    'clp_det.onnx'
)

det_model = os.path.join(
    package_dir,
    'resources',
    'pretrained_models',
    'ch_mul_v2_c_det.onnx'
)
cls_model = os.path.join(
    package_dir,
    'resources',
    'pretrained_models',
    'ch_mul_m_cls.onnx'
)
rec_model = os.path.join(
    package_dir,
    'resources',
    'pretrained_models',
    'clp_v2_c_rec.onnx'
)
rec_char_dict_path = os.path.join(
    package_dir,
    'resources',
    'char_dicts',
    'clp_dict.txt'
)

def preprocess(img):
    img = img.astype('float32')
    img -= (104, 117, 123)
    img = img.transpose(2, 0, 1)
    img = img[None, ...]
    return img

def str2providers(str):
    available_providers = ort.get_available_providers()

    if str.lower() == 'auto':
        return available_providers

    providers_dict = {
        provider.lower(): provider
        for provider in available_providers
    }

    provider_strs = [(provider_str + 'ExecutionProvider').lower()
                     for provider_str in str.split(',')]

    select_providers = [
        providers_dict[provider_str] for provider_str in provider_strs
        if provider_str in providers_dict.keys()
    ]

    if len(select_providers) == 0:
        select_providers = available_providers

    return select_providers

def load_onnx(model_path, providers='auto'):
    providers = str2providers(providers)
    sess_options = ort.SessionOptions()
    if 'DmlExecutionProvider' in providers:
        sess_options.enable_mem_pattern = False
    session = ort.InferenceSession(model_path, sess_options=sess_options)
    inputs_name = [input.name for input in session.get_inputs()]
    outputs_name = [output.name for output in session.get_outputs()]
    return session, inputs_name, outputs_name

def gen_scale(h, w, num=2):
    return np.array([w, h] * num)

def nms(dets, thresh):
    """Pure Python NMS baseline."""
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)

        inds = np.where(ovr <= thresh)[0]
        order = order[inds + 1]

    return keep

def decode(loc, priors, variances):
    """Decode locations from predictions using priors to undo
    the encoding we did for offset regression at train time.
    Args:
        loc (tensor): location predictions for loc layers,
            Shape: [num_priors,4]
        priors (tensor): Prior boxes in center-offset form.
            Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes
    Return:
        decoded bounding box predictions
    """

    boxes = np.concatenate((
        priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:],
        priors[:, 2:] * np.exp(loc[:, 2:] * variances[1])), 1)
    boxes[:, :2] -= boxes[:, 2:] / 2
    boxes[:, 2:] += boxes[:, :2]
    return boxes

def decode_landm(pre, priors, variances):
    """Decode landm from predictions using priors to undo
    the encoding we did for offset regression at train time.
    Args:
        pre (tensor): landm predictions for loc layers,
            Shape: [num_priors,10]
        priors (tensor): Prior boxes in center-offset form.
            Shape: [num_priors,4].
        variances: (list[float]) Variances of priorboxes
    Return:
        decoded landm predictions
    """
    landms = np.concatenate((priors[:, :2] + pre[:, :2] * variances[0] * priors[:, 2:],
                             priors[:, :2] + pre[:, 2:4] *
                             variances[0] * priors[:, 2:],
                             #priors[:, :2] + pre[:, 4:6] * variances[0] * priors[:, 2:],
                             priors[:, :2] + pre[:, 4:6] * \
                             variances[0] * priors[:, 2:],
                             priors[:, :2] + pre[:, 6:8] * \
                             variances[0] * priors[:, 2:],
                             ), axis=1)
    return landms

def base64_to_cv2(b64str):
    data = base64.b64decode(b64str.encode('utf8'))
    data = np.fromstring(data, np.uint8)
    data = cv2.imdecode(data, cv2.IMREAD_COLOR)
    return data

max_stars_repo_name: AgentMaker/AgentCLPR

max_issues_repo_name: AgentMaker/AgentCLPR

max_forks_repo_name: AgentMaker/AgentCLPR


text:
'''
Created on 24 Jul 2013

@author: maxz
'''
from GPy.util.latent_space_visualizations.controllers.axis_event_controller import BufferedAxisChangedController
import itertools
import numpy

class ImshowController(BufferedAxisChangedController):
    def __init__(self, ax, plot_function, plot_limits, resolution=50, update_lim=.8, **kwargs):
        """
        :param plot_function:
            function to use for creating image for plotting (return ndarray-like)
            plot_function gets called with (2D!) Xtest grid if replotting required
        :type plot_function: function
        :param plot_limits:
            beginning plot limits [xmin, ymin, xmax, ymax]

        :param kwargs: additional kwargs are for pyplot.imshow(**kwargs)
        """
        super(ImshowController, self).__init__(ax, plot_function, plot_limits, resolution, update_lim, **kwargs)

    def _init_view(self, ax, X, xmin, xmax, ymin, ymax, **kwargs):
        return ax.imshow(X, extent=(xmin, xmax,
                                    ymin, ymax),
                         vmin=X.min(),
                         vmax=X.max(),
                         **kwargs)

    def update_view(self, view, X, xmin, xmax, ymin, ymax):
        view.set_data(X)
        view.set_extent((xmin, xmax, ymin, ymax))

class ImAnnotateController(ImshowController):
    def __init__(self, ax, plot_function, plot_limits, resolution=20, update_lim=.99, **kwargs):
        """
        :param plot_function:
            function to use for creating image for plotting (return ndarray-like)
            plot_function gets called with (2D!) Xtest grid if replotting required
        :type plot_function: function
        :param plot_limits:
            beginning plot limits [xmin, ymin, xmax, ymax]
        :param text_props: kwargs for pyplot.text(**text_props)
        :param kwargs: additional kwargs are for pyplot.imshow(**kwargs)
        """
        super(ImAnnotateController, self).__init__(ax, plot_function, plot_limits, resolution, update_lim, **kwargs)

    def _init_view(self, ax, X, xmin, xmax, ymin, ymax, text_props={}, **kwargs):
        view = [super(ImAnnotateController, self)._init_view(ax, X[0], xmin, xmax, ymin, ymax, **kwargs)]
        xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
        xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False)
        ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False)
        for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin[::-1])):
            view.append(ax.text(x + xoffset, y + yoffset, "{}".format(X[1][j, i]), ha='center', va='center', **text_props))
        return view

    def update_view(self, view, X, xmin, xmax, ymin, ymax):
        super(ImAnnotateController, self).update_view(view[0], X[0], xmin, xmax, ymin, ymax)
        xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax)
        xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False)
        ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False)
        for [[i, x], [j, y]], text in itertools.izip(itertools.product(enumerate(xlin), enumerate(ylin[::-1])), view[1:]):
            text.set_x(x + xoffset)
            text.set_y(y + yoffset)
            text.set_text("{}".format(X[1][j, i]))
        return view

    def _offsets(self, xmin, xmax, ymin, ymax):
        return (xmax - xmin) / (2 * self.resolution), (ymax - ymin) / (2 * self.resolution)

max_stars_repo_name: rokroskar/GPy

max_issues_repo_name: rokroskar/GPy

max_forks_repo_name: rokroskar/GPy


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

    Authors:    Parshan Pakiman  | https://parshanpakiman.github.io/homepage/
                Selva Nadarajah  | https://selvan.people.uic.edu/

    Licensing Information: The MIT License
-------------------------------------------------------------------------------
"""
from MDP.BerOpt.bermudanOptionPricing import BermudanOption
import numpy as np

VOLATILITY          = 0.20
INTEREST_RATE       = 0.05
INIT_PRICE          = 90
NUM_EXERCISE        = 54

def get_experiment_setup():
    #--------------------------------------------------------------------------
    #   This function specifies the parameters of a perishable inventory
    #   control instance, including the MDP cost function parameters, dimension
    #   of the state space, demand distribution, optimality gap threshold,
    #   number of CPU cores, etc.
    #--------------------------------------------------------------------------
    mdp_conf                                    =   {}
    upp_bound_conf                              =   {}
    basis_func_conf                             =   {}
    solver                                      =   {}
    misc_conf                                   =   {}

    #--------------------------------------------------------------------------
    # Miscellaneous configuration
    #--------------------------------------------------------------------------
    misc_conf.update({
                'num_cpu_core'                  :   12,
                })

    #--------------------------------------------------------------------------
    # MDP configuration
    #--------------------------------------------------------------------------
    mdp_conf.update({
                'mdp'                           :   BermudanOption,
                'mdp_name'                      :   'BerOpt',
                'instance_number'               :   '1',
                'time_horizon'                  :   3,
                'num_asset'                     :   4,
                'dim_act'                       :   1,
                'knock_out_price'               :   170.0,
                'strike_price'                  :   100.0,
                'CFA_random_seed'               :   None,
                'pol_random_seed'               :   None,
                'num_CFA_sample_path'           :   None,
                'num_pol_eval_sample_path'      :   None,
                'inner_sample_size'             :   None,
                'inner_sample_seed'             :   None,
                'num_cpu_core'                  :   misc_conf['num_cpu_core'],
                'state_relevance_type'          :   None,
                })

    mdp_conf.update({
                'dim_state'                     :   mdp_conf['num_asset'] + 1,
                'volatility'                    :   np.array([VOLATILITY for _ in range(mdp_conf['num_asset'])]) ,
                'init_price'                    :   np.array([INIT_PRICE for _ in range(mdp_conf['num_asset'])]),
                'interest_rate'                 :   np.array([INTEREST_RATE for _ in range(mdp_conf['num_asset'])]) ,
                'cor_matrix'                    :   np.identity(mdp_conf['num_asset']),
                'num_stages'                    :   NUM_EXERCISE,
                'discount'                      :   np.exp(-INTEREST_RATE*(mdp_conf['time_horizon']/NUM_EXERCISE)),
    })

    #--------------------------------------------------------------------------
    # Basis functions setting
    #--------------------------------------------------------------------------
    basis_func_conf.update({
                'basis_func_type'               :   None,
                'basis_func'                    :   None,
                'dim_state'                     :   mdp_conf['dim_state'],
                'max_basis_num'                 :   None,
                'batch_size'                    :   None,
                'bandwidth'                     :   None,
                'basis_func_random_state'       :   None,
                'num_cpu_core'                  :   misc_conf['num_cpu_core']
        })

    #--------------------------------------------------------------------------
    # Upper bound setting
    #--------------------------------------------------------------------------
    upp_bound_conf.update({
                'upp_bound'                     : None,
                'upp_bound_algo_name'           : 'LNS',
                'num_MC_init_states'            : None,
                'MC_sample_path_len'            : None,
                'MC_burn_in'                    : None,
                'num_cpu_core'                  : None

        })

    #--------------------------------------------------------------------------
    # ALP solver setting
    #--------------------------------------------------------------------------
    solver.update({
                'num_cpu_core'                  : misc_conf['num_cpu_core'],
                'num_stages'                    : mdp_conf['num_stages'],
                'basis_func_batch_size'         : basis_func_conf['batch_size'],
                'abs_val_upp_bound'             : None,
        })

    config                                      =   {}
    config['mdp_conf']                          =   mdp_conf
    config['basis_func_conf']                   =   basis_func_conf
    config['misc_conf']                         =   misc_conf
    config['upp_bound_conf']                    =   upp_bound_conf
    config['solver_conf']                       =   solver
    return config


text:
# -*- coding: utf-8 -*-
"""quantum_teleportation
"""

# Commented out IPython magic to ensure Python compatibility.
import numpy as np
!pip install qiskit
from qiskit import *
# %matplotlib inline

# Create a Quantum Circuit acting on a quantum register of five qubits
circ = QuantumCircuit(5)

# Add an H gate on qubit 1, putting this qubit in superposition.
circ.h(1)
# Add a CX (CNOT) gate on control qubit 1 and target qubit 4, putting
# the qubits in a Bell state.
circ.cx(1, 4)
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a GHz state.
circ.cx(0, 1)
# Add an H gate on qubit 0, putting this qubit in superposition.
circ.h(0)
# Add a CX (CNOT) gate on control qubit 1 and target qubit 4, putting
# the qubits in a GHz state.
circ.cx(1, 4)
# Add a Z gate on qubit 4, flipping this qubit.
circ.z(4)

circ.draw()

# Import Aer
from qiskit import Aer

# Run the quantum circuit on a statevector simulator backend
backend = Aer.get_backend('statevector_simulator')

# Create a Quantum Program for execution
job = execute(circ, backend)

result = job.result()

outputstate = result.get_statevector(circ, decimals=3)
print(outputstate)

from qiskit.visualization import plot_state_city
plot_state_city(outputstate)

# Run the quantum circuit on a unitary simulator backend
backend = Aer.get_backend('unitary_simulator')
job = execute(circ, backend)
result = job.result()

# Show the results
print(result.get_unitary(circ, decimals=3))

# Create a Quantum Circuit
meas = QuantumCircuit(5, 5)
meas.barrier(range(3))
# map the quantum measurement to the classical bits
meas.measure(range(5),range(5))

# The Qiskit circuit object supports composition using
# the addition operator.
qc = circ+meas

#drawing the circuit
qc.draw()

# Use Aer's qasm_simulator
backend_sim = Aer.get_backend('qasm_simulator')

# Execute the circuit on the qasm simulator.
# We've set the number of repeats of the circuit
# to be 8192
job_sim = execute(qc, backend_sim, shots=8192)

# Grab the results from the job.
result_sim = job_sim.result()

counts = result_sim.get_counts(qc)
print(counts)

from qiskit.visualization import plot_histogram
plot_histogram(counts)

max_stars_repo_name: RafeyIqbalRahman/Quantum-Teleportation

max_issues_repo_name: RafeyIqbalRahman/Quantum-Teleportation

max_forks_repo_name: RafeyIqbalRahman/Quantum-Teleportation


text:
import numpy as np
from .baseMetric import BaseMetric
from rubin_sim.maf.utils import m52snr

__all__ = ['PhaseGapMetric', 'PeriodicQualityMetric']

class PhaseGapMetric(BaseMetric):
    """
    Measure the maximum gap in phase coverage for observations of periodic variables.

    Parameters
    ----------
    col: str, optional
        Name of the column to use for the observation times (MJD)
    nPeriods: int, optional
        Number of periods to test
    periodMin: float, optional
        Minimum period to test, in days.
    periodMax: float, optional
        Maximum period to test, in days
    nVisitsMin: int, optional
        Minimum number of visits necessary before looking for the phase gap.
    """
    def __init__(self, col='observationStartMJD', nPeriods=5, periodMin=3., periodMax=35., nVisitsMin=3,
                 metricName='Phase Gap', **kwargs):
        self.periodMin = periodMin
        self.periodMax = periodMax
        self.nPeriods = nPeriods
        self.nVisitsMin = nVisitsMin
        super(PhaseGapMetric, self).__init__(col, metricName=metricName, units='Fraction, 0-1', **kwargs)

    def run(self, dataSlice, slicePoint=None):
        if len(dataSlice) < self.nVisitsMin:
            return self.badval
        # Create 'nPeriods' evenly spaced periods within range of min to max.
        step = (self.periodMax-self.periodMin)/self.nPeriods
        if step == 0:
            periods = np.array([self.periodMin])
        else:
            periods = np.arange(self.nPeriods)
            periods = periods/np.max(periods)*(self.periodMax-self.periodMin)+self.periodMin
        maxGap = np.zeros(self.nPeriods, float)

        for i, period in enumerate(periods):
            # For each period, calculate the phases.
            phases = (dataSlice[self.colname] % period)/period
            phases = np.sort(phases)
            # Find the largest gap in coverage.
            gaps = np.diff(phases)
            start_to_end = np.array([1.0 - phases[-1] + phases[0]], float)
            gaps = np.concatenate([gaps, start_to_end])
            maxGap[i] = np.max(gaps)

        return {'periods':periods, 'maxGaps':maxGap}

    def reduceMeanGap(self, metricVal):
        """
        At each slicepoint, return the mean gap value.
        """
        return np.mean(metricVal['maxGaps'])

    def reduceMedianGap(self, metricVal):
        """
        At each slicepoint, return the median gap value.
        """
        return np.median(metricVal['maxGaps'])

    def reduceWorstPeriod(self, metricVal):
        """
        At each slicepoint, return the period with the largest phase gap.
        """
        worstP = metricVal['periods'][np.where(metricVal['maxGaps'] == metricVal['maxGaps'].max())]
        return worstP

    def reduceLargestGap(self, metricVal):
        """
        At each slicepoint, return the largest phase gap value.
        """
        return np.max(metricVal['maxGaps'])

#  To fit a periodic source well, you need to cover the full phase, and fit the amplitude.
class PeriodicQualityMetric(BaseMetric):
    def __init__(self, mjdCol='observationStartMJD', period=2., m5Col='fiveSigmaDepth',
                 metricName='PhaseCoverageMetric', starMag=20, **kwargs):
        self.mjdCol = mjdCol
        self.m5Col = m5Col
        self.period = period
        self.starMag = starMag
        super(PeriodicQualityMetric, self).__init__([mjdCol, m5Col], metricName=metricName,
                                                    units='Fraction, 0-1', **kwargs)

    def _calc_phase(self, dataSlice):
        """1 is perfectly balanced phase coverage, 0 is no effective coverage.
        """
        angles = dataSlice[self.mjdCol] % self.period
        angles = angles/self.period * 2.*np.pi
        x = np.cos(angles)
        y = np.sin(angles)

        snr = m52snr(self.starMag, dataSlice[self.m5Col])
        x_ave = np.average(x, weights=snr)
        y_ave = np.average(y, weights=snr)

        vector_off = np.sqrt(x_ave**2+y_ave**2)
        return 1.-vector_off

    def _calc_amp(self, dataSlice):
        """Fractional SNR on the amplitude, testing for a variety of possible phases
        """
        phases = np.arange(0, np.pi, np.pi/8.)
        snr = m52snr(self.starMag, dataSlice[self.m5Col])
        amp_snrs = np.sin(dataSlice[self.mjdCol]/self.period*2*np.pi + phases[:, np.newaxis])*snr
        amp_snr = np.min(np.sqrt(np.sum(amp_snrs**2, axis=1)))

        max_snr = np.sqrt(np.sum(snr**2))
        return amp_snr/max_snr

    def run(self, dataSlice, slicePoint=None):
        amplitude_fraction = self._calc_amp(dataSlice)
        phase_fraction = self._calc_phase(dataSlice)
        return amplitude_fraction * phase_fraction

max_stars_repo_name: RileyWClarke/flarubin

max_issues_repo_name: RileyWClarke/flarubin

max_forks_repo_name: RileyWClarke/flarubin