Skip to content

Commit

Permalink
removing pickle and changing to json
Browse files Browse the repository at this point in the history
  • Loading branch information
wiheto committed Jul 5, 2019
1 parent f6efad9 commit 4b9e83d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

- make_parcellation now uses templateflow and all atlases therein

- save_tenetobids_snapshot to export current teneto settings.
- save_tenetobids_snapshot to export current teneto settings. save_to_pickle (and corresponding load function) have been removed as they are not secure.

### Fixes

Expand Down
16 changes: 12 additions & 4 deletions docs/tutorial/tenetobids/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ Load/Save TenetoBIDS

TenetoBIDS objects can be easily saved as a pickle file (.pkl).

You can save you progress by using *save_pickle*.
You can save you progress by using *save_tenetobids_snapshot*.

.. code-block:: python
tnet.save_aspickle('./tenetoobj.pkl')
path = './'
tnet.save_tenetobids_snapshot(path)
Then to load it you just need to call *load_frompickle*:
This creates a json file called,
Then to load it you just need to create a new TenetoBIDS object using these paramerers.
All the history (in tnet.history) is also perserved:

.. code-block:: python
tnet = teneto.TenetoBIDS.load_frompickle('./tenetoobj.pkl')
import json
with open(path + 'TenetoBIDS_snapshot.json') as f
params = json.load(f)
tnet = teneto.TenetoBIDS(**params)
Note that any loaded files need to be reloaded.
35 changes: 1 addition & 34 deletions teneto/classes/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import inspect
import matplotlib.pyplot as plt
import json
import pickle
import nilearn
from concurrent.futures import ProcessPoolExecutor, as_completed
from scipy.interpolate import interp1d
Expand Down Expand Up @@ -1531,38 +1530,6 @@ def print_dataset_summary(self):
print('Numnber of selected files: ' + str(len(selected_files)))
print('\n - '.join(selected_files))

def save_aspickle(self, fname):
if fname[-4:] != '.pkl':
fname += '.pkl'
with open(fname, 'wb') as f:
pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)

@classmethod
def load_frompickle(cls, fname, reload_object=False):
"""
Loaded saved instance of
fname : str
path to pickle object (output of TenetoBIDS.save_aspickle)
reload_object : bool (default False)
reloads object by calling teneto.TenetoBIDS (some information lost, for development)
Returns
-------
self :
TenetoBIDS instance
"""
if fname[-4:] != '.pkl':
fname += '.pkl'
with open(fname, 'rb') as f:
tnet = pickle.load(f)
if reload_object:
reloadnet = teneto.TenetoBIDS(tnet.BIDS_dir, pipeline=tnet.pipeline, pipeline_subdir=tnet.pipeline_subdir, bids_tags=tnet.bids_tags, bids_suffix=tnet.bids_suffix,
bad_subjects=tnet.bad_subjects, confound_pipeline=tnet.confound_pipeline, raw_data_exists=tnet.raw_data_exists, njobs=tnet.njobs)
reloadnet.histroy = tnet.history
tnet = reloadnet
return tnet

# timelocked average
# np.stack(a['timelocked-tvc'].values).mean(axis=0)
# Remaked based on added meta data derived from events
Expand Down Expand Up @@ -1848,7 +1815,7 @@ def save_tenetobids_snapshot(self, path, filename='TenetoBIDS_snapshot'):
Notes
-----
To restart:
To load the snapshot:
import json
with open(path + 'TenetoBIDS_snapshot.json') as f
Expand Down
11 changes: 0 additions & 11 deletions test/tenetobids/tnet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,6 @@ def test_tnet_checksidecar():
raise AssertionError()


def test_tnet_io():
tnet = teneto.TenetoBIDS(teneto.__path__[0] + '/data/testdata/dummybids/', pipeline='fmriprep',
bids_suffix='preproc', bids_tags={'sub': '001', 'task': 'a', 'run': '01'}, raw_data_exists=False)
tnet.save_aspickle(teneto.__path__[0] +
'/data/testdata/dummybids/teneosave.pkl')
tnet2 = teneto.TenetoBIDS.load_frompickle(
teneto.__path__[0] + '/data/testdata/dummybids/teneosave.pkl')
if not tnet2.get_selected_files() == tnet.get_selected_files():
raise AssertionError()


def test_export_history():
tnet = teneto.TenetoBIDS(teneto.__path__[0] + '/data/testdata/dummybids/', pipeline='fmriprep',
bids_suffix='preproc', bids_tags={'sub': '001', 'task': 'a', 'run': '01'}, raw_data_exists=False)
Expand Down

0 comments on commit 4b9e83d

Please sign in to comment.