44from datetime import datetime
55from pathlib import Path
66from threading import RLock
7- from typing import Callable , Dict , List , NamedTuple , Optional , OrderedDict
7+ from typing import Callable , Dict , List , Optional , OrderedDict
88
99import requests
1010import xmltodict
2727requests .get , requests .post , requests .put , requests .delete = authorised_requests ()
2828
2929
30- class TiltInfoExtraction (NamedTuple ):
31- series : Callable [[Path ], str ]
32- angle : Callable [[Path ], str ]
33- tag : Callable [[Path ], str ]
34-
35-
36- def _get_tilt_series_v5_7 (p : Path ) -> str :
37- return p .name .split ("_" )[1 ]
38-
39-
4030def _get_tilt_angle_v5_7 (p : Path ) -> str :
4131 return p .name .split ("[" )[1 ].split ("]" )[0 ]
4232
4333
44- def _get_tilt_tag_v5_7 (p : Path ) -> str :
45- return p .name .split ("_" )[0 ]
46-
47-
48- def _get_slice_index_v5_11 (tag : str ) -> int :
49- slice_index = 0
50- for i , ch in enumerate (tag [::- 1 ]):
51- if not ch .isnumeric ():
52- slice_index = - i
53- break
54- if not slice_index :
55- raise ValueError (
56- f"The file tag { tag } does not end in numeric characters or is entirely numeric: cannot parse"
57- )
58- return slice_index
59-
60-
61- def _get_tilt_series_v5_11 (p : Path ) -> str :
62- tag = p .name .split ("_" )[0 ]
63- slice_index = _get_slice_index_v5_11 (tag )
64- return tag [slice_index :]
65-
66-
67- def _get_tilt_tag_v5_11 (p : Path ) -> str :
68- tag = p .name .split ("_" )[0 ]
69- slice_index = _get_slice_index_v5_11 (tag )
70- return tag [:slice_index ]
71-
72-
7334def _get_tilt_angle_v5_11 (p : Path ) -> str :
7435 _split = p .name .split ("_" )[2 ].split ("." )
7536 return "." .join (_split [:- 1 ])
7637
7738
7839def _find_angle_index (split_name : List [str ]) -> int :
7940 for i , part in enumerate (split_name ):
80- if "." in part :
41+ if "." in part and part [ 0 ]. isnumeric () :
8142 return i
82- return 0
83-
84-
85- def _get_tilt_series_v5_12 (p : Path ) -> str :
86- split_name = p .name .split ("_" )
87- angle_idx = _find_angle_index (split_name )
88- if split_name [angle_idx - 2 ].isnumeric ():
89- return split_name [angle_idx - 2 ]
90- return ""
43+ return - 1
9144
9245
9346def _get_tilt_angle_v5_12 (p : Path ) -> str :
94- split_name = p .name .split ("_" )
47+ split_name = p .stem .split ("_" )
9548 angle_idx = _find_angle_index (split_name )
49+ if angle_idx == - 1 :
50+ return ""
9651 return split_name [angle_idx ]
9752
9853
99- def _get_tilt_tag_v5_12 (p : Path ) -> str :
100- split_name = p .name .split ("_" )
101- angle_idx = _find_angle_index (split_name )
102- if split_name [angle_idx - 2 ].isnumeric ():
103- return "_" .join (split_name [: angle_idx - 2 ])
104- return "_" .join (split_name [: angle_idx - 1 ])
105-
106-
10754tomo_tilt_info = {
108- "5.7" : TiltInfoExtraction (
109- _get_tilt_series_v5_7 , _get_tilt_angle_v5_7 , _get_tilt_tag_v5_7
110- ),
111- "5.11" : TiltInfoExtraction (
112- _get_tilt_series_v5_11 , _get_tilt_angle_v5_11 , _get_tilt_tag_v5_11
113- ),
114- "5.12" : TiltInfoExtraction (
115- _get_tilt_series_v5_12 ,
116- _get_tilt_angle_v5_12 ,
117- _get_tilt_tag_v5_12 ,
118- ),
55+ "5.7" : _get_tilt_angle_v5_7 ,
56+ "5.11" : _get_tilt_angle_v5_11 ,
57+ "5.12" : _get_tilt_angle_v5_12 ,
11958}
12059
12160
@@ -177,8 +116,6 @@ def __init__(self, acquisition_software: str, basepath: Path):
177116 self ._processing_job_stash : dict = {}
178117 self ._preprocessing_triggers : dict = {}
179118 self ._lock : RLock = RLock ()
180- self ._extract_tilt_series : Callable [[Path ], str ] | None = None
181- self ._extract_tilt_tag : Callable [[Path ], str ] | None = None
182119
183120 def _flush_data_collections (self ):
184121 logger .info (
@@ -232,14 +169,7 @@ def _check_for_alignment(
232169 manual_tilt_offset : Optional [float ],
233170 pixel_size : Optional [float ],
234171 ):
235- if self ._extract_tilt_series and self ._extract_tilt_tag :
236- tilt_series = (
237- f"{ self ._extract_tilt_tag (movie_path )} _{ self ._extract_tilt_series (movie_path )} "
238- if self ._extract_tilt_tag (movie_path )
239- else self ._extract_tilt_series (movie_path )
240- )
241- else :
242- return
172+ tilt_series = _construct_tilt_series_name (movie_path )
243173
244174 if self ._motion_corrected_tilt_series .get (
245175 tilt_series
@@ -370,9 +300,7 @@ def _get_source(
370300 def _add_tilt (
371301 self ,
372302 file_path : Path ,
373- extract_tilt_series : Callable [[Path ], str ],
374303 extract_tilt_angle : Callable [[Path ], str ],
375- extract_tilt_tag : Callable [[Path ], str ],
376304 environment : MurfeyInstanceEnvironment | None = None ,
377305 required_position_files : List [Path ] | None = None ,
378306 required_strings : List [str ] | None = None ,
@@ -390,10 +318,6 @@ def _add_tilt(
390318 )
391319 if required_strings and not any (r in file_path .name for r in required_strings ):
392320 return []
393- if not self ._extract_tilt_series :
394- self ._extract_tilt_series = extract_tilt_series
395- if not self ._extract_tilt_tag :
396- self ._extract_tilt_tag = extract_tilt_tag
397321 try :
398322 tilt_angle = extract_tilt_angle (file_path )
399323 try :
@@ -733,9 +657,7 @@ def _add_tomo_tilt(
733657 tilt_series = _construct_tilt_series_name (file_path )
734658 return self ._add_tilt (
735659 file_path ,
736- tilt_info_extraction .series ,
737- tilt_info_extraction .angle ,
738- tilt_info_extraction .tag ,
660+ tilt_info_extraction ,
739661 environment = environment ,
740662 required_position_files = (
741663 required_position_files
@@ -767,9 +689,7 @@ def _extract_tilt_series(p: Path) -> str:
767689
768690 return self ._add_tilt (
769691 file_path ,
770- _extract_tilt_series ,
771692 lambda x : "." .join (x .name .split (delimiter )[- 1 ].split ("." )[:- 1 ]),
772- lambda x : "" ,
773693 environment = environment ,
774694 required_strings = [],
775695 )
0 commit comments