def run_subject(sub, maskname="graymatter-bin_mask", settings=projectSettings, options=analysisSettings, logger=None): mask = pu.load_img(paths["root"], "derivatives", sub, "masks", "{}.nii.gz".format(maskname)) fmri_data, labels, bg_image = load_betas(settings, sub, t="cope-LSS", center=True, scale=False, logger=logger) selector = labels[labels.AB == 1].sort_values(["SubRel", "TrialTag"]) fmri_data = pu.index_img(fmri_data, selector.index) options["searchlight"]["rdm_metric"] = "correlation" results = pa.searchlight_rsa( fmri_data, model_rdms[(model_rdms.type == "full")].dropna(axis=1).values[:, 2:], m=mask, write=False, logger=logger, **options["searchlight"]) pu.data_to_img(results.scores_, bg_image).to_filename( os.path.join(paths["root"], "analysis", sub, "multivariate", "rsa", "{}_{}_AB_corr_rsaSearchlight.nii.gz".format(sub, maskname))) return results
def select_data(self, phase="AB", equals=True, val=1): # will be a little more complex if equals: self.selector = self.labels[self.labels[phase] == val].sort_values(["SubRel", "TrialTag"]) else: self.selector = self.labels[self.labels[phase] != val] self.fmri_data = pu.index_img(self.fmri_data, self.selector.index) self.labels = self.labels.loc[self.selector.index]
def select_data(self, phase="AB", equals=True, val=1): if phase.upper() == "CDMATCH": self.selector = self.labels[(self.labels["CD"] == 1) & (self.labels["Match"] == "1")] else: if equals: self.selector = self.labels[self.labels[phase] == val] else: self.selector = self.labels[self.labels[phase] != val] self.fmri_data = pu.index_img(self.fmri_data, self.selector.index) self.labels = self.labels.loc[self.selector.index]
def run_subject(sub, maskname="graymatter-bin_mask", settings=projectSettings, options=analysisSettings, logger=None): mask = pu.load_img(paths["root"], "derivatives", sub, "masks", "{}.nii.gz".format(maskname), logger=logger) fmri_data, labels, bg_image = load_betas(settings, sub, t="cope-LSS", logger=logger) selector = labels[(labels.CD == 1) & (labels.Match == "1")] fmri_data = pu.index_img(fmri_data, selector.index) options["searchlight"]["estimator"] = Pipeline(steps=[ # ( # "variance_threshold", # VarianceThreshold()), ("scaling", StandardScaler()), # ("feature_select", SelectPercentile(f_classif, percentile=20)), # ("feature_select", SelectKBest(f_classif, k=100)), ("svm", LinearSVC(C=0.05)) # ("plr", LogisticRegression(C=0.05, penalty="l1", tol=0.01)) ]) # analysis # output order: CD2AB, AB2CD cv = LeaveOneGroupOut() result = pa.searchlight(fmri_data, selector.CDMainRel, m=mask, cv=cv, groups=selector['chunks'], write=False, logger=logger, **options['searchlight']) outpath = os.path.join(paths["root"], "analysis", sub, "multivariate", "searchlight", "lss", "{}_CDMatch-MainRel-cvsl.nii.gz".format(sub)) pu.data_to_img(result.scores_, bg_image, logger=logger).to_filename(outpath) return result
def main(_): import getopt # every possible variable roi = FLAGS.mask sub = FLAGS.sub phase = FLAGS.phase debug = FLAGS.debug analysis = FLAGS.analysis if FLAGS.jobs: analysisSettings["searchlight"]["n_jobs"] = int(FLAGS.jobs) if FLAGS.verbosity: analysisSettings["searchlight"]["verbose"] = int(FLAGS.verbose) if FLAGS.radius: analysisSettings["searchlight"]["radius"] = int(FLAGS.radius) # if not con: # print "not a valid contrast... exiting" # sys.exit(1) if None in [sub, roi]: print("Argument missing") sys.exit(2) logger = pu.setup_logger(os.path.join(paths['root'], 'analysis', sub, 'multivariate', 'searchlight', "lss"), fname="{}_{}-Match.log".format(roi, FLAGS.phase)) mask_file = os.path.join(paths["root"], "derivatives", sub, "masks", "{}.nii.gz".format(roi)) if analysis == "cvsl": pu.write_to_logger("Running MVPA", logger) sl = CVSearchlight(sub, mask_file, phase=phase, settings=analysisSettings["searchlight"], logger=logger, permutations=FLAGS.permutations) slargs = {} elif analysis == "rsa": pu.write_to_logger("Running RSA", logger) raw_models_df = pu.load_labels( os.path.join(paths["code"], "labels/raw_models.csv")) sl = RSASearchlight(sub, mask_file, phase=phase, settings=analysisSettings["searchlight"], logger=logger) if FLAGS.phase == "CD": sl.fmri_data = pu.index_img(sl.fmri_data, sl.selector.Match == '1') sl.labels = sl.labels[sl.selector.Match == '1'] sl.selector = sl.labels modelrdms, modelnames = get_cd_models(raw_models_df, sl.labels) else: modelnames = [ "mainrel", "rel", "numchar", "humanratings", "typicality", "w2vdiff", "concatword", "accuracy", "rstpostprob9", "rstpostprob79", "bart79thresh", "rstpostprob270", "bartregressacc", "accregressbart" ] model_rdms = get_model_rdms(raw_models_df, modelnames) # modelrdms = model_rdms[(model_rdms.type == "full")].dropna(axis=1).values[:, 2:].astype(np.float64) # deal with NAs in bart thresh rdms = model_rdms[model_rdms.type == "full"] bart = rdms[rdms.name == "rstpostprob79"].iloc[:, 2:].fillna(0) acc = rdms[rdms.name == "accuracy"].iloc[:, 2:].fillna(0) bart_regress_acc = sm.OLS(bart.T, acc.T).fit().resid acc_regress_bart = sm.OLS(acc.T, bart.T).fit().resid modelrdms = model_rdms[( model_rdms.type == "full")].iloc[:, 2:].fillna(0).values.astype( np.float64) modelrdms = np.concatenate( [modelrdms, np.vstack([bart_regress_acc, acc_regress_bart])], axis=0) slargs = {"modelrdms": modelrdms} else: pu.write_to_logger("wrong analysis specified, exiting...", logger) sys.exit(2) if not debug: _ = sl.run(**slargs) pu.write_to_logger("Session ended at " + str(datetime.now()), logger=logger)