text:
import os
import random

import numpy as np
import torch
import torch.utils.data
from tqdm import tqdm

try:
    from lib import spec_utils
except ModuleNotFoundError:
    import spec_utils

class VocalRemoverTrainingSet(torch.utils.data.Dataset):

    def __init__(self, training_set, cropsize, reduction_rate, reduction_weight, mixup_rate, mixup_alpha):
        self.training_set = training_set
        self.cropsize = cropsize
        self.reduction_rate = reduction_rate
        self.reduction_weight = reduction_weight
        self.mixup_rate = mixup_rate
        self.mixup_alpha = mixup_alpha

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

    def do_crop(self, X_path, y_path):
        X_mmap = np.load(X_path, mmap_mode='r')
        y_mmap = np.load(y_path, mmap_mode='r')

        start = np.random.randint(0, X_mmap.shape[2] - self.cropsize)
        end = start + self.cropsize

        X_crop = np.array(X_mmap[:, :, start:end], copy=True)
        y_crop = np.array(y_mmap[:, :, start:end], copy=True)

        return X_crop, y_crop

    def do_aug(self, X, y):
        if np.random.uniform() < self.reduction_rate:
            y = spec_utils.aggressively_remove_vocal(X, y, self.reduction_weight)

        if np.random.uniform() < 0.5:
            # swap channel
            X = X[::-1].copy()
            y = y[::-1].copy()

        if np.random.uniform() < 0.01:
            # inst
            X = y.copy()

        # if np.random.uniform() < 0.01:
        #     # mono
        #     X[:] = X.mean(axis=0, keepdims=True)
        #     y[:] = y.mean(axis=0, keepdims=True)

        return X, y

    def do_mixup(self, X, y):
        idx = np.random.randint(0, len(self))
        X_path, y_path, coef = self.training_set[idx]

        X_i, y_i = self.do_crop(X_path, y_path)
        X_i /= coef
        y_i /= coef

        X_i, y_i = self.do_aug(X_i, y_i)

        lam = np.random.beta(self.mixup_alpha, self.mixup_alpha)
        X = lam * X + (1 - lam) * X_i
        y = lam * y + (1 - lam) * y_i

        return X, y

    def __getitem__(self, idx):
        X_path, y_path, coef = self.training_set[idx]

        X, y = self.do_crop(X_path, y_path)
        X /= coef
        y /= coef

        X, y = self.do_aug(X, y)

        if np.random.uniform() < self.mixup_rate:
            X, y = self.do_mixup(X, y)

        X_mag = np.abs(X)
        y_mag = np.abs(y)

        return X_mag, y_mag

class VocalRemoverValidationSet(torch.utils.data.Dataset):

    def __init__(self, patch_list):
        self.patch_list = patch_list

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

    def __getitem__(self, idx):
        path = self.patch_list[idx]
        data = np.load(path)

        X, y = data['X'], data['y']

        X_mag = np.abs(X)
        y_mag = np.abs(y)

        return X_mag, y_mag

def make_pair(mix_dir, inst_dir):
    input_exts = ['.wav', '.m4a', '.mp3', '.mp4', '.flac']

    X_list = sorted([
        os.path.join(mix_dir, fname)
        for fname in os.listdir(mix_dir)
        if os.path.splitext(fname)[1] in input_exts
    ])
    y_list = sorted([
        os.path.join(inst_dir, fname)
        for fname in os.listdir(inst_dir)
        if os.path.splitext(fname)[1] in input_exts
    ])

    filelist = list(zip(X_list, y_list))

    return filelist

def train_val_split(dataset_dir, split_mode, val_rate, val_filelist):
    if split_mode == 'random':
        filelist = make_pair(
            os.path.join(dataset_dir, 'mixtures'),
            os.path.join(dataset_dir, 'instruments')
        )

        random.shuffle(filelist)

        if len(val_filelist) == 0:
            val_size = int(len(filelist) * val_rate)
            train_filelist = filelist[:-val_size]
            val_filelist = filelist[-val_size:]
        else:
            train_filelist = [
                pair for pair in filelist
                if list(pair) not in val_filelist
            ]
    elif split_mode == 'subdirs':
        if len(val_filelist) != 0:
            raise ValueError('`val_filelist` option is not available with `subdirs` mode')

        train_filelist = make_pair(
            os.path.join(dataset_dir, 'training/mixtures'),
            os.path.join(dataset_dir, 'training/instruments')
        )

        val_filelist = make_pair(
            os.path.join(dataset_dir, 'validation/mixtures'),
            os.path.join(dataset_dir, 'validation/instruments')
        )

    return train_filelist, val_filelist

def make_padding(width, cropsize, offset):
    left = offset
    roi_size = cropsize - offset * 2
    if roi_size == 0:
        roi_size = cropsize
    right = roi_size - (width % roi_size) + left

    return left, right, roi_size

def make_training_set(filelist, sr, hop_length, n_fft):
    ret = []
    for X_path, y_path in tqdm(filelist):
        X, y, X_cache_path, y_cache_path = spec_utils.cache_or_load(
            X_path, y_path, sr, hop_length, n_fft
        )
        coef = np.max([np.abs(X).max(), np.abs(y).max()])
        ret.append([X_cache_path, y_cache_path, coef])

    return ret

def make_validation_set(filelist, cropsize, sr, hop_length, n_fft, offset):
    patch_list = []
    patch_dir = 'cs{}_sr{}_hl{}_nf{}_of{}'.format(cropsize, sr, hop_length, n_fft, offset)
    os.makedirs(patch_dir, exist_ok=True)

    for X_path, y_path in tqdm(filelist):
        basename = os.path.splitext(os.path.basename(X_path))[0]

        X, y, _, _ = spec_utils.cache_or_load(X_path, y_path, sr, hop_length, n_fft)
        coef = np.max([np.abs(X).max(), np.abs(y).max()])
        X, y = X / coef, y / coef

        l, r, roi_size = make_padding(X.shape[2], cropsize, offset)
        X_pad = np.pad(X, ((0, 0), (0, 0), (l, r)), mode='constant')
        y_pad = np.pad(y, ((0, 0), (0, 0), (l, r)), mode='constant')

        len_dataset = int(np.ceil(X.shape[2] / roi_size))
        for j in range(len_dataset):
            outpath = os.path.join(patch_dir, '{}_p{}.npz'.format(basename, j))
            start = j * roi_size
            if not os.path.exists(outpath):
                np.savez(
                    outpath,
                    X=X_pad[:, :, start:start + cropsize],
                    y=y_pad[:, :, start:start + cropsize]
                )
            patch_list.append(outpath)

    return patch_list

def get_oracle_data(X, y, oracle_loss, oracle_rate, oracle_drop_rate):
    k = int(len(X) * oracle_rate * (1 / (1 - oracle_drop_rate)))
    n = int(len(X) * oracle_rate)
    indices = np.argsort(oracle_loss)[::-1][:k]
    indices = np.random.choice(indices, n, replace=False)
    oracle_X = X[indices].copy()
    oracle_y = y[indices].copy()

    return oracle_X, oracle_y, indices

if __name__ == "__main__":
    import sys
    import utils

    mix_dir = sys.argv[1]
    inst_dir = sys.argv[2]
    outdir = sys.argv[3]

    os.makedirs(outdir, exist_ok=True)

    filelist = make_pair(mix_dir, inst_dir)
    for mix_path, inst_path in tqdm(filelist):
        mix_basename = os.path.splitext(os.path.basename(mix_path))[0]

        X_spec, y_spec, _, _ = spec_utils.cache_or_load(
            mix_path, inst_path, 44100, 1024, 2048
        )

        X_mag = np.abs(X_spec)
        y_mag = np.abs(y_spec)
        v_mag = X_mag - y_mag
        v_mag *= v_mag > y_mag

        outpath = '{}/{}_Vocal.jpg'.format(outdir, mix_basename)
        v_image = spec_utils.spectrogram_to_image(v_mag)
        utils.imwrite(outpath, v_image)

max_stars_repo_name: dviralfi/MashupMaker

max_issues_repo_name: dviralfi/MashupMaker

max_forks_repo_name: dviralfi/MashupMaker


text:
import math
import numpy as np

from .synchronous_motor import SynchronousMotor

