Skip to content

Commit c056955

Browse files
committed
Treat OME-TIFF suffix as special case in parse_path()
To reduce code redundancy this also turns image_basename() into a simple wrapper for parse_path() that only returns the "basename" key.
1 parent 122d6b7 commit c056955

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
ImageJ2's *Script Parameter* `@# File`.
3232
* The dict returned by `imcflibs.pathtools.parse_path` now also contains the key
3333
`basename` that provides the filename without extension.
34+
* `imcflibs.pathtools.parse_path` treats OME-TIFF filenames as special cases now
35+
in the sense that the `.ome` part is stripped from the `basename` key and
36+
added to the `ext` key instead (as it is part of the suffix).
3437
* Many improvements / clarifications in function docstrings.

src/imcflibs/pathtools.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ def parse_path(path, prefix=""):
3838
- `path` : The same as `full`, up to (including) the last separator.
3939
- `dname` : The segment between the last two separators (directory).
4040
- `fname` : The segment after the last separator (filename).
41-
- `basename` : The filename without extension.
42-
- `ext` : The filename extension, containing max 1 dot (included).
41+
- `basename` : The filename without extension. Note that *OME-TIFF*
42+
files (having a suffix like `.ome.tif` or `.ome.tiff`) are treated as
43+
special case in the sense that the `.ome` part is also stripped from
44+
the basename and added to the `ext` key (see below).
45+
- `ext` : The filename extension, containing max 1 dot (included) with
46+
the special case of `.ome.tif` / `.ome.tiff` where 2 dots are
47+
contained to represent the full suffix.
4348
4449
Examples
4550
--------
@@ -76,6 +81,17 @@ def parse_path(path, prefix=""):
7681
'basename': 'file',
7782
'orig': 'C:\\Temp\\foo\\file.ext',
7883
'path': 'C:/Temp/foo/'}
84+
85+
Special treatment for *OME-TIFF* suffixes:
86+
87+
>>> parse_path("/path/to/some/nice.OME.tIf")
88+
{'basename': 'nice',
89+
'dname': 'some',
90+
'ext': '.OME.tIf',
91+
'fname': 'nice.OME.tIf',
92+
'full': '/path/to/some/nice.OME.tIf',
93+
'orig': '/path/to/some/nice.OME.tIf',
94+
'path': '/path/to/some/'}
7995
"""
8096
path = str(path)
8197
if prefix:
@@ -90,9 +106,12 @@ def parse_path(path, prefix=""):
90106
parsed["path"] = os.path.dirname(path) + sep
91107
parsed["fname"] = os.path.basename(path)
92108
parsed["dname"] = os.path.basename(os.path.dirname(parsed["path"]))
93-
split_ext = os.path.splitext(parsed["fname"])
94-
parsed["ext"] = split_ext[1]
95-
parsed["basename"] = split_ext[0]
109+
base, ext = os.path.splitext(parsed["fname"])
110+
parsed["ext"] = ext
111+
parsed["basename"] = base
112+
if base.lower().endswith(".ome") and ext.lower().startswith(".tif"):
113+
parsed["basename"] = base[:-4]
114+
parsed["ext"] = base[-4:] + ext
96115

97116
return parsed
98117

@@ -178,8 +197,8 @@ def image_basename(orig_name):
178197
----------
179198
orig_name : str
180199
181-
Example
182-
-------
200+
Examples
201+
--------
183202
>>> image_basename('/path/to/some_funny_image_file_01.png')
184203
'some_funny_image_file_01'
185204
@@ -189,10 +208,7 @@ def image_basename(orig_name):
189208
>>> image_basename('/tmp/FoObAr.OMe.tIf')
190209
'FoObAr'
191210
"""
192-
base = os.path.splitext(os.path.basename(orig_name))[0]
193-
if base.lower().endswith(".ome"):
194-
base = base[:-4]
195-
return base
211+
return parse_path(orig_name)["basename"]
196212

197213

198214
def gen_name_from_orig(path, orig_name, tag, suffix):

0 commit comments

Comments
 (0)