diff --git a/osl/glm/glm_epochs.py b/osl/glm/glm_epochs.py index d95b4e6..c0e431c 100644 --- a/osl/glm/glm_epochs.py +++ b/osl/glm/glm_epochs.py @@ -216,7 +216,40 @@ def get_fl_contrast(self, fl_con): ret_con.data = ret_con.data[:, fl_con, :, :] return ret_con + + def save_pkl(self, outname, overwrite=True, save_data=False): + """Save GLM-Epochs result to a pickle file. + + Parameters + ---------- + outname : str + Filename or full file path to write pickle to + overwrite : bool + Overwrite previous file if one exists? (Default value = True) + save_data : bool + Save epochs data in pickle? This is omitted by default to save disk + space (Default value = False) + """ + if Path(outname).exists() and not overwrite: + msg = "{} already exists. Please delete or do use overwrite=True." + raise ValueError(msg.format(outname)) + if hasattr(self, 'config'): + self.config.detrend_func = None # Have to drop this to pickle + + # This is hacky - but pickles are all or nothing and I don't know how + # else to do it. HDF5 would be better longer term + if save_data == False: + # Temporarily remove data before saving + dd = self.data + self.data = None + + with open(outname, 'bw') as outp: + pickle.dump(self, outp) + + # Put data back + if save_data == False: + self.data = dd #%% ------------------------------------------------------