<commit_before>#! /usr/bin/python3

from micro import Micro
from myprocess import MyProcess
from pygame import mixer
import alsaaudio
import time
import audioop
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.ndimage.filters import gaussian_laplace
import multiprocessing

def main(time_seconds, to_file, file_music):
    """
    Where time is the numbers of seconds!!!!
    """

    with Micro() as inp:

        # Set attributes: Mono, 8000 Hz, 16 bit little endian samples
        inp.setchannels(1)
        inp.setrate(8000)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)

        # The period size controls the internal number of frames per period.
        # The significance of this parameter is documented in the ALSA api.
        # For our purposes, it is suficcient to know that reads from the device
        # will return this many frames. Each frame being 2 bytes long.
        # This means that the reads below will return either 320 bytes of data
        # or 0 bytes of data. The latter is possible because we are in nonblocking
        # mode.
        inp.setperiodsize(160)

        # i is the number of loop
        i = 0
        # the numbers of sampling for each unity of time
        interval = 0.001
        # the total of sampling
        n = int(time_seconds / interval)
        # a memory shared vector
        # df = multiprocessing.Array('i', n)
        df = np.zeros(n)

        # create an other process to real time display
        # t1 = MyProcess(df, 1 / interval, time_seconds)
        # t1.start()

        # init the last mean
        # and the music
        last_mean = 1
        mixer.init()
        mixer.music.load(file_music)
        mixer.music.set_volume(0.6)
        mixer.music.play()

        while i < n:
            # Read data from device
            l, data = inp.read()
            if l:
                # Return the maximumof the absolute value of all samples in a fragment.
                df[i] = audioop.max(data, 2)
            i += 1
            time.sleep(interval)

            # update the music every half of second
            if (i % 500 == 0):

                # change data to decibel
                a = 20 * np.log10(df)
                a[np.isinf(a)] = 0
                # try to do the mean
                try:
                    mean = np.mean(a[a > 0][-500:])
                except IndexError:
                    mean = 1

                # compute the ratio
                ratio = mean / last_mean
                # to avoid the first ratio which is mean/1
                if last_mean == 1:
                    ratio = 1
                last_mean = mean
                # set the new volume
                mixer.music.set_volume(mixer.music.get_volume() * (1 + 10 * (1 - ratio)))
                print("ratio = ", (1 + 10 * (1 - ratio)))

        mixer.music.stop()
        mixer.quit()

        if to_file:
            df[400:].tofile("out.dat", sep=',')

        input("Waiting input")
        # stop the other process
        # t1.shutdown()
    return np.array(df[400:])

def plotting(df):

    sns.set_style('darkgrid')
    plt.subplot(2, 2, 1)
    plt.plot(df, label="raw data")
    plt.grid(True)
    plt.legend()

    a = 20 * np.log10(df)
    a[np.isinf(a)] = 0
    plt.subplot(2, 2, 2)
    plt.plot(a, label="decibel volume")
    plt.grid(True)
    plt.legend()

    plt.subplot(2, 2, 3)
    plt.plot(np.fft.fft(a), label="fft(raw data)")
    plt.grid(True)
    plt.legend()

    plt.subplot(2, 2, 4)
    plt.plot(gaussian_laplace(a, sigma=max(a)), label="gaussian(raw data)")
    plt.grid(True)
    plt.legend()
    plt.show()

if __name__ == '__main__':
    df = main(10, False, "/home/romain/Musique/Casseurs Flowters - Comment C'est Loin/05 - En Boucle.mp3")
    # plotting(df)
<commit_msg>change core.py<commit_after>#! /usr/bin/python3

from micro import Micro
from pygame import mixer
import alsaaudio
import time
import audioop
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.ndimage.filters import gaussian_laplace
import sys

def main(time_seconds, to_file, file_music):
    """
    Where time is the numbers of seconds!!!!
    """

    with Micro() as inp:

        # Set attributes: Mono, 8000 Hz, 16 bit little endian samples
        inp.setchannels(1)
        inp.setrate(8000)
        inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)

        # The period size controls the internal number of frames per period.
        # The significance of this parameter is documented in the ALSA api.
        # For our purposes, it is suficcient to know that reads from the device
        # will return this many frames. Each frame being 2 bytes long.
        # This means that the reads below will return either 320 bytes of data
        # or 0 bytes of data. The latter is possible because we are in nonblocking
        # mode.
        inp.setperiodsize(160)

        # i is the number of loop
        i = 0
        # the numbers of sampling for each unity of time
        interval = 0.001
        # the total of sampling
        n = int(time_seconds / interval)
        # a memory shared vector
        # df = multiprocessing.Array('i', n)
        df = np.zeros(n)

        # create an other process to real time display
        # t1 = MyProcess(df, 1 / interval, time_seconds)
        # t1.start()

        # init the last mean
        # and the music
        last_mean = 1
        mixer.init()
        mixer.music.load(file_music)
        mixer.music.set_volume(0.6)
        mixer.music.play()

        while i < n:
            # Read data from device
            l, data = inp.read()
            if l:
                # Return the maximumof the absolute value of all samples in a fragment.
                df[i] = audioop.max(data, 2)
            i += 1
            time.sleep(interval)

            # update the music every half of second
            if (i % 500 == 0):

                # change data to decibel
                a = 20 * np.log10(df)
                a[np.isinf(a)] = 0
                # try to do the mean
                try:
                    mean = np.mean(a[a > 0][-500:])
                except IndexError:
                    mean = 1

                # compute the ratio
                ratio = mean / last_mean
                # to avoid the first ratio which is mean/1
                if last_mean == 1:
                    ratio = 1
                last_mean = mean
                # set the new volume
                mixer.music.set_volume(mixer.music.get_volume() * (1 + 10 * (1 - ratio)))
                print("ratio = ", (1 + 10 * (1 - ratio)))

        mixer.music.stop()
        mixer.quit()

        if to_file:
            df[400:].tofile("out.dat", sep=',')

        input("Waiting input")
        # stop the other process
        # t1.shutdown()
    return np.array(df[400:])

def plotting(df):

    sns.set_style('darkgrid')
    plt.subplot(2, 2, 1)
    plt.plot(df, label="raw data")
    plt.grid(True)
    plt.legend()

    a = 20 * np.log10(df)
    a[np.isinf(a)] = 0
    plt.subplot(2, 2, 2)
    plt.plot(a, label="decibel volume")
    plt.grid(True)
    plt.legend()

    plt.subplot(2, 2, 3)
    plt.plot(np.fft.fft(a), label="fft(raw data)")
    plt.grid(True)
    plt.legend()

    plt.subplot(2, 2, 4)
    plt.plot(gaussian_laplace(a, sigma=max(a)), label="gaussian(raw data)")
    plt.grid(True)
    plt.legend()
    plt.show()

if __name__ == '__main__':
    if len(sys.argv) > 1:
        df = main(sys.argv[1], False, sys.argv[0])
    else:
        df = main(60, False, "/home/romain/Musique/Casseurs Flowters - Comment C'est Loin/05 - En Boucle.mp3")
<|endoftext|>


<commit_before>import time

from decorator import decorator
from logger import logger

from perfrunner.helpers.cbmonitor import with_stats
from perfrunner.helpers.misc import server_group
from perfrunner.tests import PerfTest
from perfrunner.tests.index import IndexTest
from perfrunner.tests.query import QueryTest
from perfrunner.tests.xdcr import XdcrTest, SymmetricXdcrTest

@decorator
def with_delay(rebalance, *args, **kwargs):
    test = args[0]

    time.sleep(test.rebalance_settings.start_after)

    rebalance(*args, **kwargs)

    time.sleep(test.rebalance_settings.stop_after)

@decorator
def with_reporter(rebalance, *args, **kwargs):
    test = args[0]

    test.reporter.reset_utilzation_stats()

    test.reporter.start()

    rebalance(*args, **kwargs)

    test.rebalance_time = test.reporter.finish('Rebalance')

    test.reporter.save_utilzation_stats()
    test.reporter.save_master_events()

@decorator
def with_delayed_posting(rebalance, *args, **kwargs):
    test = args[0]

    rebalance(*args, **kwargs)

    if test.rest.is_balanced(test.servers[0]):
        test.reporter.post_to_sf(test.rebalance_time)
    else:
        logger.error('Rebalance failed')

class RebalanceTest(PerfTest):

    def __init__(self, *args, **kwargs):
        super(RebalanceTest, self).__init__(*args, **kwargs)
        self.rebalance_settings = self.test_config.get_rebalance_settings()
        self.clusters = self.cluster_spec.get_clusters().values()
        self.initial_nodes = self.test_config.get_initial_nodes()
        self.nodes_after = self.rebalance_settings.nodes_after

    def change_watermarks(self, host):
        watermark_settings = self.test_config.get_watermark_settings()
        mem_quota = self.test_config.get_mem_quota()
        for bucket in self.test_config.get_buckets():
            for key, val in watermark_settings.items():
                val = self.memcached.calc_watermark(val, mem_quota)
                self.memcached.set_flusher_param(host, bucket, key, val)

    @with_delayed_posting
    @with_stats(latency=True)
    @with_delay
    @with_reporter
    def rebalance(self):
        swap = self.rebalance_settings.swap
        group_number = self.test_config.get_group_number() or 1

        for cluster, initial_nodes, nodes_after in zip(self.clusters,
                                                       self.initial_nodes,
                                                       self.nodes_after):
            master = cluster[0]

            groups = group_number > 1 and self.rest.get_server_groups(master) or {}

            if nodes_after > initial_nodes:
                new_nodes = enumerate(
                    cluster[initial_nodes:nodes_after],
                    start=initial_nodes
                )
                known_nodes = cluster[:nodes_after]
                ejected_nodes = []
            elif nodes_after < initial_nodes:
                new_nodes = []
                known_nodes = cluster[:initial_nodes]
                ejected_nodes = cluster[nodes_after:initial_nodes]
            elif swap:
                new_nodes = enumerate(
                    cluster[initial_nodes:initial_nodes + swap],
                    start=initial_nodes - swap
                )
                known_nodes = cluster[:initial_nodes + swap]
                ejected_nodes = cluster[initial_nodes - swap:initial_nodes]
            else:
                continue

            for i, host_port in new_nodes:
                host = host_port.split(':')[0]
                group = server_group(cluster[:nodes_after], group_number, i)
                uri = groups.get(group)
                self.rest.add_node(master, host, uri)
            self.rest.rebalance(master, known_nodes, ejected_nodes)
            for i, host_port in new_nodes:
                host = host_port.split(':')[0]
                self.change_watermarks(host)

            self.monitor.monitor_rebalance(master)

