text:
"""
    Based on environment in PEARL:
    https://github.com/katerakelly/oyster/blob/master/rlkit/envs/humanoid_dir.py
"""
import numpy as np
from gym.envs.mujoco import HumanoidEnv as HumanoidEnv
from gym import spaces

import random

def mass_center(model, sim):
    mass = np.expand_dims(model.body_mass, 1)
    xpos = sim.data.xipos
    return (np.sum(mass * xpos, 0) / np.sum(mass))

class HumanoidDirEnv(HumanoidEnv):

    def __init__(self, task={}, max_episode_steps=200, n_tasks=2, randomize_tasks=True):
        self.set_task(self.sample_tasks(1)[0])
        self.task_dim = 1
        self._max_episode_steps = max_episode_steps
        self.action_scale = 1 # Mujoco environment initialization takes a step,
        super(HumanoidDirEnv, self).__init__()

        # Override action space to make it range from  (-1, 1)
        assert (self.action_space.low == -self.action_space.high).all()
        self.action_scale = self.action_space.high[0]
        self.action_space = spaces.Box(low=-1.0, high=1.0, shape=self.action_space.shape) # Overriding original action_space which is (-0.4, 0.4, shape = (17, ))

    def step(self, action):
        pos_before = np.copy(mass_center(self.model, self.sim)[:2])

        rescaled_action = action * self.action_scale # Scale the action from (-1, 1) to original.
        self.do_simulation(rescaled_action, self.frame_skip)
        pos_after = mass_center(self.model, self.sim)[:2]

        alive_bonus = 5.0
        data = self.sim.data
        goal_direction = (np.cos(self._goal), np.sin(self._goal))
        lin_vel_cost = 0.25 * np.sum(goal_direction * (pos_after - pos_before)) / self.model.opt.timestep
        quad_ctrl_cost = 0.1 * np.square(data.ctrl).sum()
        quad_impact_cost = .5e-6 * np.square(data.cfrc_ext).sum()
        quad_impact_cost = min(quad_impact_cost, 10)
        reward = lin_vel_cost - quad_ctrl_cost - quad_impact_cost + alive_bonus
        qpos = self.sim.data.qpos

        done = bool((qpos[2] < 1.0) or (qpos[2] > 2.0))
        # done = False

        return self._get_obs(), reward, done, dict(reward_linvel=lin_vel_cost,
                                                   reward_quadctrl=-quad_ctrl_cost,
                                                   reward_alive=alive_bonus,
                                                   reward_impact=-quad_impact_cost)

    def _get_obs(self):
        data = self.sim.data
        return np.concatenate([data.qpos.flat[2:],
                               data.qvel.flat,
                               data.cinert.flat,
                               data.cvel.flat,
                               data.qfrc_actuator.flat,
                               data.cfrc_ext.flat])

    # def get_all_task_idx(self):
    #     return range(len(self.tasks))

    # def reset_task(self, idx):
    #     self._task = self.tasks[idx]
    #     self._goal = self._task['goal'] # assume parameterization of task by single vector

    def reset_task(self, task=None):
        if task is None:
            task = self.sample_tasks(1)[0]
        self.set_task(task)

    def set_task(self, task):
        self._goal = task

    def get_task(self):
        return np.array([self._goal])

    def sample_tasks(self, num_tasks):
        return [random.uniform(0., 2.0 * np.pi) for _ in range(num_tasks)]

max_stars_repo_name: MetaMind/varibad

max_issues_repo_name: MetaMind/varibad

max_forks_repo_name: MetaMind/varibad


text:
import math
from pathlib import Path
from typing import Union

import torch
from matplotlib import pyplot as plt
import numpy as np

from am.segment.image_utils import save_rgb_image, normalize

def plot_loss_metric(history):
    fig, axes = plt.subplots(1, 2, figsize=(15, 5))
    xs = range(1, len(history.train_loss) + 1)

    axes[0].set_title('loss')
    axes[0].set_ylim(0, min(10, max(history.train_loss.max(), history.valid_loss.max()) * 1.1))
    axes[0].plot(xs, history.train_loss, c='g', label='train')
    axes[0].plot(xs, history.valid_loss, c='r', label='valid')
    axes[0].legend()

    axes[1].set_title('metric')
    axes[1].set_ylim(0,  min(10, max(history.train_metric.max(), history.valid_metric.max()) * 1.1))
    axes[1].plot(xs, history.train_metric, c='g', label='train')
    axes[1].plot(xs, history.valid_metric, c='r', label='valid')
    axes[1].legend()

def save_history_overlay(history, path):
    overlay_path = Path(path)
    overlay_path.mkdir(exist_ok=True)
    for i, overlay in enumerate(history.overlay, 1):
        save_rgb_image(np.array(overlay), overlay_path / f'{i:02}.png')

def plot_images_grid(images, titles=None, ncols=3):
    n = len(images)
    nrows = math.ceil(n / ncols)
    fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(15, nrows * 5))
    axes = axes.flatten()
    for i, image in enumerate(images):
        array = np.array(image)
        # if array.ndim > 2:
        #     array = array[:, :, 0]
        axes[i].imshow(array)
        if titles:
            axes[i].set_title(titles[i])
    plt.show()

def convert_to_image(array: Union[np.ndarray, torch.Tensor]) -> np.ndarray:
    if type(array) == torch.Tensor:
        array = array.detach().cpu().numpy()

    if array.ndim == 4:
        image = array[0]
    else:
        image = array

    if image.ndim == 3:
        if image.shape[0] == 1:
            image = image[0]
        else:
            image = np.moveaxis(image, 0, 2)

    return normalize(image)

def predict_plot_batch(model, inputs, targets):
    targets_pred = torch.sigmoid(model(inputs.to('cuda')))
    n = min(len(inputs), len(targets))
    for i in range(n):
        plot_images_grid([
            convert_to_image(targets[i]),
            convert_to_image(inputs[i]),
            # convert_to_image(targets_pred[i]),
            convert_to_image((targets_pred[i] > 0.5).to(torch.float32))
        ], titles=['Labels', 'Inputs', 'Mask'])

def predict_plot(model, dl, n=2):
    iterator = iter(dl)
    while n > 0:
        inputs, targets = next(iterator)
        batch_n = inputs.shape[0]
        if batch_n > n:
            inputs, targets = inputs[:n], targets[:n]

        predict_plot_batch(model, inputs, targets)
        n -= batch_n

def create_uniq_exp_group(dl):
    return set(map(tuple, dl.dataset.image_df[['experiment', 'group']].values))

ext: py

max_stars_repo_name: aeisenbarth/am-segmentation

max_issues_repo_name: aeisenbarth/am-segmentation

max_forks_repo_name: aeisenbarth/am-segmentation

reason: import numpy


text:
import numpy as np

# Triad over multi-dimensional arrays (N x 2 x 4 x 8)
# Array opeations (all in multi-dimensional context):
#  * vadd2 (x1)
#  * vmul2 (x1)
####
def MD_triad(MD1, MD2, MD3):
    R = np.add(MD1, np.multiply(MD2, MD3))


text:
import sys
import math

from copy import copy, deepcopy

import pytest
import numpy as np

import siquant.dimensions as dims

from siquant.exceptions import UnitMismatchError, ImmutabilityError
from siquant import make, converter, validator, are_of, si, SIUnit

def test_q_copying():
    a = 100 * si.meters
    b = copy(a)
    c = deepcopy(a)

    assert a is not b
    assert a is not c
    assert a == b
    assert a == c

    class Ref:
        def __init__(self, value):
            self.value = value

        def __eq__(self, other):
            return self.value == other.value

    ref = Ref(100)
    ref_ref = Ref(Ref(100))

    rq = make(ref, si.meters)
    rc = copy(rq)

    assert rq == rc
    assert rq is not rc

    rqq = make(ref_ref, si.meters)
    rqc = copy(rqq)
    rqd = deepcopy(rqq)

    assert rqq is not rqc
    assert rqq == rqc

    ref_ref.value.value = 50
    assert rqq == rqc
    assert rqq != rqd

def test_q_immutability():
    mass = 100 * si.kilograms
    with pytest.raises(ImmutabilityError):
        mass.quantity = 10
    with pytest.raises(ImmutabilityError):
        del mass.quantity

def test_q_np():
    distances = make(np.array([1, 2, 3, 4, 5, 6, 7]), si.meters)
    distances *= 2

    value = distances.get_as(si.meters)
    assert np.array_equal(value, np.array([2, 4, 6, 8, 10, 12, 14]))

    assert distances[0].approx(2 * si.meters)
    assert distances[6].approx(14 * si.meters)

    assert min(distances).approx(2 * si.meters)
    assert max(distances).approx(14 * si.meters)

def test_q_round_to():
    distance = 1.23456789 * si.meters

    assert 1235 == distance.round_to(si.millimeters).quantity

    assert 1234.6 == distance.round_to(si.millimeters, 1).quantity

def test_q_approx():

    dist1 = 1.234567890 * si.meters
    dist2 = 1.234567 * si.meters

    assert dist1.approx(dist2)
    assert dist1.approx(dist2, atol=1 * si.millimeters)

    assert dist1.abs_approx(dist2)

    with pytest.raises(UnitMismatchError):
        dist1.approx(dist2, atol=1 * si.milligrams)

    assert not dist1.approx(1.23456789 * si.kilograms)

    dist1 = 1234567890 * si.meters
    dist2 = 1234567891 * si.meters

    assert dist1.approx(dist2)
    assert dist1.rel_approx(dist2)
    assert not dist1.approx(dist2, rtol=0)
    assert not dist1.abs_approx(dist2)

    assert dist1.approx(dist2, rtol=0, atol=1 * si.meters)

@pytest.mark.skipif(
    sys.version_info < (3, 5), reason="operators.matmul introduced in py35"
)
def test_q_matmul():
    from operator import matmul

    distances = make(np.array([1, 2, 3]), si.meters)
    value = matmul(distances, distances)
    assert value.get_as(si.meters ** 2) == 14

    value = matmul(distances, np.array([1, 2, 3]))
    assert value.get_as(si.meters ** 1) == 14

    with pytest.raises(TypeError):
        # numpy really should just return NotImplemented instead of throwing a TypeError
        value = matmul(np.array([1, 2, 3]), distances)
        assert value.get_as(si.meters ** 1) == 14

def test_are_of_validator():
    dim_check = validator(dims.speed_t)

    assert dim_check(1 * si.meters / si.seconds)
    assert dim_check(1 * si.meters / si.seconds, 2 * si.meters / si.seconds)
    assert not dim_check(1 * si.meters)
    assert not dim_check(1)
    assert not dim_check(None)

    with pytest.raises(TypeError):
        validator([1, 2, 3, 4, 5, 6, 7])

    with pytest.raises(ValueError):
        validator((1, 2, 3))

def test_are_of():
    assert are_of(dims.distance_t, 1 * si.meters, 2 * si.millimeters, 3 * si.kilometers)
    assert not are_of(dims.angle_t, 1, 2, 3)
    assert not are_of(dims.distance_t, 1 * si.meters, 1 * si.kilograms)

def test_create_and_extract():
    mass = make(100, SIUnit.Unit(kg=1))
    acceleration = make(1, SIUnit.Unit(m=1, s=-2))

    force = 100 * si.newtons

    assert force.quantity == 100
    assert force.get_as(si.kilonewtons) == 0.1
    assert force == mass * acceleration

def test_hash():
    angle1 = 2 * math.pi * si.radians
    angle2 = 360 * si.degrees

    assert hash(angle1) == hash(angle2)

    circumference = angle1 * 1 * si.meters
    assert hash(circumference) != hash(angle1)

def test_q_compatible():
    dist1 = 1 * si.meters
    dist2 = 1 * si.millimeters
    area = 1 * si.meters ** 2
    assert dist1.compatible(dist2)
    assert dist2.compatible(dist1)
    assert not dist1.compatible(area)
    assert not area.compatible(dist1)

    with pytest.raises(TypeError):
        dist = 1 * si.meters
        dist.compatible(si.meters)

def test_q_eq_ne():
    dist1 = 1 * si.meters
    dist2 = 1000 * si.millimeters
    dist3 = 1 * si.kilometers
    area = 1 * si.meters ** 2

    assert dist1 == dist2
    assert dist2 == dist1

    assert dist1 != dist3
    assert dist3 != dist1

    assert not area == dist1
    assert not dist1 == area

    assert not dist1 == si.meters
    assert not si.meters == dist1

    assert area != dist1
    assert dist1 != area

    assert dist1 != si.meters
    assert si.meters != dist1