class PermanentMagnetSynchronousMotor(SynchronousMotor):
    """
    =====================  ==========  ============= ===========================================
    Motor Parameter        Unit        Default Value Description
    =====================  ==========  ============= ===========================================
    r_s                    Ohm         0.78          Stator resistance
    l_d                    H           1.2           Direct axis inductance
    l_q                    H           6.3e-3        Quadrature axis inductance
    p                      1           2             Pole pair number
    j_rotor                kg/m^2      0.017         Moment of inertia of the rotor
    =====================  ==========  ============= ===========================================

    =============== ====== =============================================
    Motor Currents  Unit   Description
    =============== ====== =============================================
    i_sd            A      Direct axis current
    i_sq            A      Quadrature axis current
    i_a             A      Current through branch a
    i_b             A      Current through branch b
    i_c             A      Current through branch c
    i_alpha         A      Current in alpha axis
    i_beta          A      Current in beta axis
    =============== ====== =============================================
    =============== ====== =============================================
    Motor Voltages  Unit   Description
    =============== ====== =============================================
    u_sd            V      Direct axis voltage
    u_sq            V      Quadrature axis voltage
    u_a             V      Voltage through branch a
    u_b             V      Voltage through branch b
    u_c             V      Voltage through branch c
    u_alpha         V      Voltage in alpha axis
    u_beta          V      Voltage in beta axis
    =============== ====== =============================================

    ======== ===========================================================
    Limits / Nominal Value Dictionary Entries:
    -------- -----------------------------------------------------------
    Entry    Description
    ======== ===========================================================
    i        General current limit / nominal value
    i_a      Current in phase a
    i_b      Current in phase b
    i_c      Current in phase c
    i_alpha  Current in alpha axis
    i_beta   Current in beta axis
    i_sd     Current in direct axis
    i_sq     Current in quadrature axis
    omega    Mechanical angular Velocity
    torque   Motor generated torque
    epsilon  Electrical rotational angle
    u_a      Voltage in phase a
    u_b      Voltage in phase b
    u_c      Voltage in phase c
    u_alpha  Voltage in alpha axis
    u_beta   Voltage in beta axis
    u_sd     Voltage in direct axis
    u_sq     Voltage in quadrature axis
    ======== ===========================================================

    Note:
        The voltage limits should be the amplitude of the phase voltage (:math:`\hat{u}_S`).
        Typically the rms value for the line voltage (:math:`U_L`) is given.
        :math:`\hat{u}_S=\sqrt{2/3}~U_L`

        The current limits should be the amplitude of the phase current (:math:`\hat{i}_S`).
        Typically the rms value for the phase current (:math:`I_S`) is given.
        :math:`\hat{i}_S = \sqrt{2}~I_S`

        If not specified, nominal values are equal to their corresponding limit values.
        Furthermore, if specific limits/nominal values (e.g. i_a) are not specified they are inferred from
        the general limits/nominal values (e.g. i)
    """

    #### Parameters taken from DOI: 10.1109/TPEL.2020.3006779 (A. Brosch, S. Hanke, O. Wallscheid, J. Boecker)
    #### and DOI: 10.1109/IEMDC.2019.8785122 (S. Hanke, O. Wallscheid, J. Boecker)
    _default_motor_parameter = {
        'p': 3,
        'l_d': 0.37e-3,
        'l_q': 1.2e-3,
        'j_rotor': 0.03883,
        'r_s': 18e-3,
        'psi_p': 66e-3,
    }
    HAS_JACOBIAN = True
    _default_limits = dict(omega=4e3 * np.pi / 30, torque=0.0, i=400, epsilon=math.pi, u=300)
    _default_nominal_values = dict(omega=3e3 * np.pi / 30, torque=0.0, i=240, epsilon=math.pi, u=300)
    _default_initializer = {
        'states':  {'i_sq': 0.0, 'i_sd': 0.0, 'epsilon': 0.0},
        'interval': None,
        'random_init': None,
        'random_params': (None, None)
    }

    IO_VOLTAGES = ['u_a', 'u_b', 'u_c', 'u_sd', 'u_sq']
    IO_CURRENTS = ['i_a', 'i_b', 'i_c', 'i_sd', 'i_sq']

    def _update_model(self):
        # Docstring of superclass
        mp = self._motor_parameter
        self._model_constants = np.array([
            #                 omega,        i_d,        i_q, u_d, u_q,          omega * i_d,          omega * i_q
            [                     0, -mp['r_s'],          0,   1,   0,                    0, mp['l_q'] * mp['p']],
            [-mp['psi_p'] * mp['p'],          0, -mp['r_s'],   0,   1, -mp['l_d'] * mp['p'],                   0],
            [               mp['p'],          0,          0,   0,   0,                    0,                   0],
        ])

        self._model_constants[self.I_SD_IDX] = self._model_constants[self.I_SD_IDX] / mp['l_d']
        self._model_constants[self.I_SQ_IDX] = self._model_constants[self.I_SQ_IDX] / mp['l_q']

    def _torque_limit(self):
        # Docstring of superclass
        mp = self._motor_parameter
        if mp['l_d'] == mp['l_q']:
            return self.torque([0, self._limits['i_sq'], 0])
        else:
            i_n = self.nominal_values['i']
            _p = mp['psi_p'] / (2 * (mp['l_d'] - mp['l_q']))
            _q = - i_n ** 2 / 2
            i_d_opt = - _p / 2 - np.sqrt( (_p / 2) ** 2 - _q)
            i_q_opt = np.sqrt(i_n ** 2 - i_d_opt ** 2)
            return self.torque([i_d_opt, i_q_opt, 0])

    def torque(self, currents):
        # Docstring of superclass
        mp = self._motor_parameter
        return 1.5 * mp['p'] * (mp['psi_p'] + (mp['l_d'] - mp['l_q']) * currents[self.I_SD_IDX]) * currents[self.I_SQ_IDX]

    def electrical_jacobian(self, state, u_in, omega, *args):
        mp = self._motor_parameter
        return (
            np.array([ # dx'/dx
                [-mp['r_s'] / mp['l_d'], mp['l_q']/mp['l_d'] * omega * mp['p'], 0],
                [-mp['l_d'] / mp['l_q'] * omega * mp['p'], - mp['r_s'] / mp['l_q'], 0],
                [0, 0, 0]
            ]),
            np.array([ # dx'/dw
                mp['p'] * mp['l_q'] / mp['l_d'] * state[self.I_SQ_IDX],
                - mp['p'] * mp['l_d'] / mp['l_q'] * state[self.I_SD_IDX] - mp['p'] * mp['psi_p'] / mp['l_q'],
                mp['p']
            ]),
            np.array([ # dT/dx
                1.5 * mp['p'] * (mp['l_d'] - mp['l_q']) * state[self.I_SQ_IDX],
                1.5 * mp['p'] * (mp['psi_p'] + (mp['l_d'] - mp['l_q']) * state[self.I_SD_IDX]),
                0
            ])
        )

ext: py

reason: import numpy


text:
import pytest

import numpy as np
from io import StringIO
from dae.variants.variant import SummaryAllele
from dae.variants.family_variant import FamilyAllele
from dae.utils.variant_utils import GENOTYPE_TYPE

from dae.pedigrees.loader import FamiliesLoader

PED1 = """
# SIMPLE TRIO
familyId,    personId,    dadId,    momId,    sex,   status,    role
f1,          d1,          0,        0,        1,     1,         dad
f1,          m1,          0,        0,        2,     1,         mom
f1,          p1,          d1,       m1,       1,     2,         prb
"""

@pytest.fixture(scope="function")
def sample_family():
    families_loader = FamiliesLoader(StringIO(PED1), ped_sep=",")
    families = families_loader.load()
    family = families["f1"]
    assert len(family.trios) == 1
    return family

@pytest.mark.parametrize(
    "chromosome, position, reference, alternative, "
    + "allele_index, allele_count, genotype, expected",
    [
        (
            "chr1",
            0,
            "A",
            "T",
            1,
            2,
            np.array([[0, 0, 0], [0, 0, 1]], dtype=GENOTYPE_TYPE),
            np.array([[2, 2, 1], [0, 0, 1]], dtype=GENOTYPE_TYPE),
        ),
        (
            "chr1",
            0,
            "A",
            "T",
            1,
            2,
            np.array([[0, 0, 0], [0, 1, 1]], dtype=GENOTYPE_TYPE),
            np.array([[2, 1, 1], [0, 1, 1]], dtype=GENOTYPE_TYPE),
        ),
        (
            "chr1",
            0,
            "A",
            "T",
            1,
            2,
            np.array([[0, 0, 1], [0, 0, 1]], dtype=GENOTYPE_TYPE),
            np.array([[2, 2, 0], [0, 0, 2]], dtype=GENOTYPE_TYPE),
        ),
        (
            "chr1",
            0,
            "A",
            "G,T",
            1,
            3,
            np.array([[0, 1, 2], [0, 0, 0]], dtype=GENOTYPE_TYPE),
            np.array([[2, 1, 1], [0, 1, 0], [0, 0, 1]], dtype=GENOTYPE_TYPE),
        ),
        (
            "chr1",
            0,
            "A",
            "G,T",
            2,
            3,
            np.array([[0, 1, 2], [0, 0, 0]], dtype=GENOTYPE_TYPE),
            np.array([[2, 1, 1], [0, 1, 0], [0, 0, 1]], dtype=GENOTYPE_TYPE),
        ),
    ],
)
def test_allele_best_state(
    chromosome,
    position,
    reference,
    alternative,
    allele_index,
    allele_count,
    genotype,
    expected,
    sample_family,
):

    fa = FamilyAllele(
        SummaryAllele(
            chromosome, position, reference, alternative, 0, allele_index, {},
        ),
        sample_family,
        genotype,
        None,
    )
    fa.update_attributes({"allele_count": allele_count})
    assert np.array_equal(fa.best_state, expected)

max_stars_repo_name: iossifovlab/gpf

max_issues_repo_name: iossifovlab/gpf

max_forks_repo_name: iossifovlab/gpf


text:
import numpy as np

def read_input_data(file):
    points = []
    with open(file, "r") as f:
        for line in f.readlines():
            points.append([float(val) for val in  line.split(" ")])
    points = np.array(points)
    return points


text:
"""
Use the ``bokeh serve`` command to run the example by executing:
    bokeh serve --show gui
in your browser.
"""
from os.path import dirname, join
import numpy as np

from bokeh.io import curdoc
from bokeh.layouts import layout, Spacer
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models import HoverTool
from bokeh.models.widgets import (
    Slider,
    TextInput,
    Select,
    Paragraph,
    AutocompleteInput,
    CheckboxGroup,
    TableColumn,
    DataTable,
    Button,
    CheckboxGroup,
    Toggle,
    Select,
    Div
)
from bokeh.plotting import figure
from bokeh.events import ButtonClick

import astropy.units as u
from astropy import constants as const
from astropy.units.imperial import deg_F, inch, foot, mil

from roentgen.absorption import Material, Response
from roentgen.util import get_density, density_ideal_gas
import roentgen

u.imperial.enable()

DEFAULT_MATERIAL = ["silicon"]
DEFAULT_THICKNESS = [100.0]
DEFAULT_ENERGY_LOW = 1.0
DEFAULT_ENERGY_HIGH = 200.0
DEFAULT_DENSITY = [get_density(DEFAULT_MATERIAL[0]).value]
NUMBER_OF_MATERIALS = len(DEFAULT_MATERIAL)

DEFAULT_DETECTOR_MATERIAL = 'cdte'
DEFAULT_DETECTOR_THICKNESS = 1
DEFAULT_DETECTOR_DENSITY = get_density(DEFAULT_DETECTOR_MATERIAL).value

DEFAULT_ENERGY_RESOLUTION = 0.25

DEFAULT_AIR_THICKNESS = 1
DEFAULT_AIR_PRESSURE = 1
DEFAULT_AIR_TEMPERATURE = 25

PLOT_HEIGHT = 400
PLOT_WIDTH = 900
TOOLS = "pan,wheel_zoom,box_zoom,box_select,undo,redo,save,reset"

custom_hover = HoverTool(
    tooltips=[
        ('energy [keV]',   '@{x}{0.2f}'),
        ('transmission',  '@{y}{0.3f}'),  # use @{ } for field names with spaces
    ],
    # display a tooltip whenever the cursor is vertically in line with a glyph
    mode='vline'
)

# defaults
material_list = []

this_material = Material(DEFAULT_MATERIAL[0], DEFAULT_THICKNESS[0] * u.micron)
air_density = density_ideal_gas(DEFAULT_AIR_PRESSURE * const.atm,
                                DEFAULT_AIR_TEMPERATURE * u.Celsius)
air = Material('air', DEFAULT_AIR_THICKNESS * u.m, density=air_density)
this_detector = Material(DEFAULT_DETECTOR_MATERIAL,
                         DEFAULT_DETECTOR_THICKNESS * u.mm)

response = Response(optical_path=[this_material, air], detector=this_detector)

energy = u.Quantity(np.arange(DEFAULT_ENERGY_LOW, DEFAULT_ENERGY_HIGH,
                              DEFAULT_ENERGY_RESOLUTION), "keV")

x = energy.value
y = response.response(energy)
source = ColumnDataSource(data={"x": x, "y": y})

all_materials = list(roentgen.elements["name"]) + \
                list(roentgen.compounds["symbol"])
all_materials.sort()
all_materials = [this_material.lower() for this_material in all_materials]

# Set up the plot
plot = figure(
    plot_height=PLOT_HEIGHT,
    plot_width=PLOT_WIDTH,
    tools=TOOLS,
    x_range=[1, 50],
    y_range=[0, 1],
)
plot.yaxis.axis_label = "Transmission fraction"
plot.xaxis.axis_label = "Energy [keV]"
plot.line("x", "y", source=source, line_width=3, line_alpha=0.6)
plot.title.text = f"{response}"
plot.add_tools(custom_hover)
# Set up the inputs
ylog_checkbox = CheckboxGroup(labels=["y-log"], active=[0])

