@@ -61,6 +61,32 @@ def fpath(file_path, file_name):
61
61
return full_path
62
62
63
63
64
+ def get_files (file_path , select = None ):
65
+ """Get a list of files from a directory.
66
+
67
+ Parameters
68
+ ----------
69
+ file_path : Path or str
70
+ Name of the folder to get the list of files from.
71
+ select : str, optional
72
+ A search string to use to select files.
73
+
74
+ Returns
75
+ -------
76
+ list of str
77
+ A list of files.
78
+ """
79
+
80
+ # Get list of available files, and drop hidden files
81
+ files = os .listdir (file_path )
82
+ files = [file for file in files if file [0 ] != '.' ]
83
+
84
+ if select :
85
+ files = [file for file in files if select in file ]
86
+
87
+ return files
88
+
89
+
64
90
def save_model (model , file_name , file_path = None , append = False ,
65
91
save_results = False , save_settings = False , save_data = False ):
66
92
"""Save out data, results and/or settings from a model object into a JSON file.
@@ -130,7 +156,7 @@ def save_group(group, file_name, file_path=None, append=False,
130
156
file_name : str or FileObject
131
157
File to save data to.
132
158
file_path : Path or str, optional
133
- Path to directory to load from. If None, loads from current directory.
159
+ Path to directory to load from. If None, saves to current directory.
134
160
append : bool, optional, default: False
135
161
Whether to append to an existing file, if available.
136
162
This option is only valid (and only used) if 'file_name' is a str.
@@ -168,6 +194,48 @@ def save_group(group, file_name, file_path=None, append=False,
168
194
raise ValueError ("Save file not understood." )
169
195
170
196
197
+ def save_event (event , file_name , file_path = None , append = False ,
198
+ save_results = False , save_settings = False , save_data = False ):
199
+ """Save out results and/or settings from event object. Saves out to a JSON file.
200
+
201
+ Parameters
202
+ ----------
203
+ event : SpectralTimeEventModel
204
+ Object to save data from.
205
+ file_name : str or FileObject
206
+ File to save data to.
207
+ file_path : str, optional
208
+ Path to directory to load from. If None, saves to current directory.
209
+ append : bool, optional, default: False
210
+ Whether to append to an existing file, if available.
211
+ This option is only valid (and only used) if 'file_name' is a str.
212
+ save_results : bool, optional
213
+ Whether to save out model fit results.
214
+ save_settings : bool, optional
215
+ Whether to save out settings.
216
+ save_data : bool, optional
217
+ Whether to save out power spectra data.
218
+
219
+ Raises
220
+ ------
221
+ ValueError
222
+ If the data or save file specified are not understood.
223
+ """
224
+
225
+ fg = event .get_group (None , None , 'group' )
226
+ if save_settings and not save_results and not save_data :
227
+ fg .save (file_name , file_path , append = append , save_settings = True )
228
+ else :
229
+ ndigits = len (str (len (event )))
230
+ for ind , gres in enumerate (event .event_group_results ):
231
+ fg .group_results = gres
232
+ if save_data :
233
+ fg .power_spectra = event .spectrograms [ind , :, :].T
234
+ fg .save (file_name + '_{:0{ndigits}d}' .format (ind , ndigits = ndigits ),
235
+ file_path = file_path , append = append , save_results = save_results ,
236
+ save_settings = save_settings , save_data = save_data )
237
+
238
+
171
239
def load_json (file_name , file_path ):
172
240
"""Load json file.
173
241
0 commit comments