def _assert_array_indices(self, slice_tuple): if is_integer(slice_tuple) or isinstance(slice_tuple, string_types): return ([slice_tuple], ) else: slice_list = [] for slc in slice_tuple: if is_integer(slc) or isinstance(slc, string_types): slice_list.append([slc]) else: slice_list.append(slc) return tuple(slice_list)
def __getitem__(self, keys): output = [] if isinstance(keys, slice): try: output = (np.array(list(self._dict.values()))[keys]).tolist() except: LOG.warning("keys %s not found in IndexedOrderedDict %s!\n" "Returning None!" % (str(keys), str(self._dict))) else: output = [] for key in ensure_list(keys): try: if is_integer(key): output.append(list(self._dict.values())[key]) else: # assuming key string output.append(self._dict[key]) except: LOG.warning("key %s not found in IndexedOrderedDict %s!\n" "Returning None!" % (str(key), str(self._dict))) n_outputs = len(output) if n_outputs == 0: return None elif n_outputs == 1: return output[0] else: return output
def get_subset(self, list_of_indices_or_labels, dim, **kwargs): assert dim in [0, 1, 2, 3] list_of_indices_or_labels = ensure_list(list_of_indices_or_labels) if numpy.all([ is_integer(ind_or_lbl) for ind_or_lbl in list_of_indices_or_labels ]): return self.get_subset_by_index(list_of_indices_or_labels, dim, **kwargs) else: if dim == 0: if not numpy.all([ is_numeric(ind_or_lbl) for ind_or_lbl in list_of_indices_or_labels ]): raise_value_error( "Input consists neither of integer indices nor of points in time (floats)!: %s" % list_of_indices_or_labels) time_indices = [ self._get_index_for_time(time) for time in list_of_indices_or_labels ] return self.get_subset_by_index(time_indices, 0, **kwargs) else: if not numpy.all([ isinstance(ind_or_lbl, string_types) for ind_or_lbl in list_of_indices_or_labels ]): raise_value_error( "Input consists neither of integer indices nor of label strings!: %s" % list_of_indices_or_labels) return self.get_subset_by_label(list_of_indices_or_labels, dim, **kwargs)
def get_dimension_index(self, dim_name_or_index): if is_integer(dim_name_or_index): return self._assert_index(dim_name_or_index) elif isinstance(dim_name_or_index, string_types): return self._assert_index( self.labels_ordering.index(dim_name_or_index)) else: raise ValueError( "dim_name_or_index is neither a string nor an integer!")
def _index_or_label_or_slice(self, inputs): inputs = ensure_list(inputs) if numpy.all([is_integer(inp) for inp in inputs]): return "index" elif numpy.all([isinstance(inp, string_types) for inp in inputs]): return "label" elif numpy.all([isinstance(inp, slice) for inp in inputs]): return "slice" else: raise ValueError( "input %s is not of type integer, string or slice!" % str(inputs))
def __setitem__(self, keys, values): if isinstance(keys, slice): keys = (np.array(list(self._dict.keys()))[keys]).tolist() for key, val in zip(ensure_list(keys), ensure_list(values)): try: if is_integer(key): list(self._dict.values())[key] = val else: # assuming key string self._dict[key] = val except: LOG.warning("key %s not found in IndexedOrderedDict %s!\n" "Skipping item setting with value %s!" % (str(key), str(self._dict), str(val)))
def configure(self, tvb_model): """ """ self.nest_to_tvb_params = [] self.nest_to_tvb_params_interfaces_ids = [] self.nest_to_tvb_sv_interfaces_ids = [] for interface_id, interface in enumerate(self.nest_to_tvb_interfaces): if is_integer(interface.tvb_sv_id) and interface.tvb_sv_id >= 0: self.nest_to_tvb_sv_interfaces_ids.append(interface_id) else: self.nest_to_tvb_params_interfaces_ids.append(interface_id) # Even if the NEST target in TVB is a state variable, # we are going to create a TVB parameter with the same name self.nest_to_tvb_params.append(interface.name) self.tvb_model = tvb_model
def configure(self, tvb_model): # Organize the different kinds of interfaces and set the TVB region model of the TVB Simulator self.spikeNet_to_tvb_params = [] self.spikeNet_to_tvb_params_interfaces_ids = [] self.spikeNet_to_tvb_sv_interfaces_ids = [] for interface_id, interface in enumerate( self.spikeNet_to_tvb_interfaces): if is_integer(interface.tvb_sv_id) and interface.tvb_sv_id >= 0: self.spikeNet_to_tvb_sv_interfaces_ids.append(interface_id) else: self.spikeNet_to_tvb_params_interfaces_ids.append(interface_id) # Even if the target in TVB is a state variable, # we are going to create a TVB parameter with the same name self.spikeNet_to_tvb_params.append(interface.name) self.tvb_model = tvb_model
def configure(self, tvb_model): """ """ self.w_nest_spikes_to_tvb_sv /= self.dt # Used to convert number of spikes to a spike rate self.w_nest_spikes_to_tvb_rate /= self.dt # Used to convert number of spikes to a spike rate self.nest_to_tvb_params = [] self.nest_to_tvb_params_interfaces_ids = [] self.nest_to_tvb_sv_interfaces_ids = [] for interface_id, interface in enumerate(self.nest_to_tvb_interfaces.values()): if is_integer(interface.tvb_sv_id) and interface.tvb_sv_id >= 0: self.nest_to_tvb_sv_interfaces_ids.append(interface_id) else: self.nest_to_tvb_params_interfaces_ids.append(interface_id) self.nest_to_tvb_params.append(interface.name) self.tvb_model = tvb_model self._configure_parameters_shapes()
def select_greater_values_array_inds(values, threshold=None, percentile=None, nvals=None, verbose=False): if threshold is None and percentile is not None: threshold = np.percentile(values, percentile) if threshold is not None: return np.where(values > threshold)[0] else: if is_integer(nvals): return get_greater_values_array_inds(values, nvals) if verbose: logger.warning( "Switching to curve elbow point method since threshold=" + str(threshold)) elbow_point = curve_elbow_point(values) return get_greater_values_array_inds(values, elbow_point)
def get_sensors(self, s_type=SensorTypes.TYPE_EEG.value, name_or_index=None): sensors_set = OrderedDict() if s_type not in SensorTypesNames: raise_value_error("Invalid input sensor type!: %s" % str(s_type)) else: sensors_set = self.sensors.get(s_type, None) out_sensor = None out_projection = None if isinstance(sensors_set, OrderedDict): if isinstance(name_or_index, string_types): for sensor, projection in sensors_set.items(): if isequal_string(sensor.name, name_or_index): out_sensor = sensor out_projection = projection elif is_integer(name_or_index): out_sensor = sensors_set.keys()[name_or_index] out_projection = sensors_set.values()[name_or_index] else: return sensors_set return out_sensor, out_projection