# materials in the path
material_input = AutocompleteInput(title="Material (lowercase)", value=DEFAULT_MATERIAL[0])
material_input.completions = all_materials
material_thickness_input = TextInput(title="thickness", value=str(DEFAULT_THICKNESS[0]))
material_density_input = TextInput(title="density", value=str(this_material.density.value))

air_thickness_input = TextInput(title="air path length", value=str(DEFAULT_AIR_THICKNESS))
air_pressure_input = TextInput(title='air pressure', value=str(DEFAULT_AIR_PRESSURE))
air_temperature_input = TextInput(title='air temperature', value=str(DEFAULT_AIR_TEMPERATURE))

detector_material_input = AutocompleteInput(title='Detector', value=DEFAULT_DETECTOR_MATERIAL)
detector_material_input.completions = all_materials
detector_thickness_input = TextInput(title='thickness', value=str(DEFAULT_DETECTOR_THICKNESS))
detector_density_input = TextInput(title='density', value=str(DEFAULT_DETECTOR_DENSITY))

p = Paragraph(text="", width=500)
p.text = f"Running roentgen version {roentgen.__version__}"

columns = [
    TableColumn(field="x", title="energy [keV]"),
    TableColumn(field="y", title="Percent"),
]
data_table = DataTable(source=source, columns=columns, width=400, height=700)

download_button = Button(label="Download", button_type="success")
download_button.js_on_event(ButtonClick, CustomJS(args=dict(source=source),
                                                  code=open(join(dirname(__file__), "download.js")).read()))

def convert_air_pressure(value, current_unit, new_unit):
    if current_unit == "atm":
        air_pressure = u.Quantity(value * const.atm, "Pa")
    elif current_unit == "torr":
        air_pressure = u.Quantity(value * const.atm / 760., "Pa")
    else:
        air_pressure = u.Quantity(value, current_unit)

    if new_unit == "atm":
        return (air_pressure.to("Pa") / const.atm).value
    elif new_unit == "torr":
        return (air_pressure.to("Pa") / const.atm).value * 760.0
    else:
        return air_pressure.to(new_unit)

def update_response(attrname, old, new):
    # check whether the input variables have changed and update the response
    global response

    if not material_input.disabled:
        if str(material_input.value).lower() in all_materials:
            this_thickness = u.Quantity(material_thickness_input.value,
                                        material_thick_unit.value)
            this_density = u.Quantity(material_density_input.value,
                                      material_density_unit.value)
            this_material = Material(str(material_input.value).lower(), this_thickness,
                                     density=this_density)
    else:
        # if material not selected, just make a bogus material with no thickness
        this_material = Material('Al', 0 * u.mm)

    if not air_pressure_input.disabled:
        if air_pressure_unit.value == "atm":
            air_pressure = u.Quantity(air_pressure_input.value * const.atm, "Pa")
        elif air_pressure_unit.value == "torr":
            air_pressure = u.Quantity(air_pressure_input.value * const.atm / 760.,
                                      "Pa")
        else:
            air_pressure = u.Quantity(air_pressure_input.value,
                                      air_pressure_unit.value)

        air_path_length = u.Quantity(air_thickness_input.value,
                                     air_thick_unit.value)
        air_temperature = u.Quantity(air_temperature_input.value,
                                     air_temp_unit.value).to("Celsius",
                                                             equivalencies=u.temperature())
        air_density = density_ideal_gas(air_pressure, air_temperature)
        air = Material('air', air_path_length, density=air_density)
    else:
        # if air is not selected than just add bogus air with no thickness
        air = Material('air', 0 * u.mm, density=0 * u.g / u.cm**3)

    if not detector_material_input.disabled:
        if str(detector_material_input.value).lower() in all_materials:
            this_thickness = u.Quantity(detector_thickness_input.value,
                                        detector_thick_unit.value)
            this_density = u.Quantity(detector_density_input.value,
                                      detector_density_unit.value)
            this_detector = Material(str(detector_material_input.value).lower(), this_thickness,
                                     density=this_density)
    else:
        this_detector = None
    response = Response(optical_path=[this_material, air], detector=this_detector)

def update_data(attrname, old, new):

    if plot.x_range.start < DEFAULT_ENERGY_LOW:
        energy = u.Quantity(np.arange(DEFAULT_ENERGY_LOW, plot.x_range.end,
                                  DEFAULT_ENERGY_RESOLUTION), "keV")
    else:
        energy = u.Quantity(np.arange(plot.x_range.start, plot.x_range.end,
                                  DEFAULT_ENERGY_RESOLUTION), "keV")

    y = response.response(energy)

    plot.title.text = f"{response}"

    if plot_checkbox_group.active:
        y = np.log10(response.response(energy))
        plot.y_range.start = -4
        plot.y_range.end = 0
        plot.yaxis.axis_label = 'log(Transmission fraction)'

    else:
        plot.y_range.start = 0
        plot.y_range.end = 1
        plot.yaxis.axis_label = 'Transmission fraction'

    source.data = dict(x=energy, y=y)

def toggle_active(new):
    if 0 in new:
        material_input.disabled = False
        material_thick_unit.disabled = False
        material_density_input.disabled = False
    if 0 not in new:
        material_input.disabled = True
        material_thick_unit.disabled = True
        material_density_input.disabled = True
    if 1 in new:
        air_pressure_input.disabled = False
        air_thickness_input.disabled = False
        air_temperature_input.disabled = False
    if 1 not in new:
        air_pressure_input.disabled = True
        air_thickness_input.disabled = True
        air_temperature_input.disabled = True
    if 2 in new:
        detector_material_input.disabled = False
        detector_thickness_input.disabled = False
        detector_density_input.disabled = False
    if 2 not in new:
        detector_material_input.disabled = True
        detector_thickness_input.disabled = True
        detector_density_input.disabled = True
    return new

checkbox_group = CheckboxGroup(labels=["Material", "Air", "Detector"],
                               active=[0, 1, 2])
checkbox_group.on_click(toggle_active)

def update_button_action():
    update_response("update_plot_button", 0, 0)
    update_data("update", 0, 0)

update_plot_button = Button(label="Update Plot", button_type="success")
update_plot_button.on_click(update_button_action)

plot.x_range.on_change('start', update_data)
plot.x_range.on_change('end', update_data)

plot_checkbox_group = CheckboxGroup(labels=["ylog"], active=[])

length_units = ["m", "mm", "micron", "inch", "foot", "mil"]
pressure_units = ["Pa", "torr", "atm"]
density_units = ["g / cm ** 3", "kg / m ** 3"]
temperature_units = ["K", "deg_C", "deg_F"]

material_thick_unit = Select(title="unit", value=length_units[2], options=length_units)

def update_mat_thick_units(attr, old, new):
    material_thickness_input.value = str(u.Quantity(material_thickness_input.value, old).to(new).value)
material_thick_unit.on_change('value', update_mat_thick_units)

detector_thick_unit = Select(title="unit", value=length_units[1],
                             options=length_units)

def update_det_thick_units(attr, old, new):
    detector_thickness_input.value = str(u.Quantity(detector_thickness_input.value, old).to(new).value)

detector_thick_unit.on_change('value', update_det_thick_units)
air_thick_unit = Select(title="unit", value=length_units[0],
                        options=length_units)

def update_air_thick_units(attr, old, new):
    air_thickness_input.value = str(u.Quantity(air_thickness_input.value, old).to(new).value)

air_thick_unit.on_change('value', update_air_thick_units)

material_density_unit = Select(title="unit", value=density_units[0],
                               options=density_units)

def update_mat_density_units(attr, old, new):
    material_density_input.value = str(u.Quantity(material_density_input.value, old).to(new).value)

material_density_unit.on_change('value', update_mat_density_units)

detector_density_unit = Select(title="unit", value=density_units[0], options=density_units)

def update_det_density_units(attr, old, new):
    detector_density_input.value = str(u.Quantity(detector_density_input.value, old).to(new).value)

detector_density_unit.on_change('value', update_det_density_units)
air_pressure_unit = Select(title="unit", value=pressure_units[2],
                           options=pressure_units)

def update_air_pressure_units(attr, old, new):
    air_pressure = convert_air_pressure(air_pressure_input.value, old, new)
    air_pressure_input.value = str(air_pressure)

air_pressure_unit.on_change('value', update_air_pressure_units)

air_temp_unit = Select(title="unit", value=temperature_units[1],
                       options=temperature_units)

def update_air_temp_units(attr, old, new):
    air_temperature_input.value = str(u.Quantity(air_temperature_input.value, old).to(new, equivalencies=u.temperature()).value)

air_temp_unit.on_change('value', update_air_temp_units)

def update_material_density(attr, old, new):
    # if the material is changed then update the density
    material_density_input.value = str(get_density(material_input.value).value)

material_input.on_change('value', update_material_density)

def update_detector_density(attr, old, new):
    # if the material is changed then update the density
    detector_density_input.value = str(get_density(detector_material_input.value).value)

detector_material_input.on_change('value', update_detector_density)

curdoc().add_root(
    layout(
        [
            [plot],
            [checkbox_group, plot_checkbox_group],
            [material_input, material_thickness_input, material_thick_unit, material_density_input, material_density_unit],
            [air_pressure_input, air_pressure_unit, air_thickness_input, air_thick_unit, air_temperature_input, air_temp_unit],
            [detector_material_input, detector_thickness_input, detector_thick_unit, detector_density_input, detector_density_unit],
            #[energy_low, energy_high, energy_step],
            [download_button, Spacer(), update_plot_button],
            [p]
        ],
        sizing_mode="scale_width",
    )
)
curdoc().title = "Roentgen"

max_stars_repo_name: ehsteve/roentgen

max_issues_repo_name: samaloney/roentgen

max_forks_repo_name: samaloney/roentgen

reason: import numpy,import astropy,from astropy


text:
import tensorflow as tf
import tensorflow_addons as tfa
import numpy as np

OUTPUT_CHANNELS = 2

def downsample(filters, size, apply_batchnorm=True):
  ''' Downsampling block'''
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
      tf.keras.layers.Conv2D(filters, size, strides=2, padding='same',
                             kernel_initializer=initializer, use_bias=False))

  if apply_batchnorm:
    result.add(tf.keras.layers.BatchNormalization())
    # result.add(tfa.layers.InstanceNormalization(axis=3, center=True, scale=True,
    #                                                  gamma_initializer=tf.random_normal_initializer(0., 0.02)))

  result.add(tf.keras.layers.LeakyReLU(0.2))

  return result

def upsample(filters, size, apply_dropout=False):
  ''' Upsamling block'''
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
    tf.keras.layers.Conv2DTranspose(filters, size, strides=2,
                                    padding='same',
                                    kernel_initializer=initializer,
                                    use_bias=False))
#   # Upsampling2d + Conv
#   result.add(tf.keras.layers.UpSampling2D(size=2, interpolation='bilinear'))
#   result.add(tf.keras.layers.Conv2DTranspose(filters, size, strides=1,
#                                     padding='same',
#                                     kernel_initializer=initializer,
#                                     use_bias=False))

  result.add(tf.keras.layers.BatchNormalization())
#   result.add(tfa.layers.InstanceNormalization(axis=-1, center=True, scale=True,
                                                    #  gamma_initializer=tf.random_normal_initializer(0., 0.02)))

  if apply_dropout:
      result.add(tf.keras.layers.Dropout(0.5))

  result.add(tf.keras.layers.ReLU())

  return result

def conv_1x1_bn_relu(filters):
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(
    tf.keras.layers.Conv2D(filters, 1, strides=1,
                                    padding='same',
                                    kernel_initializer=initializer,
                                    use_bias=False))

  result.add(tf.keras.layers.BatchNormalization())
  result.add(tf.keras.layers.ReLU())

  return result

# pix2pix with bacth_sixe=32 100 epochs
def Generator():
  ''' U-net generator with 8 downsampling and 8 upsampling
  blocks like original Pix2pix model'''
  inputs = tf.keras.layers.Input(shape=[256,256,1])

  down_stack = [
    downsample(64, 4, apply_batchnorm=False), # (bs, 128, 128, 64)
    downsample(128, 4), # (bs, 64, 64, 128)
    downsample(256, 4), # (bs, 32, 32, 256)
    downsample(512, 4), # (bs, 16, 16, 512)
    downsample(512, 4), # (bs, 8, 8, 512)
    downsample(512, 4), # (bs, 4, 4, 512)
    downsample(512, 4), # (bs, 2, 2, 512)
    downsample(512, 4), # (bs, 1, 1, 512)
  ]

  up_stack = [
    upsample(512, 4, apply_dropout=True), # (bs, 2, 2, 1024)
    upsample(512, 4, apply_dropout=True), # (bs, 4, 4, 1024)
    upsample(512, 4, apply_dropout=True), # (bs, 8, 8, 1024)
    upsample(512, 4), # (bs, 16, 16, 1024)
    upsample(256, 4), # (bs, 32, 32, 512)
    upsample(128, 4), # (bs, 64, 64, 256)
    upsample(64, 4), # (bs, 128, 128, 128)
  ]

  initializer = tf.random_normal_initializer(0., 0.02)
  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,
                                         strides=2,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh') # (bs, 256, 256, 3)

  x = inputs

  # Downsampling through the model
  skips = []
  for down in down_stack:
    x = down(x)
    skips.append(x)

  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

# pix2pix2 model with batch_sixe=8 for 100 epochs
def Generator2():
  ''' U-net generator with 4 downsampling and 4 upsampling
  blocks'''
  inputs = tf.keras.layers.Input(shape=[256,256,1])

  initializer = tf.random_normal_initializer(0., 0.02)

  first = tf.keras.Sequential([
            tf.keras.layers.Conv2D(64, 4, strides=1, padding='same',
                             kernel_initializer=initializer, use_bias=False),
            tf.keras.layers.LeakyReLU()
            ]) # (bs, 256, 256, 64)

  down_stack = [
    downsample(128, 4), # (bs, 128, 128, 128)
    downsample(256, 4), # (bs, 64, 64, 256)
    downsample(512, 4), # (bs, 32, 32, 512)
    downsample(512, 4), # (bs, 16, 16, 512)
  ]

  up_stack = [
    upsample(512, 4, apply_dropout=True), # (bs, 32, 32, 1024)
    upsample(256, 4, apply_dropout=True), # (bs, 64, 64, 512)
    upsample(128, 4), # (bs, 128, 128, 256)
    upsample(64, 4),# (bs, 256, 256, 128)
        ]

  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, kernel_size=1,
                                         strides=1,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh') # (bs, 256, 256, 3)

  x = inputs

  # Downsampling through the model
  skips = []

  x = first(x)
  skips.append(x)

  for down in down_stack:
    x = down(x)
    skips.append(x)

  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

def Generator3():
  ''' U-net generator with pretrained MobileNetV2 as encoder
  (frozen) and 5 upsampling blocks in decoder.'''
  inputs = tf.keras.layers.Input(shape=[256,256,1])

  initializer = tf.random_normal_initializer(0., 0.02)

  # convert grayscale to RGB by concatenating along last axis
  x = tf.concat([inputs]*3,axis=3)

  # Pretrained models accept RGB inputs only
  backbone = tf.keras.applications.MobileNetV2(input_shape=(256,256,3), include_top=False,weights='imagenet')

  # Use the activations of these layers
  layer_names = [
        'block_1_expand_relu',   # 128x128x96
        'block_3_expand_relu',   # 64x64x144
        'block_6_expand_relu',   # 32x32x192
        'block_13_expand_relu',  # 16x16x576
        'block_16_project',      # 8x8x320
  ]

  layers = [backbone.get_layer(name).output for name in layer_names]

  # Create the feature extraction model
  down_stack = tf.keras.Model(inputs=backbone.input, outputs=layers)
  down_stack.trainable = False

  up_stack = [
    upsample(512, 4, apply_dropout=True), # (bs, 16, 16,)
    upsample(512, 4, apply_dropout=True), # (bs, 32, 32,)
    upsample(256, 4), # (bs, 64, 64,)
    upsample(128, 4),# (bs, 128, 128,)
        ]

  last_upsample = upsample(64,4) #(bs, 256, 256,)

  # if we use upsampling in this layer, output is blurry (Maybe)
  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, kernel_size=1,                        # Conv2DTranspose for other models except upsampling
                                         strides=1,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh') # (bs, 256, 256, 3)

  # Downsampling through the model
  skips = down_stack(x)
  x = skips[-1]
  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last_upsample(x)
  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

# pix2pix3_resnet50 using pretrained encoder layers, freexed encoder, train decoder layer only
def Generator4():
  ''' U-net generator with pretrained Resnet50 as encoder
  (frozen) and 4 upsampling blocks in decoder with 1x1 conv layers
  between each block to reduce feature depth.'''
  inputs = tf.keras.layers.Input(shape=[256,256,1])

  initializer = tf.random_normal_initializer(0., 0.02)

  # convert grayscale to RGB by concatenating along last axis
  x = tf.concat([inputs]*3,axis=3)

  # Pretrained models accept RGB inputs only
  backbone = tf.keras.applications.ResNet50(input_shape=(256,256,3), include_top=False,weights='imagenet')
  # Use the activations of these layers

  layer_names = ['conv1_relu', #128x128x64
    'conv2_block3_out', #64x64x256
    'conv3_block4_out', #32x32x512
    'conv4_block6_out', # 16x16x1024
    'conv5_block3_out', # 8x8x2048
    ]

  layers = [backbone.get_layer(name).output for name in layer_names]

  # Create the feature extraction model
  down_stack = tf.keras.Model(inputs=backbone.input, outputs=layers)
  down_stack.trainable = False

  up_stack = [
    upsample(512, 4, apply_dropout=True), # (bs, 16, 16, 512)
    upsample(512, 4, apply_dropout=True), # (bs, 32, 32, 512)
    upsample(256, 4), # (bs, 64, 64, 256)
    upsample(128, 4),# (bs, 128, 128, 128)
        ]
  conv_1x1_list =[
    conv_1x1_bn_relu(1024), # (bs, 8, 8, 512)
    conv_1x1_bn_relu(512), # (bs, 16, 16, 512)
    conv_1x1_bn_relu(512), # (bs, 32, 32, 512)
    conv_1x1_bn_relu(256) # (bs, 64, 64, 256)
  ]

  last_upsample = upsample(64,4) #(bs, 256, 256,)

  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, kernel_size=1,
                                         strides=1,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh') # (bs, 256, 256, 3)

  # Downsampling through the model
  skips = down_stack(x)
  x = skips[-1]
  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for c_layer, up, skip in zip(conv_1x1_list, up_stack, skips):
    x = c_layer(x)
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last_upsample(x)
  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

# pix2pix3_densenet121 using pretrained encoder layers, freexed encoder, train decoder layer only
def Generator5():
  inputs = tf.keras.layers.Input(shape=[256,256,1])

  initializer = tf.random_normal_initializer(0., 0.02)

  # convert grayscale to RGB by concatenating along last axis
  x = tf.concat([inputs]*3,axis=3)

  # Pretrained models accept RGB inputs only
  backbone = tf.keras.applications.DenseNet121(input_shape=(256,256,3), include_top=False,weights='imagenet')
  # Use the activations of these layers
# 'densenet121': (311, 139, 51, 4)
  layer_names = ['conv1/relu', #128x128x64  layer4
    'pool2_conv', #64x64x128                layer51
    'pool3_conv', #32x32x256                layer139
    'pool4_conv', # 16x16x512               layer311
    'relu', # 8x8x1024                      layer426(last)
    ]

  layers = [backbone.get_layer(name).output for name in layer_names]

  # Create the feature extraction model
  down_stack = tf.keras.Model(inputs=backbone.input, outputs=layers)
  down_stack.trainable = False

  up_stack = [
    upsample(512, 4, apply_dropout=True), # (bs, 16, 16, 512)
    upsample(512, 4, apply_dropout=True), # (bs, 32, 32, 512)
    upsample(256, 4), # (bs, 64, 64, 256)
    upsample(128, 4),# (bs, 128, 128, 128)
        ]

  last_upsample = upsample(64,4) #(bs, 256, 256,)

  last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, kernel_size=1,
                                         strides=1,
                                         padding='same',
                                         kernel_initializer=initializer,
                                         activation='tanh') # (bs, 256, 256, 3)

  # Downsampling through the model
  skips = down_stack(x)
  x = skips[-1]
  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    x = tf.keras.layers.Concatenate()([x, skip])

  x = last_upsample(x)
  x = last(x)

  return tf.keras.Model(inputs=inputs, outputs=x)

def Discriminator():
  initializer = tf.random_normal_initializer(0., 0.02)

  inp = tf.keras.layers.Input(shape=[256, 256, 1], name='input_image')          # grayscale image | L-channel
  tar = tf.keras.layers.Input(shape=[256, 256, OUTPUT_CHANNELS], name='target_image')         # (256, 256, 3) for RGB | (256, 256, 2) for ab channel

  x = tf.keras.layers.concatenate([inp, tar])

  down1 = downsample(64, 4, False)(x) # (bs, 128, 128, 64)
  down2 = downsample(128, 4)(down1) # (bs, 64, 64, 128)
  down3 = downsample(256, 4)(down2) # (bs, 32, 32, 256)

  zero_pad1 = tf.keras.layers.ZeroPadding2D()(down3) # (bs, 34, 34, 256)
  conv = tf.keras.layers.Conv2D(512, 4, strides=1,
                                kernel_initializer=initializer,
                                use_bias=False)(zero_pad1) # (bs, 31, 31, 512)

  batchnorm1 = tf.keras.layers.BatchNormalization()(conv)
#   instnorm1 = tfa.layers.InstanceNormalization(axis=3, center=True, scale=True,
#                                                      gamma_initializer=tf.random_normal_initializer(0., 0.02))(conv)

  leaky_relu = tf.keras.layers.LeakyReLU(0.2)(batchnorm1)

  zero_pad2 = tf.keras.layers.ZeroPadding2D()(leaky_relu) # (bs, 33, 33, 512)

  last = tf.keras.layers.Conv2D(1, 4, strides=1,
                                kernel_initializer=initializer)(zero_pad2) # (bs, 30, 30, 1)

  return tf.keras.Model(inputs=[inp, tar], outputs=last)


text:
import rospy
import numpy as np
from gym import spaces
from gym.envs.registration import register
import tiago_env

max_episode_steps = 1000

register(
        id='TiagoSimpleEnv-v0',
        entry_point='tiago_simple_env:TiagoSimpleEnv',
        max_episode_steps=max_episode_steps,
    )
class TiagoSimpleEnv(tiago_env.TiagoEnv):
    """
    Observation:
        Type: Box(3)
        Num     Observation
        0       absolute pos of end effector
        1       relative pos of end effector to goal
        2       distance between gripper fingers

    Actions:
        Type: Box(4)
        Num     Action
        0       x-pos of end effector
        1       y-pos of end effector
        2       z-pos of end effector
        3       distance between gripper fingers

    Reward:

    """
    def __init__(self):
        super(TiagoSimpleEnv, self).__init__()

        # Observation space
        self.obs_low = np.array([0.4, -0.25, 0.65])
        self.obs_high = np.array([0.7, 0.25, 0.75])

        self.observation_space = spaces.Box(self.obs_low, self.obs_high)

        # Action space
        #high = np.array([0.1, 0.1, 0.1])
        #self.action_space = spaces.Box(-high, high)
        self.action_space = spaces.Discrete(6)

        self.obs_points = {'x': [], 'y': [], 'z':[]}
        for x in np.arange(self.obs_low[0], self.obs_high[0]+0.1, 0.1):
            for y in np.arange(self.obs_low[1], self.obs_high[1]+0.1, 0.1):
                for z in np.arange(self.obs_low[2], self.obs_high[2]+0.1, 0.1):
                    self.obs_points['x'].append(x)
                    self.obs_points['y'].append(y)
                    self.obs_points['z'].append(z)
        self.visualize_points(self.obs_points['x'], self.obs_points['y'], self.obs_points['z'], ns='obs')

        # randomize goal
        x = np.random.uniform(self.obs_low[0], self.obs_high[0])
        y = np.random.uniform(self.obs_low[1], self.obs_high[1])
        z = np.random.uniform(self.obs_low[2], self.obs_high[2])

        x = self.obs_points['x'][np.abs(np.asarray(self.obs_points['x'])-x).argmin()]
        y = self.obs_points['y'][np.abs(np.asarray(self.obs_points['y'])-y).argmin()]
        z = self.obs_points['z'][np.abs(np.asarray(self.obs_points['z'])-z).argmin()]

        self.goal = [x, y, z]

    def _set_init_pose(self):
        """Sets the Robot in its init pose
        """
        self.visualize_points(*self.goal, ns='goal')
        x, y, z = 0.5, 0.15, 0.65
        roll, pitch, yaw = 0, np.radians(90), np.radians(90)
        self.set_gripper_joints(0.001, 0.001)
        self.set_arm_pose(x, y, z, roll, pitch, yaw)

    def _init_env_variables(self):
        """
        Inits variables needed to be initialised each time we reset at the start
        of an episode.
        :return:
        """
        self.action_count = 0

    def _set_action(self, action):
        """
        Move the robot based on the action variable given
        """
        self.action_count += 1

        x, y, z, _, _, _ = self.stored_arm_state

        plan = True
        if action == 1:
            x += 0.1
        elif action == 2:
            x -= 0.1
        elif action == 3:
            y += 0.1
        elif action == 4:
            y -= 0.1
        elif action == 5:
            z += 0.1
        elif action == 6:
            z -= 0.1
        roll, pitch, yaw = 0, np.radians(90), np.radians(90)

        x = self.obs_points['x'][np.abs(np.asarray(self.obs_points['x'])-x).argmin()]
        y = self.obs_points['y'][np.abs(np.asarray(self.obs_points['y'])-y).argmin()]
        z = self.obs_points['z'][np.abs(np.asarray(self.obs_points['z'])-z).argmin()]

        self.visualize_points(x, y, z, ns='action')
        self.action_failed = not self.set_arm_pose(x, y, z, roll, pitch, yaw)

    def _get_obs(self):
        """
        Here we define what sensor data of our robots observations
        To know which Variables we have acces to, we need to read the
        MyRobotEnv API DOCS
        :return: observations
        """
        x, y, z, _, _, _ = self.stored_arm_state
        observations = [x, y, z]
        return observations

    def _is_done(self, observations):
        """
        Decide if episode is done based on the observations
        """
        if self.action_count >= 50 or rospy.is_shutdown():
            done = True
        else:
            done = False
        return done

    def _compute_reward(self, observations, done):
        """
        Return the reward based on the observations given
        """
        _, rel, = self.calculate_distances()

        if rel <= 0.05:
            reward = 10
        else:
            reward = -rel

        return reward

    # Internal TaskEnv Methods

    def calculate_distances(self):
        ee_xyz = np.array(self.stored_arm_state[:3])
        goal_xyz = np.array(self.goal)

        abs = np.linalg.norm(ee_xyz)
        rel = np.linalg.norm(ee_xyz-goal_xyz)
        return abs, rel

max_stars_repo_name: edluffy/rl-tiago


text:
import numpy as np

#utils

def outersum(a,b):
    return np.outer(a,np.ones_like(b))+np.outer(np.ones_like(a),b)

def Spec_Mix(x,y, gamma, mu, sigma=1):
    return sigma**2 * np.exp(-gamma*outersum(x,-y)**2)*np.cos(2*np.pi*mu*outersum(x,-y))

def Spec_Mix_sine(x,y, gamma, mu, sigma=1):
    return sigma**2 * np.exp(-gamma*outersum(x,-y)**2)*np.sin(2*np.pi*mu*outersum(x,-y))


text:
# The MNIST-1D dataset | 2020
# Sam Greydanus

import numpy as np
import requests
from .transform import transform
from .utils import from_pickle, to_pickle, ObjectView, set_seed

def get_dataset_args(as_dict=False):
    arg_dict = {'num_samples': 5000,
            'train_split': 0.8,
            'template_len': 12,
            'padding': [36,60],
            'scale_coeff': .4,
            'max_translation': 48,
            'corr_noise_scale': 0.25,
            'iid_noise_scale': 2e-2,
            'shear_scale': 0.75,
            'shuffle_seq': False,
            'final_seq_length': 40,
            'seed': 42,
            'url': 'https://github.com/greydanus/mnist1d/raw/master/mnist1d_data.pkl'}
    return arg_dict if as_dict else ObjectView(arg_dict)

# basic 1D templates for the 10 digits
def get_templates():
    d0 = np.asarray([5,6,6.5,6.75,7,7,7,7,6.75,6.5,6,5])
    d1 = np.asarray([5,3,3,3.4,3.8,4.2,4.6,5,5.4,5.8,5,5])
    d2 = np.asarray([5,6,6.5,6.5,6,5.25,4.75,4,3.5,3.5,4,5])
    d3 = np.asarray([5,6,6.5,6.5,6,5,5,6,6.5,6.5,6,5])
    d4 = np.asarray([5,4.4,3.8,3.2,2.6,2.6,5,5,5,5,5,5])
    d5 = np.asarray([5,3,3,3,3,5,6,6.5,6.5,6,4.5,5])
    d6 = np.asarray([5,4,3.5,3.25,3,3,3,3,3.25,3.5,4,5])
    d7 = np.asarray([5,7,7,6.6,6.2,5.8,5.4,5,4.6,4.2,5,5])
    d8 = np.asarray([5,4,3.5,3.5,4,5,5,4,3.5,3.5,4,5])
    d9 = np.asarray([5,4,3.5,3.5,4,5,5,5,5,4.7,4.3,5])

    x = np.stack([d0,d1,d2,d3,d4,d5,d6,d7,d8,d9])
    x -= x.mean(1,keepdims=True) # whiten
    x /= x.std(1,keepdims=True)
    x -= x[:,:1]  # signal starts and ends at 0

    templates = {'x': x/6., 't': np.linspace(-5, 5, len(d0))/6.,
                 'y': np.asarray([0,1,2,3,4,5,6,7,8,9])}
    return templates

# make a dataset
def make_dataset(args=None, template=None, ):
    templates = get_templates() if template is None else template
    args = get_dataset_args() if args is None else args
    np.random.seed(args.seed) # reproducibility

    xs, ys = [], []
    samples_per_class = args.num_samples // len(templates['y'])
    for label_ix in range(len(templates['y'])):
        for example_ix in range(samples_per_class):
            x = templates['x'][label_ix]
            t = templates['t']
            y = templates['y'][label_ix]
            x, new_t = transform(x, t, args) # new_t transformation is same each time
            xs.append(x) ; ys.append(y)

    batch_shuffle = np.random.permutation(len(ys)) # shuffle batch dimension
    xs = np.stack(xs)[batch_shuffle]
    ys = np.stack(ys)[batch_shuffle]

    if args.shuffle_seq: # maybe shuffle the spatial dimension
        seq_shuffle = np.random.permutation(args.final_seq_length)
        xs = xs[...,seq_shuffle]

    new_t = new_t/xs.std()
    xs = (xs-xs.mean())/xs.std() # center the dataset & set standard deviation to 1

    # train / test split
    split_ix = int(len(ys)*args.train_split)
    dataset = {'x': xs[:split_ix], 'x_test': xs[split_ix:],
               'y': ys[:split_ix], 'y_test': ys[split_ix:],
               't':new_t, 'templates': templates}
    return dataset

# we'll cache the dataset so that it doesn't have to be rebuild every time
def get_dataset(args, path=None, verbose=True, download=True, regenerate=False, **kwargs):
    if 'args' in kwargs.keys() and kwargs['args'].shuffle_seq:
        shuffle = "_shuffle"
    else:
        shuffle = ""
    path = './mnist1d_data{}.pkl'.format(shuffle) if path is None else path

    assert not (download and regenerate), "You can either download the o.g. MNIST1D dataset or generate your own - but not both"
    try:
        if regenerate:
            raise ValueError("Regenerating dataset") # yes this is hacky
        if download:
            print("Downloading MNIST1D dataset from {}".format(args.url))
            r = requests.get(args.url, allow_redirects=True)
            open(path, 'wb').write(r.content)
            print("Saving to {}".format(path))
        dataset = from_pickle(path)
        if verbose:
            print("Successfully loaded data from {}".format(path))
    except:
        if verbose:
            print("Did or could not load data from {}. Rebuilding dataset...".format(path))
        dataset = make_dataset(args, **kwargs)
        to_pickle(dataset, path)
    return dataset


text:
from ctypes import py_object
import os
import glob
import json
import csv
import numpy as np
import math
from PIL import Image
import cv2
from PIL import Image, ImageDraw

NBR_POINTS = 32

have_instances = ['person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', 'bicycle']
id_to_label = {24:'person', 25:'rider', 26:'car', 27:'truck', 28:'bus',29:'noinstance', 30:'noinstance', 31:'train', 32:'motorcycle', 33:'bicycle'}