def test_q_ordering():
    dist1 = 1 * si.meters
    dist2 = 1000 * si.millimeters
    dist3 = 1 * si.kilometers
    dist4 = 1 * si.millimeters
    area = 1 * si.meters ** 2

    assert dist1 >= dist2
    assert dist2 <= dist1
    assert dist1 <= dist2
    assert dist2 >= dist1

    assert dist3 >= dist1
    assert dist1 <= dist3
    assert dist3 > dist1
    assert dist1 < dist3

    assert dist4 <= dist1
    assert dist1 >= dist4
    assert dist4 < dist1
    assert dist1 > dist4

    with pytest.raises(UnitMismatchError):
        dist1 > area
    with pytest.raises(UnitMismatchError):
        dist1 >= area
    with pytest.raises(UnitMismatchError):
        dist1 < area
    with pytest.raises(UnitMismatchError):
        dist1 <= area

    with pytest.raises(TypeError):
        dist1 > 1
    with pytest.raises(TypeError):
        dist1 >= 1
    with pytest.raises(TypeError):
        dist1 < 1
    with pytest.raises(TypeError):
        dist1 <= 1

def test_q_float():
    assert pytest.approx(1) == math.cos(0 * si.radians)
    assert pytest.approx(-1) == math.cos(math.pi * si.radians)
    assert pytest.approx(0) == math.cos(math.pi / 2 * si.radians)
    assert pytest.approx(0) == math.cos(math.pi * 3 / 2 * si.radians)

def test_q_truth():
    dist_true = 1 * si.meters
    dist_false = 0 * si.meters
    assert dist_true
    assert not dist_false

def test_q_abs():
    assert abs(1 * si.meters) == 1 * si.meters
    assert abs(-1 * si.meters) == 1 * si.meters

def test_q_invert():
    dist = 1000 * si.millimeters
    assert ~dist == 1 / 1000 / si.millimeters

def test_q_neg():
    dist = 1000 * si.millimeters
    assert -dist == -1000 * si.millimeters

def test_q_pow():
    dist = 1000 * si.millimeters
    assert dist ** 2 == 1 * si.meters ** 2
    assert dist ** -1 == 1 * si.meters ** -1

    dist = 100 * si.millimeters ** 2
    assert dist ** 0.5 == 10 * si.millimeters

    with pytest.raises(TypeError):
        dist ** dist
    with pytest.raises(TypeError):
        dist ** si.meters

def test_q_mul():
    dist = 1000 * si.millimeters
    assert 2 * dist == 2000 * si.millimeters
    assert dist * 2 == 2000 * si.millimeters
    assert dist * dist == 1000 * 1000 * si.millimeters ** 2

    dist *= 2
    assert dist == 2000 * si.millimeters

def test_q_div():
    dist = 1000 * si.millimeters
    assert 2 / dist == 1 / 500 / si.millimeters
    assert dist / 2 == 500 * si.millimeters
    assert dist / dist == 1 * si.unity

    assert dist / si.millimeters == 1000 * si.unity
    assert si.millimeters / dist == 1 / 1000 * si.unity

    with pytest.raises(TypeError):
        dist / (1, 2)
    with pytest.raises(TypeError):
        (1, 2) / dist

def test_dimensionality():
    dist = 1000 * si.millimeters
    assert dist.is_of(dims.distance_t)
    assert not dist.is_of(dims.area_t)

def test_q_add():
    dist1 = 1 * si.meters
    dist2 = 1000 * si.millimeters
    area = dist1 * dist2

    diff1 = dist1 + dist2
    diff2 = dist2 + dist1

    assert dist1 == dist1 + 0
    assert dist1 == 0 + dist1

    assert diff1 == 2 * si.meters
    assert diff1.units == si.millimeters

    assert diff2 == 2 * si.meters
    assert diff2.units == si.millimeters

    with pytest.raises(TypeError):
        dist1 + si.meters
    with pytest.raises(TypeError):
        si.meters + dist1

    with pytest.raises(UnitMismatchError):
        dist1 + area
    with pytest.raises(UnitMismatchError):
        area + dist1

    dist1 += dist2
    assert dist1 == 2 * si.meters

def test_q_sub():
    dist1 = 1 * si.meters
    dist2 = 1000 * si.millimeters
    area = dist1 * dist2

    assert dist1 == dist1 - 0
    assert -dist1 == 0 - dist1

    diff1 = dist1 - dist2
    diff2 = dist2 - dist1

    assert diff1 == 0 * si.meters
    assert diff1.units == si.millimeters

    assert diff2 == 0 * si.meters
    assert diff2.units == si.millimeters

    with pytest.raises(TypeError):
        dist1 - si.meters
    with pytest.raises(TypeError):
        si.meters - dist1

    with pytest.raises(UnitMismatchError):
        dist1 - area
    with pytest.raises(UnitMismatchError):
        area - dist1

    dist1 -= dist2
    assert dist1 == 0 * si.meters

def test_q_converters():
    cvtr = converter(si.meters)
    assert cvtr(1000) == 1000 * si.meters
    assert cvtr(1000 * si.meters) == 1000 * si.meters
    assert cvtr(1000 * si.millimeters) == 1 * si.meters

    with pytest.raises(UnitMismatchError):
        cvtr(1000 * si.kilograms)

max_stars_repo_name: keystonetowersystems/siquant

max_issues_repo_name: keystonetowersystems/siquant

max_forks_repo_name: keystonetowersystems/siquant


text:
""" calc_vheight.py
   ==============
   Author: S. Chakraborty
   This file is a v_Height estimator
"""
import numpy as np

def calculate_vHeight(r, elv, hop, Re = 6371.0):
    """
    Parameters
    ----------
    r: slant range in km
    elv: elevation angle in degree
    hop: back scatter hop (0.5,1,1.5, etc.)
    Re: Earth radius in km
    """

    h = np.sqrt(Re**2 + (r/(2*hop))**2 + (r/hop)*Re*np.sin(np.radians(elv))) - Re

    return h


text:
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import glob
import os
import joblib

# Just use 1 GPU
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import numpy as np
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import os
from io import BytesIO
import base64

from pyvirchow.io.operations import path_leaf
from pyvirchow.io.operations import draw_annotation
from pyvirchow.io.operations import WSIReader
from pyvirchow.deep_model.plot_utils import plot_blend
from skimage.io import imread

TRAIN_TUMOR_DIR = (
    "/Z/personal-folders/interns/saket/histopath_data/CAMELYON16/training/tumor"
)
TRAIN_NORMAL_DIR = (
    "/Z/personal-folders/interns/saket/histopath_data/CAMELYON16/training/normal"
)
TRAIN_ANNOTATION_DIR = "/Z/personal-folders/interns/saket/histopath_data/CAMELYON16/training/lesion_annotations_json"
TEST_WSI_DIR = (
    "/Z/personal-folders/interns/saket/histopath_data/CAMELYON16/testing/images"
)
TEST_ANNOTATION_DIR = "/Z/personal-folders/interns/saket/histopath_data/CAMELYON16/testing/lesion_annotations_json"
NOT_AVAIL_IMG = "/Z/personal-folders/interns/saket/github/pyvirchow/pyvirchow/web/assets/No_image_available.png"
PREDICTED_HEATMAP_DIR = {
    "cnn": "/Z/personal-folders/interns/saket/github/pyvirchow/data/wsi_heatmap_sgd",
    "rf": "/Z/personal-folders/interns/saket/github/pyvirchow/data/wsi_heatmap_rf",
}
PATCH_SIZE = 256

def fig_to_uri(in_fig, close_all=True, **save_args):
    # type: (plt.Figure) -> str
    """Credits: https://github.com/4QuantOSS/DashIntro/blob/master/notebooks/Tutorial.ipynb
    Save a figure as a URI
    :param in_fig:
    :return:
    """
    out_img = BytesIO()
    in_fig.savefig(out_img, format="png", **save_args)
    if close_all:
        in_fig.clf()
        plt.close("all")
    out_img.seek(0)  # rewind file
    encoded = base64.b64encode(out_img.read()).decode("ascii").replace("\n", "")
    return "data:image/png;base64,{}".format(encoded)

TUMOR_VALIDATE_SLIDES = [
    "tumor_005",
    "tumor_011",
    "tumor_031",
    "tumor_046",
    "tumor_065",
    "tumor_069",
    "tumor_079",
    "tumor_085",
    "tumor_097",
]

NORMAL_VALIDATE_SLIDES = [
    "normal_016",
    "normal_020",
    "normal_030",
    "normal_046",
    "normal_057",
    "normal_092",
    "normal_097",
    "normal_098",
    "normal_100",
    "normal_130",
    "normal_136",
    "normal_142",
    "normal_159",
]

def get_samples_from_dir(dir):
    # Just assume all files will have tif extension
    wsis = glob.glob(os.path.join(dir, "*.tif"))
    short_names = []
    for wsi in wsis:
        label = path_leaf(wsi).replace(".tif", "")
        # if label in TUMOR_VALIDATE_SLIDES or label in NORMAL_VALIDATE_SLIDES:
        short_names.append({"label": label, "value": wsi})
    return short_names

ALL_SAMPLES = (
    get_samples_from_dir(TRAIN_TUMOR_DIR)
    + get_samples_from_dir(TRAIN_NORMAL_DIR)
    + get_samples_from_dir(TEST_WSI_DIR)
)