class StaticRebalanceTest(RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.rebalance()

class StaticRebalanceWithIndexTest(IndexTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.define_ddocs()
        self.build_index()

        self.rebalance()

class RebalanceKVTest(RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        self.access_bg()
        self.rebalance()
        self.shutdown_event.set()

class RebalanceWithQueriesTest(QueryTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.define_ddocs()
        self.build_index()

        self.workload = self.test_config.get_access_settings()
        self.access_bg()
        self.rebalance()
        self.shutdown_event.set()

        if self.rest.is_balanced(self.servers[0]):
            self.reporter.post_to_sf(
                *self.metric_helper.calc_views_disk_size()
            )

class RebalanceWithXdcrTest(XdcrTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.init_xdcr()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()

        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        bg_process = self.access_bg()
        self.rebalance()
        bg_process.terminate()

class RebalanceWithSymmetricXdcrTest(SymmetricXdcrTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.init_xdcr()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()

        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        bg_process = self.access_bg()
        self.rebalance()
        bg_process.terminate()
<commit_msg>check balance of all clusters<commit_after>import time

from decorator import decorator
from logger import logger

from perfrunner.helpers.cbmonitor import with_stats
from perfrunner.helpers.misc import server_group
from perfrunner.tests import PerfTest
from perfrunner.tests.index import IndexTest
from perfrunner.tests.query import QueryTest
from perfrunner.tests.xdcr import XdcrTest, SymmetricXdcrTest

@decorator
def with_delay(rebalance, *args, **kwargs):
    test = args[0]

    time.sleep(test.rebalance_settings.start_after)

    rebalance(*args, **kwargs)

    time.sleep(test.rebalance_settings.stop_after)

@decorator
def with_reporter(rebalance, *args, **kwargs):
    test = args[0]

    test.reporter.reset_utilzation_stats()

    test.reporter.start()

    rebalance(*args, **kwargs)

    test.rebalance_time = test.reporter.finish('Rebalance')

    test.reporter.save_utilzation_stats()
    test.reporter.save_master_events()

@decorator
def with_delayed_posting(rebalance, *args, **kwargs):
    test = args[0]

    rebalance(*args, **kwargs)

    if test.is_balanced():
        test.reporter.post_to_sf(test.rebalance_time)
    else:
        logger.error('Rebalance failed')

class RebalanceTest(PerfTest):

    def __init__(self, *args, **kwargs):
        super(RebalanceTest, self).__init__(*args, **kwargs)
        self.rebalance_settings = self.test_config.get_rebalance_settings()
        self.clusters = self.cluster_spec.get_clusters().values()
        self.initial_nodes = self.test_config.get_initial_nodes()
        self.nodes_after = self.rebalance_settings.nodes_after

    def is_balanced(self):
        for cluster in self.clusters:
            if not self.rest.is_balanced(cluster[0]):
                return False
        return True

    def change_watermarks(self, host):
        watermark_settings = self.test_config.get_watermark_settings()
        mem_quota = self.test_config.get_mem_quota()
        for bucket in self.test_config.get_buckets():
            for key, val in watermark_settings.items():
                val = self.memcached.calc_watermark(val, mem_quota)
                self.memcached.set_flusher_param(host, bucket, key, val)

    @with_delayed_posting
    @with_stats(latency=True)
    @with_delay
    @with_reporter
    def rebalance(self):
        swap = self.rebalance_settings.swap
        group_number = self.test_config.get_group_number() or 1

        for cluster, initial_nodes, nodes_after in zip(self.clusters,
                                                       self.initial_nodes,
                                                       self.nodes_after):
            master = cluster[0]

            groups = group_number > 1 and self.rest.get_server_groups(master) or {}

            if nodes_after > initial_nodes:
                new_nodes = enumerate(
                    cluster[initial_nodes:nodes_after],
                    start=initial_nodes
                )
                known_nodes = cluster[:nodes_after]
                ejected_nodes = []
            elif nodes_after < initial_nodes:
                new_nodes = []
                known_nodes = cluster[:initial_nodes]
                ejected_nodes = cluster[nodes_after:initial_nodes]
            elif swap:
                new_nodes = enumerate(
                    cluster[initial_nodes:initial_nodes + swap],
                    start=initial_nodes - swap
                )
                known_nodes = cluster[:initial_nodes + swap]
                ejected_nodes = cluster[initial_nodes - swap:initial_nodes]
            else:
                continue

            for i, host_port in new_nodes:
                host = host_port.split(':')[0]
                group = server_group(cluster[:nodes_after], group_number, i)
                uri = groups.get(group)
                self.rest.add_node(master, host, uri)
            self.rest.rebalance(master, known_nodes, ejected_nodes)
            for i, host_port in new_nodes:
                host = host_port.split(':')[0]
                self.change_watermarks(host)

            self.monitor.monitor_rebalance(master)

class StaticRebalanceTest(RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.rebalance()

class StaticRebalanceWithIndexTest(IndexTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.define_ddocs()
        self.build_index()

        self.rebalance()

class RebalanceKVTest(RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        self.access_bg()
        self.rebalance()
        self.shutdown_event.set()

class RebalanceWithQueriesTest(QueryTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()
        self.compact_bucket()

        self.define_ddocs()
        self.build_index()

        self.workload = self.test_config.get_access_settings()
        self.access_bg()
        self.rebalance()
        self.shutdown_event.set()

        if self.is_balanced():
            self.reporter.post_to_sf(
                *self.metric_helper.calc_views_disk_size()
            )

class RebalanceWithXdcrTest(XdcrTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.init_xdcr()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()

        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        bg_process = self.access_bg()
        self.rebalance()
        bg_process.terminate()

class RebalanceWithSymmetricXdcrTest(SymmetricXdcrTest, RebalanceTest):

    def run(self):
        self.load()
        self.wait_for_persistence()

        self.init_xdcr()
        self.wait_for_persistence()

        self.hot_load()
        self.wait_for_persistence()

        self.compact_bucket()

        self.workload = self.test_config.get_access_settings()
        bg_process = self.access_bg()
        self.rebalance()
        bg_process.terminate()
<|endoftext|>


<commit_before># coding: utf-8

""" Observe particles from a given simulation """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys
import copy
import logging

# Third-party
import astropy.units as u
from astropy.utils.console import color_print
import emcee
import h5py
import matplotlib.pyplot as plt
import numpy as np
np.seterr(all="ignore")
import scipy
scipy.seterr(all="ignore")
import yaml

# Project
from streams import usys
from streams.coordinates.frame import heliocentric, galactocentric
from streams.dynamics import ObservedParticle
import streams.io as s_io
from streams.observation.gaia import gaia_spitzer_errors
import streams.potential as s_potential
from streams.util import _parse_quantity

# Create logger
logger = logging.getLogger(__name__)

def observe_simulation(class_name, particle_error_model=None, satellite_error_model=None,
                       selection_expr=None, N=None, output_file=None, overwrite=False,
                       seed=None, class_kwargs=dict()):
    """ Observe simulation data and write the output to a standard HDF5 format.

        TODO: handle missing dimensions

        Parameters
        ----------

    """

    if os.path.exists(output_file) and overwrite:
        os.remove(output_file)

    if os.path.exists(output_file):
        raise IOError("File '{}' already exists! Did you "
                      "want to use overwrite=True?".format(output_file))

    # read the simulation data from the specified class
    if seed is None:
        seed = np.random.randint(100)

    logger.debug("Using seed: {}".format(seed))
    np.random.seed(seed)

    try:
        Simulation = getattr(s_io, class_name)
    except AttributeError:
        raise ValueError("Simulation class '{}' not found!".format(class_name))

    # instantiate the specified simulation class with any additional keyword
    #   arguments, e.g., mass from SgrSimulation
    simulation = Simulation(**class_kwargs)

    # read particles from the simulation class
    sim_time = simulation.particle_units[0]/simulation.particle_units[-1]
    selection_expr = "(tub!=0) & (tub<{})".format((6000*u.Myr).to(sim_time).value) # HACK HACK HACK
    particles = simulation.particles(N=N, expr=selection_expr)
    particles = particles.to_frame(heliocentric)

    logger.debug("Read in {} particles with expr='{}'"\
                 .format(particles.nparticles, selection_expr))

    # read the satellite position
    satellite = simulation.satellite()
    satellite = satellite.to_frame(heliocentric)
    logger.debug("Read in present position of satellite...")

    # observe the particles if necessary
    if particle_error_model is not None:
        logger.info("Observing particles with {}".format(particle_error_model))
        particle_errors = particle_error_model(particles)
        o_particles = particles.observe(particle_errors)
    else:
        logger.info("Not observing particles")
        o_particles = particles

    # observe the satellite if necessary
    if satellite_error_model:
        logger.info("Observing satellite with {}".format(satellite_error_model))
        satellite_errors = satellite_error_model(satellite)
        o_satellite = satellite.observe(satellite_errors)
    else:
        logger.info("Not observing satellite")
        o_satellite = satellite

    # make a plot of true and observed positions
    true_gc_particles = particles.to_frame(galactocentric)
    gc_particles = o_particles.to_frame(galactocentric)
    all_gc_particles = simulation.particles(N=1000, expr="tub!=0")\
                                 .to_frame(galactocentric)

    fig,axes = plt.subplots(1,2,figsize=(16,8))
    markersize = 6.
    axes[0].plot(all_gc_particles["x"].value, all_gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.25)
    axes[0].plot(true_gc_particles["x"].value, true_gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5)
    axes[0].plot(gc_particles["x"].value, gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5, c='#ca0020')

    axes[1].plot(all_gc_particles["vx"].to(u.km/u.s).value,
                 all_gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.25)
    axes[1].plot(true_gc_particles["vx"].to(u.km/u.s).value,
                 true_gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5)
    axes[1].plot(gc_particles["vx"].to(u.km/u.s).value,
                 gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5, c='#ca0020')

    fname = os.path.splitext(os.path.basename(output_file))[0]
    fig.savefig(os.path.join(os.path.split(output_file)[0],
                             "{}.{}".format(fname, 'png')))

    with h5py.File(output_file, "w") as f:
        # add particle positions to file
        grp = f.create_group("particles")
        grp["data"] = o_particles._repr_X

        if isinstance(o_particles, ObservedParticle):
            grp["error"] = o_particles._repr_error_X
            grp["true_data"] = particles._repr_X
        grp["coordinate_names"] = o_particles.frame.coord_names
        grp["units"] = [str(x) for x in o_particles._repr_units]
        grp["tub"] = o_particles.tub

        grp = f.create_group("satellite")
        grp["data"] = o_satellite._repr_X
        if isinstance(o_satellite, ObservedParticle):
            grp["error"] = o_satellite._repr_error_X
            grp["true_data"] = satellite._repr_X
        grp["coordinate_names"] = o_satellite.frame.coord_names
        grp["units"] = [str(x) for x in o_satellite._repr_units]
        grp["m"] = o_satellite.m
        grp["v_disp"] = o_satellite.v_disp

        grp = f.create_group("simulation")
        grp["t1"] = simulation.t1
        grp["t2"] = simulation.t2

if __name__ == "__main__":
    from argparse import ArgumentParser

    parser = ArgumentParser(description="")
    parser.add_argument("-v", "--verbose", action="store_true",
                        dest="verbose", default=False,
                        help="Be chatty! (default = False)")
    parser.add_argument("-q", "--quiet", action="store_true", dest="quiet",
                    default=False, help="Be quiet! (default = False)")
    parser.add_argument("-o", "--overwrite", dest="overwrite", default=False, action="store_true",
                        help="Overwrite any existing data.")

    parser.add_argument("-f", "--file", dest="output_file", required=True,
                        help="Name of the file to store the data.")
    parser.add_argument("--class_name", dest="class_name", required=True,
                        help="Name of the simulation data class, e.g. SgrSimulation")
    parser.add_argument("--expr", dest="expr", default=None, type=str,
                        help="Selection expression (for numexpr) for simulation particles.")
    parser.add_argument("--N", dest="N", default=None, type=int,
                        help="Number of particles.")
    parser.add_argument("--seed", dest="seed", default=None, type=int,
                        help="Seed for random number generator.")

    parser.add_argument("--mass", dest="mass", required=True, type=str,
                        help="Satellite mass.")

    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR)
    else:
        logging.basicConfig(level=logging.INFO)

    """
        e.g.:

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=1024 --file=/Users/adrian/Projects/streams/data/observed_particles/2.5e6_N1024.hdf5 \
--seed=42 --mass="2.5e6" --overwrite

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=1024 --file=/Users/adrian/projects/streams/data/observed_particles/2.5e7_N1024.hdf5 \
--seed=42 --mass="2.5e7" --overwrite

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=1024 --file=/Users/adrian/projects/streams/data/observed_particles/2.5e8_N1024.hdf5 \
--seed=42 --mass="2.5e8" --overwrite
    """

    # TODO: class kwargs
    # particle_error_model=gaia_spitzer_errors, satellite_error_model=gaia_spitzer_errors,
    # particle_error_model=gaia_spitzer_errors, satellite_error_model=None,
    observe_simulation(args.class_name,
        particle_error_model=gaia_spitzer_errors, satellite_error_model=gaia_spitzer_errors,
        selection_expr=args.expr, N=args.N, output_file=args.output_file,
        overwrite=args.overwrite, seed=args.seed, class_kwargs=dict(mass=args.mass))
<commit_msg>figure out whether in leading or trailing tail<commit_after># coding: utf-8

""" Observe particles from a given simulation """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys
import copy
import logging

# Third-party
import astropy.units as u
from astropy.utils.console import color_print
import emcee
import h5py
import matplotlib.pyplot as plt
import numpy as np
np.seterr(all="ignore")
import scipy
scipy.seterr(all="ignore")
import yaml

# Project
from streams import usys
from streams.coordinates.frame import heliocentric, galactocentric
from streams.dynamics import ObservedParticle
import streams.io as s_io
from streams.observation.gaia import gaia_spitzer_errors
from streams.potential.lm10 import LawMajewski2010
from streams.util import _parse_quantity
from streams.integrate import LeapfrogIntegrator

# Create logger
logger = logging.getLogger(__name__)

def observe_simulation(class_name, particle_error_model=None, satellite_error_model=None,
                       selection_expr=None, N=None, output_file=None, overwrite=False,
                       seed=None, class_kwargs=dict()):
    """ Observe simulation data and write the output to a standard HDF5 format.

        TODO: handle missing dimensions

        Parameters
        ----------

    """

    if os.path.exists(output_file) and overwrite:
        os.remove(output_file)

    if os.path.exists(output_file):
        raise IOError("File '{}' already exists! Did you "
                      "want to use overwrite=True?".format(output_file))

    # read the simulation data from the specified class
    if seed is None:
        seed = np.random.randint(100)

    logger.debug("Using seed: {}".format(seed))
    np.random.seed(seed)

    try:
        Simulation = getattr(s_io, class_name)
    except AttributeError:
        raise ValueError("Simulation class '{}' not found!".format(class_name))

    # instantiate the specified simulation class with any additional keyword
    #   arguments, e.g., mass from SgrSimulation
    simulation = Simulation(**class_kwargs)

    # read particles from the simulation class
    sim_time = simulation.particle_units[0]/simulation.particle_units[-1]

    # HACK HACK HACK
    selection_expr = "(tub!=0) & (tub<{}) & (sqrt((x+8)**2 + y**2 + z**2)<50)".format((6000*u.Myr).to(sim_time).value)
    particles = simulation.particles(N=N, expr=selection_expr)

    logger.debug("Read in {} particles with expr='{}'"\
                 .format(particles.nparticles, selection_expr))

    # read the satellite position
    satellite = simulation.satellite()
    logger.debug("Read in present position of satellite...")

    ################################################################
    # HACK: Figure out if in leading or trailing tail
    s = satellite
    p = particles
    lm10_potential = LawMajewski2010()
    X = np.vstack((s._X[...,:3], p._X[...,:3].copy()))
    V = np.vstack((s._X[...,3:], p._X[...,3:].copy()))
    integrator = LeapfrogIntegrator(lm10_potential._acceleration_at,
                                    np.array(X), np.array(V),
                                    args=(X.shape[0], np.zeros_like(X)))
    ts, rs, vs = integrator.run(t1=simulation.t1, t2=simulation.t2, dt=-1)

    s_orbit = np.vstack((rs[:,0][:,np.newaxis].T, vs[:,0][:,np.newaxis].T)).T
    p_orbits = np.vstack((rs[:,1:].T, vs[:,1:].T)).T
    t_idx = np.array([np.argmin(np.fabs(ts - t)) for t in p.tub])

    p_x = np.array([p_orbits[jj,ii] for ii,jj in enumerate(t_idx)])
    s_x = np.array([s_orbit[jj,0] for jj in t_idx])
    diff = p_x-s_x

    norm_r = s_x[:,:3] / np.sqrt(np.sum(s_x[:,:3]**2, axis=-1))[:,np.newaxis]
    norm_diff_r = diff[:,:3] / np.sqrt(np.sum(diff[:,:3]**2, axis=-1))[:,np.newaxis]
    dot_prod_r = np.sum(norm_diff_r*norm_r, axis=-1)
    tail_bit = (dot_prod_r > 0.).astype(int)*2 - 1
    ################################################################

    satellite = satellite.to_frame(heliocentric)
    particles = particles.to_frame(heliocentric)

    # observe the particles if necessary
    if particle_error_model is not None:
        logger.info("Observing particles with {}".format(particle_error_model))
        particle_errors = particle_error_model(particles)
        o_particles = particles.observe(particle_errors)
    else:
        logger.info("Not observing particles")
        o_particles = particles

    # observe the satellite if necessary
    if satellite_error_model:
        logger.info("Observing satellite with {}".format(satellite_error_model))
        satellite_errors = satellite_error_model(satellite)
        o_satellite = satellite.observe(satellite_errors)
    else:
        logger.info("Not observing satellite")
        o_satellite = satellite

    # make a plot of true and observed positions
    true_gc_particles = particles.to_frame(galactocentric)
    gc_particles = o_particles.to_frame(galactocentric)
    all_gc_particles = simulation.particles(N=1000, expr="tub!=0")\
                                 .to_frame(galactocentric)

    fig,axes = plt.subplots(1,2,figsize=(16,8))
    markersize = 6.
    axes[0].plot(all_gc_particles["x"].value, all_gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.25)
    axes[0].plot(true_gc_particles["x"].value, true_gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5)
    axes[0].plot(gc_particles["x"].value, gc_particles["z"].value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5, c='#ca0020')

    axes[1].plot(all_gc_particles["vx"].to(u.km/u.s).value,
                 all_gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.25)
    axes[1].plot(true_gc_particles["vx"].to(u.km/u.s).value,
                 true_gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5)
    axes[1].plot(gc_particles["vx"].to(u.km/u.s).value,
                 gc_particles["vz"].to(u.km/u.s).value,
                 markersize=markersize, marker='o', linestyle='none', alpha=0.5, c='#ca0020')

    fname = os.path.splitext(os.path.basename(output_file))[0]
    fig.savefig(os.path.join(os.path.split(output_file)[0],
                             "{}.{}".format(fname, 'png')))

    with h5py.File(output_file, "w") as f:
        # add particle positions to file
        grp = f.create_group("particles")
        grp["data"] = o_particles._repr_X

        if isinstance(o_particles, ObservedParticle):
            grp["error"] = o_particles._repr_error_X
            grp["true_data"] = particles._repr_X
        grp["coordinate_names"] = o_particles.frame.coord_names
        grp["units"] = [str(x) for x in o_particles._repr_units]
        grp["tub"] = o_particles.tub
        grp["tail_bit"] = tail_bit

        grp = f.create_group("satellite")
        grp["data"] = o_satellite._repr_X
        if isinstance(o_satellite, ObservedParticle):
            grp["error"] = o_satellite._repr_error_X
            grp["true_data"] = satellite._repr_X
        grp["coordinate_names"] = o_satellite.frame.coord_names
        grp["units"] = [str(x) for x in o_satellite._repr_units]
        grp["m"] = o_satellite.m
        grp["v_disp"] = o_satellite.v_disp

        grp = f.create_group("simulation")
        grp["t1"] = simulation.t1
        grp["t2"] = simulation.t2

if __name__ == "__main__":
    from argparse import ArgumentParser

    parser = ArgumentParser(description="")
    parser.add_argument("-v", "--verbose", action="store_true",
                        dest="verbose", default=False,
                        help="Be chatty! (default = False)")
    parser.add_argument("-q", "--quiet", action="store_true", dest="quiet",
                    default=False, help="Be quiet! (default = False)")
    parser.add_argument("-o", "--overwrite", dest="overwrite", default=False, action="store_true",
                        help="Overwrite any existing data.")

    parser.add_argument("-f", "--file", dest="output_file", required=True,
                        help="Name of the file to store the data.")
    parser.add_argument("--class_name", dest="class_name", required=True,
                        help="Name of the simulation data class, e.g. SgrSimulation")
    parser.add_argument("--expr", dest="expr", default=None, type=str,
                        help="Selection expression (for numexpr) for simulation particles.")
    parser.add_argument("--N", dest="N", default=None, type=int,
                        help="Number of particles.")
    parser.add_argument("--seed", dest="seed", default=None, type=int,
                        help="Seed for random number generator.")

    parser.add_argument("--mass", dest="mass", required=True, type=str,
                        help="Satellite mass.")

    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    elif args.quiet:
        logging.basicConfig(level=logging.ERROR)
    else:
        logging.basicConfig(level=logging.INFO)

    """
        e.g.:

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=256 --file=/Users/adrian/Projects/streams/data/observed_particles/2.5e6_N1024.hdf5 \
--seed=42 --mass="2.5e6" --overwrite

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=256 --file=/Users/adrian/projects/streams/data/observed_particles/2.5e7_N1024.hdf5 \
--seed=42 --mass="2.5e7" --overwrite

python scripts/observe_simulation.py -v --class_name=SgrSimulation --expr='tub!=0' \
--N=256 --file=/Users/adrian/projects/streams/data/observed_particles/2.5e8_N1024.hdf5 \
--seed=42 --mass="2.5e8" --overwrite
    """

    # TODO: class kwargs
    # particle_error_model=gaia_spitzer_errors, satellite_error_model=gaia_spitzer_errors,
    # particle_error_model=gaia_spitzer_errors, satellite_error_model=None,
    observe_simulation(args.class_name,
        particle_error_model=gaia_spitzer_errors, satellite_error_model=gaia_spitzer_errors,
        selection_expr=args.expr, N=args.N, output_file=args.output_file,
        overwrite=args.overwrite, seed=args.seed, class_kwargs=dict(mass=args.mass))
<|endoftext|>


<commit_before>from django.conf.urls import patterns, url

from django.views.generic import TemplateView

from gnowsys_ndf.ndf.views import *

urlpatterns = patterns('gnowsys_ndf.ndf.views.ajax_views',
                       url(r'^collection/', 'select_drawer', name='select_drawer'),
                       url(r'^collectionNav/', 'collection_nav', name='collection_nav'),
                       url(r'^collectionView/', 'collection_view', name='collection_view'),
                       url(r'^shelf/', 'shelf', name='shelf'),
                       url(r'^add_subThemes/', 'add_sub_themes', name='add_sub_themes'),
                       url(r'^add_ThemeItems', 'add_theme_item', name='add_theme_item'),
                       url(r'^add_pages/', 'add_page', name='add_page'),
                       url(r'^add_files/', 'add_file', name='add_file'),
                       url(r'^deleteThemes/', 'delete_themes', name='delete_themes'),
                       url(r'^add_Topics/', 'add_topics', name='add_topics'),
                       url(r'^get_tree_hierarchy/(?P<node_id>[\w-]+)$', 'get_tree_hierarchy', name='get_tree_hierarchy'),
                       url(r'^drawer/', 'drawer_widget', name='drawer_widget'),
                       url(r'^change_group_settings/', 'change_group_settings', name='change_group_settings'),
                       url(r'^make_module/', 'make_module_set', name='make_module'),
                       url(r'^get_module_json/', 'get_module_json', name='get_module_json'),
                       url(r'^get_graph_json/', 'graph_nodes', name='get_graph_json'),
                       url(r'^get_data_for_drawer/', 'get_data_for_drawer', name='get_data_for_drawer'),
                       url(r'^get_data_for_drawer_of_attributetype_set/', 'get_data_for_drawer_of_attributetype_set', name='get_data_for_drawer_of_attributetype_set'),
                       url(r'^get_data_for_drawer_of_relationtype_set/', 'get_data_for_drawer_of_relationtype_set', name='get_data_for_drawer_of_relationtype_set'),
                       url(r'^deletionInstances/', 'deletion_instances', name='deletion_instances'),
                       url(r'^get_visited_location/', 'get_visited_location', name='get_visited_location'),
                       url(r'^get_online_editing_user/', 'get_online_editing_user', name="get_online_editing_user"),
                       url(r'^view_articles/', 'view_articles', name="view_articles"),
                       url(r'^get_author_set_users/', 'get_author_set_users', name="get_author_set_users"),
                       url(r'^get_filterd_user_list/', 'get_filterd_user_list', name="get_filterd_user_list"),
                       url(r'^search_tasks/', 'search_tasks', name="search_tasks"),
                       url(r'^get_group_member_user/', 'get_group_member_user', name="get_group_member_user"),
                       url(r'^remove_user_from_author_set/', 'remove_user_from_author_set', name="remove_user_from_author_set"),
                       url(r'^get_data_for_user_drawer/', 'get_data_for_user_drawer', name='get_data_for_user_drawer'),
                       url(r'^get_data_for_batch_drawer/', 'get_data_for_batch_drawer', name='get_data_for_batch_drawer'),

                       url(r'^set_user_link/', 'set_user_link', name='set_user_link'),
                       url(r'^set_enrollment_code/', 'set_enrollment_code', name='set_enrollment_code'),
                       url(r'^get_students_assignments/', 'get_students_assignments', name='get_students_assignments'),
                       url(r'^get_districts/', 'get_districts', name='get_districts'),

                       url(r'^edit_task_title/', 'edit_task_title', name='edit_task_title'),
                       url(r'^edit_task_content/', 'edit_task_content', name='edit_task_content'),

                       url(r'^get_announced_courses/', 'get_announced_courses', name='get_announced_courses'),

)
<commit_msg>Url added that redirects to function-view (get_anncourses_allstudents)<commit_after>from django.conf.urls import patterns, url

from django.views.generic import TemplateView

from gnowsys_ndf.ndf.views import *

urlpatterns = patterns('gnowsys_ndf.ndf.views.ajax_views',
                       url(r'^collection/', 'select_drawer', name='select_drawer'),
                       url(r'^collectionNav/', 'collection_nav', name='collection_nav'),
                       url(r'^collectionView/', 'collection_view', name='collection_view'),
                       url(r'^shelf/', 'shelf', name='shelf'),
                       url(r'^add_subThemes/', 'add_sub_themes', name='add_sub_themes'),
                       url(r'^add_ThemeItems', 'add_theme_item', name='add_theme_item'),
                       url(r'^add_pages/', 'add_page', name='add_page'),
                       url(r'^add_files/', 'add_file', name='add_file'),
                       url(r'^deleteThemes/', 'delete_themes', name='delete_themes'),
                       url(r'^add_Topics/', 'add_topics', name='add_topics'),
                       url(r'^get_tree_hierarchy/(?P<node_id>[\w-]+)$', 'get_tree_hierarchy', name='get_tree_hierarchy'),
                       url(r'^drawer/', 'drawer_widget', name='drawer_widget'),
                       url(r'^change_group_settings/', 'change_group_settings', name='change_group_settings'),
                       url(r'^make_module/', 'make_module_set', name='make_module'),
                       url(r'^get_module_json/', 'get_module_json', name='get_module_json'),
                       url(r'^get_graph_json/', 'graph_nodes', name='get_graph_json'),
                       url(r'^get_data_for_drawer/', 'get_data_for_drawer', name='get_data_for_drawer'),
                       url(r'^get_data_for_drawer_of_attributetype_set/', 'get_data_for_drawer_of_attributetype_set', name='get_data_for_drawer_of_attributetype_set'),
                       url(r'^get_data_for_drawer_of_relationtype_set/', 'get_data_for_drawer_of_relationtype_set', name='get_data_for_drawer_of_relationtype_set'),
                       url(r'^deletionInstances/', 'deletion_instances', name='deletion_instances'),
                       url(r'^get_visited_location/', 'get_visited_location', name='get_visited_location'),
                       url(r'^get_online_editing_user/', 'get_online_editing_user', name="get_online_editing_user"),
                       url(r'^view_articles/', 'view_articles', name="view_articles"),
                       url(r'^get_author_set_users/', 'get_author_set_users', name="get_author_set_users"),
                       url(r'^get_filterd_user_list/', 'get_filterd_user_list', name="get_filterd_user_list"),
                       url(r'^search_tasks/', 'search_tasks', name="search_tasks"),
                       url(r'^get_group_member_user/', 'get_group_member_user', name="get_group_member_user"),
                       url(r'^remove_user_from_author_set/', 'remove_user_from_author_set', name="remove_user_from_author_set"),
                       url(r'^get_data_for_user_drawer/', 'get_data_for_user_drawer', name='get_data_for_user_drawer'),
                       url(r'^get_data_for_batch_drawer/', 'get_data_for_batch_drawer', name='get_data_for_batch_drawer'),

                       url(r'^set_user_link/', 'set_user_link', name='set_user_link'),
                       url(r'^set_enrollment_code/', 'set_enrollment_code', name='set_enrollment_code'),
                       url(r'^get_students_assignments/', 'get_students_assignments', name='get_students_assignments'),
                       url(r'^get_districts/', 'get_districts', name='get_districts'),

                       url(r'^edit_task_title/', 'edit_task_title', name='edit_task_title'),
                       url(r'^edit_task_content/', 'edit_task_content', name='edit_task_content'),

                       url(r'^get_announced_courses/', 'get_announced_courses', name='get_announced_courses'),
                       url(r'^get_anncourses_allstudents/', 'get_anncourses_allstudents', name='get_anncourses_allstudents'),

)
<|endoftext|>


<commit_before>
# Various utilities and data for color processing

# RGB is the NTSC receiver primary color coordinate system
# CIERGB is the CIE, spectral RGB primary system

CIE_P1 = 700
CIE_P2 = 546.1
CIE_P3 = 435.8

XYX_from_CIERGB = [[0.490, 0.310, 0.200],
                   [0.177, 0.813, 0.011],
                   [0.000, 0.010, 0.990]]

RGB_from_XYZ = [[1.910, -0.533, -0.288],
                [-0.985, 2.000, -0.028],
                [0.058, -0.118, 0.896]]

YIQ_from_RGB = [[0.299, 0.587, 0.114],
                [0.596, -0.274, -0.322],
                [0.211, -0.523, 0.312]]

UVW_from_XYZ = [[2.0/3.0, 0, 0],
                [0,1,0],
                [-0.5,1.5,0.5]]

# Read XYZ spectral matching curves from file

varnames = ['XYZ_CIE31','XYZ_CIE64','XYZ_CIEJV']
k=-1
thisdict = globals()
for name in ['ciexyz31_1.txt','ciexyz64_1.txt','ciexyzjv.txt']:
    k = k + 1
    afile = open(name)
    lines = afile.readlines()
    afile.close()
    wlen = []
    xl = []
    yl = []
    zl = []
    for line in lines:
        this = line.split(',')
        if this[0][0] not in '0123456789':
            break
        wlen.append(int(this[0].strip()))
        xl.append(float(this[1].strip()))
        yl.append(float(this[2].strip()))
        zl.append(float(this[3].strip()))

    thisdict[varnames[k]] = (wlen,xl,yl,zl)

del thisdict, wlen, xl, yl, zl, afile, lines, this

<commit_msg>added lab and srgb coordinate systems<commit_after>
import scipy_base as sb
import scipy

# Various utilities and data for color processing
#  See http://www.cvrl.org/ for much of the data

# RGB  the linear sRGB color space using D65 as a white-point
#      (IEC 61966-2-1).  Represents standard monitor (w/o gamma correction).
# sRGB is the non-linear color-space (w/ gamma correction)
# RGBntsc is the NTSC receiver primary color coordinate system
# RGBcie is the CIE, monochromatic RGB primary system
# RGBsb is the Stiles & Burch (1955) 2-deg color coordinate system

# Primaries for the coordinate systems
cie_primaries = [700, 546.1, 435.8]

sb_primaries = [1./155 * 1e5, 1./190 * 1e5, 1./225 * 1e5]

# Matrices from Jain

xyz_from_rgbcie = [[0.490, 0.310, 0.200],
                   [0.177, 0.813, 0.011],
                   [0.000, 0.010, 0.990]]

rgbcie_from_xyz = scipy.linalg.inv(XYZ_from_RGBcie)

rgbntsc_from_xyz = [[1.910, -0.533, -0.288],
                    [-0.985, 2.000, -0.028],
                    [0.058, -0.118, 0.896]]

yiq_from_rgbntsc = [[0.299, 0.587, 0.114],
                    [0.596, -0.274, -0.322],
                    [0.211, -0.523, 0.312]]

uvw_from_xyz = [[2.0/3.0, 0, 0],
                [0,1,0],
                [-0.5,1.5,0.5]]

# From sRGB specification

xyz_from_rgb =  [[0.412453, 0.357580, 0.180423],
                 [0.212671, 0.715160, 0.072169],
                 [0.019334, 0.119193, 0.950227]]

rgb_from_xyz = scipy.linalg.inv(XYZ_from_RGB)

# LMS color space spectral matching curves provide the
#  spectral response curves of three types of cones.
#
#
# Vos, Estevez, and Walraven (1990)
# with alteration in S-cone sensitivity from
#  Stockman and Sharpe (2000)
# scaled so that sum(LMS) has a peak of 1
#  just like LMS_from_XYZ

lms_from_rgbsb = [[0.14266235473644004, 0.49009667755566039,
                   0.028959576047175539],
                  [0.013676614570405768, 0.35465861798651171,
                   0.029062883056895625],
                  [0.0, 0.00029864360424843419, 0.01837806004659253]]

# use Judd, Vos CIE color matching curves XYZJV
#  with Stockman and Sharpe(2000) S-cone alteration.
# scaled so that sum(LMS,axis=0) has a peak of 1
#  based on CIE standard observer

lms_from_xyz = [[0.15513920309034629, 0.54298741130344153,
                 -0.037010041369525896],
                [-0.15513920309034629, 0.45684891207177714,
                 0.029689739651154123],
                [0.0, 6.3686624249879016e-05, 0.0073203016383768691]]

# Read spectral matching curves from file
# XYZJV and RGBsb55 are most modern curves to use
# LMScvrl are the cone response curves from www.cvrl.org
#   (normalized to peak at 1 for all three cones)

varnames = ['xyz31','xyz64','xyzjv','rgbsb55','lmscvrl']
k=-1
thisdict = globals()
for name in ['ciexyz31_1.txt','ciexyz64_1.txt','ciexyzjv.txt',
             'sbrgb2.txt','linss2_10e_1.txt']:
    k = k + 1
    afile = open(name)
    lines = afile.readlines()
    afile.close()
    wlen = []
    xl = []
    yl = []
    zl = []
    for line in lines:
        this = line.split(',')
        if this[0].strip()[0] not in '0123456789':
            break
        wlen.append(int(this[0].strip()))
        xl.append(float(this[1].strip()))
        yl.append(float(this[2].strip()))
        try:
            zl.append(float(this[3].strip()))
        except ValueError, inst:
            msg = inst.args[0]
            if msg.startswith("empty string"):
                zl.append(0.0)
            else:
                raise inst

    thisdict[varnames[k]] = (wlen,xl,yl,zl)

del thisdict, wlen, xl, yl, zl, afile, lines, this

# XYZ white-point coordinates
#  from http://www.aim-dtp.net/aim/technology/cie_xyz/cie_xyz.htm

whitepoints = {'CIE A': ['Normal incandescent', 0.4476, 0.4074],
               'CIE B': ['Direct sunlight', 0.3457, 0.3585],
               'CIE C': ['Average sunlight', 0.3101, 0.3162],
               'CIE E': ['Normalized reference', 1.0/3, 1.0/3],
               'D50' : ['Bright tungsten', 0.3457, 0.3585],
               'D55' : ['Cloudy daylight', 0.3324, 0.3474],
               'D65' : ['Daylight', 0.312713, 0.329016],
               'D75' : ['?', 0.299, 0.3149],
               'D93' : ['low-quality old CRT', 0.2848, 0.2932]
               }
# convert to X,Y,Z white-point

def triwhite(chrwhite):
    x,y = chrwhite
    X = x / y
    Y = 1.0
    Z = (1-x-y)/y
    return X,Y,Z

for key in whitepoints.keys():
    whitepoints[key].append(triwhite(whitepoints[key][1:]))
del key

def tri2chr(tri,axis=None):
    """Convert tristimulus values to chromoticity values"""
    tri = asarray(tri)
    n = len(tri.shape)
    if axis is None:
        axis = coloraxis(tri.shape)
    slices = []
    for k in range(n):
        slices.append(slice(None))
    slices[axis] = NewAxis
    norm = scipy.sum(tri,axis=axis)[slices]
    slices[axis] = slice(None,2)
    out = tri[slices]/norm
    return out

# find the lowest dimension of size 3
def coloraxis(shape):
    for k, val in enumerate(shape):
        if val == 3:
            return k
    raise ValueError, "No Color axis found."

def convert(matrix,TTT,axis=None):
    TTT = asarray(TTT)
    if axis is None:
        axis = coloraxis(TTT.shape)
    if (axis != 0):
        TTT = sb.swapaxes(TTT,0,axis)
    oldshape = TTT.shape
    TTT = reshape(TTT,(3,-1))
    OUT = dot(matrix, TTT)
    OUT.shape = oldshape
    if (axis != 0):
        OUT = sb.swapaxes(OUT,axis,0)
    return OUT

def xyz2rgbcie(xyz,axis=None):
    return convert(rgbcie_from_xyz, xyz, axis)

def xyz2rgb(xyz,axis=None):
    return convert(rgb_from_xyz, xyz, axis)

def rgb2xyz(rgb, axis=None):
    return convert(xyz_from_rgb, rgb, axis)

def makeslices(n):
    slices = []
    for k in range(n):
        slices.append(slice(None))
    return slices

def separate_colors(xyz,axis=None):
    if axis is None:
        axis = coloraxis(xyz.shape)
    n = len(xyz.shape)
    slices = makeslices(n)
    slices[axis] = 0
    x = xyz[slices]
    slices[axis] = 1
    y = xyz[slices]
    slices[axis] = 2
    z = xyz[slices]
    return x, y, z, axis

def join_colors(c1,c2,c3,axis):
    c1,c2,c3 = asarray(c1),asarray(c2),asarray(c3)
    newshape = c1.shape[:axis] + (1,) + c1.shape[axis:]
    c1.shape = newshape
    c2.shape = newshape
    c3.shape = newshape
    return sb.concatenate((c1,c2,c3),axis=axis)

def xyz2lab(xyz, axis=None, wp=whitepoints['D65'][-1], doclip=True):
    x,y,z,axis = separate_colors(xyz,axis)
    xn,yn,zn = x/wp[0], y/wp[1], z/wp[2]
    def f(t):
        eps = 216/24389.
        kap = 24389/27.
        return sb.where(t > eps,
                        sb.power(t, 1.0/3),
                        (kap*t + 16.0)/116)
    fx,fy,fz = f(xn), f(yn), f(zn)
    L = 116*fy - 16
    a = 500*(fx - fy)
    b = 200*(fy - fz)
    if doclip:
        L = sb.clip(L, 0.0, 100.0)
        a = sb.clip(a, -500.0, 500.0)
        b = sb.clip(b, -200.0, 200.0)
    return join_colors(L,a,b,axis)

def lab2xyz(lab, axis=None, wp=whitepoints['D65'][-1]):
    lab = asarray(lab)
    L,a,b,axis = separate_colors(lab,axis)
    fy = (L+16)/116.0
    fz = fy - b / 200.
    fx = a/500.0 + fy
    def finv(y):
        eps3 = (216/24389.)**3
        kap = 24389/27.
        return sb.where(y > eps3,
                        sb.power(y,3),
                        (116*y-16)/kap)
    xr, yr, zr = finv(fx), finv(fy), finv(fz)
    return join_colors(xr*wp[0],yr*wp[1],zr*wp[2],axis)

def rgb2lab(rgb):
    return xyz2lab(rgb2xyz(rgb))

def lab2rgb(lab):
    return xyz2rgb(lab2xyz(lab))

# sRGB <-> sR'G'B'  equations from
#   http://www.w3.org/Graphics/Color/sRGB
#   http://www.srgb.com/basicsofsrgb.htm

# rgb2rgbp gives the nonlinear (gamma corrected) sR'G'B' from
#    linear sRGB values
#   approximately the same as rgb**(1.0/2.2)
def rgb2rgbp(rgb):
    rgb = asarray(rgb)
    eps = 0.0031308
    return where(rgb < eps, 12.92*rgb,
                 1.055*rgb**(1.0/2.4) - 0.055)

# rgbp2rgb gives linear sRGB values from nonlinear sR'G'B' values
#
#  approximately the same as rgbp**2.2
def rgbp2rgb(rgbp,axis=None):
    rgbp = asarray(rgbp)
    eps = 0.04045
    return where(rgbp <= eps, rgbp / 12.92,
                 power((rgbp + 0.055)/1.055,2.4))

<|endoftext|>


<commit_before>import random
import sys

class Player:

    def __init__(self):
        self.spot = 1
        self.hp = 3
        self.key = 0
        self.talisman = 0
        self.potion1 = 0
        self.potion2 = 0
        self.pole = 0

    def move_to_spot(self, spot):
        self.spot = spot

    def move_player(self, spots):
        self.spot += spots
        print self.spot

    def take_damage(self, dmg):
        self.hp += dmg

    def add_item(self, item):
        setattr(self, item, True)
        if item.startswith('potion'):
            self.hp += 2

    def lower_pole(self):
        self.pole = True

    def has_item(self, item):
        return getattr(self, item)

    def pole_down(self):
        return self.pole

    def current_spot(self):
        return self.spot

    def current_hp(self):
        return self.hp

class Game:

    def __init__(self):
        print
        print 'A new Doomed Soul enters the Deadly Danger Dungeon'
        self.player = Player()

    def check_spot(self):
        if self.player.current_spot() in dungeon:
            events[dungeon[self.player.current_spot()]].do(self.player)

    def move_spots(self):
        roll = roll_dice()
        return roll

    def check_win(self):
        if self.player.current_spot() in range(161, 170):
            print 'Player escaped!'
            return True

    def check_alive(self):
        if self.player.current_hp() > 0:
            return True
        print 'Player is dead'

    def take_turn(self):
        if not self.check_for_special():
            self.player.move_player(self.move_spots())
            self.check_spot()
        else:

            if self.pass_key():
                if self.player.current_spot() + self.move_spots() == 39:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_talisman():
                if self.player.current_spot() + self.move_spots() == 191:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_door():
                print "Used key."
                self.player.move_player(self.move_spots())
                self.check_spot()

            if self.bottom_level():
                if not self.player.pole_down:
                    if self.player.current_spot() + self.move_spots() < 195:
                        self.player.move_player(self.move_spots())
                        self.check_spot()

                if self.pass_ladder:
                    if self.player.has_item('key') == True:
                        overage = int((self.player.current_spot() + self.move_spots)) - 195
                        self.player.move_to_spot((overage + 43))
                    else:
                        overage = int((self.player.current_spot() + self.move_spots)) - 195
                        self.player.move_to_spot((overage + 35))

                else:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_use_talisman():
                print "Used talisman."
                self.player.move_player(self.move_spots())
                self.check_spot()

    def check_for_special(self):
        return self.pass_key() or self.pass_talisman or self.pass_door() or self.bottom_level()

    def pass_key(self):
        return self.player.current_spot() + self.move_spots() in range(39, 44)

    def pass_talisman(self):
        return self.player.current_spot() + self.move_spots() in range(191, 197)

    def pass_door(self):
        return self.player.current_spot() + self.move_spots() in range(82, 86)

    def bottom_level(self):
        return self.player.current_spot() in range(172, 195)

    def pass_pole(self):
        return self.player.current_spot() + self.move_spots() in range(181,187)

    def pass_ladder(self):
        return self.player.current_spot() + self.move_spots() in range(195, 201)

    def pass_use_talisman(self):
        return self.player.current_spot() + self.move_spots() in range(146, 152)

def roll_dice():
    d6 = random.randint(1, 6)
    return d6

class Event:

    def __init__(self, message, func = None):
        self.message = message
        self.func = func

    def do(self, player):
        if self.func:
            self.func(player)
            print self.message

events = {
    1: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(25)),
    2: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(8)),
    3: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(13)),
    4: Event('Fell into crocodile pit and died', lambda p: p.take_damage(-p.current_hp())),
    5: Event('Fell into spikes and died', lambda p: p.take_damage(-p.current_hp())),
    6: Event('Found potion (+2 HP)', lambda p: p.add_item('potion1')),
    7: Event('Hit by falling boulder (-1 HP)', lambda p: p.take_damage(-1)),
    8: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(177)),
    9: Event('Shot by arrows (-1 HP)', lambda p: p.take_damage(-1)),
    10: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(174)),
    11: Event('Found potion (+2 HP)', lambda p: p.add_item('potion2')),
    12: Event('Found the key', lambda p: p.add_item('key')),
    13: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(64)),
    14: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(69)),
    15: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(53)),
    16: Event('Choked on poisonous gas (-1 HP)', lambda p: p.take_damage(-1)),
    17: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(78)),
    18: Event('Fell into fire and burned to death', lambda p: p.take_damage(-p.current_hp())),
    19: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(74)),
    21: Event('Found the Talisman', lambda p: p.add_item('talisman')),
    22: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(128)),
    23: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(124)),
    24: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(53)),
    25: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(133)),
    26: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(137)),
    28: Event('Fell from the ladder (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(146)),
    29: Event('Fell from the ladder and died', lambda p: p.take_damage(-p.current_hp())),
    31: Event('Cave in (-1 HP)', lambda p: p.take_damage(-1)),
    32: Event('Touched the border and lowered the pole', lambda p: p.lower_pole())
}