for filename in sorted(glob.glob('../BBoxes/*regular_interval.json', recursive=True)):
    new_filename = filename.replace('.json', '_polar.json')
    data = json.load(open(filename))

    objects = data['annotations']

    WEIGHT_ANGLE = 100

    for object in objects:
        center_x = object['bbox'][0]
        center_y = object['bbox'][1]
        cartesian = object['poly']

        polar = []
        for i in range(0,len(cartesian), 2):
            x = cartesian[i]-center_x
            y = cartesian[i+1]-center_y

            r = math.sqrt(x*x+y*y)
            theta = math.atan(y/(x+1e-8))
            if x < 0 :
                theta = theta + math.pi
            polar.append(r)
            polar.append(theta*WEIGHT_ANGLE)

        object['poly'] = polar

    #data['annotations'] = objects
    with open(new_filename, 'w') as f:
        f.write(json.dumps(data, sort_keys=True))

#Faire un essai jouet sur un exemple de polygone des images

max_stars_repo_name: KatiaJDL/CenterPoly

max_issues_repo_name: KatiaJDL/CenterPoly

max_forks_repo_name: KatiaJDL/CenterPoly


text:
# coding: utf-8

# # XOR Model | Pickle

import numpy as np
import sklearn
import pickle
import os
from sklearn.neural_network import MLPClassifier as mlpc

os.getcwd()
os.listdir()

xs = np.array([0,0,0,1,1,0,1,1]).reshape(4,2)
ys = np.array([0,1,1,0]).reshape(4,)

mdl = mlpc(activation='relu', max_iter=10000, hidden_layer_sizes=(4,2))
mdl.fit(xs, ys)

filename = 'XOR_model.pkl'

pickle.dump(mdl, open(filename, 'wb'))

os.listdir()

# #### 'XOR_model.pkl'
#
# Model has been saved into a 'pkl' file.

# help('modules')

filename = 'XOR_model.pkl'

from flask import Flask, jsonify, request

def xor_predictor(a, b):
    output = {'XOR': 0}
    x_input = np.array([a,b]).reshape(1,2)
    m1 = pickle.load(open(filename, 'rb'))
    output['XOR'] = int(m1.predict(x_input)[0])
    return output

app = Flask(__name__)

#### builtins.TypeError TypeError: Object of type 'int32' is not JSON serializable

#### To avoid this error - use int(m1.predict(x_input)[0])

@app.route("/")
def index():
    return "XOR Predictor!!"

# In[31]:

@app.route("/XOR", methods=['GET'])
def cal_xor_predictor():
    body = request.get_data()
    header = request.headers

    try:
        num1 = int(request.args['x1'])
        num2 = int(request.args['x2'])
        if (num1 != None) and (num2 != None) and ((num1 == 0) or (num1 == 1)) and ((num2 == 0) or (num2 == 1)):
            res = xor_predictor(num1, num2)
        else:
            res = {
                'success': False,
                'message': 'invalid inputs; 0 or 1 expected'
            }
    except:
        res = {
            'success': False,
            'message': 'Unknown error'
        }

    return jsonify(res)

# In[33]:

if __name__ == "__main__":
    app.run(debug=True, port=8808)


text:
from numpy import sqrt, pi, exp
import numpy as np
from scipy.special import erf

class muprompt(object):
    def _init_(self,x,y,e=1):
        '''
        generic __init__ for all mufunction classes
        x, y, e are numpy arrays
        e is always defined,
        either provided by the caller
        or default to np.ones(x.shape[0])
        '''
        if x.shape[0]!=y.shape[0]:
            raise ValueError('x, y have different lengths')
        else:
            self.x = x
            self.y = y
        try:
            if e==1:
                self.e = np.ones(x.shape[0])
        except:
            if e.shape[0]!=x.shape[0]:
                raise ValueError('x, e have different lengths')
            else:
                self.e = e
    # ---- end generic __init__
    def f(self,x,a,x0,dx,ak1,ak2):
        '''
        fit function for PSI t0 fit on the prompt peak,
        sluggish python version
        a gaussian peak coincident with the edge betwee two plateaus (a constant + an erf)
        parameters:
        peak height,
        peak center,
        peak std width,
        first plateau,
        second plateau.
        Needs x as first ture argument because it is also invoked for plot.
        '''
        return a/(sqrt(2.*pi)*dx) * exp(-.5*((x-x0)/dx)**2)+ak2/2.+ak1+ak2/2.*erf((x-x0)/sqrt(2.)/dx)

    def __call__(self,a, x0, dx, ak1, ak2):
        '''
        chisquare, normalized if self.e was provided, unnormalized otherwise
        '''
        return sum(((self.f(self.x,a,x0,dx,ak1,ak2)-self.y)/self.e)**2)

max_stars_repo_name: RDeRenzi/mujpy

max_issues_repo_name: RDeRenzi/mujpy

max_forks_repo_name: RDeRenzi/mujpy

reason: import numpy,from numpy,from scipy


text:
import numpy as np
#from numba import jit, prange
#from astropy.stats import histogram

def power_spect_nd(input_array, box_dims, verbose=True):
	'''
	Calculate the power spectrum of input_array and return it as an n-dimensional array,
	where n is the number of dimensions in input_array
	box_side is the size of the box in comoving Mpc. If this is set to None (default),
	the internal box size is used

	Parameters:
		* input_array (numpy array): the array to calculate the
			power spectrum of. Can be of any dimensions.
		* box_dims = None (float or array-like): the dimensions of the
			box. If this is None, the current box volume is used along all
			dimensions. If it is a float, this is taken as the box length
			along all dimensions. If it is an array-like, the elements are
			taken as the box length along each axis.

	Returns:
		The power spectrum in the same dimensions as the input array.
	'''

	if np.array(box_dims).size!=3: box_dims = np.array([box_dims,box_dims,box_dims])
	if verbose: print( 'Calculating power spectrum...')
	ft = np.fft.fftshift(np.fft.fftn(input_array.astype('float64')))
	power_spectrum = np.abs(ft)**2
	if verbose: print( '...done')

	# scale
	#print(box_dims)
	boxvol = np.product(box_dims)
	#print(boxvol)
	pixelsize = boxvol/(np.product(input_array.shape))
	power_spectrum *= pixelsize**2/boxvol

	return power_spectrum

def phase_spect_nd(input_array, box_dims, verbose=True):
	'''
	Calculate the phase spectrum of input_array and return it as an n-dimensional array,
	where n is the number of dimensions in input_array
	box_side is the size of the box in comoving Mpc. If this is set to None (default),
	the internal box size is used

	Parameters:
		* input_array (numpy array): the array to calculate the
			power spectrum of. Can be of any dimensions.
		* box_dims = None (float or array-like): the dimensions of the
			box. If this is None, the current box volume is used along all
			dimensions. If it is a float, this is taken as the box length
			along all dimensions. If it is an array-like, the elements are
			taken as the box length along each axis.

	Returns:
		The power spectrum in the same dimensions as the input array.
	'''

	if np.array(box_dims).size!=3: box_dims = np.array([box_dims,box_dims,box_dims])
	if verbose: print( 'Calculating power spectrum...')
	ft = np.fft.fftshift(np.fft.fftn(input_array.astype('float64')))
	phase_spectrum = np.arctan(np.imag(ft)/np.real(ft)) #np.abs(ft)**2
	if verbose: print( '...done')

	# scale
	#print(box_dims)
	boxvol = np.product(box_dims)
	#print(boxvol)
	pixelsize = boxvol/(np.product(input_array.shape))
	power_spectrum *= pixelsize**2/boxvol

	return power_spectrum

def _get_k(input_array, box_dims):
	'''
	Get the k values for input array with given dimensions.
	Return k components and magnitudes.
	For internal use.
	'''
	if np.array(box_dims).size!=3: box_dims = np.array([box_dims,box_dims,box_dims])
	dim = len(input_array.shape)
	if dim == 1:
		x = np.arange(len(input_array))
		center = x.max()/2.
		kx = 2.*np.pi*(x-center)/box_dims[0]
		return [kx], kx
	elif dim == 2:
		x,y = np.indices(input_array.shape, dtype='int32')
		center = np.array([(x.max()-x.min())/2, (y.max()-y.min())/2])
		kx = 2.*np.pi * (x-center[0])/box_dims[0]
		ky = 2.*np.pi * (y-center[1])/box_dims[1]
		k = np.sqrt(kx**2 + ky**2)
		return [kx, ky], k
	elif dim == 3:
		x,y,z = np.indices(input_array.shape, dtype='int32')
		center = np.array([(x.max()-x.min())/2, (y.max()-y.min())/2, \
						(z.max()-z.min())/2])
		kx = 2.*np.pi * (x-center[0])/box_dims[0]
		ky = 2.*np.pi * (y-center[1])/box_dims[1]
		kz = 2.*np.pi * (z-center[2])/box_dims[2]

		k = np.sqrt(kx**2 + ky**2 + kz**2 )
		return [kx,ky,kz], k

def power_spect_1d(input_array, kbins=10, binning='log', box_dims=244/.7, return_modes=False, kmin=None, kmax=None):
	power = power_spect_nd(input_array, box_dims, verbose=0)
	[kx,ky,kz], k = _get_k(input_array, box_dims)
	if kmin is None: kmin = 2*np.pi/np.array(box_dims).max()
	if kmax is None: kmax = k.max()
	if binning=='log':
		ks = np.linspace(np.log10(kmin), np.log10(kmax), kbins+1)
		k  = np.log10(k)
	elif binning=='linear': ks = np.linspace(kmin, kmax, kbins+1)
	elif binning=='auto':
		#kht = histogram(k.flatten(), bins='knuth')
		n_bin  = 1.*k.size/kbins
		k_sort = np.sort(k.flatten())
		ks = np.array([k_sort[int(i)] for i in np.arange(n_bin/2., k.size, n_bin)])
	if binning!='auto': ks = (ks[:-1]+ks[1:])/2.
	kkk = np.hstack((k.min(),(ks[1:]+ks[:-1])/2, k.max()))
	k_width = kkk[1:]-kkk[:-1]
	ps = np.zeros(kbins)
	n_modes = np.zeros(kbins)
	k, power = k.flatten(), power.flatten()
	for i,a in enumerate(ks):
		arg = np.argwhere(np.abs(k-a)<=k_width[i]/2.)
		ps[i] = power[arg].sum()
		n_modes[i] = arg.size

	ps = ps/n_modes
	if binning=='log': ks = 10**ks
	if return_modes: return ps, ks, n_modes
	return ps, ks

