text:
import sys

import numpy as np
import sounddevice as sd

from pysinewave import utilities
from pysinewave import sinewave_generator

class SineWave:
    """Generates and plays a continuous sinewave, with smooth transitions in frequency (pitch)
        and amplitude (volume)."""

    def __init__(self, pitch=0, pitch_per_second=12, decibels=0, decibels_per_second=1, channels=1, channel_side="lr",
                 samplerate=utilities.DEFAULT_SAMPLE_RATE, clip_off=False, dither_off=False, waveform=np.sin,
                 phase_cutoff=2000000000, db_cutoff=-100):

        self.sinewave_generator = sinewave_generator.SineWaveGenerator(pitch=pitch, pitch_per_second=pitch_per_second,
                                                                       decibels=decibels,
                                                                       decibels_per_second=decibels_per_second,
                                                                       samplerate=samplerate, waveform=waveform,
                                                                       phase_cutoff=phase_cutoff, db_cutoff=db_cutoff)

        # Create the output stream
        self.output_stream = sd.OutputStream(channels=channels, callback=lambda *args: self._callback(*args),
                                             samplerate=samplerate, clip_off=clip_off, dither_off=dither_off)

        self.channels = channels

        if channel_side == 'r':
            self.channel_side = 0
        elif channel_side == 'l':
            self.channel_side = 1
        else:
            self.channel_side = -1

        self.record(start=False, clear=True)

    def _callback(self, outdata, frames, time, status):
        """Callback function for the output stream."""
        # Print any error messages we receive
        if status:
            print(status, file=sys.stderr)

        # Get and use the sinewave's next batch of data
        data = self.sinewave_generator.next_data(frames)
        outdata[:] = data.reshape(-1, 1)

        # Output on the given channel
        if self.channel_side != -1 and self.channels == 2:
            outdata[:, self.channel_side] = 0.0

        if self.is_recording:
            self.record_buffer.append(data)

    def record(self, start=None, clear=False):
        if clear:
            self.record_buffer = []
        self.is_recording = not self.is_recording if start is None else start

    def play(self):
        """Plays the sinewave (in a separate thread). Changes in frequency or amplitude will transition smoothly."""
        self.output_stream.start()

    def stop(self):
        """If the sinewave is playing, stops the sinewave."""
        self.output_stream.stop()

    def set_frequency(self, frequency):
        """Sets the goal frequency of the sinewave, which will be smoothly transitioned to."""
        self.sinewave_generator.set_frequency(frequency)

    def set_pitch(self, pitch):
        """Sets the goal pitch of the sinewave (relative to middle C),
        which will be smoothly transitioned to."""
        self.sinewave_generator.set_pitch(pitch)

    def set_volume(self, volume):
        """Sets the goal volume (in decibels, relative to medium volume) of the sinewave"""
        self.sinewave_generator.set_decibels(volume)

    def set_waveform(self, waveform):
        """Sets the goal volume (in decibels, relative to medium volume) of the sinewave"""
        self.sinewave_generator.set_waveform(waveform)

max_stars_repo_name: eyaler/pysinewave

max_issues_repo_name: eyaler/pysinewave

max_forks_repo_name: eyaler/pysinewave


text:
# --------------------------------------------------------
# Faster R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

from fast_rcnn.config import cfg
from fast_rcnn.train import filter_roidb
from utils.blob import im_list_to_blob
from utils.timer import Timer
from generate_anchors import generate_anchors
from utils.cython_bbox import bbox_overlaps
from fast_rcnn.bbox_transform import bbox_transform
import numpy as np
import cv2

def _vis_proposals(im, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return

    class_name = 'obj'
    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]

        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1], fill=False,
                          edgecolor='red', linewidth=3.5)
            )
        ax.text(bbox[0], bbox[1] - 2,
                '{:s} {:.3f}'.format(class_name, score),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14, color='white')

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

def _get_image_blob(im):
    """Converts an image into a network input.

    Arguments:
        im (ndarray): a color image in BGR order

    Returns:
        blob (ndarray): a data blob holding an image pyramid
        im_scale_factors (list): list of image scales (relative to im) used
            in the image pyramid
    """
    im_orig = im.astype(np.float32, copy=True)
    im_orig -= cfg.PIXEL_MEANS

    im_shape = im_orig.shape
    im_size_min = np.min(im_shape[0:2])
    im_size_max = np.max(im_shape[0:2])

    processed_ims = []

    assert len(cfg.TEST.SCALES) == 1
    target_size = cfg.TEST.SCALES[0]

    im_scale = float(target_size) / float(im_size_min)
    # Prevent the biggest axis from being more than MAX_SIZE
    if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:
        im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)
    im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
                    interpolation=cv2.INTER_LINEAR)
    im_info = np.hstack((im.shape[:2], im_scale))[np.newaxis, :]
    processed_ims.append(im)

    # Create a blob to hold the input images
    blob = im_list_to_blob(processed_ims)

    return blob, im_info

def im_proposals(net, im):
    """Generate RPN proposals on a single image."""
    blobs = {}
    blobs['data'], blobs['im_info'] = _get_image_blob(im)
    net.blobs['data'].reshape(*(blobs['data'].shape))
    net.blobs['im_info'].reshape(*(blobs['im_info'].shape))
    blobs_out = net.forward(
            data=blobs['data'].astype(np.float32, copy=False),
            im_info=blobs['im_info'].astype(np.float32, copy=False))

    scale = blobs['im_info'][0, 2]
    boxes = blobs_out['rois'][:, 1:].copy() / scale
    scores = blobs_out['scores'].copy()
    return boxes, scores

def imdb_proposals(net, imdb):
    """Generate RPN proposals on all images in an imdb."""

    _t = Timer()
    imdb_boxes = [[] for _ in xrange(imdb.num_images)]
    for i in xrange(imdb.num_images):
        im = None
        if cfg.TRAIN.FORMAT == 'pickle':
            with open(imdb.image_path_at(i), 'rb') as f:
                im = cPickle.load(f)
        else:
            im = cv2.imread(imdb.image_path_at(i))

        _t.tic()
        imdb_boxes[i], scores = im_proposals(net, im)
        _t.toc()
        print 'im_proposals: {:d}/{:d} {:.3f}s' \
              .format(i + 1, imdb.num_images, _t.average_time)
        if 0:
            dets = np.hstack((imdb_boxes[i], scores))
            # from IPython import embed; embed()
            _vis_proposals(im, dets[:3, :], thresh=0.9)
            plt.show()

    return imdb_boxes

def imdb_rpn_compute_stats(net, imdb, anchor_scales=(8,16,32),
                           feature_stride=16):
    raw_anchors = generate_anchors(scales=np.array(anchor_scales))
    print raw_anchors.shape
    sums = 0
    squred_sums = 0
    counts = 0
    roidb = filter_roidb(imdb.roidb)
    # Compute a map of input image size and output feature map blob
    map_w = {}
    map_h = {}
    for i in xrange(50, cfg.TRAIN.MAX_SIZE + 10):
        blobs = {
            'data': np.zeros((1, 3, i, i)),
            'im_info': np.asarray([[i, i, 1.0]])
        }
        net.blobs['data'].reshape(*(blobs['data'].shape))
        net.blobs['im_info'].reshape(*(blobs['im_info'].shape))
        blobs_out = net.forward(
            data=blobs['data'].astype(np.float32, copy=False),
            im_info=blobs['im_info'].astype(np.float32, copy=False))
        height, width = net.blobs['rpn/output'].data.shape[-2:]
        map_w[i] = width
        map_h[i] = height

    for i in xrange(len(roidb)):
        if not i % 5000:
            print 'computing %d/%d' % (i, imdb.num_images)
        im = None
        if cfg.TRAIN.FORMAT == 'pickle':
            with open(roidb[i]['image'], 'rb') as f:
                im = cPickle.load(f)
        else:
            im = cv2.imread(roidb[i]['image'])

        im_data, im_info = _get_image_blob(im)
        gt_boxes = roidb[i]['boxes']
        gt_boxes = gt_boxes * im_info[0, 2]
        height = map_h[im_data.shape[2]]
        width = map_w[im_data.shape[3]]
        # 1. Generate proposals from bbox deltas and shifted anchors
        shift_x = np.arange(0, width) * feature_stride
        shift_y = np.arange(0, height) * feature_stride
        shift_x, shift_y = np.meshgrid(shift_x, shift_y)
        shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                            shift_x.ravel(), shift_y.ravel())).transpose()
        # add A anchors (1, A, 4) to
        # cell K shifts (K, 1, 4) to get
        # shift anchors (K, A, 4)
        # reshape to (K*A, 4) shifted anchors
        A = raw_anchors.shape[0]
        K = shifts.shape[0]
        all_anchors = (raw_anchors.reshape((1, A, 4)) +
                       shifts.reshape((1, K, 4)).transpose((1, 0, 2)))
        all_anchors = all_anchors.reshape((K * A, 4))

        # only keep anchors inside the image
        inds_inside = np.where(
            (all_anchors[:, 0] >= 0) &
            (all_anchors[:, 1] >= 0) &
            (all_anchors[:, 2] < im_info[0, 1]) &  # width
            (all_anchors[:, 3] < im_info[0, 0])  # height
        )[0]

        # keep only inside anchors
        anchors = all_anchors[inds_inside, :]

        overlaps = bbox_overlaps(
            np.ascontiguousarray(anchors, dtype=np.float),
            np.ascontiguousarray(gt_boxes, dtype=np.float))

        # There are 2 types of bbox targets
        # 1. anchor whose overlaps with gt is greater than RPN_POSITIVE_OVERLAP
        argmax_overlaps = overlaps.argmax(axis=1)
        max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
        fg_inds = np.where(max_overlaps >= cfg.TRAIN.RPN_POSITIVE_OVERLAP)[0]
        # 2. anchors which best match certain gt
        gt_argmax_overlaps = overlaps.argmax(axis=0)
        gt_max_overlaps = overlaps[gt_argmax_overlaps,
                                   np.arange(overlaps.shape[1])]
        gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]
        fg_inds = np.unique(np.hstack((fg_inds, gt_argmax_overlaps)))
        gt_rois = gt_boxes[argmax_overlaps, :]

        anchors = anchors[fg_inds, :]
        gt_rois = gt_rois[fg_inds, :]
        targets = bbox_transform(anchors, gt_rois[:, :4]).astype(np.float32, copy=False)
        sums += targets.sum(axis=0)
        squred_sums += (targets ** 2).sum(axis=0)
        counts += targets.shape[0]

    means = sums / counts
    stds = np.sqrt(squred_sums / counts - means ** 2)
    print means
    print stds
    return means, stds

ext: py

max_stars_repo_name: ayzk/ft-caffe-public

max_issues_repo_name: ayzk/ft-caffe-public

max_forks_repo_name: ayzk/ft-caffe-public

reason: import numpy


text:
import numpy as np
import igraph as ig
import json
from itertools import chain, combinations

def generate_png(dot_filename, png_filename=None, prog="neato"):
    if png_filename is None:
        png_filename = dot_filename.replace(".dot", ".png")
    os.system("%s.exe -T png %s > %s" % (prog, dot_filename, png_filename))

def generate_labels(number_of_items, number_of_labels):
    labels = list(range(number_of_labels))
    if number_of_items < number_of_labels:
        np.random.shuffle(labels)
        labels = labels[:number_of_items]
    else:
        for i in range(number_of_labels, number_of_items):
            labels.append(np.random.randint(number_of_labels))
        np.random.shuffle(labels)
    return labels

