Ejemplo n.º 1
0
 def pdf_params(self, parametrization="lamda"):
     if isequal_string(parametrization, "scale"):
         return {"scale": 1.0 / self.lamda}
     elif isequal_string(parametrization, "rate"):
         return {"rate": self.rate}
     else:
         return {"lamda": self.lamda}
Ejemplo n.º 2
0
 def pdf_params(self, parametrization="lamda"):
     if isequal_string(parametrization, "scipy"):
         return {"mu": self.mu}
     elif isequal_string(parametrization, "numpy"):
         return {"lam": self.mu}
     else:
         return {"lamda": self.lamda}
Ejemplo n.º 3
0
def prepare_constraints(distribution,
                        target_stats,
                        loc=0.0,
                        scale=1.0,
                        use="manual"):
    # Preparing constraints:
    constraints = [{
        "type": "ineq",
        "fun": lambda p: fconstr(p, distribution, loc, scale, use)
    }]
    if isequal_string(distribution.type, "gamma") and np.any(
            np.in1d("mode", target_stats.keys())):
        constraints.append({
            "type": "ineq",
            "fun": lambda p: fconstr_gamma_mode(p, distribution)
        })
    elif isequal_string(distribution.type, "beta") and np.any(
            np.in1d(["mode", "median"], target_stats.keys())):
        constraints.append({
            "type":
            "ineq",
            "fun":
            lambda p: fconstr_beta_mode_median(p, distribution)
        })
    return constraints
 def pdf_params(self, parametrization="mu-sigma"):
     p = OrderedDict()
     if isequal_string(parametrization, "scipy") or isequal_string(
             parametrization, "numpy"):
         p.update(zip(["shape", "scale"], [self.shape, np.exp(self.mu)]))
         return p
     else:
         p.update(zip(["mu", "sigma"], [self.mu, self.sigma]))
         return p
 def pdf_params(self, parametrization="alpha-beta"):
     p = OrderedDict()
     if isequal_string(parametrization, "a-b") or \
        isequal_string(parametrization, "scipy") or \
        isequal_string(parametrization, "numpy"):
         p.update(zip(["a", "b"], [self.a, self.b]))
         return p
     else:
         p.update(zip(["alpha", "beta"], [self.alpha, self.beta]))
         return p
 def pdf_params(self, parametrization="a-b"):
     p = OrderedDict()
     if isequal_string(parametrization, "scipy"):
         p.update(zip(["loc", "scale"], [self.a, self.b - self.a]))
         return p
     elif isequal_string(parametrization, "numpy"):
         p.update(zip(["low", "high"], [self.low, self.high]))
         return p
     else:
         p.update(zip(["a", "b"], [self.a, self.b]))
         return p
Ejemplo n.º 7
0
 def read_dictionary_from_group(self, group, type=None):
     dictionary = dict()
     for dataset in group.keys():
         dictionary.update({dataset: group[dataset][()]})
     for attr in group.attrs.keys():
         dictionary.update({attr: group.attrs[attr]})
     if type is None:
         type = group.attrs[H5_SUBTYPE_ATTRIBUTE]
     if isequal_string(type, "DictDot"):
         return DictDot(dictionary)
     elif isequal_string(type, "OrderedDictDot"):
         return OrderedDictDot(dictionary)
     else:
         return dictionary
    def plot_fit_results(self, ests, samples, model_data, target_data, probabilistic_model=None, info_crit=None,
                         stats=None, pair_plot_params=["tau1", "sigma", "epsilon", "scale", "offset"],
                         region_violin_params=["x0", "PZ", "x1eq", "zeq"],
                         region_labels=[], regions_mode="active", seizure_indices=[],
                         trajectories_plot=True, connectivity_plot=False, skip_samples=0, title_prefix=""):
        sigma = []
        if probabilistic_model is not None:
            n_regions = probabilistic_model.number_of_regions
            region_labels = generate_region_labels(n_regions, region_labels, ". ", True)
            if probabilistic_model.parameters.get("sigma", None) is not None:
                sigma = ["sigma"]
            active_regions = ensure_list(probabilistic_model.active_regions)
        else:
            active_regions = ensure_list(model_data.get("active_regions", []))

        if isequal_string(regions_mode, "all"):
            if len(seizure_indices) == 0:
                seizure_indices = active_regions
        else:
            if len(active_regions) > 0:
                seizure_indices = [active_regions.index(ind) for ind in seizure_indices]
                if len(region_labels) > 0:
                    region_labels = region_labels[active_regions]

        if len(region_labels) == 0:
            self.print_regions_indices = True
            self.print_ts_indices = True

        figs = []

        # Pack fit samples time series into timeseries objects:
        from tvb_fit.tvb_epilepsy.top.scripts.fitting_scripts import samples_to_timeseries
        samples, target_data, x1prior, x1eps = samples_to_timeseries(samples, model_data, target_data, region_labels)
        figs.append(self.plot_fit_timeseries(target_data, samples, ests, stats, probabilistic_model, "fit_target_data",
                                             ["x1", "z"], ["dWt", "dX1t", "dZt"], sigma, seizure_indices,
                                             skip_samples, trajectories_plot, region_labels, title_prefix))

        figs.append(
            self.plot_fit_region_params(samples, stats, probabilistic_model, region_violin_params, seizure_indices,
                                        region_labels, regions_mode, False, skip_samples, title_prefix))

        figs.append(
            self.plot_fit_region_params(samples, stats, probabilistic_model, region_violin_params, seizure_indices,
                                        region_labels, regions_mode, True, skip_samples, title_prefix))

        figs.append(self.plot_fit_scalar_params(samples, stats, probabilistic_model, pair_plot_params,
                                                skip_samples, title_prefix))

        figs.append(self.plot_fit_scalar_params_iters(samples, pair_plot_params, 0, title_prefix, subplot_shape=None))


        if info_crit is not None:
            figs.append(self.plot_scalar_model_comparison(info_crit, title_prefix))
            figs.append(self.plot_array_model_comparison(info_crit, title_prefix, labels=target_data.space_labels,
                                                         xdata=target_data.time, xlabel="Time"))

        if connectivity_plot:
            figs.append(self.plot_fit_connectivity(ests, stats, probabilistic_model, "MC", region_labels, title_prefix))

        return tuple(figs)
 def pdf_params(self, parametrization="alpha-beta"):
     p = OrderedDict()
     if isequal_string(parametrization, "shape-scale"):
         p.update(zip(["shape", "scale"], [self.alpha, self.theta]))
         return p
     elif isequal_string(parametrization, "k-theta"):
         p.update(zip(["k", "theta"], [self.k, self.theta]))
         return p
     elif isequal_string(parametrization, "shape-rate"):
         p.update(zip(["shape", "rate"], [self.alpha, self.beta]))
         return p
     elif isequal_string(parametrization, "scipy"):
         p.update(zip(["a", "scale"], [self.alpha, self.theta]))
         return p
     else:
         p.update(zip(["alpha", "beta"], [self.alpha, self.beta]))
         return p
Ejemplo n.º 10
0
def eqtn_fx1(x1, z, y1, Iext1, slope, a, b, d, tau1, x1_neg=True, model="2d", x2=0.0):
    if isequal_string(str(model), '2d'):
        # Correspondence with EpileptorDP2D
        b = b - d
        return np.multiply(y1 - z + Iext1 + np.multiply(x1, np.where(x1_neg, if_ydot0(x1, a, b),
                                                                     else_ydot0_2d(x1, z, slope, d))), tau1)
    else:
        return np.multiply(y1 - z + Iext1 + np.multiply(x1, np.where(x1_neg, if_ydot0(x1, a, b),
                                                                     else_ydot0_6d(x2, z, slope))), tau1)
Ejemplo n.º 11
0
 def write_model_data_to_file(self, model_data, reset_path=False, **kwargs):
     model_data_path = kwargs.get("model_data_path", self.model_data_path)
     if reset_path:
         self.model_data_path = model_data_path
     extension = model_data_path.split(".", -1)[-1]
     if isequal_string(extension, "npy"):
         np.save(model_data_path, model_data)
     elif isequal_string(extension, "mat"):
         savemat(model_data_path, model_data)
     elif isequal_string(extension, "pkl"):
         with open(model_data_path, 'wb') as f:
             pickle.dump(model_data, f)
     elif isequal_string(extension, "R"):
         rdump(model_data_path, model_data)
     else:
         H5Writer().write_dictionary(
             model_data,
             os.path.join(os.path.dirname(model_data_path),
                          os.path.basename(model_data_path)))
