text:
# Implementations of stopping criteria
#
# Licensed under the BSD 3-Clause License
# Copyright (c) 2020, Yuriy Sverchkov

from abc import abstractmethod, ABC
from logging import getLogger, Logger
from typing import Iterable, Protocol

from numpy import ndarray

from generalizedtrees.node import NodeBase
from generalizedtrees.tree import Tree

logger: Logger = getLogger()

# Stopping criteria learner components:

## Interface definitions
class LocalStopLC(Protocol):

    @abstractmethod
    def check(self, node: NodeBase, data: ndarray, y: ndarray) -> bool:
        raise NotImplementedError

class GlobalStopLC(Protocol):

    @abstractmethod
    def check(self, tree: Tree) -> bool:
        raise NotImplementedError

## Composite criteria

class LocalStopConjunctionLC(LocalStopLC):

    subcriteria: Iterable[LocalStopLC] = []

    def __init__(self, *subcriteria):
        self.subcriteria = subcriteria

    def check(self, node: NodeBase, data: ndarray, y: ndarray) -> bool:

        for criterion in self.subcriteria:
            if not criterion.check(node, data, y): return False

        return True

class LocalStopDisjunctionLC(LocalStopLC):

    subcriteria: Iterable[LocalStopLC] = []

    def __init__(self, *subcriteria):
        self.subcriteria = subcriteria

    def check(self, node: NodeBase, data: ndarray, y: ndarray) -> bool:

        for criterion in self.subcriteria:
            if criterion.check(node, data, y): return True

        return False

class GlobalStopConjunctionLC(GlobalStopLC):

    subcriteria: Iterable[GlobalStopLC] = []

    def __init__(self, *subcriteria):
        self.subcriteria = subcriteria

    def check(self, tree: Tree) -> bool:

        for criterion in self.subcriteria:
            if not criterion.check(tree): return False

        return True

class GlobalStopDisjunctionLC(GlobalStopLC):

    subcriteria: Iterable[GlobalStopLC] = []

    def __init__(self, *subcriteria):
        self.subcriteria = subcriteria

    def check(self, tree: Tree) -> bool:

        for criterion in self.subcriteria:
            if criterion.check(tree): return True

        return False

## General stopping criteria

class NeverStopLC(LocalStopLC, GlobalStopLC):
    def check(self, *args, **kwargs) -> bool:
        return False

## Local stopping criteria

class LocalStopDepthLC(LocalStopLC):

    def __init__(self, max_depth: int):
        self.depth: int = max_depth

    def check(self, node: NodeBase, data: ndarray, y: ndarray) -> bool:
        if node.depth >= self.depth:
            logger.debug(f'Local stopping criterion met: node depth {node.depth} meets depth limit {self.depth}')
            return True
        return False

class LocalStopSaturation(LocalStopLC):

    def __init__(self, saturation: float = 1, training_only: bool = False):
        self.saturation: float = saturation
        self.training_only: bool = training_only

    def check(self, node, data: ndarray, y: ndarray) -> bool:
        item = node.item
        if self.training_only:
            if hasattr(item, 'n_training'):
                if item.n_training <= 1:
                    logger.debug(f'Local stopping criterion met: {item.n_training} samples for saturation test.')
                    return True
                else:
                    y = y[:item.n_training, :]
            else:
                logger.warn(
                    'Node saturation stopping criterion "training_only" flag is mean to be '
                    'used with model translation, but is being used with node objects that '
                    'do not support it.')

        saturation = y.mean(axis=0).max()

        if saturation >= self.saturation:
            logger.debug(f'Local stopping criterion met: node saturation={saturation} meets threshold={self.saturation}.')
            return True

        return False

## Global stopping criteria

class GlobalStopTreeSizeLC(GlobalStopLC):

    def __init__(self, max_size):
        self.size: int = max_size

    def check(self, tree: Tree):
        size = len(tree)
        if size >= self.size:
            logger.debug(f'Global stopping criterion met: tree size {size} meets size limit {self.size}.')
            return True
        return False

reason: from numpy


text:
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
"""

import json
import logging

from graph_notebook.network.EventfulNetwork import EventfulNetwork, DEFAULT_GRP
from networkx import MultiDiGraph

logging.basicConfig()
logger = logging.getLogger(__file__)

DEFAULT_LABEL_MAX_LENGTH = 10
ENTITY_KEY = "~entityType"
ID_KEY = "~id"
START_KEY = "~start"
END_KEY = "~end"
PROPERTIES_KEY = "~properties"
VERTEX_TYPE_KEY = "~entityType"
EDGE_TYPE_KEY = "~type"
LABEL_KEY = "~labels"
NODE_ENTITY_TYPE = 'node'
REL_ENTITY_TYPE = 'relationship'

class OCNetwork(EventfulNetwork):
    """OCNetwork extends the EventfulNetwork class and uses the add_results method to parse any response that returns
    nodes/relationships as part (or all) of the response.  Currently this only works with HTTPS response format but in
    the future, we will work to support Bolt based responses as well.
    """

    def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH,
                 edge_label_max_length=DEFAULT_LABEL_MAX_LENGTH, group_by_property=LABEL_KEY,
                 display_property=LABEL_KEY, edge_display_property=EDGE_TYPE_KEY,
                 tooltip_property=None, edge_tooltip_property=None,
                 ignore_groups=False):
        if graph is None:
            graph = MultiDiGraph()
        super().__init__(graph, callbacks, label_max_length, edge_label_max_length, group_by_property,
                         display_property, edge_display_property, tooltip_property, edge_tooltip_property,
                         ignore_groups)

    def get_node_property_value(self, node: dict, props: dict, title, custom_property):
        try:
            if isinstance(custom_property, dict):
                if isinstance(custom_property[title], tuple):
                    if custom_property[title][0] in props and \
                            isinstance(props[custom_property[title][0]], list) and \
                            len(props[custom_property[title][0]]) >= 2:
                        label = props[custom_property[title][0]][custom_property[title][1]]
                    else:
                        label = title
                elif custom_property[title] in props:
                    label = props[custom_property[title]]
                elif LABEL_KEY in props:
                    label = props[LABEL_KEY]
                else:
                    label = props
            elif isinstance(custom_property, tuple) and custom_property[0] in props:
                if isinstance(props[custom_property[0]], list) and len(props[custom_property[0]]) >= 2:
                    label = props[custom_property[0]][custom_property[1]]
                else:
                    label = title
            elif custom_property in [ID_KEY, 'id']:
                label = node[ID_KEY]
            elif custom_property in [LABEL_KEY, 'label']:
                label = node[LABEL_KEY]
            elif custom_property in [VERTEX_TYPE_KEY, 'type']:
                label = node[VERTEX_TYPE_KEY]
            elif custom_property in props:
                label = props[custom_property]
            else:
                label = title
        except (KeyError, IndexError) as e:
            logger.debug(e)
            label = title

        return label

    def get_edge_property_value(self, data: dict, rel: dict, custom_property):
        if custom_property is not EDGE_TYPE_KEY:
            try:
                if isinstance(custom_property, dict):
                    if isinstance(custom_property[data['label']], tuple) and \
                            custom_property[data['label']][0] in data['properties']:
                        if isinstance(data['properties'][custom_property[data['label']][0]], list) and \
                                len(data['properties'][custom_property[data['label']][0]]) >= 2:
                            display_label = str(data['properties'][custom_property[data['label']][0]]
                                                [custom_property[data['label']][1]])
                        else:
                            display_label = rel[EDGE_TYPE_KEY]
                    else:
                        display_label = data['properties'][custom_property[rel[EDGE_TYPE_KEY]]]
                elif isinstance(custom_property, tuple) and \
                        custom_property[0] in data['properties']:
                    display_label = str(data['properties'][custom_property[0]]
                                        [custom_property[1]])
                else:
                    display_label = data['properties'][custom_property]
            except (KeyError, IndexError, TypeError) as e:
                logger.debug(e)
                display_label = rel[EDGE_TYPE_KEY]
        else:
            display_label = rel[EDGE_TYPE_KEY]

        return display_label

    def parse_node(self, node: dict):
        """This parses the node parameter and adds the node to the network diagram

        Args:
            node (dict): The node dictionary to parse
        """
        # generate placeholder tooltip from label; if not present, amalgamate node property values instead
        if LABEL_KEY in node.keys():
            title_plc = node[LABEL_KEY][0]
        else:
            title_plc = ""
            for key in node:
                title_plc += str(node[key])

        if not isinstance(self.group_by_property, dict):  # Handle string format group_by
            try:
                if self.group_by_property in [LABEL_KEY, 'labels'] and len(node[LABEL_KEY]) > 0:
                    group = node[LABEL_KEY][0]
                elif self.group_by_property in [ID_KEY, 'id']:
                    group = node[ID_KEY]
                elif self.group_by_property in node[PROPERTIES_KEY]:
                    group = node[PROPERTIES_KEY][self.group_by_property]
                else:
                    group = DEFAULT_GRP
            except KeyError:
                group = DEFAULT_GRP
        else:  # handle dict format group_by
            try:
                if str(node[LABEL_KEY][0]) in self.group_by_property and len(node[LABEL_KEY]) > 0:
                    key = node[LABEL_KEY][0]
                    if self.group_by_property[key]['groupby'] in [LABEL_KEY, 'labels']:
                        group = node[LABEL_KEY][0]
                    else:
                        group = node[PROPERTIES_KEY][self.group_by_property[key]['groupby']]
                elif ID_KEY in self.group_by_property:
                    group = node[ID_KEY]
                else:
                    group = DEFAULT_GRP
            except KeyError:
                group = DEFAULT_GRP

        props = self.flatten(node)
        label = self.get_node_property_value(node, props, title_plc, self.display_property)
        title, label = self.strip_and_truncate_label_and_title(label, self.label_max_length)
        if self.tooltip_property and self.tooltip_property != self.display_property:
            title, label_plc = self.strip_and_truncate_label_and_title(
                self.get_node_property_value(node, props, title_plc, self.tooltip_property))
        data = {'properties': props, 'label': label, 'title': title, 'group': group}
        if self.ignore_groups:
            data['group'] = DEFAULT_GRP
        self.add_node(node[ID_KEY], data)

    def parse_rel(self, rel):
        data = {'properties': self.flatten(rel), 'label': rel[EDGE_TYPE_KEY], 'title': rel[EDGE_TYPE_KEY]}
        display_label = self.get_edge_property_value(data, rel, self.edge_display_property)
        edge_title, edge_label = self.strip_and_truncate_label_and_title(display_label, self.edge_label_max_length)
        if self.edge_tooltip_property and self.edge_tooltip_property != self.edge_display_property:
            edge_title = self.get_edge_property_value(data, rel, self.edge_tooltip_property)
        data['title'] = edge_title
        data['label'] = edge_label
        self.add_edge(from_id=rel[START_KEY], to_id=rel[END_KEY], edge_id=rel[ID_KEY], label=edge_label,
                      title=edge_title, data=data)

    def process_result(self, res: dict):
        """Determines the type of element passed in and processes it appropriately

        Args:
            res (dict): The dictionary to parse
        """
        if ENTITY_KEY in res:
            if res[ENTITY_KEY] == NODE_ENTITY_TYPE:
                self.parse_node(res)
            else:
                self.parse_rel(res)

    def add_results(self, results):
        """Adds the results parameter to the network

        Args:
            results (Object): Determines the type of the object and processes it appropriately
        """
        for res in results["results"]:
            if type(res) is dict:
                for k in res.keys():
                    if type(res[k]) is dict:
                        self.process_result(res[k])
                    elif type(res[k]) is list:
                        for res_sublist in res[k]:
                            try:
                                self.process_result(res_sublist)
                            except TypeError as e:
                                logger.debug(f'Property {res_sublist} in list results set is invalid, skipping')
                                logger.debug(f'Error: {e}')
                                continue

ext: py

reason: from networkx


text:
"""Energy Visualizer.

Just to make sure that the energy targets are correctly calculated.

