def set_attributes(instance, **kwargs): for attr, value in kwargs.items(): try: setattr(instance, attr, value) except: warning("Failed to set attribute %s to %s!" % (attr, instance.__class__.__name__)) return instance
def from_file(cls, path=os.path.join(CONFIGURED.input.HEAD, "connectivity.zip"), **kwargs): filename = os.path.basename(path) dirname = os.path.dirname(path) if "head" in filename.lower(): import h5py head = Head() head.path = path h5file = h5py.File(path, 'r', libver='latest') for field in []: try: setattr(head, field, h5file['/' + field][()]) except: warning("Failed to read Head field %s from file %s!" % (field, path)) for attr in ["title"]: try: setattr( head, attr, h5file.attrs.get(attr, h5file.attrs.get("TVB_%s" % attr))) except: warning("Failed to read Head attribute %s from file %s!" % (attr, path)) head.path = dirname else: kwargs["connectivity"] = filename head = None return cls.from_folder(dirname, head, **kwargs)
def print_not_equal_message(attr, field1, field2, logger): # logger.error("\n\nValueError: Original and read object field "+ attr + " not equal!") # raise_value_error("\n\nOriginal and read object field " + attr + " not equal!") warning( "Original and read object field " + attr + " not equal!" + "\nOriginal field:\n" + str(field1) + "\nRead object field:\n" + str(field2), logger)
def __init__(self, **kwargs): self.file_path = kwargs.pop("file_path", "") self._tvb = kwargs.pop("tvb_surface", TVBCorticalSurface()) self.vox2ras = kwargs.pop("vox2ras", np.array([])) for attr, value in kwargs.items(): try: if value.any(): setattr(self._tvb, attr, value) except: warning("Failed to set attribute %s to TVB surface!" % attr)
def from_instance(instance, **kwargs): result = deepcopy(instance) for attr, value in kwargs.items(): try: if len(value): setattr(result, attr, value) except: warning("Failed to set attribute %s to %s!" % (attr, instance.__class__.__name__)) result.configure() return result
def from_tvb_instance(instance, **kwargs): result = Connectivity() if isinstance(instance, TVBConnectivity): for attr, value in instance.__dict__: try: if len(value): setattr(result, attr, value) except: warning("Failed to set attribute %s of TVB %s!" % (attr, instance.__class__.__name__)) return Connectivity.from_instance(result, **kwargs)
def __init__(self, **kwargs): self.file_path = kwargs.pop("file_path", "") self._tvb = kwargs.pop("tvb_connectivity", TVBConnectivity()) self.normalized_weights = kwargs.pop("normalized_weights", np.array([])) for attr, value in kwargs.items(): try: if len(value): setattr(self._tvb, attr, value) except: warning("Failed to set attribute %s to TVB connectivity!" % attr)
def set_sensors(self, input_sensors, s_type=SensorTypes.TYPE_EEG.value, reset=False): if not isinstance(input_sensors, (Sensors, dict, list, tuple)): return raise_value_error("Invalid input sensors instance''!: %s" % str(input_sensors)) if s_type not in SensorTypesNames: raise_value_error("Invalid input sensor type!: %s" % str(s_type)) sensors_set = self.get_sensors(s_type)[0] if reset is True: sensors_set = OrderedDict() if isinstance(input_sensors, dict): input_projections = input_sensors.values() input_sensors = input_sensors.keys() else: if isinstance(input_sensors, Sensors): input_sensors = [input_sensors] else: input_sensors = list(input_sensors) input_projections = [None] * len(input_sensors) for sensor, projection in zip(input_sensors, input_projections): if not isinstance(sensor, Sensors): raise_value_error( "Input sensors:\n%s" "\nis not a valid Sensors object of type %s!" % (str(sensor), s_type)) if sensor.sensors_type != s_type: raise_value_error("Sensors %s \nare not of type %s!" % (str(sensor), s_type)) if not isinstance(projection, ProjectionMatrix): warning("projection is not set for sensor with name:\n%s!" % sensor.name) sensors_set.update({sensor: None}) else: if projection.projection_type != SensorTypesToProjectionDict[ s_type]: raise_value_error( "Disaggreement between sensors'type %s and projection's type %s!" % (sensor.sensors_type, projection.projection_type)) good_sensor_shape = (sensor.number_of_sensors, self.number_of_regions) if projection.projection_data.shape != good_sensor_shape: warning( "projections' shape %s of sensor %s " "is not equal to (number_of_sensors, number_of_regions)=%s!" % (str(projection.projection_data.shape), sensor.name, str(good_sensor_shape))) sensors_set.update({sensor: projection}) self.sensors[s_type] = sensors_set
def group_sensors_to_electrodes(self, labels=None): if self.sensors_type == SensorTypes.TYPE_SEEG: if labels is None: labels = self.labels sensor_names = np.array(split_string_text_numbers(labels)) elec_labels = np.unique(sensor_names[:, 0]) elec_inds = [] for chlbl in elec_labels: elec_inds.append(np.where(sensor_names[:, 0] == chlbl)[0]) return np.array(elec_labels), np.array(elec_inds) else: warning("No multisensor electrodes for %s sensors!" % self.sensors_type) return self.elec_labels, self.elec_inds
def configure(self): if isinstance(self.connectivity, TVBConnectivity): self.connectivity.configure() if isinstance(self.connectivity, TVBLocalConnectivity): self.local_connectivity.configure() if isinstance(self.cortical_surface, TVBSurface): self.cortical_surface.configure() if not isinstance(self.cortical_surface, TVBCorticalSurface): warning( "cortical_surface is not an instance of TVB CorticalSurface!" ) if isinstance(self.cortical_region_mapping, TVBRegionMapping): self.cortical_region_mapping.connectivity = self.connectivity self.cortical_region_mapping.surface = self.cortical_surface self.cortical_region_mapping.configure() if isinstance(self.subcortical_surface, TVBSurface): self.subcortical_surface.configure() if not isinstance(self.subcortical_surface, CorticalSurface): warning( "cortical_surface is not an instance of SubcorticalSurface!" ) if isinstance(self.subcortical_region_mapping, TVBRegionMapping): self.subcortical_region_mapping.connectivity = self.connectivity self.subcortical_region_mapping.surface = self.subcortical_surface self.subcortical_region_mapping.configure() structural = None for s_type in ["b0", "flair", "t2", "t1"]: instance = getattr(self, s_type) if isinstance(instance, TVBStructuralMRI): instance.configure() structural = instance if structural is not None: if isinstance(self.region_volume_mapping, TVBRegionVolumeMapping): self.region_volume_mapping.connectivity = self.connectivity self.region_volume_mapping.volume = structural.volume self.region_volume_mapping.configure() for s_type, p_type, s_datatype, p_datatype\ in zip(["eeg", "seeg", "meg"], [EEG_POLYMORPHIC_IDENTITY, SEEG_POLYMORPHIC_IDENTITY, MEG_POLYMORPHIC_IDENTITY], [TVBSensorsEEG, TVBSensorsInternal, TVBSensorsMEG], [TVBProjectionSurfaceEEG, TVBProjectionSurfaceSEEG, TVBProjectionSurfaceMEG]): sensor_name = "%s_sensors" % s_type sensors = getattr(self, sensor_name) if isinstance(sensors, TVBSensors): sensors.configure() if not isinstance(sensors, s_datatype): warning("%s is not an instance of TVB %s!" % (sensor_name, s_datatype.__name__)) projection_name = "%s_projection" % s_type projection = getattr(self, projection_name) if isinstance(projection, TVBProjectionMatrix): projection.sensors = sensors if not isinstance(projection, p_datatype): warning("%s is not an instance of TVB %s!" % (projection_name, p_datatype.__name__)) if isinstance(self.surface, Surface): projection.sources = self.surface projection.projection_type = p_type projection.configure()
def _determine_datasets_and_attributes(self, object, datasets_size=None): datasets_dict = {} metadata_dict = {} groups_keys = [] try: if isinstance(object, dict): dict_object = object elif hasattr(object, "to_dict"): dict_object = object.to_dict() else: dict_object = vars(object) for key, value in dict_object.items(): if isinstance(value, numpy.ndarray): # if value.size == 1: # metadata_dict.update({key: value}) # else: datasets_dict.update({key: value}) # if datasets_size is not None and value.size == datasets_size: # datasets_dict.update({key: value}) # else: # if datasets_size is None and value.size > 0: # datasets_dict.update({key: value}) # else: # metadata_dict.update({key: value}) # TODO: check how this works! Be carefull not to include lists and tuples if possible in tvb classes! elif isinstance(value, (list, tuple)): warning( "Writing %s %s to h5 file as a numpy array dataset !" % (value.__class__, key), self.logger) datasets_dict.update({key: numpy.array(value)}) else: if is_numeric(value) or isinstance(value, str): metadata_dict.update({key: value}) elif callable(value): metadata_dict.update({key: inspect.getsource(value)}) elif value is None: continue else: groups_keys.append(key) except: msg = "Failed to decompose group object: " + str(object) + "!" try: self.logger.info(str(object.__dict__)) except: msg += "\n It has no __dict__ attribute!" warning(msg, self.logger) return datasets_dict, metadata_dict, groups_keys
def from_instance(instance, **kwargs): result = deepcopy(instance) for attr, value in kwargs.items(): try: if len(value): setattr(result, attr, value) except: warning("Failed to set attribute %s to %s!" % (attr, instance.__class__.__name__)) if len(result.normalized_weights) == 0: result.normalized_weights = normalize_weights(result.weights, remove_diagonal=True, ceil=1.0) result.configure() return result
def _write_dicts_at_location(self, datasets_dict, metadata_dict, location): for key, value in datasets_dict.items(): try: location.create_dataset(key, data=value) except: warning("Failed to write to %s dataset %s %s:\n%s !" % (str(location), value.__class__, key, str(value)), self.logger) for key, value in metadata_dict.items(): try: location.attrs.create(key, value) except: warning("Failed to write to %s attribute %s %s:\n%s !" % (str(location), value.__class__, key, str(value)), self.logger) return location
def __init__(self, tvb_sensors=TVBSensors(), remove_leading_zeros_from_labels=False, **kwargs): self.name = kwargs.pop("name", "") self.file_path = kwargs.pop("file_path", "") self._tvb = tvb_sensors for attr, value in kwargs.items(): try: if len(value): setattr(self._tvb, attr, value) except: warning("Failed to set attribute %s to TVB sensors!" % attr) self._tvb.number_of_sensors = self.locations.shape[0] if len(self.name) == 0: self.name = self._tvb.sensors_type if len(self._tvb.labels) > 0: if remove_leading_zeros_from_labels: self.remove_leading_zeros_from_labels()
def __init__(self, **kwargs): super(Connectivity, self).__init__(**kwargs) self.file_path = kwargs.pop("file_path", "") self.normalized_weights = kwargs.pop("normalized_weights", np.array([])) for attr, value in kwargs.items(): try: if len(value): setattr(self, attr, value) except: warning("Failed to set attribute %s to %s!" % (attr, self.__class__.__name__)) if len(self.normalized_weights) == 0: self.normalized_weights = normalize_weights(self.weights, remove_diagonal=True, ceil=1.0) self.configure()
def _get_filepath(self, filename, patterns, used_filepaths): # Search for default names if there is no filename provided if filename is None: for pattern in patterns: filepaths = insensitive_glob( os.path.join(self.path, "*%s*" % pattern)) if len(filepaths) > 0: for filepath in filepaths: if filepath not in used_filepaths and os.path.isfile( filepath): return filepath return None else: try: return insensitive_glob( os.path.join(self.path, "*%s*" % filename))[0] except: warning("No *%s* file found in %s path!" % (filename, self.path))
def _plot_ts_spectral_analysis_raster(self, data, time=None, var_label="", time_unit="ms", freq=None, spectral_options={}, special_idx=[], labels=[], title='Spectral Analysis', figure_name=None, figsize=None): if not isinstance(figsize, (list, tuple)): figsize = self.config.figures.VERY_LARGE_SIZE if len(data.shape) == 1: n_times = data.shape[0] nS = 1 else: n_times, nS = data.shape[:2] time = assert_time(time, n_times, time_unit, self.logger) if not isinstance(time_unit, string_types): time_unit = list(time_unit)[0] time_unit = ensure_string(time_unit) if time_unit in ("ms", "msec"): fs = 1000.0 else: fs = 1.0 fs = fs / numpy.mean(numpy.diff(time)) n_special_idx = len(special_idx) if n_special_idx > 0: data = data[:, special_idx] nS = data.shape[1] if len(labels) > n_special_idx: labels = numpy.array([ str(ilbl) + ". " + str(labels[ilbl]) for ilbl in special_idx ]) elif len(labels) == n_special_idx: labels = numpy.array([ str(ilbl) + ". " + str(label) for ilbl, label in zip(special_idx, labels) ]) else: labels = numpy.array([str(ilbl) for ilbl in special_idx]) else: if len(labels) != nS: labels = numpy.array([str(ilbl) for ilbl in range(nS)]) if nS > 20: warning( "It is not possible to plot spectral analysis plots for more than 20 signals!" ) return log_norm = spectral_options.get("log_norm", False) mode = spectral_options.get("mode", "psd") psd_label = mode if log_norm: psd_label = "log" + psd_label stf, time, freq, psd = time_spectral_analysis( data, fs, freq=freq, mode=mode, nfft=spectral_options.get("nfft"), window=spectral_options.get("window", 'hanning'), nperseg=spectral_options.get("nperseg", int(numpy.round(fs / 4))), detrend=spectral_options.get("detrend", 'constant'), noverlap=spectral_options.get("noverlap"), f_low=spectral_options.get("f_low", 10.0), log_scale=spectral_options.get("log_scale", False)) min_val = numpy.min(stf.flatten()) max_val = numpy.max(stf.flatten()) if nS > 2: figsize = self.config.figures.VERY_LARGE_SIZE if len(var_label): title += ": " % var_label fig = pyplot.figure(title, figsize=figsize) fig.suptitle(title) gs = gridspec.GridSpec(nS, 23) ax = numpy.empty((nS, 2), dtype="O") img = numpy.empty((nS, ), dtype="O") line = numpy.empty((nS, ), dtype="O") for iS in range(nS, -1, -1): if iS < nS - 1: ax[iS, 0] = pyplot.subplot(gs[iS, :20], sharex=ax[iS, 0]) ax[iS, 1] = pyplot.subplot(gs[iS, 20:22], sharex=ax[iS, 1], sharey=ax[iS, 0]) else: # TODO: find and correct bug here ax[iS, 0] = pyplot.subplot(gs[iS, :20]) ax[iS, 1] = pyplot.subplot(gs[iS, 20:22], sharey=ax[iS, 0]) img[iS] = ax[iS, 0].imshow(numpy.squeeze(stf[:, :, iS]).T, cmap=pyplot.set_cmap('jet'), interpolation='none', norm=Normalize(vmin=min_val, vmax=max_val), aspect='auto', origin='lower', extent=(time.min(), time.max(), freq.min(), freq.max())) # img[iS].clim(min_val, max_val) ax[iS, 0].set_title(labels[iS]) ax[iS, 0].set_ylabel("Frequency (Hz)") line[iS] = ax[iS, 1].plot(psd[:, iS], freq, 'k', label=labels[iS]) pyplot.setp(ax[iS, 1].get_yticklabels(), visible=False) # ax[iS, 1].yaxis.tick_right() # ax[iS, 1].yaxis.set_ticks_position('both') if iS == (nS - 1): ax[iS, 0].set_xlabel("Time (" + time_unit + ")") ax[iS, 1].set_xlabel(psd_label) else: pyplot.setp(ax[iS, 0].get_xticklabels(), visible=False) pyplot.setp(ax[iS, 1].get_xticklabels(), visible=False) ax[iS, 0].autoscale(tight=True) ax[iS, 1].autoscale(tight=True) # make a color bar cax = pyplot.subplot(gs[:, 22]) pyplot.colorbar(img[0], cax=pyplot.subplot( gs[:, 22])) # fraction=0.046, pad=0.04) #fraction=0.15, shrink=1.0 cax.set_title(psd_label) self._save_figure(pyplot.gcf(), figure_name) self._check_show() return fig, ax, img, line, time, freq, stf, psd
def spikes_events_to_time_index(spike_time, time): if spike_time < time[0] or spike_time > time[-1]: warning("Spike time is outside the input time vector!") return np.argmin(np.abs(time - spike_time))
def parse_csv(fname, merge=True): if '*' in fname: import glob return parse_csv(glob.glob(fname), merge=merge) if isinstance(fname, (list, tuple)): csv = [] for csv_dict in [parse_csv(_) for _ in fname]: if len(csv_dict) > 0: csv.append(csv_dict) if merge: csv = merge_csv_data(*csv) return csv try: lines = [] with open(fname, 'r') as fd: for line in fd.readlines(): if not line.startswith('#'): lines.append(line.strip().split(',')) names = [field.split('.') for field in lines[0]] data = [] for id_line, line in enumerate(lines[1:]): append_data = True for iline in range(len(line)): try: line[iline] = float(line[iline]) except: logger = initialize_logger(__name__) logger.warn("Failed to convert string " + line[iline] + " to float!" + "\nSkipping line " + str(id_line) + ": " + str(line) + "!") append_data = False break if append_data: data.append(line) data = np.array(data) namemap = {} maxdims = {} for i, name in enumerate(names): if name[0] not in namemap: namemap[name[0]] = [] namemap[name[0]].append(i) if len(name) > 1: maxdims[name[0]] = name[1:] for name in maxdims.keys(): dims = [] for dim in maxdims[name]: dims.append(int(dim)) maxdims[name] = tuple(reversed(dims)) # data in linear order per Stan, e.g. mat is col maj # TODO array is row maj, how to distinguish matrix vs array[,]? data_ = {} for name, idx in namemap.items(): new_shape = (-1, ) + maxdims.get(name, ()) data_[name] = data[:, idx].reshape(new_shape) return data_ except: warning("Failed to read %s!" % fname) return {}
def __init__(self, input=numpy.array([[], []]), **kwargs): if isinstance(input, (Timeseries, TimeSeries)): if isinstance(input, Timeseries): self._tvb = deepcopy(input._tvb) self.ts_type = str(input.ts_type) elif isinstance(input, TimeSeries): self._tvb = deepcopy(input) if isinstance(input, TimeSeriesRegion): self.ts_type = "Region" if isinstance(input, TimeSeriesSEEG): self.ts_type = "SEEG" elif isinstance(input, TimeSeriesEEG): self.ts_type = "EEG" elif isinstance(input, TimeSeriesMEG): self.ts_type = "MEG" elif isinstance(input, TimeSeriesEEG): self.ts_type = "EEG" elif isinstance(input, TimeSeriesVolume): self.ts_type = "Volume" elif isinstance(input, TimeSeriesSurface): self.ts_type = "Surface" else: self.ts_type = "" warning( "Input TimeSeries %s is not one of the known TVB TimeSeries classes!" % str(input)) for attr, value in kwargs.items(): try: setattr(self, attr, value) except: setattr(self._tvb, attr, value) elif isinstance(input, numpy.ndarray): input = prepare_4D(input, self.logger) time = kwargs.pop("time", None) if time is not None: start_time = float( kwargs.pop("start_time", kwargs.pop("start_time", time[0]))) sample_period = float( kwargs.pop( "sample_period", kwargs.pop("sample_period", numpy.mean(numpy.diff(time))))) kwargs.update({ "start_time": start_time, "sample_period": sample_period }) # Initialize self.ts_type = kwargs.pop("ts_type", "Region") labels_ordering = kwargs.get("labels_ordering", None) # Get input sensors if any input_sensors = None if isinstance(kwargs.get("sensors", None), (TVBSensors, Sensors)): if isinstance(kwargs["sensors"], Sensors): input_sensors = kwargs["sensors"]._tvb self.ts_type = "%s sensor" % input_sensors.sensors_type kwargs.update({"sensors": input_sensors}) else: input_sensors = kwargs["sensors"] # Create Timeseries if isinstance(input_sensors, TVBSensors) or \ self.ts_type in ["SEEG sensor", "Internal sensor", "EEG sensor", "MEG sensor"]: # ...for Sensor Timeseries if labels_ordering is None: labels_ordering = LABELS_ORDERING labels_ordering[2] = "%s sensor" % self.ts_type kwargs.update({"labels_ordering": labels_ordering}) if isinstance(input_sensors, TVBSensorsInternal) or isequal_string(self.ts_type, "Internal sensor")\ or isequal_string(self.ts_type, "SEEG sensor"): self._tvb = TimeSeriesSEEG(data=input, **kwargs) self.ts_type = "SEEG sensor" elif isinstance(input_sensors, TVBSensorsEEG) or isequal_string( self.ts_type, "EEG sensor"): self._tvb = TimeSeriesEEG(data=input, **kwargs) self.ts_type = "EEG sensor" elif isinstance(input_sensors, TVBSensorsMEG) or isequal_string( self.ts_type, "MEG sensor"): self._tvb = TimeSeriesMEG(data=input, **kwargs) self.ts_type = "MEG sensor" else: raise_value_error( "Not recognizing sensors of type %s:\n%s" % (self.ts_type, str(input_sensors))) else: input_surface = kwargs.pop("surface", None) if isinstance( input_surface, (Surface, TVBSurface)) or self.ts_type == "Surface": self.ts_type = "Surface" if isinstance(input_surface, Surface): kwargs.update({"surface": input_surface._tvb}) else: kwargs.update({"surface": input_surface}) if labels_ordering is None: labels_ordering = LABELS_ORDERING labels_ordering[2] = "Vertex" kwargs.update({"labels_ordering": labels_ordering}) self._tvb = TimeSeriesSurface(data=input, **kwargs) elif isequal_string(self.ts_type, "Region"): if labels_ordering is None: labels_ordering = LABELS_ORDERING labels_ordering[2] = "Region" kwargs.update({"labels_ordering": labels_ordering}) self._tvb = TimeSeriesRegion(data=input, **kwargs) # , **kwargs elif isequal_string(self.ts_type, "Volume"): if labels_ordering is None: labels_ordering = ["Time", "X", "Y", "Z"] kwargs.update({"labels_ordering": labels_ordering}) self._tvb = TimeSeriesVolume(data=input, **kwargs) else: self._tvb = TimeSeries(data=input, **kwargs) if not numpy.all([ dim_label in self._tvb.labels_dimensions.keys() for dim_label in self._tvb.labels_ordering ]): warning( "Lack of correspondance between timeseries labels_ordering %s\n" "and labels_dimensions!: %s" % (self._tvb.labels_ordering, self._tvb.labels_dimensions.keys())) self._tvb.configure() self.configure_time() self.configure_sample_rate() if len(self.title) == 0: self._tvb.title = "%s Time Series" % self.ts_type
def assert_equal_objects(obj1, obj2, attributes_dict=None, logger=None): def print_not_equal_message(attr, field1, field2, logger): # logger.error("\n\nValueError: Original and read object field "+ attr + " not equal!") # raise_value_error("\n\nOriginal and read object field " + attr + " not equal!") warning( "Original and read object field " + attr + " not equal!" + "\nOriginal field:\n" + str(field1) + "\nRead object field:\n" + str(field2), logger) if isinstance(obj1, dict): get_field1 = lambda obj, key: obj[key] if not (isinstance(attributes_dict, dict)): attributes_dict = dict() for key in obj1.keys(): attributes_dict.update({key: key}) elif isinstance(obj1, (list, tuple)): get_field1 = lambda obj, key: get_list_or_tuple_item_safely(obj, key) indices = list(range(len(obj1))) attributes_dict = dict( list(zip([str(ind) for ind in indices], indices))) else: get_field1 = lambda obj, attribute: getattr(obj, attribute) if not (isinstance(attributes_dict, dict)): attributes_dict = dict() for key in obj1.__dict__.keys(): attributes_dict.update({key: key}) if isinstance(obj2, dict): get_field2 = lambda obj, key: obj.get(key, None) elif isinstance(obj2, (list, tuple)): get_field2 = lambda obj, key: get_list_or_tuple_item_safely(obj, key) else: get_field2 = lambda obj, attribute: getattr(obj, attribute, None) equal = True for attribute in attributes_dict: # print attributes_dict[attribute] field1 = get_field1(obj1, attributes_dict[attribute]) field2 = get_field2(obj2, attributes_dict[attribute]) try: # TODO: a better hack for the stupid case of an ndarray of a string, such as model.zmode or pmode # For non numeric types if isinstance(field1, string_types) or isinstance(field1, list) or isinstance(field1, dict) \ or (isinstance(field1, np.ndarray) and field1.dtype.kind in 'OSU'): if np.any(field1 != field2): print_not_equal_message(attributes_dict[attribute], field1, field2, logger) equal = False # For numeric numpy arrays: elif isinstance(field1, np.ndarray) and not field1.dtype.kind in 'OSU': # TODO: handle better accuracy differences, empty matrices and complex numbers... if field1.shape != field2.shape: print_not_equal_message(attributes_dict[attribute], field1, field2, logger) equal = False elif np.any(np.float32(field1) - np.float32(field2) > 0): print_not_equal_message(attributes_dict[attribute], field1, field2, logger) equal = False # For numeric scalar types elif is_numeric(field1): if np.float32(field1) - np.float32(field2) > 0: print_not_equal_message(attributes_dict[attribute], field1, field2, logger) equal = False else: equal = assert_equal_objects(field1, field2, logger=logger) except: try: warning( "Comparing str(objects) for field " + str(attributes_dict[attribute]) + " because there was an error!", logger) if np.any(str(field1) != str(field2)): print_not_equal_message(attributes_dict[attribute], field1, field2, logger) equal = False except: raise_value_error( "ValueError: Something went wrong when trying to compare " + str(attributes_dict[attribute]) + " !", logger) if equal: return True else: return False
def from_folder(cls, path=CONFIGURED.input.HEAD, head=None, **kwargs): # TODO confirm the filetypes and add (h5 and other) readers to all TVB classes .from_file methods # Default patterns: # *conn* for zip/h5 files # (*cort/subcort*)surf*(*cort/subcort*) / (*cort/subcort*)srf*(*cort/subcort*) for zip/h5 files # (*cort/subcort*)reg*map(*cort/subcort*) for txt files # *map*vol* / *vol*map* for txt files # *t1/t2/flair/b0 for ??? files # *eeg/seeg/meg*sensors/locations* / *sensors/locations*eeg/seeg/meg for txt files # # *eeg/seeg/meg*proj/gain* / *proj/gain*eeg/seeg/meg for npy/mat used_filepaths = [] if head is None: head = Head() head.path = path title = os.path.basename(path) if len(title) > 0: head.title = title # We need to read local_connectivity first to avoid confusing it with connectivity: head.local_connectivity, kwargs = \ head._load_reference(LocalConnectivity, 'local_connectivity', ["loc*conn", "conn*loc"], used_filepaths, **kwargs) # Connectivity is required # conn_instances connectivity, kwargs = \ head._load_reference(Connectivity, "connectivity", ["conn"], used_filepaths, **kwargs) if connectivity is None: raise_value_error( "A Connectivity instance is minimally required for a Head instance!" ) head.connectivity = connectivity # TVB only volume datatypes: do before region_mappings to avoid confusing them with volume_mapping structural = None for datatype, arg_name, patterns in zip([B0, Flair, T2, T1], [ "b0", "flair", "t2", "t1", ], [["b0"], ["flair"], ["t2"], ["t1"]]): try: datatype.from_file instance, kwargs = head._load_reference( datatype, arg_name, patterns, used_filepaths, **kwargs) except: warning("No 'from_file' method yet for %s!" % datatype.__class__.__name__) instance = None if instance is not None: setattr(head, arg_name, instance) volume_instance = instance if structural is not None: head.region_volume_mapping, kwargs = \ head._load_reference(RegionVolumeMapping, "region_volume_mapping", ["vol*map", "map*vol"], used_filepaths, **kwargs) # Surfaces and mappings # (read subcortical ones first to avoid confusion): head.subcortical_surface, kwargs = \ head._load_reference(SubcorticalSurface, "subcortical_surface", ["subcort*surf", "surf*subcort", "subcort*srf", "srf*subcort"], used_filepaths, **kwargs) if head.subcortical_surface is not None: # Region Mapping requires Connectivity and Surface head.subcortical_region_mapping, kwargs = \ head._load_reference(SubcorticalRegionMapping, "subcortical_region_mapping", ["subcort*reg*map", "reg*map*subcort"], used_filepaths, **kwargs) head.cortical_surface, kwargs = \ head._load_reference(CorticalSurface, "cortical_surface", ["cort*surf", "surf*cort", "cort*srf", "srf*cort", "surf", "srf"], used_filepaths, **kwargs) if head.cortical_surface is not None: # Region Mapping requires Connectivity and Surface head.cortical_region_mapping, kwargs = \ head._load_reference(CorticalRegionMapping, "cortical_region_mapping", ["cort*reg*map", "reg*map*cort", "reg*map"], used_filepaths, **kwargs) # Sensors and projections # (read seeg before eeg to avoid confusion!) for s_datatype, p_datatype, s_type in zip([ SensorsSEEG, SensorsEEG, SensorsMEG ], [ProjectionSurfaceSEEG, ProjectionSurfaceEEG, ProjectionSurfaceMEG], ["seeg", "eeg", "meg"]): arg_name = "%s_sensors" % s_type patterns = [ "%s*sensors" % s_type, "sensors*%s" % s_type, "%s*locations" % s_type, "locations*%s" % s_type ] sensors, kwargs = head._load_reference(s_datatype, arg_name, patterns, used_filepaths, **kwargs) if sensors is not None: setattr(head, arg_name, sensors) arg_name = "%s_projection" % s_type patterns = [ "%s*proj" % s_type, "proj*%s" % s_type, "%s*gain" % s_type, "gain*%s" % s_type ] projection, kwargs = head._load_reference( p_datatype, arg_name, patterns, used_filepaths, **kwargs) setattr(head, arg_name, projection) return head
def plot_bars(self, data, ax=None, fig=None, title="", group_names=[], legend_prefix="", figsize=None): def barlabel(ax, rects, positions): """ Attach a text label on each bar displaying its height """ for rect, pos in zip(rects, positions): height = rect.get_height() if pos < 0: y = -height pos = 0.75 * pos else: y = height pos = 0.25 * pos ax.text(rect.get_x() + rect.get_width() / 2., pos, '%0.2f' % y, color="k", ha='center', va='bottom', rotation=90) if fig is None: if not isinstance(figsize, (tuple, list)): figsize = self.config.figures.VERY_LARGE_SIZE fig, ax = pyplot.subplots(1, 1, figsize=figsize) show_and_save = True else: show_and_save = False if ax is None: ax = pyplot.gca() if isinstance( data, (list, tuple)): # If, there are many groups, data is a list: # Fill in with nan in case that not all groups have the same number of elements from itertools import izip_longest data = numpy.array( list(izip_longest(*ensure_list(data), fillvalue=numpy.nan))).T elif data.ndim == 1: # This is the case where there is only one group... data = numpy.expand_dims(data, axis=1).T n_groups, n_elements = data.shape posmax = numpy.nanmax(data) negmax = numpy.nanmax(-(-data)) n_groups_names = len(group_names) if n_groups_names != n_groups: if n_groups_names != 0: warning("Ignoring group_names because their number (" + str(n_groups_names) + ") is not equal to the number of groups (" + str(n_groups) + ")!") group_names = n_groups * [""] colorcycle = pyplot.rcParams['axes.prop_cycle'].by_key()['color'] n_colors = len(colorcycle) x_inds = numpy.arange(n_groups) width = 0.9 / n_elements elements = [] for iE in range(n_elements): elements.append( ax.bar(x_inds + iE * width, data[:, iE], width, color=colorcycle[iE % n_colors])) positions = numpy.array( [negmax if d < 0 else posmax for d in data[:, iE]]) positions[numpy.logical_or(numpy.isnan(positions), numpy.isinf( numpy.abs(positions)))] = 0.0 barlabel(ax, elements[-1], positions) if n_elements > 1: legend = [ legend_prefix + str(ii) for ii in range(1, n_elements + 1) ] ax.legend(tuple([element[0] for element in elements]), tuple(legend)) ax.set_xticks(x_inds + n_elements * width / 2) ax.set_xticklabels(tuple(group_names)) ax.set_title(title) ax.autoscale() # tight=True ax.set_xlim([-1.05 * width, n_groups * 1.05]) if show_and_save: fig.tight_layout() self._save_figure(fig) self._check_show() return fig, ax
def configure(self): super(TimeSeriesSensors, self).configure() if isinstance(self.sensors, Sensors) and not isinstance( self.sensors, SensorsInternal): warning("Creating %s with sensors of type %s!" % (self.__class__.__name__, self.sensors.__class__.__name__))