def generate_tree(number_of_vertices, directed=True):
    # Alexey S. Rodionov and Hyunseung Choo, On Generating Random Network Structures: Trees, ICCS 2003, LNCS 2658, pp. 879-887, 2003.
    # [connected vertices] + [unconnected vertices]
    shuffle_vertices = list(range(number_of_vertices))
    np.random.shuffle(shuffle_vertices)
    # randomly choose one vertex from the connected vertex set
    # randomly choose one vertex from the unconnected vertex set
    # connect them by one edge
    # add the latter vertex in the connected vertex set
    edges = list()
    for v in range(1, number_of_vertices):
        u = shuffle_vertices[np.random.randint(0, v)]
        v = shuffle_vertices[v]
        if get_direction():
            src_tgt = (u, v)
        else:
            src_tgt = (v, u)
        edges.append(src_tgt)
    tree = ig.Graph(directed=directed)
    tree.add_vertices(number_of_vertices)
    tree.add_edges(edges)
    return tree

def get_direction():
    return np.random.randint(0, 2)

def retrieve_multiple_edges(graph, source=-1, target=-1):
    if source != -1:
        e = graph.incident(source, mode=ig.OUT)
        if target != -1:
            e = set(e).intersection(graph.incident(target, mode=ig.IN))
        return ig.EdgeSeq(graph, e)
    else:
        if target != -1:
            e = graph.incident(target, mode=ig.IN)
        else:
            e = list()
        return ig.EdgeSeq(graph, e)

def str2bool(x):
    x = x.lower()
    return x == "true" or x == "yes" or x == "t"

def sample_element(s):
    index = np.random.randint(0, len(s))
    return s[index]

def powerset(iterable, min_size=0, max_size=-1):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = sorted(iterable)
    if max_size == -1:
        max_size = len(s)
    return chain.from_iterable(combinations(s, r) for r in range(min_size, max_size+1))

max_stars_repo_name: HKUST-KnowComp/NeuralSubIsoCnt

max_issues_repo_name: HKUST-KnowComp/NeuralSubIsoCnt

max_forks_repo_name: HKUST-KnowComp/NeuralSubIsoCnt


text:
import numpy as np

def easy_sigmoid(self):
    sigmoids = [
        np.abs(self._sig(self.c_step, slope, shift))
        for slope, shift in zip(self.shifts, self.slopes)
    ]
    action = []
    for i in range(len(self.action_vals)):
        best_action = None
        dist = 100
        for a in range(self.action_vals[i] + 1):
            if np.abs(sigmoids[i] - a / (self.action_vals[i] - 1)) < dist:
                dist = np.abs(sigmoids[i] - a / (self.action_vals[i]))
                best_action = a
        action.append(best_action)
    action_diffs = self.action - action
    r = 0
    for i in range(len(action_diffs)):
        r += 10 ** i * action_diffs[i]
    r = max(self.reward_range[0], min(self.reward_range[1], r))
    return r

def almost_easy_sigmoid(self):
    r = [
        1 - np.abs(self._sig(self.c_step, slope, shift) - (act / (max_act - 1)))
        for slope, shift, act, max_act in zip(
            self.slopes, self.shifts, self.action, self.action_vals
        )
    ]
    r = sum(r)
    r = max(self.reward_range[0], min(self.reward_range[1], r))
    return r

def sum_reward(self):
    if self.c_step == 1:
        self.rew_sum = 0
    self.rew_sum += self.get_default_reward
    if self.done:
        return self.rew_sum
    else:
        return 0

def random_reward(self):
    return np.random.uniform(self.reward_range[0], self.reward_range[1])

max_stars_repo_name: ndangtt/LeadingOnesDAC

max_issues_repo_name: ndangtt/LeadingOnesDAC

max_forks_repo_name: ndangtt/LeadingOnesDAC


text:
from keras.models import load_model
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
import numpy as np
import os
import sys

from keras.models import load_model

#model = load_model('model_saved.h5')

pb  = './model_1/saved_model.pb'

model = load_model('model_1.h5')
img_width, img_height = 10, 20

def predict(filename):
    image = load_img(filename, target_size=(img_height,img_width),color_mode="grayscale")
    img = np.array(image)
    # todo, check if below makes a difference at all
    img = img / 255.0
    img = img.reshape(1,img_height,img_width,1)
    label_distribution = model.predict(img)
    label_index = label_distribution.argmax()

    print("Predicted Class index:", label_index)

predict_num = sys.argv[1]
print("Predicting for: " + predict_num)
directory='/data/digits-data/Validation/' + str(predict_num)
#directory='/data/tmp/train'
for filename in os.listdir(directory):
    if filename.endswith(".png"):
        predict(os.path.join(directory, filename))

max_stars_repo_name: fvbakel/nonogram

max_issues_repo_name: fvbakel/nonogram

max_forks_repo_name: fvbakel/nonogram


text:
import shutil
import re
import code
import numpy
import operator
import scenario
import time
import os
import json
import sys
import liclient
import loadimpact
import math
import report
import paths

# Make loadimpact class TestResult available in this scope
TestResult = loadimpact.resources.TestResult

LI_WORLD_REGION_ID = 1
LIVE_FEEDBACK_KEY="__li_live_feedback"
CLIENTS_ACTIVE_KEY="__li_clients_active"
USER_LOAD_TIME_KEY='__li_user_load_time'

def get_enriched_metrics(config_run, title):
    return __enrich_metrics(__get_metrics(config_run, title, False))

def generate_for_completed_by_id(run_id, title):
    config_run = __get_config_run(run_id)
    generate_for_completed(config_run, title)

def generate_for_completed(config_run, title):
    __generate(config_run, title, False)

def generate_for_running_by_id(run_id, title):
    config_run = __get_config_run(run_id)
    generate_for_running(config_run, title)

def generate_for_running(config_run, title):
    __generate(config_run, title, True)

def __get_stored_metrics(run_id):
    with open(__metrics_json_path(run_id),'r') as f:
        json_str = f.read()
        return json.loads(json_str)

def __get_result_ids(config_run):
    world_id=LI_WORLD_REGION_ID

    test_result_ids = [
        TestResult.result_id_from_name(TestResult.LIVE_FEEDBACK),
        TestResult.result_id_from_name(TestResult.ACTIVE_USERS,
                                       load_zone_id=world_id),
        TestResult.result_id_from_name(TestResult.REQUESTS_PER_SECOND,
                                       load_zone_id=world_id),
        TestResult.result_id_from_name(TestResult.USER_LOAD_TIME,
                                       load_zone_id=world_id)
    ]

    # add scenario specific metrics
    scenarios = config_run["config_params"]["scenarios"]
    for scenario_name in scenarios:
        scenario_id = scenario.get(scenario_name)["id"]
        # add app page metrics for each scenario
        test_result_ids.append(__scenario_app_page_metric_id(scenario_id, scenarios[scenario_name]))
        # add core action metrics for each scenario
        test_result_ids.append(__core_actions_metric_id(scenario_id))
        test_result_ids.append(__correctness_metric_id(scenario_id, scenario_name))
    return test_result_ids

# Get the metric id for the scenario correctness (app.pass) metric, giving percentage of successful tests
def __correctness_metric_id(scenario_id, scenario_name):
    world_id=LI_WORLD_REGION_ID
#    metric_id = TestResult.result_id_from_custom_metric_name("app.pass", world_id,
#                                          scenario_id)
    # app.pass seems to not have complete data per scenario, try scenario pass metric instead
    metric_id = TestResult.result_id_from_custom_metric_name("app.scenario_" + scenario_name + ".pass", world_id,
                                          scenario_id)

    metric_id = re.sub(r'(__custom)([^_].+)', r'\1_\2', metric_id) # hack to work around custom id generation bug in python sdk
    # "__custom_4014eec137b4e674596b7db18ed795f1:13:3140094|0:3000"
    return metric_id

def __scenario_app_page_metric_id(scenario_id, scenario_config):
    world_id=LI_WORLD_REGION_ID
    page_name = __scenario_action_page_name(scenario_config)
    metric_id = TestResult.result_id_for_page(page_name, world_id, scenario_id)
    metric_id = re.sub(r'(__li_page)([^_].+)', r'\1_\2', metric_id) # hack to work around page id generation bug in python sdk
    return metric_id

def __scenario_action_page_name(scenario_config):
    if "actions-per-sec-page" in scenario_config:
        return scenario_config["actions-per-sec-page"]
    else:
        return "app"

def __core_actions_metric_id(scenario_id):
    world_id=LI_WORLD_REGION_ID
    core_action_page = "core_action"
    metric_id = TestResult.result_id_for_page(core_action_page, world_id, scenario_id)
    metric_id = re.sub(r'(__li_page)([^_].+)', r'\1_\2', metric_id) # hack to work around page id generation bug in python sdk
    return metric_id

def __get_clients_active(metrics, live_run):
    return metrics[__get_clients_active(live_run)]

def __get_metrics(config_run, title, live_run=True):
    run_id = config_run["run_id"]

    __prepare(run_id)
    __save_meta(run_id, title, config_run)

    metrics = None
    if live_run:
        print("Current runtime report is " + __report_path(run_id) + "/runtime/runtime_report.html")
        create_runtime_report = True
        test_run = liclient.client.get_test(run_id)
        stream = test_run.result_stream(__get_result_ids(config_run))
        # Wait for test to finish
        for data in stream(poll_rate=3):
            if (CLIENTS_ACTIVE_KEY not in data): # Only treat data which has real metrics, otherwise it is just booting msg etc. (e.g. live feed)
                continue
            metrics = stream.series
            __store_metrics(run_id, metrics)
            if create_runtime_report:
                # update timestamp to trigger js auto-reload
                __update_runtime_timestamp(run_id, report.get_last_timestamp(metrics))
                __runtime_charts(run_id, metrics, config_run)
            time.sleep(3)
        __store_metrics(run_id, metrics)
        metrics = stream.series
    else:
        metrics = __get_finished_run_results(run_id, config_run)
    if (CLIENTS_ACTIVE_KEY not in metrics): # Only treat data which has real metrics, otherwise it is just booting msg etc. (e.g. live feed)
        print("metrics (for config run %s) missing %s, we assume data is incomplete" % (run_id, CLIENTS_ACTIVE_KEY))
        print("submetrics present: " + ", ".join(metrics.keys()))
        return
    clients_active = metrics[CLIENTS_ACTIVE_KEY]
    if not clients_active:
        print("metrics (for config run %s) missing %s, we assume data is incomplete" % (run_id, CLIENTS_ACTIVE_KEY))
        print("submetrics present: " + ", ".join(metrics.keys()))
        return
    return metrics

def __generate(config_run, title, live_run=True):
    run_id = config_run["run_id"]
    print("Generate report for config run_id: " + repr(run_id))
    metrics = __get_metrics(config_run, title, live_run)
    metrics = __enrich_metrics(metrics)
    __complete_charts(run_id, metrics, title, config_run)
    if live_run:
        __update_runtime_timestamp(run_id, report.get_last_timestamp(metrics), "true") # trigger termination runtime report js reload loop
        print("Finished run " + repr(run_id))
    print("Complete report for test_config run " + repr(run_id) + ":\n" + complete_report_url(run_id))

def __get_finished_run_results(run_id, config_run):
    result_ids = __get_result_ids(config_run)
    result_ids_str = "?ids="
    for result_id in result_ids:
        result_ids_str += "" + result_id + ","
    result_ids_str = result_ids_str[0:-1]
    results = code.json_get('tests/%s/results%s' % (run_id, result_ids_str))
    return results

