text:
## All this functions were grenerated to dynamically print output.
import sys
import numpy as np
import time
import pylab as plt

class Printer():
    """
    Print things to stdout on one line dynamically
    """
    def __init__(self):
        self.tic=time.clock()
        sys.stdout.flush()
        self.data=[]

    def printtextoneline(self,string):
        sys.stdout.write("\r\x1b[K"+string.__str__())
        sys.stdout.flush()

    def timepercentprint(self,minv,maxv,step,i,neddies,loop2=None,diagnostics=False):
        data=[]
        percent=(float(i+1)/maxv)*100.0
        etime=round(time.clock()-self.tic)
        stmtime=round((etime/percent)*100)

        progress=int(10/(maxv/(step*(i+1))))
        emptyprog=10-progress
        if loop2!=None:
            percent2ndloop=float((float(loop2[2]+1)/loop2[1])*10)
            #print(etime*np.exp(5-((percent2ndloop*10)/percent)*5))
            #stmtime=round(etime*np.exp(-((percent2ndloop*10)/percent)*5))
            #print((etime/percent)/(percent2ndloop*10))
            #stmtime=round((((etime/percent)*100)/(percent2ndloop*10))*100)
            x=2*np.pi
            if percent2ndloop < 9.5:
                stmtime=round(np.exp(x))
            else:
                stmtime=round((((etime/percent)*100)/(percent2ndloop*10))*100)
            if percent2ndloop==10:
                percent2ndloop='>'
            else:
                percent2ndloop=int(percent2ndloop)
            sys.stdout.write("\r 0% [{0}{1}{2}]{3}% | Elapsed Time: {4} s | Estimated Time: {5} s | Info: {6} |".format("="*progress,percent2ndloop,' '*emptyprog,round(percent),etime,stmtime,neddies))
            self.data.append(stmtime)
            if i != maxv and loop2[2]!=loop2[1]:
                sys.stdout.flush()
            else:
                #print(self.data)
                #plt.plot(self.data)
                #plt.show()
                print('')
        else:
            sys.stdout.write("\r 0% [{0}>{1}]{2}% | Elapsed Time: {3} s | Estimated Time: {4} s | Info: {5} |".format("="*progress,' '*emptyprog,round(percent),etime,stmtime,neddies))
            if percent != 100 and i!=maxv:
                sys.stdout.flush()
            else:
                #print(self.data)
                #plt.plot(self.data)
                #plt.show()
                print('')

max_stars_repo_name: navidcy/trackeddy

max_issues_repo_name: powerwordlearner/trackeddy

max_forks_repo_name: powerwordlearner/trackeddy


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from copy import deepcopy

import numpy as np

from astropy.table import groups, QTable
from astropy.time import Time, TimeDelta
from astropy import units as u
from astropy.units import Quantity

from .core import TimeSeries

__all__ = ['BinnedTimeSeries']

class BinnedTimeSeries(TimeSeries):

    _require_time_column = False

    def __init__(self, data=None, start_time=None, end_time=None,
                 bin_size=None, n_bins=None, **kwargs):

        super().__init__(data=data, **kwargs)

        # FIXME: this is because for some operations, an empty time series needs
        # to be created, then columns added one by one. We should check that
        # when columns are added manually, time is added first and is of the
        # right type.
        if (data is None and start_time is None and end_time is None and
                bin_size is None and n_bins is None):
            self._required_columns = ['start_time', 'bin_size']
            return

        # First if start_time and end_time have been given in the table data, we
        # should extract them and treat them as if they had been passed as
        # keyword arguments.

        if 'start_time' in self.colnames:
            if start_time is None:
                start_time = self.columns['start_time']
                self.remove_column('start_time')
            else:
                raise TypeError("'start_time' has been given both in the table "
                                "and as a keyword argument")

        if 'bin_size' in self.colnames:
            if bin_size is None:
                bin_size = self.columns['bin_size']
                self.remove_column('bin_size')
            else:
                raise TypeError("'bin_size' has been given both in the table "
                                "and as a keyword argument")

        if start_time is None:
            raise TypeError("'start_time' has not been specified")

        if end_time is None and bin_size is None:
            raise TypeError("Either 'bin_size' or 'end_time' should be specified")

        if not isinstance(start_time, Time):
            start_time = Time(start_time)

        if end_time is not None and not isinstance(end_time, Time):
            end_time = Time(end_time)

        if bin_size is not None and not isinstance(bin_size, (Quantity, TimeDelta)):
            raise TypeError("'bin_size' should be a Quantity or a TimeDelta")

        if isinstance(bin_size, TimeDelta):
            bin_size = bin_size.sec * u.s

        if start_time.isscalar:

            # We interpret this as meaning that this is the start of the
            # first bin and that the bins are contiguous. In this case,
            # we require bin_size to be specified.

            if bin_size is None:
                raise TypeError("'start_time' is scalar, so 'bin_size' is required")

            if bin_size.isscalar:

                if data is not None:
                    # TODO: raise error if also passed explicily and inconsistent
                    n_bins = len(self)

                bin_size = np.repeat(bin_size, n_bins)

            time_delta = np.cumsum(bin_size)
            end_time = start_time + time_delta

            # Now shift the array so that the first entry is 0
            time_delta = np.roll(time_delta, 1)
            time_delta[0] = 0. * u.s

            # Make start_time into an array
            start_time = start_time + time_delta

        else:

            if len(self.colnames) > 0 and len(start_time) != len(self):
                raise ValueError("Length of 'start_time' ({0}) should match "
                                 "table length ({1})".format(len(start_time), len(self)))

            if end_time is not None:
                if end_time.isscalar:
                    times = start_time.copy()
                    times[:-1] = times[1:]
                    times[-1] = end_time
                    end_time = times
                bin_size = (end_time - start_time).sec * u.s
            elif bin_size is None:
                raise TypeError("Either 'bin_size' or 'end_time' should be specified")

        self.add_column(start_time, index=0, name='start_time')
        self.add_index('start_time')

        if bin_size.isscalar:
            bin_size = np.repeat(bin_size, len(self))

        self.add_column(bin_size, index=1, name='bin_size')

    @property
    def start_time(self):
        return self['start_time']

    @property
    def end_time(self):
        return self['start_time'] + self['bin_size']

    @property
    def centre_time(self):
        return self['start_time'] + self['bin_size'] * 0.5

    def __getitem__(self, item):
        if self._is_list_or_tuple_of_str(item):
            if 'start_time' not in item or 'bin_size' not in item:
                out = QTable([self[x] for x in item],
                             meta=deepcopy(self.meta),
                             copy_indices=self._copy_indices)
                out._groups = groups.TableGroups(out, indices=self.groups._indices,
                                                 keys=self.groups._keys)
                return out
        return super().__getitem__(item)

ext: py

max_stars_repo_name: astrofrog/astropy-timeseries

max_issues_repo_name: astrofrog/astropy-timeseries

max_forks_repo_name: astrofrog/astropy-timeseries

reason: import numpy,from astropy


text:
#!/usr/bin/env python
"""
Library of utilities for printing latex table
"""
from __future__ import absolute_import
from __future__ import print_function

import os
import sys
import numpy as np

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from matplotlib.pyplot import cm

__author__ = "Xin Wang"
__email__ = "wangxin@nii.ac.jp"
__copyright__ = "Copyright 2020, Xin Wang"

#####################
## Latex table
#####################
def return_one_row_latex(content_buffer):
    return " & ".join(content_buffer) + r"\\ " + "\n"

def return_one_row_text(content_buffer):
    return " ".join(content_buffer) + "\n"

def fill_cell(text, length, sep=''):
    return "{str:^{wid}}".format(str=text, wid=length) + sep

def wrap_value(data, wrap_factor=0):
    if wrap_factor == 0:
        return data
    else:
        ratio = (1+wrap_factor) / (1-wrap_factor)
        return np.power((1 - np.power(1 - data, ratio)), 1/ratio)

def return_latex_color_cell(value, val_min, val_max, scale, wrap, color_func):
    if scale < 0:
        value = wrap_value((value - val_min) / (val_max - val_min), wrap)*-scale
        value = -scale - value
    else:
        value = wrap_value((value - val_min) / (val_max - val_min), wrap)*scale
    # only use RGB, not RGBA
    color_code = color_func(value)[:-1]

    color_code = ', '.join(["{:0.2f}".format(x) for x in color_code])
    return r"\cellcolor[rgb]{" + color_code + "}"

def is_valid_float(val):
    try:
        float(val)
    except ValueError:
        return False
    else:
        if val != np.inf and val == val:
            return True
        else:
            return False

def return_valid_number_idx(data_array):
    is_numeric_3 = np.vectorize(is_valid_float, otypes = [bool])
    return is_numeric_3(data_array)