dungeon = {
    4: 1,
    5: 2,
    10: 3,
    15: 4,
    18: 5,
    19: 6,
    20: 5,
    23: 7,
    28: 8,
    30: 9,
    31: 9,
    32: 10,
    34: 11,
    39: 12,
    44: 11,
    46: 10,
    50: 8,
    54: 7,
    57: 5,
    58: 6,
    59: 5,
    62: 4,
    67: 13,
    72: 14,
    73: 15, # Come back to this
    79: 5,
    80: 5,
    82: 16,
    84: 17,
    86: 18,
    89: 18,
    91: 19,
    92: 16,
    94: 3,
    101: 21,
    108: 3,
    110: 16,
    111: 22,
    113: 18,
    113: 18,
    118: 23,
    120: 16,
    122: 5,
    123: 5,
    129: 24, # Come back to this
    130: 25,
    135: 26,
    140: 4,
    143: 5,
    149: 28,
    151: 28,
    153: 28,
    155: 29,
    157: 29,
    179: 31,
    181: 32,
    183: 31
}

wins = 0
deaths = 0
games = 0
finished_games = 0
turn = 0

try:
    games = sys.argv[1]
except:
    games = 1

while finished_games < int(games):
    # Begin the game
    game = Game()
    while not game.check_win() and game.check_alive():
        game.take_turn()
    finished_games +=1
    if game.check_win():
        wins +=1
    else:
        deaths +=1

