コード例 #1
0
ファイル: base.py プロジェクト: parenthetical-e/wheelerexp
    def run(self, basename, roi, cond, smooth=False, filtfile=None, event=False):
        # Save here....
        table = join_by_underscore(False, basename, roi, cond)

        # and reinit write flags.
        roicount = 0
        mode = 'w'
        header = True

        # Getting to work, find subjects data
        paths = self.data.get_roi_data_paths(roi)
        if not event:
            metas = self.data.get_metapaths_containing(cond)
        else:
            metas = self.data.get_RT_metadata_event_paths()

        # And decompose it
        for path, meta in zip(paths, metas):
            roiname = get_roiname(path)
            print("\t{0}".format(roiname))  ## ...progress

            # If were past the first Ss data, append.
            if roicount > 0:
                mode = 'a'
                header = False

            # Get data
            targets = csv_to_targets(meta)

            X = load_nii(path, clean=True, sparse=False, smooth=smooth)
            targets = tr_pad_targets(targets, "TR", X.shape[0], pad=np.nan)

            # Preprocess labels
            if filtfile is not None:
                targets = reprocess_targets(filtfile, targets, np.nan)
                assert targets["TR"].shape[0] == X.shape[0], ("target" 
                    "reprocessing is broken")
            
            
            norm = MinMaxScaler((0,1))
            X = norm.fit_transform(X.astype(np.float))
            
            Xcs, csnames, ti_cs = self.spacetime.fit_transform(
                    X, targets[cond], targets["trialcount"], 
                    self.window, self.tr)

            # and write.
            dataname = join_by_underscore(False, roiname)
            known = []
            for Xc, csname, ti in zip(Xcs, csnames, ti_cs):
                if not csname in known:
                    save_tcdf(
                            name=join_by_underscore(True, table, csname), 
                            X=Xc, 
                            cond=csname,
                            dataname=dataname,
                            index=ti.astype(np.int),
                            header=header, 
                            mode=mode,
                            float_format="%.{0}f".format(self.nsig))
                    known.append(csname)
                else:
                    save_tcdf(
                            name=join_by_underscore(True, table, csname), 
                            X=Xc, 
                            cond=csname,
                            dataname=dataname,
                            index=ti.astype(np.int),
                            header=False, 
                            mode='a',
                            float_format="%.{0}f".format(self.nsig))
            roicount += 1
コード例 #2
0
ファイル: base.py プロジェクト: parenthetical-e/wheelerexp
    def run(self, basename, cond, index, wheelerdata, cond_to_rt, 
        smooth=False,
        filtfile=None, TR=2, trname="TR", 
        n_features=10, n_univariate=None, n_accumulator=None, n_decision=None, 
        n_noise=None, drift_noise=False, step_noise=False, z_noise=False,
        drift_noise_param=None, step_noise_param=None, z_noise_param=None,
        noise_f=white, hrf_f=None, hrf_params=None, prng=None):       
        """Reproduce the cond from the wheelerdata experiment
        
        Parameters
        ---------
        basename : str
            The name for the Reproduced datafile, will be suffixed
            by each cond and scode and .csv 
            (i.e. `'{0}_{1}_{2}.csv'.format(basename, cond, scode)`).
        cond : str
            A condition name found in the wheelerdata objects metadata
        index : str
            A name of a trial index found in the wheelerdata object metadata
        wheelerdata : object, instance of Wheelerdata
            A Wheelerdata object
        cond_to_rt: dict
            A map of cond (key) to reaction time (item, (int, float))    
        smooth : boolean, optional
            Do bandpass filtering (default False)
        filtfile : str, None
            A name of json file designed for reprocessing Wheelerdata metadata
        TR : float, int
            The repitition time of the experiement
        trname : str
            The name of the index of TRs in the metadata
        n_features : int
            The number of features in total (other n_* arguements
            must sum to this value
        n_univariate : int
            The number of univariate (boxcar) features
        n_accumulator : int
            The number of accumulator features
        n_decision : int
            The number of decision features
        n_noise : int
            The number of noise features
        drift_noise : boolean, optional
            Add noise to the drift rate of the accumulator features
        step_noise : boolean, optional
            Add Noise to each step accumulator features
        z_noise : boolean, optional
            Add noise to the start value of accumulator features
        drift_noise_param : None or dict, optional
            Parameters for drift_noise which is drawn from a
            Gaussian distribution. None defaults to: 
            `{"loc": 0, "scale" : 0.5}`
        step_noise_param : None or dict, optional
            Parameters for step_noise which is drawn from a 
            Gaussian distribution. None defaults to:
            `{"loc" : 0, "scale" : 0.2, "size" : 1}`
        z_noise_param : None or dict, optional
            Parameters for z_noise which is drawn from the uniform
            distribution. None defaults to:
            `{"low" : 0.01, "high" : 0.5, "size" : 1}`
        noise_f : function, optional
            Produces noise, must have signatures like `noise, prng = f(N, prng)`
        hrf_f : function, optional
            Returns a haemodynamic response, signature hrf_f(**hrf_params)
        hrf_params : dict
            Keyword parameters for hrf_f
        prng : None or RandomState object
            Allows for independent random draws, used for all 
            random sampling
        """

        mode = 'w'
        header = True

        # All *s lists correspond to wheelerdata.scodes
        scodes = self.data.scodes
        Xs, ys, yindices = make_bold_re(
                cond, index, self.data,
                cond_to_rt,
                filtfile=filtfile, 
                trname=trname,
                noise_f=noise_f, 
                hrf_f=hrf_f, 
                hrf_params=hrf_params, 
                n_features=n_features, 
                n_univariate=n_univariate, 
                n_accumulator=n_accumulator, 
                n_decision=n_decision, 
                n_noise=n_noise, 
                drift_noise=drift_noise, 
                step_noise=step_noise, 
                z_noise=z_noise,
                drift_noise_param=drift_noise_param, 
                step_noise_param=step_noise_param, 
                z_noise_param=z_noise_param,
                prng=prng)
        
        for scode, X, y, yindex in zip(scodes, Xs, ys, yindices):
            if smooth:
                X = smoothfn(X, tr=1.5, ub=0.10, lb=0.001)
            
            # Normalize
            norm = MinMaxScaler((0,1))
            X = norm.fit_transform(X.astype(np.float))
            
            Xcs, csnames, ti_cs = self.spacetime.fit_transform(
                    X, y, yindex, self.window, self.tr)

            # Name them,
            csnames = unique_nan(y)
            csnames = sort_nanfirst(csnames)

            # and write.
            for Xc, csname, ti in zip(Xcs, csnames, ti_cs):
                save_tcdf(
                        name=join_by_underscore(True, basename, csname), 
                        X=Xc, 
                        cond=csname,
                        dataname=join_by_underscore(False, 
                                os.path.split(basename)[-1], scode),
                        index=ti.astype(np.int),
                        header=header, 
                        mode=mode,
                        float_format="%.{0}f".format(self.nsig))
            
            # After s 1 go to append mode
            mode = 'a'
            header = False