# Add dictionaries indexed by LI offset value
# Add dictionaries indexed by LI timestamp value
# Add sample_period metrics
def __enrich_metrics(metrics):
    metrics = __remove_dups_from_metrics(metrics)
    new_metrics = {}
    for sub_metric_name, sub_metrics in metrics.iteritems():
        if sub_metric_name == LIVE_FEEDBACK_KEY: # do not enrich live feedback data
            continue
        new_metrics[sub_metric_name + '.offset_idx'] = __custom_idx_dict("offset", sub_metrics)
        new_metrics[sub_metric_name + '.timestamp_idx'] = __custom_idx_dict("timestamp", sub_metrics)
    for new_metric_name, new_metric in new_metrics.iteritems():
        metrics[new_metric_name] = new_metric
    metrics['sample_period'] = __find_sample_period(metrics)
    return metrics

def __custom_idx_dict(idx_name, sub_metrics, int_conv=False):
    if int_conv:
        keys = map(lambda metric: int(metric[idx_name]), sub_metrics)
    else:
        keys = map(lambda metric: metric[idx_name], sub_metrics)
    return dict(zip(keys, sub_metrics))

# Should not be necessary but we are getting metric entry duplicates from LI Python SDK
def __remove_dups_from_metrics(metrics):
    metric_dicts = {}
    new_metrics = {}

    # remove dups
    for metric_name, rows in metrics.iteritems():
        metric_dicts[metric_name] = {}
        for row in rows:
            metric_dicts[metric_name][row["offset"]] = row

    # sort by offset
    for metric_name, rows in metric_dicts.iteritems():
        new_metrics[metric_name] = []
        for offset in sorted(rows):
            new_metrics[metric_name].append(rows[offset])

    return new_metrics

def __metrics_json_path(run_id):
    report_path = __report_path(run_id)
    return report_path + "/metrics.json"

def __store_metrics(run_id, metrics):
    json_str = json.dumps(metrics)
    try:
        file = open(__metrics_json_path(run_id), 'w') # Trying to create a new file or open one
        file.write(json_str)
        file.close()
    except:
        print('Something went wrong saving json metrics!')
        sys.exit(0) # quit Python

def __create_report_files(dir):
    shutil.copyfile(paths.REPORT_TEMPLATES_DIR + "/config_runs/runtime_report.html", dir + "/runtime/runtime_report.html")
    shutil.copyfile(paths.REPORT_TEMPLATES_DIR + "/config_runs/timestamp.js", dir + "/runtime/timestamp.js")

def __run_path(run_id):
    return paths.config_runs_dir() + "/" + repr(run_id)

def __report_path(run_id):
    return paths.config_reports_dir() + "/" + repr(run_id)

def list_runs():
    onlydirs = [ d for d in os.listdir(paths.config_runs_dir()) if os.path.isdir(os.path.join(paths.config_runs_dir(), d)) ]
    for dir in onlydirs:
        print dir

def __meta_json_path(run_id):
    run_path = __run_path(run_id)
    return run_path + "/meta.json"

def  __save_meta(run_id, title, config_run):
    file = open(__meta_json_path(run_id), 'w') # Trying to create a new file or open one
    json_str = json.dumps({"title": title, "config_run": config_run})
    file.write(json_str)
    file.close()

def __get_title(run_id):
    with open(__meta_json_path(run_id), 'r') as f:
        json_str = f.read()
        meta = json.loads(json_str)
        return meta["title"]

def __get_config_run(run_id):
    with open(__meta_json_path(int(run_id)), 'r') as f:
        json_str = f.read()
        meta = json.loads(json_str)
        return meta["config_run"]

def __prepare(run_id):
    report_path = __report_path(run_id)
    try:
        paths.mkdir_p(report_path)
        os.mkdir(report_path + "/runtime")
        os.mkdir(report_path + "/complete")
        os.mkdir(__run_path(run_id))
    except:
        pass # print "Path exists: " + report_path
    __create_report_files(report_path)

def __update_runtime_timestamp(run_id, timestamp, complete="false"):
    report_path = __report_path(run_id)
    try:
        file = open(report_path + "/runtime/timestamp.js", 'w') # Trying to create a new file or open one
        file.write("report_timestamp = " + repr(timestamp) + "; report_done = " + complete + ";")
        file.close()
    except:
        print('Something went wrong updating timestamp!')
        sys.exit(0) # quit Python

def __flows_per_sec(metrics):
    load_times = metrics[USER_LOAD_TIME_KEY]
    client_nums = metrics[CLIENTS_ACTIVE_KEY]

    # match load_time and clients_active by timestamp
    flows_per_sec = {}
    indexed_load_times = {}
    indexed_client_nums = {}
    for load_time in load_times:
        indexed_load_times[load_time['timestamp']] = load_time
    for client_num in client_nums:
        indexed_client_nums[client_num['timestamp']] = client_num

    for timestamp, load_time in indexed_load_times.iteritems():
        if timestamp in indexed_client_nums:
            # We find flows per second by dividing total clients by seconds spent per flow
            active_clients = indexed_client_nums[timestamp]['value']
            load_time_in_sec = load_time['value'] / 1000.0
            flow_per_sec_ratio = round(active_clients / load_time_in_sec, 2)
            flows_per_sec[timestamp] = flow_per_sec_ratio

    return __sort_dict(flows_per_sec)

def __sort_dict(ret):
    sorted_ret = sorted(ret.items(), key=operator.itemgetter(0))
    keys = []
    vals = []
    for entry in sorted_ret:
        keys.append(entry[0])
        vals.append(entry[1])
    return {"keys": keys, "vals": vals}

def __get_metric_as_actions_per_sec(key, metrics, divisor):
    metric_set = metrics[key]
    return map(lambda row: {"timestamp": row["timestamp"], "offset": row["offset"], "avg": round(row["value"] / divisor, 2)}, metric_set)

def __get_metric(key, metrics, divisor):
    metric_set = metrics[key]
    ret = {}
    for data in metric_set:
        ret[data['timestamp']] = round(data['value'] / divisor, 2)
    return __sort_dict(ret)

# Round to nearest whole number
def __round(float_num):
    return int(round(float_num/10, 1) * 10)

# This function will try to get the period from 70% to 90% of the stable period, assuming
# that this will give us a sensible average from the max Virtual Users load period.
def __find_sample_period(metrics):
    clients_active = metrics[CLIENTS_ACTIVE_KEY]
    clients_max = 0
    first_max_client_idx = None
    last_max_client_idx = None
    stable_peak_found = False
    last_idx = None
    for idx, row in enumerate(clients_active):
        last_idx = idx
        if row['value'] == 0: # should not happen
            continue
        if row['value'] > clients_max: # still scaling up
            clients_max = row['value']
            first_max_client_idx = idx
        elif row['value'] == clients_max: # found the ceiling?
            stable_peak_found = True
            last_max_client_idx = idx
            continue
        else: # val < clients_max, scaling down again
            if not stable_peak_found: # no stable peak level so use lower values
                last_max_client_idx = idx
            # probably scaling down again
            continue

    if last_max_client_idx == None:
        last_max_client_idx = last_idx

    # In the case that we never reached a stable roof, then ensure that
    # last_max_client_idx is at least 4 from the end
    end_buffer_min_size = 4
    if last_max_client_idx + end_buffer_min_size >= len(clients_active):
        last_max_client_idx = len(clients_active) - (end_buffer_min_size + 1)

    idx_range = last_max_client_idx - first_max_client_idx
    sample_period_start_idx = __round(first_max_client_idx + math.floor(idx_range * 0.7)) # start from 70% into max_load period
    sample_period_stop_idx = __round(first_max_client_idx + math.floor(idx_range * 0.9)) # stop at 90% into max_load period
    return {"start_row": clients_active[sample_period_start_idx],
            "end_row": clients_active[sample_period_stop_idx]}

def __make_charts(run_id, metrics, dest_dir, config_run):
    flows_per_sec = __flows_per_sec(metrics)
    time_labels = report.make_time_labels(flows_per_sec['keys'])
    active_clients = __get_metric(CLIENTS_ACTIVE_KEY, metrics, 1)
    load_times = __get_metric(USER_LOAD_TIME_KEY, metrics, 1000.0)

    report_path = __report_path(run_id)
    chart = report.pygal_line()
    chart.title = 'Flows per second / active clients'
    chart.x_labels = time_labels
    chart.add('Flows/s', flows_per_sec['vals'])
    chart.add('Active clients', active_clients['vals'], secondary=True)
    chart.render_to_file(report_path + "/" + dest_dir + "/flows_clients.svg")

    report_path = __report_path(run_id)
    chart = report.pygal_line()
    chart.title = 'Flows per second / Avg load time (s)'
    chart.x_labels = time_labels
    chart.add('Flows/s', flows_per_sec['vals'])
    chart.add('Avg load time (s)', load_times['vals'], secondary=True)
    chart.render_to_file(report_path + "/" + dest_dir + "/flows_loadtime.svg")

    report_path = __report_path(run_id)
    chart = report.pygal_line()
    chart.title = 'Flows per second'
    chart.x_labels = time_labels
    chart.add('Flows/s', flows_per_sec['vals'])
    chart.render_to_file(report_path + "/" + dest_dir + "/load.svg")

    time_labels = report.make_time_labels(load_times['keys'])
    chart = report.pygal_line()
    chart.title = 'Avg load time (s)'
    chart.x_labels = time_labels
    chart.add('Avg (s)', load_times['vals'])
    chart.render_to_file(report_path + "/" + dest_dir + "/duration.svg")

    active_clients = __get_metric(CLIENTS_ACTIVE_KEY, metrics, 1)
    time_labels = report.make_time_labels(active_clients['keys'])
    chart = report.pygal_line()
    chart.title = 'Active clients'
    chart.x_labels = time_labels
    chart.add('Active clients', active_clients['vals'])
    chart.render_to_file(report_path + "/" + dest_dir + "/active.svg")

def __page_markup(title, subtitle, config_run, metrics, dest_dir, run_id):
    markup = report.header(title, subtitle, "Scenarios")
    markup += """
"""

    scenarios = config_run["config_params"]["scenarios"]
    for scenario_name, scenario_config in scenarios.items():
        markup += __scenario_markup(scenario_name, scenario_config, metrics, dest_dir, run_id)
    markup += """
"""

    markup += report.section_title("General results for whole test config", "Normally less useful than scenario stats above because aggregated between different scenarios! Note that flows per second is not adjusted for multiple actions per scenario, so even if a scenario executes 100 actions in one scenario, the whole scenario is calculated as one action.")

    for chart in ["flows_clients", "flows_loadtime", "load", "duration", "active"]:
        markup += report.chart_markup(chart)
    markup += report.footer()
    return markup

def __scenario_app_page_metrics(scenario_name, metrics, scenario_config):
    # For legacy tests where "app" page does not wrap the whole test, and where there was only one scenario per
    # test config, we use the whole test config load time instead.
    # An example is has-session which does one login and 100 has-session calls.
    if "use-scenario-load-time" in scenario_config:
        return __get_metric_as_actions_per_sec(USER_LOAD_TIME_KEY, metrics, 1000.0)
    else:
        scenario_id = scenario.get(scenario_name)["id"]
        app_page_metric_id = __scenario_app_page_metric_id(scenario_id, scenario_config)
        return metrics[app_page_metric_id]

def __scenario_core_action_metrics(scenario_name, metrics, scenario_config):
    scenario_id = scenario.get(scenario_name)["id"]
    metric_id = __core_actions_metric_id(scenario_id)

    sub_metrics = metrics[metric_id]
    return sub_metrics

def __correctness_metrics(scenario_name, metrics, scenario_config):
    scenario_id = scenario.get(scenario_name)["id"]
    metric_id = __correctness_metric_id(scenario_id, scenario_name)

    sub_metrics = metrics[metric_id]
    return sub_metrics

