spacr.qt.regex_detect¶
Filename-metadata regex helpers for drag-and-drop and manual configuration.
Central place for four related concerns:
apply_regex()— run a compiled regex over a list of filenames, return a list ofMetadataRecorddicts (one per file that matched).validate_records()— check whether the parsed records supply the fields the rest of spaCR needs (wellID/fieldID+chanIDfor multi-channel;fieldIDfor single-channel). Returns human-friendly warning strings for anything missing.auto_detect_regex()— heuristic detector that tries each built-in regex, and if none fit, synthesises a fresh one from the common shape of the sampled filenames.tabulate_records()— render a small aligned text table of records for the Console.
Public constants:
BUILTIN_REGEXES— ordered{label: pattern}map tried byauto_detect_regex()before falling back to synthesis.REQUIRED_MULTICHANNEL/REQUIRED_SINGLECHANNEL— which group names must be present for spaCR to be happy.
Classes¶
One parsed filename. |
Functions¶
|
Run |
|
Return the best-fitting regex for a set of filenames. |
|
Render a small aligned-column table of records for the Console. |
|
Check parsed records against spaCR's downstream requirements. |
Module Contents¶
- class spacr.qt.regex_detect.MetadataRecord[source]¶
One parsed filename.
- Variables:
filename – bare filename (no path).
groups – mapping of regex group name → matched text.
- spacr.qt.regex_detect.apply_regex(filenames: Sequence[str], pattern: str) Tuple[List[MetadataRecord], List[str]][source]¶
Run
patternover each filename and return matched records.- Parameters:
filenames – bare filenames (no directory).
pattern – regex string; anchored with
re.matchsemantics.
- Returns:
(records, non_matching_filenames).
- spacr.qt.regex_detect.auto_detect_regex(filenames: Sequence[str]) Tuple[str | None, str, int][source]¶
Return the best-fitting regex for a set of filenames.
Strategy:
Try every
BUILTIN_REGEXESpattern; if one matches every file it wins immediately.Otherwise pick the built-in that matches the MOST files (>=50 %).
If nothing crosses the 50 % bar, synthesise a fresh regex from the common shape of the sample (see
_synthesise_regex()).
- Parameters:
filenames – sample filenames to fit against.
- Returns:
(pattern_or_None, label, n_matches).pattern_or_Noneis None only when synthesis also fails.
- spacr.qt.regex_detect.tabulate_records(records: Sequence[MetadataRecord], columns: Sequence[str] | None = None, max_rows: int = 10, random_sample: bool = True, seed: int = 42) str[source]¶
Render a small aligned-column table of records for the Console.
- Parameters:
records – list of parsed records.
columns – which group names to include; auto-inferred from the first record when None.
max_rows – cap on rows; sampled at random if exceeded.
random_sample – True → pick
max_rowsat random when the list is longer; False → take the firstmax_rows.seed – RNG seed so the sample is reproducible between runs.
- Returns:
multi-line string ready to feed to
ConsolePanel.append_stdout().
- spacr.qt.regex_detect.validate_records(records: Sequence[MetadataRecord], multichannel: bool = True) List[str][source]¶
Check parsed records against spaCR’s downstream requirements.
- Parameters:
records – output of
apply_regex().multichannel – True if the dataset has more than one channel; False for single-channel data (relaxes the requirement set).
- Returns:
list of warning strings — empty when everything is fine.