Author:
    Yvan Satyawan <y_satyawan@hotmail.com>
"""
import torch
import numpy as np
from PIL import Image

from math import floor, ceil

from debugging.coco_dataset import CocoDetection
from debugging.visualization_tools import map_color_values
from debugging.bbox import BoundingBox
from debugging import draw_boxes

from os.path import join

def energy_target(flattened_bbox_targets, pos_bbox_targets,
                  pos_indices, r, max_energy):
    """Calculate energy targets based on deep watershed paper.

    Args:
        flattened_bbox_targets (torch.Tensor): The flattened bbox targets.
        pos_bbox_targets (torch.Tensor): Bounding box lrtb values only for
            positions within the bounding box. We use this as an argument
            to prevent recalculating it since it is used for other things as
            well.
        pos_indices (torch.Tensor): The indices of values in
            flattened_bbox_targets which are within a bounding box
        max_energy (int): Max energy level possible.

    Notes:
        The energy targets are calculated as:
        E_max \cdot argmax_{c \in C}[1 - \sqrt{((l-r)/2)^2 + ((t-b) / 2)^2}
                                     / r]

        - r is a hyperparameter we would like to minimize.
        - (l-r)/2 is the horizontal distance to the center and will be
            assigned the variable name "horizontal"
        - (t-b)/2 is the vertical distance to the center and will be
            assigned the variable name "vertical"
        - E_max is self.max_energy
        - We don't need the argmax in this code implementation since we
            already select the bounding boxes and their respective pixels in
            a previous step.

    Returns:
        tuple: A 2 tuple with values ("pos_energies_targets",
            "energies_targets"). Both are flattened but pos_energies_targets
            only contains values within bounding boxes.
    """

    horizontal = pos_bbox_targets[:, 0] - pos_bbox_targets[:, 2]
    vertical = pos_bbox_targets[:, 1] - pos_bbox_targets[:, 3]

    # print("Horizontals: {}".format(horizontal))
    # print("Verticals: {}".format(vertical))

    horizontal = torch.div(horizontal, 2)
    vertical = torch.div(vertical, 2)

    c2 = (horizontal * horizontal) + (vertical * vertical)

    # print("c2: \n{}".format(c2))

    # We use x * x instead of x.pow(2) since it's faster by about 30%
    square_root = torch.sqrt(c2)

    # print("Sqrt: \n{}".format(square_root))

    type_dict = {'dtype': square_root.dtype,
                 'device': square_root.device}

    pos_energies = (torch.tensor([1], **type_dict)
                    - torch.div(square_root, r))
    pos_energies *= max_energy
    pos_energies = torch.max(pos_energies,
                             torch.tensor([0], **type_dict))
    pos_energies = pos_energies.floor()

    energies_targets = torch.zeros(flattened_bbox_targets.shape[0],
                                   **type_dict)
    energies_targets[pos_indices] = pos_energies

    # torch.set_printoptions(profile='full')
    # print("Energy targets: \n {}".format(pos_energies))
    # torch.set_printoptions(profile='default')
    # input()

    return pos_energies, energies_targets

def process_target(img, targets):
    """Processes targets and returns an overlaid image."""
    bboxes = BoundingBox()
    for t in targets:
        bboxes.append_target(t['bbox'], t['category_id'])
    return draw_boxes(img, bboxes, threshold=1., target=True)

def overlay_energy(image, energies_targets) -> Image.Image:
    energy_overlay = map_color_values(energies_targets.numpy(), 20)
    energy_overlay = Image.fromarray(energy_overlay)

    return Image.blend(image, energy_overlay, 0.4)

def visualize_image(image, annotations, output_dir, iterator):
    """Visualizes the energy of a single image and outputs it to a file."""
    # Get bboxes first
    bboxes = [annotation['bbox'] for annotation in annotations]

    # These are both not flattened yet
    pos_bbox_targets = []
    pos_indices = []

    width, height = image.size

    # For each pixel within the bounding boxes, calculate the lrtb box values
    # of that pixel
    # We do this by iterating through each pixel of the image
    for i in range(height):
        for j in range(width):
            for bbox in bboxes:
                left, top, bbox_width, bbox_height = bbox
                right = left + bbox_width
                bottom = top + bbox_height
                if ceil(left) <= j <= floor(right) \
                        and ceil(top) <= i <= floor(bottom):
                    # If the pixel is within the bounding box
                    dist_left = float(j) - left
                    dist_right = right - float(j)
                    dist_top = float(i) - top
                    dist_bottom = bottom - float(i)

                    pos_bbox_targets.append([dist_left, dist_top,
                                             dist_right, dist_bottom])
                    pos_indices.append((width * i) + j)

    pos_bbox_targets = np.array(pos_bbox_targets)
    flattened_bboxes = np.zeros([width * height, 4])
    flattened_bboxes[pos_indices] = pos_bbox_targets

    pos_bbox_targets = torch.tensor(pos_bbox_targets)
    flattened_bboxes = torch.tensor(flattened_bboxes)
    pos_indices = torch.tensor(pos_indices)

    pos_energies, energies_targets = energy_target(flattened_bboxes,
                                                   pos_bbox_targets,
                                                   pos_indices,
                                                   300.,
                                                   20)

    energies_targets = energies_targets.reshape([height, width])

    overlaid = overlay_energy(image, energies_targets)

    overlaid = process_target(overlaid, annotations)

    overlaid.save(join(output_dir, str(iterator) + '.png'))

def runner(coco_dataset_root, output_fp):
    """Runs the program."""
    ann_fp = join(coco_dataset_root, 'annotations', 'instances_small2017.json')
    dataset = CocoDetection(join(coco_dataset_root, 'images', 'val2017'),
                            ann_fp)

    stop = False
    i = 0

    while not stop:
        img, annotation = dataset[i]
        if len(annotation) != 0:
            visualize_image(img, annotation, output_fp, i)
            response = input('continue? [yes]')
            if response == 'no' or response == 'n':
                stop = True
        i += 1

if __name__ == '__main__':
    runner('/workspace/coco/', '/workspace/watershed-fcos/work_dirs/energy_vis')

max_stars_repo_name: tuggeluk/mmdetection

max_issues_repo_name: tuggeluk/mmdetection

max_forks_repo_name: tuggeluk/mmdetection

reason: import numpy


text:
#### !/usr/bin/env python
# coding: utf-8
from molmap.model import RegressionEstimator, MultiClassEstimator, MultiLabelEstimator
from molmap import loadmap, dataset
from molmap.show import imshow_wrap
import molmap

from sklearn.utils import shuffle
from joblib import load, dump
import numpy as np
import pandas as pd
import os

from chembench import dataset

def get_pos_weights(trainY):
    """pos_weights: neg_n / pos_n """
    dfY = pd.DataFrame(trainY)
    pos = dfY == 1
    pos_n = pos.sum(axis=0)
    neg = dfY == 0
    neg_n = neg.sum(axis=0)
    pos_weights = (neg_n / pos_n).values
    neg_weights = (pos_n / neg_n).values
    return pos_weights, neg_weights

## random
clintox = dataset.load_ClinTox() #[128] 2 outputs
sider = dataset.load_SIDER() #[256] #27 outputs
toxcast = dataset.load_ToxCast() # [1024] # 617 outputs
tox21 = dataset.load_Tox21() #[256] #12 outputs

dense_layers_config = {
    'ToxCast': [1024], # 617 outputs
    'SIDER': [128], # 27 outputs
    'Tox21':[256], # 12  outputs
    'ClinTox':[128], #2 outputs
    'ChEMBL': [2048], # 1310 outputs
    'PCBA': [256], #128 outputs
    'MUV':[512], #17 outputs
}

gpuid = 5
result_file = 'sider_toxcast_tox21.csv'

random_seeds = [2, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]

file_path = "/raid/shenwanxiang/08_Robustness/dataset_induces/split" #split
tmp_feature_dir = '/raid/shenwanxiang/08_Robustness/tempignore' #feature path

with open(result_file, 'w+') as f:
    f.write('task_name, seed, valid_auc, test_auc\n')

# the dense layers for these multi outputs tasks

res = []
for data in [toxcast, sider, tox21]:
    task_name = data.task_name
    task_type = data.task_type

    X1_name = os.path.join(tmp_feature_dir, 'X1_%s.data' % task_name)
    X2_name = os.path.join(tmp_feature_dir, 'X2_%s.data' % task_name)
    X1 = load(X1_name)
    X2 = load(X2_name)

    fmap_shape1 = X1.shape[1:]
    fmap_shape2 = X2.shape[1:]

    Y = data.y
    df = data.df
    n_outputs = Y.shape[1]

    Y = pd.DataFrame(Y).fillna(-1).values #mask nan value with -1

    dense_layers = dense_layers_config[task_name]

    for seed in random_seeds:

        train_path = os.path.join(file_path, task_name,"%s" % seed, "train.csv")
        valid_path = os.path.join(file_path, task_name,"%s" % seed, "val.csv")
        test_path = os.path.join(file_path, task_name,"%s" % seed, "test.csv")

        train_df = pd.read_csv(train_path)
        valid_df = pd.read_csv(valid_path)
        test_df = pd.read_csv(test_path)

        train_idx = df[df.smiles.isin(train_df.smiles)].index.tolist()
        valid_idx = df[df.smiles.isin(valid_df.smiles)].index.tolist()
        test_idx = df[df.smiles.isin(test_df.smiles)].index.tolist()

        print(len(train_idx), len(valid_idx), len(test_idx))

        X_train = (X1[train_idx], X2[train_idx])
        y_train = Y[train_idx]

        X_valid = (X1[valid_idx], X2[valid_idx])
        y_valid = Y[valid_idx]

        X_test = (X1[test_idx], X2[test_idx])
        y_test = Y[test_idx]

        pos_weights, neg_weights = get_pos_weights(y_train)
        loss = lambda y_true, y_pred: molmap.model.loss.weighted_cross_entropy(y_true,y_pred, pos_weights, MASK = -1)

        clf = MultiLabelEstimator(n_outputs,
                                      fmap_shape1,
                                      fmap_shape2,
                                      batch_size = 128,
                                      dense_layers = dense_layers,
                                      gpuid = gpuid,
                                      patience = 30, #speed only
                                      monitor = 'val_auc',
                                      loss = loss,
                                     )
        clf.fit(X_train,y_train, X_valid, y_valid)

        train_aucs = clf._performance.evaluate(X_train,y_train)
        valid_aucs = clf._performance.evaluate(X_valid,y_valid)
        test_aucs = clf._performance.evaluate(X_test,y_test)

        train_auc = np.nanmean(train_aucs)
        valid_auc = np.nanmean(valid_aucs)
        test_auc = np.nanmean(test_aucs)

        final_res = {'seed': seed,
                     "task_name": task_name,
                     'train_auc':train_auc,
                     'valid_auc':valid_auc,
                     'test_auc':test_auc,}
        print(final_res)

        with open(result_file, 'a+') as f:
            f.write('%s, %s, %s, %s\n' % (task_name, seed, valid_auc, test_auc))

        res.append(final_res)

pd.DataFrame(res).to_csv(result_file + '.bak.csv')

max_stars_repo_name: riversdark/bidd-molmap


text:
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the PyQwt License
# Copyright (C) 2003-2009 Gerard Vermeulen, for the original PyQwt example
# Copyright (c) 2015 Pierre Raybaut, for the PyQt5/PySide port and further
# developments (e.g. ported to PythonQwt API)
# (see LICENSE file for more details)

SHOW = True  # Show test in GUI-based test launcher

import random
import numpy as np

from qtpy.QtWidgets import QFrame
from qtpy.QtGui import QPen, QBrush
from qtpy.QtCore import QSize, Qt
from qwt import (
    QwtPlot,
    QwtPlotMarker,
    QwtSymbol,
    QwtLegend,
    QwtPlotCurve,
    QwtAbstractScaleDraw,
)

class DataPlot(QwtPlot):
    def __init__(self, *args, unattended=False):
        QwtPlot.__init__(self, *args)

        self.setCanvasBackground(Qt.white)
        self.alignScales()

        # Initialize data
        self.x = np.arange(0.0, 100.1, 0.5)
        self.y = np.zeros(len(self.x), float)
        self.z = np.zeros(len(self.x), float)

        self.setTitle("A Moving QwtPlot Demonstration")
        self.insertLegend(QwtLegend(), QwtPlot.BottomLegend)

        self.curveR = QwtPlotCurve("Data Moving Right")
        self.curveR.attach(self)
        self.curveL = QwtPlotCurve("Data Moving Left")
        self.curveL.attach(self)

        self.curveL.setSymbol(
            QwtSymbol(QwtSymbol.Ellipse, QBrush(), QPen(Qt.yellow), QSize(7, 7))
        )

        self.curveR.setPen(QPen(Qt.red))
        self.curveL.setPen(QPen(Qt.blue))

        mY = QwtPlotMarker()
        mY.setLabelAlignment(Qt.AlignRight | Qt.AlignTop)
        mY.setLineStyle(QwtPlotMarker.HLine)
        mY.setYValue(0.0)
        mY.attach(self)

        self.setAxisTitle(QwtPlot.xBottom, "Time (seconds)")
        self.setAxisTitle(QwtPlot.yLeft, "Values")

        self.startTimer(10 if unattended else 50)
        self.phase = 0.0

    def alignScales(self):
        self.canvas().setFrameStyle(QFrame.Box | QFrame.Plain)
        self.canvas().setLineWidth(1)
        for axis_id in QwtPlot.AXES:
            scaleWidget = self.axisWidget(axis_id)
            if scaleWidget:
                scaleWidget.setMargin(0)
            scaleDraw = self.axisScaleDraw(axis_id)
            if scaleDraw:
                scaleDraw.enableComponent(QwtAbstractScaleDraw.Backbone, False)

    def timerEvent(self, e):
        if self.phase > np.pi - 0.0001:
            self.phase = 0.0

        # y moves from left to right:
        # shift y array right and assign new value y[0]
        self.y = np.concatenate((self.y[:1], self.y[:-1]))
        self.y[0] = np.sin(self.phase) * (-1.0 + 2.0 * random.random())

        # z moves from right to left:
        # Shift z array left and assign new value to z[n-1].
        self.z = np.concatenate((self.z[1:], self.z[:1]))
        self.z[-1] = 0.8 - (2.0 * self.phase / np.pi) + 0.4 * random.random()

        self.curveR.setData(self.x, self.y)
        self.curveL.setData(self.x, self.z)

        self.replot()
        self.phase += np.pi * 0.02

if __name__ == "__main__":
    from qwt import tests

    app = tests.test_widget(DataPlot, size=(500, 300))

max_stars_repo_name: fochoao/cpython

max_issues_repo_name: fochoao/cpython

max_forks_repo_name: fochoao/cpython


text:
from preprocessing import read_dataframe
import pandas as pd
import numpy as np
from pathos.multiprocessing import ProcessingPool as Pool
import logging

logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

class SubmissionGenerator(object):

    def __init__(self, results, processed):
        self.results = results
        self.processed = processed

    def generate(self, original):
        for i, row in original.iterrows():
            if not i % 1000:
                log.info(str(i))
            try:
                labels_query = self.processed[self.processed['joined_ngram'] == original.at[i, 'joined_ngram']].to_dict('records')[0]['labels']
            except:
                labels_query = ''
            try:
                prediction_query = self.results[self.results['joined_ngram'] == original.at[i, 'joined_ngram']].to_dict('records')[0]['prediction']
            except:
                prediction_query = ''
            original.at[i, 'labels'] = labels_query
            original.at[i, 'prediction'] = prediction_query
        return original

def parallelize_dataframe(df, func, n_cores=16):
    df_split = np.array_split(df, n_cores)
    pool = Pool(n_cores)
    df = pd.concat(pool.map(func, df_split))
    pool.close()
    pool.join()
    return df

if __name__ == '__main__':
    options = {
        "results" : "results.csv",
        "processed": "./corpora/wiki/en/processed/test.txt",
        "original": "./corpora/wiki/en/original/test_enwiki_2019_11_30.csv"
    }

    results = pd.read_csv(options['results'])
    results = results[['token', 'des_seg']]
    results = results.rename(columns={'token': 'joined_ngram', 'des_seg': 'prediction'})
    results = results.dropna()
    results = results.drop_duplicates(subset='joined_ngram')

    processed = read_dataframe(options['processed'], field1='joined_ngram', field2='labels')
    processed = processed.dropna()

    original = pd.read_csv(options['original'])
    original['labels'] = ''
    original['prediction'] = ''

    submission = SubmissionGenerator(results, processed)

    original = parallelize_dataframe(original, submission.generate)

    original.to_csv("results_enwiki_2019_11_30.tsv", sep='\t')


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 11 16:19:39 2014

"""
import os
import sys
import imp
# Put location of
sys.path.insert(0,  os.path.abspath(os.path.join(os.path.dirname(__file__), '..\\..')) + '\\modules') # add ODYM module directory to system path
#NOTE: Hidden variable __file__ must be know to script for the directory structure to work.
# Therefore: When first using the model, run the entire script with F5 so that the __file__ variable can be created.

import dynamic_stock_model as dsm # remove and import the class manually if this unit test is run as standalone script
imp.reload(dsm)

import numpy as np
import unittest

###############################################################################
"""My Input for fixed lifetime"""
Time_T_FixedLT = np.arange(0,10)
Inflow_T_FixedLT  = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
lifetime_FixedLT  = {'Type': 'Fixed', 'Mean': np.array([5])}
lifetime_FixedLT0 = {'Type': 'Fixed', 'Mean': np.array([0])}
#lifetime_FixedLT = {'Type': 'Fixed', 'Mean': np.array([5,5,5,5,5,5,5,5,5,5])}
lifetime_NormLT  = {'Type': 'Normal', 'Mean': np.array([5]), 'StdDev': np.array([1.5])}
lifetime_NormLT0 = {'Type': 'Normal', 'Mean': np.array([0]), 'StdDev': np.array([1.5])}
###############################################################################
"""My Output for fixed lifetime"""
Outflow_T_FixedLT = np.array([0, 0, 0, 0, 0, 1, 2, 3, 4, 5])

Outflow_TC_FixedLT = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 3, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 4, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 5, 0, 0, 0, 0, 0]])

Stock_T_FixedLT = np.array([1, 3, 6, 10, 15, 20, 25, 30, 35, 40])

StockChange_T_FixedLT = np.array([1, 2, 3, 4, 5, 5, 5, 5, 5, 5])

Stock_TC_FixedLT = np.array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                             [1, 2, 0, 0, 0, 0, 0, 0, 0, 0],
                             [1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
                             [1, 2, 3, 4, 0, 0, 0, 0, 0, 0],
                             [1, 2, 3, 4, 5, 0, 0, 0, 0, 0],
                             [0, 2, 3, 4, 5, 6, 0, 0, 0, 0],
                             [0, 0, 3, 4, 5, 6, 7, 0, 0, 0],
                             [0, 0, 0, 4, 5, 6, 7, 8, 0, 0],
                             [0, 0, 0, 0, 5, 6, 7, 8, 9, 0],
                             [0, 0, 0, 0, 0, 6, 7, 8, 9, 10]])

Bal = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
"""My Output for normally distributed lifetime"""

Stock_TC_NormLT = np.array([[  9.99570940e-01,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  9.96169619e-01,   1.99914188e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  9.77249868e-01,   1.99233924e+00,   2.99871282e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  9.08788780e-01,   1.95449974e+00,   2.98850886e+00,
          3.99828376e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  7.47507462e-01,   1.81757756e+00,   2.93174960e+00,
          3.98467848e+00,   4.99785470e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  5.00000000e-01,   1.49501492e+00,   2.72636634e+00,
          3.90899947e+00,   4.98084810e+00,   5.99742564e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  2.52492538e-01,   1.00000000e+00,   2.24252239e+00,
          3.63515512e+00,   4.88624934e+00,   5.97701772e+00,
          6.99699658e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  9.12112197e-02,   5.04985075e-01,   1.50000000e+00,
          2.99002985e+00,   4.54394390e+00,   5.86349921e+00,
          6.97318734e+00,   7.99656752e+00,   0.00000000e+00,
          0.00000000e+00],
       [  2.27501319e-02,   1.82422439e-01,   7.57477613e-01,
          2.00000000e+00,   3.73753731e+00,   5.45273268e+00,
          6.84074908e+00,   7.96935696e+00,   8.99613846e+00,
          0.00000000e+00],
       [  3.83038057e-03,   4.55002639e-02,   2.73633659e-01,
          1.00997015e+00,   2.50000000e+00,   4.48504477e+00,
          6.36152146e+00,   7.81799894e+00,   8.96552657e+00,
          9.99570940e+00]])

Stock_T_NormLT = np.array([  0.99957094,   2.9953115 ,   5.96830193,   9.85008113,
        14.4793678 ,  19.60865447,  24.99043368,  30.46342411,
        35.95916467,  41.45873561])

Outflow_T_NormLT = np.array([  4.29060333e-04,   4.25944090e-03,   2.70095728e-02,
         1.18220793e-01,   3.70713330e-01,   8.70713330e-01,
         1.61822079e+00,   2.52700957e+00,   3.50425944e+00,
         4.50042906e+00])

Outflow_TC_NormLT = np.array([[  4.29060333e-04,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       [  3.40132023e-03,   8.58120666e-04,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  1.89197514e-02,   6.80264047e-03,   1.28718100e-03,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  6.84610878e-02,   3.78395028e-02,   1.02039607e-02,
          1.71624133e-03,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  1.61281318e-01,   1.36922176e-01,   5.67592541e-02,
          1.36052809e-02,   2.14530167e-03,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  2.47507462e-01,   3.22562636e-01,   2.05383263e-01,
          7.56790055e-02,   1.70066012e-02,   2.57436200e-03,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  2.47507462e-01,   4.95014925e-01,   4.83843953e-01,
          2.73844351e-01,   9.45987569e-02,   2.04079214e-02,
          3.00342233e-03,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00],
       [  1.61281318e-01,   4.95014925e-01,   7.42522387e-01,
          6.45125271e-01,   3.42305439e-01,   1.13518508e-01,
          2.38092416e-02,   3.43248267e-03,  -0.00000000e+00,
         -0.00000000e+00],
       [  6.84610878e-02,   3.22562636e-01,   7.42522387e-01,
          9.90029850e-01,   8.06406589e-01,   4.10766527e-01,
          1.32438260e-01,   2.72105619e-02,   3.86154300e-03,
         -0.00000000e+00],
       [  1.89197514e-02,   1.36922176e-01,   4.83843953e-01,
          9.90029850e-01,   1.23753731e+00,   9.67687907e-01,
          4.79227614e-01,   1.51358011e-01,   3.06118821e-02,
          4.29060333e-03]])

StockChange_T_NormLT = np.array([ 0.99957094,  1.99574056,  2.97299043,  3.88177921,  4.62928667,
        5.12928667,  5.38177921,  5.47299043,  5.49574056,  5.49957094])

"""My Output for Weibull-distributed lifetime"""

Stock_TC_WeibullLT = np.array([[1, 0,  0,  0,  0,  0,  0, 0, 0,  0],  # computed with Excel and taken from there
                             [0.367879441,	2,	0,	0,	0,	0,	0,	0,	0,	0],
                             [0.100520187,	0.735758882,	3,	0,	0,	0,	0,	0,	0,	0],
                            [0.023820879,	0.201040373,	1.103638324,	4,	0,	0,	0,	0,	0,	0],
                            [0.005102464,	0.047641758,	0.30156056,	1.471517765,5,	0,	0,	0,	0,	0],
                            [0.001009149,	0.010204929,	0.071462637,	0.402080746,1.839397206,	6,	0,	0,	0,	0],
                            [0.000186736,	0.002018297,	0.015307393,	0.095283516,	0.502600933,	2.207276647,	7,	0,	0,	0],
                            [3.26256E-05,	0.000373472,	0.003027446,	0.020409858,	0.119104394,	0.60312112,	2.575156088,	8,	0,	0],
                            [5.41828E-06,	6.52513E-05,	0.000560208,	0.004036594,	0.025512322,	0.142925273,	0.703641306,	2.943035529,	9,	0],
                            [8.59762E-07,	1.08366E-05,	9.78769E-05,	0.000746944,	0.005045743,	0.030614786,	0.166746152,	0.804161493,	3.310914971,	10]])

Stock_T_WeibullLT = np.array([1,2.367879441,3.836279069,5.328499576,6.825822547,8.324154666,9.822673522,11.321225,12.8197819,14.31833966])

Outflow_T_WeibullLT = np.array([0,0.632120559,1.531600372,2.507779493,3.502677029,4.50166788,5.501481144,6.501448519,7.5014431,8.501442241])

Outflow_TC_WeibullLT = np.array([[0,	0,	0,	0,	0,	0,	0,	0,	0,	0],
                                [0.632120559,	0,	0,	0,	0,	0,	0,	0,	0,	0],
                                [0.267359255,	1.264241118,	0,	0,	0,	0,	0,	0,	0,	0],
                                [0.076699308,	0.534718509,	1.896361676,	0,	0,	0,	0,	0,	0,	0],
                                [0.018718414,	0.153398615,	0.802077764,	2.528482235,	0,	0,	0,	0,	0,	0],
                                [0.004093316,	0.037436829,	0.230097923,	1.069437018,	3.160602794,	0,	0,	0,	0,	0],
                                [0.000822413,	0.008186632,	0.056155243,	0.306797231,	1.336796273,	3.792723353,	0,	0,	0,	0],
                                [0.00015411,	0.001644825,	0.012279947,	0.074873658,	0.383496539,	1.604155527,	4.424843912,	0,	0,	0],
                                [2.72074E-05,	0.000308221,	0.002467238,	0.016373263,	0.093592072,	0.460195846,	1.871514782,	5.056964471,	0,	0],
                                [4.55852E-06, 5.44147E-05	,     0.000462331	,     0.00328965,   	0.020466579,	0.112310487,	0.536895154,	2.138874037,	5.689085029,	0]])

StockChange_T_WeibullLT = np.array([1,1.367879441,1.468399628,1.492220507,1.497322971,1.49833212,1.498518856,1.498551481,1.4985569,1.498557759])

lifetime_WeibullLT = {'Type': 'Weibull', 'Shape': np.array([1.2]), 'Scale': np.array([1])}
InitialStock_WB = np.array([0.01, 0.01, 0.08, 0.2,  0.2,  2,  2,  3,  4,  7.50])
Inflow_WB = np.array([11631.1250671964,	1845.6048709861,	2452.0593141014,	1071.0305279511,	198.1868742385,	391.9674590243,	83.9599583940,	29.8447516023,	10.8731273138,	7.5000000000])
# We need 10 digits AFTER the . to get a 9 digits after the . overlap with np.testing.
# The total number of counting digits is higher, because there are up to 5 digits before the .

# For the stock-driven model with initial stock, colculated with Excel
Sc_InitialStock_2_Ref = np.array([[ 3.29968072,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ],
       [ 3.28845263,  5.1142035 ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ],
       [ 3.2259967 ,  5.09680099,  2.0068288 ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ],
       [ 3.        ,  5.        ,  2.        ,  4.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ],
       [ 2.46759471,  4.64972578,  1.962015  ,  3.98638888,  4.93427563,
         0.        ,  0.        ,  0.        ,  0.        ],
       [ 1.65054855,  3.82454624,  1.82456634,  3.91067739,  4.91748538,
         3.8721761 ,  0.        ,  0.        ,  0.        ],
       [ 0.83350238,  2.55819937,  1.50076342,  3.63671549,  4.82409004,
         3.85899993,  2.78772936,  0.        ,  0.        ],
       [ 0.30109709,  1.2918525 ,  1.00384511,  2.9913133 ,  4.48613916,
         3.78570788,  2.77824333,  3.36180162,  0.        ],
       [ 0.07510039,  0.46667297,  0.5069268 ,  2.00085849,  3.68999109,
         3.5205007 ,  2.72547754,  3.35036215,  3.66410986]])

Sc_InitialStock_2_Ref_Sum = np.array([  3.29968072,   8.40265614,  10.32962649,  14.        ,
        18.        ,  20.        ,  20.        ,  20.        ,  20.        ])

Oc_InitialStock_2_Ref = np.array([[  1.41636982e-03,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00],
       [  1.12280883e-02,   2.19524375e-03,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00],
       [  6.24559363e-02,   1.74025106e-02,   8.61420234e-04,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00],
       [  2.25996698e-01,   9.68009922e-02,   6.82879736e-03,
          1.71697802e-03,  -0.00000000e+00,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00],
       [  5.32405289e-01,   3.50274224e-01,   3.79849998e-02,
          1.36111209e-02,   2.11801070e-03,  -0.00000000e+00,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00],
       [  8.17046165e-01,   8.25179532e-01,   1.37448656e-01,
          7.57114903e-02,   1.67902556e-02,   1.66211031e-03,
         -0.00000000e+00,  -0.00000000e+00,  -0.00000000e+00],
       [  8.17046165e-01,   1.26634687e+00,   3.23802924e-01,
          2.73961897e-01,   9.33953405e-02,   1.31761643e-02,
          1.19661751e-03,  -0.00000000e+00,  -0.00000000e+00],
       [  5.32405289e-01,   1.26634687e+00,   4.96918311e-01,
          6.45402188e-01,   3.37950879e-01,   7.32920558e-02,
          9.48603036e-03,   1.44303487e-03,  -0.00000000e+00],
       [  2.25996698e-01,   8.25179532e-01,   4.96918311e-01,
          9.90454815e-01,   7.96148072e-01,   2.65207178e-01,
          5.27657861e-02,   1.14394721e-02,   1.57279902e-03]])

I_InitialStock_2_Ref  = np.array([ 3.30109709,  5.11639875,  2.00769022,  4.00171698,  4.93639364,  3.87383821,  2.78892598,  3.36324466,  3.66568266])

""" Test case with fixed lifetime for initial stock"""
Time_T_FixedLT_X = np.arange(1, 9, 1)
lifetime_FixedLT_X = {'Type': 'Fixed', 'Mean': np.array([5])}
InitialStock_X = np.array([0, 0, 0, 7, 5, 4, 3, 2])
Inflow_X = np.array([0, 0, 0, 7, 5, 4, 3, 2])

Time_T_FixedLT_XX = np.arange(1, 11, 1)
lifetime_NormLT_X = {'Type': 'Normal', 'Mean': np.array([5]), 'StdDev': np.array([1.5])}
InitialStock_XX = np.array([0.01, 0.01, 0.08, 0.2,  0.2,  2,  2,  3,  4,  7.50])
Inflow_XX = np.array([ 2.61070664,  0.43955789,  0.87708508,  0.79210262,  0.4,
        2.67555857,  2.20073139,  3.06983925,  4.01538044,  7.50321933])

""" Test case with normally distributed lifetime for initial stock and stock-driven model"""
Time_T_FixedLT_2  = np.arange(1, 10, 1)
lifetime_NormLT_2 = {'Type': 'Normal', 'Mean': np.array([5]), 'StdDev': np.array([1.5])}
InitialStock_2    = np.array([3,5,2,4])
FutureStock_2     = np.array([0,0,0,0,18,20,20,20,20])
ThisSwitchTime    = 5 # First year with future stock curve, start counting from 1.
Inflow_2          = np.array([3.541625588,	5.227890554,2.01531097,4])

###############################################################################
"""Create Dynamic Stock Models and hand over the pre-defined values."""
# For zero lifetime: border case
myDSM0 = dsm.DynamicStockModel(t=Time_T_FixedLT, i=Inflow_T_FixedLT, lt=lifetime_FixedLT0)

# For fixed LT
myDSM = dsm.DynamicStockModel(t=Time_T_FixedLT, i=Inflow_T_FixedLT, lt=lifetime_FixedLT)

myDSM2 = dsm.DynamicStockModel(t=Time_T_FixedLT, s=Stock_T_FixedLT, lt=lifetime_FixedLT)

myDSMx = dsm.DynamicStockModel(t=Time_T_FixedLT_X, lt=lifetime_FixedLT_X)
TestInflow_X = myDSMx.compute_i_from_s(InitialStock=InitialStock_X)

myDSMxy = dsm.DynamicStockModel(t=Time_T_FixedLT_X, i=TestInflow_X, lt=lifetime_FixedLT_X)

# For zero normally distributed lifetime: border case
myDSM0n = dsm.DynamicStockModel(t=Time_T_FixedLT, i=Inflow_T_FixedLT, lt=lifetime_NormLT0)

# For normally distributed Lt
myDSM3 = dsm.DynamicStockModel(t=Time_T_FixedLT, i=Inflow_T_FixedLT, lt=lifetime_NormLT)

myDSM4 = dsm.DynamicStockModel(t=Time_T_FixedLT, s=Stock_T_NormLT, lt=lifetime_NormLT)

myDSMX = dsm.DynamicStockModel(t=Time_T_FixedLT_XX, lt=lifetime_NormLT_X)
TestInflow_XX = myDSMX.compute_i_from_s(InitialStock=InitialStock_XX)

myDSMXY = dsm.DynamicStockModel(t=Time_T_FixedLT_XX, i=TestInflow_XX, lt=lifetime_NormLT_X)

# Test compute_stock_driven_model_initialstock:
TestDSM_IntitialStock = dsm.DynamicStockModel(t=Time_T_FixedLT_2, s=FutureStock_2, lt=lifetime_NormLT_2)
Sc_InitialStock_2,Oc_InitialStock_2,I_InitialStock_2 = TestDSM_IntitialStock.compute_stock_driven_model_initialstock(InitialStock = InitialStock_2, SwitchTime = ThisSwitchTime)
# Compute stock back from inflow
TestDSM_IntitialStock_Verify = dsm.DynamicStockModel(t=Time_T_FixedLT_2, i=I_InitialStock_2, lt=lifetime_NormLT_2)
Sc_Stock_2 = TestDSM_IntitialStock_Verify.compute_s_c_inflow_driven()
Sc_Stock_2_Sum = Sc_Stock_2.sum(axis =1)
Sc_Stock_Sum   = TestDSM_IntitialStock_Verify.compute_stock_total()
Sc_Outflow_t_c = TestDSM_IntitialStock_Verify.compute_o_c_from_s_c()

# For Weibull-distributed Lt
myDSMWB1 = dsm.DynamicStockModel(t=Time_T_FixedLT, i=Inflow_T_FixedLT, lt=lifetime_WeibullLT)

myDSMWB2 = dsm.DynamicStockModel(t=Time_T_FixedLT, s=Stock_T_WeibullLT, lt=lifetime_WeibullLT)

myDSMWB3 = dsm.DynamicStockModel(t=Time_T_FixedLT_XX, lt=lifetime_WeibullLT)
TestInflow_WB = myDSMWB3.compute_i_from_s(InitialStock=InitialStock_XX)

myDSMWB4 = dsm.DynamicStockModel(t=Time_T_FixedLT_XX, i=TestInflow_WB, lt=lifetime_WeibullLT)
# Compute full stock model in correct order
###############################################################################
"""Unit Test Class"""

class KnownResultsTestCase(unittest.TestCase):

    def test_inflow_driven_model_fixedLifetime_0(self):
        """Test Inflow Driven Model with Fixed product lifetime of 0."""
        np.testing.assert_array_equal(myDSM0.compute_s_c_inflow_driven(), np.zeros(Stock_TC_FixedLT.shape))
        np.testing.assert_array_equal(myDSM0.compute_stock_total(), np.zeros((Stock_TC_FixedLT.shape[0])))
        np.testing.assert_array_equal(myDSM0.compute_stock_change(), np.zeros((Stock_TC_FixedLT.shape[0])))
        np.testing.assert_array_equal(myDSM0.compute_outflow_mb(), Inflow_T_FixedLT)
        np.testing.assert_array_equal(myDSM0.check_stock_balance(), Bal.transpose())

    def test_inflow_driven_model_fixedLifetime(self):
        """Test Inflow Driven Model with Fixed product lifetime."""
        np.testing.assert_array_equal(myDSM.compute_s_c_inflow_driven(), Stock_TC_FixedLT)
        np.testing.assert_array_equal(myDSM.compute_stock_total(),Stock_T_FixedLT)
        np.testing.assert_array_equal(myDSM.compute_o_c_from_s_c(), Outflow_TC_FixedLT)
        np.testing.assert_array_equal(myDSM.compute_outflow_total(), Outflow_T_FixedLT)
        np.testing.assert_array_equal(myDSM.compute_stock_change(), StockChange_T_FixedLT)
        np.testing.assert_array_equal(myDSM.check_stock_balance(), Bal.transpose())

    def test_stock_driven_model_fixedLifetime(self):
        """Test Stock Driven Model with Fixed product lifetime."""
        np.testing.assert_array_equal(myDSM2.compute_stock_driven_model()[0], Stock_TC_FixedLT)
        np.testing.assert_array_equal(myDSM2.compute_stock_driven_model()[1], Outflow_TC_FixedLT)
        np.testing.assert_array_equal(myDSM2.compute_stock_driven_model()[2], Inflow_T_FixedLT)
        np.testing.assert_array_equal(myDSM2.compute_outflow_total(), Outflow_T_FixedLT)
        np.testing.assert_array_equal(myDSM2.compute_stock_change(), StockChange_T_FixedLT)
        np.testing.assert_array_equal(myDSM2.check_stock_balance(), Bal.transpose())

    def test_inflow_driven_model_normallyDistrLifetime_0(self):
        """Test Inflow Driven Model with Fixed product lifetime of 0."""
        np.testing.assert_array_equal(myDSM0n.compute_s_c_inflow_driven(), np.zeros(Stock_TC_FixedLT.shape))
        np.testing.assert_array_equal(myDSM0n.compute_stock_total(), np.zeros((Stock_TC_FixedLT.shape[0])))
        np.testing.assert_array_equal(myDSM0n.compute_stock_change(), np.zeros((Stock_TC_FixedLT.shape[0])))
        np.testing.assert_array_equal(myDSM0n.compute_outflow_mb(), Inflow_T_FixedLT)
        np.testing.assert_array_equal(myDSM0n.check_stock_balance(), Bal.transpose())

    def test_inflow_driven_model_normallyDistLifetime(self):
        """Test Inflow Driven Model with normally distributed product lifetime."""
        np.testing.assert_array_almost_equal(myDSM3.compute_s_c_inflow_driven(), Stock_TC_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM3.compute_stock_total(), Stock_T_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM3.compute_o_c_from_s_c(), Outflow_TC_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM3.compute_outflow_total(), Outflow_T_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM3.compute_stock_change(), StockChange_T_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM3.check_stock_balance(), Bal.transpose(), 12)

    def test_stock_driven_model_normallyDistLifetime(self):
        """Test Stock Driven Model with normally distributed product lifetime."""
        np.testing.assert_array_almost_equal(
            myDSM4.compute_stock_driven_model()[0], Stock_TC_NormLT, 8)
        np.testing.assert_array_almost_equal(
            myDSM4.compute_stock_driven_model()[1], Outflow_TC_NormLT, 8)
        np.testing.assert_array_almost_equal(
            myDSM4.compute_stock_driven_model()[2], Inflow_T_FixedLT, 8)
        np.testing.assert_array_almost_equal(myDSM4.compute_outflow_total(), Outflow_T_NormLT, 8)
        np.testing.assert_array_almost_equal(
            myDSM4.compute_stock_change(), StockChange_T_NormLT, 8)
        np.testing.assert_array_almost_equal(myDSM4.check_stock_balance(), Bal.transpose(), 12)

    def test_inflow_driven_model_WeibullDistLifetime(self):
        """Test Inflow Driven Model with Weibull-distributed product lifetime."""
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_s_c_inflow_driven(), Stock_TC_WeibullLT, 9)
        np.testing.assert_array_almost_equal(myDSMWB1.compute_stock_total(), Stock_T_WeibullLT, 8)
        np.testing.assert_array_almost_equal(myDSMWB1.compute_o_c_from_s_c(), Outflow_TC_WeibullLT, 9)
        np.testing.assert_array_almost_equal(myDSMWB1.compute_outflow_total(), Outflow_T_WeibullLT, 9)
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_stock_change(), StockChange_T_WeibullLT, 9)
        np.testing.assert_array_almost_equal(myDSMWB1.check_stock_balance(), Bal.transpose(), 12)

    def test_stock_driven_model_WeibullDistLifetime(self):
        """Test Stock Driven Model with Weibull-distributed product lifetime."""
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_stock_driven_model()[0], Stock_TC_WeibullLT, 8)
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_stock_driven_model()[1], Outflow_TC_WeibullLT, 8)
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_stock_driven_model()[2], Inflow_T_FixedLT, 8)
        np.testing.assert_array_almost_equal(myDSMWB1.compute_outflow_total(), Outflow_T_WeibullLT, 9)
        np.testing.assert_array_almost_equal(
            myDSMWB1.compute_stock_change(), StockChange_T_WeibullLT, 8)
        np.testing.assert_array_almost_equal(myDSMWB1.check_stock_balance(), Bal.transpose(), 12)

    def test_inflow_from_stock_fixedLifetime(self):
        """Test computation of inflow from stock with Fixed product lifetime."""
        np.testing.assert_array_equal(TestInflow_X, Inflow_X)
        np.testing.assert_array_equal(myDSMxy.compute_s_c_inflow_driven()[-1, :], InitialStock_X)

    def test_inflow_from_stock_normallyDistLifetime(self):
        """Test computation of inflow from stock with normally distributed product lifetime."""
        np.testing.assert_array_almost_equal(TestInflow_XX, Inflow_XX, 8)
        np.testing.assert_array_almost_equal(myDSMXY.compute_s_c_inflow_driven()[-1, :], InitialStock_XX, 9)

    def test_inflow_from_stock_WeibullDistLifetime(self):
        """Test computation of inflow from stock with Weibull-distributed product lifetime."""
        np.testing.assert_array_almost_equal(TestInflow_WB, Inflow_WB, 9)
        np.testing.assert_array_almost_equal(myDSMWB4.compute_s_c_inflow_driven()[-1, :], InitialStock_WB, 9)

    def test_compute_stock_driven_model_initialstock(self):
        """Test stock-driven model with initial stock given."""
        np.testing.assert_array_almost_equal(I_InitialStock_2, I_InitialStock_2_Ref, 8)
        np.testing.assert_array_almost_equal(Sc_InitialStock_2, Sc_InitialStock_2_Ref, 8)
        np.testing.assert_array_almost_equal(Sc_InitialStock_2.sum(axis =1), Sc_InitialStock_2_Ref_Sum, 8)
        np.testing.assert_array_almost_equal(Oc_InitialStock_2, Oc_InitialStock_2_Ref, 8)

    if __name__ == '__main__':
        unittest.main()

max_stars_repo_name: DominikWiedenhofer/ODYM

max_issues_repo_name: DominikWiedenhofer/ODYM

max_forks_repo_name: DominikWiedenhofer/ODYM


text:
# Third party code
#
# The following code are copied or modified from:
# https://github.com/google-research/motion_imitation

"""The inverse kinematic utilities."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import typing

_IDENTITY_ORIENTATION = (0, 0, 0, 1)

def joint_angles_from_link_position(
    robot: typing.Any,
    link_position: typing.Sequence[float],
    link_id: int,
    joint_ids: typing.Sequence[int],
    base_translation: typing.Sequence[float] = (0, 0, 0),
    base_rotation: typing.Sequence[float] = (0, 0, 0, 1)):
  """Uses Inverse Kinematics to calculate joint angles.

  Args:
    robot: A robot instance.
    link_position: The (x, y, z) of the link in the body frame. This local frame
      is transformed relative to the COM frame using a given translation and
      rotation.
    link_id: The link id as returned from loadURDF.
    joint_ids: The positional index of the joints. This can be different from
      the joint unique ids.
    base_translation: Additional base translation.
    base_rotation: Additional base rotation.

  Returns:
    A list of joint angles.
  """
  # Projects to local frame.
  base_position, base_orientation = robot.GetBasePosition(
  ), robot.GetBaseOrientation()
  base_position, base_orientation = robot.pybullet_client.multiplyTransforms(
      base_position, base_orientation, base_translation, base_rotation)

  # Projects to world space.
  world_link_pos, _ = robot.pybullet_client.multiplyTransforms(
      base_position, base_orientation, link_position, _IDENTITY_ORIENTATION)
  ik_solver = 0
  all_joint_angles = robot.pybullet_client.calculateInverseKinematics(
      robot.quadruped, link_id, world_link_pos, solver=ik_solver)

  # Extract the relevant joint angles.
  joint_angles = [all_joint_angles[i] for i in joint_ids]
  return joint_angles

def link_position_in_base_frame(
    robot: typing.Any,
    link_id: int,
):
  """Computes the link's local position in the robot frame.

  Args:
    robot: A robot instance.
    link_id: The link to calculate its relative position.

  Returns:
    The relative position of the link.
  """
  base_position, base_orientation = robot.GetBasePosition(
  ), robot.GetBaseOrientation()
  inverse_translation, inverse_rotation = robot.pybullet_client.invertTransform(
      base_position, base_orientation)

  link_state = robot.pybullet_client.getLinkState(robot.quadruped, link_id)
  link_position = link_state[0]
  link_local_position, _ = robot.pybullet_client.multiplyTransforms(
      inverse_translation, inverse_rotation, link_position, (0, 0, 0, 1))
  # link_local_position = [0]*3
  return np.array(link_local_position)

def compute_jacobian(
    robot: typing.Any,
    link_id: int,
):
  """Computes the Jacobian matrix for the given link.

  Args:
    robot: A robot instance.
    link_id: The link id as returned from loadURDF.

  Returns:
    The 3 x N transposed Jacobian matrix. where N is the total DoFs of the
    robot. For a quadruped, the first 6 columns of the matrix corresponds to
    the CoM translation and rotation. The columns corresponds to a leg can be
    extracted with indices [6 + leg_id * 3: 6 + leg_id * 3 + 3].
  """

  all_joint_angles = [state[0] for state in robot.joint_states]
  zero_vec = [0] * len(all_joint_angles)
  jv, _ = robot.pybullet_client.calculateJacobian(robot.quadruped, link_id,
                                                  (0, 0, 0), all_joint_angles,
                                                  zero_vec, zero_vec)
  jacobian = np.array(jv)
  return jacobian


text:
import os
import numpy as np
import open3d as o3d
from scipy.io import savemat

def main(dir_root):
    depth_path = "data/dtaas/dtaas_trainval/depth"
    if not os.path.exists(depth_path):
        os.makedirs(depth_path)

    dir_names = os.listdir(dir_root)
    dir_names.sort()
    for idx, dir_name in enumerate(dir_names):
        pts_name = f"{dir_root}/{dir_name}/{dir_name}.pts"

        with open(pts_name, "rt", encoding = 'utf-8') as fr:
            points = []
            fr.readline() # Skip a header.
            while True:
                line = fr.readline()
                if not line:
                    break;
                points.append(line.strip().split(" "))
            npPoints = np.array(points)
            print(npPoints.shape)
            floatPoint = npPoints[:, 0:3].astype(float)
            intPoint = npPoints[:, 3:6].astype(int)
            binPoint = np.concatenate((floatPoint, intPoint), axis=1)

            savemat(f"{depth_path}/{idx+1:06d}.mat", {'instance': binPoint})

if __name__ == '__main__':
    dir_root = 'data/dtaas/DTAAS'
    main(
        dir_root=dir_root
    )

reason: import numpy,from scipy


text:
#!/usr/bin/env python3

import numpy as np
import numpy.random as npr

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('bmh')

for i in range(5):
    nz, neq, nineq = 2,0,10
    G = npr.uniform(-1., 1., (nineq,nz))
    z0 = np.zeros(nz)
    s0 = np.ones(nineq)

    l, u = -4, 4
    b = np.linspace(l, u, num=1000)
    C, D = np.meshgrid(b, b)
    Z = []
    for c,d in zip(C.ravel(), D.ravel()):
        x = np.array([c,d])
        z = np.all(G.dot(x) <= G.dot(z0)+s0).astype(np.float32)
        Z.append(z)
    Z = np.array(Z).reshape(C.shape)

    fig, ax = plt.subplots(1, 1, figsize=(8,8))
    plt.axis([l, u, l, u])
    CS = plt.contourf(C, D, Z, cmap=plt.cm.Blues)
    f = 'data/2016-11-02/init.{}.png'.format(i)
    plt.savefig(f)
    print('created '+f)

max_stars_repo_name: titaneric/optnet

max_issues_repo_name: titaneric/optnet

max_forks_repo_name: titaneric/optnet


text:
import numpy as np
from utils import in_model
from config import opt
import process
import warnings

warnings.filterwarnings("ignore")

class train_Dataset:
    def __init__(self, img_list):
        self.img_path = opt.path_img
        self.img_list = img_list

        return

    def __getitem__(self, idx):
        path_name = self.img_list[idx]

        # for img
        tmp_data = in_model.get_img(self.img_path, path_name)

        img_dict = process.train_preprocess(tmp_data)
        img_list = [img_dict['T1'], img_dict['T2'], img_dict['FL']]
        img = np.array(img_list)
        img = img.astype('float32')

        bbox_array = img_dict['BBOX']
        annot = in_model.get_bbox(bbox_array)
        annot = annot.astype('float32')

        return_list = [path_name, img, annot]

        return return_list

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

class val_Dataset:
    def __init__(self, img_list):
        self.img_path = opt.path_img
        self.img_list = img_list
        return

    def __getitem__(self, idx):
        path_name = self.img_list[idx]

        # for img
        tmp_data = in_model.get_img(self.img_path, path_name)

        img_dict = process.val_preprocess(tmp_data)
        img_list = [img_dict['T1'], img_dict['T2'], img_dict['FL']]
        img = np.array(img_list)
        img = img.astype('float32')

        bbox_array = img_dict['BBOX']
        annot = in_model.get_bbox(bbox_array)
        annot = annot.astype('float32')

        return_list = [path_name, img, annot]

        return return_list

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

max_stars_repo_name: zhanghaoyue/MMMT-DL

max_issues_repo_name: zhanghaoyue/MMMT-DL

max_forks_repo_name: zhanghaoyue/MMMT-DL


text:
import matplotlib
matplotlib.use('agg')
import  matplotlib.pyplot as plt
import numpy as np

def plot_confusion_matrix(mx, save_file_name='mx.png'):
    fig = plt.figure(figsize=(8,5))
    ax = fig.add_subplot(111)
    cax = ax.matshow(mx, cmap=plt.cm.bone)
    ticks = np.arange(0, 10, 1)
    ax.set_xticks(ticks)
    ax.set_yticks(ticks)
    ax.set_xticklabels(ticks)
    ax.set_yticklabels(ticks)
    fig.colorbar(cax)
    plt.savefig(save_file_name, format='png', dpi=300)

def plot_digits(instances, images_per_row=10, save_file_name='digits.png', **options):
    plt.figure(figsize=(9,9))
    size = 28
    images_per_row = min(len(instances), images_per_row)
    images = [instance.reshape(size,size) for instance in instances]
    n_rows = (len(instances) - 1) // images_per_row + 1
    row_images = []
    n_empty = n_rows * images_per_row - len(instances)
    images.append(np.zeros((size, size * n_empty)))
    for row in range(n_rows):
        rimages = images[row * images_per_row : (row + 1) * images_per_row]
        row_images.append(np.concatenate(rimages, axis=1))
    image = np.concatenate(row_images, axis=0)
    plt.imshow(image, cmap = matplotlib.cm.binary, **options)
    plt.axis("off")
    plt.savefig(save_file_name, format='png', dpi=300)

def plot_digit(data):
    image = data.reshape(28, 28)
    plt.imshow(image, cmap = matplotlib.cm.binary,
               interpolation="nearest")
    plt.axis("off")


text:
#!/usr/bin/env python
# coding: utf-8

# # A Recommendation Engine for The Recipes by Using Collaborative Filtering in Python

# In[1]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.metrics.pairwise import pairwise_distances

# In[2]:

I = pd.read_csv('./Data_Source/interactions_train.csv')
R = pd.read_csv('./Data_Source/RAW_recipes.csv')
I.info()
R.info()

# In[3]:

I.rating.value_counts().plot(kind = 'bar', fontsize = 14,
                             figsize = (5,2)).set_title('Distribution of Rating',
                                                        fontsize = 16, ha = 'center', va = 'bottom')

plt.show()

# ## Clean up the data

# In[4]:

_all = I.drop(['date', 'u', 'i'], axis = 1)
_all

# In[5]:

grouped_1 = _all.groupby(['user_id'], as_index = False, sort = False).agg({'recipe_id':'count'}).reset_index(drop = True)
grouped_1 = grouped_1.rename(columns = {'recipe_id':'reviews_count'})
grouped_1 = grouped_1.sort_values('reviews_count', ascending = False).iloc[:7500,:]
grouped_1

# In[6]:

grouped_2 = _all.groupby(['recipe_id'], as_index = False, sort = False).agg({'user_id':'count'}).reset_index(drop = True)
grouped_2 = grouped_2.rename(columns = {'user_id':'reviews_count'})
grouped_2 = grouped_2.sort_values('reviews_count', ascending = False).iloc[:7500,:]
grouped_2

# In[7]:

_part = pd.merge(_all.merge(grouped_1).drop(['reviews_count'], axis = 1), grouped_2).drop(['reviews_count'], axis = 1)
_part

# In[8]:

print('unique users:',len(_part.user_id.unique()))
print('unique recipes:',len(_part.recipe_id.unique()))

# In[9]:

grouped_user = _part.groupby(['user_id'], as_index = False, sort = False).agg({'recipe_id':'count'}).reset_index(drop = True)
grouped_user = grouped_user.rename(columns = {'recipe_id':'reviews_count'})

grouped_recipe = _part.groupby(['recipe_id'], as_index = False, sort = False).agg({'user_id':'count'}).reset_index(drop = True)
grouped_recipe = grouped_recipe.rename(columns = {'user_id':'reviews_count'})

display(grouped_user[['reviews_count']].describe())
display(grouped_recipe[['reviews_count']].describe())

# In[10]:

_part.rating.value_counts().plot(kind = 'bar', fontsize = 14,
                                            figsize = (5,2)).set_title('Distribution of Rating',
                                                                      fontsize = 16, ha = 'center', va = 'bottom')

plt.show()

# In[11]:

new_userID = dict(zip(list(_part['user_id'].unique()),
                      list(range(len(_part['user_id'].unique())))))
display(new_userID)

new_recipeID = dict(zip(list(_part['recipe_id'].unique()),
                      list(range(len(_part['recipe_id'].unique())))))
display(new_recipeID)

# In[12]:

df = _part.replace({'user_id': new_userID, 'recipe_id': new_recipeID})
df

# In[13]:

print('The recipes without names: ', R['id'][R['name'].isnull()].values[0])
display(df[df['recipe_id'] == R['id'][R['name'].isnull()].values[0]])

# In[16]:

recipe = R[['name', 'id', 'ingredients']].merge(_part[['recipe_id']],
                                                left_on = 'id', right_on = 'recipe_id',
                                                how = 'right').drop(['id'], axis = 1).drop_duplicates().reset_index(drop = True)
recipe

# In[14]:

print('unique users:',len(_part.user_id.unique()))
print('unique recipes:',len(_part.recipe_id.unique()))

# In[15]:

mean = df.groupby(['user_id'], as_index = False, sort = False).mean().rename(columns = {'rating':'rating_mean'})
df = df.merge(mean[['user_id','rating_mean']], how = 'left')
df.insert(2, 'rating_adjusted', df['rating'] - df['rating_mean'])
df

# In[17]:

train_data, test_data = train_test_split(df, test_size = 0.25)
display(train_data)
display(test_data)

# In[18]:

n_users = df.user_id.unique()
n_items = df.recipe_id.unique()

train_data_matrix = np.zeros((n_users.shape[0], n_items.shape[0]))
for row in train_data.itertuples():
    train_data_matrix[row[1]-1, row[2]-1] = row[3]

display(train_data_matrix.shape)
display(train_data_matrix)

# In[19]:

test_data_matrix = np.zeros((n_users.shape[0], n_items.shape[0]))
for row in test_data.itertuples():
    test_data_matrix[row[1]-1, row[2]-1] = row[3]

display(test_data_matrix.shape)
display(test_data_matrix)

# ## Centered cosine similarity

# In[20]:

user_similarity = 1 - pairwise_distances(train_data_matrix, metric = 'cosine')

display(user_similarity.shape)
display(user_similarity)

# In[21]:

item_similarity = 1 - pairwise_distances(train_data_matrix.T, metric = 'cosine')

display(item_similarity.shape)
display(item_similarity)

# ## Predict the ratings

# In[22]:

def predict(ratings, similarity, _type = 'user'):
    if _type == 'user':
        pred = similarity.dot(ratings) / np.array([np.abs(similarity).sum(axis = np.newaxis)])

    elif _type == 'item':
        pred = ratings.dot(similarity) / np.array([np.abs(similarity).sum(axis = 1)])

    return pred

# In[23]:

user_pred = predict(train_data_matrix, user_similarity, _type = 'user')

display(user_pred.shape)
display(user_pred)

# In[24]:

user_pred_df = pd.DataFrame(user_pred, columns = list(n_items))
user_pred_df.insert(0, 'user_id', list(n_users))

user_pred_df

# In[25]:

item_pred = predict(train_data_matrix, item_similarity, _type = 'item')

display(item_pred.shape)
display(item_pred)

# In[26]:

item_pred_df = pd.DataFrame(item_pred, columns = list(n_items))
item_pred_df.insert(0, 'user_id', list(n_users))

item_pred_df

# ## Evaluation of the predictions

# In[27]:

def RMSE(prediction, ground_truth):
    prediction = prediction[ground_truth.nonzero()].flatten()
    ground_truth = ground_truth[ground_truth.nonzero()].flatten()

    return sqrt(mean_squared_error(prediction, ground_truth))

# In[28]:

user_RMSE = RMSE(user_pred, test_data_matrix)
item_RMSE = RMSE(item_pred, test_data_matrix)

print('user_RMSE = {}'.format(user_RMSE))
print('item_RMSE = {}'.format(item_RMSE))

# ## The recommendation engine

# In[29]:

def getRecommendations_UserBased(user_id, top_n = 10):
    for old_user, new_user in new_userID.items():
        if user_id == new_user:
            print(f'Top {top_n} Recommended Recipes for Original User ID: {old_user}\n')

    movie_rated = list(df['recipe_id'].loc[df['user_id'] == user_id])
    _all = user_pred_df.loc[user_pred_df['user_id'] == user_id].copy()
    _all.drop(user_pred_df[movie_rated], axis = 1, inplace = True)
    unwatch_sorted = _all.iloc[:,1:].sort_values(by = _all.index[0], axis = 1, ascending = False)
    dict_top_n = unwatch_sorted.iloc[:, :top_n].to_dict(orient = 'records')

    i = 1
    for recipe_id in list(dict_top_n[0].keys()):
        for old_recipe, new_recipe in new_recipeID.items():
            if recipe_id == new_recipe:
                name = recipe[recipe['recipe_id'] == old_recipe]['name'].values[0]
                ingredients = recipe[recipe['recipe_id'] == old_recipe]['ingredients'].values[0]

                print(f'Top {i} Original Recipe ID: {old_recipe} - {name}\n Ingredients: {ingredients}\n')

                i += 1

    return dict_top_n[0]

# In[31]:

R1_UserBased = getRecommendations_UserBased(702)
R1_UserBased

# In[32]:

R1_ItemBased = getRecommendations_ItemBased(702)
R1_ItemBased

# In[33]:

R2_UserBased = getRecommendations_UserBased(408, 5)
R2_UserBased

# In[34]:

R2_ItemBased = getRecommendations_ItemBased(408, 5)
R2_ItemBased

# In[35]:

R3_UserBased = getRecommendations_UserBased(204, 7)
R3_UserBased

# In[36]:

R3_ItemBased = getRecommendations_ItemBased(204, 7)
R3_ItemBased


text:
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.

from envs import locomotion_gym_env
from envs import locomotion_gym_config
from envs.env_wrappers import imitation_wrapper_env
from envs.env_wrappers import observation_dictionary_to_array_wrapper
from envs.env_wrappers import trajectory_generator_wrapper_env
from envs.env_wrappers import simple_openloop
from envs.env_wrappers import simple_TG_group
from envs.env_wrappers import imitation_task
from envs.sensors import environment_sensors
from envs.sensors import sensor_wrappers
from envs.sensors import robot_sensors
from envs.utilities import controllable_env_randomizer_from_config
from robots import laikago
import numpy as np

def build_imitation_env(motion_files, num_parallel_envs, mode,
                        enable_randomizer, enable_rendering,
                        action_lim = 0.2, hist = 3,
                        curr_steps = 30000000, path = 0):
    assert len(motion_files) > 0

    curriculum_episode_length_start = 20
    curriculum_episode_length_end = 600

    sim_params = locomotion_gym_config.SimulationParameters()
    sim_params.enable_rendering = enable_rendering

    gym_config = locomotion_gym_config.LocomotionGymConfig(simulation_parameters=sim_params)

    robot_class = laikago.Laikago

    # sensors = [
    #     sensor_wrappers.HistoricSensorWrapper(
    #         wrapped_sensor=robot_sensors.MotorAngleSensor(num_motors=laikago.NUM_MOTORS), num_history=3),
    #     sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.IMUSensor(), num_history=3),
    #     sensor_wrappers.HistoricSensorWrapper(
    #         wrapped_sensor=environment_sensors.LastActionSensor(num_actions=laikago.NUM_MOTORS), num_history=3)
    # ]

    sensors = [
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.BasePositionSensor(), num_history=hist),
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.IMUSensor(['Y', 'R', 'dR', 'P', 'dP']), num_history=hist),
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.MotorAngleSensor(num_motors=laikago.NUM_MOTORS), num_history=hist),
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=environment_sensors.LastActionSensor(num_actions=laikago.NUM_MOTORS), num_history=hist)
    ]

    # Look at this, this is the TG now
    trajectory_generator = simple_TG_group.SimpleTGGroup(
        action_limit=action_lim,
        init_lg_param=None, is_touting=2, init_f_tg=2)

    init_lg_param = trajectory_generator.init_lg_param
    # print(" initial tg parameters is this")
    # print(init_lg_param)
    init_lg_param = np.concatenate([np.zeros([12]), init_lg_param[1:]])

    tg_init_position = trajectory_generator.get_action(current_time=0, input_action=init_lg_param)

    task = imitation_task.ImitationTask(ref_motion_filenames=motion_files,
                                        enable_cycle_sync=True,
                                        tar_frame_steps=[1, 2, 10, 30],
                                        ref_state_init_prob=0.9,
                                        warmup_time=0.25,tg_init_position=tg_init_position,
                                        path = path)

    randomizers = []
    if enable_randomizer:
        randomizer = controllable_env_randomizer_from_config.ControllableEnvRandomizerFromConfig(verbose=False)
        randomizers.append(randomizer)

    env = locomotion_gym_env.LocomotionGymEnv(gym_config=gym_config, robot_class=robot_class,
                                              env_randomizers=randomizers, robot_sensors=sensors, task=task)

    env = observation_dictionary_to_array_wrapper.ObservationDictionaryToArrayWrapper(env)

    env = trajectory_generator_wrapper_env.TrajectoryGeneratorWrapperEnv(env,
                                                                         trajectory_generator=trajectory_generator)

    if mode == "test":
        curriculum_episode_length_start = curriculum_episode_length_end

    env = imitation_wrapper_env.ImitationWrapperEnv(env,
                                                    episode_length_start=curriculum_episode_length_start,
                                                    episode_length_end=curriculum_episode_length_end,
                                                    curriculum_steps=curr_steps,
                                                    num_parallel_envs=num_parallel_envs)
    return env

def build_other_env(motion_files, num_parallel_envs, mode,
                        enable_randomizer, enable_rendering, action_lim = 0.2, curr_steps = 30000000):
    assert len(motion_files) > 0

    curriculum_episode_length_start = 20
    curriculum_episode_length_end = 600

    sim_params = locomotion_gym_config.SimulationParameters()
    sim_params.enable_rendering = enable_rendering

    gym_config = locomotion_gym_config.LocomotionGymConfig(simulation_parameters=sim_params)

    robot_class = laikago.Laikago

    # sensors = [
    #     sensor_wrappers.HistoricSensorWrapper(
    #         wrapped_sensor=robot_sensors.MotorAngleSensor(num_motors=laikago.NUM_MOTORS), num_history=3),
    #     sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.IMUSensor(), num_history=3),
    #     sensor_wrappers.HistoricSensorWrapper(
    #         wrapped_sensor=environment_sensors.LastActionSensor(num_actions=laikago.NUM_MOTORS), num_history=3)
    # ]

    sensors = [
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.BasePositionSensor(), num_history=3),
        sensor_wrappers.HistoricSensorWrapper(wrapped_sensor=robot_sensors.IMUSensor(), num_history=3),
        sensor_wrappers.HistoricSensorWrapper(
            wrapped_sensor=robot_sensors.MotorAngleSensor(num_motors=laikago.NUM_MOTORS), num_history=3),
        sensor_wrappers.HistoricSensorWrapper(
            wrapped_sensor=environment_sensors.LastActionSensor(num_actions=laikago.NUM_MOTORS), num_history=3)
    ]

    # Look at this, this is the TG now
    trajectory_generator = simple_TG_group.SimpleTGGroup(
        action_limit=action_lim,
        init_lg_param=None, is_touting=2, init_f_tg=2)

    init_lg_param = trajectory_generator.init_lg_param
    # print(" initial tg parameters is this")
    # print(init_lg_param)
    init_lg_param = np.concatenate([np.zeros([12]), init_lg_param[1:]])

    tg_init_position = trajectory_generator.get_action(current_time=0, input_action=init_lg_param)

    task = imitation_task.ImitationTask(ref_motion_filenames=motion_files,
                                        enable_cycle_sync=True,
                                        tar_frame_steps=[1, 2, 10, 30],
                                        ref_state_init_prob=0.9,
                                        warmup_time=0.25,tg_init_position=tg_init_position)

    randomizers = []
    if enable_randomizer:
        randomizer = controllable_env_randomizer_from_config.ControllableEnvRandomizerFromConfig(verbose=False)
        randomizers.append(randomizer)

    env = locomotion_gym_env.LocomotionGymEnv(gym_config=gym_config, robot_class=robot_class,
                                              env_randomizers=randomizers, robot_sensors=sensors, task=task)

    env = observation_dictionary_to_array_wrapper.ObservationDictionaryToArrayWrapper(env)

    env = trajectory_generator_wrapper_env.TrajectoryGeneratorWrapperEnv(env,
                                                                         trajectory_generator=trajectory_generator)

    if mode == "test":
        curriculum_episode_length_start = curriculum_episode_length_end

    env = imitation_wrapper_env.ImitationWrapperEnv(env,
                                                    episode_length_start=curriculum_episode_length_start,
                                                    episode_length_end=curriculum_episode_length_end,
                                                    curriculum_steps=curr_steps,
                                                    num_parallel_envs=num_parallel_envs)
    return env


text:
import numpy as np
import os
import sys
import datetime
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

ctd = \
  np.genfromtxt('../comsat/CTD/COMSAT 2011 CTD.txt',
                skip_header = 1,
                dtype = 'i4,i4,i4,i4,S12,S14,'+','.join(['f8'] * 15),
                converters = {3: lambda(x):
                              x.strip('"') if x.strip('"').isdigit() else '0'},
                names = ['rownumber', 'I', "year", "Lake", "Date", "Time",
                         "Cond", "Temp", "Pres", "R.Temp", "PAR",
                         "R.D.O2", "TFlC.a", "TFlE.a", "TFlP.a", "Depth",
                         "Salin", "SpecCond", "DensAnom", "SoS", "rdO2C"])
# ctd['Lake'] == 0 where lake is Bogstad and year 2010, see converters

ctd = ctd[ctd['Lake'] > 0, :]
lake_ids = set(ctd['Lake'])

startdate = datetime.date(2009, 1, 1)
enddate = datetime.date(2012, 12, 31)
ndays = (enddate - startdate).days + 1
mylakedates = [startdate + datetime.timedelta(td) for td in range(ndays)]

pdf =  PdfPages('temperature profile -- comsat field vs MyLake lots assumptions uncalibrated.pdf')
for l in lake_ids:
    datestr = set(ctd[ctd['Lake'] == l, :]['Date'])
    assert len(datestr) == 1, 'there is something wrong here'
    year, month, day = list(datestr)[0].strip('"').split('/')
    thedate = datetime.date(int('20' + year), int(month), int(day))
    boolindex = np.array([mld == thedate for mld in mylakedates])
    mylakefname = \
      os.path.join('..', 'mylake', 'COMSAT' + str(l), 'i3co',
                   'NORA10_11km_interpolated_2009-2012_COMSAT' +
                   str(l) + '_i3co_1.5fielddepth_cone_18secchi_Minnesota',
                   'Tzt.txt')
    if not os.path.exists(mylakefname):
        continue
    ml = np.genfromtxt(mylakefname, dtype = 'f4')[boolindex, :].flatten()
    mldepth = 0.5 + np.arange(len(ml))
    c = ctd[ctd['Lake'] == l, :][['Temp', 'Depth']]
    c = np.sort(c, order = 'Depth')
    fig, ax = plt.subplots(1, 1)
    plt.plot(ml, mldepth, color = 'blue',
             label = 'MyLake lots assumptions, uncalibrated')
    plt.plot(c['Temp'], c['Depth'], color = 'darkgray',
             label = 'COMSAT field CTD measurements')
    plt.ylabel('COMSAT lake %s: %s' % (l, thedate.isoformat()))
    plt.xlabel('Temperature (deg C)')
    ax.axes.set_xlim(4, 25)
    ax.axes.set_ylim(20, 0)
    ax.xaxis.tick_top()
    plt.legend(loc = 'lower right')
    pdf.savefig()
    plt.close()
pdf.close()

max_stars_repo_name: kojitominaga/scratch

max_issues_repo_name: kojitominaga/scratch

max_forks_repo_name: kojitominaga/scratch


text:
"""
HNSW Utility Class
Needs:
    1. Index Creation
    2. Index Management(Adding, deleting)
    3. Handling labels(maintain mappings, )
