• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / tomo_mapper / 16725814017

04 Aug 2025 02:09PM UTC coverage: 86.311% (-3.3%) from 89.629%
16725814017

push

github

web-flow
Release v1.1.0

350 of 449 new or added lines in 22 files covered. (77.95%)

9 existing lines in 2 files now uncovered.

2396 of 2776 relevant lines covered (86.31%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/parser/impl/TxtParser.py
1
import logging
1✔
2
from typing import Optional
1✔
3

4
from PIL import Image
1✔
5

6
from src.Preprocessor import Preprocessor
1✔
7
from src.model.ImageMD import ImageMD
1✔
8
from src.parser.ImageParser import ImageParser, ParserMode
1✔
9
from src.parser.mapping_util import map_a_dict
1✔
10
from src.resources.maps.mapping import textparser_tomo_tescan
1✔
11
from src.util import input_to_dict
1✔
12
import configparser
1✔
13

14

15

16
#TODO: would this have any benefit from replacing with tifffile lib?
17

18
class TxtParser(ImageParser):
1✔
19

20
    internal_mapping = None
1✔
21
    def __init__(self, mode):
1✔
22
        if mode == ParserMode.TOMO:
1✔
23
            m1 = input_to_dict(textparser_tomo_tescan.read_text())
1✔
24
            self.internal_mapping = m1
1✔
25
        super().__init__(mode)
1✔
26

27
    @staticmethod
1✔
28
    def expected_input_format():
1✔
29
        return "text/plain"
1✔
30

31
    def parse(self, file_path, mapping) -> tuple[ImageMD, str]:
1✔
32
        input_md = self._read_input_file(file_path)
1✔
33
        if not input_md:
1✔
NEW
34
            logging.warning("No metadata extractable from {}".format(file_path))
×
NEW
35
            return None, None
×
36

37
        if not mapping and not self.internal_mapping:
1✔
NEW
38
            logging.error("No mapping provided for image parsing. Aborting")
×
NEW
39
            exit(1)
×
40
        mapping_dict = mapping if mapping else self.internal_mapping
1✔
41
        image_md = map_a_dict(input_md, mapping_dict)
1✔
42
        #print("image_md: ", image_md)
43

44
        Preprocessor.normalize_all_units(image_md)
1✔
45
        Preprocessor.normalize_all_datetimes(image_md)
1✔
46

47
        if self.mode == ParserMode.TOMO:
1✔
NEW
48
            image_from_md = self._create_tomo_image(image_md, file_path)
×
49
        else:
50
            image_from_md = ImageMD(image_metadata=image_md, filePath="")
1✔
51

52
        #print("image_from_md: ", image_from_md)
53
        return image_from_md, image_md
1✔
54

55
    def _create_tomo_image(self, image_md, fp) -> ImageMD:
1✔
56

NEW
57
        image_md_format = {
×
58
            "acquisition_info": image_md["acquisition"],
59
            "dataset_metadata": image_md["acquisition"]["dataset"],
60
            "image_metadata": image_md["acquisition"]["dataset"]["images"],
61
            "filePath": fp
62
        }
63

NEW
64
        image_md_format["dataset_metadata"].pop("images")
×
NEW
65
        if image_md_format.get("image_metadata"):
×
NEW
66
            image_md_format["image_metadata"]["localPath"] = fp
×
67

NEW
68
        return ImageMD(**image_md_format)
×
69

70
    def _read_input_file(self, file_path) -> Optional[dict]:
1✔
71
        """
72
        Reading input may be done with a predefined tag or without. In the latter case we try to extract from all tags and use the joint dictionary for mapping.
73
        :param file_path: image file path
74
        :param tagID: tag to extract from, may be None
75
        :return: data from extracted tag(s) as dict
76
        """
77
        #print(f"I am trying to read a {file_path}")
78

79
        config = configparser.ConfigParser(allow_no_value=True, delimiters=(" "))
1✔
80
        config.optionxform = str
1✔
81

82
        # Read the .txt file either as UTF-8 or a different byte format using errors="replace"
83
        with open(file_path, "r", encoding="utf-8", errors="replace") as file:
1✔
84
            md = file.read()
1✔
85

86
        output_dict = {}
1✔
87
        output_dict.update(input_to_dict(md))
1✔
88

89
        return output_dict
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc