text:
from collections import OrderedDict
from dataclasses import dataclass
from pathlib import Path
from typing import Mapping, Sequence

import numpy as np
import pandas as pd
import torch
import torch.utils.data as tud
import torchaudio

from squawk.ctqdm import ctqdm

@dataclass
class DatasetInfo(object):
    name: str
    sample_rate: int
    label_map: Mapping[str, int]

    def __post_init__(self):
        self.num_labels = len(self.label_map)

class LruCache(object):

    def __init__(self, capacity=np.inf, load_fn=None):
        self.capacity = capacity
        self.cache = OrderedDict()
        self.load_fn = load_fn

    def __getitem__(self, key):
        try:
            value = self.cache.pop(key)
            self.cache[key] = value
        except KeyError:
            if self.load_fn is not None:
                self[key] = value = self.load_fn(key)
            else:
                raise KeyError
        return value

    def __setitem__(self, key, value):
        try:
            self.cache.pop(key)
        except KeyError:
            if len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)
        self.cache[key] = value

@dataclass
class ClassificationExample(object):
    audio: torch.Tensor
    label: int

@dataclass
class ClassificationBatch(object):
    audio: torch.Tensor
    labels: torch.LongTensor
    lengths: torch.LongTensor = None

    def to(self, device):
        self.audio = self.audio.to(device)
        self.labels = self.labels.to(device)
        self.lengths = self.lengths.to(device)

    def pin_memory(self):
        self.audio = self.audio.pin_memory()
        self.labels = self.labels.pin_memory()
        if self.lengths is not None: self.lengths = self.lengths.pin_memory()
        return self

@dataclass
class ClassificationDataset(tud.Dataset):
    audio_data: Sequence[str]
    label_data: Sequence[int]
    info: DatasetInfo
    lru_cache: LruCache

    def __post_init__(self):
        self.lru_cache.load_fn = self.load

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

    def load(self, idx):
        return torchaudio.load(self.audio_data[idx])[0]

    def split(self, proportion):
        proportion = int(proportion * len(self.audio_data))
        audio_data1 = self.audio_data[:proportion]
        audio_data2 = self.audio_data[proportion:]
        label_data1 = self.label_data[:proportion]
        label_data2 = self.label_data[proportion:]
        return ClassificationDataset(audio_data1, label_data1, self.info, LruCache(self.lru_cache.capacity)),\
               ClassificationDataset(audio_data2, label_data2, self.info, LruCache(self.lru_cache.capacity))

    def __getitem__(self, idx):
        return ClassificationExample(self.lru_cache[idx], self.label_data[idx])

def load_gsc(folder: Path, lru_maxsize=np.inf):
    def load_split(name):
        dev_path = folder / 'validation_list.txt'
        test_path = folder / 'testing_list.txt'
        labels = []
        for x in folder.glob('*'):
            if x.is_dir():
                labels.append(x.name)
        labels = sorted(labels)
        l2idx = {x: idx for idx, x in enumerate(labels)}
        with open(dev_path) as f:
            dev_set = list(map(str.strip, f.readlines()))
        with open(test_path) as f:
            test_set = list(map(str.strip, f.readlines()))
        if name == 'dev':
            tgt_set = dev_set
        elif name == 'test':
            tgt_set = test_set
        else:
            all_set = set(f'{str(x.parent.name)}/{str(x.name)}' for x in folder.glob('*/*.wav'))
            dev_set = set(dev_set)
            test_set = set(test_set)
            tgt_set = (all_set - dev_set) - test_set
        return ClassificationDataset([f'{str(folder)}/{x}' for x in tgt_set],
                                     [l2idx[x.split('/')[0]] for x in tgt_set],
                                     DatasetInfo('GSC', 16000, l2idx),
                                     LruCache(lru_maxsize))
    return load_split('training'), load_split('dev'), load_split('test')

def load_freesounds(folder: Path, lru_maxsize=np.inf):
    def load_split(name):
        train_csv_path = folder / 'FSDKaggle2018.meta' / f'train_post_competition.csv'
        labels_csv_path = folder / 'FSDKaggle2018.meta' / f'{name}_post_competition{"_scoring_clips" if name == "test" else ""}.csv'
        df = pd.read_csv(labels_csv_path, quoting=3)
        train_df = pd.read_csv(train_csv_path, quoting=3)
        idx2l = sorted(list(set(train_df['label'].unique())))
        l2idx = {lbl: idx for idx, lbl in enumerate(idx2l)}
        label_map = {}
        for row in df[['fname', 'label']].itertuples():
            label_map[row.fname] = l2idx[row.label]

        data_folder = folder / f'FSDKaggle2018.audio_{name}'
        audio_data = []
        label_data = []
        for wav_file in ctqdm(list(data_folder.glob('*.wav')), desc=f'Preparing {name} dataset'):
            audio_data.append(str(wav_file))
            label_data.append(label_map[wav_file.name])
        _, sr = torchaudio.load(wav_file)
        return ClassificationDataset(audio_data, label_data, DatasetInfo('FreeSounds', sr, l2idx), LruCache(lru_maxsize))
    train_split, dev_split = load_split('train').split(0.9)
    test_split = load_split('test')
    return train_split, dev_split, test_split

max_stars_repo_name: daemon/squawk

max_issues_repo_name: daemon/squawk

max_forks_repo_name: daemon/squawk


text:
import numpy as np
import cv2
from mss import mss
import time
from imutils import resize

