Skip to content

Commit 3091104

Browse files
committed
images fix
1 parent e2441b0 commit 3091104

File tree

1 file changed

+9
-85
lines changed

1 file changed

+9
-85
lines changed

intake_xarray/image.py

Lines changed: 9 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -159,86 +159,6 @@ def extract_tags(d):
159159
return {'EXIF ' + tag: exif_data[:,i] for i, tag in enumerate(exif_tags)}
160160

161161

162-
def reader(
163-
file, chunks, imread=None, preprocess=None, coerce_shape=None, exif_tags=None
164-
):
165-
"""Read a file object and output an dask xarray object
166-
167-
NOTE: inspired by dask.array.image.imread but altering the input to accept
168-
a just one file object.
169-
170-
Parameters
171-
----------
172-
file : OpenFile
173-
File object
174-
chunks : int or dict
175-
Chunks is used to load the new dataset into dask
176-
arrays. ``chunks={}`` loads the dataset with dask using a single
177-
chunk for all arrays.
178-
imread : function (optional)
179-
Optionally provide custom imread function.
180-
Function should expect a file object and produce a numpy array.
181-
Defaults to ``skimage.io.imread``.
182-
preprocess : function (optional)
183-
Optionally provide custom function to preprocess the image.
184-
Function should expect a numpy array for a single image.
185-
coerce_shape : tuple len 2 (optional)
186-
Optionally coerce the shape of the height and width of the image
187-
by setting `coerce_shape` to desired shape.
188-
exif_tags : boolean or list of str (optional)
189-
Controls whether exif tags are extracted from the images. If a
190-
list, the elements are treated as the particular tags to
191-
extract from each image. For any other truthy value, all tags
192-
that were able to be extracted from a sample image are used.
193-
When tags are extracted, an xarray Dataset is returned, with
194-
each exif tag in a corresponding data variable of the Dataset,
195-
(of type `Optional[exifread.classes.IfdTag]`), and the image
196-
data in a data variable 'raster'.
197-
198-
Returns
199-
-------
200-
Dask xarray.DataArray or xarray.Dataset of the image, and
201-
(optionally) the value of any requested EXIF tags. Treated as one
202-
chunk unless chunks kwarg is specified.
203-
"""
204-
import numpy as np
205-
from xarray import DataArray, Dataset
206-
207-
if not imread:
208-
from skimage.io import imread
209-
210-
with file as f:
211-
array = imread(f)
212-
if coerce_shape is not None:
213-
array = _coerce_shape(sample, shape=coerce_shape)
214-
if preprocess:
215-
array = preprocess(array)
216-
217-
ny, nx = array.shape[:2]
218-
coords = {'y': np.arange(ny),
219-
'x': np.arange(nx)}
220-
dims = ('y', 'x')
221-
222-
if len(array.shape) == 3:
223-
nchannel = array.shape[2]
224-
coords['channel'] = np.arange(nchannel)
225-
dims += ('channel',)
226-
227-
if exif_tags:
228-
exif_dict = _dask_exifread([file], exif_tags)
229-
exif_dict_ds = {tag: ((), arr[0]) for tag, arr in exif_dict.items()}
230-
231-
return Dataset(
232-
{
233-
'raster': (dims, array),
234-
**exif_dict_ds,
235-
},
236-
coords=coords,
237-
).chunk(chunks=chunks)
238-
else:
239-
return DataArray(array, coords=coords, dims=dims).chunk(chunks=chunks)
240-
241-
242162
def multireader(files, chunks, concat_dim, exif_tags, **kwargs):
243163
"""Read a stack of images into a dask xarray object
244164
@@ -373,7 +293,7 @@ class ImageReader(readers.BaseReader):
373293

374294

375295
def _read(self, urlpath, chunks=None, concat_dim='concat_dim',
376-
metadata=None, path_as_pattern=False,
296+
metadata=None, path_as_pattern=None,
377297
storage_options=None, exif_tags=None, **kwargs):
378298
"""
379299
This function is called when the data source refers to more
@@ -387,15 +307,19 @@ def _read(self, urlpath, chunks=None, concat_dim='concat_dim',
387307
"""
388308
import pandas as pd
389309
from xarray import DataArray
310+
path_as_pattern = path_as_pattern or (path_as_pattern is None and "{" in urlpath)
390311

391312
if path_as_pattern:
313+
from intake.readers.utils import pattern_to_glob
314+
392315
url = pattern_to_glob(urlpath)
393-
__, _, paths = fsspec.get_fs_token_paths(url, **(data.storage_options or {}))
394-
field_values = reverse_formats(data.url, paths)
316+
__, _, paths = fsspec.get_fs_token_paths(url, **(storage_options or {}))
317+
field_values = reverse_formats(urlpath, paths)
318+
paths = paths
395319
else:
396-
url = urlpath
320+
paths = urlpath
397321

398-
files = fsspec.open_files(urlpath, **(storage_options or {}))
322+
files = fsspec.open_files(paths, **(storage_options or {}))
399323

400324
out = multireader(
401325
files, chunks, concat_dim, exif_tags, **kwargs

0 commit comments

Comments
 (0)