"""
import os
import shutil
import hnswlib
import pandas as pd
import numpy as np
import pickle
from utils import *

"""
making new index -- needs new index size,
"""
class Index():
    """
    Index Classes
    """
    def __init__(self, hnsw_params:dict):
        self.SAVE_DIR = hnsw_params.get("save_dir", INDEX_DIR)
        self.SAVE_FILE = hnsw_params.get("save_file", "hnsw_index.bin")
        self.CURR_IDX_SIZE = self.find_current_index_size()
        self.M = hnsw_params.get("M", 200)
        self.ef_construction = hnsw_params.get("ef_construction", 200)
        self.item_batch_size = hnsw_params.get("item_batch_size", 10)
        self.num_threads = hnsw_params.get("num_threads", MAX_SEARCH_THREADS)
        self.num_elements = hnsw_params.get("num_elements", None)
        self.label_mapping = hnsw_params.get("label_mapping", None)
        self.index = hnswlib.Index(space='cosine', dim=EMB_DIM)
        self.index_loaded = False

    def init_search(self):
        """
        load any presaved index if not, return empty index def
        """
        print("Loading saved index")
        self.index.load_index(self.SAVE_DIR+self.SAVE_FILE,
                              max_elements=self.CURR_IDX_SIZE)
        self.index_loaded = True

    def find_current_index_size(self):
        """
            Find saved index size
        """
        try:
            return pickle.load(open(INDEX_SIZE_PATH, 'rb'))
        except Exception as e:
            print("No Index size pre saved")
            return None

    def define_index(self, idx_size):
        """
        define fresh new index with params given when init
        save in given path
        """
        print("First time?")
        # Initing index - the maximum number of elements should be known beforehand
        self.CURR_IDX_SIZE = idx_size
        self.index.init_index(max_elements=self.CURR_IDX_SIZE,
                              ef_construction=self.ef_construction,
                              M=self.M)
        print(f"Saving Fresh New index: {self.SAVE_DIR+self.SAVE_FILE}")
        self.index.save_index(self.SAVE_DIR+self.SAVE_FILE)
        print(f"Save index size: {self.CURR_IDX_SIZE}")
        pickle.dump(self.CURR_IDX_SIZE, open(INDEX_SIZE_PATH, 'wb'))

    def _batch(self, iterable1, iterable2, n=1):
        l1 = iterable1.shape[0]
        l2 = iterable2.shape[0]
        assert l1 == l2
        # print("LEN OF ITER", l)
        for ndx in range(0, l1, n):
            # print("ITER", iterable[ndx:min(ndx + n, l)])
            yield iterable1[ndx:min(ndx + n, l1)], iterable2[ndx:min(ndx + n, l1)]

    def get_current_count(self):
        if not self.index_loaded:
            print("Index Not Loaded, please run init_search() first")
            return False
        return self.index.get_current_count()

    def update_index(self, data, data_labels):
        """
        batch add things to index and save to save path
        data should be:
            [(vector, label)]
        """

        if not self.index_loaded:
            print("Index Not Loaded, please run init_search() first")
            return False

        print(f"""
            Loading Previously saved index:
                Loading File:     {self.SAVE_FILE}
                New max_elements: {self.CURR_IDX_SIZE + len(data)}
        """)
        try:
            num_elements = len(data)
            self.index.load_index(self.SAVE_DIR+self.SAVE_FILE,
                                  max_elements=self.CURR_IDX_SIZE+num_elements)
            self.CURR_IDX_SIZE += num_elements
        except Exception as e:
            print(e)
            print("Probably not saved earlier")
            return None

        # List of qids to update as 'processed=True'
        update_list = []
        data = np.array(data)
        data_labels = np.array(data_labels)
        # Element insertion (can be called several times):
        print(f"Adding n={self.item_batch_size} batches of items from the data")
        for d, dl in self._batch(data, data_labels, n=self.item_batch_size):
            self.index.add_items(data=d,
                                 ids=dl,
                                 num_threads=self.num_threads)
            update_list.extend(dl)

        print(f"Saving new index of size {self.index.get_current_count()}")

        # Save index so far
        self.index.save_index(self.SAVE_DIR+self.SAVE_FILE)

        self.CURR_IDX_SIZE = self.index.get_current_count()
        print(f"Save index size: {self.CURR_IDX_SIZE}")
        pickle.dump(self.CURR_IDX_SIZE, open(INDEX_SIZE_PATH, 'wb'))

    def search(self, query_vector, max_nearest_nbrs):
        if not self.index_loaded:
            print("Index Not Loaded, please run init_search() first")
            return False

        labels, distances = self.index.knn_query(list(query_vector),
                                                 k=max_nearest_nbrs,
                                                 num_threads=self.num_threads)
        if labels is None:
            print(f"No Matches found")
            return {"matches": []}

        if self.label_mapping is not None:
            resp = []
            for q_ids, dists in zip(labels, distances):
                raw_set = []
                for qid, d in zip(q_ids, dists):
                    raw_set.append((int(qid), d, self.label_mapping[qid]))
                raw_set = sorted(raw_set, key=lambda x: x[1], reverse=False)
                resp.append([{k: (v, "%.3f"%((1-d)*100))} for k, d, v in raw_set])

        return {"matches": resp[0]}

    def __str__(self):
        return f"""
        HNSW Index Params:
            SAVE_DIR: {self.SAVE_DIR},
            SAVE_FILE: {self.SAVE_FILE},
            CURR_IDX_SIZE: {self.CURR_IDX_SIZE},
            M: {self.M},
            ef_construction: {self.ef_construction},
            item_batch_size: {self.item_batch_size},
            num_threads: {self.num_threads},
            index_loaded: {self.index_loaded}
        """

max_stars_repo_name: shishir-joshi/qualia

max_issues_repo_name: shishir-joshi/qualia

max_forks_repo_name: shishir-joshi/qualia


text:
import gym
import numpy as np
from torchvision.utils import save_image

from .fixobj import FixedObjectGoalEnv

class IntervalGoalEnv(FixedObjectGoalEnv):
	def __init__(self, args):
		self.img_size = args.img_size
		FixedObjectGoalEnv.__init__(self, args)

	def generate_goal(self):
		if self.target_goal_center is not None:
			ndim = self.target_goal_center.ndim
			if ndim > 1:
				ind = np.random.randint(len(self.target_goal_center))
				goal_center = self.target_goal_center[ind]
			else:
				goal_center = self.target_goal_center
			if isinstance(self.target_range, np.ndarray):
				if self.target_range.size == 2:
					range_to_use = np.concatenate([self.target_range, np.zeros(shape=1)])
				elif self.target_range.size == 3:
					range_to_use = self.target_range, np.zeros(shape=1)
				offset = np.random.uniform(-range_to_use, range_to_use)
			else:
				offset = np.random.uniform(-self.target_range, self.target_range, size=3)
			goal = goal_center + offset
			goal[2] = goal_center[2]
		else:
			if self.has_object:
				goal = self.initial_gripper_xpos[:3] + self.target_offset
				if self.args.env=='FetchSlide-v1':
					goal[0] += self.target_range*0.5
					goal[1] += np.random.uniform(-self.target_range, self.target_range)*0.5
				else:
					goal[0] += np.random.uniform(-self.target_range, self.target_range)
					goal[1] += np.random.uniform(-self.target_range, self.target_range)
				goal[2] = self.height_offset + int(self.target_in_the_air)*0.45
			else:
				goal = self.initial_gripper_xpos[:3] + np.array([np.random.uniform(-self.target_range, self.target_range), self.target_range, self.target_range])
		return goal.copy()


text:
import numpy as np
from . import renderer, misc, visibility

def vsd(dist_est, dist_gt, dist_test, visib_gt, delta, tau, cost_type='tlinear'):
    """
    Visible Surface Discrepancy.
    :param delta: Tolerance used for estimation of the visibility masks.
    :param tau: Misalignment tolerance.
    :param cost_type: Pixel-wise matching cost:
        'tlinear' - Used in the original definition of VSD in:
            Hodan et al., On Evaluation of 6D Object Pose Estimation, ECCVW 2016
        'step' - Used for SIXD Challenge 2017. It is easier to interpret.
    :return: Error of pose_est w.r.t. pose_gt.
    """
    # Visibility mask of the model in the estimated pose
    visib_est = visibility.estimate_visib_mask_est(dist_test, dist_est, visib_gt, delta)

    # Intersection and union of the visibility masks
    visib_inter = np.logical_and(visib_gt, visib_est)
    visib_union = np.logical_or(visib_gt, visib_est)

    # Pixel-wise matching cost
    costs = np.abs(dist_gt[visib_inter] - dist_est[visib_inter])
    if cost_type == 'step':
        costs = costs >= tau
    elif cost_type == 'tlinear': # Truncated linear
        costs *= (1.0 / tau)
        costs[costs > 1.0] = 1.0
    else:
        print('Error: Unknown pixel matching cost.')
        exit(-1)

    # costs_vis = np.ones(dist_gt.shape)
    # costs_vis[visib_inter] = costs
    # import matplotlib.pyplot as plt
    # plt.matshow(costs_vis)
    # plt.colorbar()
    # plt.show()

    # Visible Surface Discrepancy
    visib_union_count = visib_union.sum()
    visib_comp_count = visib_union_count - visib_inter.sum()
    if visib_union_count > 0:
        e = (costs.sum() + visib_comp_count) / float(visib_union_count)
    else:
        e = 1.0
    return e

def _vsd(R_est, t_est, R_gt, t_gt, model, depth_test, K, delta, tau,
        cost_type='tlinear'):
    """
    Visible Surface Discrepancy.
    :param R_est, t_est: Estimated pose (3x3 rot. matrix and 3x1 trans. vector).
    :param R_gt, t_gt: GT pose (3x3 rot. matrix and 3x1 trans. vector).
    :param model: Object model given by a dictionary where item 'pts'
    is nx3 ndarray with 3D model points.
    :param depth_test: Depth image of the test scene.
    :param K: Camera matrix.
    :param delta: Tolerance used for estimation of the visibility masks.
    :param tau: Misalignment tolerance.
    :param cost_type: Pixel-wise matching cost:
        'tlinear' - Used in the original definition of VSD in:
            Hodan et al., On Evaluation of 6D Object Pose Estimation, ECCVW 2016
        'step' - Used for SIXD Challenge 2017. It is easier to interpret.
    :return: Error of pose_est w.r.t. pose_gt.
    """

    im_size = (depth_test.shape[1], depth_test.shape[0])

    # Render depth images of the model in the estimated and the ground truth pose
    depth_est = renderer.render(model, im_size, K, R_est, t_est, clip_near=100,
                                clip_far=10000, mode='depth')

    depth_gt = renderer.render(model, im_size, K, R_gt, t_gt, clip_near=100,
                               clip_far=10000, mode='depth')

    # Convert depth images to distance images
    dist_test = misc.depth_im_to_dist_im(depth_test, K)
    dist_gt = misc.depth_im_to_dist_im(depth_gt, K)
    dist_est = misc.depth_im_to_dist_im(depth_est, K)

    # Visibility mask of the model in the ground truth pose
    visib_gt = visibility.estimate_visib_mask_gt(dist_test, dist_gt, delta)

    # Visibility mask of the model in the estimated pose
    visib_est = visibility.estimate_visib_mask_est(dist_test, dist_est, visib_gt, delta)

    # Intersection and union of the visibility masks
    visib_inter = np.logical_and(visib_gt, visib_est)
    visib_union = np.logical_or(visib_gt, visib_est)

    # Pixel-wise matching cost
    costs = np.abs(dist_gt[visib_inter] - dist_est[visib_inter])
    if cost_type == 'step':
        costs = costs >= tau
    elif cost_type == 'tlinear': # Truncated linear
        costs *= (1.0 / tau)
        costs[costs > 1.0] = 1.0
    else:
        print('Error: Unknown pixel matching cost.')
        exit(-1)

    # costs_vis = np.ones(dist_gt.shape)
    # costs_vis[visib_inter] = costs
    # import matplotlib.pyplot as plt
    # plt.matshow(costs_vis)
    # plt.colorbar()
    # plt.show()

    # Visible Surface Discrepancy
    visib_union_count = visib_union.sum()
    visib_comp_count = visib_union_count - visib_inter.sum()
    if visib_union_count > 0:
        e = (costs.sum() + visib_comp_count) / float(visib_union_count)
    else:
        e = 1.0
    return e

max_stars_repo_name: bertid/clean-pvnet

max_issues_repo_name: danikhani/clean-pvnet

max_forks_repo_name: danikhani/clean-pvnet