Skip to content

Commit d6183dd

Browse files
authored
Merge pull request #1104 from SchmollerLab/fix_datastruct_ome_tiff
Fix create data structure from ome.tif
2 parents f1843a9 + c287908 commit d6183dd

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

cellacdc/acdc_regex.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,32 @@ def get_function_names(text, include_class_methods=True):
2323
pattern = r'\ndef\s+([a-zA-Z_]\w*)\s*\('
2424
return re.findall(pattern, text)
2525

26-
def is_alphanumeric_filename(text, allow_space=True):
26+
def is_alphanumeric_filename(
27+
text,
28+
allow_space=True,
29+
allowed: str | list[str] | None=None
30+
):
2731
if allow_space:
2832
pattern = r'^[\w\-_. ]+$'
2933
else:
3034
pattern = r'^[\w\-_.]+$'
31-
is_single_or_no_dot = len(re.findall(r'\.', text)) <= 1
32-
return bool(re.match(pattern, text)) and is_single_or_no_dot
35+
36+
if allowed is None:
37+
allowed = []
38+
39+
if isinstance(allowed, str):
40+
allowed = (allowed,)
41+
42+
max_num_dots = 1
43+
if allowed is not None:
44+
max_num_dots += sum([txt.count('.') for txt in allowed])
45+
46+
for allowed_text in allowed:
47+
allowed_text = re.escape(allowed_text)
48+
pattern = pattern.replace(r'+$', fr'+({allowed_text})?$')
49+
50+
is_less_max_num_dots = len(re.findall(r'\.', text)) <= max_num_dots
51+
return bool(re.match(pattern, text)) and is_less_max_num_dots
3352

3453
def get_non_alphanumeric_characters(text):
3554
return re.findall(r'[^\w\-.]', text)

cellacdc/dataStruct.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,8 +1927,11 @@ def checkFileFormat(self, raw_src_path):
19271927
return files
19281928

19291929
def checkFileNames(self, raw_filenames, raw_src_path):
1930+
allowed = (
1931+
'.ome.tif',
1932+
)
19301933
for file in raw_filenames:
1931-
if not acdc_regex.is_alphanumeric_filename(file):
1934+
if not acdc_regex.is_alphanumeric_filename(file, allowed=allowed):
19321935
msg = widgets.myMessageBox(wrapText=False)
19331936
txt = html_utils.paragraph(
19341937
f"""

cellacdc/load.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,8 +3659,12 @@ def __init__(self):
36593659

36603660
class OMEXML_Channel:
36613661
def __init__(self, Channel) -> None:
3662-
self.Name = Channel.attrib.get('Name', '')
3663-
self.node = Channel.attrib
3662+
if not Channel or Channel is None:
3663+
self.Name = 'not_found'
3664+
self.node = None
3665+
else:
3666+
self.Name = Channel.attrib.get('Name', '')
3667+
self.node = Channel.attrib
36643668

36653669
class OMEXML_Pixels:
36663670
def __init__(self, Pixels, node, ome_schema) -> None:
@@ -3683,7 +3687,10 @@ def __init__(self, Pixels, node, ome_schema) -> None:
36833687
self.PhysicalSizeZ = node.get('PhysicalSizeZ', 1.0)
36843688

36853689
def Channel(self, channel_index=0):
3686-
Channel = self.Pixels.findall(f'{self.ome_schema}Channel')[channel_index]
3690+
try:
3691+
Channel = self.Pixels.findall(f'{self.ome_schema}Channel')[channel_index]
3692+
except Exception as err:
3693+
Channel = None
36873694
return OMEXML_Channel(Channel)
36883695

36893696
class OMEXML:
@@ -3697,12 +3704,20 @@ def read_omexml_string(self):
36973704
return tif.ome_metadata
36983705

36993706
def parse_metadata(self):
3707+
self.root = None
3708+
self.ome_schema = None
37003709
self.omexml_string = self.read_omexml_string()
3710+
if self.omexml_string is None:
3711+
return
3712+
37013713
self.root = ET.fromstring(self.omexml_string)
37023714
self.ome_schema = re.findall(r'({.+})OME', self.root.tag)[0]
37033715

37043716
def instrument(self):
37053717
instrument = OMEXML_intrument()
3718+
if self.root is None:
3719+
return instrument
3720+
37063721
instrument_xml = self.root.find(f'{self.ome_schema}Instrument')
37073722
if instrument_xml is None:
37083723
return instrument
@@ -3716,9 +3731,15 @@ def instrument(self):
37163731
return instrument
37173732

37183733
def get_image_count(self):
3734+
if self.root is None:
3735+
return 1
3736+
37193737
return len(self.root.findall(f'{self.ome_schema}Image'))
37203738

37213739
def image(self):
3740+
if self.root is None:
3741+
return OMEXML_image(None, 'not_found')
3742+
37223743
Image = self.root.find(f'{self.ome_schema}Image')
37233744
Pixels = Image.find(f'{self.ome_schema}Pixels')
37243745
image = OMEXML_image(Pixels, self.ome_schema)

0 commit comments

Comments
 (0)