print str(games) + ' games played'
print str(deaths) + ' deaths'
print str(wins) + ' wins'
<commit_msg>Fixed bug where player wouldnt start moving<commit_after>import random
import sys

class Player:

    def __init__(self):
        self.spot = 1
        self.hp = 3
        self.key = 0
        self.talisman = 0
        self.potion1 = 0
        self.potion2 = 0
        self.pole = 0

    def move_to_spot(self, spot):
        self.spot = spot

    def move_player(self, spots):
        self.spot += spots

    def take_damage(self, dmg):
        self.hp += dmg

    def add_item(self, item):
        setattr(self, item, True)
        if item.startswith('potion'):
            self.hp += 2

    def lower_pole(self):
        self.pole = True

    def has_item(self, item):
        return getattr(self, item)

    def pole_down(self):
        return self.pole

    def current_spot(self):
        return self.spot

    def current_hp(self):
        return self.hp

class Game:

    def __init__(self):
        print
        print 'A new Doomed Soul enters the Deadly Danger Dungeon'
        self.player = Player()

    def check_spot(self):
        print self.player.current_spot()
        if self.player.current_spot() in dungeon:
            events[dungeon[self.player.current_spot()]].do(self.player)

    def move_spots(self):
        roll = roll_dice()
        return roll

    def check_win(self):
        if self.player.current_spot() in range(161, 170):
            print 'Player escaped!'
            return True

    def check_alive(self):
        if self.player.current_hp() > 0:
            return True
        print 'Player is dead'

    def take_turn(self):
        if not self.check_for_special():
            self.player.move_player(self.move_spots())
            self.check_spot()
        else:

            if self.pass_key():
                if self.player.current_spot() + self.move_spots() == 39:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_talisman():
                if self.player.current_spot() + self.move_spots() == 191:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_door():
                print "Used key."
                self.player.move_player(self.move_spots())
                self.check_spot()

            if self.bottom_level():
                if not self.player.pole_down:
                    if self.player.current_spot() + self.move_spots() < 195:
                        self.player.move_player(self.move_spots())
                        self.check_spot()

                if self.pass_ladder:
                    if self.player.has_item('key') == True:
                        overage = int((self.player.current_spot() + self.move_spots)) - 195
                        self.player.move_to_spot((overage + 43))
                    else:
                        overage = int((self.player.current_spot() + self.move_spots)) - 195
                        self.player.move_to_spot((overage + 35))

                else:
                    self.player.move_player(self.move_spots())
                    self.check_spot()

            if self.pass_use_talisman():
                print "Used talisman."
                self.player.move_player(self.move_spots())
                self.check_spot()

            self.player.move_player(self.move_spots())
            self.check_spot()

    def check_for_special(self):
        return self.pass_key() or self.pass_talisman or self.pass_door() or self.bottom_level()

    def pass_key(self):
        return self.player.current_spot() + self.move_spots() in range(39, 44)

    def pass_talisman(self):
        return self.player.current_spot() + self.move_spots() in range(191, 197)

    def pass_door(self):
        return self.player.current_spot() + self.move_spots() in range(82, 86)

    def bottom_level(self):
        return self.player.current_spot() in range(172, 195)

    def pass_pole(self):
        return self.player.current_spot() + self.move_spots() in range(181,187)

    def pass_ladder(self):
        return self.player.current_spot() + self.move_spots() in range(195, 201)

    def pass_use_talisman(self):
        return self.player.current_spot() + self.move_spots() in range(146, 152)