def power_spect_2d(input_array, kbins=10, binning='log', box_dims=244/.7, return_modes=False, nu_axis=2):
	'''
	Calculate the power spectrum and bin it in kper and kpar
	input_array is the array to calculate the power spectrum from

	Parameters:
		input_array (numpy array): the data array
		nu_axis = 2 (integer): the line-of-sight axis
		kbins = 10 (integer or array-like): The number of bins,
			If you want different bins for kper and kpar, then provide a list [n_kper, n_par]
		box_dims = 244/.7 (float or array-like): the dimensions of the
			box. If this is None, the current box volume is used along all
			dimensions. If it is a float, this is taken as the box length
			along all dimensions. If it is an array-like, the elements are
			taken as the box length along each axis.
		return_n_modes = False (bool): if true, also return the
			number of modes in each bin
		binning = 'log' : It defines the type of binning in k-space. The other option is
				    'linear' or 'mixed'.

	Returns:
		A tuple with (Pk, kper_bins, kpar_bins) if return_modes is False else (Pk, kper_bins, kpar_bins, n_modes),
		where Pk is an array with the power spectrum of dimensions (n_kper x n_kpar),
		mubins is an array with the mu bin centers,
		kbins is an array with the k bin centers and
		n_modes is the number of modes.

	'''
	if np.array(kbins).size==1: kbins = [kbins, kbins]
	power = power_spect_nd(input_array, box_dims, verbose=0)
	[kx,ky,kz], k = _get_k(input_array, box_dims)
	kdict = {}
	kdict['0'], kdict['1'], kdict['2'] = kx, ky, kz
	del kx, ky, kz
	kz = kdict[str(nu_axis)]
	kp = np.sqrt(kdict[str(np.setdiff1d([0,1,2],nu_axis)[0])]**2+kdict[str(np.setdiff1d([0,1,2],nu_axis)[1])]**2)
	kz = np.abs(kz)
	# print(np.abs(kp[kp!=0]).min(),np.abs(kz[kz!=0]).min())
	if binning=='log':
		kper = np.linspace(np.log10(np.abs(kp[kp!=0]).min()), np.log10(kp.max()), kbins[0]+1)
		kpar = np.linspace(np.log10(np.abs(kz[kz!=0]).min()), np.log10(kz.max()), kbins[1]+1)
		kp, kz  = np.log10(kp), np.log10(kz)
	elif binning=='linear':
		kper = np.linspace(np.abs(kp[kp!=0]).min(), kp.max(), kbins[0]+1)
		kpar = np.linspace(np.abs(kz[kz!=0]).min(), kz.max(), kbins[1]+1)
	k_width = kper[1]-kper[0], kpar[1]-kpar[0]
	# print(10**kper,10**kpar)
	kper = (kper[:-1]+kper[1:])/2.
	kpar = (kpar[:-1]+kpar[1:])/2.
	# print(10**kper,10**kpar)
	ps = np.zeros((kbins[0],kbins[1]))
	n_modes = np.zeros((kbins[0],kbins[1]))
	kp, kz, power = kp.flatten(), kz.flatten(), power.flatten()
	for i,a in enumerate(kper):
		for j,b in enumerate(kpar):
			arg = np.intersect1d(np.argwhere(np.abs(kp-a)<=k_width[0]/2.), np.argwhere(np.abs(kz-b)<=k_width[1]/2.))
			ps[i,j] = power[arg].sum()
			n_modes[i,j] = arg.size

	ps = ps/n_modes
	if binning=='log': kper, kpar = 10**kper, 10**kpar
	if return_modes: return ps, kper, kpar, n_modes
	# print(kper,kpar)
	return ps, kper, kpar

def dimensional_power(input_array, kbins=10, binning='log', box_dims=244/.7, return_modes=False):
	ps, ks, n_modes = power_spect_1d(input_array, kbins=kbins, binning=binning, box_dims=box_dims, return_modes=1)
	if return_modes: return ps*ks**3/2/np.pi**2, ks, n_modes
	return ps*ks**3/2/np.pi**2, ks

def power_spect_mu(input_array, kbins=10, box_dims=244/.7, return_modes=False, mubins=10, binning='log', nu_axis=2):
	if type(binning)==str: binning = [binning, binning]
	power = power_spect_nd(input_array, box_dims, verbose=0)
	[kx,ky,kz], k = _get_k(input_array, box_dims)
	kdict = {}
	kdict['0'], kdict['1'], kdict['2'] = kx, ky, kz
	del kx, ky, kz
	kpar = kdict[str(nu_axis)]
	kper = np.sqrt(kdict[str(np.setdiff1d([0,1,2],nu_axis)[0])]**2+kdict[str(np.setdiff1d([0,1,2],nu_axis)[1])]**2)
	m = np.abs(kpar/k)

	if binning[0]=='log':
		ks = np.linspace(np.log10(np.abs(k[k!=0]).min()), np.log10(k.max()), kbins+1)
		k  = np.log10(k)
	else: ks = np.linspace(np.abs(k[k!=0]).min(), k.max(), kbins+1)
	ks = (ks[:-1]+ks[1:])/2.
	k_width = ks[1]-ks[0]

	if binning[1]=='log':
		m1 = m[np.isfinite(m)]
		mu = np.linspace(np.log10(np.abs(m1[m1!=0]).min()), np.log10(m1.max()), mubins+1)
		m  = np.log10(m)
	else: mu = np.linspace(0,1,mubins+1);
	mu = (mu[:-1]+mu[1:])/2.
	m_width = mu[1]-mu[0]

	ps = np.zeros((kbins, mubins))
	n_modes = np.zeros((kbins, mubins))

	k, m, power = k.flatten(), m.flatten(), power.flatten()
	for i,a in enumerate(ks):
		for j,b in enumerate(mu):
			arg = np.intersect1d(np.argwhere(np.abs(k-a)<=k_width/2.), np.argwhere(np.abs(m[np.isfinite(m)]-b)<=m_width/2.))
			ps[i,j] = power[arg].sum()
			n_modes[i,j] = arg.size

	ps = ps/n_modes
	if binning[0]=='log': ks = 10**ks
	if binning[1]=='log': mu = 10**mu
	if return_modes: return ps, ks, mu, n_modes
	return ps, ks, mu

def plot_2d_power(ps, xticks, yticks, xlabel, ylabel):
	import matplotlib.pyplot as plt
	xticks, yticks = np.round(xticks, decimals=2), np.round(yticks, decimals=2)
	plt.imshow(ps, origin='lower')
	locs, labels = plt.yticks()
	new_labels = yticks[locs.astype(int)[1:-1]]
	plt.yticks(locs[1:-1], new_labels)
	plt.ylabel(ylabel)
	locs, labels = plt.xticks()
	new_labels = xticks[locs.astype(int)[1:-1]]
	plt.xticks(locs[1:-1], new_labels)
	plt.xlabel(xlabel)
	plt.colorbar()
	plt.show()

reason: import numpy,from numba,from astropy


text:
# Copyright 2020 Google LLC, University of Victoria, Czech Technical University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import numpy as np

def get_eval_path(mode, cfg):
    if mode == 'feature':
        return get_feature_path(cfg)
    elif mode == 'match':
        return get_match_path(cfg)
    elif mode == 'filter':
        return get_filter_path(cfg)
    elif mode == 'model':
        return get_geom_path(cfg)
    elif mode == 'stereo':
        return get_stereo_path(cfg)
    elif mode == 'multiview':
        return get_multiview_path(cfg)
    else:
        raise ValueError('Unknown job type')

def get_eval_file(mode, cfg, job_id=None):
    if job_id:
        return os.path.join(get_eval_path(mode, cfg),
                            '{}.{}'.format(job_id, mode))
    else:
        try:
            file_list = os.listdir(get_eval_path(mode, cfg))
            valid_file = [
                file for file in file_list if file.split('.')[-1] == mode
            ]
            if len(valid_file) == 0:
                return None
            elif len(valid_file) == 1:
                return os.path.join(get_eval_path(mode, cfg), valid_file[0])
            else:
                print('Should never be here')
                import IPython
                IPython.embed()
                return None
        except FileNotFoundError:
            os.makedirs(get_eval_path(mode, cfg))
            return None

def get_data_path(cfg):
    '''Returns where the per-dataset results folder is stored.

    TODO: This probably should be done in a neater way.
    '''

    # Get data directory for 'set_100'
    return os.path.join(cfg.path_data, cfg.dataset, cfg.scene,
                        'set_{}'.format(cfg.num_max_set))

def get_base_path(cfg):
    '''Returns where the per-dataset results folder is stored.'''

    return os.path.join(cfg.path_results, cfg.dataset, cfg.scene)

def get_feature_path(cfg):
    '''Returns where the keypoints and descriptor results folder is stored.

    Method names converted to lower-case.'''

    common = cfg.method_dict['config_common']
    return os.path.join(
        get_base_path(cfg),
        '{}_{}_{}'.format(common['keypoint'].lower(), common['num_keypoints'],
                          common['descriptor'].lower()))

def get_kp_file(cfg):
    '''Returns the path to the keypoint file.'''

    return os.path.join(get_feature_path(cfg), 'keypoints.h5')

def get_scale_file(cfg):
    '''Returns the path to the scale file.'''

    return os.path.join(get_feature_path(cfg), 'scales.h5')

def get_score_file(cfg):
    '''Returns the path to the score file.'''

    return os.path.join(get_feature_path(cfg), 'scores.h5')

def get_angle_file(cfg):
    '''Returns the path to the angle file.'''

    return os.path.join(get_feature_path(cfg), 'angles.h5')

def get_affine_file(cfg):
    '''Returns the path to the angle file.'''

    return os.path.join(get_feature_path(cfg), 'affine.h5')

def get_desc_file(cfg):
    '''Returns the path to the descriptor file.'''

    return os.path.join(get_feature_path(cfg), 'descriptors.h5')

def get_match_name(cfg):
    '''Return folder name for the matching model.

    Converted to lower-case to avoid conflicts.'''
    cur_key = 'config_{}_{}'.format(cfg.dataset, cfg.task)

    # simply return 'custom_matcher' if it is provided
    if cfg.method_dict[cur_key]['use_custom_matches']:
        return cfg.method_dict[cur_key]['custom_matches_name']

    # consturct matcher name
    matcher = cfg.method_dict[cur_key]['matcher']

    # Make a custom string for the matching folder
    label = []

    # Base name
    label += [matcher['method']]

    # flann/bf
    if matcher['flann']:
        label += ['flann']
    else:
        label += ['bf']

    # number of neighbours
    label += ['numnn-{}'.format(matcher['num_nn'])]

    # distance
    label += ['dist-{}'.format(matcher['distance'])]

    # 2-way matching
    if not matcher['symmetric']['enabled']:
        label += ['nosym']
    else:
        label += ['sym-{}'.format(matcher['symmetric']['reduce'])]

    # filtering
    if matcher['filtering']['type'] == 'none':
        label += ['nofilter']
    elif matcher['filtering']['type'].lower() in [
            'snn_ratio_pairwise', 'snn_ratio_vs_last'
    ]:
        # Threshold == 1 means no ratio test
        # It just makes writing the config files easier
        if matcher['filtering']['threshold'] == 1:
            label += ['nofilter']
        else:
            label += [
                'filter-{}-{}'.format(matcher['filtering']['type'],
                                      matcher['filtering']['threshold'])
            ]
    elif matcher['filtering']['type'].lower() == 'fginn_ratio_pairwise':
        label += [
            'filter-fginn-pairwise-{}-{}'.format(
                matcher['filtering']['threshold'],
                matcher['filtering']['fginn_radius'])
        ]
    else:
        raise ValueError('Unknown filtering type')

    # distance filtering
    if 'descriptor_distance_filter' in matcher:
        if 'threshold' in matcher['descriptor_distance_filter']:
            max_dist = matcher['descriptor_distance_filter']['threshold']
            label += ['maxdist-{:.03f}'.format(max_dist)]

    return '_'.join(label).lower()