def print_table(data_array, column_tag, row_tag,
                print_format = "1.2f",
                with_color_cell = True,
                colormap='Greys',
                colorscale=0.5,
                colorwrap=0,
                col_sep='',
                print_latex_table=True,
                print_text_table=True,
                print_format_along_row=True,
                color_minmax_in = 'global',
                pad_data_column = 0,
                pad_dummy_col = 0):
    """
    print a latex table given the data (np.array) and tags

    input
    -----
      data_array: np.array [M, N]
      column_tag: list of str, length N, tag in the first row
      row_tag: list of str, length M, tags in first col of each row

      print_format: str or list of str, specify the format to print number
                    default "1.2f"
      print_format_along_row: bool, when print_format is a list, is this
                    list specified for rows? Default True
                    If True, row[n] will use print_format[n]
                    If False, col[n] will use print_format[n]

      with_color_cell: bool, default True,
                      whether to use color in each latex cell
      colormap: str, color map name (matplotlib)
      colorscale: float, default 0.5,
                    the color value will be between (0, colorscale)
                  list of float
      colorwrap: float, default 0, wrap the color-value mapping curve
                 colorwrap > 0 works like mels-scale curve
      col_sep: str, additional string to separate columns.
                  You may use '\t' or ',' for CSV
      print_latex_table: bool, print the table as latex command (default True)
      print_text_table: bool, print the table as text format (default True)
      color_minmax_in: how to decide the max and min to compute cell color?
                 'global': get the max and min values from the input matrix
                 'row': get the max and min values from the current row
                 'col': get the max and min values from the current column
                  (min, max): given the min and max values
                 default is global
      pad_data_column: int, pad columns on the left or right of data matrix
                  (the tag column will still be on the left)
                  0: no padding (default)
                  -N: pad N dummy data columns to the left
                   N: pad N dummy data columns to the right

      pad_dummy_col: int, pad columns to the left or right of the table
                  (the column will be padded to the left of head column)
                  0: no padding (default)
                  N: pad N columns to the left

    output
    ------
      latext_table, text_table

    Tables will be printed to the screen.
    The latex table will be surrounded by begin{tabular}...end{tabular}
    It can be directly pasted to latex file.
    However, it requires usepackage{colortbl} to show color in table cell.
    """
    if column_tag is None:
        column_tag = ["" for data in data_array[0, :]]
    if row_tag is None:
        row_tag = ["" for data in data_array]

    if pad_data_column < 0:
        column_tag = ["" for x in range(-pad_data_column)] + column_tag
        dummy_col = np.zeros([data_array.shape[0], -pad_data_column]) + np.nan
        data_array = np.concatenate([dummy_col, data_array], axis=1)
    elif pad_data_column > 0:
        column_tag = ["" for x in range(pad_data_column)] + column_tag
        dummy_col = np.zeros([data_array.shape[0], pad_data_column]) + np.nan
        data_array = np.concatenate([data_array, dummy_col], axis=1)
    else:
        pass

    # check print_format
    if type(print_format) is not list:
        if print_format_along_row:
            # repeat the tag
            print_format = [print_format for x in row_tag]
        else:
            print_format = [print_format for x in column_tag]
    else:
        if print_format_along_row:
            assert len(print_format) == len(row_tag)
        else:
            assert len(print_format) == len(column_tag)

    # color configuration
    color_func = cm.get_cmap(colormap)
    data_idx = return_valid_number_idx(data_array)
    #value_min = np.min(data_array[data_idx])
    #value_max = np.max(data_array[data_idx])

    def get_latex_color(data_array, row_idx, col_idx, color_minmax_in):
        x = data_array[row_idx, col_idx]
        if color_minmax_in == 'row':
            data_idx = return_valid_number_idx(data_array[row_idx])
            value_min = np.min(data_array[row_idx][data_idx])
            value_max = np.max(data_array[row_idx][data_idx])
            if type(colorscale) is list:
                colorscale_tmp = colorscale[row_idx]
        elif color_minmax_in == 'col':
            data_idx = return_valid_number_idx(data_array[:, col_idx])
            value_min = np.min(data_array[:, col_idx][data_idx])
            value_max = np.max(data_array[:, col_idx][data_idx])
            if type(colorscale) is list:
                colorscale_tmp = colorscale[col_idx]
        elif type(color_minmax_in) is tuple or type(color_minmax_in) is list:
            value_min = color_minmax_in[0]
            value_max = color_minmax_in[1]
            if type(colorscale) is list:
                colorscale_tmp = colorscale[row_idx, col_idx]
        else:
            data_idx = return_valid_number_idx(data_array)
            value_min = np.min(data_array[data_idx])
            value_max = np.max(data_array[data_idx])
            if type(colorscale) is list:
                colorscale_tmp = colorscale[row_idx, col_idx]

        if type(colorscale) is not list:
            colorscale_tmp = colorscale

        # return a color command for latex cell
        return return_latex_color_cell(x, value_min, value_max,
                                       colorscale_tmp, colorwrap, color_func)

    # maximum width for tags in 1st column
    row_tag_max_len = max([len(x) for x in row_tag])

    # maximum width for data and tags for other columns
    if print_format_along_row:
        tmp_len = []
        for idx, data_row in enumerate(data_array):
            tmp_len.append(
                max([len("{num:{form}}".format(num=x, form=print_format[idx])) \
                     for x in data_row]))
    else:
        tmp_len = []
        for idx, data_col in enumerate(data_array.T):
            tmp_len.append(
                max([len("{num:{form}}".format(num=x, form=print_format[idx])) \
                     for x in data_col]))
    col_tag_max_len = max([len(x) for x in column_tag] + tmp_len)

    # prepare buffer
    text_buffer = ""
    latex_buffer = ""
    text_cell_buffer = []
    latex_cell_buffer = []

    # latex head
    if pad_dummy_col > 0:
        latex_buffer += r"\begin{tabular}{" \
                        + ''.join(['c' for x in column_tag + ['']])
        latex_buffer += ''.join(['c' for x in range(pad_dummy_col)]) + r"}"+"\n"
    else:
        latex_buffer += r"\begin{tabular}{" \
                        + ''.join(['c' for x in column_tag + ['']]) + r"}"+"\n"

    # head row
    #  for latex
    hrow = [fill_cell("", row_tag_max_len)] \
           + [fill_cell(x, col_tag_max_len) for x in column_tag]
    if pad_dummy_col > 0:
        hrow = [fill_cell("", 1) for x in range(pad_dummy_col)] + hrow

    latex_buffer += return_one_row_latex(hrow)
    latex_cell_buffer.append(hrow)

    #  for plain text (add additional separator for each column)
    hrow = [fill_cell("", row_tag_max_len, col_sep)] \
           + [fill_cell(x, col_tag_max_len, col_sep) for x in column_tag]
    text_buffer += return_one_row_text(hrow)
    text_cell_buffer.append(hrow)

    # contents
    row = data_array.shape[0]
    col = data_array.shape[1]
    for row_idx in np.arange(row):
        # row head
        row_content_latex = [fill_cell(row_tag[row_idx], row_tag_max_len)]
        row_content_text = [fill_cell(row_tag[row_idx],row_tag_max_len,col_sep)]

        if pad_dummy_col > 0:
            row_content_latex = [fill_cell("", 1) for x in range(pad_dummy_col)] \
                                + row_content_latex

        # each column in the raw
        for col_idx in np.arange(col):

            if print_format_along_row:
                tmp_print_format = print_format[row_idx]
            else:
                tmp_print_format = print_format[col_idx]

            if is_valid_float(data_array[row_idx,col_idx]):
                num_str = "{num:{form}}".format(num=data_array[row_idx,col_idx],
                                                form=tmp_print_format)
                latex_color_cell = get_latex_color(data_array, row_idx, col_idx,
                                                   color_minmax_in)
            elif type(data_array[row_idx,col_idx]) is str:
                num_str = "{num:{form}}".format(num=data_array[row_idx,col_idx],
                                                form=tmp_print_format)
                latex_color_cell = ''
            else:
                num_str = ''
                latex_color_cell = ''

            if not with_color_cell:
                latex_color_cell = ''

            row_content_text.append(
                fill_cell(num_str, col_tag_max_len, col_sep))

            row_content_latex.append(
                fill_cell(latex_color_cell + ' ' + num_str, col_tag_max_len))

        # latex table content
        latex_buffer += return_one_row_latex(row_content_latex)
        latex_cell_buffer.append(row_content_latex)
        # text content
        text_buffer += return_one_row_text(row_content_text)
        text_cell_buffer.append(row_content_text)

    latex_buffer += r"\end{tabular}" + "\n"

    if print_latex_table:
        print(latex_buffer)
    if print_text_table:
        print(text_buffer)
    return latex_buffer, text_buffer, latex_cell_buffer, text_cell_buffer

def concatenate_table(table_list, ignore_initial=True, add_separator=1, latex=True):
    rows = [len(x) for x in table_list]
    if len(list(set(rows))) > 1:
        print("Input tables have different row numbers")
        return None

    output_text = ""
    output_table = []
    for row in range(len(table_list[0])):
        temp = []
        for idx, subtable in enumerate(table_list):
            if ignore_initial:
                temp += subtable[row][1:]
            else:
                temp += subtable[row]
            if add_separator and idx < len(table_list)-1:
                temp += ['' for x in range(add_separator)]
        output_table.append(temp)
        output_text += return_one_row_latex(temp)

    # latex head
    latex_buffer = r"\begin{tabular}{" \
                 + ''.join(['c' for x in temp + ['']]) + r"}" + "\n"
    latex_buffer += output_text
    latex_buffer += r"\end{tabular}" + "\n"

    return latex_buffer, output_table

if __name__ == "__main__":
    print("Tools for printing table for latex")

max_stars_repo_name: Nijta/project-NN-Pytorch-scripts

max_issues_repo_name: Nijta/project-NN-Pytorch-scripts

max_forks_repo_name: Nijta/project-NN-Pytorch-scripts

reason: import numpy


text:
"""
Compute metrics on rendered images (after eval.py).
First computes per-object metric then reduces them. If --multicat is used
then also summarized per-categority metrics. Use --reduce_only to skip the
per-object computation step.

Note eval.py already outputs PSNR/SSIM.
This also computes LPIPS and is useful for double-checking metric is correct.
"""

import os
import os.path as osp
import argparse
import skimage.measure
from tqdm import tqdm
import warnings
import lpips
import numpy as np
import torch
import imageio
import json

parser = argparse.ArgumentParser(description="Calculate PSNR for rendered images.")
parser.add_argument(
    "--datadir",
    "-D",
    type=str,
    default="/home/group/chairs_test",
    help="Dataset directory; note: different from usual, directly use the thing please",
)
parser.add_argument(
    "--output",
    "-O",
    type=str,
    default="eval",
    help="Root path of rendered output (our format, from eval.py)",
)
parser.add_argument(
    "--dataset_format",
    "-F",
    type=str,
    default="dvr",
    help="Dataset format, nerf | srn | dvr",
)
parser.add_argument(
    "--list_name", type=str, default="softras_test", help="Filter list prefix for DVR",
)
parser.add_argument(
    "--gpu_id",
    type=int,
    default=0,
    help="GPU id. Only single GPU supported for this script.",
)
parser.add_argument(
    "--overwrite", action="store_true", help="overwriting existing metrics.txt",
)
parser.add_argument(
    "--exclude_dtu_bad", action="store_true", help="exclude hardcoded DTU bad views",
)
parser.add_argument(
    "--multicat",
    action="store_true",
    help="Prepend category id to object id. Specify if model fits multiple categories.",
)

parser.add_argument(
    "--viewlist",
    "-L",
    type=str,
    default="",
    help="Path to source view list e.g. src_dvr.txt; if specified, excludes the source view from evaluation",
)
parser.add_argument(
    "--eval_view_list", type=str, default=None, help="Path to eval view list"
)
parser.add_argument(
    "--primary", "-P", type=str, default="", help="List of views to exclude"
)
parser.add_argument(
    "--lpips_batch_size", type=int, default=32, help="Batch size for LPIPS",
)

parser.add_argument(
    "--reduce_only",
    "-R",
    action="store_true",
    help="skip the map (per-obj metric computation)",
)
parser.add_argument(
    "--metadata",
    type=str,
    default="metadata.yaml",
    help="Path to dataset metadata under datadir, used for getting category names if --multicat",
)
parser.add_argument(
    "--dtu_sort", action="store_true", help="Sort using DTU scene order instead of lex"
)
args = parser.parse_args()

if args.dataset_format == "dvr":
    list_name = args.list_name + ".lst"
    img_dir_name = "image"
elif args.dataset_format == "srn":
    list_name = ""
    img_dir_name = "rgb"
elif args.dataset_format == "nerf":
    warnings.warn("test split not implemented for NeRF synthetic data format")
    list_name = ""
    img_dir_name = ""
else:
    raise NotImplementedError("Not supported data format " + args.dataset_format)

data_root = args.datadir
render_root = args.output

