Exemple #1
0
def read_spikes_from_file(file_path,
                          min_atom=0,
                          max_atom=float('inf'),
                          min_time=0,
                          max_time=float('inf'),
                          split_value="\t"):
    """ Read spikes from a file formatted as:\
        <time>\t<neuron ID>

    :param file_path: absolute path to a file containing spike values
    :type file_path: str
    :param min_atom: min neuron ID to which neurons to read in
    :type min_atom: int
    :param max_atom: max neuron ID to which neurons to read in
    :type max_atom: int
    :param min_time: min time slot to read neurons values of.
    :type min_time: int
    :param max_time: max time slot to read neurons values of.
    :type max_time: int
    :param split_value: the pattern to split by
    :type split_value: str
    :return:\
        a numpy array with max_atom elements each of which is a list of\
        spike times.
    :rtype: numpy.array(int, int)
    """
    # pylint: disable=too-many-arguments

    # For backward compatibility as previous version tested for None rather
    # than having default values
    if min_atom is None:
        min_atom = 0
    if max_atom is None:
        max_atom = float('inf')
    if min_time is None:
        min_time = 0
    if max_time is None:
        max_time = float('inf')

    data = []
    with open(file_path, 'r') as fsource:
        read_data = fsource.readlines()

    evaluator = SafeEval()
    for line in read_data:
        if line.startswith('#'):
            continue
        values = line.split(split_value)
        time = float(evaluator.eval(values[0]))
        neuron_id = float(evaluator.eval(values[1]))
        if (min_atom <= neuron_id < max_atom and min_time <= time < max_time):
            data.append([neuron_id, time])
    data.sort()
    return numpy.array(data)
def read_spikes_from_file(file_path, min_atom=0, max_atom=float('inf'),
                          min_time=0, max_time=float('inf'), split_value="\t"):
    """ Read spikes from a file formatted as:\
        <time>\t<neuron ID>

    :param file_path: absolute path to a file containing spike values
    :type file_path: str
    :param min_atom: min neuron ID to which neurons to read in
    :type min_atom: int
    :param max_atom: max neuron ID to which neurons to read in
    :type max_atom: int
    :param min_time: min time slot to read neurons values of.
    :type min_time: int
    :param max_time: max time slot to read neurons values of.
    :type max_time: int
    :param split_value: the pattern to split by
    :type split_value: str
    :return:\
        a numpy array with max_atom elements each of which is a list of\
        spike times.
    :rtype: numpy.array(int, int)
    """
    # pylint: disable=too-many-arguments

    # For backward compatibility as previous version tested for None rather
    # than having default values
    if min_atom is None:
        min_atom = 0
    if max_atom is None:
        max_atom = float('inf')
    if min_time is None:
        min_time = 0
    if max_time is None:
        max_time = float('inf')

    data = []
    with open(file_path, 'r') as fsource:
        read_data = fsource.readlines()

    evaluator = SafeEval()
    for line in read_data:
        if line.startswith('#'):
            continue
        values = line.split(split_value)
        time = float(evaluator.eval(values[0]))
        neuron_id = float(evaluator.eval(values[1]))
        if (min_atom <= neuron_id < max_atom and
                min_time <= time < max_time):
            data.append([neuron_id, time])
    data.sort()
    return numpy.array(data)
def test_environment_eval():
    evaluator = SafeEval(Abc)
    assert evaluator.eval("Abc(1)") == 2
    with pytest.raises(NameError):
        evaluator.eval("Def(1)")
    with pytest.raises(TypeError):
        evaluator.eval("Abc(1,2)")
Exemple #4
0
def read_in_data_from_file(file_path,
                           min_atom,
                           max_atom,
                           min_time,
                           max_time,
                           extra=False):
    """ Read in a file of data values where the values are in a format of:
        <time>\t<atom ID>\t<data value>

    :param str file_path: absolute path to a file containing the data
    :param int min_atom: min neuron ID to which neurons to read in
    :param int max_atom: max neuron ID to which neurons to read in
    :param extra:
    :param min_time: min time slot to read neurons values of.
    :type min_time: float or int
    :param max_time: max time slot to read neurons values of.
    :type max_time: float or int
    :return: a numpy array of (time stamp, atom ID, data value)
    :rtype: ~numpy.ndarray(tuple(float, int, float))
    """
    times = list()
    atom_ids = list()
    data_items = list()
    evaluator = SafeEval()
    with open(file_path, 'r') as f:
        for line in f.readlines():
            if line.startswith('#'):
                continue
            if extra:
                time, neuron_id, data_value, extra = line.split("\t")
            else:
                time, neuron_id, data_value = line.split("\t")
            time = float(evaluator.eval(time))
            neuron_id = int(evaluator.eval(neuron_id))
            data_value = float(evaluator.eval(data_value))
            if (min_atom <= neuron_id < max_atom
                    and min_time <= time < max_time):
                times.append(time)
                atom_ids.append(neuron_id)
                data_items.append(data_value)
            else:
                print("failed to enter {}:{}".format(neuron_id, time))

    result = numpy.dstack((atom_ids, times, data_items))[0]
    return result[numpy.lexsort((times, atom_ids))]
def test_multi_environment():
    s = Support()
    evaluator = SafeEval(Abc, s.Def)
    assert evaluator.eval("x+y*z", x=1, y=2, z=3) == 7
    assert evaluator.eval("Def(Abc(x))", x=3) == 8
    with pytest.raises(NameError):
        assert evaluator.eval("Ghi(Def(Abc(y)))", y=3) == 1
    assert evaluator.eval("Ghi(Def(Abc(y)))", y=3, Ghi=s.Ghi) == 4.0
    assert evaluator.eval("Ghi(Def(Abc(y)))", y=3, Ghi=s.Def) == 16
    # Not sure if the next test should pass; Python, huh...
    assert evaluator.eval("Ghi.__name__", y=3, Ghi=s.Def) == "Def"
def read_in_data_from_file(
        file_path, min_atom, max_atom, min_time, max_time, extra=False):
    """ Read in a file of data values where the values are in a format of:
        <time>\t<atom ID>\t<data value>

    :param file_path: absolute path to a file containing the data
    :param min_atom: min neuron ID to which neurons to read in
    :param max_atom: max neuron ID to which neurons to read in
    :param min_time: min time slot to read neurons values of.
    :param max_time: max time slot to read neurons values of.
    :return: a numpy array of (time stamp, atom ID, data value)
    """
    times = list()
    atom_ids = list()
    data_items = list()
    evaluator = SafeEval()
    with open(file_path, 'r') as f:
        for line in f.readlines():
            if line.startswith('#'):
                continue
            if extra:
                time, neuron_id, data_value, extra = line.split("\t")
            else:
                time, neuron_id, data_value = line.split("\t")
            time = float(evaluator.eval(time))
            neuron_id = int(evaluator.eval(neuron_id))
            data_value = float(evaluator.eval(data_value))
            if (min_atom <= neuron_id < max_atom and
                    min_time <= time < max_time):
                times.append(time)
                atom_ids.append(neuron_id)
                data_items.append(data_value)
            else:
                print("failed to enter {}:{}".format(neuron_id, time))

    result = numpy.dstack((atom_ids, times, data_items))[0]
    return result[numpy.lexsort((times, atom_ids))]
Exemple #7
0
# support for arbitrary expression for the distance dependence
_d_expr_context = SafeEval(math,
                           numpy,
                           arccos,
                           arcsin,
                           arctan,
                           arctan2,
                           ceil,
                           cos,
                           cosh,
                           exp,
                           fabs,
                           floor,
                           fmod,
                           hypot,
                           ldexp,
                           log,
                           log10,
                           modf,
                           power,
                           sin,
                           sinh,
                           sqrt,
                           tan,
                           tanh,
                           maximum,
                           minimum,
                           e=e,
                           pi=pi)

# global objects
logger = FormatAdapter(logging.getLogger(__name__))
_expr_context = SafeEval(math,
                         numpy,
                         numpy.arccos,
                         numpy.arcsin,
                         numpy.arctan,
                         numpy.arctan2,
                         numpy.ceil,
                         numpy.cos,
                         numpy.cosh,
                         numpy.exp,
                         numpy.fabs,
                         numpy.floor,
                         numpy.fmod,
                         numpy.hypot,
                         numpy.ldexp,
                         numpy.log,
                         numpy.log10,
                         numpy.modf,
                         numpy.power,
                         numpy.sin,
                         numpy.sinh,
                         numpy.sqrt,
                         numpy.tan,
                         numpy.tanh,
                         numpy.maximum,
                         numpy.minimum,
                         e=numpy.e,
                         pi=numpy.pi)

def test_defined_names():
    evaluator = SafeEval(math, x=123.25)
    assert evaluator.eval("math.floor(x+d)", d=0.875) == 124.0
def test_packages():
    evaluator = SafeEval(math)
    assert evaluator.eval("math.floor(d)", d=2.5) == 2.0
def test_state_sensitive():
    s = Support()
    evaluator = SafeEval(s.Jkl)
    assert evaluator.eval("Jkl(Jkl(Jkl(x)))", x=2.5) == 16.5
def test_simple_eval():
    evaluator = SafeEval()
    assert evaluator.eval("1+2*3") == 7
    assert evaluator.eval("x+y*z", x=1, y=2, z=3) == 7