class DetectBall:   # Aim Lab SETTINGS - PARAM2 - 23 OR LOWER (EXPERIMENT MORE) MAXRADIOUS 45 - 50 (NOT SURE)

    def __init__(self,
                 monitor = {'top':int(1080/4),'left':int(1920/4),
                            'width':int(1920/2),'height':int(1080/2)},
                 minDist=100,param1=30,param2=45,minRadius=10,maxRadius=115):

        self.monitor = monitor

        self.minDist = minDist
        self.param1 = param1
        self.param2 = param2
        self.minRadius = minRadius
        self.maxRadius = maxRadius

        self.color = (123, 204, 15)
        self.line_thickness = 3

        self.end_point = (int(round(self.monitor['width'] / 2)),
                           int(round(self.monitor['height'] / 2)))

    def draw_on_screen(self, img: str, blurred:str) -> None:
        ''' The following function draws circles around objects'''
        circles = cv2.HoughCircles(blurred,cv2.HOUGH_GRADIENT,1,self.minDist,
                                   param1=self.param1,param2=self.param2,
                                   minRadius=self.minRadius,maxRadius=self.maxRadius)
        # Line thickness
        thickness = 3

        # Draw
        if circles is not None:
            circles = np.uint16(np.around(circles))

            for i in circles[0,:]:
                cv2.circle(img, (i[0], i[1]), i[2], self.color, 2)

                # Draw Lines
                cv2.line(img, (i[0], i[1]), self.end_point,
                         self.color, self.line_thickness)

    @staticmethod
    def convert_img(img: str) -> list: # The list is a bunch of lists of numbers
        '''
            converts an image into gray, and the it blurs
            in order to find objects
        '''
        # Convert img into grey
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # Blur Img Out
        blurred = cv2.medianBlur(gray, 25) #cv2.bilateralFilter(gray,10,50,50)

        return (gray, blurred)

    def get_image(self) -> None:
        ''' Records the screen by taking a bunch of screenshots '''
        sct = mss()
        while True:
            last_time = time.time()
            # Get img
            sct_img = sct.grab(self.monitor)
            # Read igm
            img = np.array(sct_img)
            # Convert img
            gray, blur_img = DetectBall.convert_img(img)
            # Draw Circle
            self.draw_on_screen(img, blur_img)
            # Display Text On Img
            cv2.putText(img, f'fps: {1 / (time.time() - last_time)}'[0:9],
		               (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.0, self.color, 2)

            # Show Final Card
            cv2.imshow('screen', resize(img, width=1000))

            if (cv2.waitKey(1) & 0xFF) == ord('q'):
                cv2.destroyAllWindows()
                break

def main():
    det = DetectBall()
    det.get_image()

if __name__ == '__main__':
    main()

ext: py

max_stars_repo_name: lnaq/BallDetection

max_issues_repo_name: lnaq/BallDetection

max_forks_repo_name: lnaq/BallDetection

reason: import numpy


text:
import pickle
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from typing import List
from scipy.stats import ttest_ind
import os
import pdb

def get_authors_above_chance(model_dict: dict, p_vals: bool = False) -> List:
    scientists_to_models = {k: (-float("inf"), None) for k in model_dict['1NN']}

    for model in model_dict:
        print("MODEL: ", model)
        for scientist in model_dict[model]:
            print("SCIENTIST: ", scientist)
            print(model_dict[model][scientist])
            if p_vals and model_dict[model][scientist][1] * len(scientists_to_models) > 0.05:
                continue
            if p_vals:
                val = model_dict[model][scientist][0]
            else:
                val = model_dict[model][scientist][0]
            print(val)
            if val > scientists_to_models[scientist][0]:
                scientists_to_models[scientist] = (val, model)
    print(scientists_to_models)
    authors_above_chance = [k for k, v in scientists_to_models.items() if v[0] > 0]
    authors_below_chance = [k for k, v in scientists_to_models.items() if v[0] <= 0]

    return authors_above_chance, authors_below_chance

def get_histogram_data(data_filename: str, authors_above_chance: List, authors_below_chance: List, col: int = 2) -> tuple:
    num_authors_above = []
    num_authors_below = []
    for author in authors_above_chance:
        in_filename = os.path.join(data_filename, author + ".csv")
        num_authors_above.append(get_avg_author_number(in_filename, col=col))
    for author in authors_below_chance:
        in_filename = os.path.join(data_filename, author + ".csv")
        num_authors_below.append(get_avg_author_number(in_filename, col=col))

    return num_authors_above, num_authors_below

def make_histogram(dist1: List, dist2: List) -> None:
    sns.distplot(dist1, color="skyblue", label="Predicted above chance", norm_hist=True)
    sns.distplot(dist2, color="red", label="Predicted below chance", norm_hist=True)
    plt.legend()
    print(ttest_ind(dist1, dist2))
    plt.savefig("above-below-sbert.png")
    plt.savefig("above-below-sbert.eps")
    plt.show()

def get_avg_author_number(in_filename: str, col: int = 2) -> None:
    df = pd.read_csv(in_filename, header=None)
    author_num = df[col].mean(axis=0)
    if author_num != author_num:
        pdb.set_trace()
    return author_num

if __name__ == "__main__":
    models = pickle.load(open("results/full-2/physics.p", "rb"))
    del models["Null"]
    above_chance, below_chance = get_authors_above_chance(models)
    #a_c, b_c = get_histogram_data("data/turing_winners/vecs-abstracts-w-labels/labels-and-authors", above_chance, below_chance, col=3)
    print("ABOVE CHANCE:", len(above_chance))
    print("BELOW CHANCE: ", len(below_chance))
    #make_histogram(a_c, b_c)
    #get_avg_author_number("data/turing_winners/vecs-abstracts-w-labels/labels-and-authors/Adi-Shamir.csv")

reason: from scipy


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import numpy as np
from functools import reduce

from detectron.core.config import cfg

from scipy.stats import norm
from scipy.optimize import basinhopping

class GMM:
    def __init__(self, mus, sigmas, phis):
        self.norms = []
        self.mus = mus
        self.phis = phis
        for mu, sigma in zip(mus, sigmas):
            self.norms.append(norm(loc=mu, scale=sigma))

    def predict(self, x):
        p = 0.
        for n, phi in zip(self.norms, self.phis):
            p += n.pdf(x) * phi
        return p

    def max(self):
        # use method L-BFGS-B because the problem is smooth and bounded
        minimizer_kwargs = dict(method="L-BFGS-B", bounds=[(self.mus.min(), self.mus.max())])
        f = lambda x: -self.predict(x)
        res = basinhopping(f, self.mus.mean(), minimizer_kwargs=minimizer_kwargs)
        return res.x

    def exhaustive(self):
        pts = np.linspace(self.mus.min(), self.mus.max(), 800)
        return pts[self.predict(pts).argmax()]

max_stars_repo_name: hehewa/CAT

max_issues_repo_name: wahahamyt/CAT

max_forks_repo_name: wahahamyt/CAT

reason: import numpy,from scipy


text:
"""
    This file to run trajectory, hence handling OpenFOAM files and executing them in machine

    called in : replay_buffer.py
"""

import _thread
import os
import queue
import subprocess

import numpy as np

class env:
    """
        This Class is to run trajectory, hence handling OpenFOAM files and executing them in machine
    """

    def __init__(self, n_worker, buffer_size, control_between):
        """

        Args:
            n_worker: no of trajectories at the same time (worker)
            buffer_size: total number of trajectories
            contol_between: random starting point range of action in trajectory
        """
        self.n_worker = n_worker
        self.buffer_size = buffer_size
        self.control_between = control_between

    def rand_n_to_contol(self, n):
        """
        To get the random number from the range -> .2f%

        Args:
            n: number of random sampled number (in this case n=1)

        Returns: random number

        """
        np.random.seed()
        n_rand = np.random.uniform(self.control_between[0], self.control_between[1], n)
        n_rand = np.round(n_rand, 2)
        return n_rand

    def process_waiter(self, proc, job_name, que):
        """
            This method is to wait for the executed process till it is completed
        """
        try:
            proc.wait()
        finally:
            que.put((job_name, proc.returncode))

    def run_trajectory(self, buffer_counter, proc, results, sample):
        """
        To run the trajectories

        Args:
            buffer_counter: which trajectory to run (n -> traj_0, traj_1, ... traj_n)
            proc: array to hold process waiting flag
            results: array to hold process finish flag
            sample: number of iteration of main ppo

        Returns: execution of OpenFOAM Allrun file in machine

        """
        # get the random start
        rand_control_traj = self.rand_n_to_contol(1)

        # changing of end time to keep trajectory length equal
        endtime = round(float(rand_control_traj[0] + 5), 2)

        # make dir for new trajectory
        traj_path = f"./env/sample_{sample}/trajectory_{buffer_counter}"
        print(f"\n starting trajectory : {buffer_counter} \n")
        os.makedirs(traj_path, exist_ok=True)

        # copy files form base_case
        # change starting time of control -> 0.org/U && system/controlDict
        # change of ending time -> system/controlDict
        os.popen(f'cp -r ./env/base_case/agentRotatingWallVelocity/* {traj_path}/ && '
                 f'sed -i "s/startTime.*/startTime       {rand_control_traj[0]};/g" {traj_path}/0.org/U &&'
                 f'sed -i "/^endTime/ s/endTime.*/endTime         {endtime};/g" {traj_path}/system/controlDict &&'
                 f'sed -i "s/timeStart.*/timeStart       {rand_control_traj[0]};/g" {traj_path}/system/controlDict')

        # executing Allrun to start trajectory
        proc[buffer_counter] = subprocess.Popen(
            ['singularity', 'run', '../of2006-py1.6-cpu.sif', './Allrun', f'{traj_path}/'])
        _thread.start_new_thread(self.process_waiter,
                                 (proc[buffer_counter], f"trajectory_{buffer_counter}", results))

    def sample_trajectories(self, sample):
        """

        Args:
            sample: main ppo iteration counter

        Returns: execution of n number of trajectory (n = buffer_size)

        """
        # set the counter to count the numbre of trajectory
        buffer_counter = 0

        # list for the status of trajectory running or finished
        proc = []

        # set the n_workers
        for t in range(int(self.buffer_size)):
            item = "proc_" + str(t)
            proc.append(item)

        # get status of trajectory
        results = queue.Queue()
        process_count = 0

        # execute the n = n_workers trajectory simultaneously
        for _ in np.arange(self.n_worker):
            self.run_trajectory(buffer_counter, proc, results, sample)
            process_count += 1
            # increase the counter of trajectory number
            buffer_counter += 1

        # check for any worker is done. if so give next trajectory to that worker
        while process_count > 0:
            job_name, rc = results.get()
            print("job : ", job_name, "finished with rc =", rc)
            if self.buffer_size > buffer_counter:
                self.run_trajectory(buffer_counter, proc, results, sample)
                process_count += 1
                buffer_counter += 1
            process_count -= 1

max_issues_repo_name: AndreWeiner/flowAcylenderDLR


text:
"""
1D velocity model tools.
"""
import numpy
from . import interp

def v30gtl(v30, vt, z, zt=350.0, a=0.5, b=2.0/3.0, c=2.0):
    """
    V30 derived GTL
    """
    z = z / zt
    f = z + b * (z - z * z)
    g = a - (a + 3.0 * c) * z + c * z * z + 2.0 * c * numpy.sqrt(z)
    v = f * vt + g * v30
    return v

def dreger(prop, depth):
    """
    SoCal model of Dreger and Helmberger (1991).
    prop: 'rho', 'vp', or 'vs'.
    depth: Array of depth values in meters.
    Returns array of properties (kg/m^3 or m/s)
    """
    m = {
        'z':   (5.5,  5.5, 16.0, 16.0, 35.0, 35.0),
        'rho': (2.4,  2.67, 2.67, 2.8,  2.8,  3.0),
        'vp':  (5.5,  6.3,  6.3,  6.7,  6.7,  7.8),
        'vs':  (3.18, 3.64, 3.64, 3.87, 3.87, 4.5),
    }
    depth = numpy.asarray(depth)
    z = 1000.0 * numpy.array(m['z'])
    f = 1000.0 * numpy.array(m[prop])
    f = numpy.interp(depth, z, f)
    return f

def boore_rock(depth):
    """
    Boore and Joyner (1997) generic rock site Vs model.
    Takes an array of depth values in meters.
    Returns an array of S-wave velocities in m/s.
    """
    depth = numpy.asarray(depth)
    vs = numpy.empty_like(depth)
    vs.fill(float('nan'))
    z0 = -0.00001
    zve = [
        (1.0,     245.0, 0.0),
        (30.0,   2206.0, 0.272),
        (190.0,  3542.0, 0.407),
        (4000.0, 2505.0, 0.199),
        (8000.0, 2927.0, 0.086),
    ]
    for z1, v, e in zve:
        i = (z0 < depth) & (depth <= z1)
        vs[i] = (v * 0.001 ** e) * depth[i] ** e
        z0 = z1
    return vs

def boore_hard_rock(depth):
    """
    Boore and Joyner (1997) generic very hard rock site Vs.
    Takes array of depth values in meters.
    Returns array of S-wave velocities in m/s.
    """
    depth = numpy.asarray(depth)
    v = [
        2768.0, 2808.0, 2847.0, 2885.0, 2922.0, 2958.0, 2993.0, 3026.0,
        3059.0, 3091.0, 3122.0, 3151.0, 3180.0, 3208.0, 3234.0, 3260.0,
    ]
    vs = interp.interp1([0.0, 750.0], v, depth)
    z0 = 750.0
    zve = [
        (2200.0, 3324.0, 0.0670),
        (8000.0, 3447.0, 0.0209),
    ]
    for z1, v, e in zve:
        i = (z0 < depth) & (depth <= z1)
        vs[i] = (v * 0.001 ** e) * depth[i] ** e
        z0 = z1
    return vs

max_stars_repo_name: gely/coseis

max_issues_repo_name: gely/coseis

max_forks_repo_name: gely/coseis


text:
# encoding: utf-8
"""
@author: Chong Li
@time: 2021/06/28 18:39
@desc:
"""

import numpy as np
import torch
import torchvision
from torch import nn
from networks.head.rcnn_head.rcnn_utils import MultiScaleRoIAlign, TwoMLPHead, FastRCNNPredictor
from utils.bbox_utils import BBoxClipToImageSize, BBoxSizeFilter, BBoxBatchIOU
from networks.rpn.rpn_utils import (Matcher, BalancedPositiveNegativeSampler,
                                    NormalizedProposalEncoder, NormalizedProposalDecoder)

class RcnnHead(nn.Module):
    def __init__(self,
                 roi_pool_size=7, roi_in_channels=256,
                 roi_out_channels=1024,
                 num_classes=20,
                 include_bg=False,
                 score_thresh=0.1,
                 nms_thresh=0.5,
                 proposal_stds=(0.1, 0.1, 0.2, 0.2),
                 proposal_means=(0., 0., 0., 0.),
                 fg_iou_thresh=0.5,
                 bg_iou_thresh=0.5,
                 num_sample=512,
                 pos_ratio=0.25 ):
        super(RcnnHead, self).__init__()
        # cls and box regress all include the bg
        if include_bg == False:
            num_classes += 1
        self.include_bg = include_bg
        self.score_thresh = score_thresh
        self.nms_thresh = nms_thresh
        bg_iou_thresh = min(bg_iou_thresh, fg_iou_thresh)
        self.proposal_matcher = Matcher(fg_iou_thresh, bg_iou_thresh)
        self.fg_bg_sampler = BalancedPositiveNegativeSampler(num_sample, pos_ratio)
        self.proposal_encode = NormalizedProposalEncoder(stds=proposal_stds, means=proposal_means)
        self.proposal_decode = NormalizedProposalDecoder(stds=proposal_stds, means=proposal_means)
        self.roi_pool = MultiScaleRoIAlign(output_size=roi_pool_size)
        self.roi_head = TwoMLPHead(roi_in_channels*roi_pool_size**2, roi_out_channels)
        self.roi_predict = FastRCNNPredictor(roi_out_channels, num_classes)

    def incoming_label(self, inputs):
        self.gt_label = inputs['label'].clone()
        self.gt_bboxes_num = inputs['bboxes_num']

    def forward(self, inputs, is_training=False):
        if is_training == True:
            return self._forward_train(inputs)
        else:
            return self._forward_test(inputs)

    def _forward_test(self, inputs):
        r"""
        rpn_props, rcnn_props: we use the rpn/rcnn intermediate proposals to
        analyze the more detailed performance of the detector
        """
        proposals = inputs['proposals']
        props_feature = self.roi_pool(inputs['features'], inputs['proposals'],
                                      inputs['anchor_sizes'], inputs['strides'])

        props_feature = self.roi_head(props_feature)
        res = self.roi_predict(props_feature)
        bboxes_score = res['props_score']
        bboxes_pred = res['props']
        num_classes = bboxes_score.shape[-1]
        image_h = inputs['features'][0].shape[-2] * inputs['strides'][0]
        image_w = inputs['features'][0].shape[-1] * inputs['strides'][0]

        bboxes_per_image = [len(b) for b in proposals]
        proposals = torch.cat(proposals, dim=0).unsqueeze(1)
        bboxes_score = torch.softmax(bboxes_score, dim=-1)
        bboxes_pred = bboxes_pred.reshape(len(proposals), -1, 4)
        bboxes_pred = self.proposal_decode(bboxes_pred, proposals)
        bboxes_score = bboxes_score.split(bboxes_per_image, 0)
        bboxes_pred = bboxes_pred.split(bboxes_per_image, 0)
        proposals = proposals.squeeze(1).split(bboxes_per_image, 0)

        keep_res = []
        rpn_props = []
        rcnn_props = []
        for bboxes, scores, props in zip(bboxes_pred, bboxes_score, proposals):
            # create labels for each prediction
            labels = torch.arange(num_classes, device=scores.device)
            labels = labels.view(1, -1).expand_as(scores)
            # remove predictions with the background label
            bboxes, scores, labels = bboxes[:, 1:], scores[:, 1:], labels[:, 1:]
            max_scores = torch.max(scores, dim=-1, keepdim=True)[0]
            mask = scores == max_scores
            bboxes, scores, labels = bboxes[mask], scores[mask], labels[mask]
            #bboxes, scores, labels = bboxes.reshape(-1, 4), scores.reshape(-1), labels.reshape(-1)
            bboxes = BBoxClipToImageSize(bboxes, image_h, image_w)
            bboxes, scores = BBoxSizeFilter(bboxes, scores, 0)
            rcnn_bboxes = bboxes.clone().detach().cpu().numpy()
            props = props.detach().cpu().numpy()
            rpn_props.append(np.pad(props, ((0,0), (0,2))))
            rcnn_props.append(np.pad(rcnn_bboxes, ((0,0), (0,2))))
            # remove low scoring boxes
            inds = torch.where(scores > self.score_thresh)[0]
            if len(inds) ==0:
                keep_res.append(np.array([]).reshape(-1,6))
                continue
            bboxes, scores, labels = bboxes[inds], scores[inds], labels[inds]
            # non-maximum suppression, independently done per class
            keep = torchvision.ops.batched_nms(bboxes, scores, labels, self.nms_thresh)
            bboxes, scores, labels = bboxes[keep], scores[keep], labels[keep]
            if self.include_bg == False:
                labels -= 1
            # (N, [xmin，ymin,xmax,ymax, class, score])
            res = torch.cat([bboxes, labels.unsqueeze(-1), scores.unsqueeze(-1)], dim=-1)
            keep_res.append(res.detach().cpu().numpy())
        return {
            'res': keep_res,
            'rpn': rpn_props,
            'rcnn': rcnn_props
        }

    def _forward_train(self, inputs):
        props_info = self._generate_train_props_info(inputs['proposals'])
        props_feature = self.roi_pool(inputs['features'], props_info['proposals'],
                                      inputs['anchor_sizes'], inputs['strides'])
        props_feature = self.roi_head(props_feature)
        res = self.roi_predict(props_feature)
        res['props_label'] = torch.cat(props_info['props_label'], dim=0)
        res['props_targets'] = torch.cat(props_info['props_targets'], dim=0)
        res['rpn'] = inputs['training']
        return res

    def _sample_props_idx(self, cls_label):
        sampled_pos_inds, sampled_neg_inds = self.fg_bg_sampler(cls_label)
        sampled_inds = []
        for pos_inds_img, neg_inds_img in zip(sampled_pos_inds, sampled_neg_inds):
            img_sampled_inds = torch.where(pos_inds_img | neg_inds_img)[0]
            sampled_inds.append(img_sampled_inds)
        return sampled_inds

    def _encode_proposals(self, reference_boxes, proposals):
        boxes_per_image = [len(b) for b in reference_boxes]
        reference_boxes = torch.cat(reference_boxes, dim=0)
        proposals = torch.cat(proposals, dim=0)
        targets = self.proposal_encode(reference_boxes, proposals)
        return targets.split(boxes_per_image, 0)

    def _assign_targets_to_proposals(self, proposals):
        cls_label_list = []
        matched_idxs_list = []
        for b_proposals, b_label, gt_num in zip(proposals, self.gt_label, self.gt_bboxes_num):
            if gt_num == 0:
                cls_label = torch.zeros(len(b_proposals), dtype=b_proposals.dtype,
                                            device=b_proposals.device)
                matched_idxs = torch.zeros(len(b_proposals), dtype=b_proposals.dtype,
                                            device=b_proposals.device) - 1
                cls_label_list.append(cls_label)
                matched_idxs_list.append(matched_idxs)
                continue
            gt_bboxes = b_label[:gt_num][...,:4]
            gt_cls_label = b_label[:gt_num][...,4]
            ious = BBoxBatchIOU(gt_bboxes.unsqueeze(1), b_proposals.unsqueeze(0))
            matched_idxs = self.proposal_matcher(ious)
            cls_label = gt_cls_label[matched_idxs.clamp(min=0)]
            # Label background (below the low threshold)
            bg_inds = matched_idxs==self.proposal_matcher.BELOW_LOW_THRESHOLD
            cls_label[bg_inds] = 0
            matched_idxs[bg_inds] = -1
            # Label ignore proposals (between low and high thresholds)
            ignore_inds = matched_idxs == self.proposal_matcher.BETWEEN_THRESHOLDS
            cls_label[ignore_inds] = -1
            matched_idxs[ignore_inds] = -1
            cls_label_list.append(cls_label)
            matched_idxs_list.append(matched_idxs)
        return cls_label_list, matched_idxs_list

    def _generate_train_props_info(self, proposals):
        # append ground-truth bboxes to propos
        proposals = [
            torch.cat((props, b_label[:gt_num][...,:4]))
            for props, b_label, gt_num in zip(proposals, self.gt_label, self.gt_bboxes_num)]
        matched_cls_label, matched_idxs = self._assign_targets_to_proposals(proposals)
        sampled_inds = self._sample_props_idx(matched_cls_label)
        matched_gt_bboxes = []
        for idx, (sam_inds, b_label, gt_num) in enumerate(zip(sampled_inds, self.gt_label, self.gt_bboxes_num)):
            proposals[idx] = proposals[idx][sam_inds]
            matched_cls_label[idx] = matched_cls_label[idx][sam_inds]
            matched_idxs[idx] = matched_idxs[idx][sam_inds]
            gt_bboxes = b_label[:gt_num][...,:4]
            if gt_num == 0:
                gt_bboxes = torch.zeros((1, 4), dtype=proposals[idx].dtype, device=proposals[idx].device)
            matched_bboxes = gt_bboxes[matched_idxs[idx].clamp(min=0)]
            # Background (negative examples)
            bg_inds = matched_idxs[idx] < 0
            matched_bboxes[bg_inds] = 0
            matched_gt_bboxes.append(matched_bboxes)
        proposals_targets = self._encode_proposals(matched_gt_bboxes, proposals)
        return {
            'proposals': proposals,
            'props_label': matched_cls_label,
            'props_targets': proposals_targets }


text:
# Import essential libraries
import requests
import cv2
import numpy as np
import imutils

# Replace the below URL with your own. Make sure to add "/shot.jpg" at last.
url = "http://192.168.0.100:8080/shot.jpg"

# While loop to continuously fetching data from the Url
while True:
    img_resp = requests.get(url)
    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
    img = cv2.imdecode(img_arr, -1)
    img = imutils.resize(img, width=1000, height=1800)
    cv2.imshow("Android_cam", img)

    # Press Esc key to exit
    if cv2.waitKey(1) == 27:
        break

cv2.destroyAllWindows()


text:
# Lint as: python2, python3
# -*- coding: utf-8 -*-
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Utilities for converting CuDNN RNN params to Lingvo RNN weights."""

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

import numpy as np

import tensorflow as tf

from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops

NUM_PARAMS_PER_LSTM_LAYER = cudnn_rnn_ops.CudnnLSTM._NUM_PARAMS_PER_LAYER  # pylint:disable=protected-access
UNI_RNN = cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION
BI_RNN = cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION

class CuDNNLSTMInitializer(object):
  """Helper class for cudnn rnn weights initialization."""

  def __init__(self, num_input_nodes, num_cell_nodes, direction=UNI_RNN):
    self._input_nodes = num_input_nodes
    self._cell_nodes = num_cell_nodes
    self._direction = direction

  def OpaqueParamsShape(self, dtype):
    return cudnn_rnn_ops.cudnn_rnn_opaque_params_size(
        rnn_mode='lstm',
        num_layers=1,
        num_units=self._cell_nodes,
        input_size=self._input_nodes,
        input_mode='linear_input',
        direction=self._direction,
        dtype=dtype)

  @property
  def weight_shapes(self):
    """Return the shapes of weight tensors of each gate."""
    input_nodes = self._input_nodes
    cell_nodes = self._cell_nodes
    w_i, w_f, w_c, w_o = [(input_nodes, cell_nodes)] * 4
    r_i, r_f, r_c, r_o = [(cell_nodes, cell_nodes)] * 4
    if self._direction == BI_RNN:
      return [w_i, w_f, w_c, w_o, r_i, r_f, r_c, r_o] * 2
    else:
      return [w_i, w_f, w_c, w_o, r_i, r_f, r_c, r_o]

  @property
  def weight_sizes(self):
    """Return the sizes of weight tensor of each gate."""
    shapes = self.weight_shapes
    return [np.prod(shape) for shape in shapes]

  @property
  def weight_size(self):
    """Return the accumulated weight size."""
    return np.sum(self.weight_sizes)

  @property
  def bias_shapes(self):
    """Return the shapes of bias tensors of each gate."""
    if self._direction == BI_RNN:
      return [(self._cell_nodes)] * 16
    else:
      return [(self._cell_nodes)] * 8

  @property
  def bias_sizes(self):
    """Return the sizes of bias tensor of each gate."""
    return self.bias_shapes

  @property
  def bias_size(self):
    """Return the accumulated bias size."""
    return np.sum(self.bias_sizes)

  def InitOpaqueParams(self, dtype, base_initializer):
    """Uses base_initializer to init weights from opaque cudnn params.

    Args:
      dtype: data type.
      base_initializer: a callable that returns a tensor given shape, dtype and
          partition_info.
    Returns:
      A initialized opaque cudnn params. Its weights are initialized with the
      base_initializer, and biases are set to zero.
    """
    # The shape argument isn't used.
    weights = [
        base_initializer(sp, dtype, partition_info=None)
        for sp in self.weight_shapes
    ]
    biases = [tf.zeros(sp, dtype=dtype) for sp in self.bias_shapes]
    return cudnn_rnn_ops.cudnn_rnn_canonical_to_opaque_params(
        rnn_mode='lstm',
        num_layers=1,
        num_units=self._cell_nodes,
        input_size=self._input_nodes,
        weights=weights,
        biases=biases,
        input_mode='linear_input',
        direction=self._direction)

def _StitchWeights(w_i, w_f, w_c, w_o,
                   r_i, r_f, r_c, r_o,
                   input_dim, cell_dim):
  r"""Stitching LSTM per-gate weights to comform to LSTMCellSimple layout.

  LSTMCellSimple uses a single weight Tensor of shape [input_dim, 4 * cell_dim].
  This method puts the weight tensors together.

  Args:
    w_i:
    w_f:
    w_c:
    w_o:
      weights applied on cell input.
    r_i:
    r_f:
    r_c:
    r_o:
      weights applied on recurrent input.
    input_dim: an int, LSTM input dim.
    cell_dim: an int, LSTM cell dim.

  Returns:
    A weight Tensor.
  """
  # pylint: disable=invalid-name
  W_i = tf.concat([
      tf.reshape(w_i, [cell_dim, input_dim]),
      tf.reshape(r_i, [cell_dim, cell_dim])], axis=1)
  W_f = tf.concat([
      tf.reshape(w_f, [cell_dim, input_dim]),
      tf.reshape(r_f, [cell_dim, cell_dim])], axis=1)
  W_c = tf.concat([
      tf.reshape(w_c, [cell_dim, input_dim]),
      tf.reshape(r_c, [cell_dim, cell_dim])], axis=1)
  W_o = tf.concat([
      tf.reshape(w_o, [cell_dim, input_dim]),
      tf.reshape(r_o, [cell_dim, cell_dim])], axis=1)
  # pylint: enable=invalid-name
  # CuDNN weights are in ifco order, Lingvo LSTMCellSimple is cifo order.
  return tf.transpose(tf.concat([W_c, W_i, W_f, W_o], axis=0))

def _StitchBiases(b_wi, b_wf, b_wc, b_wo,
                  b_ri, b_rf, b_rc, b_ro):
  r"""Stitching LSTM per-gate biases to comform to LSTMCellSimple layout.

  LSTMCellSimple uses a single bias Tensor of shape [4 * cell_dim]. This method
  puts the bias tensors together.

  Args:
    b_wi:
    b_wf:
    b_wc:
    b_wo:
      biases applied on cell input.
    b_ri:
    b_rf:
    b_rc:
    b_ro:
      biases applied on recurrent input.

  Returns:
    A bias Tensor.
  """
  return (
      tf.concat([b_wc, b_wi, b_wf, b_wo], axis=0) +
      tf.concat([b_rc, b_ri, b_rf, b_ro], axis=0))

def _CuDNNParamsToCanonical(cudnn_params, input_dim, cell_dim, direction):
  r"""Convert a single piece CuDNN params to canonical params of LSTM gates.

  Args:
    cudnn_params: A Tensor containing all weights and biases of a
      CuDNN LSTM. The shape of cudnn_params given input_dim, cell_dim and
      direction can be obtained by py_utils.CuDNNInitializer.effective_shape.
    input_dim: an int, LSTM cell input dimension.
    cell_dim: an int, LSTM cell hidden dimension.
    direction: cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION or
      cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION.
  Returns:
    A list of weight Tensor and a list of bias Tensor, in the order they appear
    in input `cudnn_params`, described above.
  Raises:
    ValueError: for invalid `direction`.
  """
  if direction not in (UNI_RNN, BI_RNN):
    raise ValueError('\'direction\' must be %s or %s, receive %s.' %
                     (UNI_RNN, BI_RNN, direction))
  cudnn_initializer = CuDNNLSTMInitializer(input_dim, cell_dim, direction)
  weights, biases = tf.split(cudnn_params,
                             [cudnn_initializer.weight_size,
                              cudnn_initializer.bias_size],
                             axis=0)
  weights = tf.split(weights, cudnn_initializer.weight_sizes, axis=0)
  biases = tf.split(biases, cudnn_initializer.bias_sizes, axis=0)
  return weights, biases

def RecoverLSTMCellSimpleWeightsFromCuDNN(cudnn_params, input_dim, cell_dim,
                                          direction):
  r"""Recover LSTMCellSimple-compatible weights from (uni/bi)CuDNNLSTM weights.

  Args:
    cudnn_params: A Tensor containing all weights and biases of a
      CuDNN LSTM. The shape of cudnn_params given input_dim, cell_dim and
      direction can be obtained by py_utils.CuDNNInitializer.effective_shape.
    input_dim: an int, LSTM cell input dimension.
    cell_dim: an int, LSTM cell hidden dimension.
    direction: cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION or
      cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION.
  Returns:
    A list of weight Tensor and a list of bias Tensor.
  Raises:
    ValueError: for invalid `direction`.
  """
  if direction not in (cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION,
                       cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION):
    raise ValueError('\'direction\' must be %s or %s, receive %s.' %
                     (cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION,
                      cudnn_rnn_ops.CUDNN_RNN_BIDIRECTION, direction))
  weights, biases = _CuDNNParamsToCanonical(cudnn_params, input_dim, cell_dim,
                                            direction)
  if direction == cudnn_rnn_ops.CUDNN_RNN_UNIDIRECTION:
    assert len(weights) == NUM_PARAMS_PER_LSTM_LAYER
    assert len(weights) == NUM_PARAMS_PER_LSTM_LAYER
    w_i, w_f, w_c, w_o, r_i, r_f, r_c, r_o = weights
    b_wi, b_wf, b_wc, b_wo, b_ri, b_rf, b_rc, b_ro = biases
    w = _StitchWeights(w_i, w_f, w_c, w_o,
                       r_i, r_f, r_c, r_o,
                       input_dim, cell_dim)
    b = _StitchBiases(b_wi, b_wf, b_wc, b_wo,
                      b_ri, b_rf, b_rc, b_ro)
    return (w, b)
  else:
    assert len(weights) == 2 * NUM_PARAMS_PER_LSTM_LAYER
    assert len(weights) == 2 * NUM_PARAMS_PER_LSTM_LAYER
    (fwd_w_i, fwd_w_f, fwd_w_c, fwd_w_o,
     fwd_r_i, fwd_r_f, fwd_r_c, fwd_r_o,
     bak_w_i, bak_w_f, bak_w_c, bak_w_o,
     bak_r_i, bak_r_f, bak_r_c, bak_r_o) = weights
    (fwd_b_wi, fwd_b_wf, fwd_b_wc, fwd_b_wo,
     fwd_b_ri, fwd_b_rf, fwd_b_rc, fwd_b_ro,
     bak_b_wi, bak_b_wf, bak_b_wc, bak_b_wo,
     bak_b_ri, bak_b_rf, bak_b_rc, bak_b_ro) = biases
    fwd_w = _StitchWeights(fwd_w_i, fwd_w_f, fwd_w_c, fwd_w_o,
                           fwd_r_i, fwd_r_f, fwd_r_c, fwd_r_o,
                           input_dim, cell_dim)
    bak_w = _StitchWeights(bak_w_i, bak_w_f, bak_w_c, bak_w_o,
                           bak_r_i, bak_r_f, bak_r_c, bak_r_o,
                           input_dim, cell_dim)
    fwd_b = _StitchBiases(fwd_b_wi, fwd_b_wf, fwd_b_wc, fwd_b_wo,
                          fwd_b_ri, fwd_b_rf, fwd_b_rc, fwd_b_ro)
    bak_b = _StitchBiases(bak_b_wi, bak_b_wf, bak_b_wc, bak_b_wo,
                          bak_b_ri, bak_b_rf, bak_b_rc, bak_b_ro)
    return (fwd_w, bak_w), (fwd_b, bak_b)

class CudNNParamsFormatConverterLSTM(
    cudnn_rnn_ops.CudnnParamsFormatConverterLSTM):
  r"""Lingvo CuDNN LSTM params converter.

  Used by Lingvo CuDNNLSTMSaveable to convert between Cudnn and Lingvo LSTM
  formats.
  """

  def _cudnn_to_tf_gate_params(self, *cu_gate_order):
    """Put CuDNN gate params to lingvo RNN cell order."""
    i_g, f_g, c_g, o_g = cu_gate_order
    return [c_g, i_g, f_g, o_g]

  def _tf_to_cudnn_gate_params(self, *tf_gate_order):
    """Put lingvo RNN cell gate params to CuDNN order."""
    c_g, i_g, f_g, o_g = tf_gate_order
    return [i_g, f_g, c_g, o_g]

class CuDNNLSTMSaveable(tf.contrib.cudnn_rnn.CudnnLSTMSaveable):
  r"""Lingvo CuDNN LSTM opaque params saveable.

  Save CuDNN opaque params in lingvo canonical format such that the
  checkpoints can be used by both CuDNN and platform-independent RNN cells.

  CuDNN LSTM equation:

      | i_t = σ(w_i * x_t + r_i * h_(t-1) + b_wi + b_ri)
      | f_t = σ(w_f * x_t + r_f * h_(t-1) + b_wf + b_rf)
      | o_t = σ(w_o * x_t + r_o * h_(t-1) + b_wo + b_ro)
      | c'_t = tanh(w_c * x_t + r_c * h_(t-1) + b_wc + b_rc)
      | c_t = f_t ◦ c_(t-1) + i_t ◦ c'_t
      | h_t = o_t ◦ tanh(c_t)

  When saving, the opaque param is first transformed into a list of tensors
  in CuDNN canonical format, then further processed to be in the format of
  LSTMCellSimple vars.

  When recovering from a CuDNN graph, the restored tensors go through the
  reverse of the aforementioned process.

  When recovering from graphs built with LSTMCellSimple, the tensors in the
  checkpoints are ready to use, with the right shapes and names.

  Specifically the tensors are saved in the following order:

  .. code-block:: none

      ------------------------------------------------------------
      | weights                    | biases                      |
      ------------------------------------------------------------
       \                             \
        -------------------------------
        | layer1     |layer2     |... |
        -------------------------------
        \             \
         ---------------
         |fwd   |bak   |
         ------------

  Conceptually, for each layer, cudnn lstm has the following params and layout:

  .. code-block:: none

      -------------------------------------------------
      | w_i | w_f | w_c | w_o | r_i | r_f | r_c | r_o |
      -------------------------------------------------
      ---------------------------------------------------------
      | b_wi | b_wf | b_wc | b_wo | b_ri | b_rf | b_rc | b_ro |
      ---------------------------------------------------------

  While Lingvo LSTM params and layout is the following:

  .. code-block:: none

      -----------------------------
      | w_c' | w_i' | w_f' | w_o' |
      | r_c' | r_i' | r_f' | r_o' |
      -----------------------------
      ---------------------------------------------------------
      | b_wc + b_rc | b_wi + b_ri | b_wf + b_rf | b_wo + b_ro |
      ---------------------------------------------------------

  The shapes of each element before transpose is reflected by
  `CuDNNLSTMInitializer.{weight_shapes, biase_shapes}`.
  """

  _format_converter_cls = CudNNParamsFormatConverterLSTM

  def __init__(self,
               opaque_params,
               cell_nodes,
               input_nodes,
               rnn_cell_name,
               scope=None,
               name='params_canonical'):
    """Constructor.

    Args:
      opaque_params: opaque CuDNN params, a single tensor w/ no static shape.
      cell_nodes: a int, num of nodes in a lstm cell.
      input_nodes: a int, the num of nodes in input.
      rnn_cell_name: the name of RNN cell in the CuDNNLSTM-ish layer. Configured
        via LSTMCellSimple.Params().name.
      scope: the variable scope of the layer variable. If not set, default to
        current variable scope.
      name: name of the saveable, should be unique in a graph.
    """
    self._rnn_cell_name = rnn_cell_name
    scope = scope or tf.get_variable_scope()
    super(CuDNNLSTMSaveable, self).__init__(
        opaque_params=opaque_params,
        num_layers=1,
        num_units=cell_nodes,
        input_size=input_nodes,
        direction=UNI_RNN,
        scope=scope,
        name=name)

  def _tf_canonical_names_single_layer(self, prefix, tf_wts_names, tf_bs_names):
    r"""Transform single layer Cudnn canonicals to tf canonicals.

    Args:
      prefix: the shared prefix of all tensor names.
      tf_wts_names: a list where names of transformed weights are stored.
      tf_bs_names: a list where names of transformed biases are stored.
    """
    tf_wts_names.append(prefix + '/wm/var')
    tf_bs_names.append(prefix + '/b/var')

  def _tf_canonical_name_prefix(self, layer, is_fwd=True):
    """The prefix of names under which lingvo canonical params are saved."""
    del is_fwd
    # Lingvo only uses single layer.
    assert layer == 0
    return self._rnn_cell_name

class BidiCuDNNLSTMSaveable(CuDNNLSTMSaveable):
  """Lingvo CuDNN LSTM opaque params saveable."""

  def __init__(self,
               opaque_params,
               cell_nodes,
               input_nodes,
               fw_cell_name,
               bw_cell_name,
               scope=None,
               name='params_canonical'):
    """Constructor.

    Args:
      opaque_params: opaque CuDNN params, a single tensor w/ no static shape.
      cell_nodes: a int, num of nodes in a lstm cell.
      input_nodes: a int, the num of nodes in input.
      fw_cell_name:
      bw_cell_name: the name of RNN cell in the BidiCuDNNLSTM-ish layer.
        Configured via LSTMCellSimple.Params().name.
      scope: the variable scope of the layer variable. If not set, default to
        current variable scope.
      name: name of the saveable, should be unique in a graph.
    """
    self._fw_cell_name = fw_cell_name
    self._bw_cell_name = bw_cell_name
    scope = scope or tf.get_variable_scope()
    super(CuDNNLSTMSaveable, self).__init__(
        opaque_params=opaque_params,
        num_layers=1,
        num_units=cell_nodes,
        input_size=input_nodes,
        direction=BI_RNN,
        scope=scope,
        name=name)

  def _tf_canonical_name_prefix(self, layer, is_fwd=True):
    """The prefix of names under which lingvo canonical params are saved."""
    # Lingvo only uses single layer.
    assert layer == 0
    return self._fw_cell_name if is_fwd else self._bw_cell_name


text:
# Copyright 2019 Google LLC
#
# 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
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import contextlib
import enum
import fcntl
import functools
import os
import pathlib
import queue
import signal
import sys
import termios
import threading
import time

import numpy as np

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GLib', '2.0')
gi.require_version('GObject', '2.0')
gi.require_version('Gst', '1.0')
gi.require_version('GstBase', '1.0')
gi.require_version('GstGL', '1.0')
gi.require_version('GstPbutils', '1.0')
gi.require_version('GstVideo', '1.0')
from gi.repository import GLib, GObject, Gst, GstBase, GstGL, GstVideo, Gtk

GObject.threads_init()
Gst.init([])
Gtk.init([])

from gi.repository import GstPbutils  # Must be called after Gst.init().

from PIL import Image

from .gst import *
COMMAND_SAVE_FRAME = ' '
COMMAND_PRINT_INFO = 'p'
COMMAND_QUIT       = 'q'
WINDOW_TITLE       = 'Coral'

class Display(enum.Enum):
    FULLSCREEN = 'fullscreen'
    WINDOW = 'window'
    NONE = 'none'

    def __str__(self):
        return self.value

# @contextlib.contextmanager
# def nonblocking(fd):
#     os.set_blocking(fd, False)
#     try:
#         yield
#     finally:
#         os.set_blocking(fd, True)

@contextlib.contextmanager
def term_raw_mode(fd):
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~(termios.ICANON | termios.ECHO)
    termios.tcsetattr(fd, termios.TCSANOW, new)
    try:
        yield
    finally:
        termios.tcsetattr(fd, termios.TCSAFLUSH, old)

# @contextlib.contextmanager
# def Commands():
#     commands = queue.Queue()

#     def on_keypress(fd, flags):
#         for ch in sys.stdin.read():
#             commands.put(ch)
#         return True

#     def get_nowait():
#         try:
#             return commands.get_nowait()
#         except queue.Empty:
#             return None

#     if sys.stdin.isatty():
#         fd = sys.stdin.fileno()
#         GLib.io_add_watch(fd, GLib.IO_IN, on_keypress)
#         with term_raw_mode(fd), nonblocking(fd):
#             yield get_nowait
#     else:
#         yield lambda: None

@contextlib.contextmanager
def Worker(process, maxsize=0):
    commands = queue.Queue(maxsize)

    def run():
        while True:
            args = commands.get()
            if args is None:
                break
            process(*args)
            commands.task_done()

    thread = threading.Thread(target=run)
    thread.start()
    try:
        yield commands
    finally:
        commands.put(None)
        thread.join()

def save_frame(rgb, size, overlay=None, ext='png'):
    tag = '%010d' % int(time.monotonic() * 1000)
    img = Image.frombytes('RGB', size, rgb, 'raw')
    name = 'img-%s.%s' % (tag, ext)
    img.save(name)
    print('Frame saved as "%s"' % name)
    if overlay:
        name = 'img-%s.svg' % tag
        with open(name, 'w') as f:
            f.write(overlay)
        print('Overlay saved as "%s"' % name)

Layout = collections.namedtuple('Layout', ('size', 'window', 'inference_size', 'render_size'))

def make_layout(inference_size, render_size):
    inference_size = Size(*inference_size)
    render_size = Size(*render_size)
    size = min_outer_size(inference_size, render_size)
    window = center_inside(render_size, size)
    return Layout(size=size, window=window,
                  inference_size=inference_size, render_size=render_size)

def caps_size(caps):
    structure = caps.get_structure(0)
    return Size(structure.get_value('width'),
                structure.get_value('height'))

def get_video_info(filename):
    uri = pathlib.Path(filename).absolute().as_uri()
    discoverer = GstPbutils.Discoverer()
    info = discoverer.discover_uri(uri)

    streams = info.get_video_streams()
    assert len(streams) == 1
    return streams[0]

def get_seek_element(pipeline):
    element = pipeline.get_by_name('glsink')
    if not element:
        element = pipeline
    query = Gst.Query.new_seeking(Gst.Format.TIME)
    if element.query(query):
        _,  seekable, _, _ = query.parse_seeking()
        return element
    return None

@contextlib.contextmanager
def pull_sample(sink):
    sample = sink.emit('pull-sample')
    buf = sample.get_buffer()
    result, mapinfo = buf.map(Gst.MapFlags.READ)
    if result:
        yield sample, mapinfo.data
    buf.unmap(mapinfo)

def new_sample_callback(process,streamName):
    def callback(sink, pipeline):
        with pull_sample(sink) as (sample, data):
            #print(data)
            process(data, streamName)
        return Gst.FlowReturn.OK
    return callback

def on_bus_message(bus, message, pipeline, loop):
    if message.type == Gst.MessageType.EOS:
        seek_element = get_seek_element(pipeline)
        if loop and seek_element:
            flags = Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT
            if not seek_element.seek_simple(Gst.Format.TIME, flags, 0):
                Gtk.main_quit()
        else:
            Gtk.main_quit()
    elif message.type == Gst.MessageType.WARNING:
        err, debug = message.parse_warning()
        sys.stderr.write('Warning: %s: %s\n' % (err, debug))
    elif message.type == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        sys.stderr.write('Error: %s: %s\n' % (err, debug))
        Gtk.main_quit()

def on_sink_eos(sink, pipeline):
    overlay = pipeline.get_by_name('overlay')
    if overlay:
        overlay.set_eos()

def on_new_sample(sink,pipeline):
    with pull_sample(sink) as (sample, data):
        nparr = np.frombuffer(data, dtype=np.uint8)
        image = nparr.reshape(480, 640, 3)
        im = Image.fromarray(image)
        im.save("test.jpeg")
    return Gst.FlowReturn.OK

def run_gen(render_overlay_gen, *, source, loop, display):
    inference_size = render_overlay_gen.send(None)  # Initialize.
    next(render_overlay_gen)
    return run(inference_size,
        lambda tensor, layout, command:
            render_overlay_gen.send((tensor, layout, command)),
        source=source,
        loop=loop,
        display=display)

def run(inference_size, render_overlay, *, source, loop, display):
    result = get_pipeline(source, inference_size, display)
    if result:
        layout, pipeline = result
        run_pipeline(pipeline, layout, loop, render_overlay, display)
        return True

    return False

def get_pipeline(source, inference_size, display):
    fmt = parse_format(source)
    if fmt:
        layout = make_layout(inference_size, fmt.size)
        return layout, camera_pipeline(fmt, layout, display)

    filename = os.path.expanduser(source)
    if os.path.isfile(filename):
        info = get_video_info(filename)
        render_size = Size(info.get_width(), info.get_height())
        layout = make_layout(inference_size, render_size)
        return layout, file_pipline(info.is_image(), filename, layout, display)

    return None

def camera_pipeline(fmt, layout, display):
    if display is Display.NONE:
        return camera_headless_pipeline(fmt, layout)
    else:
        return camera_display_pipeline(fmt, layout)

def file_pipline(is_image, filename, layout, display):
    if display is Display.NONE:
        if is_image:
            return image_headless_pipeline(filename, layout)
        else:
            return video_headless_pipeline(filename, layout)
    else:
        fullscreen = display is Display.FULLSCREEN
        if is_image:
            return image_display_pipeline(filename, layout)
        else:
            return video_display_pipeline(filename, layout)

def quit():
    Gtk.main_quit()

def run_pipeline(source,on_buffer,signals):
    # Create pipeline
    pipeline = source
    pipeline = Gst.parse_launch(pipeline)

    # Set up a pipeline bus watch to catch errors.
    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect('message', on_bus_message, pipeline, False)

    with Worker(save_frame) as images:

        for name, signals in signals.items():
            component = pipeline.get_by_name(name)
            if component:
                for signal_name, signal_handler in signals.items():
                    component.connect(signal_name, signal_handler, pipeline)

        GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)

        # Run pipeline.
        pipeline.set_state(Gst.State.PLAYING)
        try:
            Gtk.main()
        except KeyboardInterrupt:
            pass
        finally:
            pipeline.set_state(Gst.State.NULL)

        # Process all pending MainContext operations.
        while GLib.MainContext.default().iteration(False):
            pass

max_stars_repo_name: Danny-Dasilva/TPUCameraManager

max_issues_repo_name: Danny-Dasilva/TPUCameraManager

max_forks_repo_name: Danny-Dasilva/TPUCameraManager


text:
"""Test tensorpac functions."""
import numpy as np
import matplotlib

from tensorpac import Pac, EventRelatedPac, PreferredPhase
from tensorpac.utils import pac_trivec

class TestPac(object):
    """Tests for Pac, Erpac and Preferred phase."""

    def test_id_pac_definition(self):
        """Test Pac object definition."""
        nmeth, nsuro, nnorm = 6, 4, 5
        for k in range(nmeth):
            for i in range(nsuro):
                for j in range(nnorm):
                    p = Pac(idpac=(k + 1, i, j))
                    str(p)

    def test_filtering_definition(self):
        """Test filtering defintion."""
        dcomplex = ['hilbert', 'wavelet']
        cycle = (12, 24)
        width = 12
        for k in dcomplex:
            Pac(dcomplex=k, cycle=cycle, width=width)

    def test_spectral(self):
        """Test filtering using the provided filters."""
        data = np.random.rand(3, 1000)
        p = Pac()
        dcomplex = ['hilbert', 'wavelet']
        for k in dcomplex:
            p.dcomplex = k
            p.filter(1024, data, n_jobs=1)

    def test_filter(self):
        """Test filter method."""
        data = np.random.rand(2, 1000)
        p = Pac()
        p.filter(256, data, 'phase')
        p.filter(256, data, 'phase', edges=2)
        p.filter(256, data, 'amplitude')

    def test_fit(self):
        """Test all Pac methods."""
        pha = np.random.rand(2, 7, 1024)
        amp = np.random.rand(3, 7, 1024)
        nmeth, nsuro, nnorm = 5, 4, 5
        p = Pac()
        for k in range(nmeth):
            for i in range(nsuro):
                for j in range(nnorm):
                    p.idpac = (k + 1, i, j)
                    p.fit(pha, amp, n_jobs=1, n_perm=10)

    def test_filterfit(self):
        """Test filtering test computing PAC."""
        data = np.random.rand(2, 1024)
        p = Pac(idpac=(4, 0, 0))
        p.filterfit(1024, data, n_jobs=1)
        p.idpac = (1, 1, 1)
        p.filterfit(1024, data, data, n_jobs=1, n_perm=2)
        p.dcomplex = 'wavelet'
        p.filter(1024, data, n_jobs=1)

    def test_properties(self):
        """Test Pac properties."""
        p = Pac()
        # Idpac :
        p.idpac
        p.idpac = (2, 1, 1)
        # Dcomplex :
        p.dcomplex
        p.dcomplex = 'wavelet'
        # Cycle :
        p.cycle
        p.cycle = (12, 24)
        # Width :
        p.width
        p.width = 12

    def test_pac_comodulogram(self):
        """Test Pac object definition.

        This test works locally but failed on travis...
        """
        matplotlib.use('agg')
        f, tridx = pac_trivec()
        pac = np.random.rand(20, 10)
        pval = np.random.rand(20, 10)
        p = Pac(f_pha=np.arange(11), f_amp=np.arange(21))
        p.comodulogram(np.random.rand(10, 10, 20))
        p.comodulogram(pac, rmaxis=True, dpaxis=True, interp=(.1, .1))
        p.comodulogram(pac, plotas='contour', pvalues=pval)
        p.comodulogram(pac, plotas='pcolor', pvalues=pval, levels=[.5, .7],
                       under='gray', over='red', bad='orange')
        p = Pac(f_pha=np.arange(11), f_amp=f)
        pac = np.random.rand(len(f))
        p.triplot(pac, f, tridx)
        p.savefig('test_savefig.png')
        p.show()
        matplotlib.pyplot.close('all')

class TestErpac(object):
    """Test EventRelatedPac class."""

    def test_filter(self):
        """Test function filter."""
        data = np.random.rand(7, 1000)
        p = EventRelatedPac()
        p.filter(256, data, 'phase')
        p.filter(256, data, 'amplitude')

    def test_fit(self):
        """Test function fit."""
        data = np.random.rand(100, 1000)
        p = EventRelatedPac()
        pha = p.filter(256, data, 'phase')
        amp = p.filter(256, data, 'amplitude')
        p.fit(pha, amp, method='circular')
        p.fit(pha, amp, method='gc')
        p.fit(pha, amp, method='gc', n_perm=2)
        p.fit(pha, amp, method='gc', smooth=5)
        p.surrogates, p.pvalues

    def test_filterfit(self):
        """Test function filterfit."""
        p = EventRelatedPac()
        x_pha = np.random.rand(100, 1000)
        x_amp = np.random.rand(100, 1000)
        p.filterfit(256, x_pha, x_amp=x_amp, method='circular')
        p.filterfit(256, x_pha, x_amp=x_amp, method='gc')

class TestPreferredPhase(object):
    """Test EventRelatedPac class."""

    def test_filter(self):
        """Test function filter."""
        data = np.random.rand(7, 1000)
        p = PreferredPhase()
        p.filter(256, data, 'phase')
        p.filter(256, data, 'amplitude')

    def test_fit(self):
        """Test function fit."""
        data = np.random.rand(100, 1000)
        p = PreferredPhase()
        pha = p.filter(256, data, 'phase')
        amp = p.filter(256, data, 'amplitude')
        p.fit(pha, amp)

    def test_filterfit(self):
        """Test function filterfit."""
        p = PreferredPhase()
        x_pha = np.random.rand(100, 1000)
        x_amp = np.random.rand(100, 1000)
        p.filterfit(256, x_pha, x_amp=x_amp)

    def test_polar_plot(self):
        """Test the polar plot."""
        matplotlib.use('agg')
        p = PreferredPhase(f_pha=[5, 7], f_amp=(60, 200, 10, 1))
        x_pha = np.random.rand(100, 1000)
        x_amp = np.random.rand(100, 1000)
        ampbin, pp, vecbin = p.filterfit(256, x_pha, x_amp=x_amp)
        pp = np.squeeze(pp).T
        ampbin = np.squeeze(ampbin).mean(-1)
        p.polar(ampbin.T, vecbin, p.yvec, cmap='RdBu_r', interp=.1,
                cblabel='Amplitude bins')
        matplotlib.pyplot.close('all')

max_issues_repo_name: raphaelvallat/tensorpac

max_forks_repo_name: raphaelvallat/tensorpac


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

import os
import sys

root_path = "/".join(os.path.realpath(__file__).split("/")[:-4])
if root_path not in sys.path:
    sys.path.insert(0, root_path)

import numpy as np
from sklearn.metrics import matthews_corrcoef
from sklearn.metrics import f1_score, recall_score, precision_score

def cal_f1_score(pcs, rec):
    tmp = 2 * pcs * rec / (pcs + rec)
    return round(tmp, 4)

def simple_accuracy(preds, labels):
    return (preds == labels).mean()

def acc_and_f1(preds, labels):
    acc = simple_accuracy(preds, labels)
    f1 = f1_score(y_true=labels, y_pred=preds)
    return {
        "acc": acc,
        "f1": f1,
        "acc_and_f1": (acc + f1) / 2,
    }

def f1_measures(preds, labels):
    f1 = f1_score(y_true=labels, y_pred=preds)
    recall = recall_score(y_true=labels, y_pred=preds)
    precision = precision_score(y_true=labels, y_pred=preds)
    return {
        "f1": f1,
        "recall": recall,
        "precision": precision
    }

def acc(preds, labels):
    acc = simple_accuracy(preds, labels)
    return {
        "acc": acc,
    }

max_stars_repo_name: EuphoriaYan/ICCRE

max_issues_repo_name: EuphoriaYan/ICCRE

max_forks_repo_name: EuphoriaYan/ICCRE


text:
""" Train model on CIFAR """

import os
import argparse
import chainer
import numpy as np
import pandas as pd
import matplotlib

matplotlib.use("Agg")

# training utilities
import utils

parser = argparse.ArgumentParser(prog="Train ResNet on CIFAR")
# Model parameters
parser.add_argument("--n_layer", type=int, default=20, help="Number of ResNet layers")
parser.add_argument(
    "--n_class", type=int, default=10, help="Number of classes (select 10 or 100)"
)
# AdaLoss parameters
parser.add_argument("--method", type=str, default="approx_range", help="AdaLoss method")
parser.add_argument(
    "--loss_scale", type=float, default=1, help="Initial loss scale value"
)
parser.add_argument(
    "--update_per_n_iteration",
    type=int,
    default=1,
    help="The frequency of updating loss scale.",
)
# Training parameters
parser.add_argument("--out", type=str, default=None, help="Where to store results.")
parser.add_argument(
    "--manual_seed",
    type=int,
    default=0,
    help="Random seed for training and model initialization (default: 0)",
)
parser.add_argument("--train_batch", type=int, default=128)
# Device
parser.add_argument("--gpu", type=int, default=-1, help="GPU ID")
parser.add_argument("--lr", type=float, default=0.1)
args = parser.parse_args()

def main():
    """ Run training """
    kwargs = {}
    if args.n_layer >= 110:  # additional config
        kwargs["warmup_attr_ratio"] = 0.1
        kwargs["warmup_n_epoch"] = 5

    if args.update_per_n_iteration != 1:
        args.out += "-freq_{}".format(args.update_per_n_iteration)

    gdf, sdf, ldf = utils.train(
        args.n_layer,
        n_class=args.n_class,
        method=args.method,
        init_scale=args.loss_scale,
        device=args.gpu,
        update_per_n_iteration=args.update_per_n_iteration,
        manual_seed=args.manual_seed,
        train_batch=args.train_batch,
        learnrate=args.lr,
        **kwargs
    )

    # Plot results
    fig = utils.plot(
        gdf,
        sdf,
        ldf,
        title="Train ResNet-{n_layer} on CIFAR-{n_class} by {method} (scale={loss_scale}, seed={manual_seed})".format(
            n_layer=args.n_layer,
            n_class=args.n_class,
            method=args.method,
            loss_scale=args.loss_scale,
            manual_seed=args.manual_seed,
        ),
    )
    # Save results
    if args.out is not None:
        os.makedirs(args.out, exist_ok=True)

        gdf.to_csv(os.path.join(args.out, "grad_stats.csv"))
        sdf.to_csv(os.path.join(args.out, "loss_scale.csv"))
        ldf.to_csv(os.path.join(args.out, "train_logs.csv"))
        fig.savefig(os.path.join(args.out, "summary.pdf"))

if __name__ == "__main__":
    main()

max_stars_repo_name: kumasento/gradient-scaling

max_issues_repo_name: kumasento/gradient-scaling

max_forks_repo_name: kumasento/gradient-scaling


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

# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel. All rights reserved.

import numpy as np
import pandas as pd

from pandapower.shortcircuit.idx_brch import IKSS_F, IKSS_T, IP_F, IP_T, ITH_F, ITH_T
from pandapower.shortcircuit.idx_bus import IKSS1, IP, ITH, IKSS2
from pandapower.pypower.idx_bus import VM, VA, BUS_TYPE
from pandapower.results_bus import _get_bus_idx, _set_buses_out_of_service
from pandapower.results import _get_aranged_lookup, _get_branch_results
from pandapower.shortcircuit.idx_bus import C_MIN, C_MAX

def _get_bus_ppc_idx_for_br_all_results(net, ppc, bus):
    bus_lookup = net._pd2ppc_lookups["bus"]
    if bus is None:
        bus = net.bus.index

    ppc_index = np.arange(np.shape(bus)[0])
    ppc_index[ppc["bus"][bus_lookup[ppc_index], BUS_TYPE] == 4] = -1
    return bus, ppc_index

def _extract_results(net, ppc, ppc_0, bus):
    _get_bus_results(net, ppc, ppc_0, bus)
    if net._options["branch_results"]:
        if net._options['return_all_currents']:
            _get_line_all_results(net, ppc, bus)
            _get_trafo_all_results(net, ppc, bus)
            _get_trafo3w_all_results(net, ppc, bus)
        else:
            _get_line_results(net, ppc)
            _get_trafo_results(net, ppc)
            _get_trafo3w_results(net, ppc)

def _extract_single_results(net, ppc):
    for element in ["line", "trafo"]:
        net["res_%s_sc"%element] = pd.DataFrame(np.nan, index=net[element].index,
                                                columns=net["_empty_res_%s"%element].columns,
                                                dtype='float')
    _get_single_bus_results(net, ppc)
    net["_options"]["ac"] = True
    net["_options"]["trafo_loading"] = "current"
    bus_lookup_aranged = _get_aranged_lookup(net)
    bus_pq = np.zeros(shape=(len(net["bus"].index), 2), dtype=np.float)
    _get_branch_results(net, ppc, bus_lookup_aranged, bus_pq, suffix="_sc")

def _get_single_bus_results(net, ppc):
    _set_buses_out_of_service(ppc)
    bus_idx = _get_bus_idx(net)
    case = net._options["case"]
    c = ppc["bus"][bus_idx, C_MIN] if case == "min" else ppc["bus"][bus_idx, C_MAX]
    net["res_bus"]["vm_pu"] = np.nan
    net["res_bus_sc"]["vm_pu"] = c - ppc["bus"][bus_idx, VM]
    net["res_bus_sc"]["va_degree"] = ppc["bus"][bus_idx, VA]

def _get_bus_results(net, ppc, ppc_0, bus):
    if bus is None:
        bus = slice(None)

    bus_lookup = net._pd2ppc_lookups["bus"]
    ppc_index = bus_lookup[net.bus.index]

    if net["_options"]["fault"] == "1ph":
        net.res_bus_sc["ikss_ka"] = ppc_0["bus"][ppc_index, IKSS1] + ppc["bus"][ppc_index, IKSS2]
    else:
        net.res_bus_sc["ikss_ka"] = ppc["bus"][ppc_index, IKSS1] + ppc["bus"][ppc_index, IKSS2]
    if net._options["ip"]:
        net.res_bus_sc["ip_ka"] = ppc["bus"][ppc_index, IP]
    if net._options["ith"]:
        net.res_bus_sc["ith_ka"] = ppc["bus"][ppc_index, ITH]

    net.res_bus_sc = net.res_bus_sc.loc[bus, :]

def _get_line_results(net, ppc):
    branch_lookup = net._pd2ppc_lookups["branch"]
    case = net._options["case"]
    if "line" in branch_lookup:
        f, t = branch_lookup["line"]
        minmax = np.max if case == "max" else np.min
        net.res_line_sc["ikss_ka"] = minmax(ppc["branch"][f:t, [IKSS_F, IKSS_T]].real, axis=1)
        if net._options["ip"]:
            net.res_line_sc["ip_ka"] = minmax(ppc["branch"][f:t, [IP_F, IP_T]].real, axis=1)
        if net._options["ith"]:
            net.res_line_sc["ith_ka"] = minmax(ppc["branch"][f:t, [ITH_F, ITH_T]].real, axis=1)

def _get_line_all_results(net, ppc, bus):
    case = net._options["case"]

    bus, ppc_index = _get_bus_ppc_idx_for_br_all_results(net, ppc, bus)
    branch_lookup = net._pd2ppc_lookups["branch"]

    multindex = pd.MultiIndex.from_product([net.res_line_sc.index, bus], names=['line','bus'])
    net.res_line_sc = net.res_line_sc.reindex(multindex)

    if "line" in branch_lookup:
        f, t = branch_lookup["line"]
        minmax = np.maximum if case == "max" else np.minimum

        net.res_line_sc["ikss_ka"] = minmax(ppc["internal"]["branch_ikss_f"][f:t, ppc_index].real.reshape(-1, 1),
                                            ppc["internal"]["branch_ikss_t"][f:t, ppc_index].real.reshape(-1, 1))
        if net._options["ip"]:
            net.res_line_sc["ip_ka"] = minmax(ppc["internal"]["branch_ip_f"][f:t, ppc_index].real.reshape(-1, 1),
                                              ppc["internal"]["branch_ip_t"][f:t, ppc_index].real.reshape(-1, 1))
        if net._options["ith"]:
            net.res_line_sc["ith_ka"] = minmax(ppc["internal"]["branch_ith_f"][f:t, ppc_index].real.reshape(-1, 1),
                                               ppc["internal"]["branch_ith_t"][f:t, ppc_index].real.reshape(-1, 1))

def _get_trafo_results(net, ppc):
    branch_lookup = net._pd2ppc_lookups["branch"]
    if "trafo" in branch_lookup:
        f, t = branch_lookup["trafo"]
        net.res_trafo_sc["ikss_hv_ka"] = ppc["branch"][f:t, IKSS_F].real
        net.res_trafo_sc["ikss_lv_ka"] = ppc["branch"][f:t, IKSS_T].real

def _get_trafo_all_results(net, ppc, bus):
    bus, ppc_index = _get_bus_ppc_idx_for_br_all_results(net, ppc, bus)
    branch_lookup = net._pd2ppc_lookups["branch"]

    multindex = pd.MultiIndex.from_product([net.res_trafo_sc.index, bus], names=['trafo', 'bus'])
    net.res_trafo_sc = net.res_trafo_sc.reindex(multindex)

    if "trafo" in branch_lookup:
        f, t = branch_lookup["trafo"]
        net.res_trafo_sc["ikss_hv_ka"] = ppc["internal"]["branch_ikss_f"][f:t, ppc_index].real.reshape(-1, 1)
        net.res_trafo_sc["ikss_lv_ka"] = ppc["internal"]["branch_ikss_t"][f:t, ppc_index].real.reshape(-1, 1)

def _get_trafo3w_results(net, ppc):
    branch_lookup = net._pd2ppc_lookups["branch"]
    if "trafo3w" in branch_lookup:
        f, t = net._pd2ppc_lookups["branch"]["trafo3w"]
        hv = int(f + (t - f) / 3)
        mv = int(f + 2 * (t - f) / 3)
        lv = t
        net.res_trafo3w_sc["ikss_hv_ka"] = ppc["branch"][f:hv, IKSS_F].real
        net.res_trafo3w_sc["ikss_mv_ka"] = ppc["branch"][hv:mv, IKSS_T].real
        net.res_trafo3w_sc["ikss_lv_ka"] = ppc["branch"][mv:lv, IKSS_T].real

def _get_trafo3w_all_results(net, ppc, bus):
    bus, ppc_index = _get_bus_ppc_idx_for_br_all_results(net, ppc, bus)
    branch_lookup = net._pd2ppc_lookups["branch"]

    multindex = pd.MultiIndex.from_product([net.res_trafo3w_sc.index, bus], names=['trafo3w', 'bus'])
    net.res_trafo3w_sc = net.res_trafo3w_sc.reindex(multindex)

    if "trafo3w" in branch_lookup:
        f, t = branch_lookup["trafo3w"]
        hv = int(f + (t - f) / 3)
        mv = int(f + 2 * (t - f) / 3)
        lv = t
        net.res_trafo3w_sc["ikss_hv_ka"] = ppc["internal"]["branch_ikss_f"][f:hv, ppc_index].real.reshape(-1, 1)
        net.res_trafo3w_sc["ikss_mv_ka"] = ppc["internal"]["branch_ikss_t"][hv:mv, ppc_index].real.reshape(-1, 1)
        net.res_trafo3w_sc["ikss_lv_ka"] = ppc["internal"]["branch_ikss_t"][mv:lv, ppc_index].real.reshape(-1, 1)

max_stars_repo_name: julffers/pandapower

max_issues_repo_name: julffers/pandapower

max_forks_repo_name: julffers/pandapower


text:
#!/usr/bin/env python3

'''
Creates a movie of infinitely zooming FBM.

All coordinates are X,Y floats with values that increase rightward and downward:

    World Space: [0,0 through 1,1] spans the entire island.
     Tile Space: [0,0 through 1,1] spans the smallest tile that wholly encompasses the viewport.
 Viewport Space: [0,0 through 1,1] spans the current view.

At Z = 0, Tile Space is equivalent to World Space.

Note that numpy requires Row,Col integer coordinates, but we internalize those at the lowest level.
(see sample_pixel)

'''

from os import system
from tqdm import tqdm
from sdl2.ext import clipline

import cairo
import imageio
import numpy as np
import scipy.interpolate as interp

def vec2(x, y): return np.array([x, y], dtype=np.float)
def vec3(x, y, z): return np.array([x, y, z], dtype=np.float)
def grid(w, h): return np.zeros([int(h), int(w)], dtype=np.float)

# Configuration.
Resolution = vec2(512,512)
VideoFps = 30
NumFrames = VideoFps * 5
vsTargetLn = vec2([.4,.4], [.9,.9])
vsPanFocus = vec2(0.5, 0.5)
SeaLevel = 0.5
NicePalette = [
    000, 0x001070 , # Dark Blue
    126, 0x2C5A7C , # Light Blue
    127, 0xE0F0A0 , # Yellow
    128, 0x5D943C , # Dark Green
    160, 0x606011 , # Brown
    200, 0xFFFFFF , # White
    255, 0xFFFFFF ] # White

# Global data.
NoiseFrequency = 16.0
NumLayers = 4
Width, Height = Resolution
Lut = grid(3, 256)
Zoom = int(0)
vsTargetPt = vec2(-1,-1)
ViewImage = grid(Width, Height) ## Current viewport. Includes NumLayers of high-freq noise.
TileImage = grid(Width, Height) ## Smallest encompassing tile (accumulated low-freq noise).
Viewports = []

def update_view(nlayers = NumLayers):
    resample_image(ViewImage, TileImage, Viewports[-1])
    seed = Zoom
    for vp in Viewports[:nlayers]:
        noise = gradient_noise(Resolution, vp, NoiseFrequency, seed=seed)
        np.copyto(ViewImage, 2 * (ViewImage + noise))
        seed = seed + 1

def update_tile():
    global Zoom
    global Viewports
    global NoiseFrequency
    # Render a new base tile by adding one layer of noise.
    update_view(1)
    np.copyto(TileImage, ViewImage)
    # Left-shift the viewports array and push on a new high-frequency layer.
    Viewports = Viewports[1:] + [vec2((0,0),(1,1))]
    Zoom = Zoom + 1
    NoiseFrequency = min(NoiseFrequency * 1.5, 512.0)

def main():
    global vsTargetPt
    global NoiseFrequency
    global NumLayers

    NoiseFrequency = 16.0
    NumLayers = 4
    create_viewports()
    np.copyto(Lut, create_palette())
    np.copyto(TileImage, create_basetile(Width, Height))

    update_view()
    np.copyto(TileImage, ViewImage)
    NumLayers = 1
    create_viewports()

    update_view()
    vsTargetPt = marching_line(ViewImage, vsTargetLn)
    writer = imageio.get_writer('out.mp4', fps=VideoFps, quality=9)
    for frame in tqdm(range(NumFrames)):

        # Draw the heightmap for the current viewport.
        update_view()

        # Recompute the point of interest.
        vsTargetPt = marching_line(ViewImage, vsTargetLn)

        # Draw the overlay and convert the heightmap into color.
        rgb = render_view()
        writer.append_data(np.uint8(rgb))

        # Compute the pan / zoom adjustments for the largest viewport.
        vpdelta = shrink_viewport(Viewports[-1], zoom_speed=10, pan_speed=0.05)

        # Propagate the movement to all layer viewports.
        for vp in reversed(Viewports):
            np.copyto(vp, vp + vpdelta)
            vpdelta = vpdelta / 2

        # If the largest viewport is sufficiently small, it's time to increment zoom.
        vp = Viewports[-1]
        vpextent = vp[1] - vp[0]
        if vpextent[0] < 0.5 and vpextent[1] < 0.5:
            update_tile()

    writer.close()

def render_view():
    lo, hi = np.amin(ViewImage), np.amax(ViewImage)
    L1 = Lut[np.uint8(255 * (0.5 + 0.5 * ViewImage / (hi - lo)))]
    draw_overlay(L1, vsTargetLn, vsTargetPt)
    lo, hi = np.amin(TileImage), np.amax(TileImage)
    L2 = Lut[np.uint8(255 * (0.5 + 0.5 * TileImage / (hi - lo)))]
    # Crop so that the stack is roughly 1920x1080
    crop = Width - Width * 960/1080
    w0, w1 = int(crop/2), int(Width - crop/2)
    L1 = L1[:, w0:w1, :]
    L2 = L2[:, w0:w1, :]
    return np.hstack([L1, L2])

def shrink_viewport(viewport, zoom_speed, pan_speed):
    vpextent = viewport[1] - viewport[0]
    pandir = vsTargetPt - vsPanFocus
    pan_delta = pan_speed * pandir
    zoom_delta = zoom_speed * vpextent / Resolution
    return pan_delta + vec2(zoom_delta, -zoom_delta)

def draw_overlay(dst, lineseg, pxcoord):
    dims = [dst.shape[1], dst.shape[0]]
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, dims[0], dims[1])
    ctx = cairo.Context(surface)
    ctx.scale(dims[0], dims[1])
    ctx.set_line_width(0.005)
    # Stroke a path along lineseg
    ctx.set_source_rgba(1.0, 0.8, 0.8, 0.15)
    ctx.move_to(lineseg[0][0], lineseg[0][1])
    ctx.line_to(lineseg[1][0], lineseg[1][1])
    ctx.stroke()
    # Draw circle around pxcoord
    ctx.set_source_rgba(1.0, 0.8, 0.8, 0.15)
    ctx.save()
    ctx.translate(pxcoord[0], pxcoord[1])
    ctx.scale(0.02, 0.02)
    ctx.arc(0., 0., 1., 0., 2 * np.pi)
    ctx.restore()
    ctx.stroke()
    if False:
        f = cairo.ToyFontFace("")
        ctx.move_to(.5, .5)
        ctx.show_text("Philip")
    # Perform composition
    buf = surface.get_data()
    rgb = np.ndarray(shape=dims[:2], dtype=np.uint32, buffer=buf)
    color = np.float32([(rgb >> 8) & 0xff, (rgb >> 16) & 0xff, (rgb >> 0) & 0xff])
    color = color.swapaxes(0, 2).swapaxes(0, 1)
    a = np.float32((rgb >> 24) & 0xff) / 255.0
    alpha = np.array([a, a, a]).swapaxes(0, 2).swapaxes(0, 1)
    np.copyto(dst, dst * (1 - alpha) + color)

def clumpy(cmd):
    result = system('./clumpy ' + cmd)
    if result: raise Exception("clumpy failed with: " + cmd)

def gradient_noise(dims, viewport, frequency, seed):
    (left, top), (right, bottom) = 2 * (viewport - 0.5)
    args = "{}x{} '{},{},{},{}' {} {}".format(
        dims[0], dims[1],
        left, -bottom, right, -top,
        frequency, seed)
    clumpy("gradient_noise " + args)
    noise = np.load('gradient_noise.npy')
    return noise

def sample_pixel(image_array, x, y):
    rows, cols = image_array.shape
    row, col = int(y * rows), int(x * cols)
    if row < 0 or col < 0 or col >= cols or row >= rows:
        return 0
    return image_array[row][col]

def marching_line(image_array, line_segment):
    x0, y0 = line_segment[0]
    x1, y1 = line_segment[1]
    val = sample_pixel(image_array, x0, y0)
    sgn = np.sign(val)
    divs = float(max(Resolution))
    dx = (x1 - x0) / divs
    dy = (y1 - y0) / divs
    for i in range(int(divs)):
        x = x0 + i * dx
        y = y0 + i * dy
        val = sample_pixel(image_array, x, y)
        if np.sign(val) != sgn:
            return [x, y]
    print(f'Could not find in {x0:3.3},{y0:3.3} -- {x1:3.3},{y1:3.3}')
    return [0.5,0.5]

# TODO: use NicePalette
def create_palette():
    r = np.hstack([np.linspace(0.0,     0.0, num=128), np.linspace(0.0,     0.0, num=128)])
    g = np.hstack([np.linspace(0.0,     0.0, num=128), np.linspace(128.0, 255.0, num=128)])
    b = np.hstack([np.linspace(128.0, 255.0, num=128), np.linspace(0.0,    64.0, num=128)])
    return np.float32([r, g, b]).transpose()

# Hermite interpolation, also known as smoothstep:
#     (-1 => 0)     (0 => 1)     (+1 => 0)
def hermite(t):
    return 1 - (3 - 2*np.abs(t))*t*t

def create_basetile(width, height):
    rows = hermite([np.linspace(-1.0, 1.0, num=height)])
    cols = hermite([np.linspace(-1.0, 1.0, num=width)]).T
    return rows * cols - SeaLevel

def resample_image(dst, src, viewport):
    height, width = dst.shape
    domain = [np.linspace(0, 1, num) for num in (width, height)]
    [(left, top), (right, bottom)] = viewport
    vrange = np.linspace(left, right, num=width)
    urange = np.linspace(top, bottom, num=height)
    f = interp.interp1d(domain[0], src, kind='linear', fill_value='extrapolate')
    temp = f(vrange)
    f = interp.interp1d(domain[1], temp.T, kind='linear', fill_value='extrapolate')
    newimg = f(urange).T
    np.copyto(dst, newimg)

def create_viewports():
    global Viewports
    Viewports = []
    frequency = 1
    for x in range(NumLayers):
        vp = vec2((0,0), (frequency,frequency))
        Viewports.insert(0, vp)
        frequency = frequency / 2

main()

max_stars_repo_name: prideout/clumpy

max_issues_repo_name: prideout/clumpy

max_forks_repo_name: prideout/clumpy

reason: import numpy,import scipy


text:
#!/usr/bin/env python

# --------------------------------------------------------
# R-FCN
# Copyright (c) 2016 Yuwen Xiong
# Licensed under The MIT License [see LICENSE for details]
# Written by Yuwen Xiong
# --------------------------------------------------------

"""
Demo script showing detections in sample images.

See README.md for installation instructions before running.
"""

import _init_paths
from fast_rcnn.config import cfg
from fast_rcnn.test import im_detect
from fast_rcnn.nms_wrapper import nms
from utils.timer import Timer
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import caffe, os, sys, cv2
import argparse

CLASSES = ('__background__',
           'aeroplane', 'bicycle', 'bird', 'boat',
           'bottle', 'bus', 'car', 'cat', 'chair',
           'cow', 'diningtable', 'dog', 'horse',
           'motorbike', 'person', 'pottedplant',
           'sheep', 'sofa', 'train', 'tvmonitor')

NETS = {'ResNet-101': ('ResNet-101',
                  'resnet101_rfcn_final.caffemodel'),
        'ResNet-50': ('ResNet-50',
                  'resnet50_rfcn_final.caffemodel')}

def vis_detections(im, class_name, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return
    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]
        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1], fill=False,
                          edgecolor='red', linewidth=3.5)
            )
        ax.text(bbox[0], bbox[1] - 2,
                '{:s} {:.3f}'.format(class_name, score),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14, color='white')

    ax.set_title(('{} detections with '
                  'p({} | box) >= {:.1f}').format(class_name, class_name,
                                                  thresh),
                  fontsize=14)
    plt.axis('off')
    plt.tight_layout()
    plt.draw()

def demo_orig(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    im_detect(net, im)
    im_info = net.blobs['im_info'].data
    rois = net.blobs['rois'].data
    rois = rois/net.blobs['im_info'].data[0,2]
    roi_scores = net.blobs['rois_score'].data
    attention = net.blobs['attention'].data.squeeze()
    ind = np.argsort(attention)[::-1]
    attention = attention[ind]
    rois_all = np.hstack((rois[:,1:],roi_scores))
    rois_all =  rois_all[ind]
    for i in xrange(5):
        ascore = attention[i]
        roi = rois_all[i]
        cv2.rectangle(im,(roi[0],roi[1]),(roi[2],roi[3]),(255,0,0),1)

    cv2.imshow('im',im)
    cv2.waitKey(0)
    timer.toc()

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

def demo(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    im_detect(net, im)
    cls_score = net.blobs['cls_score'].data.copy()
    cls_score_reindexed_caffe= net.blobs['cls_score_reindexed'].data.copy()
    vatt_caffe = net.blobs['vatt'].data.copy()
    cls_score_tiled_caffe= net.blobs['cls_score_tiled'].data.copy()
    cls_score_tiled_transposed_caffe = net.blobs['cls_score_tiled_transposed'].data.copy()
    vatt_raw_caffe = net.blobs['vatt_raw'].data.copy()
    attention_caffe = net.blobs['attention'].data.copy()
    attention_tiled_caffe = net.blobs['attention_tiled'].data.copy()
    cls_score_tiled_caffe = net.blobs['cls_score_tiled'].data.copy()

    cls_score_transposed = cls_score.transpose((1,0,2,3))
    cls_score_reindexed = cls_score_transposed[15,...]

    attention = softmax(cls_score_reindexed.squeeze())
    rois = net.blobs['rois'].data
    rois = rois/net.blobs['im_info'].data[0,2]
    roi_scores = net.blobs['rois_score'].data

    vatt = np.zeros((rois.shape[0],21,1,1),np.float32)
    for i in xrange(vatt.shape[0]):
        vatt[i] += attention[i] * cls_score[i]
    #vatt = vatt.sum(axis=0)
    vatt_summed= vatt.sum(axis=0)
    attention = net.blobs['attention'].data[:,0].squeeze()
    ind = np.argsort(attention)[::-1]
    attention = attention[ind]
    rois_all = np.hstack((rois[:,1:],roi_scores))
    rois_all =  rois_all[ind]
    for i in xrange(5):
        ascore = attention[i]
        roi = rois_all[i]
        cv2.rectangle(im,(roi[0],roi[1]),(roi[2],roi[3]),(255,0,0),1)

    cv2.imshow('im',im)
    cv2.waitKey(0)
    timer.toc()

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser(description='Faster R-CNN demo')
    parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
                        default=0, type=int)
    parser.add_argument('--cpu', dest='cpu_mode',
                        help='Use CPU mode (overrides --gpu)',
                        action='store_true')
    parser.add_argument('--net', dest='demo_net', help='Network to use [ResNet-101]',
                        choices=NETS.keys(), default='ResNet-50')

    args = parser.parse_args()

    return args

if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()

    prototxt = os.path.join(cfg.MODELS_DIR, NETS[args.demo_net][0],
                            'rfcn_end2end', 'test_iccv_rpn.prototxt')
    #caffemodel = os.path.join(cfg.DATA_DIR, 'rfcn_models_iccv/resnet50_rfcn_iter_10000.caffemodel')

    caffemodel = os.path.join(cfg.DATA_DIR, 'rfcn_models/resnet50_rfcn_final.caffemodel')
    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\n').format(caffemodel))

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        cfg.GPU_ID = args.gpu_id
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)

    # Warmup on a dummy image
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    for i in xrange(2):
        im_detect(net, im)

    #im_names = ['000456.jpg', '000542.jpg', '001150.jpg',
                #'001763.jpg', '004545.jpg']
    im_names = ['004545.jpg']
    for im_name in im_names:
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print 'Demo for data/demo/{}'.format(im_name)
        demo(net, im_name)

    plt.show()


text:
"""Generic curve fitting tool"""
import numpy as np
from scipy.optimize import curve_fit
from ..common.datacondition import deglitch

class CurveFitter1D:
    """1-dim curve fitting tool class. Unnecessary abstraction. To be deprecated."""
    def __init__(self, x=[0], y=[0], func=lambda x: 0):
        """Load data and fit function.

        func: f(x, *param) form
        """
        #k = deglitch(y, factor=1e6)      # remove glitch by orders of mag
        #self.x, self.y = x[k], y[k]
        self.x, self.y = x, y
        self.func = func

    def fit(self, guess):
        self.popt, self.pcov = curve_fit(self.func, self.x, self.y, guess)
        return self.popt, self.pcov

    def build_curve(self, **kwargs):
        """Build fit data arrays.

        Keyword arguments:
            num, minfac, maxfac
            xmin, xmax: overides the limits given by data
        """
        num     = kwargs.get("num", 100)
        minfac  = kwargs.get("minfac", 1)
        maxfac  = kwargs.get("maxfac", 1)
        xmin = kwargs.get("xmin", np.amin(self.x))
        xmax = kwargs.get("xmax", np.amax(self.x))

        self.xfit = np.array(np.linspace((xmin + xmax)/2 +
                    (xmin - xmax)/2*minfac,
                    (xmin + xmax)/2 - (xmin - xmax)/2*maxfac, num))
        self.yfit = self.func(self.xfit, *self.popt)
        return self.xfit, self.yfit

    def get_popt(self):
        return self.popt

    def get_pcov(self):
        return self.pcov

    def set_popt(self, popt):
        """Set fit parameters manually for reasons other than fitting."""
        self.popt = popt

max_stars_repo_name: bebaek/cryomem

max_issues_repo_name: bebaek/cryomem

max_forks_repo_name: bebaek/cryomem


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

my = [1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,
        2007,2008,2009,2010,2011,2012,2013]
mean_year = np.array(my)

mt = [11.84,12.33,12.59,12.45,12.27,13.41,13.35,12.79,12.90,12.67,12.62,12.17,
        13.11,12.40,12.67,12.86,12.29,13.07,12.83,12.45,13.33]
mean_temp = np.array(mt)

ay = [1993,1994,1995,1996,1997,1998,
     1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
average_year = np.array(ay)

at = [11.84,12.33,12.59,12.45,12.27,13.41,13.35,12.79,12.9,12.67,12.62,12.17,
     13.11,12.4,12.67,12.86,12.29,13.07,12.83,12.45,13.33]
average_temp = np.array(at)

sy = [1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,
      2007,2008,2009,2010,2011,2012,2013]
sea_year = np.array(sy)

st = [22.9843,22.1622,21.9661,21.8135,21.1279,22.1117,20.8799,21.2933,22.6084,
      21.4905,21.3952,23.1089,21.6462,24.1099,22.1351,21.7013,21.989,20.9917,
      23.1961,23.3739,21.491]
sea_temp = np.array(st)

plt.subplot(2, 2, 1)
plt.plot(mean_year, mean_temp, color = "darkgreen")
plt.title("Mean Temperature For Seven Locations Nz")
plt.xlabel("Year")
plt.ylabel("Temperature")

plt.subplot(2, 2, 2)
plt.plot(average_year, average_temp, color = "darkblue")
plt.title("Average Temperature Nz")
plt.xlabel("Year")
plt.ylabel("Temperature")

plt.subplot(2, 2, 3)
plt.plot(sea_year, sea_temp, color = "red")
plt.title("Average Sea Temperature Nz")
plt.xlabel("Year")
plt.ylabel("Temperature")

plt.tight_layout()
plt.show()


text:
"""
 Copyright (c) 2020, salesforce.com, inc.
 All rights reserved.
 SPDX-License-Identifier: BSD-3-Clause
 For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""
import numpy as np

def eval_table_f1(examples, pred_tables_list):
    metrics = dict()
    prec_micro, recall_micro, f1_micro = [], [], []
    for example, pred_tables in zip(examples, pred_tables_list):
        gt_tables = set(example.gt_table_names)
        pred_tables = pred_tables[0]
        assert(len(pred_tables) > 0)
        correct = gt_tables & pred_tables
        precision = len(correct) / len(pred_tables)
        recall = len(correct) / len(gt_tables)
        f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0

        if recall != 1:
            pass
            # print('GT: {}'.format(gt_sql))
            # print('GT tables: {}'.format(sorted(gt_tables)))
            # print('PR: {}'.format(pred_sql))
            # print('Pred tables: {}'.format(sorted(pred_tables)))
            # print()
            # import pdb
            # pdb.set_trace()

        prec_micro.append(precision)
        recall_micro.append(recall)
        f1_micro.append(f1)

    metrics['precision'] = np.mean(prec_micro)
    metrics['recall'] = np.mean(recall_micro)
    metrics['f1'] = np.mean(f1_micro)
    return metrics

max_stars_repo_name: sythello/TabularSemanticParsing

max_issues_repo_name: sythello/TabularSemanticParsing

max_forks_repo_name: sythello/TabularSemanticParsing


text:
import os
import pandas as pd
import numpy as np

from . import physical_params as pp
from . import utils

def read_planet_data(filepath, completo=True):

    columns = ['System', 'Mp', 'Mperr', 'Rp', 'Rperr', \
                    'Mstar', 'Mstarerr', 'Teq', 'sma', \
                    'Tstar', 'Tstarerr', 'Rstar', 'Rstarerr']
    data = pd.read_csv(filepath, names=columns)

    if completo:
        # compute stellar luminosity as computed by COMPLETO
        Msample = pp.compute_mstar_completo_given_observables(
                    data['Tstar'], data['Tstarerr'], \
                    data['Rstar'], data['Rstarerr'], data['Mstar'][0])
        med, errl, erru    = utils.inference(Msample)
        mstar_completo     = med
        mstar_completo_err = ( errl + erru ) / 2.

        # append the stellar mass + err as computed by COMPLETO to the catalog
        data['Mstar_completo']    = mstar_completo
        data['Mstarerr_completo'] = mstar_completo_err

        return data

    return data

def get_catalog(filepath, filename, download_catalog=False, clean_data=True, filter_data=True):
    '''
    Load TEPCAT catalog

    Args:
        filepath         : the path to the catalog
        filename         : name of the catalog
        download_catalog : if True, download catalog!
        clean_data       : if True, select only planets with measured masses, radii, and semimajor axis (i.e. != -1)
        filter_data      : if True, select only planets with:
                           0.37 < Mp / MJ < 2.6
                           4000 < Tstar/K < 7000
                           logg > 4.0
                           0.01 < semimajor axis (AU) < 0.1

    Returns:
        System, Mp, Mperr, Rp, Rperr, Mstar, Mstarerr
    '''
    if download_catalog is True:
        os.system('wget http://www.astro.keele.ac.uk/jkt/tepcat/allplanets-ascii.txt -P data/')

    # original columns from the TEPCAT catalog
    columns = ["System", "Tstar", "Tstaru", "Tstarl", "FeH", "FeHu", "FeHl", "Mstar",
           "Mstaru", "Mstarl", "Rstar", "Rstaru", "Rstarl", "logg",
           "loggu", "loggl", "rhos", "rhosu", "rhosl", "period", "ecc",
           "eccu", "eccl", "sma", "smau", "smal", "Mp", "mpu", "mpl", "Rp",
           "rpu", "rpl", "gravityp", "gravitypu", "gravitypl", "rhop",
           "rhopu", "rhopl", "teq", "tequ", "teql", "discovery-ref", "recent-ref"]

    catalog = pd.read_csv(filepath+filename, sep = '\s+', names = columns, skiprows=1)

    # these are the only columns this function returns
    selected_columns = ['System', 'Mp', 'Mperr', 'Rp', 'Rperr', \
                    'Mstar', 'Mstarerr', 'Teq', 'sma', 'smaerr', \
                    'Tstar', 'Tstarerr', 'Rstar', 'Rstarerr']

    if clean_data is True:
        catalog = catalog.loc[ (catalog['Mp'] > 0) & (catalog['Rp'] > 0) & (catalog['sma'] > 0)]

        # compute symmetric uncertainties
        catalog['Mperr']    = ( catalog['mpu'] + catalog['mpl'] ) / 2.
        catalog['Rperr']    = ( catalog['rpu'] + catalog['rpl'] ) / 2.
        catalog['Mstarerr'] = ( catalog['Mstaru'] + catalog['Mstarl'] ) / 2.
        catalog['smaerr']   = ( catalog['smau'] + catalog['smal'] ) / 2.
        catalog['Tstarerr'] = ( catalog['Tstaru'] + catalog['Tstarl'] ) / 2.
        catalog['Rstarerr'] = ( catalog['Rstaru'] + catalog['Rstarl'] ) / 2.
        catalog['Feherr']   = ( catalog['FeHu'] + catalog['FeHl'] ) / 2.

        # compute Teq
        catalog['Teq'] = pp.compute_teq(catalog['sma'], catalog['Rstar'], catalog['Tstar'])

        if not filter_data:
            return catalog[selected_columns].reset_index(drop=True)

        else:
            # filter data
            catalog = catalog.loc[ (catalog['Mp'] >= 0.37) & (catalog['Mp'] <= 13) \
                        & (catalog['Tstar'] > 4000) & (catalog['Tstar'] < 7000) \
                        & (catalog['logg'] >= 4) \
                        & (catalog['sma'] > 0.01) & (catalog['sma'] < 0.1)]

            return catalog[selected_columns].reset_index(drop=True)

max_stars_repo_name: psarkis/bloatedHJs

max_issues_repo_name: psarkis/bloatedHJs

max_forks_repo_name: psarkis/bloatedHJs