def scenario_peak_core_actions_per_sec(scenario_name, metrics, scenario_config):
    core_action_metrics = __scenario_core_action_metrics(scenario_name, metrics, scenario_config)
    if not core_action_metrics:
        return []
    sample_period_metrics = __extract_sample_metrics(core_action_metrics, metrics['sample_period'])
    return __add_active_clients_from_sub_metrics(scenario_config, __core_actions_per_sec(sample_period_metrics, metrics, scenario_config), metrics)

def scenario_peak_core_actions_per_sec_avg(scenario_name, metrics, scenario_config):
    actions_per_sec = scenario_peak_core_actions_per_sec(scenario_name, metrics, scenario_config)
    return report.arr_avg(actions_per_sec, "actions_per_sec")

def __scenario_markup(scenario_name, scenario_config, metrics, dest_dir, run_id):
    markup = ""

    # add summary line here with core results for scenario, create and show scenario chart
    peak_actions_per_sec = scenario_peak_core_actions_per_sec(scenario_name, metrics, scenario_config)
    if not peak_actions_per_sec:
        print("No data found for scenario (%s) %s" % (run_id, scenario_name))
        markup += "
No data found for scenario (%s) %s

" % (run_id, scenario_name)
        return markup
    peak_actions_per_sec_avg = report.arr_avg(peak_actions_per_sec, "actions_per_sec")

    peak_active_clients = map(lambda row: row["proportional_clients_active"], peak_actions_per_sec)
    # get peak users avg
    peak_clients_avg = int(numpy.mean(peak_active_clients))

    # Get whole test period chart for actions/s
    core_action_metrics = __scenario_core_action_metrics(scenario_name, metrics, scenario_config)
    actions_per_sec = __core_actions_per_sec(core_action_metrics, metrics, scenario_config)
    sub_metrics = actions_per_sec
    chart_title = "actions per sec"
    y_label = "Actions/s"
    y_key = "actions_per_sec"
    markup += __chart_and_markup(chart_title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, True)

    # peak period action/s chart
    sub_metrics = peak_actions_per_sec
    chart_title = "peak traffic actions"
    y_label = "Actions/s"
    y_key = "actions_per_sec"
    markup += __chart_and_markup(chart_title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, False)

    # Get whole test period chart for avg duration
    app_page_metrics = __scenario_app_page_metrics(scenario_name, metrics, scenario_config)
    sub_metrics = app_page_metrics
    chart_title = "avg total duration"
    y_label = "Avg total duration (s)"
    y_key = "avg"
    markup += __chart_and_markup(chart_title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, True)

    # Get whole test period chart for avg core action duration
    sub_metrics = core_action_metrics
    chart_title = "core action duration"
    y_label = "Avg duration (s)"
    y_key = "avg"
    markup += __chart_and_markup(chart_title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, True)

    # Get whole test period chart for correctness
    sub_metrics = __correctness_metrics(scenario_name, metrics, scenario_config)
    chart_title = "correctness"
    y_label = "Correctness (1 = 100% correct)"
    markup += __chart_and_markup(chart_title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, True)

    return markup

def __add_active_clients_from_sub_metrics(scenario_config, sub_metrics, metrics):
    ret = []
    for row in sub_metrics:
        if "clients_active" in row:
            return # already has clients_active data so ignore
        row["clients_active"] = __clients_active_for_row(row, metrics)
        row["proportional_clients_active"] = row["clients_active"] * (scenario_config['percent-of-users'] / 100.0) # multiply by the fraction of users reserved for this scenario
        ret.append(row)
    return ret

def __chart_and_markup(title, scenario_name, scenario_config, run_id, y_label, y_key, peak_actions_per_sec_avg, peak_clients_avg, sub_metrics, metrics, dest_dir, display_active_clients):
    markup = ""
    # strip alphanum
    pattern = re.compile('[\W_]+')
    chart_id = "." + pattern.sub('', title)

    chart_name = scenario_name + chart_id
    chart_title = scenario_name + ': ' + title + (" (users: %d)" % (peak_clients_avg))
    active_clients = None
    if display_active_clients:
        sub_metrics = __add_active_clients_from_sub_metrics(scenario_config, sub_metrics, metrics)
        active_clients = map(lambda row: row["proportional_clients_active"], sub_metrics)

    report.make_time_chart(__report_path(run_id), metrics, chart_title, chart_name, run_id, y_label, sub_metrics, y_key, dest_dir, display_active_clients, active_clients)

    markup += ("""

Avg %s for scenario: %s, """ % (title, scenario_name))
    markup += ("""Peak period average actions/s: %.02f
""" % peak_actions_per_sec_avg)
    markup += report.chart_markup(chart_name)
    markup += "
"

    return markup

def __runtime_charts(run_id, metrics, config_run):
    __make_charts(run_id, metrics, "runtime", config_run)

def __get_timestamp_closest_value(ts_indexed_rows, needle_ts, value_name="value"):
    closest_ts = needle_ts
    closest_ts_val = ts_indexed_rows[closest_ts][value_name]
    return closest_ts_val

    # didn't get bisect to work correctly but in any case it should not be necessary because all timestamps seem to be present in li_clients_active
    # keys = ts_indexed_rows.keys()
    # closest_ts = __find_le(keys, needle_ts)
    # return closest_ts_val

# def __find_le(l, x):
#     'Find rightmost value less than or equal to x in list a'
#     pos = bisect.bisect(l, x)
#     if l[pos + 1] == x: # exact value exists
#         return x
#     return l[pos]

# Get the active_clients value closest to the timestamp of the row
def __clients_active_for_row(row, metrics):
    row_timestamp = row["timestamp"]
    closest_clients_active = __get_timestamp_closest_value(metrics[CLIENTS_ACTIVE_KEY + ".timestamp_idx"], row_timestamp)
    return closest_clients_active

def  __core_actions_per_sec(sub_metrics, metrics, scenario_config):
    exit
    ret = []
    last_row = None
    for row in sub_metrics:
        if last_row:
            time_period_length = (row["timestamp"] - last_row["timestamp"]) / 1000000.0
            actions_per_sec = row["count"] / time_period_length # count of actions / period duration
        else:
            actions_per_sec = 0.0
        ret.append({
            "timestamp": row["timestamp"],
            "offset": row["offset"],
            "actions_per_sec": actions_per_sec
        })
        last_row = row
    return ret

# extract data from the sample period
def __extract_sample_metrics(sub_metrics, sample_period):
    start_idx = None
    end_idx = None
    for idx, row in enumerate(sub_metrics):
        if row["timestamp"] < sample_period["start_row"]["timestamp"]: # before sample period
            continue
        elif row["timestamp"] == sample_period["end_row"]["timestamp"]: # found row matching end_row timestamp, use as end_idx
            end_idx = idx
            break
        elif row["timestamp"] > sample_period["end_row"]["timestamp"]: # found row surpassing end_row timestamp, return previous row
            end_idx = idx - 1
            break
        elif row["timestamp"] >= sample_period["start_row"]["timestamp"]: # found first row >= start_row timestamp, use as start_idx
            if not start_idx:
                start_idx = idx
    return sub_metrics[start_idx:end_idx]

def __complete_charts(run_id, metrics, title, config_run):
    if title == None:
        title = ""

    subtitle = "Run " + repr(run_id) + ", " + report.get_start_time(metrics)
    __make_charts(run_id, metrics, "complete", config_run)

    file = open(complete_report_url(run_id), 'w') # Trying to create a new file or open one
    file.write(__page_markup(title, subtitle, config_run, metrics, "complete", run_id))
    file.close()

def complete_report_url(run_id):
    report_path = __report_path(run_id)
    return report_path + "/complete/complete_report.html"

max_stars_repo_name: schibsted/overloadimpact

max_issues_repo_name: schibsted/overloadimpact

max_forks_repo_name: schibsted/overloadimpact


text:
#!/opt/conda/bin/python

# replace pylab import
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pylab as pylab
import numpy

de = numpy.genfromtxt("total internal energy_EDRAHT.txt")
dm = numpy.genfromtxt("forces fx,fy,fz_NROT.txt")
pylab.plot(de[:,0],de[:,1],'b',dm[:,0],dm[:,3],'r')
pylab.grid(True)
pylab.xlim([0,1])
pylab.xlabel("t")
pylab.ylabel("y")
pylab.legend(["Energy","Moment"],loc=0)
pylab.savefig("Biegung-history")

max_stars_repo_name: opme/CalculiX-Examples

max_issues_repo_name: opme/CalculiX-Examples

max_forks_repo_name: opme/CalculiX-Examples


text:
import numpy as np

class Base(object):

    def __init__(self, index, feature=np.array([0.])):
        self.index = index
        self.feature = feature

    def encode(self, dim=None,
               index=True, feature=True,
               vertical=False):

        if not dim:
            dim = self.index + 1

        x = np.array([])

        if index:
            x = np.concatenate((x, self.index_one_hot(dim)))

        if feature:
            x = np.concatenate((x, self.feature))

        return x if not vertical else np.array([x]).T

    def index_one_hot(self, dim):
        if self.index >= dim:
            raise ValueError('number of dimensions must be greater than index: %d' % self.index)

        x = np.zeros(dim)
        x[self.index] = 1.
        return x

class User(Base):

    def __str__(self):
        if len(self.feature) == 1 and self.feature[0] == 0.:
            return 'User(index={})'.format(self.index)
        else:
            return 'User(index={}, feature={})'.format(self.index, self.feature)

class Item(Base):

    def __str__(self):
        if len(self.feature) == 1 and self.feature[0] == 0.:
            return 'Item(index={})'.format(self.index)
        else:
            return 'Item(index={}, feature={})'.format(self.index, self.feature)

class Event(object):

    def __init__(self, user, item, value=1., context=np.array([0.])):
        self.user = user
        self.item = item
        self.value = value
        self.context = context

    def encode(self, n_user=None, n_item=None,
               index=True, feature=True, context=True,
               vertical=False):

        x = self.user.encode(dim=n_user, index=index,
                             feature=feature, vertical=False)

        if context:
            x = np.concatenate((x, self.context))

        iv = self.item.encode(dim=n_item, index=index,
                              feature=feature, vertical=False)
        x = np.concatenate((x, iv))

        return x if not vertical else np.array([x]).T

    def __str__(self):
        if len(self.context) == 1 and self.context[0] == 0.:
            return 'Event(user={}, item={}, value={})'.format(self.user, self.item, self.value)
        else:
            return 'Event(user={}, item={}, value={}, context={})'.format(self.user, self.item, self.value, self.context)

max_stars_repo_name: KevinGetandGive/flurs

max_issues_repo_name: KevinGetandGive/flurs

max_forks_repo_name: KevinGetandGive/flurs


text:
"""
Function for creating and saving output data

    Parameters
    ----------

    Returns
    -------

    Notes
    -----
    uncomment last line to create output data
"""
import numpy as np

from initGEQ import initGEQ
from getErrors import getErrors

def createOutputData():
    dataInput = np.loadtxt('data/test/dataInputTest.csv', delimiter=',')
    dataOutput = np.zeros((len(dataInput),31))
    dataError = np.zeros((len(dataInput),3))
    dataErrorAbsolute = np.zeros((len(dataInput),31))

    for i in range((len(dataInput))):
        [numsopt,densopt,fs,fc2,G_db2,G2opt_db,fc1,bw] = initGEQ(dataInput[i].reshape(31,1))
        dataOutput[i] = G2opt_db.T
        helper = getErrors(numsopt,densopt,fs,dataInput[i].reshape(31,1),fc1)
        dataError[i] = helper[0:3]
        dataErrorAbsolute[i] = helper[3:4][0].reshape(1,31)

    # define data
    data = np.asarray(dataOutput, dtype=np.float64,)
    # save to csv file
    np.savetxt('data/dataOutputTestNew.csv', data, delimiter=',')

    # define data
    dataErr = np.asarray(dataError, dtype=np.float64,)
    dataErrAbsolute = np.asarray(dataErrorAbsolute, dtype=np.float64,)
    # save to csv file
    np.savetxt('data/dataErrorTestNew.csv', dataErr, delimiter=',')
    np.savetxt('data/dataErrorTestNewAbsolute.csv', dataErrAbsolute, delimiter=',')