def roll_dice():
    d6 = random.randint(1, 6)
    return d6

class Event:

    def __init__(self, message, func = None):
        self.message = message
        self.func = func

    def do(self, player):
        if self.func:
            self.func(player)
            print self.message

events = {
    1: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(25)),
    2: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(8)),
    3: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(13)),
    4: Event('Fell into crocodile pit and died', lambda p: p.take_damage(-p.current_hp())),
    5: Event('Fell into spikes and died', lambda p: p.take_damage(-p.current_hp())),
    6: Event('Found potion (+2 HP)', lambda p: p.add_item('potion1')),
    7: Event('Hit by falling boulder (-1 HP)', lambda p: p.take_damage(-1)),
    8: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(177)),
    9: Event('Shot by arrows (-1 HP)', lambda p: p.take_damage(-1)),
    10: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(174)),
    11: Event('Found potion (+2 HP)', lambda p: p.add_item('potion2')),
    12: Event('Found the key', lambda p: p.add_item('key')),
    13: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(64)),
    14: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(69)),
    15: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(53)),
    16: Event('Choked on poisonous gas (-1 HP)', lambda p: p.take_damage(-1)),
    17: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(78)),
    18: Event('Fell into fire and burned to death', lambda p: p.take_damage(-p.current_hp())),
    19: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(74)),
    21: Event('Found the Talisman', lambda p: p.add_item('talisman')),
    22: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(128)),
    23: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(124)),
    24: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(53)),
    25: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(133)),
    26: Event('Fell down deeper into the dungeon (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(137)),
    28: Event('Fell from the ladder (-1 HP)', lambda p: p.take_damage(-1) and p.move_to_spot(146)),
    29: Event('Fell from the ladder and died', lambda p: p.take_damage(-p.current_hp())),
    31: Event('Cave in (-1 HP)', lambda p: p.take_damage(-1)),
    32: Event('Touched the border and lowered the pole', lambda p: p.lower_pole())
}