app = dash.Dash()
app.layout = html.Div(
    [
        html.Div(
            [
                dcc.Dropdown(
                    id="wsi-dropdown", options=ALL_SAMPLES, value="Select a sample"
                )
            ],
            style={"width": "20%", "display": "block"},
        ),
        html.Div(
            [html.Img(id="thumbnail_plot_truth", src="", width="100%", height="100%")],
            id="plot_div_truth",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
        html.Div(
            [html.Div(id="embed")],
            id="plot_div_iframe",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
        html.Div(
            [html.Img(id="thumbnail_plot_cnn", src="", width="100%", height="100%")],
            id="plot_div_cnn",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
        html.Div(
            [html.Img(id="thumbnail_plot_rf", src="", width="100%", height="100%")],
            id="plot_div_rf",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
        html.Div(
            [
                html.Img(
                    id="thumbnail_plot_cnn_mask", src="", width="100%", height="100%"
                )
            ],
            id="plot_div_cnn_mask",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
        html.Div(
            [
                html.Img(
                    id="thumbnail_plot_rf_mask", src="", width="100%", height="100%"
                )
            ],
            id="plot_div_rf_mask",
            style={"width": "45%", "display": "inline-block", "float": "left"},
        ),
    ]
)

@app.callback(
    Output(component_id="thumbnail_plot_truth", component_property="src"),
    [Input("wsi-dropdown", "value")],
)
def update_output_truth(slide_path):
    slide = WSIReader(slide_path, 40)
    uid = slide.uid

    n_cols = int(slide.width / 256)
    n_rows = int(slide.height / 256)

    thumbnail = slide.get_thumbnail((n_cols, n_rows))
    thumbnail = np.array(thumbnail)
    fig, ax = plt.subplots()
    ax.imshow(thumbnail)
    if "tumor" in uid:
        # Load annotation
        json_filepath = os.path.join(TRAIN_ANNOTATION_DIR, uid + ".json")
        draw_annotation(json_filepath, 0, 0, 1 / 256, ax)
    elif "test" in uid:
        # Load annotation
        json_filepath = os.path.join(TEST_ANNOTATION_DIR, uid + ".json")
        if os.path.isfile(json_filepath):
            draw_annotation(json_filepath, 0, 0, 1 / 256, ax)
    # ax.axis('off')
    ax.set_title("Ground truth (Manual annotation)")
    fig.tight_layout()
    out_url = fig_to_uri(fig)
    plt.close("all")
    return out_url

@app.callback(Output("embed", "children"), [Input("wsi-dropdown", "value")])
def update_output_iframe(slide_path):
    slide = WSIReader(slide_path, 40)
    uid = slide.uid
    if "tumor" in uid:
        # These servers are being run through deepzoom_multiserver.py script
        # available as part of examples of openslide-python
        port = 5000
    elif "normal" in uid:
        port = 5001
    elif "test" in uid:
        port = 5002
    return html.Iframe(
        src="http://192.168.221.21:{}/{}.tif".format(port, uid),
        width="100%",
        height="480px;",
    )

@app.callback(
    Output(component_id="thumbnail_plot_cnn", component_property="src"),
    [Input("wsi-dropdown", "value")],
)
def update_output_cnn(slide_path):
    uid = path_leaf(slide_path).replace(".tif", "")
    pickle_file = os.path.join(PREDICTED_HEATMAP_DIR["cnn"], uid + ".joblib.pickle")
    slide = WSIReader(slide_path, 40)
    n_cols = int(slide.width / 256)
    n_rows = int(slide.height / 256)

    thumbnail = slide.get_thumbnail((n_cols, n_rows))
    thumbnail = np.array(thumbnail)
    if os.path.isfile(pickle_file):
        thumbnail_predicted = joblib.load(pickle_file)
        fig, ax = plt.subplots()
        plot_blend(thumbnail, thumbnail_predicted, ax, alpha=1)
        # ax.axis('off')
        ax.set_title("CNN - Predicted Heatmap")
    else:
        fig, ax = plt.subplots()
        thumbnail = imread(NOT_AVAIL_IMG)
        ax.imshow(thumbnail)
        ax.axis("off")

        # ax.axis('off')
    fig.tight_layout()
    slide.close()
    out_url = fig_to_uri(fig)
    plt.close("all")
    return out_url

@app.callback(
    Output(component_id="thumbnail_plot_rf", component_property="src"),
    [Input("wsi-dropdown", "value")],
)
def update_output_rf(slide_path):
    uid = path_leaf(slide_path).replace(".tif", "")
    pickle_file = os.path.join(PREDICTED_HEATMAP_DIR["rf"], uid + ".joblib.pickle")
    slide = WSIReader(slide_path, 40)
    n_cols = int(slide.width / 256)
    n_rows = int(slide.height / 256)

    thumbnail = slide.get_thumbnail((n_cols, n_rows))
    thumbnail = np.array(thumbnail)
    if os.path.isfile(pickle_file):
        thumbnail_predicted = joblib.load(pickle_file)
        fig, ax = plt.subplots()
        plot_blend(thumbnail, thumbnail_predicted, ax, alpha=1)
        # ax.axis('off')
        ax.set_title("RF - Predicted Heatmap")
    else:
        fig, ax = plt.subplots()
        thumbnail = imread(NOT_AVAIL_IMG)
        ax.imshow(thumbnail)
        ax.axis("off")

        # ax.axis('off')
    fig.tight_layout()
    slide.close()
    out_url = fig_to_uri(fig)
    plt.close("all")
    return out_url

@app.callback(
    Output(component_id="thumbnail_plot_cnn_mask", component_property="src"),
    [Input("wsi-dropdown", "value")],
)
def update_output_cnn_mask(slide_path):
    uid = path_leaf(slide_path).replace(".tif", "")
    pickle_file = os.path.join(PREDICTED_HEATMAP_DIR["cnn"], uid + ".joblib.pickle")
    if os.path.isfile(pickle_file):
        thumbnail_predicted = joblib.load(pickle_file)
        fig, ax = plt.subplots()
        ax.imshow(
            (thumbnail_predicted > 0.75).astype(np.int), cmap="gray", vmin=0, vmax=1
        )
        # ax.set_title(' (white=tumor, black=not_tumor)')
        # ax.axis('off')
        ax.set_title("CNN - Predicted Mask\n (white=tumor, black=normal)")
    else:
        fig, ax = plt.subplots()
        thumbnail = imread(NOT_AVAIL_IMG)
        ax.imshow(thumbnail)
        ax.axis("off")
    fig.tight_layout()
    out_url = fig_to_uri(fig)
    plt.close("all")
    return out_url

@app.callback(
    Output(component_id="thumbnail_plot_rf_mask", component_property="src"),
    [Input("wsi-dropdown", "value")],
)
def update_output_rf_mask(slide_path):
    uid = path_leaf(slide_path).replace(".tif", "")
    pickle_file = os.path.join(PREDICTED_HEATMAP_DIR["rf"], uid + ".joblib.pickle")
    if os.path.isfile(pickle_file):
        thumbnail_predicted = joblib.load(pickle_file)
        fig, ax = plt.subplots()
        ax.imshow(
            (thumbnail_predicted > 0.75).astype(np.int), cmap="gray", vmin=0, vmax=1
        )
        # ax.set_title(' (white=tumor, black=not_tumor)')
        # ax.axis('off')
        ax.set_title("RF - Predicted Mask \n (white=tumor, black=normal)")
    else:
        fig, ax = plt.subplots()
        thumbnail = imread(NOT_AVAIL_IMG)
        ax.imshow(thumbnail)
        ax.axis("off")
    fig.tight_layout()
    out_url = fig_to_uri(fig)
    plt.close("all")
    return out_url

if __name__ == "__main__":
    app.title = "Virchow: Classification of Histopathology Images"
    app.run_server(host="192.168.221.21", debug=True)

max_stars_repo_name: saketkc/pyvirchow

max_issues_repo_name: saketkc/pyvirchow

max_forks_repo_name: saketkc/pyvirchow


text:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""SHERIFS
Seismic Hazard and Earthquake Rates In Fault Systems

Version 1.0

@author: thomas
"""
import os
import numpy as np
import matplotlib.pyplot as plt

def box_plot(data_for_boxplot,label_for_boxplot,title_for_boxplot,path_for_boxplot):

    data = data_for_boxplot
    label = label_for_boxplot

    fs = 9  # fontsize

    plt.boxplot(data, labels=label, showmeans=True)
    plt.title(title_for_boxplot, fontsize=fs)
    plt.grid()
    plt.savefig(path_for_boxplot,dpi = 180, transparent=True)
    #plt.show()
    plt.close()

def moment_rate(Run_name,plot_moment_rate,geologic_moment_rate_no_as,geologic_moment_rate,
                seismological_moment_rate,scenarios_names_list,total_list_scenario_name,
                MFD_type_list,total_list_MFD_type):

    '''#############################
    ###############################
    #     comparison of the moment rate
    #   geologic, geodetic, sismologic
    ###############################
    ##############################'''
    if plot_moment_rate == True:
        name_list = ['Potential moment rate\nof the fault system',
        'Modelled moment rate\n(NMS) taken into account',
        'Moment rate calculated\nfrom the earthquake catalog']
        data_for_boxplot = []
        label_for_boxplot = name_list
        data_for_boxplot.append(geologic_moment_rate_no_as)
        data_for_boxplot.append(geologic_moment_rate)
        data_for_boxplot.append(seismological_moment_rate)
#            data_for_boxplot.append(geodetic_moment_rate)
#            data_for_boxplot.append(geodetic_moment_rate_as)

        if not os.path.exists(str(Run_name) + '/analysis/figures/compare_moment_rate'):
            os.makedirs(str(Run_name) + '/analysis/figures/compare_moment_rate')

        path_for_boxplot = str(Run_name) + '/analysis/figures/compare_moment_rate/M0.png'
        title_for_boxplot = 'Compare M0'
        box_plot(data_for_boxplot,label_for_boxplot,title_for_boxplot,path_for_boxplot)

    '''####################################
    #plot moment rate for each set of rupture scenario used
    #######################################'''
    if plot_moment_rate == True:
        data_for_boxplot = []
        label_for_boxplot = []
        for scenario_name in scenarios_names_list :
            label_for_boxplot.append(scenario_name)
            label_for_boxplot.append(scenario_name + '_no_NMS')

            M0_scenario_name = []
            rows = np.where(np.array(total_list_scenario_name) == scenario_name)[0]
            M0_no_as_scenario_name = []
            for index in rows :
                M0_scenario_name.append(geologic_moment_rate[index])
                M0_no_as_scenario_name.append(geologic_moment_rate_no_as[index])
            data_for_boxplot.append(M0_scenario_name)
            data_for_boxplot.append(M0_no_as_scenario_name)

        path_for_boxplot = str(Run_name) + '/analysis/figures/compare_moment_rate/M0_each_model_sce_rup.png'
        title_for_boxplot = 'Compare M0 - each model \nof rupture scenario'
        box_plot(data_for_boxplot,label_for_boxplot,title_for_boxplot,path_for_boxplot)

    '''####################################
    #plot moment rate for each shape of MFD
    #######################################'''
    if plot_moment_rate == True:
        data_for_boxplot = []
        label_for_boxplot = []
        for mfd in MFD_type_list :
            label_for_boxplot.append(mfd)
            label_for_boxplot.append(mfd + '_no_NMS')

            M0_mfd = []
            rows = np.where(np.array(total_list_MFD_type) == mfd)[0]
            M0_no_as_mfd = []
            for index in rows :
                M0_mfd.append(geologic_moment_rate[index])
                M0_no_as_mfd.append(geologic_moment_rate_no_as[index])
            data_for_boxplot.append(M0_mfd)
            data_for_boxplot.append(M0_no_as_mfd)

        path_for_boxplot = str(Run_name) + '/analysis/figures/compare_moment_rate/M0_each_mfd.png'
        title_for_boxplot = 'Compare M0 - each model \nof MFD'
        box_plot(data_for_boxplot,label_for_boxplot,title_for_boxplot,path_for_boxplot)


text:
#%%
import typing as tp
import math

import numpy as np
from matplotlib import pyplot as plt

from core import utils

files = utils.Paths.gen_files()
file = next(files)

image = utils.parse_image(file.image)

h, w, c = image.shape

# https://stackoverflow.com/questions/2827393/angles-between-two-n-dimensional-vectors-in-python
def imshow_array(arr: np.ndarray, points: tp.Iterable[utils.Point] = ()) -> None:
    fig, ax = plt.subplots(1)
    ax.imshow(arr)
    for point in points:
        circ = plt.Circle((point.x, point.y), 30)
        ax.add_patch(circ)

eyes = utils.Point.to_np(file.points[:2])
desired = np.array([[206, 306], [256, 256]])
x = np.linalg.solve(eyes, desired)

def unit_vector(vector):
    """Returns the unit vector of the vector."""
    return vector / np.linalg.norm(vector)

def angle_between(v1, v2):
    """Returns the angle in radians between vectors 'v1' and 'v2'::

    >>> angle_between((1, 0, 0), (0, 1, 0))
    1.5707963267948966
    >>> angle_between((1, 0, 0), (1, 0, 0))
    0.0
    >>> angle_between((1, 0, 0), (-1, 0, 0))
    3.141592653589793
    """
    v1_u = unit_vector(v1)
    v2_u = unit_vector(v2)
    return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))

# https://en.wikipedia.org/wiki/Rotation_matrix
def rotate(points: tp.Iterable[utils.Point], theta: float):
    rotation_matrix = np.array(
        [[math.cos(theta), -math.sin(theta)], [math.sin(theta), -math.cos()]
    )

"""
    eye1 eye2
x [[500, 245],
y  [603, 435]])

maybe we want 512x512 images, x1 y1

so we want maybe
x [[206, 306],
y  [256, 256]])
"""

# %%

max_stars_repo_name: gettodaze/eigen-neko

max_issues_repo_name: gettodaze/eigen-neko

max_forks_repo_name: gettodaze/eigen-neko


text:
"""
The 'dwtcoeffs' module contains predefined wavelets, as well as the classes necessary to
create more user-defined wavelets.

Wavelets are defined by the Wavelet class. A Wavelet object mainly consists of four Filter
objects (defined by the Filter class) representing the decomposition and reconstruction
low pass and high pass filters.

Examples:
    You can define your own wavelet by creating four filters, and combining them to a wavelet:

    >>> decomp_lp = Filter([1 / np.sqrt(2), 1 / np.sqrt(2)], 0)
    >>> decomp_hp = Filter([1 / np.sqrt(2), -1 / np.sqrt(2)], 1)
    >>> recon_lp = Filter([1 / np.sqrt(2), 1 / np.sqrt(2)], 0)
    >>> recon_hp = Filter([-1 / np.sqrt(2), 1 / np.sqrt(2)], 1)
    >>> haar = Wavelet(decomp_lp, decomp_hp, recon_lp, recon_hp)

"""

import numpy as np
import tensorflow as tf
from tfwavelets.utils import adapt_filter, to_tf_mat

class Filter:
    """
    Class representing a filter.

    Attributes:
        coeffs (tf.constant):      Filter coefficients
        zero (int):                Origin of filter (which index of coeffs array is
                                   actually indexed as 0).
        edge_matrices (iterable):  List of edge matrices, used for circular convolution.
                                   Stored as 3D TF tensors (constants).
    """

    def __init__(self, coeffs, zero, dtype=tf.float32):
        """
        Create a filter based on given filter coefficients

        Args:
            coeffs (np.ndarray):       Filter coefficients
            zero (int):                Origin of filter (which index of coeffs array is
                                       actually indexed as 0).
        """
        self.dtype=dtype;
        if dtype == tf.float32:
            self.npdtype = np.float32;
        elif dtype == tf.float64:
            self.npdtype = np.float64;

        self.coeffs = tf.constant(adapt_filter(coeffs), dtype=self.dtype)

        if not isinstance(coeffs, np.ndarray):
            coeffs = np.array(self.coeffs)
        self._coeffs = coeffs.astype(self.npdtype)

        self.zero = zero

        self.edge_matrices = to_tf_mat(self._edge_matrices(), dtype=self.dtype)

    def __getitem__(self, item):
        """
        Returns filter coefficients at requested indeces. Indeces are offset by the filter
        origin

        Args:
            item (int or slice):    Item(s) to get

        Returns:
            np.ndarray: Item(s) at specified place(s)
        """
        if isinstance(item, slice):
            return self._coeffs.__getitem__(
                slice(item.start + self.zero, item.stop + self.zero, item.step)
            )
        else:
            return self._coeffs.__getitem__(item + self.zero)

    def num_pos(self):
        """
        Number of positive indexed coefficients in filter, including the origin. Ie,
        strictly speaking it's the number of non-negative indexed coefficients.

        Returns:
            int: Number of positive indexed coefficients in filter.
        """
        return len(self._coeffs) - self.zero

    def num_neg(self):
        """
        Number of negative indexed coefficients, excluding the origin.

        Returns:
            int: Number of negative indexed coefficients
        """
        return self.zero

    def _edge_matrices(self):
        """Computes the submatrices needed at the ends for circular convolution.

        Returns:
            Tuple of 2d-arrays, (top-left, top-right, bottom-left, bottom-right).
        """
        if not isinstance(self._coeffs, np.ndarray):
            self._coeffs = np.array(self._coeffs)

        n, = self._coeffs.shape
        self._coeffs = self._coeffs[::-1]

        # Some padding is necesssary to keep the submatrices
        # from having having columns in common
        padding = max((self.zero, n - self.zero - 1))
        matrix_size = n + padding
        filter_matrix = np.zeros((matrix_size, matrix_size), dtype=self.npdtype)
        negative = self._coeffs[
                   -(self.zero + 1):]  # negative indexed filter coeffs (and 0)
        positive = self._coeffs[
                   :-(self.zero + 1)]  # filter coeffs with strictly positive indeces

        # Insert first row
        filter_matrix[0, :len(negative)] = negative

        # Because -0 == 0, a length of 0 makes it impossible to broadcast
        # (nor is is necessary)
        if len(positive) > 0:
            filter_matrix[0, -len(positive):] = positive

        # Cycle previous row to compute the entire filter matrix
        for i in range(1, matrix_size):
            filter_matrix[i, :] = np.roll(filter_matrix[i - 1, :], 1)

        # TODO: Indexing not thoroughly tested
        num_pos = len(positive)
        num_neg = len(negative)
        top_left = filter_matrix[:num_pos, :(num_pos + num_neg - 1)]
        top_right = filter_matrix[:num_pos, -num_pos:]
        bottom_left = filter_matrix[-num_neg + 1:, :num_neg - 1]
        bottom_right = filter_matrix[-num_neg + 1:, -(num_pos + num_neg - 1):]

        # Indexing wrong when there are no negative indexed coefficients
        if num_neg == 1:
            bottom_left = np.zeros((0, 0), dtype=self.npdtype)
            bottom_right = np.zeros((0, 0), dtype=self.npdtype)

        return top_left, top_right, bottom_left, bottom_right

class TrainableFilter(Filter):
    """
    Class representing a trainable filter.

    Attributes:
        coeffs (tf.Variable):      Filter coefficients
        zero (int):                Origin of filter (which index of coeffs array is
                                   actually indexed as 0).
    """

    def __init__(self, initial_coeffs, zero, dtype=tf.float32, name=None):
        """
        Create a trainable filter initialized with given filter coefficients

        Args:
            initial_coeffs (np.ndarray):    Initial filter coefficients
            zero (int):                     Origin of filter (which index of coeffs array
                                            is actually indexed as 0).
            name (str):                     Optional. Name of tf variable created to hold
                                            the filter coeffs.
        """
        super().__init__(initial_coeffs, zero, dtype=dtype)

        self.coeffs = tf.Variable(
            initial_value=adapt_filter(initial_coeffs),
            trainable=True,
            name=name,
            dtype=dtype,
            constraint=tf.keras.constraints.max_norm(np.sqrt(2), [1, 2])
        )

        # Erase stuff that will be invalid once the filter coeffs has changed
        self._coeffs = [None]*len(self._coeffs)
        self.edge_matrices = None

class Wavelet:
    """
    Class representing a wavelet.

    Attributes:
        decomp_lp (Filter):    Filter coefficients for decomposition low pass filter
        decomp_hp (Filter):    Filter coefficients for decomposition high pass filter
        recon_lp (Filter):     Filter coefficients for reconstruction low pass filter
        recon_hp (Filter):     Filter coefficients for reconstruction high pass filter
    """

    def __init__(self, decomp_lp, decomp_hp, recon_lp, recon_hp):
        """
        Create a new wavelet based on specified filters

        Args:
            decomp_lp (Filter):    Filter coefficients for decomposition low pass filter
            decomp_hp (Filter):    Filter coefficients for decomposition high pass filter
            recon_lp (Filter):     Filter coefficients for reconstruction low pass filter
            recon_hp (Filter):     Filter coefficients for reconstruction high pass filter
        """
        self.decomp_lp = decomp_lp
        self.decomp_hp = decomp_hp
        self.recon_lp = recon_lp
        self.recon_hp = recon_hp

class TrainableWavelet(Wavelet):
    """
    Class representing a trainable wavelet

    Attributes:
        decomp_lp (TrainableFilter):    Filter coefficients for decomposition low pass filter
        decomp_hp (TrainableFilter):    Filter coefficients for decomposition high pass filter
        recon_lp (TrainableFilter):     Filter coefficients for reconstruction low pass filter
        recon_hp (TrainableFilter):     Filter coefficients for reconstruction high pass filter
    """

    def __init__(self, wavelet):
        """
        Create a new trainable wavelet initialized as specified wavelet

        Args:
            wavelet (Wavelet):          Starting point for the trainable wavelet
        """
        super().__init__(
            TrainableFilter(wavelet.decomp_lp._coeffs, wavelet.decomp_lp.zero),
            TrainableFilter(wavelet.decomp_hp._coeffs, wavelet.decomp_hp.zero),
            TrainableFilter(wavelet.recon_lp._coeffs, wavelet.recon_lp.zero),
            TrainableFilter(wavelet.recon_hp._coeffs, wavelet.recon_hp.zero)
        )

# Haar wavelet
haar = Wavelet(
    Filter(np.array([0.70710677, 0.70710677]), 1),
    Filter(np.array([-0.70710677, 0.70710677]), 0),
    Filter(np.array([0.70710677, 0.70710677]), 0),
    Filter(np.array([0.70710677, -0.70710677]), 1),
)

# Daubechies wavelets
db1 = haar
db2 = Wavelet(
    Filter(np.array([-0.12940952255092145,
                     0.22414386804185735,
                     0.836516303737469,
                     0.48296291314469025]), 3),
    Filter(np.array([-0.48296291314469025,
                     0.836516303737469,
                     -0.22414386804185735,
                     -0.12940952255092145]), 0),
    Filter(np.array([0.48296291314469025,
                     0.836516303737469,
                     0.22414386804185735,
                     -0.12940952255092145]), 0),
    Filter(np.array([-0.12940952255092145,
                     -0.22414386804185735,
                     0.836516303737469,
                     -0.48296291314469025]), 3)
)
db3 = Wavelet(
    Filter(np.array([0.035226291882100656,
                    -0.08544127388224149,
                    -0.13501102001039084,
                    0.4598775021193313,
                    0.8068915093133388,
                    0.3326705529509569]), 5),
    Filter(np.array([-0.3326705529509569,
                    0.8068915093133388,
                    -0.4598775021193313,
                    -0.13501102001039084,
                    0.08544127388224149,
                    0.035226291882100656]), 0),
    Filter(np.array([0.3326705529509569,
                    0.8068915093133388,
                    0.4598775021193313,
                    -0.13501102001039084,
                    -0.08544127388224149,
                    0.035226291882100656]), 0),
    Filter(np.array([0.035226291882100656,
                    0.08544127388224149,
                    -0.13501102001039084,
                    -0.4598775021193313,
                    0.8068915093133388,
                    -0.3326705529509569]), 5)
)
db4 = Wavelet(
    Filter(np.array([-0.010597401784997278,
                    0.032883011666982945,
                    0.030841381835986965,
                    -0.18703481171888114,
                    -0.02798376941698385,
                    0.6308807679295904,
                    0.7148465705525415,
                    0.23037781330885523]), 7),
    Filter(np.array([-0.23037781330885523,
                    0.7148465705525415,
                    -0.6308807679295904,
                    -0.02798376941698385,
                    0.18703481171888114,
                    0.030841381835986965,
                    -0.032883011666982945,
                    -0.010597401784997278]), 0),
    Filter(np.array([0.23037781330885523,
                    0.7148465705525415,
                    0.6308807679295904,
                    -0.02798376941698385,
                    -0.18703481171888114,
                    0.030841381835986965,
                    0.032883011666982945,
                    -0.010597401784997278]), 0),
    Filter(np.array([-0.010597401784997278,
                    -0.032883011666982945,
                    0.030841381835986965,
                    0.18703481171888114,
                    -0.02798376941698385,
                    -0.6308807679295904,
                    0.7148465705525415,
                    -0.23037781330885523]), 7)
)

def get_wavelet(wavelet_name, dtype=tf.float32):
    """
    Get a wavelet based on the wavelets name.

    Args:
        wavelet_name (str): Name of the wavelet ('haar', 'db1', 'db2', 'db3' or 'db4').

    Returns:
        A wavelet object. If the wavelet name is not recognized, it returns None.
    """
    wname = wavelet_name.lower()
    if wname == 'db1' or wname == 'haar':
        # Haar wavelet
        a = 1/np.sqrt(2);
        haar = Wavelet(
                       Filter(np.array([a,  a]), 1, dtype=dtype),
                       Filter(np.array([-a, a]), 0, dtype=dtype),
                       Filter(np.array([a,  a]), 0, dtype=dtype),
                       Filter(np.array([a, -a]), 1, dtype=dtype),
                        )
        return haar
    elif wname == 'db2':
        db2 = Wavelet(
            Filter(np.array([-0.12940952255092145,
                             0.22414386804185735,
                             0.836516303737469,
                             0.48296291314469025]), 3, dtype=dtype),
            Filter(np.array([-0.48296291314469025,
                             0.836516303737469,
                             -0.22414386804185735,
                             -0.12940952255092145]), 0, dtype=dtype),
            Filter(np.array([0.48296291314469025,
                             0.836516303737469,
                             0.22414386804185735,
                             -0.12940952255092145]), 0, dtype=dtype),
            Filter(np.array([-0.12940952255092145,
                             -0.22414386804185735,
                             0.836516303737469,
                             -0.48296291314469025]), 3, dtype=dtype)
        )
        return db2
    elif wname == 'db3':
        db3 = Wavelet(
            Filter(np.array([0.035226291882100656,
                            -0.08544127388224149,
                            -0.13501102001039084,
                            0.4598775021193313,
                            0.8068915093133388,
                            0.3326705529509569]), 5, dtype=dtype),
            Filter(np.array([-0.3326705529509569,
                            0.8068915093133388,
                            -0.4598775021193313,
                            -0.13501102001039084,
                            0.08544127388224149,
                            0.035226291882100656]), 0, dtype=dtype),
            Filter(np.array([0.3326705529509569,
                            0.8068915093133388,
                            0.4598775021193313,
                            -0.13501102001039084,
                            -0.08544127388224149,
                            0.035226291882100656]), 0, dtype=dtype),
            Filter(np.array([0.035226291882100656,
                            0.08544127388224149,
                            -0.13501102001039084,
                            -0.4598775021193313,
                            0.8068915093133388,
                            -0.3326705529509569]), 5, dtype=dtype)
        )
        return db3
    elif wname == 'db4':
        db4 = Wavelet(
            Filter(np.array([-0.010597401784997278,
                            0.032883011666982945,
                            0.030841381835986965,
                            -0.18703481171888114,
                            -0.02798376941698385,
                            0.6308807679295904,
                            0.7148465705525415,
                            0.23037781330885523]), 7, dtype=dtype),
            Filter(np.array([-0.23037781330885523,
                            0.7148465705525415,
                            -0.6308807679295904,
                            -0.02798376941698385,
                            0.18703481171888114,
                            0.030841381835986965,
                            -0.032883011666982945,
                            -0.010597401784997278]), 0, dtype=dtype),
            Filter(np.array([0.23037781330885523,
                            0.7148465705525415,
                            0.6308807679295904,
                            -0.02798376941698385,
                            -0.18703481171888114,
                            0.030841381835986965,
                            0.032883011666982945,
                            -0.010597401784997278]), 0, dtype=dtype),
            Filter(np.array([-0.010597401784997278,
                            -0.032883011666982945,
                            0.030841381835986965,
                            0.18703481171888114,
                            -0.02798376941698385,
                            -0.6308807679295904,
                            0.7148465705525415,
                            -0.23037781330885523]), 7, dtype=dtype)
        )
        return db4
    else:
        return None

max_stars_repo_name: UiO-CS/tf-wavelets

max_issues_repo_name: comp-imaging-sci/pic-recon

max_forks_repo_name: UiO-CS/tf-wavelets


text:
import unittest
import numpy as np
import netCDF4 as nc
from ncagg.aggregator import initialize_aggregation_file
import os
import tempfile
from ncagg.config import Config

class TestFileInitialization(unittest.TestCase):
    def setUp(self):
        _, self.filename = tempfile.mkstemp()

    def tearDown(self):
        os.remove(self.filename)

    def test_initialize_basic(self):
        """Ensure aggregation file is created with proper dimensions according to the config."""
        config = Config.from_dict(
            {
                "dimensions": [{"name": "x", "size": None}, {"name": "y", "size": 10}],
                "variables": [
                    {"name": "x", "dimensions": ["x", "y"], "datatype": "int8"}
                ],
                "global attributes": [],
            }
        )
        initialize_aggregation_file(config, self.filename)
        with nc.Dataset(self.filename) as nc_check:
            self.assertEqual(len(nc_check.dimensions), 2)
            self.assertEqual(nc_check.dimensions["y"].size, 10)
            self.assertFalse(nc_check.dimensions["y"].isunlimited())
            self.assertTrue(nc_check.dimensions["x"].isunlimited())

    def test_initialize_several_variables(self):
        """Ensure aggregation file is created correctly according to the variable config."""
        config = Config.from_dict(
            {
                "dimensions": [{"name": "x", "size": None}, {"name": "y", "size": 10}],
                "variables": [
                    {
                        "name": "foo",
                        "dimensions": ["x", "y"],
                        "datatype": "float32",
                        "attributes": {"units": "seconds"},
                    },
                    {
                        "name": "foo_x",
                        "dimensions": ["x"],
                        "datatype": "float64",
                        "attributes": {"units": "floops", "created_by": "the flooper"},
                    },
                ],
                "global attributes": [],
            }
        )
        initialize_aggregation_file(config, self.filename)
        with nc.Dataset(self.filename) as nc_check:
            self.assertEqual(len(nc_check.variables), 2)
            self.assertEqual(nc_check.variables["foo"].dimensions, ("x", "y"))
            self.assertEqual(nc_check.variables["foo"].datatype, np.dtype(np.float32))
            self.assertEqual(nc_check.variables["foo"].units, "seconds")
            self.assertEqual(nc_check.variables["foo_x"].dimensions, ("x",))
            self.assertEqual(nc_check.variables["foo_x"].datatype, np.dtype(np.float64))
            self.assertEqual(nc_check.variables["foo_x"].units, "floops")
            self.assertEqual(
                nc_check.variables["foo_x"].getncattr("created_by"), "the flooper"
            )

    def test_initialize_with_list_attribute(self):
        """Ensure aggregation file is created with proper dimensions according to the config."""
        config = Config.from_dict(
            {
                "dimensions": [{"name": "x", "size": None}, {"name": "y", "size": 10}],
                "variables": [
                    {
                        "name": "x",
                        "dimensions": ["x", "y"],
                        "datatype": "int8",
                        "attributes": {"valid_range": [0, 10]},
                    }
                ],
                "global attributes": [],
            }
        )
        initialize_aggregation_file(config, self.filename)
        with nc.Dataset(self.filename) as nc_check:
            self.assertEqual(len(nc_check.dimensions), 2)
            self.assertEqual(nc_check.variables["x"].valid_range[0], 0)
            self.assertEqual(nc_check.variables["x"].valid_range[1], 10)


text:
__author__ = 'marko'

import numpy as np
from random import randint
from skimage.feature import hessian_matrix
from skimage.morphology import disk
from skimage.filters.rank import entropy
from preprocess import Preprocess
import cv2

class ImageSample(object):
    '''Image wrapper class that is used for samples extraction from images'''

    def __init__(self, img=None, path=None, block_size=5):
        if path and not img:
            img = cv2.imread(path)

        #img = Preprocess().blur_image(img)
        self.block_size = block_size
        self.img_rgb = img.copy()
        self.img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
        #self.img_ycbcr = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
        self.img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        self.height, self.width, _ = img.shape
        self.Hxx, self.Hxy, self.Hyy = hessian_matrix(self.img_gray)
        #vector, self.hog = hog(self.img_gray, orientations=8, pixels_per_cell=(3, 3),
        #            cells_per_block=(1, 1), visualise=True)

        neighours = disk(25)
        self.entropy = entropy(self.img_gray, neighours)

    def to_samples(self, n='all'):
        """
        Extract samples from image.
        :param n: number of samples per image
        :return: list of samples
        """
        samples = []
        add_sample = lambda fts: samples.append(fts) if fts else None

        if n == 'all':
            for i in xrange(self.height):
                for j in xrange(self.width):
                    add_sample(self._get_features(i, j))
        else:
            cnt = 0
            while len(samples) < n:
                i, j = randint(0, self.height-1), randint(0, self.width-1)
                add_sample(self._get_features(i, j))

        return samples

    def _get_features(self, i, j):
        """
        Calculates vector of features for pixel given with i,j coordinate
        :param i:
        :param j:
        :return:
        """
        features = []
        margin = self.block_size/2

        for ii in xrange(i-margin, i+margin):
            for jj in xrange(j-margin, j+margin):
                if ii < 0 or jj < 0 or ii >= self.height or jj >= self.width:
                    return None
                else:
                    features.extend(self.img_rgb[ii, jj])
                    features.extend(self.img_hsv[ii, jj])
                    #features.extend(self.img_ycbcr[ii, jj])

                    features.append(self.Hxx[ii, jj])
                    features.append(self.Hxy[ii, jj])
                    features.append(self.Hyy[ii, jj])
                    features.append(self.entropy[ii, jj])

                    #features.append(self.hog[ii, jj])

        return features

    # def find_vegetation(self, classifier):
    #     """
    #     Extract vegetation mask from image using given classifier
    #     :param classifier:
    #     :return:
    #     """
    #     result = np.zeros((self.height, self.width))
    #     samples = []
    #     ij = []
    #
    #     for i in xrange(self.height):
    #         for j in xrange(self.width):
    #             fts = self._get_features(i, j)
    #             if fts:
    #                 samples.append(fts)
    #                 ij.append((i, j))
    #
    #     classified = classifier.predict(samples)
    #
    #     for point, label in zip(ij, classified):
    #         result[point] = label
    #
    #     return result
    def find_vegetation(self, classifier):
        """
        Extract vegetation mask from image using given classifier
        :param classifier:
        :return:
        """
        result = np.zeros((self.height, self.width))
        samples = []
        ij = []
        limit = 20000

        for i in xrange(self.height):
            for j in xrange(self.width):
                fts = self._get_features(i, j)
                if fts:
                    samples.append(fts)
                    ij.append((i, j))

                if len(samples) > limit:
                    classified = classifier.predict(samples)
                    for point, label in zip(ij, classified):
                        result[point] = label

                    samples = []
                    ij = []

        classified = classifier.predict(samples)
        for point, label in zip(ij, classified):
            result[point] = label

        samples = []
        ij = []
        return result

    def find_vegetation_slow(self, classifier):
        """
        Extract vegetation mask from image using given classifier
        :param classifier:
        :return:
        """
        result = np.zeros((self.height, self.width))
        for i in xrange(self.height):
            for j in xrange(self.width):
                fts = self._get_features(i, j)
                if fts:
                    result[i, j] = classifier.predict(fts)

        return result


text:
##Fringe_cGAN Code written by Hanxun Jin
##Please cite: Jin, H., Clifton, R. J., & Kim, K. S. (2021). Dynamic fracture of a bicontinuously nanostructured copolymer: A deep learning analysis of big-data-generating experiment. arXiv preprint arXiv:2112.01971.

import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.initializers import RandomNormal
from tensorflow.keras.models import Model
from tensorflow.keras import Input
from tensorflow.keras.layers import Conv2D,Conv2DTranspose,UpSampling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Concatenate
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.utils import plot_model
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
import numpy as np
from numpy import asarray
import glob
from random import randint
import random
import datetime

def plot_results(images, epoch, label, n_cols=None):
    '''visualizes fake images'''

    n_cols = n_cols or len(images)
    n_rows = (len(images) - 1) // n_cols + 1

    if images.shape[-1] == 1:
        images = np.squeeze(images, axis=-1)

    plt.figure(figsize=(n_cols, n_rows))

    filename = 'Epoch_'+str(epoch)+ 'label_'+str(label)

    for index, image in enumerate(images):
        plt.subplot(n_rows, n_cols, index + 1)
        plt.imshow(image, cmap='gray')
        plt.axis("off")

    plt.suptitle('Epoch: '+str(epoch))
    plt.savefig('./cGAN_output/' + filename +'.png', bbox_inches='tight',dpi=100)
    plt.close()

# load full fringe images
img_size =128
newsize=(img_size,img_size)
images =  [asarray(Image.open(file).resize(newsize)) for file in sorted(glob.glob("./FEM961/fringes_961_PU_Nocor/*.png"))]
images_array_label = asarray(images)

images_array_label= (images_array_label.reshape(-1, img_size, img_size, 1))/255.0

# load missed fringe images
newsize=(img_size,img_size)
images =  [asarray(Image.open(file).resize(newsize)) for file in sorted(glob.glob("./FEM961/fringes_961_PU_cor_half_new/*.png"))]
images_array_input = asarray(images)

images_array_input= (images_array_input.reshape(-1, img_size, img_size, 1))/255.0
#check one img
#plt.imshow(images_array_input[500],cmap='gray')

#prepare the dataset
#dataset_norm = [images_array_input, images_array_label]

X_train, X_test, y_train, y_test = train_test_split(images_array_input, images_array_label, test_size=0.1)

#train dataset
dataset_a=tf.data.Dataset.from_tensor_slices(X_train) # miss fringe image
dataset_b=tf.data.Dataset.from_tensor_slices(y_train) # full fringe image

zip_dataset=tf.data.Dataset.zip((dataset_b,dataset_a))

BATCH_SIZE = 32
train_dataset = zip_dataset.shuffle(961).batch(BATCH_SIZE, drop_remainder=True).prefetch(1)

#test dataset
dataset_a=tf.data.Dataset.from_tensor_slices(X_test) # miss fringe image
dataset_b=tf.data.Dataset.from_tensor_slices(y_test) # full fringe image

zip_dataset=tf.data.Dataset.zip((dataset_b,dataset_a))

BATCH_SIZE = 32
test_dataset = zip_dataset.shuffle(961).batch(BATCH_SIZE, drop_remainder=True).prefetch(1)

def build_generator():
        """U-Net Generator"""

        def conv2d(layer_input, filters, f_size=4, bn=True):
            """Layers used during downsampling"""
            d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
            d = LeakyReLU(alpha=0.2)(d)
            if bn:
                d = BatchNormalization(momentum=0.8)(d)
            return d

        def deconv2d(layer_input, skip_input, filters, f_size=4, dropout_rate=0):
            """Layers used during upsampling"""
            u = UpSampling2D(size=2)(layer_input)
            u = Conv2D(filters, kernel_size=f_size, strides=1, padding='same', activation='relu')(u)
            if dropout_rate:
                u = Dropout(dropout_rate)(u)
            u = BatchNormalization(momentum=0.8)(u)
            u = Concatenate()([u, skip_input]) #skip connection
            return u

        d0 = Input(shape=img_shape)

        # Downsampling
        d1 = conv2d(d0, gf, bn=False)
        d2 = conv2d(d1, gf*2)
        d3 = conv2d(d2, gf*4)
        d4 = conv2d(d3, gf*8)
        d5 = conv2d(d4, gf*8)
        d6 = conv2d(d5, gf*8)
        d7 = conv2d(d6, gf*8)

        # Upsampling
        u1 = deconv2d(d7, d6, gf*8)
        u2 = deconv2d(u1, d5, gf*8)
        u3 = deconv2d(u2, d4, gf*8)
        u4 = deconv2d(u3, d3, gf*4)
        u5 = deconv2d(u4, d2, gf*2)
        u6 = deconv2d(u5, d1, gf)

        u7 = UpSampling2D(size=2)(u6)
        output_img = Conv2D(channels, kernel_size=4, strides=1, padding='same', activation='tanh')(u7)

        return Model(d0, output_img)

def build_discriminator():
        # a small function to make one layer of the discriminator
        def d_layer(layer_input, filters, f_size=4, bn=True):
            """Discriminator layer"""
            d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
            d = LeakyReLU(alpha=0.2)(d)
            if bn:
                d = BatchNormalization(momentum=0.8)(d)
            return d

        img_A = Input(shape=img_shape)
        img_B = Input(shape=img_shape)

        # Concatenate image and conditioning image by channels to produce input
        combined_imgs = Concatenate(axis=-1)([img_A, img_B])

        d1 = d_layer(combined_imgs, df, bn=False)
        d2 = d_layer(d1, df*2)
        d3 = d_layer(d2, df*4)
        d4 = d_layer(d3, df*8)

        validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(d4)

        return Model([img_A, img_B], validity)

# Input shape
img_rows = 128
img_cols = 128
channels = 1
img_shape = (img_rows, img_cols, channels)

# Calculate output shape of D (PatchGAN)
patch = int(img_rows / 2**4)
disc_patch = (patch, patch, 1)

# Number of filters in the first layer of G and D
gf = 64
df = 64

optimizer = Adam(0.0002, 0.5)

# Build and compile the discriminator
discriminator = build_discriminator()
discriminator.compile(loss='mse',
            optimizer=optimizer,
            metrics=['accuracy'])

# Build the generator
generator = build_generator()

# Input images and their conditioning images
img_A = Input(shape=img_shape)
img_B = Input(shape=img_shape)

# By conditioning on B generate a fake version of A
fake_A = generator(img_B)

# For the combined model we will only train the generator
discriminator.trainable = False

# Discriminators determines validity of translated images / condition pairs
valid = discriminator([fake_A, img_B])

combined = Model(inputs=[img_A, img_B], outputs=[valid, fake_A])
combined.compile(loss=['mse', 'mae'],
                              loss_weights=[1, 100],
                              optimizer=optimizer)

def train(train_dataset,test_dataset, epochs, batch_size=1, show_interval=1):

        start_time = datetime.datetime.now()

        # Adversarial loss ground truths
        valid = np.ones((batch_size,) + disc_patch)
        fake = np.zeros((batch_size,) + disc_patch)

        norm_train_epoch =[]
        norm_test_epoch =[]

        for epoch in range(epochs):
            for batch_i, (imgs_A, imgs_B) in enumerate(train_dataset):

                #  Train Discriminator

                # Condition on B and generate a translated version
                fake_A = generator.predict(imgs_B)

                # Train the discriminators (original images = real / generated = Fake)
                d_loss_real = discriminator.train_on_batch([imgs_A, imgs_B], valid)
                d_loss_fake = discriminator.train_on_batch([fake_A, imgs_B], fake)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

                #  Train Generator
                g_loss = combined.train_on_batch([imgs_A, imgs_B], [valid, imgs_A])

                elapsed_time = datetime.datetime.now() - start_time

                # Plot the progress

                # If at show interval => show generated image samples

            #train performance
            norm_train_list = []
            for batch_i, (imgs_A_tr, imgs_B_tr) in enumerate(train_dataset):

                fake_train = generator.predict(imgs_B_tr)
				norm_train_list.append(tf.keras.losses.mean_squared_error(imgs_A_tr, fake_train))
                #norm_train_list.append (tf.norm(fake_train-imgs_A_tr, ord='euclidean')) #L2 norm

            #test performance
            norm_test_list = []
            for batch_i, (imgs_A_te, imgs_B_te) in enumerate(test_dataset):

                fake_test = generator.predict(imgs_B_te)
				norm_test_list.append(tf.keras.losses.mean_squared_error(imgs_A_te, fake_test))
                #norm_test_list.append (tf.norm(fake_test-imgs_A_te, ord='euclidean')) #L2 norm

            cur_norm_train = np.mean(np.array(norm_train_list))
            cur_norm_test = np.mean(np.array(norm_test_list))

            norm_train_epoch.append(cur_norm_train)
            norm_test_epoch.append(cur_norm_test)

            if epoch % show_interval == 0:
                plot_results(fake_A, epoch+1, 'Gen',8)
                plot_results(imgs_A, epoch+1, 'True', 8)
                plot_results(imgs_B, epoch+1, 'Miss', 8)

            #epoch, Dloss, Gloss, train L2 norm, test L2 norm
            with open('epoch_L2norm.txt', 'a+') as f:
                f.write('%d, %f, %3d%%, %f, %f, %f\n' % (epoch+1,d_loss[0], 100*d_loss[1], g_loss[0], cur_norm_train,cur_norm_test ))

            print ("[Epoch %d/%d]  [D loss: %f, acc: %3d%%] [G loss: %f] [train L2 error: %f] [test L2 error: %f] time: %s" % (epoch+1, epochs,

                                                                         d_loss[0], 100*d_loss[1],
                                                                         g_loss[0], cur_norm_train,cur_norm_test,
                                                                         elapsed_time))
        return norm_train_epoch, norm_test_epoch

norm_train_epoch, norm_test_epoch = train(train_dataset,test_dataset, epochs=500, batch_size=32, show_interval=1)

generator.save('saved_model/my_cGANmodel_half')

max_stars_repo_name: hanxunjin/Fringe-cGAN

max_issues_repo_name: hanxunjin/Fringe-cGAN

max_forks_repo_name: hanxunjin/Fringe-cGAN

reason: import numpy,from numpy


text:
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['font.size'] = 18.0
mpl.rcParams['font.weight'] = 'bold'
from astropy.io import ascii
from astropy.table import join
import glob
import os,shutil
from datetime import datetime,timedelta
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
try:
    from fancy_plot import fancy_plot
    fp = True
except:
    print 'Fancy plot not in your python path please add'
    fp = False

class cms2_plot:

    def __init__(self,time,cmsdir='',outdir='%Y/%m/%d/%H%M/',fit_file='fit_information.dat',lop_file='fit_loops.dat'):
        """Creates plots of helicity vs free energy and Poloidal vs Axial flux"""
        if cmsdir == '': cmsdir = open('cms2_dir','r').readlines()[0][:-1]#read in first line of cms2_dir and format to send to script
        if cmsdir[-1] != '/': cmsdir=cmsdir+'/'
        self.time = time
        self.form = "%Y/%m/%d %H:%M:%S"
        self.time = datetime.strptime(self.time,self.form)
        if outdir[-1] != '/': outdir = outdir+'/'
        self.bdir = datetime.strftime(self.time,outdir)
        self.cdir = cmsdir
        self.ifit = fit_file
        self.loop = lop_file

    def read_model(self):
        """Reads the formatted model file to extract axial and poloidal fluxes"""

        axi = []
        pol = []
        #read the file name for each model
        for i in self.tdat['mod_nam']:
            mod_num = i.split('_')[0] #get modelN
            mod_pat = mod_num+'_path' #path file

            fil = open(self.cdir+self.bdir+mod_pat,'r').readlines()
            pol.append(fil[1].split()[-1]) #get polodial flux
            axi.append(fil[-3].split()[-1]) #get axial flux

        #add axial and poloidal flux to main table
        self.tdat['axi'] = np.array(axi).astype('float')
        self.tdat['pol'] = np.array(pol).astype('float')

        #sort in order of polodial then axial fluxes
        self.tdat.sort(['pol','axi'])

        #set up plotting order based on pol and axi fluxes
        self.tdat['plot_id'] = np.arange(self.tdat['pol'].size)

    def read_infit(self):
        """Read the best fit model file"""
        from astropy.io import ascii
        self.fdat = ascii.read(self.cdir+self.bdir+self.ifit)
        self.ldat = ascii.read(self.cdir+self.bdir+self.loop)
        self.tdat = join(self.fdat,self.ldat,keys=['mod_nam'])

        self.comp_tsig()
        self.read_model()
        #remove some bad coronal loops

#use model id for abbicus
        self.tdat['mod_id'] = [int(i.split('_')[0].replace('model','')) for i in self.tdat['mod_nam']]

    def comp_tsig(self):
        """Add total sigma to table"""
        cols = self.tdat.columns.keys() #list of columns in table
        bfit = [i for i in cols if "bfit" in i] #All the bfit columns
        self.sind = int(bfit[0].split('_')[1])#start index for loops
        self.eind = int(bfit[-1].split('_')[1])#end index for loops
        self.tind = self.eind-self.sind+1 #total number of coronal loops

        #fitting stat values
        fsta = [i for i in bfit if ((("_5" in i) & ("_5_" not in i)) | ("_5_5" in i))]

        #normalize minimums
        for i in fsta: self.tdat[i] = self.tdat[i]/self.tdat[i][self.tdat[i] > 0.].min()

        #init new column
        self.tdat['bfit_t_5'] = 0.0
        self.tdat['bfit_s_5'] = 0.0
        for i in fsta: self.tdat['bfit_t_5'] = self.tdat[i]+self.tdat['bfit_t_5']#total uncertainty
        for i in fsta: self.tdat['bfit_s_5'] = self.tdat[i]+self.tdat['bfit_s_5']**2.#sq. total uncertainty
        self.tdat['bfit_a_5'] = self.tdat['bfit_t_5']/self.tind #average
        self.tdat['bfit_s_5'] = (self.tdat['bfit_s_5'])**.5/self.tind #sum squared average

        #uncertainty in average
        self.tdat['bfit_u_5'] = 0.0
        for i in fsta: self.tdat['bfit_u_5'] = (self.tdat[i]-self.tdat['bfit_a_5'])**2.+self.tdat['bfit_u_5']
        self.tdat['bfit_u_5'] = np.sqrt(self.tdat['bfit_u_5'])/float(self.tind)

    def create_plots(self):
        """Create plots using the best fit data"""
        self.read_infit()

        self.figt,self.axt = plt.subplots(ncols=3,nrows=1,gridspec_kw={'width_ratios':[50,50,10]},figsize=(8,4))
        #self.figt.subplots_adjust(wspace=0.6)
        self.figi,self.axi = plt.subplots(nrows=2,ncols=2,sharey=True)
        #model number plots
        self.figm,self.axm = plt.subplots()

#        self.grid = gridspec.GridSpec(1,3,width_ratios=[10,10,1],height_ratios=[1])
        self.axt = [i for i in self.axt.ravel()]
        self.axi = [i for i in self.axi.ravel()]

#xvalues for 4 figure plot
        xfigs = ['axi','pol','free_energy','helicity']
        names = {'axi': ['Axial','Mx'],'pol': ['Poloidal','Mx/cm'], 'free_energy' : ['Free Energy','ergs'], 'helicity': ['Helicity','Mx$^2$']}

#set color for eruptions and non eruptive states
        self.tdat['color'] = 'black'
        self.tdat['color'][self.tdat['eruption'] == 'Yes'] = 'red'

        use = self.tdat['bfit_a_5'] > 0 #reject filled values
        print self.tdat['bfit_a_5'][use].max(),self.tdat['bfit_a_5'][use].min()
#plot 4 panel plot
        ccolor = plt.cm.jet #colors for cycling
        ocolor = ccolor(np.linspace(0,1,self.tind))
        for i in np.arange(self.tind)+self.sind:
        #plot as a function of model number
            self.axm.scatter(self.tdat['plot_id'][use].astype('float'),self.tdat['bfit_{0:1d}_5'.format(i)][use].astype('float'),c=ocolor[i],label='{0:1d}'.format(i))
            for k,j in enumerate(self.axi): j.scatter(self.tdat[xfigs[k]][use].astype('float'),self.tdat['bfit_{0:1d}_5'.format(i)][use].astype('float'),c=ocolor[i],label='{0:1d}'.format(i))

        for k,j in enumerate(self.axi):
            j.set_xlabel('{0} [{1}]'.format(*names[xfigs[k]]))
            j.set_ylabel('Normed Quad. Mean Dis.')
        self.axi[0].legend(loc='upper left',frameon=False,fontsize=4)
        #self.pol[0].set_ylbot[-3.5,-2.])
        self.axi[0].set_yscale('log')
        self.axi[0].set_xscale('log')
        self.axi[1].set_xscale('log')
        #self.axi[0].set_ylim([0.0001,0.01])

#model fit parameter fit labels
        self.axm.scatter(self.tdat['plot_id'][use],self.tdat['bfit_a_5'][use].astype('float'),marker='x',c='black',label='Ave.')
        self.axm.errorbar(self.tdat['plot_id'][use],self.tdat['bfit_a_5'][use].astype('float'),yerr=self.tdat['bfit_u_5'][use],fmt='x',c='black',label=None,markersize=1)
        self.axm.set_ylabel('Normed Quad. Mean Dis.')
        self.axm.set_xlabel('Axial Flux [Mx]/Poloidal Flux [Mx/cm]')
        self.axm.set_yscale('log')
        #set scale of tick labels so they fit nicely on the plot 2018/08/27 J. Prchlik
        self.axm.tick_params(axis='both', which='both', labelsize=10)

#set up nested plot labels
#for the value closer to the axis (axial flux)
        xticks_top = [i for i in self.tdat['plot_id'][use]]
        xticks_val = ['{0:3.1e}'.format(float(i)) for i in self.tdat['axi'][use]]
#create vertical alignment  array
        xticks_vrt = [0]*len(xticks_top)

#for the values farther from the axis (axial flux)
        #unique poloidal fluxes
        u_pol = np.unique(self.tdat['pol'][use])
        #save starting point ending point and median
        xticks_bot_st = []
        xticks_bot_nd = []
        #find first instance of axial flux value
        #store values in lists
        for i in u_pol:
            tix_p, = np.where(self.tdat['pol'] == i)
            xticks_bot_st.append(np.min(self.tdat['plot_id'][tix_p])-0.75)
            xticks_bot_nd.append(np.max(self.tdat['plot_id'][tix_p])+0.75)
            #use the starting and end points to create a mid point for the ticks
            xticks_top.append(round((int(xticks_bot_nd[-1])-int(xticks_bot_st[-1]))/2.+int(xticks_bot_st[-1])+.5))
            xticks_val.append('{0:3.1e}'.format(float(i)))
            #offset vertical plotting point
            xticks_vrt.append(-.15)

        #sort by tick value
        sorter = np.argsort(xticks_top)
        xticks_top = np.array(xticks_top)[sorter]
        xticks_val = np.array(xticks_val)[sorter]
        xticks_vrt = np.array(xticks_vrt)[sorter]

        #create xticks with new labals
        self.axm.set_xticks(xticks_top)
        self.axm.set_xticks(xticks_bot_st,minor=True)
        self.axm.set_xticklabels(xticks_val)

        #adjust vertical offset
        for j,i in zip(self.axm.get_xticklabels(),xticks_vrt):
            j.set_y(i)
            if i > -.02: j.set_rotation(90)

        #make long bars for minor ticks
        self.axm.tick_params(axis='x',which='minor',direction='out',length=55.001)

#3D plot
        vmin =  0.0015
        vmax =  0.0025
        vmin =  1.00
        vmax =  2.0
        cmap =  plt.cm.Greys
        cmap =  plt.cm.Blues
        cax =self.axt[0].scatter(self.tdat['axi'][use].astype('float'),self.tdat['pol'][use].astype('float'),c=self.tdat['bfit_a_5'][use],cmap=cmap,vmin=vmin,vmax=vmax,edgecolor=self.tdat['color'][use])
        self.axt[1].scatter(self.tdat['free_energy'][use],self.tdat['helicity'][use],c=self.tdat['bfit_a_5'][use],cmap=cmap,vmin=vmin,vmax=vmax,edgecolor=self.tdat['color'][use])

        #set labels
        self.axt[0].set_xlabel('Axial Flux [Mx]')
        self.axt[0].set_ylabel('Poloidal Flux [Mx/cm]')
        self.axt[0].set_yscale('log')
        self.axt[0].set_xscale('log')

        self.axt[1].set_xlabel('Free Energy [erg]')
        self.axt[1].set_ylabel('Helicity [Mx$^2$]')

        cbar = self.figt.colorbar(cax,cax=self.axt[2])
        cbar.set_label('Normed Quad. Mean Dis.')
 #       self.axt[1,1].scatter(self.tdat['free_energy'][use],self.tdat['helicity'][use],c=self.tdat['bfit_s_5'][use],cmap=plt.cm.magma,vmin= 0.00015,vmax=0.000600)
#Use fancy plot if module exists
        if fp:
            #fancy_plot(self.axm)
            self.axm.grid(which='minor',color='black',linestyle='--',linewidth=1,axis='x')
            self.axm.yaxis.set_tick_params(which='both',direction='in')
            self.axm.yaxis.set_tick_params(which='major',length=7)
            self.axm.yaxis.set_tick_params(which='minor',length=3)
            for i in self.axt: fancy_plot(i)
            for i in self.axi: fancy_plot(i)

        self.figt.tight_layout()

        self.figt.savefig(self.cdir+self.bdir+'total_fit_{0:%Y%m%d%H%M%S}.png'.format(self.time),bbox_pad=.1,bbox_inches='tight')
        self.figt.savefig(self.cdir+self.bdir+'total_fit_{0:%Y%m%d%H%M%S}.eps'.format(self.time),bbox_pad=.1,bbox_inches='tight')
        self.figi.savefig(self.cdir+self.bdir+'indvd_fit_{0:%Y%m%d%H%M%S}.png'.format(self.time),bbox_pad=.1,bbox_inches='tight')
        self.figm.savefig(self.cdir+self.bdir+'model_fit_{0:%Y%m%d%H%M%S}.eps'.format(self.time),bbox_pad=.1,bbox_inches='tight')
        self.figm.savefig(self.cdir+self.bdir+'model_fit_{0:%Y%m%d%H%M%S}.png'.format(self.time),bbox_pad=.1,bbox_inches='tight')
        self.figi.savefig(self.cdir+self.bdir+'indvd_fit_{0:%Y%m%d%H%M%S}.eps'.format(self.time),bbox_pad=.1,bbox_inches='tight')

reason: import numpy,from astropy


text:
import numpy as np
from netCDF4 import Dataset as open_ncfile
import os, glob
from modelsDef import defModels
import matplotlib.pyplot as plt

def read_toe_rcp85(varread, listfiles, ignore, ndomains):
    """
    Read ToE array from different model files, save in one ouput array with one dimension being total number
    of runs
    :param varread: string, ToE variable name in files
           listfiles: list of files to read (one file per model)
           ignore: list of models to ignore
           ndomains: number of domain names
    :return: varToEA: numpy array (number of total runs, number of domains) ToE in the Atlantic
             varToEP numpy array (number of total runs, number of domains)
             varToEI numpy array (number of total runs, number of domains)
             nMembers: numpy array containing number of members per model, dimension=nb of models
    """
    nruns=0
    nrunmax = 100
    nmodels = len(listfiles)
    nMembers = np.ma.zeros(nmodels)

    # Initialize varToE containing ToE of all runs
    varToEA = np.ma.masked_all((nrunmax, ndomains))
    varToEP = np.ma.masked_all((nrunmax, ndomains))
    varToEI = np.ma.masked_all((nrunmax, ndomains))

    for i in range(nmodels):
        file_toe = listfiles[i]
        ftoe = open_ncfile(file_toe, 'r')
        name = os.path.basename(file_toe).split('.')[1]

        if name not in ignore:
            # Read ToE (members, basin, domain)
            toeread = ftoe.variables[varread][:]
            nMembers[i] = int(toeread.shape[0])
            print('- Reading ToE of %s with %d members'%(name,nMembers[i]))
            nruns1 = int(nruns + nMembers[i])

            run_labels = ftoe.variables['run_label'][:]
            #print('   ',run_labels)

            # Save ToE
            varToEA[nruns:nruns1,:] = toeread[:,1,:]
            varToEP[nruns:nruns1,:] = toeread[:,2,:]
            varToEI[nruns:nruns1,:] = toeread[:,3,:]

            nruns = nruns1

    print('Total number of runs:', nruns)
    varToEA = varToEA[0:nruns,:]
    varToEP = varToEP[0:nruns,:]
    varToEI = varToEI[0:nruns,:]

    return varToEA, varToEP, varToEI, nMembers

def read_gsat_rcp85(indir,listfiles,ignore):

    """
    Read GSAT array from different model files, save anomaly in one ouput array with one dimension being total number
    of runs, the other being the time
    :param indir: directories where GSAT files are stored
           listfiles: list of ToE files to read (one file per model) to keep same order as when reading ToE
           ignore: list of models to ignore
    :return: GSAT anomaly array (years, nb of runs)

    """

    nruns=0
    nrunmax = 100
    nmodels = len(listfiles)
    nMembers = np.ma.empty(nmodels)
    timN=240

    # -- Initialize GSAT
    gsat_anom = np.ma.masked_all((timN,nrunmax))

    for i in range(nmodels):
        file_toe = listfiles[i] # Read toe file in the same order to retrieve model name
        name = os.path.basename(file_toe).split('.')[1]
        ftoe = open_ncfile(file_toe,'r')
        run_labels = ftoe.variables['run_label'][:] # Read run labels of model i
        if name != 'HadGEM2-ES':
            iystart = 11
        else:
            iystart = 2

        if name not in ignore:

            # Read GSAT
            file_gsat = glob.glob(indir+ 'GSAT.*'+name+'*.nc')[0] # GSAT File
            fgsat = open_ncfile(file_gsat, 'r')
            gsatread = fgsat.variables['GSAT'][:]
            run_labels_GSAT = fgsat.variables['members_name'][:]
            nMembers[i] = int(gsatread.shape[1])
            print('- Reading GSAT of %s with %d members'%(name,nMembers[i]))
            # print('  gsatread shape : ',gsatread.shape)
            nruns1 = int(nruns + nMembers[i])

            # Re-organize order of members so that it's the same as ToE array
            gsatread_cor = np.ma.masked_all_like(gsatread)
            for k in range(int(nMembers[i])):
                idx_cor = list(run_labels).index(run_labels_GSAT[k])
                gsatread_cor[:,idx_cor] = gsatread[:,k]
                #print('  ',k,run_labels_GSAT[k], run_labels[k], run_labels[idx_cor])

            # Save GSAT
            gsatread_anom = gsatread_cor - np.ma.average(gsatread_cor[0:50,:],axis=0) # Anomaly relative to first 50 years
            gsat_anom[:,nruns:nruns1] = gsatread_anom[iystart:,:] # Keep 1861-2005
            # print('  gsatanom shape : ',gsat_anom.shape)
            nruns = nruns1

    print('Total number of runs:', nruns)
    gsat_anom = gsat_anom[:,0:nruns]

    return gsat_anom

def read_toe_1pctCO2(varread, indir, models, ignore, ndomains):
    """
    Read ToE array from different model files, save in one ouput array with one dimension being total number
    of models
    :param varread: string, ToE variable name in files
           indir: directory of files to read (one file per model)
           models: dict containing models' characteristics
           ignore: list of models to ignore
           ndomains: number of domain names
    :return: varToEA: numpy array (number of total runs, number of domains) ToE in the Atlantic
             varToEP numpy array (number of total runs, number of domains)
             varToEI numpy array (number of total runs, number of domains)
             nMembers: numpy array containing number of members per model, dimension=nb of models
    """

    nmodels=len(models)

    # -- Initialize varToE containing ToE
    varToEA = np.ma.masked_all((nmodels,ndomains))
    varToEP = np.ma.masked_all((nmodels,ndomains))
    varToEI = np.ma.masked_all((nmodels,ndomains))

    nMembers=np.ma.zeros(nmodels)

    for i, model in enumerate(models):

        if model['name'] not in ignore:

            # Read file
            file_CO2piC = glob.glob(indir+'/'+ '*'+model['name']+'*.nc')[0]
            fpiC = open_ncfile(file_CO2piC, 'r')

            # Read ToE (basin, domain)
            toeread = fpiC.variables[varread][:]
            print('- Reading ToE of ' + model['name'])

            # Save ToE
            varToEA[i,:] = toeread[1,:]
            varToEP[i,:] = toeread[2,:]
            varToEI[i,:] = toeread[3,:]

            nMembers[i] = 1

    print('Total number of models:', np.sum(nMembers))

    # Remove rows with masked values (due to ignored model(s))
    if len(ignore) != 0:
        idx = np.argwhere(nMembers==0)[0]
        if len(idx) !=0:
            idx = idx[0]
            varToEA_bis = np.delete(varToEA,idx,0) # Delete row at index idx
            varToEA = np.ma.masked_greater(varToEA_bis,200) # Now turn back into a masked array
            varToEP_bis = np.delete(varToEP,idx,0)
            varToEP = np.ma.masked_greater(varToEP_bis,200)
            varToEI_bis = np.delete(varToEI,idx,0)
            varToEI = np.ma.masked_greater(varToEI_bis,200)

    return varToEA, varToEP, varToEI, nMembers

def read_gsat_1pctCO2(indir,models,ignore):

    """
    Read GSAT array from different model files, save anomaly in one ouput array with one dimension being total number
    of runs, the other being the time
    :param indir: directories where GSAT files are stored
           models: dict containing models' characteristics
           ignore: list of models to ignore
    :return: GSAT anomaly array (years, nb of runs)

    """

    nmodels = len(models)
    nMembers = np.ma.zeros(nmodels)
    timN=140

    indir_1pctCO2 = indir + '1pctCO2/'
    indir_piC = indir + 'piControl/'

    # -- Initialize GSAT
    gsat_anom = np.ma.masked_all((timN,nmodels-len(ignore)))
    imod = 0

    for i,model in enumerate(models):

        if model['name'] not in ignore:

            # Read GSAT 1pctCO2
            file_gsat_CO2 = glob.glob(indir_1pctCO2+ '*'+model['name']+'*.nc')[0] # GSAT File
            fgsatCO2 = open_ncfile(file_gsat_CO2, 'r')
            gsatread_CO2 = fgsatCO2.variables['GSAT'][0:timN]
            print('- Reading GSAT of %s'%(model['name'],))

            # Read GSAT PiControl
            file_gsat_piC = glob.glob(indir_piC+ '*'+model['name']+'*.nc')[0] # GSAT File
            fgsatpiC = open_ncfile(file_gsat_piC, 'r')
            gsatread_piC = fgsatpiC.variables['GSAT'][:]

            # Compute and Save GSAT anomaly
            gsat_anom[:,imod] = gsatread_CO2 - np.ma.average(gsatread_piC,axis=0)
            imod = imod+1

            nMembers[i]=1

    print('Total number of models:', np.sum(nMembers))

    return gsat_anom


text:
# import libraries
from flask import Flask, render_template, request
import pandas as pd
import json
import plotly
import plotly.express as px
import plotly.graph_objs as go
import networkx as nx
import os
import requests

# flask app
app = Flask(__name__)

# time estimation model
def time_estimation(G, df, start_node, end_node, w_of_day, h_of_day, wk_coef):
    time_estimation = 0

    path_list = []
    path = nx.shortest_path(
        G=G,
        source=start_node,
        target=end_node) # save the shortest path
    temp = path[0][0:4]
    path_list.append(temp)
    for key in path:
        if key[0:4] != temp:
            temp = key[0:4]
            path_list.append(temp) # area id in the shortest path

    for i in range(len(path_list) - 1): # using past data to make the prediction
        if len(df.loc[(df['sourceid'] == int(path_list[i])) & (df['dstid'] == int(path_list[i + 1])) & (df['hod'] == h_of_day)]['mean_travel_time']) == 0:
            if len(df.loc[(df['sourceid'] == int(path_list[i])) & (df['dstid'] == int(path_list[i + 1]))]) == 0:
                time_estimation += float(df.loc[(df['sourceid'] == int(path_list[i]))]['mean_travel_time'].min())
            else:
                time_estimation += float(df.loc[(df['sourceid'] == int(path_list[i])) & (df['dstid'] == int(path_list[i + 1]))]['mean_travel_time'].mean())
        else:
            time_estimation += float(df.loc[(df['sourceid'] == int(path_list[i])) & (df['dstid'] == int(path_list[i + 1])) & (df['hod'] == h_of_day)]['mean_travel_time'])

    time_estimation = wk_coef.iloc[w_of_day-1]['mean_travel_time'] * time_estimation

    return str(round(time_estimation / 60, 2)) + " minutes"

# find the shortest path and make the plot
def shortest_path_plot(G, start_node, end_node):
    edge_x = []
    edge_y = []
    for edge in G.edges():
        x0, y0 = G.nodes[edge[0]]['pos']
        x1, y1 = G.nodes[edge[1]]['pos']
        edge_x.append(x0)
        edge_x.append(x1)
        edge_x.append(None)
        edge_y.append(y0)
        edge_y.append(y1)
        edge_y.append(None)

    edge_trace = go.Scatter(
        x=edge_x, y=edge_y,
        line={'width': 0.5, 'color': '#888'},
        hoverinfo='text',
        mode='lines')

    fig = go.Figure(data=edge_trace,
                    layout=go.Layout(
                        showlegend=False,
                        hovermode='closest',
                        margin=dict(b=20, l=5, r=5, t=40),
                        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
                    )

    path = nx.shortest_path(
        G=G,
        source=start_node,
        target=end_node)

    path_x = []
    path_y = []
    for key in path:
        x0, y0 = G.nodes[key]['pos']
        path_x.append(x0)
        path_y.append(y0)

    fig.add_trace(go.Scatter(x=path_x,
                             y=path_y,
                             line=dict(width=5, color='blue'),
                             hoverinfo='none',
                             mode='lines'))

    return fig

# download traffic data from google drive
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

@app.route('/')
def main_page():
    fig = plotly.io.read_json(os.getcwd()+'/data/graph.json')
    fig.update_layout(
        showlegend=False,
        autosize=False,
        width=800,
        height=800,
        margin=dict(
            l=50,
            r=50,
            b=10,
            t=100,
            pad=4))
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    return render_template('main.html', graphJSON=graphJSON)

@app.route('/', methods=['POST'])
def parse_urls():
    if request.method == 'POST':
        start_node = str(request.form['from'])+'-0-0'
        end_node = str(request.form['to'])+'-0-0'
        w_of_day = int(request.form['dow'])
        h_of_day = int(request.form['hod'])
        try:
            df = pd.read_csv(os.getcwd()+"/data/traffic_data.csv")
        except FileNotFoundError:
            download_file_from_google_drive("1pYEZFp9d0ibrX8BaV9cmM0hkgtJC0Gxj", os.getcwd()+"/data/traffic_data.csv")
            df = df = pd.read_csv(os.getcwd()+"/data/traffic_data.csv")
        wk_coef = pd.read_csv(os.getcwd()+"/data/wk_coef.csv")
        G = nx.read_gpickle(os.getcwd()+"/data/graph.gpickle")
        fig = shortest_path_plot(G=G, start_node=start_node, end_node=end_node)
        time = time_estimation(G=G, df=df, start_node=start_node, end_node=end_node, w_of_day=w_of_day, h_of_day=h_of_day, wk_coef=wk_coef)
        fig.update_layout(
            showlegend=False,
            autosize=False,
            width=800,
            height=800,
            margin=dict(
                l=50,
                r=50,
                b=10,
                t=100,
                pad=4))
        graph = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
        return render_template('submit.html', time=time, graph=graph)

app.run()

max_stars_repo_name: Russell-Duan/traffic

max_issues_repo_name: Russell-Duan/traffic

max_forks_repo_name: Russell-Duan/traffic

reason: import networkx


text:
from .input import CInputBase
from .calc import CCalcBase
from bayesfast import ModuleBase
from bayesfast.utils.collections import PropertyList
from collections import OrderedDict
from itertools import chain
import numpy as np
import camb

__all__ = ['CAMB']

class CAMB(ModuleBase):
    '''CAMB cosmology model.'''
    def __init__(self, c_input, c_calc, output_vars=None, get_output=None,
                 delete_vars=[], label=None):
        super().__init__(output_vars=output_vars, delete_vars=delete_vars,
                         input_shapes=-1, output_shapes=None, label=label)
        self.c_input = c_input
        self.c_calc = c_calc
        self.get_output = get_output

    @property
    def c_input(self):
        return self._c_input

    @c_input.setter
    def c_input(self, ci):
        if isinstance(ci, CInputBase):
            self._c_input = ci
        else:
            self._c_input = None
            raise ValueError('c_input should be a subclass of CInputBase.')

    @property
    def c_calc(self):
        return self._c_calc

    @c_calc.setter
    def c_calc(self, cc):
        if isinstance(cc, CCalcBase):
            cc = [cc]
        self._c_calc = PropertyList(cc, self._calc_check)

    @staticmethod
    def _calc_check(c_calc):
        if not all(isinstance(cc, CCalcBase) for cc in c_calc):
            raise ValueError('some element of c_calc is not a subclass of '
                             'CCalcBase.')
        return c_calc

    @property
    def input_vars(self):
        return self.c_input.input_vars

    @property
    def output_vars(self):
        if self._output_vars is None:
            try:
                ov = [v for c in self.c_calc for v in c.output_vars]
                assert all(isinstance(v, str) for v in ov)
                ov = sorted(set(ov), key=ov.index) # remove redundant elements
                return ov
            except Exception:
                raise RuntimeError('failed to get output_vars from c_calc.')
        else:
            return self._output_vars

    @output_vars.setter
    def output_vars(self, ov):
        if ov is None:
            self._output_vars = None
        else:
            self._output_vars = PropertyList(
                ov, lambda x: ModuleBase._var_check(x, 'output', 'raise', 1,
                np.inf))

    @staticmethod
    def _dict_output(tmp_dict):
        return list(tmp_dict.values())

    @property
    def get_output(self):
        if self._get_output is None:
            return self._dict_output
        else:
            return self._get_output

    @get_output.setter
    def get_output(self, go):
        if go is None or callable(go):
            self._get_output = go
        else:
            raise ValueError('invalid value for get_output.')

    def set_output(self, modules):
        try:
            if hasattr(modules, 'camb_output_vars'):
                modules = (modules,)
            ov = list(chain(*[list(m.camb_output_vars) for m in modules]))
            assert all([callable(m.camb_get_output) for m in modules])
            go = self.multi_output([m.camb_get_output for m in modules])
            self.output_vars = ov
            self.get_output = go
        except Exception:
            raise ValueError('failed to set output for the modules you give.')

    @staticmethod
    def _check_output(output):
        if isinstance(output, np.ndarray):
            return [output]
        elif isinstance(output, (list, tuple)):
            return output
        else:
            raise RuntimeError('invalid value in _check_output.')

    @classmethod
    def multi_output(cls, fun_list):
        return lambda x: list(
            chain(*[cls._check_output(fun(x)) for fun in fun_list]))

    def _fun(self, x):
        params = self.c_input(x)
        data = camb.CAMBdata()

        min_calc = max(calc.min_calc for calc in self.c_calc)
        if min_calc == 0:
            data.set_params(params)
        elif min_calc == 1:
            data.calc_background_no_thermo(params)
        elif min_calc == 2:
            data.calc_background(params)
        elif min_calc == 3:
            data.calc_power_spectra(params)
        else:
            raise RuntimeError('unexpected value for min_calc.')

        tmp_dict = OrderedDict()
        for calc in self.c_calc:
            calc(data, tmp_dict)
        return self.get_output(tmp_dict)

max_stars_repo_name: HerculesJack/cosmofast

max_issues_repo_name: HerculesJack/cosmofast

max_forks_repo_name: HerculesJack/cosmofast