Ejemplo n.º 12
0
 def update_active_regions(self,
                           probabilistic_model,
                           e_values=[],
                           x0_values=[],
                           lsa_propagation_strengths=[],
                           reset=False):
     if reset:
         probabilistic_model.update_active_regions([])
     for m in ensure_list(self.active_regions_selection_methods):
         if isequal_string(m, "E"):
             probabilistic_model = self.update_active_regions_e_values(
                 probabilistic_model, e_values, reset=False)
         elif isequal_string(m, "x0"):
             probabilistic_model = self.update_active_regions_x0_values(
                 probabilistic_model, x0_values, reset=False)
         elif isequal_string(m, "LSA"):
             probabilistic_model = self.update_active_regions_lsa(
                 probabilistic_model,
                 lsa_propagation_strengths,
                 reset=False)
     return probabilistic_model
Ejemplo n.º 13
0
 def load_model_data_from_file(self, reset_path=False, **kwargs):
     model_data_path = kwargs.get("model_data_path", self.model_data_path)
     if reset_path:
         self.model_data_path = model_data_path
     extension = model_data_path.split(".", -1)[-1]
     if isequal_string(extension, "R"):
         model_data = rload(model_data_path)
     elif isequal_string(extension, "npy"):
         model_data = np.load(model_data_path).item()
     elif isequal_string(extension, "mat"):
         model_data = loadmat(model_data_path)
     elif isequal_string(extension, "pkl"):
         with open(model_data_path, 'wb') as f:
             model_data = pickle.load(f)
     elif isequal_string(extension, "h5"):
         model_data = H5Reader().read_dictionary(model_data_path)
     else:
         raise_not_implemented_error(
             "model_data file (" + model_data_path +
             ") that are not one of (.R, .npy, .mat, .pkl) cannot be read!")
     return model_data
Ejemplo n.º 14
0
 def set_noise(self, sim_settings, **kwargs):
     # Check if the user provides a preconfigured noise instance to override
     noise = kwargs.get("noise", None)
     if isinstance(noise, Noise):
         self._check_noise_intesity_size(noise.nsig)
         sim_settings.noise_intensity = noise.nsig
         if noise.ntau == 0:
             sim_settings.noise_type = WHITE_NOISE
         else:
             sim_settings.noise_type = COLORED_NOISE
         sim_settings.noise_ntau = noise.ntau
     else:
         if isequal_string(sim_settings.noise_type, COLORED_NOISE):
             noise = self.generate_colored_noise(
                 sim_settings.noise_intensity, sim_settings.noise_ntau,
                 **kwargs)
         else:
             noise = self.generate_white_noise(sim_settings.noise_intensity)
         sim_settings.noise_ntau = noise.ntau
     return noise, sim_settings
def normalize_signals(signals, normalization=None):
    if isinstance(normalization, basestring):
        if isequal_string(normalization, "zscore"):
            signals = zscore(signals, axis=None)  # / 3.0
        elif isequal_string(normalization, "mean"):
            signals -= (signals.mean(axis=0) * np.ones(signals.shape[1:]))
        elif isequal_string(normalization, "baseline-maxstd"):
            signals -= np.percentile(np.percentile(signals, 1, axis=0), 1)
            signals /= np.max(np.std(signals, axis=0))
        elif isequal_string(normalization, "minmax"):
            signals -= signals.min()
            signals /= signals.max()
        elif isequal_string(normalization, "min"):
            signals -= signals.min()
        elif isequal_string(normalization, "baseline"):
            signals -= np.percentile(np.percentile(signals, 1, axis=0), 1)
        elif isequal_string(normalization, "baseline-amplitude"):
            signals -= np.percentile(np.percentile(signals, 1, axis=0), 1)
            signals /= np.percentile(np.percentile(signals, 99, axis=0), 99)
        elif isequal_string(normalization, "baseline-std"):
            signals -= np.percentile(np.percentile(signals, 1, axis=0), 1)
            signals /= signals.std()
        elif normalization.find("baseline") == 0:
            signals -= np.percentile(np.percentile(signals, 1, axis=0), 1)
            try:
                amplitude = float(normalization.split("baseline-")[1])
                signals /= np.percentile(np.percentile(signals, 99, axis=0),
                                         99)
                signals *= amplitude
            except:
                pass
        else:
            raise_value_error(
                "Ignoring signals' normalization " + normalization +
                ",\nwhich is not one of the currently available " +
                str(NORMALIZATION_METHODS) + "!")

    return signals
Ejemplo n.º 16
0
def generate_cmdstan_options(method, **kwargs):
    options = OrderedDict()
    if isequal_string(method, "sample"):  # for sample or sampling
        for option, value in STAN_SAMPLE_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
        if isequal_string(options["algorithm"], "hmc"):
            for option, value in STAN_HMC_OPTIONS.iteritems():
                options.update({option: kwargs.pop(option, value)})
            if isequal_string(options["engine"], "nuts"):
                options.update({
                    "max_depth":
                    kwargs.pop("max_depth", STAN_NUTS_OPTIONS["max_depth"])
                })
            elif isequal_string(options["engine"], "static"):
                options.update({
                    "int_time":
                    kwargs.pop("int_time", STAN_STATIC_OPTIONS["int_time"])
                })
        for option, value in STAN_SAMPLE_ADAPT_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
    elif isequal_string(method,
                        "variational"):  # for variational or vb or advi
        for option, value in STAN_VARIATIONAL_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
        for option, value in STAN_VARIATIONAL_ADAPT_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
    elif isequal_string(
            method, "optimize"):  # for optimization or optimizing or optimize
        for option, value in STAN_OPTIMIZE_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
        if (options["algorithm"].find("bfgs") >= 0):
            for option, value in STAN_BFGS_OPTIONS.iteritems():
                options.update({option: kwargs.pop(option, value)})
    elif isequal_string(method, "diagnose"):  # for diagnose or diagnosing
        for option, value in STAN_DIAGNOSE_TEST_GRADIENT_OPTIONS.iteritems():
            options.update({option: kwargs.pop(option, value)})
    for option, value in STAN_OUTPUT_OPTIONS.iteritems():
        options.update({option: kwargs.pop(option, value)})
    options.update({"init": kwargs.get("init", 2)})
    options.update({"random_seed": kwargs.get("random_seed", 12345)})
    options.update({"random_seed": kwargs.get("seed", options["random_seed"])})
    options.update({"n_chains_or_runs": kwargs.get("n_chains_or_runs", 4)})
    return options
Ejemplo n.º 17
0
 def _calc_kurt(self, loc=0.0, scale=1.0, use="scipy"):
     if isequal_string(use, "scipy"):
         return self._scipy(loc, scale).stats(moments="k")
     else:
         return self.calc_kurt_manual(loc, scale)
Ejemplo n.º 18
0
 def _calc_std(self, loc=0.0, scale=1.0, use="scipy"):
     if isequal_string(use, "scipy"):
         return self._scipy(loc, scale).std()
     else:
         return self.calc_std_manual(loc, scale)