# create output data
#createOutputData()

max_stars_repo_name: spatialaudio/geq-design

max_issues_repo_name: spatialaudio/geq-design

max_forks_repo_name: spatialaudio/geq-design


text:
# coding: utf-8

import argparse
import importlib
import json
import numpy as np
import os
import pytest
import tempfile
import torch

from espnet.nets.pytorch_backend.nets_utils import pad_list

def make_arg(**kwargs):
    train_defaults = dict(
        elayers=1,
        subsample="1_2_2_1_1",
        etype="vggblstm",
        eunits=16,
        eprojs=8,
        dtype="lstm",
        dlayers=1,
        dunits=16,
        atype="location",
        aheads=2,
        awin=5,
        aconv_chans=4,
        aconv_filts=10,
        mtlalpha=0.5,
        lsm_type="",
        lsm_weight=0.0,
        sampling_probability=0.0,
        adim=16,
        dropout_rate=0.0,
        dropout_rate_decoder=0.0,
        nbest=5,
        beam_size=2,
        penalty=0.5,
        maxlenratio=1.0,
        minlenratio=0.0,
        ctc_weight=0.2,
        lm_weight=0.0,
        rnnlm=None,
        verbose=2,
        char_list=["a", "e", "i", "o", "u"],
        outdir=None,
        ctc_type="warpctc",
        report_cer=False,
        report_wer=False,
        sym_space="<space>",
        sym_blank="<blank>",
        replace_sos=False,
        tgt_lang=False,
        enc_init=None,
        enc_init_mods='enc.',
        dec_init=None,
        dec_init_mods='dec.,att.',
        model_module='espnet.nets.pytorch_backend.e2e_asr:E2E'
    )
    train_defaults.update(kwargs)

    return argparse.Namespace(**train_defaults)

def get_default_scope_inputs():
    idim = 40
    odim = 5
    ilens = [20, 15]
    olens = [4, 3]

    return idim, odim, ilens, olens

def pytorch_prepare_inputs(idim, odim, ilens, olens, is_cuda=False):
    np.random.seed(1)

    xs = [np.random.randn(ilen, idim).astype(np.float32) for ilen in ilens]
    ys = [np.random.randint(1, odim, olen).astype(np.int32) for olen in olens]
    ilens = np.array([x.shape[0] for x in xs], dtype=np.int32)

    xs_pad = pad_list([torch.from_numpy(x).float() for x in xs], 0)
    ys_pad = pad_list([torch.from_numpy(y).long() for y in ys], -1)
    ilens = torch.from_numpy(ilens).long()

    if is_cuda:
        xs_pad = xs_pad.cuda()
        ys_pad = ys_pad.cuda()
        ilens = ilens.cuda()

    return xs_pad, ilens, ys_pad

@pytest.mark.parametrize("enc_init, enc_mods, dec_init, dec_mods, mtlalpha", [
    (None, "enc.", None, "dec., att.", 0.0),
    (None, "enc.", None, "dec., att.", 0.5),
    (None, "enc.", None, "dec., att.", 1.0),
    (True, "enc.", None, "dec., att.", 0.5),
    (None, "enc.", True, "dec., att.", 0.0),
    (None, "enc.", True, "dec., att.", 0.5),
    (None, "enc.", True, "dec., att.", 1.0),
    (True, "enc.", True, "dec., att.", 0.0),
    (True, "enc.", True, "dec., att.", 0.5),
    (True, "enc.", True, "dec., att.", 1.0),
    (True, "test", None, "dec., att.", 0.0),
    (True, "test", None, "dec., att.", 0.5),
    (True, "test", None, "dec., att.", 1.0),
    (None, "enc.", True, "test", 0.0),
    (None, "enc.", True, "test", 0.5),
    (None, "enc.", True, "test", 1.0),
    (True, "enc.enc.0", None, "dec., att.", 0.0),
    (True, "enc.enc.0", None, "dec., att.", 0.5),
    (True, "enc.enc.0", None, "dec., att.", 1.0),
    (None, "enc.", True, "dec.embed.", 0.0),
    (None, "enc.", True, "dec.embed.", 0.5),
    (None, "enc.", True, "dec.embed.", 1.0),
    (True, "enc.enc.0, enc.enc.1", True, "dec., att.", 0.0),
    (True, "enc.enc.0", True, "dec.embed.,dec.decoder.1", 0.5),
    (True, "enc.enc.0, enc.enc.1", True, "dec.embed.,dec.decoder.1", 1.0)])
def test_pytorch_trainable_transferable_and_decodable(enc_init, enc_mods, dec_init, dec_mods, mtlalpha):
    idim, odim, ilens, olens = get_default_scope_inputs()
    args = make_arg()

    module = importlib.import_module('espnet.nets.pytorch_backend.e2e_asr')
    model = module.E2E(idim, odim, args)

    batch = pytorch_prepare_inputs(idim, odim, ilens, olens)

    loss = model(*batch)
    loss.backward()

    with torch.no_grad():
        in_data = np.random.randn(20, idim)
        model.recognize(in_data, args, args.char_list)

    if not os.path.exists(".pytest_cache"):
        os.makedirs(".pytest_cache")
    utils = importlib.import_module('espnet.asr.asr_utils')

    tmppath = tempfile.mktemp()
    utils.torch_save(tmppath, model)

    if enc_init is not None:
        enc_init = tmppath
    if dec_init is not None:
        dec_init = tmppath

    # create dummy model.json for saved model to go through
    # get_model_conf(...) called in load_trained_modules method.
    model_conf = os.path.dirname(tmppath) + '/model.json'
    with open(model_conf, 'wb') as f:
        f.write(json.dumps((40, 5, vars(args)),
                           indent=4, ensure_ascii=False, sort_keys=True).encode('utf_8'))

    args = make_arg(enc_init=enc_init, enc_init_mods=enc_mods,
                    dec_init=dec_init, dec_init_mods=dec_mods,
                    mtlalpha=mtlalpha)
    transfer = importlib.import_module('espnet.asr.pytorch_backend.asr_init')
    model = transfer.load_trained_modules(40, 5, args)

    loss = model(*batch)
    loss.backward()

    with torch.no_grad():
        in_data = np.random.randn(20, idim)
        model.recognize(in_data, args, args.char_list)


text:
# Copyright 2021 Variscite LTD
# SPDX-License-Identifier: BSD-3-Clause

"""
:platform: Unix/Yocto
:synopsis: Class to handle overlay on single images and frames.

.. moduleauthor:: Diego Dorta <diego.d@variscite.com>
"""

import colorsys
import random

import cv2
import numpy as np

from pyvar.ml.config import CLASSIFICATION
from pyvar.ml.config import DETECTION
from pyvar.ml.utils.config import FONT, FPS_MSG, INF_TIME_MSG

class Overlay:
    """
    :ivar inference_time_info: shows the inference time on image/frame;
    :ivar scores_info: shows the scores on image/frame;
    :ivar extra_info: shows extra info on image/frame;
    :ivar framerate_info: shows framerate on image/frame.
    """
    def __init__(self):
        """
        Constructor method for the Label class.
        """
        self.inference_time_info = True
        self.scores_info = True
        self.extra_info = True
        self.framerate_info = False

    @staticmethod
    def generate_colors(labels):
        hsv_tuples = [(x / len(labels), 1., 1.) for x in range(len(labels))]
        colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        colors = list(map(lambda x: (int(x[0] * 255),
                                     int(x[1] * 255),
                                     int(x[2] * 255)), colors))
        random.seed(10101)
        random.shuffle(colors)
        random.seed(None)
        return colors

    def info(self, category=None, image=None, top_result=None,
             labels=None, inference_time=None, model_name=None,
             source_file=None, fps=None):
        """
        Draw information on single images and frames such as inference time,
        scores, model name, and source file.

        Args:
            category (str): model category (classification or detection);
            image (numpy array): original image to overlay the information.
            top_result (list): top results from the inference.
            labels (list): list of the read labels.
            inference_time (str): inference time from TFLiteInterpreter class.
            model_name (str): the model name.
            source_file (str): the source file name.
            fps (float): fpsit from Framerate class.

        Returns:
            The :obj:`numpy.array` image format with the overlaid information.
        """
        inference_position = (3, 20)
        if self.scores_info:
            if category is CLASSIFICATION:
                for idx, (i, score) in enumerate(top_result):
                    label_position = (3, 35 * idx + 60)
                    cv2.putText(
                        image,
                        f"{labels[i]} - {score:0.4f}",
                        label_position,
                        FONT['hershey'],
                        FONT['size'],
                        FONT['color']['black'],
                        FONT['thickness'] + 2)
                    cv2.putText(
                        image,
                        f"{labels[i]} - {score:0.4f}",
                        label_position,
                        FONT['hershey'],
                        FONT['size'],
                        FONT['color']['blue'],
                        FONT['thickness'])
            elif category is DETECTION:
                colors = self.generate_colors(labels)
                image_height, image_width, _ = image.shape
                for obj in top_result:
                    pos = obj['pos']
                    _id = obj['_id']

                    x1 = int(pos[1] * image_width)
                    x2 = int(pos[3] * image_width)
                    y1 = int(pos[0] * image_height)
                    y2 = int(pos[2] * image_height)

                    top = max(0, np.floor(y1 + 0.5).astype('int32'))
                    left = max(0, np.floor(x1 + 0.5).astype('int32'))
                    bottom = min(image_height, np.floor(y2 + 0.5).astype('int32'))
                    right = min(image_width, np.floor(x2 + 0.5).astype('int32'))

                    label_size = cv2.getTextSize(
                                     labels[_id], FONT['hershey'],
                                     FONT['size'], FONT['thickness'])[0]

                    label_rect_left = int(left - 3)
                    label_rect_top = int(top - 3)
                    label_rect_right = int(left + 3 + label_size[0])
                    label_rect_bottom = int(top - 5 - label_size[1])

                    cv2.rectangle(
                        image, (left, top), (right, bottom),
                        colors[int(_id) % len(colors)], 6)
                    cv2.rectangle(
                        image, (label_rect_left, label_rect_top),
                        (label_rect_right, label_rect_bottom),
                        colors[int(_id) % len(colors)], -1)
                    cv2.putText(
                        image, labels[_id], (left, int(top - 4)),
                        FONT['hershey'], FONT['size'],
                        FONT['color']['black'], FONT['thickness'])

        if self.inference_time_info:
            cv2.putText(
                image, f"{INF_TIME_MSG}: {inference_time}", inference_position,
                FONT['hershey'], 0.5, FONT['color']['black'], 2, cv2.LINE_AA)
            cv2.putText(
                image, f"{INF_TIME_MSG}: {inference_time}", inference_position,
                FONT['hershey'], 0.5, FONT['color']['white'], 1, cv2.LINE_AA)

        if self.extra_info:
            y_offset = image.shape[0] - cv2.getTextSize(
                                            source_file, FONT['hershey'],
                                            0.5, 2)[0][1]
            cv2.putText(
                image, f"source: {source_file}", (3, y_offset), FONT['hershey'],
                0.5, FONT['color']['black'], 2, cv2.LINE_AA)
            cv2.putText(
                image, f"source: {source_file}", (3, y_offset), FONT['hershey'],
                0.5, FONT['color']['white'], 1, cv2.LINE_AA)
            y_offset -= (cv2.getTextSize(
                             model_name, FONT['hershey'], 0.5, 2)[0][1] + 3)
            cv2.putText(
                image, f"model: {model_name}", (3, y_offset), FONT['hershey'],
                0.5, FONT['color']['black'], 2, cv2.LINE_AA)
            cv2.putText(
                image, f"model: {model_name}", (3, y_offset), FONT['hershey'],
                0.5, FONT['color']['white'], 1, cv2.LINE_AA)

        if self.framerate_info:
            fps_msg = f"{FPS_MSG}: {int(fps)}"
            x_offset = image.shape[1] - (cv2.getTextSize(
                                             fps_msg, FONT['hershey'],
                                             0.8, 2)[0][0] + 10)
            cv2.putText(
                image, fps_msg, (x_offset, 25), FONT['hershey'], 0.8,
                FONT['color']['black'], 2, cv2.LINE_AA)
            cv2.putText(
                image, fps_msg, (x_offset, 25), FONT['hershey'], 0.8,
                FONT['color']['white'], 1, cv2.LINE_AA)
        return image

max_stars_repo_name: dorta/pyvar

max_issues_repo_name: varigit/pyvar

max_forks_repo_name: dorta/pyvar


text:
"""
TSP Crossover implementation for GA.
"""

# Author: Genevieve Hayes (modified by Andrew Rollings)
# License: BSD 3 clause

import numpy as np

from mlrose.algorithms.crossovers._crossover_base import _CrossOverBase

class TSPCrossOver(_CrossOverBase):

    def __init__(self, opt_prob):
        super().__init__(opt_prob)

    def mate(self, p1, p2):
        return self._mate_fill(p1, p2)
        """
        if np.random.randint(2) == 0:
            return self._mate_fill(p1, p2)
        else:
            return self._mate_traverse(p1, p2)
        """

    def _mate_fill(self, p1, p2):
        if self._length > 1:
            n = 1 + np.random.randint(self._length - 1)
            child = np.array([0] * self._length)
            child[:n] = p1[:n]

            unvisited = [node for node in p2 if node not in p1[:n]]
            child[n:] = unvisited
        elif np.random.randint(2) == 0:
            child = np.copy(p1)
        else:
            child = np.copy(p2)
        return child

    def _mate_traverse(self, parent_1, parent_2):
        if self._length > 1:
            next_a = np.append(parent_1[1:], parent_1[-1])
            next_b = np.append(parent_2[1:], parent_2[-1])

            visited = [False] * self._length
            child = np.array([0] * self._length)

            v = np.random.randint(len(parent_1))
            child[0] = v
            visited[v] = True
            for i in range(1, len(child)):
                cur = child[i-1]
                na = next_a[cur]
                nb = next_b[cur]
                va = visited[na]
                vb = visited[nb]
                if va and not vb:
                    nx = nb
                elif not va and vb:
                    nx = na
                elif not va and not vb:
                    fa = self._opt_prob.fitness_fn.calculate_fitness([cur, na])
                    fb = self._opt_prob.fitness_fn.calculate_fitness([cur, nb])
                    nx = nb if fa > fb else na  # opposite because they're distance and smaller is better
                else:
                    while True:
                        nx = np.random.randint(len(parent_1))
                        if not visited[nx]:
                            break
                child[i] = nx
                visited[nx] = True
        elif np.random.randint(2) == 0:
            child = np.copy(parent_1, copy=True)
        else:
            child = np.copy(parent_2, copy=True)
        return child

max_stars_repo_name: tapos/mlrose

max_issues_repo_name: tapos/mlrose

max_forks_repo_name: tapos/mlrose


text:
import os
import sys
import argparse
import random
from addict import Dict
import numpy as np
import torch

__all__ = ['parse_inference_args', 'seed_everything', 'HiddenPrints']
__all__ += ['get_batch_size', 'infer_bnn']

def infer_bnn(net, bnn_post, param_logL, test_loader,
              batch_size, n_dropout, n_samples_per_dropout,
              device, global_seed=123, mode='default'):
    """Infer with MC dropout

    """
    Y_dim = param_logL.Y_dim
    init_pos = np.empty([batch_size, n_dropout, n_samples_per_dropout, Y_dim])
    mcmc_pred = np.empty([batch_size, n_dropout, param_logL.out_dim])
    with torch.no_grad():
        net.train()
        # Send some empty forward passes through the test data without backprop
        # to adjust batchnorm weights
        # (This is often not necessary. Beware if using for just 1 lens.)
        for nograd_pass in range(5):
            for X_, Y_ in test_loader:
                X = X_.to(device)
                _ = net(X)
        # Obtain MC dropout samples
        for d in range(n_dropout):
            net.eval()
            for X_, Y_ in test_loader:
                X = X_.to(device)
                if mode == 'default_with_truth_mean':
                    Y = Y_.to(device)
                pred = net(X)
                break
            mcmc_pred_d = pred.cpu().numpy()
            # Replace BNN posterior's primary gaussian mean with truth values
            if mode == 'default_with_truth_mean':
                mcmc_pred_d[:, :Y_dim] = Y[:, :Y_dim].cpu().numpy()
            # Leave only the MCMC parameters in pred
            mcmc_pred_d = param_logL.remove_params_from_pred(mcmc_pred_d,
                                                             return_as_tensor=False)
            # Populate pred that will define the MCMC penalty function
            mcmc_pred[:, d, :] = mcmc_pred_d
            # Instantiate posterior to generate BNN samples, which will serve as
            # initial positions for walkers
            bnn_post.set_sliced_pred(mcmc_pred_d)
            init_pos[:, d, :, :] = bnn_post.sample(n_samples_per_dropout,
            sample_seed=global_seed+d)  # just the lens model params, no D_dt
    return init_pos, mcmc_pred

def get_batch_size(cfg_lens_indices, cfg_n_test, args_lens_indices_path):
    """Figure out how many consecutive lenses BNN will predict on

    Parameters
    ----------
    cfg_lens_indices : list
        lens indices specified in the config file
    cfg_n_test : int
        number of test lenses specified in the config file
    args_lens_indices_path : os.path instance or str
        path to the text file containing lens indices, from the command line

    """
    if cfg_lens_indices is None:
        if args_lens_indices_path is None:
            # Test on all n_test lenses in the test set
            n_test = cfg_n_test
            lens_range = range(n_test)
        else:
            # Test on the lens indices in a text file at the specified path
            lens_range = []
            with open(args_lens_indices_path, "r") as f:
                for line in f:
                    lens_range.append(int(line.strip()))
            n_test = len(lens_range)
            msg = ("Performing H0 inference on {n_test}"
                   " specified lenses...")
            print(msg)
    else:
        if args_lens_indices_path is None:
            # Test on the lens indices specified in the test config file
            lens_range = cfg_lens_indices
            n_test = len(lens_range)
            msg = ("Performing H0 inference on {n_test}"
                   " specified lenses...")
            print(msg)
        else:
            raise ValueError("Specific lens indices were specified in both the"
                             " test config file and the command-line argument.")
    batch_size = max(lens_range) + 1
    return batch_size, n_test, lens_range

def parse_inference_args():
    """Parse command-line arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('test_config_file_path',
                        help='path to the user-defined test config file')
    parser.add_argument('--lens_indices_path', default=None, dest='lens_indices_path', type=str,
                        help='path to a text file with specific lens indices to test on (Default: None)')
    args = parser.parse_args()
    # sys.argv rerouting for setuptools entry point
    if args is None:
        args = Dict()
        args.user_cfg_path = sys.argv[0]
        #args.n_data = sys.argv[1]
    return args

def seed_everything(global_seed):
    """Seed everything for reproducibility

    global_seed : int
        seed for `np.random`, `random`, and relevant `torch` backends

    """
    np.random.seed(global_seed)
    random.seed(global_seed)
    torch.manual_seed(global_seed)
    torch.cuda.manual_seed(global_seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

class HiddenPrints:
    """Hide standard output

    """
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.close()
        sys.stdout = self._original_stdout


text:
#!/usr/bin/env python
from __future__ import absolute_import, unicode_literals, print_function
__doc__ = """
Script that does default visualizations (marginal plots, 1-d and 2-d).

Author: Johannes Buchner (C) 2013
"""
import numpy
from numpy import exp, log
import matplotlib.pyplot as plt
import sys, os
import json
import pymultinest

# if len(sys.argv) != 2:
# 	sys.stderr.write("""SYNOPSIS: %s <output-root>
#
# 	output-root: 	Where the output of a MultiNest run has been written to.
# 	            	Example: chains/1-
# %s""" % (sys.argv[0], __doc__))
# 	sys.exit(1)

# prefix = sys.argv[1]

prefix='planets/m10.0_a0.1_e0.0_omega0.0_ps1_'
print('model "%s"' % prefix)
parameters = json.load(open(prefix + 'params.json'))
n_params = len(parameters)

a = pymultinest.Analyzer(n_params = n_params, outputfiles_basename = prefix)
a.get_mode_stats()
s = a.get_stats()

json.dump(s, open(prefix + 'stats.json', 'w'), indent=4)

print('  marginal likelihood:')
print('    ln Z = %.1f +- %.1f' % (s['global evidence'], s['global evidence error']))
print('  parameters:')
for p, m in zip(parameters, s['marginals']):
	lo, hi = m['1sigma']
	med = m['median']
	sigma = (hi - lo) / 2
	i = max(0, int(-numpy.floor(numpy.log10(sigma))) + 1)
	fmt = '%%.%df' % i
	fmts = '\t'.join(['    %-15s' + fmt + " +- " + fmt])
	print(fmts % (p, med, sigma))

print('creating marginal plot ...')
p = pymultinest.PlotMarginal(a)

values = a.get_equal_weighted_posterior()
assert n_params == len(s['marginals'])
modes = s['modes']

dim2 = os.environ.get('D', '1' if n_params > 20 else '2') == '2'
nbins = 100 if n_params < 3 else 20
if dim2:
	plt.figure(figsize=(5*n_params, 5*n_params))
	for i in range(n_params):
		plt.subplot(n_params, n_params, i + 1)
		plt.xlabel(parameters[i])

		m = s['marginals'][i]
		plt.xlim(m['5sigma'])

		oldax = plt.gca()
		x,w,patches = oldax.hist(values[:,i], bins=nbins, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2)
		oldax.set_ylim(0, x.max())

		newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False)
		p.plot_marginal(i, ls='-', color='blue', linewidth=3)
		newax.set_ylim(0, 1)

		ylim = newax.get_ylim()
		y = ylim[0] + 0.05*(ylim[1] - ylim[0])
		center = m['median']
		low1, high1 = m['1sigma']
		#print(center, low1, high1)
		newax.errorbar(x=center, y=y,
			xerr=numpy.transpose([[center - low1, high1 - center]]),
			color='blue', linewidth=2, marker='s')
		oldax.set_yticks([])
		#newax.set_yticks([])
		newax.set_ylabel("Probability")
		ylim = oldax.get_ylim()
		newax.set_xlim(m['5sigma'])
		oldax.set_xlim(m['5sigma'])
		#plt.close()

		for j in range(i):
			plt.subplot(n_params, n_params, n_params * (j + 1) + i + 1)
			p.plot_conditional(i, j, bins=20, cmap = plt.cm.gray_r)
			for m in modes:
				plt.errorbar(x=m['mean'][i], y=m['mean'][j], xerr=m['sigma'][i], yerr=m['sigma'][j])
			plt.xlabel(parameters[i])
			plt.ylabel(parameters[j])
			#plt.savefig('cond_%s_%s.pdf' % (params[i], params[j]), bbox_tight=True)
			#plt.close()

	plt.savefig(prefix + 'marg.pdf')
	plt.savefig(prefix + 'marg.png')
	plt.close()
