@@ -673,7 +673,7 @@ def dump_to_json(
673673 ) -> None :
674674 """
675675 Dump recording extractor to json file.
676- The extractor can be re-loaded with load_extractor (json_file)
676+ The extractor can be re-loaded with load (json_file)
677677
678678 Parameters
679679 ----------
@@ -715,7 +715,7 @@ def dump_to_pickle(
715715 ):
716716 """
717717 Dump recording extractor to a pickle file.
718- The extractor can be re-loaded with load_extractor (pickle_file)
718+ The extractor can be re-loaded with load (pickle_file)
719719
720720 Parameters
721721 ----------
@@ -752,7 +752,9 @@ def dump_to_pickle(
752752 file_path .write_bytes (pickle .dumps (dump_dict ))
753753
754754 @staticmethod
755- def load (file_path : Union [str , Path ], base_folder : Optional [Union [Path , str , bool ]] = None ) -> "BaseExtractor" :
755+ def load (
756+ file_or_folder_path : Union [str , Path ], base_folder : Optional [Union [Path , str , bool ]] = None
757+ ) -> "BaseExtractor" :
756758 """
757759 Load extractor from file path (.json or .pkl)
758760
@@ -761,74 +763,10 @@ def load(file_path: Union[str, Path], base_folder: Optional[Union[Path, str, boo
761763 * save (...) a folder which contain data + json (or pickle) + metadata.
762764
763765 """
764- error_msg = (
765- f"{ file_path } is not a file or a folder. It should point to either a json, pickle file or a "
766- "folder that is the result of extractor.save(...)"
767- )
768- if not is_path_remote (file_path ):
769- file_path = Path (file_path )
770-
771- if base_folder is True :
772- base_folder = file_path .parent
773-
774- if file_path .is_file ():
775- # standard case based on a file (json or pickle)
776- if str (file_path ).endswith (".json" ):
777- with open (file_path , "r" ) as f :
778- d = json .load (f )
779- elif str (file_path ).endswith (".pkl" ) or str (file_path ).endswith (".pickle" ):
780- with open (file_path , "rb" ) as f :
781- d = pickle .load (f )
782- else :
783- raise ValueError (error_msg )
784-
785- if "warning" in d :
786- print ("The extractor was not serializable to file" )
787- return None
788-
789- extractor = BaseExtractor .from_dict (d , base_folder = base_folder )
790-
791- elif file_path .is_dir ():
792- # case from a folder after a calling extractor.save(...)
793- folder = file_path
794- file = None
795-
796- if folder .suffix == ".zarr" :
797- from .zarrextractors import read_zarr
798-
799- extractor = read_zarr (folder )
800- else :
801- # For backward compatibility (v<=0.94) we check for the cached.json/pkl/pickle files
802- # In later versions (v>0.94) we use the si_folder.json file
803- for dump_ext in ("json" , "pkl" , "pickle" ):
804- f = folder / f"cached.{ dump_ext } "
805- if f .is_file ():
806- file = f
807-
808- f = folder / f"si_folder.json"
809- if f .is_file ():
810- file = f
811-
812- if file is None :
813- raise ValueError (error_msg )
814- extractor = BaseExtractor .load (file , base_folder = folder )
815-
816- else :
817- raise ValueError (error_msg )
818- else :
819- # remote case - zarr
820- if str (file_path ).endswith (".zarr" ):
821- from .zarrextractors import read_zarr
822-
823- extractor = read_zarr (file_path )
824- else :
825- raise NotImplementedError (
826- "Only zarr format is supported for remote files and you should provide a path to a .zarr "
827- "remote path. You can save to a valid zarr folder using: "
828- "`extractor.save(folder='path/to/folder', format='zarr')`"
829- )
766+ # use loading.py and keep backward compatibility
767+ from .loading import load
830768
831- return extractor
769+ return load ( file_or_folder_path , base_folder = base_folder )
832770
833771 def __reduce__ (self ):
834772 """
@@ -1179,50 +1117,6 @@ def _check_same_version(class_string, version):
11791117 return "unknown"
11801118
11811119
1182- def load_extractor (file_or_folder_or_dict , base_folder = None ) -> BaseExtractor :
1183- """
1184- Instantiate extractor from:
1185- * a dict
1186- * a json file
1187- * a pickle file
1188- * folder (after save)
1189- * a zarr folder (after save)
1190-
1191- Parameters
1192- ----------
1193- file_or_folder_or_dict : dictionary or folder or file (json, pickle)
1194- The file path, folder path, or dictionary to load the extractor from
1195- base_folder : str | Path | bool (optional)
1196- The base folder to make relative paths absolute.
1197- If True and file_or_folder_or_dict is a file, the parent folder of the file is used.
1198-
1199- Returns
1200- -------
1201- extractor: Recording or Sorting
1202- The loaded extractor object
1203- """
1204- if isinstance (file_or_folder_or_dict , dict ):
1205- assert not isinstance (base_folder , bool ), "`base_folder` must be a string or Path when loading from dict"
1206- return BaseExtractor .from_dict (file_or_folder_or_dict , base_folder = base_folder )
1207- else :
1208- return BaseExtractor .load (file_or_folder_or_dict , base_folder = base_folder )
1209-
1210-
1211- def load_extractor_from_dict (d , base_folder = None ) -> BaseExtractor :
1212- warnings .warn ("Use load_extractor(..) instead" )
1213- return BaseExtractor .from_dict (d , base_folder = base_folder )
1214-
1215-
1216- def load_extractor_from_json (json_file , base_folder = None ) -> "BaseExtractor" :
1217- warnings .warn ("Use load_extractor(..) instead" )
1218- return BaseExtractor .load (json_file , base_folder = base_folder )
1219-
1220-
1221- def load_extractor_from_pickle (pkl_file , base_folder = None ) -> "BaseExtractor" :
1222- warnings .warn ("Use load_extractor(..) instead" )
1223- return BaseExtractor .load (pkl_file , base_folder = base_folder )
1224-
1225-
12261120class BaseSegment :
12271121 def __init__ (self ):
12281122 self ._parent_extractor = None
0 commit comments