PyOPIA 2.0.4__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PyOPIA
3
- Version: 2.0.4
3
+ Version: 2.1.0
4
4
  Summary: A Python Ocean Particle Image Analysis toolbox.
5
5
  Home-page: https://github.com/sintef/pyopia
6
6
  Keywords: Ocean,Particles,Imaging,Measurement,Size distribution
@@ -0,0 +1 @@
1
+ __version__ = '2.1.0'
@@ -147,6 +147,23 @@ def process(config_filename: str):
147
147
  logger.debug(''.join(traceback.format_tb(e.__traceback__)))
148
148
 
149
149
 
150
+ @app.command()
151
+ def merge_mfdata(path_to_data: str, prefix='*'):
152
+ '''Combine a multi-file directory of STATS.nc files into a single '-STATS.nc' file
153
+ that can then be loaded with {func}`pyopia.io.load_stats`
154
+
155
+ Parameters
156
+ ----------
157
+ path_to_data : str
158
+ Folder name containing nc files with pattern '*Image-D*-STATS.nc'
159
+
160
+ prefix : str
161
+ Prefix to multi-file dataset (for replacing the wildcard in '*Image-D*-STATS.nc').
162
+ Defaults to '*'
163
+ '''
164
+ pyopia.io.merge_and_save_mfdataset(path_to_data, prefix=prefix)
165
+
166
+
150
167
  def setup_logging(pipeline_config):
151
168
  '''Configure logging
152
169
 
@@ -36,7 +36,7 @@ def load_image(filename):
36
36
  array
37
37
  raw image
38
38
  '''
39
- img = np.load(filename, allow_pickle=False).astype(np.uint8)
39
+ img = np.load(filename, allow_pickle=False).astype(np.float64)
40
40
  return img
41
41
 
42
42
 
@@ -10,6 +10,7 @@ import toml
10
10
  import xarray
11
11
  import os
12
12
  from glob import glob
13
+ import xarray as xr
13
14
 
14
15
  from pyopia import __version__ as pyopia_version
15
16
 