else:
	from matplotlib.backends.backend_pdf import PdfPages
	sys.stderr.write('1dimensional only. Set the D environment variable \n')
	sys.stderr.write('to D=2 to force 2d marginal plots.\n')
	pp = PdfPages(prefix + 'marg1d.pdf')

	for i in range(n_params):
		plt.figure(figsize=(3, 3))
		plt.xlabel(parameters[i])
		plt.locator_params(nbins=5)

		m = s['marginals'][i]
		iqr = m['q99%'] - m['q01%']
		xlim = m['q01%'] - 0.3 * iqr, m['q99%'] + 0.3 * iqr
		#xlim = m['5sigma']
		plt.xlim(xlim)

		oldax = plt.gca()
		x,w,patches = oldax.hist(values[:,i], bins=numpy.linspace(xlim[0], xlim[1], 20), edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2)
		oldax.set_ylim(0, x.max())

		newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False)
		p.plot_marginal(i, ls='-', color='blue', linewidth=3)
		newax.set_ylim(0, 1)

		ylim = newax.get_ylim()
		y = ylim[0] + 0.05*(ylim[1] - ylim[0])
		center = m['median']
		low1, high1 = m['1sigma']
		#print center, low1, high1
		newax.errorbar(x=center, y=y,
			xerr=numpy.transpose([[center - low1, high1 - center]]),
			color='blue', linewidth=2, marker='s')
		oldax.set_yticks([])
		newax.set_ylabel("Probability")
		ylim = oldax.get_ylim()
		newax.set_xlim(xlim)
		oldax.set_xlim(xlim)
		plt.savefig(pp, format='pdf', bbox_inches='tight')
		plt.close()
	pp.close()

reason: import numpy,from numpy


text:
import numpy as np
import warnings
from pathlib import Path
from typing import Union, Optional

from spikeinterface.core import (BaseSorting, BaseSortingSegment)
from spikeinterface.core.core_tools import define_function_from_class

from .neobaseextractor import NeoBaseRecordingExtractor, NeoBaseSortingExtractor

try:
    from lxml import etree as et

    HAVE_LXML = True
except ImportError:
    HAVE_LXML = False

PathType = Union[str, Path]
OptionalPathType = Optional[PathType]

class NeuroScopeRecordingExtractor(NeoBaseRecordingExtractor):
    """
    Class for reading data from neuroscope
    Ref: http://neuroscope.sourceforge.net

    Based on :py:class:`neo.rawio.NeuroScopeRawIO`

    Parameters
    ----------
    file_path: str
        The xml file.
    stream_id: str or None
        If several stream, specify the one you want.
    all_annotations: bool  (default False)
        Load exhaustively all annotation from neo.
    """
    mode = 'file'
    NeoRawIOClass = 'NeuroScopeRawIO'

    def __init__(self, file_path, stream_id=None, all_annotations=False):
        neo_kwargs = {'filename': str(file_path)}
        NeoBaseRecordingExtractor.__init__(self, stream_id=stream_id, all_annotations=all_annotations, **neo_kwargs)
        self._kwargs.update(dict(file_path=str(file_path)))

class NeuroScopeSortingExtractor(BaseSorting):
    """
    Extracts spiking information from an arbitrary number of .res.%i and .clu.%i files in the general folder path.

    The .res is a text file with a sorted list of spiketimes from all units displayed in sample (integer '%i') units.
    The .clu file is a file with one more row than the .res with the first row corresponding to the total number of
    unique ids in the file (and may exclude 0 & 1 from this count)
    with the rest of the rows indicating which unit id the corresponding entry in the .res file refers to.
    The group id is loaded as unit property 'group'.

    In the original Neuroscope format:
        Unit ID 0 is the cluster of unsorted spikes (noise).
        Unit ID 1 is a cluster of multi-unit spikes.

    The function defaults to returning multi-unit activity as the first index, and ignoring unsorted noise.
    To return only the fully sorted units, set keep_mua_units=False.

    The sorting extractor always returns unit IDs from 1, ..., number of chosen clusters.

    Parameters
    ----------
    folder_path : str
        Optional. Path to the collection of .res and .clu text files. Will auto-detect format.
    resfile_path : PathType
        Optional. Path to a particular .res text file. If given, only the single .res file
        (and the respective .clu file) are loaded
    clufile_path : PathType
        Optional. Path to a particular .clu text file. If given, only the single .clu file
        (and the respective .res file) are loaded
    keep_mua_units : bool
        Optional. Whether or not to return sorted spikes from multi-unit activity. Defaults to True.
    exclude_shanks : list
        Optional. List of indices to ignore. The set of all possible indices is chosen by default, extracted as the
        final integer of all the .res.%i and .clu.%i pairs.
    xml_file_path : PathType, optional
        Path to the .xml file referenced by this sorting.
    """

    extractor_name = "NeuroscopeSortingExtractor"
    installed = HAVE_LXML
    installation_mesg = "Please install lxml to use this extractor!"

    def __init__(
        self,
        folder_path: OptionalPathType = None,
        resfile_path: OptionalPathType = None,
        clufile_path: OptionalPathType = None,
        keep_mua_units: bool = True,
        exclude_shanks: Optional[list] = None,
        xml_file_path: OptionalPathType = None,
    ):
        assert self.installed, self.installation_mesg

        assert not (folder_path is None and resfile_path is None and clufile_path is None), \
            "Either pass a single folder_path location, or a pair of resfile_path and clufile_path! None received."

        if resfile_path is not None:
            assert clufile_path is not None, "If passing resfile_path or clufile_path, both are required!"
            resfile_path = Path(resfile_path)
            clufile_path = Path(clufile_path)
            assert resfile_path.is_file() and clufile_path.is_file(), \
                f"The resfile_path ({resfile_path}) and clufile_path ({clufile_path}) must be .res and .clu files!"

            assert folder_path is None, "Pass either a single folder_path location, " \
                                        "or a pair of resfile_path and clufile_path! All received."
            folder_path_passed = False
            folder_path = resfile_path.parent
            res_files = [resfile_path]
            clu_files = [clufile_path]
        else:
            assert folder_path is not None, "Either pass resfile_path and clufile_path, or folder_path!"
            folder_path = Path(folder_path)
            assert folder_path.is_dir(), "The folder_path must be a directory!"

            res_files = sorted([f for f in folder_path.iterdir() if f.is_file() and ".res" in f.suffixes and not
                                f.name.endswith("~")])
            clu_files = sorted([f for f in folder_path.iterdir() if f.is_file() and ".clu" in f.suffixes and not
                                f.name.endswith("~")])

            assert len(res_files) > 0 or len(clu_files) > 0, \
                "No .res or .clu files found in the folder_path!"

            folder_path_passed = True  # flag for setting kwargs for proper dumping

        if exclude_shanks is not None:  # dumping checks do not like having an empty list as default
            assert all([isinstance(x, (int, np.integer)) and x >= 0 for x in
                        exclude_shanks]), "Optional argument 'exclude_shanks' must contain positive integers only!"
        else:
            exclude_shanks = []
        xml_file_path = _handle_xml_file_path(
            folder_path=folder_path, initial_xml_file_path=xml_file_path)
        xml_root = et.parse(str(xml_file_path)).getroot()
        sampling_frequency = float(xml_root.find(
            'acquisitionSystem').find('samplingRate').text)

        if len(res_files) > 1:
            res_ids = [int(x.suffix[1:]) for x in res_files]
            clu_ids = [int(x.suffix[1:]) for x in clu_files]
            assert sorted(res_ids) == sorted(
                clu_ids), "Unmatched .clu.%i and .res.%i files detected!"
            if any([x not in res_ids for x in exclude_shanks]):
                warnings.warn(
                    "Detected indices in exclude_shanks that are not in the directory! These will be ignored.")
            shank_ids = res_ids
        else:
            shank_ids = None

        resfile_names = [x.name[:x.name.find('.res')] for x in res_files]
        clufile_names = [x.name[:x.name.find('.clu')] for x in clu_files]
        assert np.all(r == c for (r, c) in zip(resfile_names, clufile_names)), \
            "Some of the .res.%i and .clu.%i files do not share the same name!"

        all_unit_ids = []
        all_spiketrains = []
        all_unit_shank_ids = []
        for i, (res_file, clu_file) in enumerate(zip(res_files, clu_files)):
            if shank_ids is not None:
                shank_id = shank_ids[i]
                if shank_id in exclude_shanks:
                    continue

            with open(res_file) as f:
                res = np.array([int(line) for line in f], np.int64)
            with open(clu_file) as f:
                clu = np.array([int(line) for line in f], np.int64)

            n_spikes = len(res)
            if n_spikes > 0:
                # Extract the number of unique IDs from the first line of the clufile then remove it from the list
                n_clu = clu[0]
                clu = np.delete(clu, 0)
                unique_ids = np.unique(clu)
                assert len(unique_ids) == n_clu, (
                    "First value of .clu file ({clufile_path}) does not match number of unique IDs!"
                )
                unit_map = dict(zip(unique_ids, list(range(1, n_clu + 1))))

                if 0 in unique_ids:
                    unit_map.pop(0)
                if not keep_mua_units and 1 in unique_ids:
                    unit_map.pop(1)
                if len(all_unit_ids) > 0:
                    last_unit_id = all_unit_ids[-1]
                else:
                    last_unit_id = 0
                new_unit_ids = [u + last_unit_id for u in unit_map.values()]
                all_unit_ids += new_unit_ids
                for s_id in unit_map:
                    all_spiketrains.append(res[(clu == s_id).nonzero()])

                if shank_ids is not None:
                    all_unit_shank_ids += [shank_id] * len(new_unit_ids)

        BaseSorting.__init__(self, sampling_frequency=sampling_frequency,
                             unit_ids=all_unit_ids)
        self.add_sorting_segment(
            NeuroScopeSortingSegment(all_unit_ids, all_spiketrains))

        self.extra_requirements.append('lxml')

        # set "group" property based on shank ids
        if len(all_unit_shank_ids) > 0:
            self.set_property("group", all_unit_shank_ids)

        if folder_path_passed:
            self._kwargs = dict(
                folder_path=str(folder_path.absolute()),
                resfile_path=None,
                clufile_path=None,
                keep_mua_units=keep_mua_units,
                exclude_shanks=exclude_shanks,
                xml_file_path=xml_file_path
            )
        else:
            self._kwargs = dict(
                folder_path=None,
                resfile_path=str(resfile_path.absolute()),
                clufile_path=str(clufile_path.absolute()),
                keep_mua_units=keep_mua_units,
                exclude_shanks=exclude_shanks,
                xml_file_path=xml_file_path
            )

class NeuroScopeSortingSegment(BaseSortingSegment):
    def __init__(self, unit_ids, spiketrains):
        BaseSortingSegment.__init__(self)
        self._unit_ids = list(unit_ids)
        self._spiketrains = spiketrains

    def get_unit_spike_train(self, unit_id, start_frame, end_frame):
        unit_index = self._unit_ids.index(unit_id)
        times = self._spiketrains[unit_index]
        if start_frame is not None:
            times = times[times >= start_frame]
        if end_frame is not None:
            times = times[times < end_frame]
        return times

def _find_xml_file_path(folder_path: PathType):
    xml_files = [f for f in folder_path.iterdir() if f.is_file()
                 if f.suffix == ".xml"]
    assert any(xml_files), "No .xml files found in the folder_path."
    assert len(
        xml_files) == 1, "More than one .xml file found in the folder_path! Specify xml_file_path."
    xml_file_path = xml_files[0]
    return xml_file_path

def _handle_xml_file_path(folder_path: PathType, initial_xml_file_path: PathType):
    if initial_xml_file_path is None:
        xml_file_path = _find_xml_file_path(folder_path=folder_path)
    else:
        assert Path(initial_xml_file_path).is_file(
        ), f".xml file ({initial_xml_file_path}) not found!"
        xml_file_path = initial_xml_file_path
    return xml_file_path

read_neuroscope_recording = define_function_from_class(source_class=NeuroScopeRecordingExtractor,
                                                   name="read_neuroscope_recording")
read_neuroscope_sorting = define_function_from_class(source_class=NeuroScopeSortingExtractor,
                                                 name="read_neuroscope_sorting")

def read_neuroscope(file_path, stream_id=None, keep_mua_units=False,
                    exclude_shanks=None, load_recording=True, load_sorting=False):
    """
    Read neuroscope recording and sorting.
    This function assumses that all .res and .clu files are in the same folder as
    the .xml file.

    Parameters
    ----------
    file_path: str
        The xml file.
    stream_id: str or None
    keep_mua_units: bool
        Optional. Whether or not to return sorted spikes from multi-unit activity. Defaults to True.
    exclude_shanks: list
        Optional. List of indices to ignore. The set of all possible indices is chosen by default, extracted as the
        final integer of all the .res. % i and .clu. % i pairs.
    load_recording: bool
        If True, the recording is loaded (default True)
    load_sorting: bool
        If True, the sorting is loaded (default False)
    """
    outputs = ()
    # TODO add checks for recording and sorting existence
    if load_recording:
        recording = NeuroScopeRecordingExtractor(file_path=file_path, stream_id=stream_id)
        outputs = outputs + (recording,)
    if load_sorting:
        folder_path = Path(file_path).parent
        sorting = NeuroScopeSortingExtractor(folder_path=folder_path, keep_mua_units=keep_mua_units,
                                             exclude_shanks=exclude_shanks)
        outputs = outputs + (sorting,)

    if len(outputs) == 1:
        outputs = outputs[0]

    return outputs

max_stars_repo_name: vncntprvst/spikeinterface

max_issues_repo_name: vncntprvst/spikeinterface

max_forks_repo_name: vncntprvst/spikeinterface


text:
import wx
import glfw
from OpenGL.GL import *

from numpy.random import rand

def rand_inrange(min, max):
    return (max - min)*rand() + min

class MyGL():
    def __init__(self, size=(640, 480), showflag=True):
        if not glfw.init():
            raise Exception("RuntimeError: glfw.init() failed")

        glfw.window_hint(glfw.VISIBLE, showflag) #set window invisible
        self.window = glfw.create_window(640, 480, "test", None, None)

        if not self.window:
            glfw.terminate()
            raise Exception("WindowError: glfw.create_window() failed")

        glfw.make_context_current(self.window)

    def WindowRoutie(self):
        while not glfw.window_should_close(self.window):
            glClearColor(0.0, 0.0, 0.0, 0.0)
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            self.MyDraw()
            glfw.swap_buffers(self.window)
            glfw.poll_events()

        glfw.destroy_window(self.window)
        glfw.terminate()

    def MyDraw(self):
        glBegin(GL_POLYGON)
        for i in range(3):
            glColor3f(rand_inrange(0, 1), rand_inrange(0, 1), rand_inrange(0, 1))
            glVertex2f(rand_inrange(-0.5, 0.5), rand_inrange(-0.5, 0.5))
        glEnd()

if __name__ == "__main__":
    mygl = MyGL()
    mygl.WindowRoutie()

max_stars_repo_name: Marble-GP/WxPython-SamplesAndTutorials

max_issues_repo_name: Marble-GP/WxPython-SamplesAndTutorials

max_forks_repo_name: Marble-GP/WxPython-SamplesAndTutorials

reason: from numpy


text:
import os,sys,glob,copy
import numpy as np

SCRIPTPATH = os.environ.get("SCRIPTPATH")
ROSETTAPATH = os.environ.get("ROSETTAPATH")
DLPATH = os.environ.get("DANPATH")
CURR = os.getcwd()
import utils

def avrg_trj(dcut=0.2):
    if os.path.exists('Qsel.avrg.relaxed.pdb'): return
    print("Running structural averaging around Qsel.pdb, dcut=%.2f..."%(dcut))

    os.system('cat iter_*/gen.retag.out > gen.total.out')
    cmd = '%s/rosetta_scripts/avrgsilent.sh %s %s %.3f %s'%(SCRIPTPATH,"gen.total.out","Qsel.pdb",dcut,"Qsel.avrg")

    print("CMD: ", cmd)
    os.system(cmd)
    os.system('cp Qsel.avrg.relaxed.pdb %s'%(CURR))
    # outcome: Qsel.avrg.relaxed.pdb

def main(ihyb_path):
    os.chdir(ihyb_path)
    its = [int(l.split('/')[0].split('_')[-1]) for l in glob.glob('iter_*/ref.out')]
    its.sort()
    fit = its[-1]
    print("Working on %s, final iteration iter_%d detected"%(ihyb_path,fit))

    l = utils.scparse('iter_%d/ref.out'%fit,['Q'])[1]
    maxQ = float(l.split()[-1])

    # Get representative model -- since Qpool been used, model0 always has the best Q
    if not os.path.exists('Qsel.pdb'):
        os.system('ln -s iter_%d/iter%d.%d.pdb ./Qsel.pdb'%(fit,fit,0))
    print("%s Qsel: iter%d.%d.pdb, Q=%.3f"%(ihyb_path,fit,0,maxQ))

    # Average trajectory around the representative model followed by relaxation
    avrg_trj()
    os.chdir(CURR)

if __name__ == "__main__":
    ihyb_path = sys.argv[1]
    main(ihyb_path)

max_stars_repo_name: Zuricho/DeepAccNet

max_issues_repo_name: Zuricho/DeepAccNet

max_forks_repo_name: Zuricho/DeepAccNet


text:
# coding: utf-8
# Copyright 2020 Tarkan Temizoz

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

import optuna
import numpy as np
import xgboost as xgb

from bayes_opt.utils import test_learning
from sklearn.model_selection import StratifiedKFold

class bayes_xgboost:
    """ Hyperparameter optimisation for Logistic Regression using Bayesian Optimization.

    Attributes:
        formatter: formatter for the specified experiment
        data: the data set
        labels: labels of the data
        returns: returns of the data
        bayes_params: bayesian optimization parameters
        bayes_trials: number of trials
        inner_cval: whether to apply inner-cross validation
        n_val_splits: number of inner-cross validation folds
    """

    def __init__(self, formatter, data, labels, returns):
        """Instantiates the attributes and parameters.

        Args:
            formatter: formatter for the specified experiment
            data: the data set
            labels: labels of the data
            returns: returns of the data
        """

        self.formatter = formatter
        self.bayes_params = self.formatter.get_bayes_params()
        self.bayes_trials = self.bayes_params["bayes_trials"]
        self.inner_cval = self.bayes_params["inner_cval"]
        self.n_val_splits = self.bayes_params.get("n_val_splits", 10)
        self._xgsteps = 20

        self.data = data
        self.labels = labels
        self.returns = returns

    def train_bayes_boost(self, trial):
        """Applying bayesian optimization trials

        Args:
            trial: bayesian optimization trial

        Returns:
            mean total returns
        """

        # setting up the search space
        space = {'eta': trial.suggest_uniform('eta', 0.01, 0.4),
                'max_depth': trial.suggest_int('max_depth', 3, 10),
                'min_child_weight': trial.suggest_int('min_child_weight', 1, 10),
                'gamma': trial.suggest_uniform('gamma', 0, 0.4),
                'colsample_bytree': trial.suggest_uniform('colsample_bytree', 0.1, 1),
                'steps': self._xgsteps
                }

        param = {'eta' : space['eta'],
                'max_depth' : space['max_depth'],
                'min_child_weight' : space['min_child_weight'],
                'gamma' : space['gamma'],
                'colsample_bytree' : space['colsample_bytree'],
                'objective': 'multi:softprob',
                'num_class': self.returns.shape[1]
                }

        # maximize the train results, no inner-cross validation
        if self.inner_cval == False:

            D_train = xgb.DMatrix(self.data, label=self.labels)
            model = xgb.train(param, D_train, space['steps'])
            probs = model.predict(D_train)
            result,_ = test_learning(probs, self.returns)

        # apply inner-cross validation
        else:

            test_return = np.zeros(self.n_val_splits)
            count = 0
            skf = StratifiedKFold(self.n_val_splits, shuffle=True, random_state=self.formatter.seed)

            for train_index, test_index in skf.split(self.data, self.labels):

                x_train, x_test = self.data[train_index], self.data[test_index]
                _, r_test = self.returns[train_index], self.returns[test_index]
                y_train, y_test = self.labels[train_index], self.labels[test_index]

                D_train = xgb.DMatrix(x_train, label=y_train)
                D_test = xgb.DMatrix(x_test, label=y_test)
                model = xgb.train(param, D_train, space['steps'])
                probs = model.predict(D_test)
                test_return[count],_ = test_learning(probs, r_test)
                count += 1

            result = np.mean(test_return)

        return result

    def bayes(self):
        """Building bayesian optimization environment.

        # apply inner-cross validationReturns:
            the optimal hyperparameters thus far
        """

        sampler = optuna.samplers.TPESampler()
        study = optuna.create_study(sampler=sampler, direction='maximize')
        study.optimize(func=self.train_bayes_boost, n_trials=self.bayes_trials)

        return study.best_params

max_stars_repo_name: tarkantemizoz/Cost-Sensitive-Learning

max_issues_repo_name: tarkantemizoz/Cost-Sensitive-Learning

max_forks_repo_name: tarkantemizoz/Cost-Sensitive-Learning


text:
import xarray as _xr
import matplotlib.pyplot as _plt
import numpy as _np
from johnspythonlibrary2.Plot import subTitle as _subTitle

def abel_inverse_transform_1D(data_in, method='three point', plot=False):
	"""
 	Wrapper for the PyAbel library

 	Parameters
 	----------
 	data_in : 2D xarray dataarray
		input data
 	method : str
		'three point', etc.
 	plot : bool, optional
		optional plot

 	Returns
 	-------
 	data_out : 2D xarray dataarray
		output data

 	Examples
 	--------

 	Example 1::

		import abel
		import numpy as np
		import matplotlib.pyplot as plt
		import xarray as xr

		def gauss(r, r0, sigma):
		    return np.exp(-(r-r0)**2/sigma**2)

		c2 = 51  # an odd-number of points
		sigma = c2/5   # 1/e-width of the Gaussian
		r = np.arange(c2)

		# 1-D Gaussian function ------------------
		data = gauss(r, 0, sigma)
		data_in = xr.DataArray( data, dims=['y'], coords = [r])

		data_out = abel_inverse_transform_1D(data_in, method='three point', plot=True)
		data_out = abel_inverse_transform_1D(data_in, method='basex', plot=True)

 	"""
	import abel

	if _np.mod(len(data_in),2)==0:
		raise Exception('Data length needs to be an odd number')

	# calculate step
	y=data_in.coords[data_in.dims[0]].data
	dr=_np.mean(y[1:]-y[:-1])

	# perform abel transform
	if method == 'three point':
		trans = abel.dasch.three_point_transform(data_in, dr=dr)
	elif method == 'basex':
		trans = abel.basex.basex_transform(data_in, dr=dr, verbose=True, basis_dir=None, direction='inverse')
	else:
		raise Exception('Method not implemented or not recognized')

	data_out = _xr.DataArray( trans, dims = 'r', coords = [data_in.coords[data_in.dims[0]].data])

	if plot==True:

		fig,ax=_plt.subplots(2,sharex=True)

		data_in.plot(ax=ax[0])
		data_out.plot(ax=ax[1])
		_subTitle( ax[0], 'Input signal')
		_subTitle( ax[1], 'Transformed signal')

	return data_out