dungeon = {
    4: 1,
    5: 2,
    10: 3,
    15: 4,
    18: 5,
    19: 6,
    20: 5,
    23: 7,
    28: 8,
    30: 9,
    31: 9,
    32: 10,
    34: 11,
    39: 12,
    44: 11,
    46: 10,
    50: 8,
    54: 7,
    57: 5,
    58: 6,
    59: 5,
    62: 4,
    67: 13,
    72: 14,
    73: 15, # Come back to this
    79: 5,
    80: 5,
    82: 16,
    84: 17,
    86: 18,
    89: 18,
    91: 19,
    92: 16,
    94: 3,
    101: 21,
    108: 3,
    110: 16,
    111: 22,
    113: 18,
    113: 18,
    118: 23,
    120: 16,
    122: 5,
    123: 5,
    129: 24, # Come back to this
    130: 25,
    135: 26,
    140: 4,
    143: 5,
    149: 28,
    151: 28,
    153: 28,
    155: 29,
    157: 29,
    179: 31,
    181: 32,
    183: 31
}

wins = 0
deaths = 0
games = 0
finished_games = 0
turn = 0

try:
    games = sys.argv[1]
except:
    games = 1

while finished_games < int(games):
    # Begin the game
    game = Game()
    while not game.check_win() and game.check_alive():
        game.take_turn()
    finished_games +=1
    if game.check_win():
        wins +=1
    else:
        deaths +=1

print str(games) + ' games played'
print str(deaths) + ' deaths'
print str(wins) + ' wins'
<|endoftext|>


<commit_before># Copyright 2016 Raytheon BBN Technologies
#
# 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

import os
import platform
from copy import deepcopy

import numpy as np
import scipy.signal

from .filter import Filter
from auspex.parameter import Parameter, IntParameter, FloatParameter
from auspex.stream import  DataStreamDescriptor, InputConnector, OutputConnector
from auspex.log import logger