@@ -32,15 +33,17 @@ def write_stats(stats,
32
33
  Appends if file already exists.
33
34
 
34
35
  Args:
35
- datafilename (str): filame prefix for -STATS.h5 file that may or may not include a path
36
- stats_all (DataFrame): stats dataframe returned from processImage()
37
- export_name_len (int): max number of chars allowed for col 'export name'
38
- append (bool): Append all processed data into one nc file.
39
- Defaults to True.
40
- If False, then one nc file will be generated per raw image,
41
- which can be loaded using :func:`pyopia.io.combine_stats_netcdf_files`
42
- This is useful for larger datasets, where appending causes substantial slowdown
43
- as the dataset gets larger.
36
+ datafilename (str) : Filame prefix for -STATS.h5 file that may or may not include a path
37
+ stats (DataFrame or xr.Dataset) : STATS dataframe
38
+ export_name_len (int) : Max number of chars allowed for col 'export name'
39
+ append (bool) : Append all processed data into one nc file.
40
+ Defaults to True.
41
+ If False, then one nc file will be generated per raw image,
42
+ which can be loaded using :func:`pyopia.io.combine_stats_netcdf_files`
43
+ This is useful for larger datasets,
44
+ where appending causes substantial slowdown
45
+ as the dataset gets larger.
46
+ image_stats (xr.Dataset) : image_stats data
44
47
  '''
45
48
 
46
49
  if len(stats) == 0: # to avoid issue with wrong time datatypes in xarray
@@ -64,7 +67,12 @@ def write_stats(stats,
64
67
  meta.attrs['PyOpia version'] = pyopia_version
65
68
  meta.attrs['Pipeline steps'] = settings
66
69
  elif dataformat == 'nc':
67
- xstats = make_xstats(stats, settings)
70
+
71
+ if isinstance(stats, xr.Dataset):
72
+ xstats = stats
73
+ else:
74
+ xstats = make_xstats(stats, settings)
75
+
68
76
  if append and os.path.isfile(datafilename + '-STATS.nc'):
69
77
  existing_stats = load_stats(datafilename + '-STATS.nc')
70
78
  xstats = xarray.concat([existing_stats, xstats], 'index')
@@ -147,7 +155,7 @@ def load_stats(datafilename):
147
155
  return stats
148
156
 
149
157
 
150
- def combine_stats_netcdf_files(path_to_data):
158
+ def combine_stats_netcdf_files(path_to_data, prefix='*'):
151
159
  '''Combine a multi-file directory of STATS.nc files into a 'stats' xarray dataset created by :func:`pyopia.io.write_stats`
152
160
  when using 'append = false'
153
161
 
@@ -156,14 +164,20 @@ def combine_stats_netcdf_files(path_to_data):
156
164
  path_to_data : str
157
165
  Folder name containing nc files with pattern '*Image-D*-STATS.nc'
158
166
 
167
+ prefix : str
168
+ Prefix to multi-file dataset (for replacing the wildcard in '*Image-D*-STATS.nc').
169
+ Defaults to '*'
170
+
159
171
  Returns
160
172
  -------
161
- DataFrame
162
- STATS xarray dataset
173
+ tuple
174
+ xstats STATS xarray dataset, image_stats dataset
163
175
  '''
164
176
 
165
- sorted_filelist = sorted(glob(os.path.join(path_to_data, '*Image-D*-STATS.nc')))
166
- with xarray.open_mfdataset(sorted_filelist, combine='nested', concat_dim='index') as ds:
177
+ sorted_filelist = sorted(glob(os.path.join(path_to_data, prefix + 'Image-D*-STATS.nc')))
178
+ with xarray.open_mfdataset(sorted_filelist, combine='nested', concat_dim='index',
179
+ decode_cf=False, parallel=False,
180
+ coords='minimal', compat='override') as ds:
167
181
  xstats = ds.load()
168
182
 
169
183
  # Check if we have image statistics in the last file, if so, load it.
@@ -180,6 +194,53 @@ def combine_stats_netcdf_files(path_to_data):
180
194
  return xstats, image_stats
181
195
 
182
196
 
197
+ def merge_and_save_mfdataset(path_to_data, prefix='*'):
198
+ '''Combine a multi-file directory of STATS.nc files into a single '-STATS.nc' file
199
+ that can then be loaded with {func}`pyopia.io.load_stats`
200
+
201
+ Parameters
202
+ ----------
203
+ path_to_data : str
204
+ Folder name containing nc files with pattern '*Image-D*-STATS.nc'
205
+
206
+ prefix : str
207
+ Prefix to multi-file dataset (for replacing the wildcard in '*Image-D*-STATS.nc').
208
+ Defaults to '*'
209
+ '''
210
+
211
+ logging.info(f'combine stats netcdf files from {path_to_data}')
212
+ xstats, image_stats = combine_stats_netcdf_files(path_to_data, prefix=prefix)
213
+
214
+ settings = steps_from_xstats(xstats)
215
+
216
+ prefix_out = os.path.basename(settings['steps']['output']['output_datafile'])
217
+ output_name = os.path.join(path_to_data, prefix_out)
218
+
219
+ logging.info(f'writing {output_name}')
220
+ write_stats(xstats,
221
+ output_name,
222
+ settings,
223
+ image_stats=image_stats)
224
+ logging.info(f'writing {output_name} done.')
225
+
226
+
227
+ def steps_from_xstats(xstats):
228
+ '''Get the steps attribute from xarray version of the particle stats into a dictionary
229
+
230
+ Parameters
231
+ ----------
232
+ xstats : xarray.DataSet
233
+ xarray version of the particle stats dataframe, containing metadata
234
+
235
+ Returns
236
+ -------
237
+ dict
238
+ TOML-formatted dictionary of pipeline steps
239
+ '''
240
+ steps = toml.loads(xstats.__getattr__('steps'))
241
+ return steps
242
+
243
+
183
244
  def load_stats_as_dataframe(stats_file):
184
245
  '''A loading function for stats files that forces stats into a pandas DataFrame
185
246
 
@@ -7,8 +7,8 @@ from typing import TypedDict
7
7
  import datetime
8
8
  import pandas as pd
9
9
  from operator import methodcaller
10
- import toml
11
10
  import sys
11
+ from pyopia.io import steps_from_xstats as steps_from_xstats # noqa: E(F401)
12
12
  import logging
13
13
 
14
14
  logger = logging.getLogger()
@@ -273,23 +273,6 @@ def steps_to_string(steps):
273
273
  return steps_str
274
274
 
275
275
 
276
- def steps_from_xstats(xstats):
277
- '''Get the steps attribute from xarray version of the particle stats into a dictionary
278
-
279
- Parameters
280
- ----------
281
- xstats : xarray.DataSet
282
- xarray version of the particle stats dataframe, containing metadata
283
-
284
- Returns
285
- -------
286
- dict
287
- TOML-formatted dictionary of pipeline steps
288
- '''
289
- steps = toml.loads(xstats.__getattr__('steps'))
290
- return steps
291
-
292
-
293
276
  def build_repr(toml_steps, step_name):
294
277
  '''Build a callable object from settings, which can be used to construct the pipeline steps dict
295
278
 
@@ -1 +0,0 @@
1
- __version__ = '2.0.4'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes