Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions bmtk/analyzer/firing_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import h5py

from bmtk.utils.sonata.config import SonataConfig


def convert_rates(rates_file):
Expand Down Expand Up @@ -87,3 +91,83 @@ def plot_rates_popnet(cell_models_file, rates_file, model_keys=None, save_as=Non

if show_plot:
plt.show()


def plot_rates(config_file=None, rates_files=None, population=None, times=None, label_column='node_id', title=None, show=True,
save_as=None, group_by=None, group_excludes=None,
nodes_file=None, node_types_file=None, plt_style=None):

def is_hdf5(file_path):
try:
h5py.File(file_path, 'r')
return True
except Exception:
return False

def is_csv(file_path):
try:
pd.read_csv(file_path)
return True
except Exception:
return False


sonata_config = SonataConfig.from_json(config_file) if config_file else None

rates_paths = set()
if isinstance(rates_files, (list, tuple, pd.Series)):
rates_paths.update([Path(rf).absolute() for rf in rates_files])
elif rates_files is not None:
rates_paths.add(Path(rates_files))

if sonata_config is not None:
for k in ['rates_file', 'rates_file_csv', 'rates_file_h5']:
if k in sonata_config.output:
rates_paths.add(Path(sonata_config.get('output', {}).get('output_dir', '.')) / Path(sonata_config.output[k]))
break

fig, ax = plt.subplots(1, 1)
for rpath in rates_paths:
if is_csv(rpath):
rates_df = pd.read_csv(rpath, sep=' ')
for (population, node_id), subdf in rates_df.groupby(['population', 'node_id']):
times = subdf['timestamps'].values
frs = subdf['firing_rates'].values

if label_column == 'population':
label = population
elif label_column == 'node_id':
label = node_id
elif label_column in subdf.columns:
label = subdf[label_column].iloc[0]
else:
label = ''

ax.plot(times, frs, label=label)

elif is_hdf5(rpath):
with h5py.File(rpath, 'r') as h5:
for population, popgrp in h5['/rates'].items():
times = popgrp['mapping/time'][()]
node_ids = popgrp['mapping/node_ids'][()]

if label_column == 'population':
labels = [population]*len(node_ids)
elif label_column == 'node_id':
labels = node_ids
elif label_column in popgrp['mapping']:
labels = popgrp['mapping'][label_column][()].astype(str)
else:
labels = ['']*len(node_ids)

for idx, node_id in enumerate(node_ids):
frs = popgrp['data'][:, idx]
ax.plot(times, frs, label=labels[idx])


ax.legend()
ax.set_ylabel('firing rates (Hz)')
ax.set_xlabel('times (ms)')

if show:
plt.show()
3 changes: 3 additions & 0 deletions bmtk/simulator/core/node_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def fetch_nodes(self):
for node in pop.filter(self._filter):
yield node

def __len__(self) -> int:
return len(self.node_ids)


class NodeSetAll(NodeSet):
def __init__(self, network):
Expand Down
2 changes: 1 addition & 1 deletion bmtk/simulator/core/sonata_reader/node_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def preprocess_node_types(network, node_population):
params_dir = network.get_component('point_neuron_models_dir')
elif model_type == 'point_soma':
params_dir = network.get_component('point_neuron_models_dir')
elif model_type == 'population':
elif model_type in ['population', 'rate_population']:
params_dir = network.get_component('population_models_dir')
elif model_type == 'lgnmodel' or model_type == 'virtual':
params_dir = network.get_component('filter_models_dir')
Expand Down
45 changes: 43 additions & 2 deletions bmtk/simulator/popnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,47 @@
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from .popnetwork import PopNetwork
from .popsimulator import PopSimulator
from six import string_types

from bmtk.simulator.core.simulation_config import SimulationConfig
from bmtk.simulator.core.io_tools import io
from .config import Config
from .ssn import inputs_generator, init_function, activation_function


class PopNetwork:
@staticmethod
def from_config(conf, **properties):
if isinstance(conf, SimulationConfig):
config = conf
else:
try:
config = SimulationConfig.load(conf)
except Exception as e:
io.log_exception('Could not convert {} (type "{}") to json.'.format(conf, type(conf)))

if config.target_simulator is None:
io.log_debug('Unspecified PopNet "target_simulator", defaulting to SSN (Options: SSN, DiPDE)')
elif config.target_simulator.lower() == 'dipde':
from .dipde import PopNetwork
return PopNetwork.from_config(config, **properties)
elif config.target_simulator.lower() == 'ssn':
from .ssn import PopNetwork
return PopNetwork.from_config(config, **properties)
else:
io.log_exception(f'Unrecognized PopNet target_simulator "{config.target_simulator}')


class PopSimulator:
@staticmethod
def from_config(configure, network, **properties):
if network.target_simulator is None:
io.log_debug('Unspecified PopNet "target_simulator", defaulting to SSN (Options: SSN, DiPDE)')
elif network.target_simulator.lower() == 'dipde':
from .dipde import PopSimulator
return PopSimulator.from_config(configure, network, **properties)
elif network.target_simulator.lower() == 'ssn':
from .ssn import PopSimulator
return PopSimulator.from_config(configure, network, **properties)
else:
io.log_exception(f'Unrecognized PopNet target_simulator "{config.target_simulator}')
2 changes: 1 addition & 1 deletion bmtk/simulator/popnet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def from_json(config_file, validate=False):


class Config(SimulationConfig):
pass
pass
25 changes: 25 additions & 0 deletions bmtk/simulator/popnet/dipde/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2017. Allen Institute. All rights reserved
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from .popnetwork import PopNetwork
from .popsimulator import PopSimulator
from ..config import Config
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import numpy as np

from bmtk.simulator.core.simulator_network import SimNetwork
from bmtk.simulator.popnet import utils as poputils
from bmtk.simulator.popnet.sonata_adaptors import PopEdgeAdaptor
from bmtk.simulator.popnet.dipde import utils as poputils
from bmtk.simulator.popnet.dipde.sonata_adaptors import PopEdgeAdaptor

from dipde.internals.internalpopulation import InternalPopulation
from dipde.internals.externalpopulation import ExternalPopulation
Expand Down Expand Up @@ -162,6 +162,10 @@ def __init__(self, group_by='node_type_id', **properties):
self._external_connections = {}
self._all_connections = []

@property
def target_simulator(self):
return 'DiPDE'

@property
def populations(self):
return self._all_populations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@
import dipde

from bmtk.simulator.core.simulator import Simulator
from . import config as cfg
# from bmtk. import config as cfg
from bmtk.simulator.core.simulation_config import SimulationConfig
from bmtk.simulator.core.io_tools import io
from . import utils as poputils
import bmtk.simulator.utils.simulation_inputs as inputs
from bmtk.utils.reports.spike_trains import SpikeTrains
from bmtk.utils.io import firing_rates


def from_json(config_file, validate=False):
conf_dict = SimulationConfig.from_json(config_file)
conf_dict.io = io
return conf_dict


class PopSimulator(Simulator):
def __init__(self, graph, dt=0.0001, tstop=0.0, overwrite=True):
self._graph = graph
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions bmtk/simulator/popnet/ssn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2025. Allen Institute. All rights reserved
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from .popnetwork import PopNetwork
from .popsimulator import PopSimulator
from .pyfunction_cache import inputs_generator, init_function, activation_function, add_activation_function

from bmtk.simulator.core.simulation_config import SimulationConfig as Config
# from .config import Config
1 change: 1 addition & 0 deletions bmtk/simulator/popnet/ssn/default_setters/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import activation_functions
22 changes: 22 additions & 0 deletions bmtk/simulator/popnet/ssn/default_setters/activation_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from bmtk.simulator.popnet.ssn.pyfunction_cache import add_activation_function

try:
from numba import njit

except ImportError as ie:
from bmtk.simulator.popnet.ssn.utils import empty_decorator
njit = empty_decorator


@njit
def relu2(array):
# if the element is negative, set it to zero using loop
# destructive method (alters the original array), but faster than the above one.
for i in range(len(array)):
if array[i] < 0:
array[i] = 0
return array


add_activation_function(relu2, name='default', overwrite=False)
add_activation_function(relu2, overwrite=False)
3 changes: 3 additions & 0 deletions bmtk/simulator/popnet/ssn/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .initial_states import InitStatesMod
from .rates_recorder import RatesRecorderMod
from .external_inputs import ExternalRatesMod
87 changes: 87 additions & 0 deletions bmtk/simulator/popnet/ssn/modules/external_inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import numpy as np
import pandas as pd
import h5py

from ..pyfunction_cache import py_modules
from .sim_module import SimulatorMod


class ExternalRatesMod(SimulatorMod):
def __init__(self, name, input_type, module, **kwargs):
self._name = name
self._input_type = input_type
self._module = module
self._params = kwargs

def initialize(self, sim):
if self._module == 'npy':
npy_path = self._params['file']
inputs_arr = np.load(npy_path)

if sim.tstop is None:
# TODO: WARNING THAT TSTOP IS BEING SET BY INPUT
sim.tstop = len(inputs_arr)*sim.dt

if sim.nsteps < len(inputs_arr):
# TODO: WARN THAT input is being cut
inputs_arr = inputs_arr[:sim.nsteps]

elif sim.nsteps > len(inputs_arr):
# GIVE WARNING
inputs_arr = np.append(inputs_arr, np.zeros(sim.nsteps - len(inputs_arr)))

node_set = sim.network.get_node_set(self._params.get('node_set', 'all'))

for node in node_set.fetch_nodes():
ssn_node = sim.network.get_node(node.population_name, node.node_id)
ssn_node.external_inputs = inputs_arr.flatten()


elif self._module == 'function':
fnc_name = self._params['inputs_generator']
generator_fnc = py_modules.user_function(fnc_name)

node_set = sim.network.get_node_set(self._params.get('node_set', 'all'))
for node in node_set.fetch_nodes():
ssn_node = sim.network.get_node(node.population_name, node.node_id)
external_inputs = generator_fnc(ssn_node, sim)
ssn_node.external_inputs = external_inputs

if sim.tstop is None:
# TODO: WARNING THAT TSTOP IS BEING SET BY INPUT
sim.tstop = len(external_inputs)*sim.dt

elif self._module == 'csv':
# TODO: Check Timestamps match, line up if needed
rates_df = pd.read_csv(self._params['file'], sep=self._params.get('sep', ' '))
node_set = sim.network.get_node_set(self._params.get('node_set', 'all'))
for node in node_set.fetch_nodes():
ssn_node = sim.network.get_node(node.population_name, node.node_id)

# TODO: Check that node exists in file
inputs = rates_df[(rates_df['node_id'] == ssn_node.node_id) & (rates_df['population'] == ssn_node.population)]['firing_rates']
if len(inputs) > 0:
ssn_node.external_inputs = inputs

if sim.tstop is None:
# TODO: WARNING THAT TSTOP IS BEING SET BY INPUT
sim.tstop = len(inputs)*sim.dt

elif self._module in ['h5', 'sonata']:
with h5py.File(self._params['file'], 'r') as h5:
ratesgrp = h5['/rates']
node_set = sim.network.get_node_set(self._params.get('node_set', 'all'))
for node in node_set.fetch_nodes():
ssn_node = sim.network.get_node(node.population_name, node.node_id)

node_id_map = ratesgrp[f'{ssn_node.population}/mapping/node_ids'][()]
idx = np.argwhere(node_id_map == ssn_node.node_id)[0][0]
inputs = ratesgrp[f'{ssn_node.population}/data'][:, idx]

ssn_node.external_inputs = inputs

if sim.tstop is None:
# TODO: WARNING THAT TSTOP IS BEING SET BY INPUT
sim.tstop = len(inputs)*sim.dt
else:
raise ValueError('Unknown module')
Loading
Loading