Ejemplo n.º 19
0
def prepare_signal_observable(data,
                              seizure_length=SEIZURE_LENGTH,
                              on_off_set=[],
                              rois=[],
                              preprocessing=TARGET_DATA_PREPROCESSING,
                              low_hpf=LOW_HPF,
                              high_hpf=HIGH_HPF,
                              low_lpf=LOW_LPF,
                              high_lpf=HIGH_LPF,
                              win_len_ratio=WIN_LEN_RATIO,
                              plotter=None,
                              title_prefix=""):

    title_prefix = title_prefix + str(np.where(len(title_prefix) > 0, "_",
                                               "")) + "fit_data_preproc"

    ts_service = TimeseriesService()

    # Select rois if any:
    n_rois = len(rois)
    if n_rois > 0:
        if data.number_of_labels > n_rois:
            logger.info("Selecting signals...")
            if isinstance(rois[0], basestring):
                data = data.get_subspace_by_labels(rois)
            else:
                data = data.get_subspace_by_index(rois)

    # First cut data close to the desired interval
    if len(on_off_set) == 0:
        on_off_set = [data.time_start, data.time_end]
    duration = on_off_set[1] - on_off_set[0]
    temp_on_off = [
        np.maximum(data.time_start,
                   on_off_set[0] - 2 * duration / win_len_ratio),
        np.minimum(data.time_end, on_off_set[1] + 2 * duration / win_len_ratio)
    ]
    data = data.get_time_window_by_units(temp_on_off[0], temp_on_off[1])
    if plotter:
        plotter.plot_raster({"SelectedTimeInterval": data.squeezed},
                            data.time,
                            time_units=data.time_unit,
                            special_idx=[],
                            title='Selected time interval time series',
                            offset=0.1,
                            figure_name=title_prefix + '_SelectedRaster',
                            labels=data.space_labels)
        plotter.plot_timeseries({"SelectedTimeInterval": data.squeezed},
                                data.time,
                                time_units=data.time_unit,
                                special_idx=[],
                                title='Selected time interval time series',
                                figure_name=title_prefix + '_SelectedTS',
                                labels=data.space_labels)

    for i_preproc, preproc in enumerate(preprocessing):

        stri_preproc = str(i_preproc + 1)

        # Now filter, if needed, before decimation introduces any artifacts
        if isequal_string(preproc, "hpf"):
            high_hpf = np.minimum(high_hpf, 256.0)
            logger.info("High-pass filtering signals...")
            data = ts_service.filter(data,
                                     low_hpf,
                                     high_hpf,
                                     "bandpass",
                                     order=3)
            if plotter:
                plotter.plot_raster({"High-pass filtering": data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title='High-pass filtered Time Series',
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    '_%sHpfRaster' % stri_preproc,
                                    labels=data.space_labels)
                plotter.plot_timeseries({"High-pass filtering": data.squeezed},
                                        data.time,
                                        time_units=data.time_unit,
                                        special_idx=[],
                                        title='High-pass filtered Time Series',
                                        figure_name=title_prefix +
                                        '_%sHpfTS' % stri_preproc,
                                        labels=data.space_labels)

        if isequal_string(preproc, "mean-center"):
            logger.info("Mean center data...")
            data = ts_service.normalize(data, "mean")
            if plotter:
                plotter.plot_raster({"Mean centered signals": data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title='Mean centered Time Series',
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    '_%sMeanCenteredRaster' % stri_preproc,
                                    labels=data.space_labels)
                plotter.plot_timeseries(
                    {"Mean centered signals": data.squeezed},
                    data.time,
                    time_units=data.time_unit,
                    special_idx=[],
                    title='Mean centered Time Series',
                    figure_name=title_prefix +
                    '_%sMeanCenteredTS' % stri_preproc,
                    labels=data.space_labels)

        if isequal_string(preproc, "spectrogram"):
            # or
            # ...get the signals' envelope via Hilbert transform
            temp_duration = temp_on_off[1] - temp_on_off[0]
            decim_ratio = np.maximum(
                1,
                int(
                    np.floor((1.0 * data.time_length / seizure_length) *
                             (duration / temp_duration))))
            data.data = np.array(data.data).astype("float64")
            data = ts_service.spectrogram_envelope(data, high_hpf, low_hpf,
                                                   decim_ratio)
            data.data /= data.data.std()
            data.data = np.array(data.data).astype("float32")
            temp_on_off = [data.time_start, data.time_end]
            if plotter:
                plotter.plot_raster({"Spetrogram signals": data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title='Spectrogram Time Series',
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    '_%sSpectrogramRaster' % stri_preproc,
                                    labels=data.space_labels)
                plotter.plot_timeseries({"Spectrogram signals": data.squeezed},
                                        data.time,
                                        time_units=data.time_unit,
                                        special_idx=[],
                                        title='Spectrogram Time Series',
                                        figure_name=title_prefix +
                                        '_%sSpectrogramTS' % stri_preproc,
                                        labels=data.space_labels)
        plot_envelope = ""
        if preproc.lower().find(
                "envelope"
        ) >= 0:  # isequal_string(preproc, "hilbert_envelope") or isequal_string(preproc, "abs_envelope"):
            plot_envelope = preproc
            if isequal_string(preproc, "hilbert_envelope"):
                # or
                # ...get the signals' envelope via Hilbert transform
                data = ts_service.hilbert_envelope(data)
                # or
            # elif isequal_string(preproc, "square"):
            #     # Square data to get positive "power like" timeseries (introducing though higher frequencies)
            #     data = ts_service.square(data)
            else:  # isequal_string(preproc, "abs_envelope"): # "abs_envelope"
                data = ts_service.abs_envelope(data)
            if plotter:
                plot_envelop_ = plot_envelope.replace(" ", "_")
                plotter.plot_raster({plot_envelop_: data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title=plot_envelope,
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    "_%s" % stri_preproc + plot_envelop_ +
                                    "Raster",
                                    labels=data.space_labels)
                plotter.plot_timeseries({plot_envelop_: data.squeezed},
                                        data.time,
                                        time_units=data.time_unit,
                                        special_idx=[],
                                        title=plot_envelope,
                                        figure_name=title_prefix +
                                        "_%s" % stri_preproc + plot_envelop_ +
                                        "TS",
                                        labels=data.space_labels)

        if isequal_string(preproc, "log"):
            logger.info("Computing log of signals...")
            data = ts_service.log(data)
            if plotter:
                plotter.plot_raster({"LogTimeSeries": data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title='Log of Time Series',
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    '_%sLogRaster' % stri_preproc,
                                    labels=data.space_labels)
                plotter.plot_timeseries({"LogTimeSeries": data.squeezed},
                                        data.time,
                                        time_units=data.time_unit,
                                        special_idx=[],
                                        title='Log of Time Series',
                                        figure_name=title_prefix +
                                        '_%sLogTS' % stri_preproc,
                                        labels=data.space_labels)

        # Now convolve or low pass filter to smooth...
        if isequal_string(preproc, "convolve"):
            win_len = int(np.round(1.0 * data.time_length / win_len_ratio))
            str_win_len = str(win_len)
            data = ts_service.convolve(data, win_len)
            logger.info("Convolving signals with a square window of " +
                        str_win_len + " points...")
            if plotter:
                plotter.plot_raster(
                    {"ConvolutionSmoothing": data.squeezed},
                    data.time,
                    time_units=data.time_unit,
                    special_idx=[],
                    offset=0.1,
                    title='Convolved Time Series with a window of ' +
                    str_win_len + " points",
                    figure_name=title_prefix + '_%s_%spointWinConvolRaster' %
                    (stri_preproc, str_win_len),
                    labels=data.space_labels)
                plotter.plot_timeseries(
                    {"ConvolutionSmoothing": data.squeezed},
                    data.time,
                    time_units=data.time_unit,
                    special_idx=[],
                    title='Convolved Time Series with a window of ' +
                    str_win_len + " points",
                    figure_name=title_prefix + '_%s_%spointWinConvolTS' %
                    (stri_preproc, str_win_len),
                    labels=data.space_labels)

        elif isequal_string(preproc, "lpf"):
            high_lpf = np.minimum(high_lpf, 512.0)
            logger.info("Low-pass filtering signals...")
            data = ts_service.filter(data,
                                     low_lpf,
                                     high_lpf,
                                     "bandpass",
                                     order=3)
            if plotter:
                plotter.plot_raster({"Low-pass filtering": data.squeezed},
                                    data.time,
                                    time_units=data.time_unit,
                                    special_idx=[],
                                    title='Low-pass filtered Time Series',
                                    offset=0.1,
                                    figure_name=title_prefix +
                                    '_%sLpfRaster' % stri_preproc,
                                    labels=data.space_labels)
                if plotter:
                    plotter.plot_timeseries(
                        {"Low-pass filtering": data.squeezed},
                        data.time,
                        time_units=data.time_unit,
                        special_idx=[],
                        title='Low-pass filtered Time Series',
                        figure_name=title_prefix + '_%sLpfTS' % stri_preproc,
                        labels=data.space_labels)

    if "decimate" in preprocessing:
        # Now decimate to get close to seizure_length points
        temp_duration = temp_on_off[1] - temp_on_off[0]
        decim_ratio = np.maximum(
            1,
            int(
                np.round((1.0 * data.time_length / seizure_length) *
                         (duration / temp_duration))))
        if decim_ratio > 1:
            str_decim_ratio = str(decim_ratio)
            logger.info("Decimating signals " + str_decim_ratio + " times...")
            data = ts_service.decimate(data, decim_ratio)
            if plotter:
                plotter.plot_raster(
                    {str_decim_ratio + " wise Decimation": data.squeezed},
                    data.time,
                    time_units=data.time_unit,
                    special_idx=[],
                    title=str_decim_ratio + " wise Decimation",
                    offset=0.1,
                    figure_name=title_prefix + "_%s_%sxDecimRaster" %
                    (stri_preproc, str_decim_ratio),
                    labels=data.space_labels)
                plotter.plot_timeseries(
                    {str_decim_ratio + " wise Decimation": data.squeezed},
                    data.time,
                    time_units=data.time_unit,
                    special_idx=[],
                    title=str_decim_ratio + " wise Decimation",
                    figure_name=title_prefix + "_%s_%sxDecimTS" %
                    (stri_preproc, str_decim_ratio),
                    labels=data.space_labels)

    # # Cut to the desired interval
    data = data.get_time_window_by_units(on_off_set[0], on_off_set[1])

    for preproc in preprocessing:
        if preproc in NORMALIZATION_METHODS:
            # Finally, normalize signals
            logger.info("Normalizing signals...")
            data = ts_service.normalize(
                data, preproc
            )  # "baseline", "baseline-std", "baseline-amplitude" or "zscore
    if plotter:
        plotter.plot_raster({"ObservationRaster": data.squeezed},
                            data.time,
                            time_units=data.time_unit,
                            special_idx=[],
                            offset=0.1,
                            title='Observation Raster Plot',
                            figure_name=title_prefix + 'ObservationRaster',
                            labels=data.space_labels)
        plotter.plot_timeseries({"Observation": data.squeezed},
                                data.time,
                                time_units=data.time_unit,
                                special_idx=[],
                                title='Observation Time Series',
                                figure_name=title_prefix + 'ObservationTS',
                                labels=data.space_labels)
    return data
Ejemplo n.º 20
0
def main_vep(config=Config(),
             ep_name=EP_NAME,
             K_unscaled=K_UNSCALED_DEF,
             ep_indices=[],
             hyp_norm=0.99,
             manual_hypos=[],
             sim_type="paper",
             pse_flag=PSE_FLAG,
             sa_pse_flag=SA_PSE_FLAG,
             sim_flag=SIM_FLAG,
             n_samples=100,
             test_write_read=False):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)
    # -------------------------------Reading data-----------------------------------
    reader = TVBReader() if config.input.IS_TVB_MODE else H5Reader()
    writer = H5Writer()
    plotter = Plotter(config)
    logger.info("Reading from: " + config.input.HEAD)
    head = reader.read_head(config.input.HEAD)
    plotter.plot_head(head)
    if test_write_read:
        writer.write_head(head, os.path.join(config.out.FOLDER_RES, "Head"))
    # --------------------------Hypothesis definition-----------------------------------

    hypotheses = []
    # Reading a h5 file:

    if len(ep_name) > 0:
        # For an Excitability Hypothesis you leave e_indices empty
        # For a Mixed Hypothesis: you give as e_indices some indices for values > 0
        # For an Epileptogenicity Hypothesis: you give as e_indices all indices for values > 0
        hyp_file = HypothesisBuilder(head.connectivity.number_of_regions, config=config).set_normalize(hyp_norm). \
            build_hypothesis_from_file(ep_name, e_indices=ep_indices)
        hyp_file.name += ep_name
        # print(hyp_file.string_regions_disease(head.connectivity.region_labels))
        hypotheses.append(hyp_file)

    hypotheses += manual_hypos

    # --------------------------Hypothesis and LSA-----------------------------------
    for hyp in hypotheses:
        logger.info("\n\nRunning hypothesis: " + hyp.name)

        all_regions_indices = np.array(range(head.number_of_regions))
        healthy_indices = np.delete(all_regions_indices,
                                    hyp.regions_disease_indices).tolist()

        logger.info("\n\nCreating model configuration...")
        model_config_builder = ModelConfigurationBuilder("EpileptorDP2D", head.connectivity, K_unscaled=K_unscaled). \
                                    set_parameter("tau1", TAU1_DEF).set_parameter("tau0", TAU0_DEF)
        mcs_file = os.path.join(config.out.FOLDER_RES,
                                hyp.name + "_model_config_builder.h5")
        writer.write_model_configuration_builder(model_config_builder,
                                                 mcs_file)
        if test_write_read:
            logger.info(
                "Written and read model configuration builders are identical?: "
                + str(
                    assert_equal_objects(
                        model_config_builder,
                        reader.read_model_configuration_builder(mcs_file),
                        logger=logger)))
        # Fix healthy regions to default equilibria:
        # model_configuration = \
        #        model_config_builder.build_model_from_E_hypothesis(hyp)
        # Fix healthy regions to default x0s:
        model_configuration = model_config_builder.build_model_from_hypothesis(
            hyp)
        mc_path = os.path.join(config.out.FOLDER_RES,
                               hyp.name + "_ModelConfig.h5")
        writer.write_model_configuration(model_configuration, mc_path)
        if test_write_read:
            logger.info(
                "Written and read model configuration are identical?: " + str(
                    assert_equal_objects(model_configuration,
                                         reader.read_model_configuration(
                                             mc_path),
                                         logger=logger)))
        # Plot nullclines and equilibria of model configuration
        plotter.plot_state_space(model_configuration,
                                 head.connectivity.region_labels,
                                 special_idx=hyp.regions_disease_indices,
                                 figure_name=hyp.name + "_StateSpace")

        logger.info("\n\nRunning LSA...")
        lsa_service = LSAService(eigen_vectors_number=1)
        lsa_hypothesis = lsa_service.run_lsa(hyp, model_configuration)

        lsa_path = os.path.join(config.out.FOLDER_RES,
                                lsa_hypothesis.name + "_LSA.h5")
        lsa_config_path = os.path.join(config.out.FOLDER_RES,
                                       lsa_hypothesis.name + "_LSAConfig.h5")
        writer.write_hypothesis(lsa_hypothesis, lsa_path)
        writer.write_lsa_service(lsa_service, lsa_config_path)
        if test_write_read:
            logger.info("Written and read LSA services are identical?: " + str(
                assert_equal_objects(lsa_service,
                                     reader.read_lsa_service(lsa_config_path),
                                     logger=logger)))
            logger.info(
                "Written and read LSA hypotheses are identical (no input check)?: "
                + str(
                    assert_equal_objects(lsa_hypothesis,
                                         reader.read_hypothesis(lsa_path),
                                         logger=logger)))
        plotter.plot_lsa(lsa_hypothesis,
                         model_configuration,
                         lsa_service.weighted_eigenvector_sum,
                         lsa_service.eigen_vectors_number,
                         head.connectivity.region_labels,
                         None,
                         lsa_service=lsa_service)

        if pse_flag:
            # --------------Parameter Search Exploration (PSE)-------------------------------
            logger.info("\n\nRunning PSE LSA...")
            pse_results = pse_from_lsa_hypothesis(
                n_samples,
                lsa_hypothesis,
                model_configuration.connectivity,
                model_config_builder,
                lsa_service,
                head.connectivity.region_labels,
                param_range=0.1,
                global_coupling=[{
                    "indices": all_regions_indices
                }],
                healthy_regions_parameters=[{
                    "name": "x0_values",
                    "indices": healthy_indices
                }],
                logger=logger,
                save_flag=True)[0]
            plotter.plot_lsa(lsa_hypothesis, model_configuration,
                             lsa_service.weighted_eigenvector_sum,
                             lsa_service.eigen_vectors_number,
                             head.connectivity.region_labels, pse_results)

            pse_lsa_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_PSE_LSA_results.h5")
            writer.write_dictionary(pse_results, pse_lsa_path)
            if test_write_read:
                logger.info(
                    "Written and read parameter search results are identical?: "
                    + str(
                        assert_equal_objects(pse_results,
                                             reader.read_dictionary(
                                                 pse_lsa_path),
                                             logger=logger)))

        if sa_pse_flag:
            # --------------Sensitivity Analysis Parameter Search Exploration (PSE)-------------------------------
            logger.info("\n\nrunning sensitivity analysis PSE LSA...")
            sa_results, pse_sa_results = \
                sensitivity_analysis_pse_from_lsa_hypothesis(n_samples, lsa_hypothesis,
                                                             model_configuration.connectivity,
                                                             model_config_builder, lsa_service,
                                                             head.connectivity.region_labels,
                                                             method="sobol", param_range=0.1,
                                                             global_coupling=[{"indices": all_regions_indices,
                                                                               "bounds": [0.0, 2 *
                                                                                          model_config_builder.K_unscaled[
                                                                                              0]]}],
                                                             healthy_regions_parameters=[
                                                                 {"name": "x0_values", "indices": healthy_indices}],
                                                             config=config)
            plotter.plot_lsa(lsa_hypothesis,
                             model_configuration,
                             lsa_service.weighted_eigenvector_sum,
                             lsa_service.eigen_vectors_number,
                             head.connectivity.region_labels,
                             pse_sa_results,
                             title="SA PSE Hypothesis Overview")

            sa_pse_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_SA_PSE_LSA_results.h5")
            sa_lsa_path = os.path.join(
                config.out.FOLDER_RES,
                lsa_hypothesis.name + "_SA_LSA_results.h5")
            writer.write_dictionary(pse_sa_results, sa_pse_path)
            writer.write_dictionary(sa_results, sa_lsa_path)
            if test_write_read:
                logger.info(
                    "Written and read sensitivity analysis results are identical?: "
                    + str(
                        assert_equal_objects(sa_results,
                                             reader.read_dictionary(
                                                 sa_lsa_path),
                                             logger=logger)))
                logger.info(
                    "Written and read sensitivity analysis parameter search results are identical?: "
                    + str(
                        assert_equal_objects(pse_sa_results,
                                             reader.read_dictionary(
                                                 sa_pse_path),
                                             logger=logger)))

        if sim_flag:
            # --------------------------Simulation preparations-----------------------------------
            # If you choose model...
            # Available models beyond the TVB Epileptor (they all encompass optional variations from the different papers):
            # EpileptorDP: similar to the TVB Epileptor + optional variations,
            # EpileptorDP2D: reduced 2D model, following Proix et all 2014 +optional variations,
            # EpleptorDPrealistic: starting from the TVB Epileptor + optional variations, but:
            #      -x0, Iext1, Iext2, slope and K become noisy state variables,
            #      -Iext2 and slope are coupled to z, g, or z*g in order for spikes to appear before seizure,
            #      -correlated noise is also used
            # We don't want any time delays for the moment
            head.connectivity.tract_lengths *= config.simulator.USE_TIME_DELAYS_FLAG
            sim_types = ensure_list(sim_type)
            for sim_type in sim_types:
                # ------------------------------Simulation--------------------------------------
                logger.info(
                    "\n\nConfiguring %s simulation from model_configuration..."
                    % sim_type)
                if isequal_string(sim_type, "realistic"):
                    sim_builder = SimulatorBuilder(model_configuration).set_model("EpileptorDPrealistic"). \
                        set_fs(2048.0).set_simulation_length(60000.0)
                    sim_builder.model_config.tau0 = 60000.0
                    sim_builder.model_config.tau1 = 0.2
                    sim_builder.model_config.slope = 0.25
                    sim_builder.model_config.pmode = np.array([PMODE_DEF])
                    sim_settings = sim_builder.build_sim_settings()
                    sim_settings.noise_type = COLORED_NOISE
                    sim_settings.noise_ntau = 20
                    # Necessary a more stable integrator:
                    sim_settings.integrator_type = "Dop853Stochastic"
                elif isequal_string(sim_type, "fitting"):
                    sim_builder = SimulatorBuilder(model_configuration).set_model("EpileptorDP2D"). \
                        set_fs(2048.0).set_fs_monitor(2048.0).set_simulation_length(300.0)
                    sim_builder.model_config.tau0 = 30.0
                    sim_builder.model_config.tau1 = 0.5
                    sim_settings = sim_builder.build_sim_settings()
                    sim_settings.noise_intensity = np.array([0.0, 1e-5])
                elif isequal_string(sim_type, "reduced"):
                    sim_builder = \
                        SimulatorBuilder(model_configuration).set_model("EpileptorDP2D").set_fs(
                            4096.0).set_simulation_length(1000.0)
                    sim_settings = sim_builder.build_sim_settings()
                elif isequal_string(sim_type, "paper"):
                    sim_builder = SimulatorBuilder(
                        model_configuration).set_model("Epileptor")
                    sim_settings = sim_builder.build_sim_settings()
                else:
                    sim_builder = SimulatorBuilder(
                        model_configuration).set_model("EpileptorDP")
                    sim_settings = sim_builder.build_sim_settings()

                # Integrator and initial conditions initialization.
                # By default initial condition is set right on the equilibrium point.
                sim, sim_settings = \
                    sim_builder.build_simulator_TVB_from_model_sim_settings(head.connectivity, sim_settings)
                sim_path = os.path.join(
                    config.out.FOLDER_RES,
                    lsa_hypothesis.name + "_" + sim_type + "_sim_settings.h5")
                model_path = os.path.join(
                    config.out.FOLDER_RES,
                    lsa_hypothesis.name + sim_type + "_model.h5")
                writer.write_simulation_settings(sim.settings, sim_path)
                writer.write_simulator_model(
                    sim.model, model_path, sim.connectivity.number_of_regions)
                if test_write_read:
                    # TODO: find out why it cannot set monitor expressions
                    logger.info(
                        "Written and read simulation settings are identical?: "
                        + str(
                            assert_equal_objects(
                                sim.settings,
                                reader.read_simulation_settings(sim_path),
                                logger=logger)))
                    # logger.info("Written and read simulation model are identical?: " +
                    #             str(assert_equal_objects(sim.model,
                    #                                      reader.read_epileptor_model(model_path), logger=logger)))

                logger.info("\n\nSimulating %s..." % sim_type)
                sim_output, status = sim.launch_simulation(
                    report_every_n_monitor_steps=100, timeseries=Timeseries)
                if not status:
                    logger.warning("\nSimulation failed!")
                else:
                    time = np.array(sim_output.time).astype("f")
                    logger.info("\n\nSimulated signal return shape: %s",
                                sim_output.shape)
                    logger.info("Time: %s - %s", time[0], time[-1])
                    logger.info("Values: %s - %s", sim_output.data.min(),
                                sim_output.data.max())
                    if not status:
                        logger.warning("\nSimulation failed!")
                    else:
                        sim_output, seeg = compute_seeg_and_write_ts_to_h5(
                            sim_output,
                            sim.model,
                            head.sensorsSEEG,
                            os.path.join(config.out.FOLDER_RES,
                                         sim_type + "_ts.h5"),
                            seeg_gain_mode="lin",
                            hpf_flag=True,
                            hpf_low=10.0,
                            hpf_high=512.0)

                        # Plot results
                        plotter.plot_simulated_timeseries(
                            sim_output,
                            sim.model,
                            lsa_hypothesis.lsa_propagation_indices,
                            seeg_dict=seeg,
                            spectral_raster_plot=False,
                            title_prefix=hyp.name,
                            spectral_options={"log_scale": True})
Ejemplo n.º 21
0
    def plot_timeseries(self,
                        data_dict,
                        time=None,
                        mode="ts",
                        subplots=None,
                        special_idx=[],
                        subtitles=[],
                        labels=[],
                        offset=0.5,
                        time_units="ms",
                        title='Time series',
                        figure_name=None,
                        figsize=FiguresConfig.LARGE_SIZE):
        n_vars = len(data_dict)
        vars = data_dict.keys()
        data = data_dict.values()
        data_lims = []
        for id, d in enumerate(data):
            if isequal_string(mode, "raster"):
                data[id] = (d - d.mean(axis=0))
                drange = numpy.max(data[id].max(axis=0) - data[id].min(axis=0))
                data[id] = data[id] / drange  # zscore(d, axis=None)
            data_lims.append([d.min(), d.max()])
        data_shape = data[0].shape
        n_times, nTS = data_shape[:2]
        if len(data_shape) > 2:
            nSamples = data_shape[2]
        else:
            nSamples = 1
        if special_idx is None:
            special_idx = []
        n_special_idx = len(special_idx)
        if len(subtitles) == 0:
            subtitles = vars
        if isinstance(labels, list) and len(labels) == n_vars:
            labels = [
                generate_region_labels(nTS, label, ". ", self.print_ts_indices)
                for label in labels
            ]
        else:
            labels = [
                generate_region_labels(nTS, labels, ". ",
                                       self.print_ts_indices)
                for _ in range(n_vars)
            ]
        if isequal_string(mode, "traj"):
            data_fun, plot_lines, projection, n_rows, n_cols, def_alpha, loopfun, \
            subtitle, subtitle_col, axlabels, axlimits = \
                self._trajectories_plot(n_vars, nTS, nSamples, subplots)
        else:
            if isequal_string(mode, "raster"):
                data_fun, time, plot_lines, projection, n_rows, n_cols, def_alpha, loopfun, \
                subtitle, subtitle_col, axlabels, axlimits, axYticks = \
                    self._timeseries_plot(time, n_vars, nTS, n_times, time_units, 0, offset, data_lims)

            else:
                data_fun, time, plot_lines, projection, n_rows, n_cols, def_alpha, loopfun, \
                subtitle, subtitle_col, axlabels, axlimits, axYticks = \
                    self._timeseries_plot(time, n_vars, nTS, n_times, time_units, ensure_list(subplots)[0])
        alpha_ratio = 1.0 / nSamples
        alphas = numpy.maximum(
            numpy.array([def_alpha] * nTS) * alpha_ratio, 0.1)
        alphas[special_idx] = numpy.maximum(alpha_ratio, 0.1)
        if isequal_string(mode, "traj") and (n_cols * n_rows > 1):
            colors = numpy.zeros((nTS, 4))
            colors[special_idx] = \
                numpy.array([numpy.array([1.0, 0, 0, 1.0]) for _ in range(n_special_idx)]).reshape((n_special_idx, 4))
        else:
            cmap = matplotlib.cm.get_cmap('jet')
            colors = numpy.array([cmap(0.5 * iTS / nTS) for iTS in range(nTS)])
            colors[special_idx] = \
                numpy.array([cmap(1.0 - 0.25 * iTS / nTS) for iTS in range(n_special_idx)]).reshape((n_special_idx, 4))
        colors[:, 3] = alphas
        lines = []
        pyplot.figure(title, figsize=figsize)
        pyplot.hold(True)
        axes = []
        for icol in range(n_cols):
            if n_rows == 1:
                # If there are no more rows, create axis, and set its limits, labels and possible subtitle
                axes += ensure_list(
                    pyplot.subplot(n_rows,
                                   n_cols,
                                   icol + 1,
                                   projection=projection))
                axlimits(data_lims, time, n_vars, icol)
                axlabels(labels[icol % n_vars], vars, n_vars, n_rows, 1, 0)
                pyplot.gca().set_title(subtitles[icol])
            for iTS in loopfun(nTS, n_rows, icol):
                if n_rows > 1:
                    # If there are more rows, create axes, and set their limits, labels and possible subtitles
                    axes += ensure_list(
                        pyplot.subplot(n_rows,
                                       n_cols,
                                       iTS + 1,
                                       projection=projection))
                    axlimits(data_lims, time, n_vars, icol)
                    subtitle(labels[icol % n_vars], iTS)
                    axlabels(labels[icol % n_vars], vars, n_vars, n_rows,
                             (iTS % n_rows) + 1, iTS)
                lines += ensure_list(
                    plot_lines(data_fun(data, time, icol), iTS, colors,
                               labels[icol % n_vars]))
            if isequal_string(
                    mode,
                    "raster"):  # set yticks as labels if this is a raster plot
                axYticks(labels[icol % n_vars], nTS)
                yticklabels = pyplot.gca().yaxis.get_ticklabels()
                self.tick_font_size = numpy.minimum(
                    self.tick_font_size,
                    int(numpy.round(self.tick_font_size * 100.0 / nTS)))
                for iTS in range(nTS):
                    yticklabels[iTS].set_fontsize(self.tick_font_size)
                    if iTS in special_idx:
                        yticklabels[iTS].set_color(colors[iTS, :3].tolist() +
                                                   [1])
                pyplot.gca().yaxis.set_ticklabels(yticklabels)
                pyplot.gca().invert_yaxis()

        if self.config.figures.MOUSE_HOOVER:
            for line in lines:
                self.HighlightingDataCursor(line,
                                            formatter='{label}'.format,
                                            bbox=dict(fc='white'),
                                            arrowprops=dict(
                                                arrowstyle='simple',
                                                fc='white',
                                                alpha=0.5))

        self._save_figure(pyplot.gcf(), figure_name)
        self._check_show()
        return pyplot.gcf(), axes, lines
def main_sampling_service(config=Config()):
    logger = initialize_logger(__name__, config.out.FOLDER_LOGS)

    n_samples = 100
    logger.info("\nDeterministic numpy.linspace sampling:")
    sampler = DeterministicSampler(n_samples=n_samples, grid_mode=True)
    samples, stats = sampler.generate_samples(low=1.0,
                                              high=2.0,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nStochastic uniform sampling with numpy:")
    sampler = ProbabilisticSampler(n_samples=n_samples,
                                   sampling_module="numpy")
    #                                      a (low), b (high)
    samples, stats = sampler.generate_samples(
        parameter=(1.0, 2.0),
        probability_distribution=ProbabilityDistributionTypes.UNIFORM,
        shape=(2, ),
        stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))

    logger.info(sampler.__repr__())

    logger.info("\nStochastic truncated normal sampling with scipy:")
    sampler = ProbabilisticSampler(n_samples=n_samples)
    #                                   loc (mean), scale (sigma)
    samples, stats = sampler.generate_samples(parameter=(1.5, 1.0),
                                              probability_distribution="norm",
                                              low=1,
                                              high=2,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nSensitivity analysis sampling:")
    sampler = SalibSamplerInterface(n_samples=n_samples, sampler="latin")
    samples, stats = sampler.generate_samples(low=1,
                                              high=2,
                                              shape=(2, ),
                                              stats=True)
    for key, value in stats.items():
        logger.info("\n" + key + ": " + str(value))
    logger.info(sampler.__repr__())

    logger.info("\nTesting distribution class and conversions...")
    sampler = ProbabilisticSampler(n_samples=n_samples)
    for distrib_name in ProbabilityDistributionTypes.available_distributions:
        logger.info("\n" + distrib_name)
        logger.info("\nmode/mean, std to distribution " + distrib_name + ":")
        if np.in1d(distrib_name, [
                ProbabilityDistributionTypes.EXPONENTIAL,
                ProbabilityDistributionTypes.CHISQUARE
        ]):
            target_stats = {"mean": 1.0}
            stats_m = "mean"
        elif np.in1d(distrib_name, [
                ProbabilityDistributionTypes.BERNOULLI,
                ProbabilityDistributionTypes.POISSON
        ]):
            target_stats = {"mean": np.ones((2, ))}
            stats_m = "mean"
        elif isequal_string(distrib_name,
                            ProbabilityDistributionTypes.BINOMIAL):
            target_stats = {"mean": 1.0, "std": 2.0}
            stats_m = "mean"
        else:
            if isequal_string(distrib_name,
                              ProbabilityDistributionTypes.UNIFORM):
                target_stats = {"mean": 1.0, "std": 2.0}
                stats_m = "mean"
            else:
                target_stats = {"mean": 1.0, "std": 2.0}
                stats_m = "mean"
        parameter1 = generate_probabilistic_parameter(
            name="test1_" + distrib_name,
            low=0.0,
            high=2.0,
            p_shape=(2, 2),
            probability_distribution=distrib_name,
            optimize_pdf=True,
            use="manual",
            **target_stats)
        name2 = "test2_" + distrib_name
        defaults = set_parameter_defaults(name2,
                                          _pdf=distrib_name,
                                          _shape=(2, 2),
                                          _lo=0.0,
                                          _hi=2.0,
                                          **(deepcopy(target_stats)))
        parameter2 = set_parameter(name=name2, use="manual", **defaults)
        for parameter in (parameter1, parameter2):
            logger.info(str(parameter))
            samples = sampler.generate_samples(parameter=parameter, stats=True)
            for key, value in stats.items():
                logger.info("\n" + key + ": " + str(value))
            diff = target_stats[stats_m] - stats[stats_m]
            if np.any(np.abs(diff.flatten()) > 0.001):
                logger.warning(
                    "Large difference between target and resulting samples' " +
                    stats_m + "!: " + str(diff))
            del parameter
Ejemplo n.º 23
0
 def sample(self, parameter=(), loc=0.0, scale=1.0, **kwargs):
     nr.seed(self.random_seed)
     if isinstance(parameter, (ProbabilisticParameterBase,
                               TransformedProbabilisticParameterBase)):
         parameter_shape = parameter.p_shape
         low = parameter.low
         high = parameter.high
         prob_distr = parameter
         loc = parameter.loc
         scale = parameter.scale
     else:
         parameter_shape = kwargs.pop("shape", (1, ))
         low = kwargs.pop("low", -CalculusConfig.MAX_SINGLE_VALUE)
         high = kwargs.pop("high", CalculusConfig.MAX_SINGLE_VALUE)
         prob_distr = kwargs.pop("probability_distribution", "uniform")
     low, high = self.check_for_infinite_bounds(low, high)
     low, high, n_outputs, parameter_shape = self.check_size(
         low, high, parameter_shape)
     self.adjust_shape(parameter_shape)
     out_shape = tuple([self.n_samples] + list(self.shape)[:-1])
     if np.any(low > -CalculusConfig.MAX_SINGLE_VALUE) or np.any(
             high < CalculusConfig.MAX_SINGLE_VALUE):
         if not (isequal_string(self.sampling_module, "scipy")):
             self.logger.warning(
                 "Switching to scipy for truncated distributions' sampling!"
             )
         self.sampling_module = "scipy"
         if isinstance(prob_distr, basestring):
             self.sampler = getattr(ss, prob_distr)(*parameter, **kwargs)
             samples = self._truncated_distribution_sampling(
                 {
                     "low": low,
                     "high": high
                 }, out_shape) * scale + loc
         elif isinstance(prob_distr,
                         (ProbabilisticParameterBase,
                          TransformedProbabilisticParameterBase)):
             self.sampler = prob_distr.scipy()
             samples = self._truncated_distribution_sampling(
                 {
                     "low": low,
                     "high": high
                 }, out_shape)
     elif self.sampling_module.find("scipy") >= 0:
         if isinstance(prob_distr, basestring):
             self.sampler = getattr(ss, prob_distr)(*parameter, **kwargs)
             samples = self.sampler.rvs(size=out_shape) * scale + loc
         elif isinstance(prob_distr, ProbabilisticParameterBase):
             self.sampler = prob_distr._scipy(**kwargs)
             samples = self.sampler.rvs(size=out_shape)
     elif self.sampling_module.find("numpy") >= 0:
         if isinstance(prob_distr, basestring):
             self.sampler = lambda size: getattr(nr, prob_distr)(
                 *parameter, size=size, **kwargs)
             samples = self.sampler(out_shape) * scale + loc
         elif isinstance(prob_distr,
                         (ProbabilisticParameterBase,
                          TransformedProbabilisticParameterBase)):
             self.sampler = lambda size: prob_distr.numpy(size=size)
             samples = self.sampler(out_shape)
     return samples.T
def from_model_configuration_to_simulation(model_configuration, head, lsa_hypothesis, rescale_x1eq=None,
                                           sim_type="realistic", ts_file=None, seeg_gain_mode="lin", hpf_flag=False,
                                           hpf_low=10.0, hpf_high=512.0, config=Config(), plotter=False,
                                           title_prefix=""):
    # Choose model
    # Available models beyond the TVB Epileptor (they all encompass optional variations from the different papers):
    # EpileptorDP: similar to the TVB Epileptor + optional variations,
    # EpileptorDP2D: reduced 2D model, following Proix et all 2014 +optional variations,
    # EpleptorDPrealistic: starting from the TVB Epileptor + optional variations, but:
    #      -x0, Iext1, Iext2, slope and K become noisy state variables,
    #      -Iext2 and slope are coupled to z, g, or z*g in order for spikes to appear before seizure,
    #      -multiplicative correlated noise is also used
    # Optional variations:

    if rescale_x1eq is not None:
        model_config = deepcopy(model_configuration)
        x1eq_min = np.min(model_config.x1eq)
        model_config.x1eq = interval_scaling(model_config.x1eq, min_targ=x1eq_min, max_targ=rescale_x1eq,
                                              min_orig=x1eq_min, max_orig=np.max(model_config.x1eq))
        zeq = calc_eq_z(model_config.x1eq, model_config.yc, model_config.Iext1, "2d",
                        np.zeros(model_config.zeq.shape), model_config.slope, model_config.a,
                                 model_config.b, model_config.d)
        model_config.x0 = calc_x0(model_config.x1eq, zeq, model_config.K, model_config.connectivity,
                                  model_config.zmode, z_pos=True, shape=zeq.shape, calc_mode="non_symbol")
    else:
        model_config = model_configuration

    # ------------------------------Simulation--------------------------------------
    hypname = lsa_hypothesis.name.replace("_LSA", "")
    logger.info("\n\nConfiguring simulation...")
    if isequal_string(sim_type, "realistic"):
        sim, sim_settings= build_simulator_TVB_realistic(model_config, head.connectivity)
    elif isequal_string(sim_type, "fitting"):
        sim, sim_settings = build_simulator_TVB_fitting(model_config, head.connectivity)
    elif isequal_string(sim_type, "paper"):
        sim, sim_settings = build_simulator_TVB_paper(model_config, head.connectivity)
    else:
        sim, sim_settings = build_simulator_TVB_default(model_config, head.connectivity)
    dynamical_model = sim.model
    writer = H5Writer()
    if config.out.FOLDER_RES.find(hypname) >= 0:
        model_path = os.path.join(config.out.FOLDER_RES, dynamical_model._ui_name + "_model.h5")
        title_prefix += ""
    else:
        model_path = os.path.join(config.out.FOLDER_RES, hypname + dynamical_model._ui_name + "_model.h5")
        title_prefix += hypname
    writer.write_simulator_model(sim.model, model_path, sim.connectivity.number_of_regions)

    seeg=[]
    if ts_file is not None and os.path.isfile(ts_file):
        logger.info("\n\nLoading previously simulated time series from file: " + ts_file)
        sim_output = H5Reader().read_timeseries(ts_file)
        seeg = TimeseriesService().compute_seeg(sim_output.get_source(), head.sensorsSEEG, sum_mode=seeg_gain_mode)
    else:
        logger.info("\n\nSimulating %s..." % sim_type)
        sim_output, status = sim.launch_simulation(report_every_n_monitor_steps=100, timeseries=Timeseries)
        if not status:
            logger.warning("\nSimulation failed!")
        else:
            time = np.array(sim_output.time).astype("f")
            logger.info("\n\nSimulated signal return shape: %s", sim_output.shape)
            logger.info("Time: %s - %s", time[0], time[-1])
            sim_output, seeg = compute_seeg_and_write_ts_to_h5(sim_output, sim.model, head.sensorsSEEG,
                                                               ts_file, seeg_gain_mode=seeg_gain_mode,
                                                               hpf_flag=hpf_flag, hpf_low=hpf_low, hpf_high=hpf_high)

    if plotter:
        if not isinstance(plotter, Plotter):
            plotter = Plotter(config)
        # Plot results
        plotter.plot_simulated_timeseries(sim_output, sim.model, lsa_hypothesis.all_disease_indices, seeg_dict=seeg,
                                          spectral_raster_plot=False, title_prefix=title_prefix,
                                          spectral_options={"log_scale": True})

    return {"source": sim_output, "seeg": seeg}, sim
Ejemplo n.º 25
0
 def build_simulator(self, connectivity, **kwargs):
     if isequal_string(self.simulator, "java"):
         return self.build_simulator_java_from_model_configuration(
             connectivity, **kwargs)
     else:
         return self.build_simulator_TVB(connectivity, **kwargs)
    def plot_state_space(self, model_config, region_labels=[], special_idx=[],
                         figure_name="", approximations=False):
        if model_config.model_name == "Epileptor2D":
            model = "2d"
        else:
            model = "6d"
        add_name = " " + "Epileptor " + model + " z-" + str(numpy.where(model_config.zmode[0], "exp", "lin"))
        figure_name = figure_name + add_name

        region_labels = generate_region_labels(model_config.number_of_regions, region_labels, ". ")
        # n_region_labels = len(region_labels)
        # if n_region_labels == model_config.number_of_regions:
        #     region_labels = numpy.array(["%d. %s" % l for l in zip(range(model_config.number_of_regions), region_labels)])
        # else:
        #     region_labels = numpy.array(["%d" % l for l in range(model_config.number_of_regions)])

        # Fixed parameters for all regions:
        x1eq = model_config.x1eq
        zeq = model_config.zeq
        x0 = a = b = d = yc = slope = Iext1 = Iext2 = s = tau1 = tau0 = zmode = 0.0
        for p in ["x0", "a", "b", "d", "yc", "slope", "Iext1", "Iext2", "s", "tau1", "tau0", "zmode"]:
            exec (p + " = numpy.mean(model_config." + p + ")")

        fig = pyplot.figure(figure_name, figsize=FiguresConfig.SMALL_SIZE)

        # Lines:
        x1 = numpy.linspace(-2.0, 1.0, 100)
        if isequal_string(model, "2d"):
            y1 = yc
        else:
            y1 = calc_eq_y1(x1, yc, d=d)
        # x1 nullcline:
        zX1 = calc_fx1(x1, z=0, y1=y1, Iext1=Iext1, slope=slope, a=a, b=b, d=d, tau1=1.0, x1_neg=True, model=model,
                       x2=0.0)  # yc + Iext1 - x1 ** 3 - 2.0 * x1 ** 2
        x1null, = pyplot.plot(x1, zX1, 'b-', label='x1 nullcline', linewidth=1)
        ax = pyplot.gca()
        ax.axes.hold(True)
        # z nullcines
        # center point (critical equilibrium point) without approximation:
        # zsq0 = yc + Iext1 - x1sq0 ** 3 - 2.0 * x1sq0 ** 2
        x0e = calc_x0_val_to_model_x0(X0_CR_DEF, yc, Iext1, a=a, b=b, d=d, zmode=model_config.zmode)
        x0ne = calc_x0_val_to_model_x0(X0_DEF, yc, Iext1, a=a, b=b, d=d, zmode=model_config.zmode)
        zZe = calc_fz(x1, z=0.0, x0=x0e, tau1=1.0, tau0=1.0, zmode=model_config.zmode)  # for epileptogenic regions
        zZne = calc_fz(x1, z=0.0, x0=x0ne, tau1=1.0, tau0=1.0, zmode=model_config.zmode)  # for non-epileptogenic regions
        zE1null, = pyplot.plot(x1, zZe, 'g-', label='z nullcline at critical point (e_values=1)', linewidth=1)
        zE2null, = pyplot.plot(x1, zZne, 'g--', label='z nullcline for e_values=0', linewidth=1)
        if approximations:
            # The point of the linear approximation (1st order Taylor expansion)
            x1LIN = def_x1lin(X1_DEF, X1EQ_CR_DEF, len(region_labels))
            x1SQ = X1EQ_CR_DEF
            x1lin0 = numpy.mean(x1LIN)
            # The point of the square (parabolic) approximation (2nd order Taylor expansion)
            x1sq0 = numpy.mean(x1SQ)
            # approximations:
            # linear:
            x1lin = numpy.linspace(-5.5 / 3.0, -3.5 / 3, 30)
            # x1 nullcline after linear approximation:
            # yc + Iext1 + 2.0 * x1lin0 ** 3 + 2.0 * x1lin0 ** 2 - \
            # (3.0 * x1lin0 ** 2 + 4.0 * x1lin0) * x1lin  # x1
            zX1lin = calc_fx1_2d_taylor(x1lin, x1lin0, z=0, y1=yc, Iext1=Iext1, slope=slope, a=a, b=b, d=d, tau1=1.0,
                                        x1_neg=None, order=2)  #
            # center point without approximation:
            # zlin0 = yc + Iext1 - x1lin0 ** 3 - 2.0 * x1lin0 ** 2
            # square:
            x1sq = numpy.linspace(-5.0 / 3, -1.0, 30)
            # x1 nullcline after parabolic approximation:
            # + 2.0 * x1sq ** 2 + 16.0 * x1sq / 3.0 + yc + Iext1 + 64.0 / 27.0
            zX1sq = calc_fx1_2d_taylor(x1sq, x1sq0, z=0, y1=yc, Iext1=Iext1, slope=slope, a=a, b=b, d=d, tau1=1.0,
                                       x1_neg=None, order=3, shape=x1sq.shape)
            sq, = pyplot.plot(x1sq, zX1sq, 'm--', label='Parabolic local approximation', linewidth=2)
            lin, = pyplot.plot(x1lin, zX1lin, 'c--', label='Linear local approximation', linewidth=2)
            pyplot.legend(handles=[x1null, zE1null, zE2null, lin, sq])
        else:
            pyplot.legend(handles=[x1null, zE1null, zE2null])

        # Points:
        ii = range(len(region_labels))
        n_special_idx = len(special_idx)
        if n_special_idx > 0:
            ii = numpy.delete(ii, special_idx)
        points = []
        for i in ii:
            point = pyplot.text(x1eq[i], zeq[i], str(i), fontsize=10, color='k', alpha=0.3,
                                 label=str(i) + '.' + region_labels[i])
            # point, = pyplot.plot(x1eq[i], zeq[i], '*', mfc='k', mec='k',
            #                      ms=10, alpha=0.3, label=str(i) + '.' + region_labels[i])
            points.append(point)
        if n_special_idx > 0:
            for i in special_idx:
                point = pyplot.text(x1eq[i], zeq[i], str(i), fontsize=10, color='r', alpha=0.8,
                                     label=str(i) + '.' + region_labels[i])
                # point, = pyplot.plot(x1eq[i], zeq[i], '*', mfc='r', mec='r', ms=10, alpha=0.8,
                #                      label=str(i) + '.' + region_labels[i])
                points.append(point)
        # ax.plot(x1lin0, zlin0, '*', mfc='r', mec='r', ms=10)
        # ax.axes.text(x1lin0 - 0.1, zlin0 + 0.2, 'e_values=0.0', fontsize=10, color='r')
        # ax.plot(x1sq0, zsq0, '*', mfc='m', mec='m', ms=10)
        # ax.axes.text(x1sq0, zsq0 - 0.2, 'e_values=1.0', fontsize=10, color='m')

        # Vector field
        X1, Z = numpy.meshgrid(numpy.linspace(-2.0, 1.0, 41), numpy.linspace(0.0, 6.0, 31), indexing='ij')
        if isequal_string(model, "2d"):
            y1 = yc
            x2 = 0.0
        else:
            y1 = calc_eq_y1(X1, yc, d=d)
            x2 = 0.0  # as a simplification for faster computation without important consequences
            # x2 = calc_eq_x2(Iext2, y2eq=None, zeq=X1, x1eq=Z, s=s)[0]
        fx1 = calc_fx1(X1, Z, y1=y1, Iext1=Iext1, slope=slope, a=a, b=b, d=d, tau1=tau1, x1_neg=None,
                       model=model, x2=x2)
        fz = calc_fz(X1, Z, x0=x0, tau1=tau1, tau0=tau0, zmode=zmode)
        C = numpy.abs(fx1) + numpy.abs(fz)
        pyplot.quiver(X1, Z, fx1, fz, C, edgecolor='k', alpha=.5, linewidth=.5)
        pyplot.contour(X1, Z, fx1, 0, colors='b', linestyles="dashed")

        ax.set_title("Epileptor states pace at the x1-z phase plane of the" + add_name)
        ax.axes.autoscale(tight=True)
        ax.axes.set_ylim([0.0, 6.0])
        ax.axes.set_xlabel('x1')
        ax.axes.set_ylabel('z')

        if self.config.figures.MOUSE_HOOVER:
            self.HighlightingDataCursor(points[0], formatter='{label}'.format, bbox=dict(fc='white'),
                                        arrowprops=dict(arrowstyle='simple', fc='white', alpha=0.5))

        if len(fig.get_label()) == 0:
            fig.set_label(figure_name)
        else:
            figure_name = fig.get_label().replace(": ", "_").replace(" ", "_").replace("\t", "_")

        self._save_figure(None, figure_name)
        self._check_show()
        return fig
Ejemplo n.º 27
0
def generate_cmdstan_fit_command(fitmethod,
                                 options,
                                 model_path,
                                 model_data_path,
                                 output_filepath,
                                 diagnostic_filepath,
                                 command_path=None):
    command = model_path
    if isequal_string(fitmethod, "sample"):
        command += " method=sample" ' \\' + "\n"
        for option in STAN_SAMPLE_OPTIONS.keys():
            command += "\t\t" + option + "=" + str(
                options[option]) + ' \\' + "\n"
        if isequal_string(options["algorithm"], "hmc"):
            for option in STAN_HMC_OPTIONS.keys():
                command += "\t\t\t\t" + option + "=" + str(
                    options[option]) + ' \\' + "\n"
            if isequal_string(options["engine"], "nuts"):
                command += "\t\t\t\t\t\tmax_depth=" + str(
                    options["max_depth"]) + ' \\' + "\n"
            elif isequal_string(options["engine"], "static"):
                command += "\t\t\t\t\t\tint_time=" + str(
                    options["int_time"]) + ' \\' + "\n"
    elif isequal_string(fitmethod, "variational"):
        command += " method=variational" ' \\' + "\n"
        for option in STAN_VARIATIONAL_OPTIONS.keys():
            # due to sort_dict, we know that algorithm is the first option
            command += "\t\t\t\t" + option + "=" + str(
                options[option]) + ' \\' + "\n"
    elif isequal_string(fitmethod, "optimize"):
        command += " method=optimize" ' \\' + "\n"
        for option in STAN_OPTIMIZE_OPTIONS.keys():
            command += "\t\t" + option + "=" + str(
                options[option]) + ' \\' + "\n"
        if (options["algorithm"].find("bfgs") >= 0):
            for option in STAN_BFGS_OPTIONS.keys():
                command += "\t\t\t\t" + option + "=" + str(
                    options[option]) + ' \\' + "\n"
    # + " data file=" + model_data_path
    elif isequal_string(fitmethod, "diagnose"):
        command += " method=diagnose" ' \\' + "\n" + "\t\ttest=gradient "
        for option in STAN_DIAGNOSE_TEST_GRADIENT_OPTIONS.keys():
            command += "\t\t\t\t" + +option + "=" + str(
                options[option]) + ' \\' + "\n"
    if isequal_string(fitmethod, "sample") or isequal_string(
            fitmethod, "variational"):
        command += "\t\tadapt" ' \\' + "\n"
        if isequal_string(fitmethod, "sample"):
            adapt_options = STAN_SAMPLE_ADAPT_OPTIONS
        else:
            adapt_options = STAN_VARIATIONAL_ADAPT_OPTIONS
        for option in adapt_options.keys():
            if option.find("iter") < 0:
                command += "\t\t\t\t" + option + "=" + str(
                    options[option]) + ' \\' + "\n"
            else:
                command += "\t\t\t\t" + "iter=" + str(
                    options[option]) + ' \\' + "\n"
    command += "\t\tdata file=" + model_data_path + ' \\' + "\n"
    command += "\t\tinit=" + str(options["init"]) + ' \\' + "\n"
    command += "\t\trandom seed=" + str(options["random_seed"]) + ' \\' + "\n"
    if diagnostic_filepath == "":
        diagnostic_filepath = os.path.join(
            os.path.dirname(output_filepath),
            STAN_OUTPUT_OPTIONS["diagnostic_file"])
    if options["n_chains_or_runs"] > 1:
        command = ("for i in {1.." + str(options["n_chains_or_runs"]) +
                   "}\ndo\n" + "\t" + command + "\t\tid=$i" + ' \\' + "\n" +
                   "\t\toutput file=" + output_filepath[:-4] + "$i.csv"
                   ' \\' + "\n" + "\t\tdiagnostic_file=" +
                   diagnostic_filepath[:-4] + "$i.csv"
                   ' \\' + "\n" + "\t\trefresh=" + str(options["refresh"]) +
                   " &" + "\n" + "done")
    else:
        command += "\t\toutput file=" + output_filepath + ' \\' + "\n"
        command += "\t\tdiagnostic_file=" + diagnostic_filepath + ' \\' + "\n"
        command += "\t\trefresh=" + str(options["refresh"])
    command = "chmod +x " + model_path + "\n" + command
    command = ''.join(command)
    if isinstance(command_path, basestring):
        command_path = os.path.join(os.path.dirname(output_filepath),
                                    "command.sh")
        command_file = open(command_path, "w")
        command_file.write("#!/bin/bash\n" + command.replace("\t", ""))
        command_file.close()
    return command, output_filepath, diagnostic_filepath