コード例 #3
0
ファイル: base.py プロジェクト: parenthetical-e/wheelerexp
    def run(self, basename, smooth=False, filtfile=None, 
        n=None, tr=None, n_rt=None, n_trials_per_cond=None,
        durations=None ,noise=None, n_features=None, n_univariate=None, 
        n_accumulator=None, n_decision=None, n_noise=None, 
        n_repeated=None, drift_noise=False, step_noise=False):
        
        # Write init
        mode = 'w'
        header = True

        for scode in range(n):
            # If were past the first Ss data, append.
            if scode > 0:
                mode = 'a'
                header = False

            # Create the data
            X, y, y_trialcount = make_bold(
                    n_rt, 
                    n_trials_per_cond, 
                    tr, 
                    durations=durations, 
                    noise=noise, 
                    n_features=n_features, 
                    n_univariate=n_univariate, 
                    n_accumulator=n_accumulator, 
                    n_decision=n_decision,
                    n_noise=n_noise,
                    n_repeated=n_repeated,
                    drift_noise=drift_noise,
                    step_noise=step_noise)

            targets = construct_targets(trial_index=y_trialcount, y=y)

            # Drop baseline trials created by make_bold
            baselinemask = np.arange(y.shape[0])[y != 0]
            X = X[baselinemask, ]
            targets = filter_targets(baselinemask, targets)

            # Filter and
            if filtfile is not None:
                X, targets = filterX(filtfile, X, targets)
            if smooth:
                X = smoothfn(X, tr=1.5, ub=0.10, lb=0.001)
            
            # Normalize
            norm = MinMaxScaler((0,1))
            X = norm.fit_transform(X.astype(np.float))
            
            # finally decompose.
            Xcs, csnames, ti_cs = self.spacetime.fit_transform(
                    X, targets["y"], targets["trial_index"], 
                    self.window)
            
            # Name them,
            csnames = unique_nan(y)
            csnames = sort_nanfirst(csnames)

            # and write.
            for Xc, csname, ti in zip(Xcs, csnames, ti_cs):
                save_tcdf(
                        name=join_by_underscore(True, basename, csname), 
                        X=Xc, 
                        cond=csname,
                        dataname=join_by_underscore(False, 
                                os.path.split(basename)[-1], scode),
                        index=ti.astype(np.int),
                        header=header, 
                        mode=mode,
                        float_format="%.{0}f".format(self.nsig))