Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/slc/ann_file.py: 0%
32 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
1# MIT License
3# Copyright (c) 2024 Jeremy Fix
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
24class AnnFile:
25 r"""
27 From the documentation :
29 The annotation file (.ann) is a keyword/value ASCII file in which the value on the right of the equals sign
30 corresponds to the keyword on the left of the equals sign. The number of keywords may change with time, so the line
31 number should not be assumed to be constant for any given keyword.
33 In addition, the spacing between keywords and
34 values may change. The units are given in parenthesis between the keyword and equal sign, and may change from
35 annotation file to annotation file and within each annotation file.
37 Comments are indicated by semicolons (;), and
38 may occur at the beginning of a line, or at the middle of a line (everything after the semicolon on that line is a
39 comment). The length of each text line is variable, and ends with a carriage return. There may be lines with
40 just a carriage return or spaces and a carriage return.
43 """
45 def __init__(self, filename):
46 self.filename = filename
47 self.parameters = []
48 self.data = self.read()
50 def read(self):
51 with open(self.filename, "r") as f:
52 for l in f:
53 l = l.strip()
54 if l.startswith(";"):
55 # Skip comments
56 continue
57 if len(l) != 0:
58 # Skip empty lines
59 fields = l.split(" = ")
60 if len(fields) != 2:
61 continue
62 key = fields[0].strip()
63 # key might be :
64 # ISLR Noise Calibration Term LRTI80 (-)
65 # slc_1_1x1_mag.set_cols (pixels)
66 # ....
67 # We split the key by spaces, take all but the last token (the units) and join them with an underscore
68 # to get a key that is easier to use
69 key = "_".join(
70 list(map(lambda s: s.strip().lower(), key.split()))[:-1]
71 ).replace(".", "_")
73 # Now we process the value, which may end with a comment
74 value = fields[1].split(";")[0].strip()
75 try:
76 value = int(value)
77 except ValueError:
78 try:
79 value = float(value)
80 except ValueError:
81 pass
82 setattr(self, key, value)
83 self.parameters.append(key)
85 def __repr__(self):
86 myrep = f"AnnFile({self.filename})\n"
87 for p in self.parameters:
88 myrep += f" {p} = {getattr(self, p)}\n"
89 return myrep