def run_map():
    if args.multicat:
        cats = os.listdir(data_root)

        def fmt_obj_name(c, x):
            return c + "_" + x

    else:
        cats = ["."]

        def fmt_obj_name(c, x):
            return x

    use_exclude_lut = len(args.viewlist) > 0
    if use_exclude_lut:
        print("Excluding views from list", args.viewlist)
        with open(args.viewlist, "r") as f:
            tmp = [x.strip().split() for x in f.readlines()]
        exclude_lut = {
            x[0] + "/" + x[1]: torch.tensor(list(map(int, x[2:])), dtype=torch.long)
            for x in tmp
        }
    base_exclude_views = list(map(int, args.primary.split()))
    if args.exclude_dtu_bad:
        base_exclude_views.extend(
            [3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 36, 37, 38, 39]
        )

    if args.eval_view_list is not None:
        with open(args.eval_view_list, "r") as f:
            eval_views = list(map(int, f.readline().split()))
            print("Only using views", eval_views)
    else:
        eval_views = None

    all_objs = []

    print("CATEGORICAL SUMMARY")
    total_objs = 0

    for cat in cats:
        cat_root = osp.join(data_root, cat)
        if not osp.isdir(cat_root):
            continue

        objs = sorted([x for x in os.listdir(cat_root)])

        if len(list_name) > 0:
            list_path = osp.join(cat_root, list_name)
            with open(list_path, "r") as f:
                split = set([x.strip() for x in f.readlines()])
            objs = [x for x in objs if x in split]

        objs_rend = [osp.join(render_root, fmt_obj_name(cat, x)) for x in objs]

        objs = [osp.join(cat_root, x) for x in objs]
        objs = [x for x in objs if osp.isdir(x)]

        objs = list(zip(objs, objs_rend))
        objs_avail = [x for x in objs if osp.exists(x[1])]
        print(cat, "TOTAL", len(objs), "AVAILABLE", len(objs_avail))
        #  assert len(objs) == len(objs_avail)
        total_objs += len(objs)
        all_objs.extend(objs_avail)
    print(">>> USING", len(all_objs), "OF", total_objs, "OBJECTS")

    cuda = "cuda:" + str(args.gpu_id)
    lpips_vgg = lpips.LPIPS(net="vgg").to(device=cuda)

    def get_metrics(rgb, gt):
        ssim = skimage.measure.compare_ssim(rgb, gt, multichannel=True, data_range=1)
        psnr = skimage.measure.compare_psnr(rgb, gt, data_range=1)
        return psnr, ssim

    def isimage(path):
        ext = osp.splitext(path)[1]
        return ext == ".jpg" or ext == ".png"

    def process_obj(path, rend_path):
        if len(img_dir_name) > 0:
            im_root = osp.join(path, img_dir_name)
        else:
            im_root = path
        out_path = osp.join(rend_path, "metrics.txt")
        if osp.exists(out_path) and not args.overwrite:
            return
        ims = [x for x in sorted(os.listdir(im_root)) if isimage(x)]
        psnr_avg = 0.0
        ssim_avg = 0.0
        gts = []
        preds = []
        num_ims = 0
        if use_exclude_lut:
            lut_key = osp.basename(rend_path).replace("_", "/")
            exclude_views = exclude_lut[lut_key]
        else:
            exclude_views = []
        exclude_views.extend(base_exclude_views)

        for im_name in ims:
            im_path = osp.join(im_root, im_name)
            im_name_id = int(osp.splitext(im_name)[0])
            im_name_out = "{:06}.png".format(im_name_id)
            im_rend_path = osp.join(rend_path, im_name_out)
            if osp.exists(im_rend_path) and im_name_id not in exclude_views:
                if eval_views is not None and im_name_id not in eval_views:
                    continue
                gt = imageio.imread(im_path).astype(np.float32)[..., :3] / 255.0
                pred = imageio.imread(im_rend_path).astype(np.float32) / 255.0

                psnr, ssim = get_metrics(pred, gt)
                psnr_avg += psnr
                ssim_avg += ssim
                gts.append(torch.from_numpy(gt).permute(2, 0, 1) * 2.0 - 1.0)
                preds.append(torch.from_numpy(pred).permute(2, 0, 1) * 2.0 - 1.0)
                num_ims += 1
        gts = torch.stack(gts)
        preds = torch.stack(preds)

        lpips_all = []
        preds_spl = torch.split(preds, args.lpips_batch_size, dim=0)
        gts_spl = torch.split(gts, args.lpips_batch_size, dim=0)
        with torch.no_grad():
            for predi, gti in zip(preds_spl, gts_spl):
                lpips_i = lpips_vgg(predi.to(device=cuda), gti.to(device=cuda))
                lpips_all.append(lpips_i)
            lpips = torch.cat(lpips_all)
        lpips = lpips.mean().item()
        psnr_avg /= num_ims
        ssim_avg /= num_ims
        out_txt = "psnr {}\nssim {}\nlpips {}".format(psnr_avg, ssim_avg, lpips)
        with open(out_path, "w") as f:
            f.write(out_txt)

    for obj_path, obj_rend_path in tqdm(all_objs):
        process_obj(obj_path, obj_rend_path)

def run_reduce():
    if args.multicat:
        meta = json.load(open(osp.join(args.datadir, args.metadata), "r"))
        cats = sorted(list(meta.keys()))
        cat_description = {cat: meta[cat]["name"].split(",")[0] for cat in cats}

    all_objs = []
    objs = [x for x in os.listdir(render_root)]
    objs = [osp.join(render_root, x) for x in objs if x[0] != "_"]
    objs = [x for x in objs if osp.isdir(x)]
    if args.dtu_sort:
        objs = sorted(objs, key=lambda x: int(x[x.rindex("/") + 5 :]))
    else:
        objs = sorted(objs)
    all_objs.extend(objs)

    print(">>> PROCESSING", len(all_objs), "OBJECTS")

    METRIC_NAMES = ["psnr", "ssim", "lpips"]

    out_metrics_path = osp.join(render_root, "all_metrics.txt")

    if args.multicat:
        cat_sz = {}
        for cat in cats:
            cat_sz[cat] = 0

    all_metrics = {}
    for name in METRIC_NAMES:
        if args.multicat:
            for cat in cats:
                all_metrics[cat + "." + name] = 0.0
        all_metrics[name] = 0.0

    should_print_all_objs = len(all_objs) < 100

    for obj_root in tqdm(all_objs):
        metrics_path = osp.join(obj_root, "metrics.txt")
        with open(metrics_path, "r") as f:
            metrics = [line.split() for line in f.readlines()]
        if args.multicat:
            cat_name = osp.basename(obj_root).split("_")[0]
            cat_sz[cat_name] += 1
            for metric, val in metrics:
                all_metrics[cat_name + "." + metric] += float(val)

        for metric, val in metrics:
            all_metrics[metric] += float(val)
        if should_print_all_objs:
            print(obj_root, end=" ")
            for metric, val in metrics:
                print(val, end=" ")
            print()

    for name in METRIC_NAMES:
        if args.multicat:
            for cat in cats:
                if cat_sz[cat] > 0:
                    all_metrics[cat + "." + name] /= cat_sz[cat]
        all_metrics[name] /= len(all_objs)
        print(name, all_metrics[name])

    metrics_txt = []
    if args.multicat:
        for cat in cats:
            if cat_sz[cat] > 0:
                cat_txt = "{:12s}".format(cat_description[cat])
                for name in METRIC_NAMES:
                    cat_txt += " {}: {:.6f}".format(name, all_metrics[cat + "." + name])
                cat_txt += " n_inst: {}".format(cat_sz[cat])
                metrics_txt.append(cat_txt)

        total_txt = "---\n{:12s}".format("total")
    else:
        total_txt = ""
    for name in METRIC_NAMES:
        total_txt += " {}: {:.6f}".format(name, all_metrics[name])
    metrics_txt.append(total_txt)

    metrics_txt = "\n".join(metrics_txt)
    with open(out_metrics_path, "w") as f:
        f.write(metrics_txt)
    print("WROTE", out_metrics_path)
    print(metrics_txt)

if __name__ == "__main__":
    if not args.reduce_only:
        print(">>> Compute")
        run_map()
    print(">>> Reduce")
    run_reduce()

max_stars_repo_name: Depersonalizc/pixel-nerf

max_issues_repo_name: Depersonalizc/pixel-nerf

max_forks_repo_name: Depersonalizc/pixel-nerf


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Author: José Sánchez-Gallego (gallegoj@uw.edu)
# @Date: 2019-03-04
# @Filename: healpix.py
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause)
#
# @Last modified by: José Sánchez-Gallego (gallegoj@uw.edu)
# @Last modified time: 2019-03-13 15:01:07

import healpy
import numpy
import shapely.geometry
import shapely.prepared
import shapely.vectorized

__all__ = ['nested_regrade', 'get_minimum_nside_pixarea', 'tile_geometry']