try:
    # load libchannelizer to access Intel IPP filtering functions
    import numpy.ctypeslib as npct
    from ctypes import c_int, c_size_t
    np_float  = npct.ndpointer(dtype=np.float32, flags='C_CONTIGUOUS')

    libchannelizer_path = os.path.abspath(os.path.join( os.path.dirname(__file__), "libchannelizer"))
    if "Windows" in platform.platform():
        os.environ["PATH"] += ";" + libchannelizer_path
    libipp = npct.load_library("libchannelizer",  libchannelizer_path)
    libipp.filter_records_fir.argtypes = [np_float, c_size_t, c_int, np_float, c_size_t, c_size_t, np_float]
    libipp.filter_records_iir.argtypes = [np_float, c_size_t, np_float, c_size_t, c_size_t, np_float]
    libipp.init()

    load_fallback = False
except:
    logger.warning("Could not load channelizer library; falling back to python methods.")
    load_fallback = True

class Channelizer(Filter):
    """Digital demodulation and filtering to select a particular frequency multiplexed channel"""

    sink              = InputConnector()
    source            = OutputConnector()
    decimation_factor = IntParameter(value_range=(1,100), default=2, snap=1)
    frequency         = FloatParameter(value_range=(-5e9,5e9), increment=1.0e6, default=-9e6)
    bandwidth         = FloatParameter(value_range=(0.00, 100e6), increment=0.1e6, default=5e6)

    def __init__(self, frequency=None, bandwidth=None, decimation_factor=None, **kwargs):
        super(Channelizer, self).__init__(**kwargs)
        if frequency:
            self.frequency.value = frequency
        if bandwidth:
            self.bandwidth.value = bandwidth
        if decimation_factor:
            self.decimation_factor.value = decimation_factor
        self.quince_parameters = [self.decimation_factor, self.frequency, self.bandwidth]

    def update_descriptors(self):
        logger.debug('Updating Channelizer "%s" descriptors based on input descriptor: %s.', self.name, self.sink.descriptor)

        # extract record time sampling
        time_pts = self.sink.descriptor.axes[-1].points
        self.record_length = len(time_pts)
        self.time_step = time_pts[1] - time_pts[0]
        logger.debug("Channelizer time_step = {}".format(self.time_step))

        # convert bandwidth normalized to Nyquist interval
        n_bandwidth = self.bandwidth.value * self.time_step * 2
        n_frequency = self.frequency.value * self.time_step * 2

        # arbitrarily decide on three stage filter pipeline
        # 1. first stage decimating filter on real data
        # 2. second stage decimating filter on mixed product to boost n_bandwidth
        # 3. final channel selecting filter at n_bandwidth/2

        # anecdotally don't decimate more than a factor of eight for stability

        self.decim_factors = [1]*3
        self.filters = [None]*3

        # first stage decimating filter
        # maximize first stage decimation:
        #     * minimize subsequent stages time taken
        #     * filter and decimate while signal is still real
        #     * first stage decimation cannot be too large or then 2omega signal from mixing will alias
        d1 = 1
        while (d1 < 8) and (2*n_frequency <= 0.8/d1) and (d1 < self.decimation_factor.value):
            d1 *= 2
            n_bandwidth *= 2
            n_frequency *= 2

        if d1 > 1:
            # create an anti-aliasing filter
            # pass-band to 0.8 * decimation factor; anecdotally single precision needs order <= 4 for stability
            b,a = scipy.signal.cheby1(4, 3, 0.8/d1)
            b = np.float32(b)
            a = np.float32(a)
            self.decim_factors[0] = d1
            self.filters[0]  = (b,a)

        # store decimated reference for mix down
        ref = np.exp(2j*np.pi * self.frequency.value * time_pts[::d1], dtype=np.complex64)
        self.reference_r = np.real(ref)
        self.reference_i = np.imag(ref)

        # second stage filter to bring n_bandwidth/2 up
        # decimation cannot be too large or will impinge on channel bandwidth (keep n_bandwidth/2 <= 0.8)
        d2 = 1
        while (d2 < 8) and ((d1*d2) < self.decimation_factor.value) and (n_bandwidth/2 <= 0.8):
            d2 *= 2
            n_bandwidth *= 2
            n_frequency *= 2

        if d2 > 1:
            # create an anti-aliasing filter
            # pass-band to 0.8 * decimation factor; anecdotally single precision needs order <= 4 for stability
            b,a = scipy.signal.cheby1(4, 3, 0.8/d2)
            b = np.float32(b)
            a = np.float32(a)
            self.decim_factors[1] = d2
            self.filters[1]  = (b,a)

        # final channel selection filter
        if n_bandwidth < 0.1:
            raise ValueError("Insufficient decimation to achieve stable filter")

        b,a = scipy.signal.cheby1(4, 3, n_bandwidth/2)
        b = np.float32(b)
        a = np.float32(a)
        self.decim_factors[2] = self.decimation_factor.value // (d1*d2)
        self.filters[2]  = (b,a)

        # update output descriptors
        decimated_descriptor = DataStreamDescriptor()
        decimated_descriptor.axes = self.sink.descriptor.axes[:]
        decimated_descriptor.axes[-1] = deepcopy(self.sink.descriptor.axes[-1])
        decimated_descriptor.axes[-1].points = self.sink.descriptor.axes[-1].points[self.decimation_factor.value-1::self.decimation_factor.value]
        decimated_descriptor.exp_src = self.sink.descriptor.exp_src
        decimated_descriptor.dtype = np.complex64
        for os in self.source.output_streams:
            os.set_descriptor(decimated_descriptor)
            if os.end_connector is not None:
                os.end_connector.update_descriptors()

    async def process_data(self, data):
        # Assume for now we get a integer number of records at a time
        # TODO: handle partial records
        num_records = data.size // self.record_length
        reshaped_data = np.reshape(data, (num_records, self.record_length), order="C")

        # first stage decimating filter
        if self.filters[0] is not None:
            stacked_coeffs = np.concatenate(self.filters[0])
            # filter
            filtered = np.empty_like(reshaped_data)
            libipp.filter_records_iir(stacked_coeffs, self.filters[0][0].size-1, reshaped_data, self.record_length, num_records, filtered)

            # decimate
            if self.decim_factors[0] > 1:
                filtered = filtered[:, ::self.decim_factors[0]]

        # mix with reference
        # keep real and imaginary separate for filtering below
        filtered_r = self.reference_r * filtered
        filtered_i = self.reference_i * filtered

        # channel selection filters
        for ct in [1,2]:
            if self.filters[ct] == None:
                continue

            coeffs = self.filters[ct]
            stacked_coeffs = np.concatenate(self.filters[ct])
            out_r = np.empty_like(filtered_r)
            out_i = np.empty_like(filtered_i)
            libipp.filter_records_iir(stacked_coeffs, self.filters[ct][0].size-1, filtered_r, filtered_r.shape[-1], num_records, out_r)
            libipp.filter_records_iir(stacked_coeffs, self.filters[ct][0].size-1, filtered_i, filtered_i.shape[-1], num_records, out_i)

            # decimate
            if self.decim_factors[ct] > 1:
                filtered_r = np.copy(out_r[:, ::self.decim_factors[ct]], order="C")
                filtered_i = np.copy(out_i[:, ::self.decim_factors[ct]], order="C")
            else:
                filtered_r = out_r
                filtered_i = out_i

        filtered = filtered_r + 1j*filtered_i

        # recover gain from selecting single sideband
        filtered *= 2

        # push to ouptut connectors
        for os in self.source.output_streams:
            await os.push(filtered)