def get_filter_path(cfg):
    '''Returns folder location for the outlier filter results.'''

    cur_key = 'config_{}_{}'.format(cfg.dataset, cfg.task)

    # Bypass this when using custom matches
    if cfg.method_dict[cur_key]['use_custom_matches']:
        return os.path.join(get_match_path(cfg), 'no_filter')

    # Otherwise, depends on the filter method
    outlier_filter = cfg.method_dict[cur_key]['outlier_filter']
    if outlier_filter['method'] in ['cne-bp-nd']:
        return os.path.join(get_match_path(cfg), outlier_filter['method'])
    elif outlier_filter['method'] == 'none':
        return os.path.join(get_match_path(cfg), 'no_filter')
    else:
        raise ValueError('Unknown outlier_filter type')

def get_match_path(cfg):
    '''Returns where the match results folder is stored.'''
    return os.path.join(get_feature_path(cfg), get_match_name(cfg))

def get_match_file(cfg):
    '''Returns the path to the match file.'''

    return os.path.join(get_match_path(cfg), 'matches.h5')

def get_match_cost_file(cfg):
    '''Returns the path to the match file.'''

    return os.path.join(get_match_path(cfg), 'matching_cost.h5')

def get_geom_name(cfg):
    '''Return folder name for the geometry model.

    Converted to lower-case to avoid conflicts.'''

    geom = cfg.method_dict['config_{}_{}'.format(cfg.dataset,
                                                 cfg.task)]['geom']
    method = geom['method'].lower()

    # This entry is a temporary fix
    if method == 'cv2-patched-ransac-f':
        label = '_'.join([
            method, 'th',
            str(geom['threshold']),
            'conf', str(geom['confidence']),
            'maxiter', str(geom['max_iter'])
        ])
    elif method in ['cv2-ransac-e', 'cv2-ransac-f']:
        label = '_'.join([
            method,
            'th',
            str(geom['threshold']),
            'conf',
            str(geom['confidence']),
        ])
    elif method in ['cmp-degensac-f', 'cmp-degensac-f-laf', 'cmp-gc-ransac-e']:
        label = '_'.join([
            method, 'th',
            str(geom['threshold']), 'conf',
            str(geom['confidence']), 'max_iter',
            str(geom['max_iter']), 'error',
            str(geom['error_type']), 'degencheck',
            str(geom['degeneracy_check'])
        ])
    elif method in ['cmp-gc-ransac-f', 'skimage-ransac-f', 'cmp-magsac-f']:
        label = '_'.join([
            method, 'th',
            str(geom['threshold']), 'conf',
            str(geom['confidence']), 'max_iter',
            str(geom['max_iter'])
        ])
    elif method in ['cv2-lmeds-e', 'cv2-lmeds-f']:
        label = '_'.join([method, 'conf', str(geom['confidence'])])
    elif method in ['intel-dfe-f']:
        label = '_'.join([
            method, 'th',
            str(geom['threshold']), 'postprocess',
            str(geom['postprocess'])
        ])
    elif method in ['cv2-7pt', 'cv2-8pt']:
        label = method
    else:
        raise ValueError('Unknown method for E/F estimation')

    return label.lower()

def get_geom_path(cfg):
    '''Returns where the match results folder is stored.'''

    geom_name = get_geom_name(cfg)
    return os.path.join(get_filter_path(cfg), 'stereo-fold-{}'.format(cfg.run),
                        geom_name)

def get_geom_file(cfg):
    '''Returns the path to the match file.'''

    return os.path.join(get_geom_path(cfg), 'essential.h5')

def get_geom_inl_file(cfg):
    '''Returns the path to the match file.'''
    return os.path.join(get_geom_path(cfg), 'essential_inliers.h5')

def get_geom_cost_file(cfg):
    '''Returns the path to the geom cost file.'''
    return os.path.join(get_geom_path(cfg), 'geom_cost.h5')

def get_cne_temp_path(cfg):
    return os.path.join(get_filter_path(cfg), 'temp_cne')

def get_filter_match_file_for_computing_model(cfg):
    filter_match_file = os.path.join(get_filter_path(cfg),
        'matches_imported_stereo_{}.h5'.format(cfg.run))
    if os.path.isfile(filter_match_file):
        return filter_match_file
    else:
        return get_filter_match_file(cfg)

def get_filter_match_file(cfg):
    return os.path.join(get_filter_path(cfg), 'matches_inlier.h5')

def get_filter_cost_file(cfg):
    return os.path.join(get_filter_path(cfg), 'matches_inlier_cost.h5')

def get_cne_data_dump_path(cfg):
    return os.path.join(get_cne_temp_path(cfg), 'data_dump')

def get_stereo_path(cfg):
    '''Returns the path to where the stereo results are stored.'''

    return os.path.join(get_geom_path(cfg), 'set_{}'.format(cfg.num_max_set))

def get_stereo_pose_file(cfg, th=None):
    '''Returns the path to where the stereo pose file.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(get_stereo_path(cfg),
                        'stereo_pose_errors{}.h5'.format(label))

def get_repeatability_score_file(cfg, th=None):
    '''Returns the path to the repeatability file.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(get_stereo_path(cfg),
                        'repeatability_score_file{}.h5'.format(label))

def get_stereo_epipolar_pre_match_file(cfg, th=None):
    '''Returns the path to the match file.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(get_stereo_path(cfg),
                        'stereo_epipolar_pre_match_errors{}.h5'.format(label))

def get_stereo_epipolar_refined_match_file(cfg, th=None):
    '''Returns the path to the filtered match file.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(
        get_stereo_path(cfg),
        'stereo_epipolar_refined_match_errors{}.h5'.format(label))

def get_stereo_epipolar_final_match_file(cfg, th=None):
    '''Returns the path to the match file after RANSAC.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(
        get_stereo_path(cfg),
        'stereo_epipolar_final_match_errors{}.h5'.format(label))

def get_stereo_depth_projection_pre_match_file(cfg, th=None):
    '''Returns the path to the errors file for input matches.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(
        get_stereo_path(cfg),
        'stereo_projection_errors_pre_match{}.h5'.format(label))

def get_stereo_depth_projection_refined_match_file(cfg, th=None):
    '''Returns the path to the errors file for filtered matches.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(
        get_stereo_path(cfg),
        'stereo_projection_errors_refined_match{}.h5'.format(label))

def get_stereo_depth_projection_final_match_file(cfg, th=None):
    '''Returns the path to the errors file for final matches.'''

    label = '' if th is None else '-th-{:s}'.format(th)
    return os.path.join(
        get_stereo_path(cfg),
        'stereo_projection_errors_final_match{}.h5'.format(label))

def get_colmap_path(cfg):
    '''Returns the path to colmap results for individual bag.'''

    if cfg.colmap_mapper:
        return os.path.join(get_multiview_path(cfg), 'all')
    return os.path.join(get_multiview_path(cfg),
                        'bag_size_{}'.format(cfg.bag_size),
                        'bag_id_{:05d}'.format(cfg.bag_id))

def get_multiview_path(cfg):
    '''Returns the path to multiview folder.'''

    return os.path.join(get_filter_path(cfg),
                        'multiview-fold-{}'.format(cfg.run))

def get_colmap_mark_file(cfg):
    '''Returns the path to colmap flag.'''

    return os.path.join(get_colmap_path(cfg), 'colmap_has_run')

def get_colmap_pose_file(cfg):
    '''Returns the path to colmap pose files.'''

    return os.path.join(get_colmap_path(cfg), 'colmap_pose_errors.h5')

def get_colmap_output_path(cfg):
    '''Returns the path to colmap outputs.'''

    return os.path.join(get_colmap_path(cfg), 'colmap')

def get_colmap_temp_path(cfg):
    '''Returns the path to colmap working path.'''

    # TODO: Do we want to use slurm temp directory?
    return os.path.join(get_colmap_path(cfg), 'temp_colmap')

def parse_file_to_list(file_name, data_dir):
    '''
    Parses filenames from the given text file using the `data_dir`

    :param file_name: File with list of file names
    :param data_dir: Full path location appended to the filename

    :return: List of full paths to the file names
    '''

    fullpath_list = []
    with open(file_name, 'r') as f:
        while True:
            # Read a single line
            line = f.readline()
            # Check the `line` type
            if not isinstance(line, str):
                line = line.decode('utf-8')
            if not line:
                break
            # Strip `\n` at the end and append to the `fullpath_list`
            fullpath_list.append(os.path.join(data_dir, line.rstrip('\n')))
    return fullpath_list

def get_fullpath_list(data_dir, key):
    '''
    Returns the full-path lists to image info in `data_dir`

    :param data_dir: Path to the location of dataset
    :param key: Which item to retrieve from

    :return: Tuple containing fullpath lists for the key item
    '''
    # Read the list of images, homography and geometry
    list_file = os.path.join(data_dir, '{}.txt'.format(key))

    # Parse files to fullpath lists
    fullpath_list = parse_file_to_list(list_file, data_dir)

    return fullpath_list

def get_item_name_list(fullpath_list):
    '''Returns each item name in the full path list, excluding the extension'''

    return [os.path.splitext(os.path.basename(_s))[0] for _s in fullpath_list]

def get_stereo_viz_folder(cfg):
    '''Returns the path to the stereo visualizations folder.'''

    base = os.path.join(cfg.method_dict['config_common']['json_label'].lower(),
                        cfg.dataset, cfg.scene, 'stereo')

    return os.path.join(cfg.path_visualization, 'png', base), \
           os.path.join(cfg.path_visualization, 'jpg', base)

def get_colmap_viz_folder(cfg):
    '''Returns the path to the multiview visualizations folder.'''

    base = os.path.join(cfg.method_dict['config_common']['json_label'].lower(),
                        cfg.dataset, cfg.scene, 'multiview')

    return os.path.join(cfg.path_visualization, 'png', base), \
           os.path.join(cfg.path_visualization, 'jpg', base)

def get_stereo_viz_folder_debug(cfg):
    '''Returns the path to the stereo visualizations folder.'''

    base = os.path.join(cfg.method_dict['config_common']['json_label'].lower(),
                        cfg.dataset, cfg.scene, 'stereo')

    return os.path.join(cfg.path_visualization + '-debug', 'png', base), \
           os.path.join(cfg.path_visualization + '-debug', 'jpg', base)

def get_colmap_viz_folder_debug(cfg):
    '''Returns the path to the multiview visualizations folder.'''

    base = os.path.join(cfg.method_dict['config_common']['json_label'].lower(),
                        cfg.dataset, cfg.scene, 'multiview')

    return os.path.join(cfg.path_visualization, 'png', base), \
           os.path.join(cfg.path_visualization, 'jpg', base)

def get_pairs_per_threshold(data_dir):
    pairs = {}
    for th in np.arange(0, 1, 0.1):
        pairs['{:0.1f}'.format(th)] = np.load(
            '{}/new-vis-pairs/keys-th-{:0.1f}.npy'.format(data_dir, th))
    return pairs

max_stars_repo_name: keeeeenw/image-matching-benchmark

max_issues_repo_name: keeeeenw/image-matching-benchmark

max_forks_repo_name: keeeeenw/image-matching-benchmark