def nested_regrade(pixels, nside_in, nside_out):
    """Returns the parent/children pixels from a given HealPix nested pixel.

    The HealPix nested mode follows a quadrilateral tree pixel scheme (see
    Figure 1 in Górski et al. 2005) When the resolution (nside) increases,
    each pixel is divided in four pixels. The numbering of such pixels follows
    a simple binary representation achieved by appending binary digits to the
    new pixels. So, for example, pixel 22 (``b10110``) becomes four new pixels
    with binary numbers ``b1011000, b1011001, b1011010, and b1011011``,
    corresponding to decimal ``91, 92, 93, and 94``.

    This function returns the child pixels from a given pixel when going from
    resolution ``nside_in`` to ``nside_out``, if ``nside_out > nside_in``, or
    the parent pixel if ``nside_out < nside_in``.

    Note that this function works only for pixels using the nested indexing and
    should not be used with the ring indexing.

    Parameters
    ----------
    pixels : int or ~numpy.ndarray
        The pixels for which we want to get the parents/children. Can be a
        single integer or an array of indices.
    nside_in : int
        The ``nside`` of the input pixels. Must be one of :math:`2^k` where
        :math:`k\in[0,1,2,\dots]`.
    nside_out : int
        The destination ``nside``.

    Returns
    -------
    output : `int` or `~numpy.ndarray`
        If ``nside_out < nside_in`` and ``pixels`` is a single value, this
        will be an integer with the parent of the input pixel. If ``pixels``
        is an array the output will be an array of the same size in which each
        element is the parent of the corresponding pixel in the input array.
        If ``nside_out > nside_in`` and ``pixels`` is an integer, the output
        will be an array with the child pixels. The size of the array will be
        :math:`2^{2l}` where ``l=k_out-k_in`` (:math:`n_{side}=2^k`).
        If the input in an array of pixels, the output will be a 2D array in
        which each row contains the child pixels of the corresponding pixel
        in the input array.

    """

    assert nside_in != nside_out, 'nside_in cannot be equal to nside_out.'

    k_in = numpy.log2(nside_in)
    k_out = numpy.log2(nside_out)

    assert k_in.is_integer() and k_out.is_integer(), \
        'nside_in or nside_out are not power of 2.'

    pixels = numpy.atleast_1d(pixels).astype(int)
    assert pixels.ndim == 1, 'dimension of input pixels is invalid.'

    # Npix = 12 * nside**2
    assert numpy.all(pixels <= 12 * nside_in**2), \
        'some pixel indices are greater than the maximum allowed for nside_in.'

    if k_in > k_out:

        degraded = numpy.right_shift(pixels, 2 * int(k_in - k_out))

        return degraded[0] if len(degraded) == 1 else degraded

    else:

        prograded = numpy.zeros((len(pixels), int(2**(2 * (k_out - k_in)))), dtype=int)
        prograded += pixels[numpy.newaxis].T
        for ii in range(int(k_out - k_in))[::-1]:

            prograded = numpy.left_shift(prograded, 2)

            repeats = numpy.repeat([0, 1, 2, 3], (4 ** ii) or 1)
            tiles = numpy.tile(repeats, prograded.shape[1] // len(repeats))

            prograded += tiles

        return prograded

def get_minimum_nside_pixarea(pixarea):
    """Returns the minimum nside needed to achieve a certain pixel resolution.

    Parameters
    ----------
    pixarea : float
        The pixel area in square degrees.

    Returns
    -------
    nside : int
        The minimum nside that guarantees a pixel size of at least the input
        pixel area.

    """

    nside = numpy.sqrt(3. / numpy.pi / pixarea) * 60.
    kk = int(numpy.ceil(numpy.log2(nside)))

    return int(2**kk)

def tile_geometry(polygon, nside, ring=False, return_coords=False, inclusive=True):
    """Returns a list of pixels that tile a Shapely polygon.

    While `healpy.query_polygon` provides an efficient method to tile a
    polygon, that function assumes that the lines joining the vertices of the
    polygon are the segments of the great circles that intersect the vertices.
    This is relatively irrelevant for polygons that cover a small area on the
    sky but for large polygons the result is significantly different from what
    one would expect.

    This function tiles a `Polygon <shapely:Polygon>` assuming an Euclidian
    distance between the vertices. The polygon must have longitude
    limits defined between -360 and 720 degrees.

    If ``inclusive=True`` the function will return all pixels that overlap with
    the region. The area of the pixel is approximated as a circle with the
    same area as a pixel. This approximation is good for low latitudes but
    becomes worse for higher latitudes, where the shape of the pixel cannot
    be approximated by a circle. If ``inclusive=False`` only pixels whose
    centre is inside the region will be returned.

    Parameters
    ----------
    polygon : shapely:Polygon or ~numpy.ndarray
        The polygon to tile. If an array, it must be a collection of ``Nx2``
        points defining the position of the vertices. The polygon must be
        convex.
    nside : int
        The nside of the pixels used to tile the polygon.
    ring : bool
        By default the function returns the values in the nested pixel
        ordering. If ``ring=True`` the equivalent ring pixels will be returned.
    return_coords : bool
        If `True`, returns an array with the longitude and latitude of the
        pixels in degrees.
    inclusive : bool
        Whether to return pixels that only partially overlap with the region.

    Returns
    -------
    tiling : `~numpy.ndarray`
        An array with the list of pixels that tile the geometry or (if
        ``return_coords=True``) the longitude and latitude of the pixels.

    """

    # nside = 2^k

    assert numpy.log2(nside).is_integer(), 'nside is not a power of 2.'
    k_end = int(numpy.log2(nside))

    if not isinstance(polygon, shapely.geometry.Polygon):
        polygon = shapely.geometry.Polygon(polygon.tolist())

    # Create a prepared polygon. This allows only contained and intersect
    # operations but that's all we need and it's more efficient.
    prep_polygon = shapely.prepared.PreparedGeometry(polygon)

    pixels = []
    intersect = []

    for kk in range(0, k_end + 1):

        nside_k = 2**kk

        # Approximates the pixel as a circle of radius r.
        rr = numpy.sqrt(healpy.nside2pixarea(nside_k, degrees=True) / numpy.pi)

        # If k=0 (first HealPix level) we test all the 12 pixels. Otherwise we
        # take the pixels that overlapped in the previous level and test each
        # one of their children.
        if kk == 0 and len(intersect) == 0:
            pix_to_test = list(range(0, 12))
        else:
            pix_to_test = nested_regrade(intersect, 2**(kk - 1), nside_k).flatten().tolist()
            intersect = []

        for pix in pix_to_test:

            lon, lat = healpy.pix2ang(nside_k, pix, nest=True, lonlat=True)

            # We offset the pixel to check so that if the polygon wraps around
            # [0, 360] we still overlap with it.
            for offset in [0, -360, 360]:

                # Create a Point object with a radius dd centred at the
                # position of the pixel +/- the offset.
                point = shapely.geometry.Point(lon + offset, lat).buffer(rr)

                # If a pixel is completely contained by the polygon, adds
                # all the nested pixels at the nside resolution and we are done.
                if prep_polygon.contains(point):
                    if nside_k < nside:
                        pixels += nested_regrade(pix, nside_k, nside).flatten().tolist()
                    else:
                        pixels.append(pix)
                    break

                # If we are at the final nside level and the pixel intersects
                # with the polygon, we include it.
                if nside_k == nside and inclusive and prep_polygon.intersects(point):
                    pixels.append(pix)
                    break

                # If we are not yet at the final nside level we want to be greedy.
                # We create a pixel with twice the radius and check if it intersects.
                # If it does we add it to the list of pixels to test in the next
                # nside level. We do this to compensate for the fact that pixels
                # at high latitudes are significantly non-circular and if we
                # just use point we may be missing some pixels.
                if nside_k < nside:
                    point_2r = shapely.geometry.Point(lon + offset, lat).buffer(2. * rr)
                    if prep_polygon.intersects(point_2r):
                        intersect.append(pix)
                        break

        intersect = numpy.unique(intersect).tolist()

    if len(pixels) == 0:
        raise ValueError('the list of tiling pixels is empty.')

    pixels = numpy.unique(pixels)

    if return_coords:
        lon, lat = healpy.pix2ang(nside, pixels, nest=True, lonlat=True)
        return numpy.array([lon, lat]).T

    if ring:
        return healpy.nest2ring(nside, pixels)

    return pixels

max_stars_repo_name: EricPell/lvmsurveysim

max_issues_repo_name: EricPell/lvmsurveysim

max_forks_repo_name: EricPell/lvmsurveysim


text:
import numpy as np
import argparse # Command parser
from pathlib import Path # file handling
import csv # For diameter history analysis
from datetime import datetime
import util
import video_stream

# Constants
RECORDING_DIR = "recordings" # location at which PD recordings are saved
ZOOM            = 2          # zoom amount
FOCUS_BOX_SCALE = 3          # bounding box scale with respect to zoomed resolution

# Main exec
def main():
    # Handle arguments
    parser = argparse.ArgumentParser(description="Pupil detector")
    parser.add_argument("--src", default=0, required=False,
                        help="Location of video source, or 0 for camera device.")
    args = parser.parse_args()

    # out
    path = ""

    # Recording
    rec = False
    rec_init = 0
    rec_pd = {}
    rec_elapsed = -1

    # Pupil diameter
    pd_std = 0 # calibrated pupil diameter
    pd_cal = False # whether or not due to calibrate pupil diameter
    pd = 0 # current pupil diameter
    perc = -1

    # Set up video stream and run in a separate thread
    vs = video_stream.VideoStream(src=args.src,
                                  zoom=ZOOM,
                                  focus_box_scale=FOCUS_BOX_SCALE,
                                  square=False)

    while True:
        if rec and rec_init == 0: # start of recording
            rec_init = util.now()

            # Generate recording folder and parent folder(s)
            path = "{rd}/{ts}".format(rd=RECORDING_DIR, # timestamp of the recording
                    ts=datetime.fromtimestamp(rec_init//1000).strftime("%Y-%m-%d %H.%M.%S"))
            Path(path).mkdir(parents=True, exist_ok=True)

            path_video = f"{path}/output.avi"

            # Open processed output video
            vs.setup_output(path_video)

            print(f"Generated '{path_video}'")

        # Render and obtain ROI and PD
        frame, pd = vs.render_frame(pd, perc, rec_elapsed)

        #### Post-processing for next frame ####

        # Get elapsed time for tick
        if rec: rec_elapsed = round((util.now()-rec_init)/1000, 3) # [s]

        # Detection of pupil
        if pd > 0:
            if pd_cal: # if pupil diameter is due a calibration
                pd_std = pd # set new pupil diameter standard
                pd_cal = False # toggle calibration off

            # Update PD history if recording is enabled
            if rec: rec_pd[rec_elapsed] = pd # [s]

            # Adjust relative percentage if a standard has been set
            if pd_std != 0: perc = 100*(pd-pd_std)/pd_std

        # Write recorded video to file
        if rec: vs.save_frame(frame)

        # Reset parameters if recording has been toggled off
        if not rec and rec_init != 0:
            # Save PD history to CSV file
            with open(f"{path}/pd_history.csv", "w", newline="") as csv_file:
                writer = csv.writer(csv_file)
                writer.writerow(["Time [s]", "Relative PD"])
                for ms in rec_pd:
                    writer.writerow([ms, rec_pd[ms]])

            # Reset
            rec_init = 0 # reset recording timer
            rec_pd = {} # reset recording PD
            rec_elapsed = -1 # reset elapsed recorded seconds

        # Hot keys
        k = vs.wait_key(1) & 0xFF
        if   k != 0xFF:     print(f"> {chr(k)}") # display pressed key
        if   k == ord("q") and ~rec: break # [Q] exit playback when not recording
        elif k == ord("r"): rec = ~rec     # [R] toggle recording
        elif k == ord("c"): pd_cal = True  # [C] toggle pupil diameter calibration

    vs.close()

if __name__ == "__main__": main()


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

# This script builds a network manually using network.add, so that it
# has two 3-bus disconnected AC networks, connected by HVDC
# links. Storage and generation (wind and gas) is then optimised.

# The network is then written as CSV files to csv_folder_name.

import datetime

import numpy as np
import pandas as pd

import pypsa

# ensure the same random number are produced when refactoring
np.random.seed(1)

csv_folder_name = "opf-storage-data"

# Build the Network object, which stores all other objects

network = pypsa.Network()

# Build the snapshots we consider for the first T hours in 2015

T = 12

network.set_snapshots(
    pd.to_datetime(
        [datetime.datetime(2015, 1, 1) + datetime.timedelta(hours=i) for i in range(T)]
    )
)

# let each snapshot represent 3 hours
network.snapshot_weightings = pd.Series(3.0, index=network.snapshots)

print("network:", network)
print("snapshots:", network.snapshots)

# add fuel types
network.add("Carrier", "gas", co2_emissions=0.24)
network.add("Carrier", "wind")
network.add("Carrier", "battery")

# in kg CO2e
network.add("GlobalConstraint", "co2_limit", sense="<=", constant=1000.0)

# The network is two three-node AC networks connected by 2 point-to-point DC links

# building block
n = 3

# copies
c = 2

# add buses
for i in range(n * c):
    network.add("Bus", i, v_nom="380")

# add lines
for i in range(n * c):
    network.add(
        "Line",
        i,
        bus0=str(i),
        bus1=str(n * (i // n) + (i + 1) % n),
        x=np.random.random(),
        capital_cost=0.2 * np.random.random(),
        s_nom_extendable=True,
    )

# make one line non-extendable
network.lines.at["2", "s_nom"] = 200.0
network.lines.at["2", "s_nom_extendable"] = False

# add HVDC lines
for i in range(2):
    network.add(
        "Link",
        "TL %d" % (i),
        bus0=str(i),
        bus1=str(3 + i),
        p_max_pu=0.9,
        p_min_pu=-0.9,
        p_nom=1000,
        capital_cost=0.2 * np.random.random(),
        p_nom_extendable=True,
    )

# make one HVDC non-extendable
network.links.at["TL 1", "p_nom"] = 300.0
network.links.at["TL 1", "p_nom_extendable"] = False

# add loads
for i in range(n * c):
    network.add("Load", i, bus=str(i))

# add some generators
for i in range(n * c):
    # storage
    network.add(
        "StorageUnit",
        "Storage %d" % (i),
        bus=str(i),
        p_nom=0,
        carrier="battery",
        marginal_cost=4 * np.random.random(),
        capital_cost=1000 * np.random.random(),
        p_nom_extendable=True,
        p_max_pu=1,
        p_min_pu=-1,
        efficiency_store=0.9,
        efficiency_dispatch=0.95,
        standing_loss=0.01,
        max_hours=6,
    )
    # wind generator
    network.add(
        "Generator",
        "Wind %d" % (i),
        bus=str(i),
        p_nom=100,
        carrier="wind",
        marginal_cost=0
        + 0.01
        * np.random.random(),  # non-zero marginal cost ensures unique optimisation result
        capital_cost=2000 + 1000 * np.random.random(),
        p_nom_extendable=True,
        p_nom_min=100,
    )
    # gas generator
    network.add(
        "Generator",
        "Gas %d" % (i),
        bus=str(i),
        p_nom=0,
        carrier="gas",
        marginal_cost=2 + 4 * np.random.random(),
        capital_cost=100 + 100 * np.random.random(),
        efficiency=0.35 + 0.01 * np.random.random(),
        p_max_pu=0.85,
        p_min_pu=0.02,
        p_nom_extendable=True,
        p_nom_min=0,
    )

# make some objects non-extendable - useful for unit testing
network.generators.at["Gas 0", "p_nom"] = 350
network.generators.at["Gas 0", "p_nom_extendable"] = False

network.generators.at["Wind 2", "p_nom"] = 150
network.generators.at["Wind 2", "p_nom_extendable"] = False

network.storage_units.at["Storage 1", "p_nom"] = 25
network.storage_units.at["Storage 1", "p_nom_extendable"] = False

# now attach some time series

network.loads_t.p_set = pd.DataFrame(
    index=network.snapshots,
    columns=network.loads.index,
    data=1000 * np.random.rand(len(network.snapshots), len(network.loads)),
)

wind_generators = network.generators[network.generators.carrier == "wind"]

network.generators_t.p_max_pu = network.generators_t.p_max_pu.reindex(
    columns=wind_generators.index
)

network.generators_t.p_max_pu.loc[:, wind_generators.index] = pd.DataFrame(
    index=network.snapshots,
    columns=wind_generators.index,
    data=np.random.rand(len(network.snapshots), len(wind_generators)),
)

network.storage_units.state_of_charge_initial = 0.0

# make the storage more complicated
network.storage_units.at["Storage 2", "cyclic_state_of_charge"] = True
network.storage_units.at["Storage 4", "cyclic_state_of_charge"] = True

network.storage_units_t.state_of_charge_set.loc[:, "Storage 3"] = np.nan
network.storage_units_t.state_of_charge_set.at[network.snapshots[3], "Storage 3"] = 50.0

network.storage_units_t.state_of_charge_set.loc[:, "Storage 4"] = np.nan
network.storage_units_t.state_of_charge_set.at[network.snapshots[2], "Storage 4"] = 25.0

network.storage_units_t.inflow.loc[:, "Storage 0"] = 0.0
network.storage_units_t.inflow[["Storage 0"]] = 20.0

network.name = "Test 6 bus"

network.export_to_csv_folder(csv_folder_name)

max_stars_repo_name: p-glaum/PyPSA

max_issues_repo_name: p-glaum/PyPSA

max_forks_repo_name: p-glaum/PyPSA


text:
from __future__ import print_function, division
import numpy as np
from Top2Phase.utils import PeriodicCKDTree, distance_pbc
from tqdm import tqdm
from pymatgen.core.structure import IStructure

import mdtraj as md
import numpy as np
import os, sys
import argparse
import boo

def main():
    parser = argparse.ArgumentParser('The traj.xtc convertor to order parameters npz')
    parser.add_argument('--xtc_file', help='path to the xtc file')
    parser.add_argument('--gro_file', help='path to gro file')
    parser.add_argument('--npz_file', help='path to save order parmeters as a npz file')
    parser.add_argument('--radius', type=float,  default=0.7,  help='radius for neigh list')
    parser.add_argument('--skip', type=int, default=10, help='skip frames')

    args = parser.parse_args()
    print("topology file: ", args.gro_file)
    print("processing trajectory: ", args.xtc_file)
    print("with radius: ", args.radius)
    print("saving to file: ", args.npz_file)

    if not os.path.exists(args.xtc_file):
        raise NameError(args.xtc_file + 'file does not exit')
    if not os.path.exists(args.gro_file):
        raise NameError(args.gro_file + 'file does not exit')

    traj = md.load(args.xtc_file, top=args.gro_file)
    traj_coords = traj.xyz[:,::4,:]
    lattices = traj.unitcell_vectors
    atom_types = np.array([8 for i in range(traj_coords.shape[1])]).astype(int)
    target_index = np.array([i for i in range(int(traj_coords.shape[1]))]).astype(int)

    nbr_lists, diag_lattices = [], []
    radius = args.radius
    cnt_frame = 0
    Qall_dict ={}
    for coord, lattice in tqdm(zip(traj_coords, lattices),
                        total=len(traj_coords)):
        # take the diagonal part of the lattice matrix
        lattice = np.diagonal(lattice)
        diag_lattices.append(lattice)
        pkdt = PeriodicCKDTree(lattice, coord)
        all_nbrs_idxes = pkdt.query_ball_point(coord, radius)
        nbr_list = []
        lsis = np.zeros(coord.shape[0])
        qtet = np.zeros(coord.shape[0])
        for idx, nbr_idxes in enumerate(all_nbrs_idxes):
            nbr_dists = distance_pbc(coord[idx],
                    coord[nbr_idxes],
                    lattice)
            nbr_idx_dist = sorted(zip(nbr_idxes, nbr_dists),key=lambda x: x[1])
            #print(idx, nbr_idx_dist[1:7])
            list_lsi = []
            for idy in nbr_idx_dist[1:]:
                if idy[1] <0.37:
                    list_lsi.append(idy[1])
            lsis[idx] = np.mean((np.array(list_lsi) -np.mean(list_lsi))**2)
            if len(nbr_idx_dist) > 7:
                for idy in nbr_idx_dist[1:7]:
                    nbr_list.append([idx, idy[0]])
            if len(nbr_idx_dist) > 5:
                for idj in  range(3):
                    for idk in range(idj+1,4):
                        d1 = nbr_idx_dist[idj+1][1]
                        d2 = nbr_idx_dist[idk+1][1]
                        d3 = distance_pbc(coord[nbr_idx_dist[idj+1][0]],
                           coord[[nbr_idx_dist[idk+1][0]]],
                           lattice)
                        qtet[idx] += ((d1**2 + d2**2 - d3**2)/(2 * d1 * d2) + 1/3)**2
                qtet[idx] = 1 - (3/8)*qtet[idx]
        nbr_list = np.array(nbr_list).astype(int)
        q4m = boo.bonds2qlm(coord, nbr_list, l=4, periods=lattice)
        Q4m , _ = boo.coarsegrain_qlm(q4m, nbr_list, np.array([True for i in range(coord.shape[0])]))
        Q4_m = boo.ql(Q4m)
        q6m =  boo.bonds2qlm(coord, nbr_list, l=6, periods=lattice)
        Q6m , _ = boo.coarsegrain_qlm(q6m, nbr_list, np.array([True for i in range(coord.shape[0])]))
        Q6_m = boo.ql(Q6m)
        q8m =  boo.bonds2qlm(coord, nbr_list, l=8, periods=lattice)
        Q8m , _ = boo.coarsegrain_qlm(q8m, nbr_list, np.array([True for i in range(coord.shape[0])]))
        Q8_m = boo.ql(Q8m)
        q10m =  boo.bonds2qlm(coord, nbr_list, l=10, periods=lattice)
        Q10m , _ = boo.coarsegrain_qlm(q10m, nbr_list, np.array([True for i in range(coord.shape[0])]))
        Q10_m = boo.ql(Q10m)
        q12m =  boo.bonds2qlm(coord, nbr_list, l=12, periods=lattice)
        Q12m , _ = boo.coarsegrain_qlm(q12m, nbr_list, np.array([True for i in range(coord.shape[0])]))
        Q12_m = boo.ql(Q12m)

        Qall_dict['frame_'+str(cnt_frame)+'_q4'] = Q4_m
        Qall_dict['frame_'+str(cnt_frame)+'_q6'] = Q6_m
        Qall_dict['frame_'+str(cnt_frame)+'_q8'] = Q8_m
        Qall_dict['frame_'+str(cnt_frame)+'_q10'] = Q10_m
        Qall_dict['frame_'+str(cnt_frame)+'_q12'] = Q12_m
        Qall_dict['frame_'+str(cnt_frame)+'_lsi'] = lsis
        Qall_dict['frame_'+str(cnt_frame)+'_qtet'] = qtet
        cnt_frame += 1

    Qall_dict["n_frames"] = cnt_frame
    Qall_dict["n_atoms"] = coord.shape[0]
    print("Saving file: ", args.npz_file)
    np.savez_compressed(args.npz_file, **Qall_dict)
    print("Successfully saved file: ", args.npz_file)
if __name__ == "__main__":
    main()


text:
# -*- coding: utf-8 -*-
"""
1D{1,1} datasets
----------------
"""
# %%
# In the following example, we illustrate how one can covert a Numpy array into
# a CSDM object. Start by importing the Numpy and csdmpy libraries.
import matplotlib.pyplot as plt
import numpy as np

import csdmpy as cp

# %%
# Let's generate two 1D NumPy arrays as the dependent variables of as our dataset.
test_data1 = np.zeros(500)
test_data1[250] = 1

test_data2 = np.zeros(500)
test_data2[150] = 1

# %%
# Create the two DependentVariable objects from the numpy objects.
dv1 = cp.as_dependent_variable(test_data1, unit="%")
dv2 = cp.as_dependent_variable(test_data2, unit="J")

# %%
# Create the corresponding dimension object. Here, we create a LinearDimension object.
dim = cp.LinearDimension(count=500, increment="43 cm", coordinates_offset="-0.1 km")

# %%
# Creating the CSDM object.
csdm_object = cp.CSDM(dependent_variables=[dv1, dv2], dimensions=[dim])

# %%
# Plot of the dataset.
plt.figure(figsize=(5, 3.5))
ax = plt.gca(projection="csdm")
ax.plot(csdm_object)
plt.tight_layout()
plt.show()

# %%
# To serialize the file, use the save method.
csdm_object.save("1D_11_dataset.csdf")

max_stars_repo_name: DeepanshS/csdmpy

max_issues_repo_name: deepanshs/csdmpy

max_forks_repo_name: deepanshs/csdmpy


text:
# Adapted for numpy/ma/cdms2 by convertcdms.py
import MV2
import numpy
import cdms2
import genutil

def get_parenthesis_content(code):
    opened = 0
    closed = 0
    for i, s in enumerate(code):
        if s == "(":
            opened += 1
        elif s == ")":
            closed += 1
        if opened != 0 and closed == opened:
            return code[code.find("(") + 1:i]
    return ""

def make_var(lap, id=None, shape=None):
    lap = MV2.array(lap)
    if shape is not None:
        lap = MV2.reshape(lap, shape)
    if id is not None:
        lap.id = id
    return lap

# TODO: need an actual 'vars.txt' file for the doctests
def readAscii(text_file, header=0, ids=None, shape=None,
              next='------', separators=[';', ',', ':']):
    """
    Reads data from an ascii file to generate a list of transient(s)/varable(s)

    :Example:

        .. doctest:: genutil_ASCII_readascii

            >>> vars=genutil.ASCII.readAscii("vars.txt") # use default params

    :param text_file: A string, containing the path to an ASCII File to read from.
    :type text_file: str

    :param header: Number of header lines, these lines will be skipped.
    :type header: int

    :param ids: List of values to use as variable ids (1 per variable returned)
    :type ids: list

    :param shape: use the tuple/list in this list as the final shape of the variable read.
    :type shape: tuple or list

    :param next: character string marking separation between variables (i.e. '------')
    :type next: str

    :param separators: ([';',',', ':']) List of characters recognized as column
        separators. Can be represented as a list or a string.
        If it is a string, separator characters must be space-delimited.
    :type separators: list or str

    :returns: List containing transient(s) variable(s) possibly named after ids and reshaped from the 'shape' option.
    :rtype: list
    """
    sep = []
    if isinstance(separators, str):
        separators = separators.split()
    for s in separators:
        sep.append(s)

    f = open(text_file)
    lst = f.readlines()
    f.close()
    if not isinstance(ids, (tuple, list)):
        ids = [ids]
    vars = []
    lap = []
    for ln in lst[header:]:
        for s in sep:
            ln = ln.replace(s, ' ')
        for s in ln.split():
            if s == next:
                if len(vars) > len(ids) - 1:
                    Id = None
                else:
                    Id = ids[len(vars)]
                if shape is None or len(vars) > len(shape):
                    sh = None
                else:
                    sh = shape[len(vars)]
                if lap != []:
                    vars.append(make_var(lap, shape=sh, id=Id))
                    lap = []
            else:
                if s != '':
                    lap.append(float(s))
    if len(vars) > len(ids) - 1:
        Id = None
    else:
        Id = ids[len(vars)]
    if lap != []:
        vars.append(make_var(lap, shape=shape, id=Id))
    if len(vars) > 1:
        return vars
    else:
        return vars[0]

def read_col(text_file, header=0, cskip=0, cskip_type='columns', axis=False, ids=None, idrow=0,
             separators=[';', ',', ':']):
    """
    Reads column-stored data from ASCII files

    :Example:

        .. doctest:: genutil_ASCII_read_col

            >>> vars = genutil.ASCII.read_col("vars.txt") # use default params

    :param text_file: ASCII File to read from.
    :type text_file:

    :param header: Number of header lines, these lines will be skipped.
    :type header: int

    :param cskip: Number of 'column'/'character' to skip (dummy column)
    :type cskip: int

    :param cskip_type: One of 'columns' or 'characters'. Specifies which should be skipped.
    :type cskip_type: str

    :param axis: Boolean flag indicating whether to use as the values for the first column as
        variable axis (x values in y(x)).
    :type axis: bool

    :param idrow: Is the first row representing the ids of var generated.
    :type idrow:

    :param ids: (None) use the values in this list as variable ids (1 per column returned)
    :type ids:

    :param separators: ([';',',', ':']) List of characters recognized as column
        separator. Can be represented as a list or a string.
        If it is a string, separator characters must be space-delimited.
    :type separators: list or str

    :returns: List containing 1 transient variable per column in the files.
        Variable ids are optionally determined by first row.
        Variable axis may be the first column.
    :rtype: list
    """

    sep = []
    if isinstance(separators, str):
        separators = separators.split()
    for s in separators:
        sep.append(s)

    f = open(text_file)
    lst = f.readlines()
    f.close()
    lst = lst[header:]
    if not isinstance(ids, (tuple, list)):
        ids = [ids]
    vars = None
    for ln in lst:
        if cskip_type == 'characters':
            ln = ln[cskip:]
        for s in sep:
            ln = ln.replace(s, ' ')
        sp = ln.split()
        if cskip_type == 'columns':
            sp = sp[cskip:]
        if vars is None:
            nvars = len(sp)
            vars = []
            for i in range(nvars):
                vars.append([])
        if idrow:
            ids = sp
            idrow = 0
        else:
            for i in range(nvars):
                vars[i].append(float(sp[i]))
    for i in range(nvars):
        vars[i] = MV2.array(vars[i])
        if ids != [None]:
            vars[i].id = ids[i]
    if axis:
        id = vars[0].id
        axval = vars.pop(0).filled()
        axindices = numpy.argsort(axval)
        ax = cdms2.createAxis(numpy.sort(axval))
        ax.id = id
        for i in range(nvars - 1):
            tmp = MV2.array(genutil.arrayindexing.get(vars[i], axindices))
            tmp.id = vars[i].id
            tmp.setAxis(0, ax)
            vars[i] = tmp
    if len(vars) > 1:
        return vars
    else:
        return vars[0]

readAsciiCols = read_col

max_stars_repo_name: CDAT/genutil

max_issues_repo_name: CDAT/genutil

max_forks_repo_name: CDAT/genutil


text:
import numpy as np

from typing import List, Union, Optional

from dover_lap.libs.turn import Turn
from dover_lap.src.label_mapping import LabelMapping
from dover_lap.src.label_voting import LabelVoting

class DOVERLap:
    @classmethod
    def combine_turns_list(
        cls,
        turns_list: List[List[Turn]],
        file_id: str,
        label_mapping: Optional[str] = "greedy",
        sort_first: Optional[bool] = False,
        second_maximal: Optional[bool] = False,
        tie_breaking: Optional[str] = "uniform",
        weight_type: Optional[str] = "rank",
        dover_weight: Optional[float] = 0.1,
        custom_weight: Optional[List[str]] = None,
    ) -> List[List[Turn]]:

        # Label mapping stage
        mapped_turns_list, weights = LabelMapping.get_mapped_turns_list(
            turns_list,
            file_id,
            method=label_mapping,
            second_maximal=second_maximal,
            sort_first=sort_first,
        )

        # Compute weights based on rank
        if weight_type == "rank":
            ranks = cls.__get_ranks(weights)
            weights = cls.__compute_weights(ranks, dover_weight)
        elif weight_type == "custom":
            assert isinstance(custom_weight, list)
            weights = np.array([float(x) for x in custom_weight])
        elif weight_type == "norm":
            weights /= np.linalg.norm(weights, ord=1)  # use normalized weights

        # Label voting stage
        combined_turns_list = LabelVoting.get_combined_turns(
            mapped_turns_list, file_id, tie_breaking, weights
        )
        return combined_turns_list

    def __get_ranks(weights: np.array) -> np.array:

        weights /= np.linalg.norm(weights, ord=1)
        temp = weights.argsort()
        ranks = np.empty_like(temp)
        ranks[temp] = np.arange(len(weights)) + 1
        return ranks

    def __compute_weights(ranks: np.array, weight: float) -> np.array:

        out_weights = 1 / np.power(ranks, weight)
        out_weights /= np.linalg.norm(out_weights, ord=1)
        return out_weights

max_stars_repo_name: damian-romero/dover-lap

max_issues_repo_name: damian-romero/dover-lap

max_forks_repo_name: damian-romero/dover-lap


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

import os
import cPickle
from ConfigParser import ConfigParser

import numpy as np

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from pylab import Figure

from CoolProp.HumidAirProp import HAProps, HAProps_Aux

from PyQt4.QtGui import (QDialog, QGridLayout, QProgressBar, QLabel,
                         QDialogButtonBox, QPushButton, QFileDialog, QApplication)

Preferences = ConfigParser()
config_path = os.path.join(os.path.dirname(__file__), "psyrc")
Preferences.read(config_path)
P = Preferences.getfloat("General", "P")

def _Pbar(Z):
    """
    ASHRAE Fundamentals Handbook pag 1.1 eq. 3
    input:
        Z: altitude, m
    return
        standard atmosphere barometric pressure, Pa
    """
    return 101325. * (1 - 2.25577e-5 * Z)**5.256

class PsychroPlot(FigureCanvasQTAgg):
    """
    Plot widget for psychrometric chart
        Add custom margins
        Define a pointer to text state properties, to remove and redraw
    """

    def __init__(self, parent=None, width=15, height=5, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvasQTAgg.__init__(self, self.fig)
        self.setParent(parent)
        self.axes2D = self.fig.add_subplot(111)
        FigureCanvasQTAgg.updateGeometry(self)
        self.axes2D.figure.subplots_adjust(left=0.01, right=0.92,
                                           bottom=0.05, top=0.98)
        self.notes = []

    def plot(self, *args, **kwargs):
        self.axes2D.plot(*args, **kwargs)

    def config(self):
        self.axes2D.set_autoscale_on(False)
        self.axes2D.set_xlabel(u"Tdb, ºC")
        self.axes2D.set_ylabel("Absolute humidity, kg/kg")
        self.axes2D.yaxis.set_ticks_position("right")
        self.axes2D.yaxis.set_label_position("right")

        tmin = Preferences.getfloat("Psychr", "isotdbStart") - 273.15
        tmax = Preferences.getfloat("Psychr", "isotdbEnd") - 273.15

        self.axes2D.set_xlim(tmin, tmax)
        self.axes2D.set_ylim(0, 0.04)

    def showPointData(self, state):
        self.clearPointData()

        yi = 0.99
        keys = "tdb", "tdp", "twb", "HR", "w", "h", "v", "rho"
        units = u"ºC", u"ºC", u"ºC", "%", "kgw/kgda", "kJ/kg", u"m³/kg", u"kg/m³"
        for key, txt in zip(keys, units):
            self.notes.append(self.axes2D.annotate(
                "%s: %0.4f %s" % (key, state.__getattribute__(key), txt), (0.01, yi),
                xycoords='axes fraction', size="small", va="center"))
            yi -= 0.025
        self.draw()

    def clearPointData(self):
        while self.notes:
            anotation = self.notes.pop()
            anotation.remove()
        self.draw()

class PsyCoolprop(object):
    """
    Psychrometric state using coolprop library

    kwargs definition parameters:
        P: Pressure, Pa
        z: altitude, m

        tdp: dew-point temperature
        tdb: dry-bulb temperature
        twb: web-bulb temperature
        w: Humidity Ratio [kg water/kg dry air]
        HR: Relative humidity
        h: Mixture enthalpy
        v: Mixture specified volume

    P: mandatory input for barometric pressure, z is an alternate pressure input
    it needs other two input parameters:
        0 - tdb, w
        1 - tdb, HR
        2 - tdb, twb
        3 - tdb, tdp
        4 - tdp, HR
        5 - tdp, twb
        6 - twb, w
    """
    kwargs = {"z": 0.0,
              "P": 0.0,

              "tdb": 0.0,
              "tdb": 0.0,
              "twb": 0.0,
              "w": None,
              "HR": None,
              "h": None,
              "v": 0.0}
    status = 0
    msg = "Unknown variables"

    def __init__(self, **kwargs):
        self.kwargs = self.__class__.kwargs.copy()
        self.__call__(**kwargs)

    def __call__(self, **kwargs):
        self.kwargs.update(kwargs)

        if self.calculable:
            self.status = 1
            self.calculo()
            self.msg = "Solved"

    @property
    def calculable(self):
        tdp = self.kwargs.get("tdp", 0)
        tdb = self.kwargs.get("tdb", 0)
        twb = self.kwargs.get("twb", 0)
        w = self.kwargs.get("w", None)
        HR = self.kwargs.get("HR", None)
        h = self.kwargs.get("h", None)
        v = self.kwargs.get("v", 0)

        self._mode = 0
        if tdb and w is not None:
            self._mode = ("Tdb", "W")
        elif tdb and HR is not None:
            self._mode = ("Tdb", "RH")
        elif tdb and twb:
            self._mode = ("Tdb", "Twb")
        elif tdb and tdp:
            self._mode = ("Tdb", "Tdp")
        elif tdp and HR is not None:
            self._mode = ("Tdp", "RH")

        return bool(self._mode)

    def calculo(self):
        tdp, tdb, twb, P, Pvs, Pv, ws, w, HR, v, h = self._lib()
        self.tdp = tdp - 273.15
        self.tdb = tdb - 273.15
        self.twb = twb - 273.15
        self.P = P
        self.Pvs = Pvs
        self.Pv = Pv
        self.ws = ws
        self.w = w
        self.HR = HR
        self.mu = w / ws * 100
        self.v = v
        self.rho = 1 / v
        self.h = h
        self.Xa = 1 / (1 + self.w / 0.62198)
        self.Xw = 1 - self.Xa

    def args(self):
        # Correct coolprop custom namespace versus pychemqt namespace
        if "Tdb" in self._mode:
            self.kwargs["Tdb"] = self.kwargs["tdb"]
        if "Twb" in self._mode:
            self.kwargs["Twb"] = self.kwargs["twb"]
        if "Tdp" in self._mode:
            self.kwargs["Tdp"] = self.kwargs["tdp"]
        if "RH" in self._mode:
            self.kwargs["RH"] = self.kwargs["HR"]
        if "W" in self._mode:
            self.kwargs["W"] = self.kwargs["w"]

        var1 = self.kwargs[self._mode[0]]
        var2 = self.kwargs[self._mode[1]]

        # units conversion to coolprop expected unit:
        # HR in 0-1, H in kJ/kg, S in kJ/kgK
        if "RH" in self._mode[0]:
            var1 /= 100.
        if "RH" in self._mode[1]:
            var2 /= 100.

        args = ("P", self._P_kPa, self._mode[0], var1, self._mode[1], var2)
        return args

    def _P(self):
        """Barometric pressure calculation, Pa"""
        if self.kwargs["P"]:
            P = self.kwargs["P"]
        elif self.kwargs["z"]:
            P = _Pbar(self.kwargs["z"])
        else:
            P = 101325.
        return P

    @property
    def _P_kPa(self):
        """Property for ease access to pressure in kPa"""
        P = self._P()
        return P / 1000.

    def _lib(self):
        args = self.args()
        P = self._P()

        if "Tdb" in self._mode:
            tdb = self.kwargs["Tdb"]
        else:
            tdb = HAProps("Tdb", *args)
        tdp = HAProps("Tdp", *args)
        twb = HAProps("Twb", *args)
        w = HAProps("W", *args)
        HR = HAProps("RH", *args) * 100
        Pvs = HAProps_Aux("p_ws", tdb, self._P_kPa, w)[0] * 1000
        Pv = Pvs * HR / 100
        ws = HAProps("W", "P", self._P_kPa, "Tdb", tdb, "RH", 1)
        v = HAProps("V", *args)
        h = HAProps("H", *args)

        return tdp, tdb, twb, P, Pvs, Pv, ws, w, HR, v, h

    @classmethod
    def calculatePlot(cls, parent):
        """Function to calculate points in chart"""

        data = {}
        P = Preferences.getfloat("General", "P")
        P_kPa = P / 1000
        t = cls.LineList("isotdb", Preferences)

        # Saturation line
        Hs = []
        for tdb in t:
            Hs.append(HAProps("W", "P", P_kPa, "Tdb", tdb, "RH", 1))
            parent.progressBar.setValue(5 * len(Hs) / len(t))
        data["t"] = t
        data["Hs"] = Hs

        # left limit of isow lines
        H = cls.LineList("isow", Preferences)
        th = []
        for w in H:
            if w:
                tdp = HAProps("Tdp", "P", 101.325, "W", w, "RH", 1)
                th.append(tdp - 273.15)
            else:
                tmin = Preferences.getfloat("Psychr", "isotdbStart")
                th.append(tmin - 273.15)
        data["H"] = H
        data["th"] = th

        # Humidity ratio lines
        hr = cls.LineList("isohr", Preferences)
        Hr = {}
        cont = 0
        for i in hr:
            Hr[i] = []
            for tdb in t:
                Hr[i].append(HAProps("W", "P", P_kPa, "Tdb", tdb, "RH", i / 100.))
                cont += 1
                parent.progressBar.setValue(5 + 10 * cont / len(hr) / len(Hs))
        data["Hr"] = Hr

        # Twb
        lines = cls.LineList("isotwb", Preferences)
        Twb = {}
        cont = 0
        for T in lines:
            ws = HAProps("W", "P", P_kPa, "RH", 1, "Tdb", T)
            H = [ws, 0]
            Tw = [T - 273.15, HAProps("Tdb", "P", P_kPa, "Twb", T, "RH", 0) - 273.15]
            cont += 1
            parent.progressBar.setValue(15 + 75 * cont / len(lines))
            Twb[T] = (H, Tw)
        data["Twb"] = Twb

        # v
        lines = cls.LineList("isochor", Preferences)
        V = {}
        rh = np.arange(1, -0.05, -0.05)
        for cont, v in enumerate(lines):
            w = []
            Td = []
            for r in rh:
                w.append(HAProps("W", "P", P_kPa, "RH", r, "V", v))
                Td.append(HAProps("Tdb", "P", P_kPa, "RH", r, "V", v) - 273.15)
            parent.progressBar.setValue(90 + 10 * cont / len(lines))
            V[v] = (Td, w)
        data["v"] = V

        return data

    @staticmethod
    def LineList(name, Preferences):
        """Return a list with the values of isoline name to plot"""
        if Preferences.getboolean("Psychr", name + "Custom"):
            t = []
            for i in Preferences.get("Psychr", name + 'List').split(','):
                if i:
                    t.append(float(i))
        else:
            start = Preferences.getfloat("Psychr", name + "Start")
            end = Preferences.getfloat("Psychr", name + "End")
            step = Preferences.getfloat("Psychr", name + "Step")
            t = np.arange(start, end, step)
        return t

class UI_Psychrometry(QDialog):
    """Psychrometric charts tool"""

    def __init__(self, parent=None):
        super(UI_Psychrometry, self).__init__(parent)
        self.showMaximized()
        self.setWindowTitle("Psychrometric chart")

        layout = QGridLayout(self)
        self.diagrama2D = PsychroPlot(self, dpi=90)
        self.diagrama2D.fig.canvas.mpl_connect('motion_notify_event', self.scroll)
        layout.addWidget(self.diagrama2D, 1, 1, 1, 2)
        self.progressBar = QProgressBar()
        self.progressBar.setVisible(False)
        layout.addWidget(self.progressBar, 2, 1)
        self.status = QLabel()
        layout.addWidget(self.status, 2, 1)

        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Close)
        butonPNG = QPushButton("Save as PNG")
        self.buttonBox.addButton(butonPNG, QDialogButtonBox.AcceptRole)
        self.buttonBox.rejected.connect(self.reject)
        self.buttonBox.accepted.connect(self.savePNG)
        layout.addWidget(self.buttonBox, 2, 2)

        self.plot()

    def savePNG(self):
        """Save chart image to png file"""
        fname = unicode(QFileDialog.getSaveFileName(
            self, "Save chart to file",
            "./", "Portable Network Graphics (*.png)"))
        self.diagrama2D.fig.savefig(fname, facecolor='#eeeeee')

    def drawlabel(self, name, Preferences, t, W, label, unit):
        """
        Draw annotation for isolines
            name: name of isoline
            Preferences: Configparse instance of pychemqt preferences
            t: x array of line
            W: y array of line
            label: text value to draw
            unit: text units to draw
        """
        if Preferences.getboolean("Psychr", name + "label"):
            tmin = Preferences.getfloat("Psychr", "isotdbStart") - 273.15
            tmax = Preferences.getfloat("Psychr", "isotdbEnd") - 273.15
            x = tmax - tmin
            wmin = Preferences.getfloat("Psychr", "isowStart")
            wmax = Preferences.getfloat("Psychr", "isowEnd")
            y = wmax - wmin

            i = 0
            for ti, wi in zip(t, W):
                if tmin <= ti <= tmax and wmin <= wi <= wmax:
                    i += 1
            label = str(label)
            if Preferences.getboolean("Psychr", name + "units"):
                label += unit
            pos = Preferences.getfloat("Psychr", name + "position")
            p = int(i * pos / 100 - 1)
            rot = np.arctan((W[p] - W[p - 1]) / y / (t[p] - t[p - 1]) * x) * 360.0 / 2.0 / np.pi
            self.diagrama2D.axes2D.annotate(label, (t[p], W[p]),
                rotation=rot, size="small", ha="center", va="center")

    def plot(self):
        """Plot chart"""
        Preferences = ConfigParser()
        Preferences.read("psyrc")

        self.diagrama2D.axes2D.clear()
        self.diagrama2D.config()
        filename = "%i.pkl" % P
        if os.path.isfile(filename):
            with open(filename, "r") as archivo:
                data = cPickle.load(archivo)
                self.status.setText("Loading cached data...")
                QApplication.processEvents()
        else:
            self.progressBar.setVisible(True)
            self.status.setText("Calculating data, be patient...")
            QApplication.processEvents()
            data = PsyCoolprop.calculatePlot(self)
            cPickle.dump(data, open(filename, "w"))
            self.progressBar.setVisible(False)
        self.status.setText("Plotting...")
        QApplication.processEvents()

        tmax = Preferences.getfloat("Psychr", "isotdbEnd") - 273.15

        t = [ti - 273.15 for ti in data["t"]]
        Hs = data["Hs"]
        format = {}
        format["ls"] = Preferences.get("Psychr", "saturationlineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "saturationlineWidth")
        format["color"] = Preferences.get("Psychr", "saturationColor")
        format["marker"] = Preferences.get("Psychr", "saturationmarker")
        format["markersize"] = 3
        self.diagrama2D.plot(t, Hs, **format)

        format = {}
        format["ls"] = Preferences.get("Psychr", "isotdblineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "isotdblineWidth")
        format["color"] = Preferences.get("Psychr", "isotdbColor")
        format["marker"] = Preferences.get("Psychr", "isotdbmarker")
        format["markersize"] = 3
        for i, T in enumerate(t):
            self.diagrama2D.plot([T, T], [0, Hs[i]], **format)

        H = data["H"]
        th = data["th"]
        format = {}
        format["ls"] = Preferences.get("Psychr", "isowlineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "isowlineWidth")
        format["color"] = Preferences.get("Psychr", "isowColor")
        format["marker"] = Preferences.get("Psychr", "isowmarker")
        format["markersize"] = 3
        for i, H in enumerate(H):
            self.diagrama2D.plot([th[i], tmax], [H, H], **format)

        format = {}
        format["ls"] = Preferences.get("Psychr", "isohrlineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "isohrlineWidth")
        format["color"] = Preferences.get("Psychr", "isohrColor")
        format["marker"] = Preferences.get("Psychr", "isohrmarker")
        format["markersize"] = 3
        for Hr, H0 in data["Hr"].iteritems():
            self.diagrama2D.plot(t, H0, **format)
            self.drawlabel("isohr", Preferences, t, H0, Hr, "%")

        format = {}
        format["ls"] = Preferences.get("Psychr", "isotwblineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "isotwblineWidth")
        format["color"] = Preferences.get("Psychr", "isotwbColor")
        format["marker"] = Preferences.get("Psychr", "isotwbmarker")
        format["markersize"] = 3
        for T, (H, Tw) in data["Twb"].iteritems():
            self.diagrama2D.plot(Tw, H, **format)
            value = T - 273.15
            txt = u"ºC"
            self.drawlabel("isotwb", Preferences, Tw, H, value, txt)

        format = {}
        format["ls"] = Preferences.get("Psychr", "isochorlineStyle")
        format["lw"] = Preferences.getfloat("Psychr", "isochorlineWidth")
        format["color"] = Preferences.get("Psychr", "isochorColor")
        format["marker"] = Preferences.get("Psychr", "isochormarker")
        format["markersize"] = 3
        for v, (Td, H) in data["v"].iteritems():
            self.diagrama2D.plot(Td, H, **format)
            value = v
            txt = u"m³/kg"
            self.drawlabel("isochor", Preferences, Td, H, value, txt)

        self.diagrama2D.draw()
        self.status.setText("P = %i Pa" % P)

    def scroll(self, event):
        """Update graph annotate when mouse scroll over chart"""
        if event.xdata and event.ydata:
            punto = self.createState(event.xdata, event.ydata)
            if event.ydata < punto.ws:
                self.diagrama2D.showPointData(punto)
            else:
                self.diagrama2D.clearPointData()

    def createState(self, x, y):
        """Create psychrometric state from click or mouse position"""
        tdb = x + 273.15
        punto = PsyCoolprop(P=P, tdb=tdb, w=y)
        return punto

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    aireHumedo = UI_Psychrometry()
    aireHumedo.show()
    sys.exit(app.exec_())

max_stars_repo_name: pauliacomi/CoolProp

max_issues_repo_name: pauliacomi/CoolProp

max_forks_repo_name: pauliacomi/CoolProp


text:
import numpy as np
from numpy.testing import assert_equal, assert_raises

from src.tools.lists import find
from src.trace_set.database import Database
from src.trace_set.set_hw import TraceSetHW

def window_cxt(trace_cxt, sample_trace, window_margin: int = None):
    start_ix = find(trace_cxt[0], sample_trace)

    win_size = len(sample_trace)
    window = np.array((start_ix, start_ix + win_size))

    if window_margin is None:
        window_margin = win_size

    # Window with context
    cxt = window + np.array((-window_margin, window_margin))

    # Return the window relative to the window context.
    rel_window = window - cxt[0]

    return rel_window, cxt

if __name__ == '__main__':
    ts = TraceSetHW(Database.aisy)
    trs_cxt, _ = ts.profile()

    win = (1000, 2000)
    trs = trs_cxt[0][win[0]:win[1]]
    rel_win, _ = window_cxt(trs_cxt, trs)

    assert_equal(win, rel_win)

    trs2 = trs_cxt[1][win[0]:win[1]]
    with assert_raises(IndexError):
        window_cxt(trs_cxt, trs2)

max_stars_repo_name: ccberg/LA

max_issues_repo_name: ccberg/LA

max_forks_repo_name: ccberg/LA

reason: import numpy,from numpy


text:
from collections import OrderedDict

import numpy as np
import torch
import torch.optim as optim

import rlkit.torch.pytorch_util as ptu
from rlkit.core.eval_util import create_stats_ordered_dict
from rlkit.torch.torch_rl_algorithm import TorchTrainer
import wandb
import os

class BCTrainer(TorchTrainer):
    def __init__(
            self,
            env,
            policy,
            policy_lr=1e-3,
            optimizer_class=optim.Adam,
            log_dir=None,
            wand_b=True,
            variant_dict=None,
            real_data=False,
            log_pickle=True,
            pickle_log_rate=5,
            imgstate = False,
            validation=False,
            validation_buffer=None,
            bc_cql_comp=False,
            *args, **kwargs
    ):
        super().__init__()
        self.env = env
        self.imgstate = imgstate
        self.policy = policy
        self.log_dir = log_dir

        self.log_pickle=log_pickle
        self.pickle_log_rate=pickle_log_rate

        self.policy_optimizer = optimizer_class(
            self.policy.parameters(),
            lr=policy_lr,
        )

        self._optimizer_class = optimizer_class #for loading

        self.eval_statistics = OrderedDict()
        self._n_train_steps_total = 0
        self._need_to_update_eval_statistics = True

        self._current_epoch = 0
        self._log_epoch = 0

        self._num_policy_update_steps = 0
        self.discrete = False

        self.validation = validation
        self.validation_buffer = validation_buffer

        self.real_data = real_data
        self.log_dir = log_dir
        self.wand_b = wand_b

        if self.wand_b:
            wandb.init(project='bc_cql_comp' if bc_cql_comp else 'cog_cql', reinit=True)
            wandb.run.name=log_dir.split('/')[-1]
            if variant_dict is not None:
                wandb.config.update(variant_dict)

    def train_from_torch(self, batch, online=False):
        self._current_epoch += 1

        if self.imgstate:
            obs = batch['observations']
            state = batch['state']
        else:
            obs = batch['observations']
        actions = batch['actions']
        """
        Policy and Alpha Loss
        """
        """Start with BC"""
        new_obs_actions, policy_mean, policy_log_std, log_pi, *_ = self.policy(
            obs, reparameterize=True, return_log_prob=True, extra_fc_input = state if self.imgstate else None,
        )
        alpha = 0.0
        policy_log_prob = self.policy.log_prob(obs, actions, extra_fc_input = state if self.imgstate else None)
        policy_loss = (alpha * log_pi - policy_log_prob).mean()
        """
        Update networks
        """
        self._num_policy_update_steps += 1
        self.policy_optimizer.zero_grad()
        policy_loss.backward(retain_graph=False)
        self.policy_optimizer.step()

        """
        Save some statistics for eval
        """
        if self._need_to_update_eval_statistics:
            if self.log_pickle and self._log_epoch % self.pickle_log_rate == 0:
                new_path = os.path.join(self.log_dir,'model_pkl')
                if not os.path.isdir(new_path):
                    os.mkdir(new_path)
                torch.save({
                    'policy_state_dict': self.policy.state_dict(),
                }, os.path.join(new_path, str(self._log_epoch)+'.pt'))

            if self.validation:
                num_val = 32
                batch_val = self.validation_buffer.random_batch(num_val)
                if self.imgstate:
                    state_val = ptu.from_numpy(batch_val['state'])
                rewards_val = ptu.from_numpy(batch_val['rewards'])
                terminals_val = ptu.from_numpy(batch_val['terminals'])
                obs_val = ptu.from_numpy(batch_val['observations'])
                actions_val = ptu.from_numpy(batch_val['actions'])
                next_obs_val = ptu.from_numpy(batch_val['next_observations'])
                val_new_obs_actions, val_policy_mean, val_policy_log_std, val_log_pi, *_ = self.policy(
                    obs_val, reparameterize=True, return_log_prob=True, extra_fc_input=state if self.imgstate else None,
                )
                val_policy_log_prob = self.policy.log_prob(obs_val, actions_val, extra_fc_input=state if self.imgstate else None)
                val_policy_loss = (alpha * val_log_pi - val_policy_log_prob).mean()
                self.eval_statistics['Val Policy Loss'] = np.mean(ptu.get_numpy(
                    val_policy_loss
                ))
                self.eval_statistics.update(create_stats_ordered_dict(
                    'Val Log Pis',
                    ptu.get_numpy(val_policy_log_prob),
                ))
                self.eval_statistics.update(create_stats_ordered_dict(
                    'Val Policy mu',
                    ptu.get_numpy(val_policy_mean),
                ))
                self.eval_statistics.update(create_stats_ordered_dict(
                    'Val Policy log std',
                    ptu.get_numpy(val_policy_log_std),
                ))

            self._need_to_update_eval_statistics = False
            """
            Eval should set this to None.
            This way, these statistics are only computed for one batch.
            """
            self.eval_statistics[
                'Num Policy Updates'] = self._num_policy_update_steps
            self.eval_statistics['Policy Loss'] = np.mean(ptu.get_numpy(
                policy_loss
            ))
            self.eval_statistics.update(create_stats_ordered_dict(
                'Log Pis',
                ptu.get_numpy(log_pi),
            ))
            self.eval_statistics.update(create_stats_ordered_dict(
                'Policy mu',
                ptu.get_numpy(policy_mean),
            ))
            self.eval_statistics.update(create_stats_ordered_dict(
                'Policy log std',
                ptu.get_numpy(policy_log_std),
            ))
            if self.wand_b:
                wandb.log({'trainer/'+k:v for k,v in self.eval_statistics.items()}, step=self._log_epoch)
            self._log_epoch += 1
        self._n_train_steps_total += 1

    def get_diagnostics(self):
        return self.eval_statistics

    def end_epoch(self, epoch):
        self._need_to_update_eval_statistics = True

    @property
    def networks(self):
        base_list = [self.policy]
        return base_list


text:
# script adopted from https://gist.github.com/lampts/026a4d6400b1efac9a13a3296f16e655

import gensim
import numpy as np
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector

words = ('Sacramento', 'California', 'Oregon', 'Salem', 'Washington', 'Olympia')

# loading your gensim
model = gensim.models.KeyedVectors.load_word2vec_format('~/Downloads/GoogleNews-vectors-negative300.bin.gz', binary=True, limit=200000)

# project part of vocab, 10K of 300 dimension
w2v_10K = np.zeros((6, 300))
with open("/Users/hannes/Downloads/prefix_metadata.tsv", 'w+') as file_metadata:
    # for i, word in enumerate(model.index2word[:200000]):
    #     w2v_10K[i] = model[word]
    #     file_metadata.write(word.encode('utf-8') + '\n')
    for i, word in enumerate(list(words)):
        w2v_10K[i] = model[word]
        file_metadata.write(word.encode('utf-8') + '\n')

# define the model without training
sess = tf.InteractiveSession()

with tf.device("/cpu:0"):
    embedding = tf.Variable(w2v_10K, trainable=False, name='word2vec_embedding')

tf.global_variables_initializer().run()

saver = tf.train.Saver()
writer = tf.summary.FileWriter('/Users/hannes/Downloads', sess.graph)

# adding into projector
config = projector.ProjectorConfig()
embed = config.embeddings.add()
embed.tensor_name = 'word2vec_embedding'
embed.metadata_path = '/Users/hannes/Downloads//prefix_metadata.tsv'

# Specify the width and height of a single thumbnail.
projector.visualize_embeddings(writer, config)

saver.save(sess, '/Users/hannes/Downloads/prefix_model.ckpt', global_step=1000)

# open tensorboard with logdir, check localhost:6006 for viewing your embedding.
# tensorboard --logdir="./projector/"

max_stars_repo_name: tonymullen/nlpia

max_issues_repo_name: tonymullen/nlpia

max_forks_repo_name: tonymullen/nlpia


text:
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2021/5/15 下午1:11
# @Author : PH
# @Version：V 0.1
# @File : nms.py
# @desc : 非极大值抑制
import numpy as np

def calc_ious(a, areas, x1, x2, y1, y2):
    area_a = np.abs(a[0] - a[2]) * np.abs(a[1] - a[3])
    # max_bbox 与其他bbox的所有交集的面积
    Ix1 = np.maximum(a[0], x1)
    Ix2 = np.minimum(a[2], x2)
    Iy1 = np.maximum(a[1], y1)
    Iy2 = np.minimum(a[3], y2)
    Iw = np.maximum(Ix2 - Ix1, 0)
    Ih = np.maximum(Iy2 - Iy1, 0)
    area_I = Iw * Ih
    area_U = area_a + areas - area_I
    return area_I * 1.0 / area_U

# 自己的实现，但是好像有小bug？？？
def nms(bbox, scores):
    bbox = np.array(bbox)
    scores = np.array(scores)
    sort_idx = np.argsort(-scores)  # 按照降序排序
    sort_bbox = bbox[sort_idx]
    sort_scores = scores[sort_idx]
    # 所有bbox的面积,calc_ious()会用到
    x1 = sort_bbox[:, 0]
    y1 = sort_bbox[:, 1]
    x2 = sort_bbox[:, 2]
    y2 = sort_bbox[:, 3]
    areas = (x2 - x1) * (y2 - y1)

    # 选择最终bbox
    select_bbox = []
    while len(sort_bbox) != 0:
        select_bbox.append((sort_bbox[0].tolist(), sort_scores[0]))  # 保存分数最大的bbox和分数
        # 对得分最大max_bbox计算其他other_bbox和它的iou，大于0.4的other_bbox全部从初始bbox array中删除（包括当前max_bbox）
        ious = calc_ious(sort_bbox[0], areas, x1, x2, y1, y2)
        # 删除和当前bbox的iou高于iou的其他bbox
        del_idx = np.where(ious > 0.4)  # 用where返回符合条件的idx
        sort_bbox = np.delete(sort_bbox, del_idx, axis=0)
        sort_scores = np.delete(sort_scores, del_idx)
        areas = np.delete(areas, del_idx)
        x1, x2 = np.delete(x1, del_idx), np.delete(x2, del_idx)
        y1, y2 = np.delete(y1, del_idx), np.delete(y2, del_idx)
    return select_bbox

# 网上别人的实现
def nms2(dets, score):
    dets = np.array(dets)
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    score = np.array(score)

    order = score.argsort()[::-1]
    area = (x2 - x1) * (y2 - y1)
    res = []
    while order.size >= 1:
        i = order[0]
        res.append([x1[i], y1[i], x2[i], y2[i], score[i]])

        # intersect area left top point(xx1, yy1): xx1 >= x1, yy1 >= y1
        # intersect area right down point(xx2, yy2): xx2 <= x2, yy2 <= y2

        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, (xx2 - xx1))
        h = np.maximum(0.0, (yy2 - yy1))
        intersect = w * h

        # iou = intersect area / union; union = box1 + box2 - intersect
        iou = intersect / (area[i] + area[order[1:]] - intersect)

        # update order index;ind +1:because ind is obtain by index [1:]
        ind = np.where(iou <= 0.4)[0]
        order = order[ind + 1]

    return res

max_stars_repo_name: phww/Study-Model-Myself

max_issues_repo_name: phww/Study-Model-Myself

max_forks_repo_name: phww/Study-Model-Myself