class LibChannelizerFallback(object):
    @staticmethod
    def filter_records_fir(coeffs,
                           num_taps, # ignored
                           decim_factor,
                           recs,
                           record_length, # ignored (uses shape of recs)
                           num_records, # ignored (uses shape of recs)
                           result):
        # split out a, b coefficients
        b = coeffs[:len(coeffs)//2]
        a = coeffs[len(coeffs)//2:]

        filtered_signal = scipy.signal.lfilter(b, a, recs)
        result[:] = np.copy(filtered_signal[:, ::decim_factor], order="C")

    @staticmethod
    def filter_records_iir(coeffs,
                           order, # ignored
                           recs,
                           record_length, # ignored (uses shape of recs)
                           num_records, # ignored (uses shape of recs)
                           result):
        # split out a, b coefficients
        b = coeffs[:len(coeffs)//2]
        a = coeffs[len(coeffs)//2:]

        result[:] = scipy.signal.lfilter(b, a, recs)

if load_fallback:
    libipp = LibChannelizerFallback()
<commit_msg>channelizer updates both points and original_points<commit_after># Copyright 2016 Raytheon BBN Technologies
#
# 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

import os
import platform
from copy import deepcopy

import numpy as np
import scipy.signal

from .filter import Filter
from auspex.parameter import Parameter, IntParameter, FloatParameter
from auspex.stream import  DataStreamDescriptor, InputConnector, OutputConnector
from auspex.log import logger

try:
    # load libchannelizer to access Intel IPP filtering functions
    import numpy.ctypeslib as npct
    from ctypes import c_int, c_size_t
    np_float  = npct.ndpointer(dtype=np.float32, flags='C_CONTIGUOUS')

    libchannelizer_path = os.path.abspath(os.path.join( os.path.dirname(__file__), "libchannelizer"))
    if "Windows" in platform.platform():
        os.environ["PATH"] += ";" + libchannelizer_path
    libipp = npct.load_library("libchannelizer",  libchannelizer_path)
    libipp.filter_records_fir.argtypes = [np_float, c_size_t, c_int, np_float, c_size_t, c_size_t, np_float]
    libipp.filter_records_iir.argtypes = [np_float, c_size_t, np_float, c_size_t, c_size_t, np_float]
    libipp.init()

    load_fallback = False
except:
    logger.warning("Could not load channelizer library; falling back to python methods.")
    load_fallback = True

class Channelizer(Filter):
    """Digital demodulation and filtering to select a particular frequency multiplexed channel"""

    sink              = InputConnector()
    source            = OutputConnector()
    decimation_factor = IntParameter(value_range=(1,100), default=2, snap=1)
    frequency         = FloatParameter(value_range=(-5e9,5e9), increment=1.0e6, default=-9e6)
    bandwidth         = FloatParameter(value_range=(0.00, 100e6), increment=0.1e6, default=5e6)

    def __init__(self, frequency=None, bandwidth=None, decimation_factor=None, **kwargs):
        super(Channelizer, self).__init__(**kwargs)
        if frequency:
            self.frequency.value = frequency
        if bandwidth:
            self.bandwidth.value = bandwidth
        if decimation_factor:
            self.decimation_factor.value = decimation_factor
        self.quince_parameters = [self.decimation_factor, self.frequency, self.bandwidth]

    def update_descriptors(self):
        logger.debug('Updating Channelizer "%s" descriptors based on input descriptor: %s.', self.name, self.sink.descriptor)

        # extract record time sampling
        time_pts = self.sink.descriptor.axes[-1].points
        self.record_length = len(time_pts)
        self.time_step = time_pts[1] - time_pts[0]
        logger.debug("Channelizer time_step = {}".format(self.time_step))

        # convert bandwidth normalized to Nyquist interval
        n_bandwidth = self.bandwidth.value * self.time_step * 2
        n_frequency = self.frequency.value * self.time_step * 2

        # arbitrarily decide on three stage filter pipeline
        # 1. first stage decimating filter on real data
        # 2. second stage decimating filter on mixed product to boost n_bandwidth
        # 3. final channel selecting filter at n_bandwidth/2

        # anecdotally don't decimate more than a factor of eight for stability

        self.decim_factors = [1]*3
        self.filters = [None]*3

        # first stage decimating filter
        # maximize first stage decimation:
        #     * minimize subsequent stages time taken
        #     * filter and decimate while signal is still real
        #     * first stage decimation cannot be too large or then 2omega signal from mixing will alias
        d1 = 1
        while (d1 < 8) and (2*n_frequency <= 0.8/d1) and (d1 < self.decimation_factor.value):
            d1 *= 2
            n_bandwidth *= 2
            n_frequency *= 2

        if d1 > 1:
            # create an anti-aliasing filter
            # pass-band to 0.8 * decimation factor; anecdotally single precision needs order <= 4 for stability
            b,a = scipy.signal.cheby1(4, 3, 0.8/d1)
            b = np.float32(b)
            a = np.float32(a)
            self.decim_factors[0] = d1
            self.filters[0]  = (b,a)

        # store decimated reference for mix down
        ref = np.exp(2j*np.pi * self.frequency.value * time_pts[::d1], dtype=np.complex64)
        self.reference_r = np.real(ref)
        self.reference_i = np.imag(ref)

        # second stage filter to bring n_bandwidth/2 up
        # decimation cannot be too large or will impinge on channel bandwidth (keep n_bandwidth/2 <= 0.8)
        d2 = 1
        while (d2 < 8) and ((d1*d2) < self.decimation_factor.value) and (n_bandwidth/2 <= 0.8):
            d2 *= 2
            n_bandwidth *= 2
            n_frequency *= 2

        if d2 > 1:
            # create an anti-aliasing filter
            # pass-band to 0.8 * decimation factor; anecdotally single precision needs order <= 4 for stability
            b,a = scipy.signal.cheby1(4, 3, 0.8/d2)
            b = np.float32(b)
            a = np.float32(a)
            self.decim_factors[1] = d2
            self.filters[1]  = (b,a)

        # final channel selection filter
        if n_bandwidth < 0.1:
            raise ValueError("Insufficient decimation to achieve stable filter")

        b,a = scipy.signal.cheby1(4, 3, n_bandwidth/2)
        b = np.float32(b)
        a = np.float32(a)
        self.decim_factors[2] = self.decimation_factor.value // (d1*d2)
        self.filters[2]  = (b,a)

        # update output descriptors
        decimated_descriptor = DataStreamDescriptor()
        decimated_descriptor.axes = self.sink.descriptor.axes[:]
        decimated_descriptor.axes[-1] = deepcopy(self.sink.descriptor.axes[-1])
        decimated_descriptor.axes[-1].points = self.sink.descriptor.axes[-1].points[self.decimation_factor.value-1::self.decimation_factor.value]
        decimated_descriptor.axes[-1].original_points = decimated_descriptor.axes[-1].points
        decimated_descriptor.exp_src = self.sink.descriptor.exp_src
        decimated_descriptor.dtype = np.complex64
        for os in self.source.output_streams:
            os.set_descriptor(decimated_descriptor)
            if os.end_connector is not None:
                os.end_connector.update_descriptors()

    async def process_data(self, data):
        # Assume for now we get a integer number of records at a time
        # TODO: handle partial records
        num_records = data.size // self.record_length
        reshaped_data = np.reshape(data, (num_records, self.record_length), order="C")

        # first stage decimating filter
        if self.filters[0] is not None:
            stacked_coeffs = np.concatenate(self.filters[0])
            # filter
            filtered = np.empty_like(reshaped_data)
            libipp.filter_records_iir(stacked_coeffs, self.filters[0][0].size-1, reshaped_data, self.record_length, num_records, filtered)

            # decimate
            if self.decim_factors[0] > 1:
                filtered = filtered[:, ::self.decim_factors[0]]

        # mix with reference
        # keep real and imaginary separate for filtering below
        filtered_r = self.reference_r * filtered
        filtered_i = self.reference_i * filtered

        # channel selection filters
        for ct in [1,2]:
            if self.filters[ct] == None:
                continue

            coeffs = self.filters[ct]
            stacked_coeffs = np.concatenate(self.filters[ct])
            out_r = np.empty_like(filtered_r)
            out_i = np.empty_like(filtered_i)
            libipp.filter_records_iir(stacked_coeffs, self.filters[ct][0].size-1, filtered_r, filtered_r.shape[-1], num_records, out_r)
            libipp.filter_records_iir(stacked_coeffs, self.filters[ct][0].size-1, filtered_i, filtered_i.shape[-1], num_records, out_i)

            # decimate
            if self.decim_factors[ct] > 1:
                filtered_r = np.copy(out_r[:, ::self.decim_factors[ct]], order="C")
                filtered_i = np.copy(out_i[:, ::self.decim_factors[ct]], order="C")
            else:
                filtered_r = out_r
                filtered_i = out_i

        filtered = filtered_r + 1j*filtered_i

        # recover gain from selecting single sideband
        filtered *= 2

        # push to ouptut connectors
        for os in self.source.output_streams:
            await os.push(filtered)

class LibChannelizerFallback(object):
    @staticmethod
    def filter_records_fir(coeffs,
                           num_taps, # ignored
                           decim_factor,
                           recs,
                           record_length, # ignored (uses shape of recs)
                           num_records, # ignored (uses shape of recs)
                           result):
        # split out a, b coefficients
        b = coeffs[:len(coeffs)//2]
        a = coeffs[len(coeffs)//2:]

        filtered_signal = scipy.signal.lfilter(b, a, recs)
        result[:] = np.copy(filtered_signal[:, ::decim_factor], order="C")

    @staticmethod
    def filter_records_iir(coeffs,
                           order, # ignored
                           recs,
                           record_length, # ignored (uses shape of recs)
                           num_records, # ignored (uses shape of recs)
                           result):
        # split out a, b coefficients
        b = coeffs[:len(coeffs)//2]
        a = coeffs[len(coeffs)//2:]

        result[:] = scipy.signal.lfilter(b, a, recs)

if load_fallback:
    libipp = LibChannelizerFallback()
<|endoftext|>


<commit_before>from brian import *
from brian.tools.datamanager import *
import sys
import neurotools as nt

def load_data(dirname):
    dataman = DataManager(dirname)
    voltage = []
    output_spikes = []
    input_rate = []
    jitter = []
    sync = []
    num_inputs = []
    weight = []
    for data in dataman.itervalues():
        if not len(data):
            continue
        voltage.append(data['mem'])
        output_spikes.append(data['output_spikes'])
        input_rate.append(data['f_in'])
        jitter.append(data['jitter'])
        sync.append(data['sync'])
        num_inputs.append(data['N_in'])
        weight.append(data['w_in'])
    flatdata = {
            'mem': voltage,
            'output_spikes': output_spikes,
            'input_rate': input_rate,
            'jitter': jitter,
            'sync': sync,
            'num_inputs': num_inputs,
            'input_weight': weight,
            }
    return flatdata

def calculate_slopes(data):
    npss = []
    mems = data['mem']
    output_spikes = data['output_spikes']
    for v, spikes in zip(mems, output_spikes):
        npss.append(nt.norm_firing_slope(v[0], spikes[0],
            th=15*mV, tau=10*ms, dt=0.1*ms, w=2*ms)[0])
    return npss

def aggregate_slopes(data):
    npss = data['slopes']
    sync = data['sync']
    jitter = data['jitter']
    sync_range = sorted(unique(sync))
    jitter_range = sorted(unique(jitter))
    img = zeros((len(sync_range), len(jitter_range)))
    for si, ji, slp in zip(sync, jitter, npss):
        x = sync_range.index(si)
        y = jitter_range.index(float(ji))
        img[x,y] += slp
    return img

if __name__=='__main__':
    data_dir = sys.argv[1]
    data = load_data(data_dir)
    npss = calculate_slopes(data)
    data['slopes'] = npss
    img = aggregate_slopes(data)
    filename = "aggregated_slopes.npz"
    print("Saving aggregated data to %s" % (filename))
    np.savez(filename, slopes=img)
    # draw it!
    imshow(img, origin='lower', aspect='auto')
    import IPython
    IPython.embed()

<commit_msg>Calculate and aggregate slope data<commit_after>from brian import *
from brian.tools.datamanager import *
import sys
import neurotools as nt

def load_data(dirname):
    dataman = DataManager(dirname)
    voltage = []
    output_spikes = []
    input_rate = []
    jitter = []
    sync = []
    num_inputs = []
    weight = []
    for data in dataman.itervalues():
        if not len(data):
            continue
        voltage.append(data['mem'])
        output_spikes.append(data['output_spikes'])
        input_rate.append(data['f_in'])
        jitter.append(data['jitter'])
        sync.append(data['sync'])
        num_inputs.append(data['N_in'])
        weight.append(data['w_in'])
    flatdata = {
            'mem': voltage,
            'output_spikes': output_spikes,
            'input_rate': input_rate,
            'jitter': jitter,
            'sync': sync,
            'num_inputs': num_inputs,
            'input_weight': weight,
            }
    return flatdata

def calculate_slopes(data):
    npss = []
    mems = data['mem']
    output_spikes = data['output_spikes']
    for v, spikes in zip(mems, output_spikes):
        npss.append(nt.norm_firing_slope(v[0], spikes[0],
            th=15*mV, tau=10*ms, dt=0.1*ms, w=2*ms)[0])
    return npss

def aggregate_slopes(data):
    npss = data['slopes']
    sync = data['sync']
    jitter = data['jitter']
    sync_range = sorted(unique(sync))
    jitter_range = sorted(unique(jitter))
    img = zeros((len(jitter_range), len(sync_range)))
    for si, ji, slp in zip(sync, jitter, npss):
        j = jitter_range.index(float(ji))
        s = sync_range.index(si)
        img[j,s] += slp
    return sync_range, jitter_range, img

if __name__=='__main__':
    data_dir = sys.argv[1]
    data = load_data(data_dir)
    npss = calculate_slopes(data)
    data['slopes'] = npss
    sync, jitter, img = aggregate_slopes(data)
    filename = "aggregated_slopes.npz"
    print("Saving aggregated data to %s" % (filename))
    np.savez(filename, slopes=img)
    # draw it!
    # extent = (min(sync), max(sync), min(jitter), max(jitter))
    # imshow(img, extent=extent, origin='lower', aspect='auto')
    # group simulations by input params & firing rate
    imgdata = []
    for slope, s, j, n, w, out in zip(
            npss, data['sync'], data['jitter'], data['num_inputs'],
            data['input_weight'], data['output_spikes']):
        fout = len(out[0])
        if 90 < fout < 110 and 0.00015*volt < w < 0.00025*volt and n == 100:
            imgdata.append((s, j, slope))

<|endoftext|>