Skip to content

Commit

Permalink
generalize custom object IO
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed May 26, 2024
1 parent 3c86871 commit 8625d8a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
35 changes: 20 additions & 15 deletions convnwb/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import yaml

#from convnwb.objects import Task, Bundle, Electrodes
from convnwb.io.utils import get_files, check_ext, check_folder, make_session_name
from convnwb.modutils.dependencies import safe_import, check_dependency

Expand Down Expand Up @@ -142,29 +143,33 @@ def load_configs(files, folder=None):

### TASK OBJECTS

def save_task_object(task, file_name, folder=None):
"""Save a task object.
def save_object(custom_object, file_name, folder=None):
"""Save a custom object.
Parameters
----------
task : Task
Task object to save out.
custom_object : Task or Electrodes
Object to save out.
file_name : str
File name to give the saved out task file.
File name to give the saved out object.
folder : str or Path, optional
Folder to save out to.
Notes
-----
Task objects are saved and loaded as pickle files.
Custom objects are saved and loaded as pickle files.
"""

with open(check_ext(check_folder(file_name, folder), '.task'), 'wb') as fobj:
pickle.dump(task, fobj)
ext = '.' + str(type(custom_object)).split('.')[-1].strip("'>").lower()
if 'task' in ext:
ext = '.task'

with open(check_ext(check_folder(file_name, folder), ext), 'wb') as fobj:
pickle.dump(custom_object, fobj)

def load_task_object(file_name, folder=None):
"""Load a task object.

def load_object(file_name, folder=None):
"""Load a custom object.
Parameters
----------
Expand All @@ -175,18 +180,18 @@ def load_task_object(file_name, folder=None):
Returns
-------
task
custom_object
Loaded task object.
Notes
-----
Task objects are saved and loaded as pickle files.
Custom objects are saved and loaded as pickle files.
"""

with open(check_ext(check_folder(file_name, folder), '.task'), 'rb') as load_obj:
task = pickle.load(load_obj)
with open(check_folder(file_name, folder), 'rb') as load_obj:
custom_object = pickle.load(load_obj)

return task
return custom_object


## OTHER FILE I/O
Expand Down
1 change: 0 additions & 1 deletion convnwb/tests/io/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import pandas as pd

from convnwb.tests.tobjects import TestTask
from convnwb.tests.tsettings import TEST_FILE_PATH

from convnwb.io.check import *
Expand Down
29 changes: 21 additions & 8 deletions convnwb/tests/io/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import pandas as pd

from convnwb.tests.tobjects import TestTask
from convnwb.tests.tsettings import TEST_FILE_PATH

from convnwb.io.io import *
Expand Down Expand Up @@ -51,20 +50,34 @@ def test_load_configs():
configs = load_configs(f_names, TEST_FILE_PATH)
assert isinstance(configs, dict)

def test_save_task_object():
def test_save_object(ttask, tbundle, telectrodes):

task = TestTask()
f_name = 'task_obj'
save_task_object(task, f_name, TEST_FILE_PATH)

save_object(ttask, f_name, TEST_FILE_PATH)
assert os.path.exists(TEST_FILE_PATH / (f_name + '.task'))

def test_load_task_object():
f_name = 'bundle_obj'
save_object(tbundle, f_name, TEST_FILE_PATH)
assert os.path.exists(TEST_FILE_PATH / (f_name + '.bundle'))

f_name = 'task_obj'
task = load_task_object(f_name, TEST_FILE_PATH)
f_name = 'electrodes_obj'
save_object(telectrodes, f_name, TEST_FILE_PATH)
assert os.path.exists(TEST_FILE_PATH / (f_name + '.electrodes'))

def test_load_object():

f_name = 'task_obj.task'
task = load_object(f_name, TEST_FILE_PATH)
assert task

f_name = 'bundle_obj.bundle'
bundle = load_object(f_name, TEST_FILE_PATH)
assert bundle

f_name = 'electrodes_obj.electrodes'
electrodes = load_object(f_name, TEST_FILE_PATH)
assert electrodes

def test_save_txt():

text = "Words, words, words."
Expand Down
4 changes: 2 additions & 2 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ NWB & Custom file I/O
save_config
load_config
load_configs
save_task_object
load_task_object
save_object
load_object

General file I/O
~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 8625d8a

Please sign in to comment.