Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/alos2/parse_utils.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-13 08:53 +0000

1# MIT License 

2 

3# Copyright (c) 2024 Jeremy Fix 

4 

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: 

11 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14 

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. 

22 

23 

24def format_key_val(key_value): 

25 key, value = key_value 

26 return key.replace("_", " ").capitalize() + f" : {value}" 

27 

28 

29def format_dictionary(record, level=0): 

30 strs = map(format_key_val, record.items()) 

31 # Add heading padding 

32 strs = map(lambda s: " " * (level * 4) + s, strs) 

33 return "\n".join(strs) 

34 

35 

36def read_field(fh, start_byte, num_bytes, type_bytes, expected): 

37 fh.seek(start_byte) 

38 data_bytes = fh.read(num_bytes) 

39 if type_bytes[0] == "A": 

40 value = data_bytes.decode("ascii") 

41 elif type_bytes[0] == "B": 

42 # Binary number representation, big_endian 

43 value = int.from_bytes(data_bytes, "big") 

44 elif type_bytes[0] == "I": 

45 data_str = data_bytes.decode("ascii").strip() 

46 if len(data_str) == 0: 

47 value = None 

48 else: 

49 value = int(data_str) 

50 elif type_bytes[0] == "E": 

51 # This is a Em.n format 

52 # Exponential notation, right fill 

53 m, n = type_bytes[1:].split(".") 

54 value = float(data_bytes.decode("ascii").strip()) 

55 elif type_bytes[0] == "F": 

56 # This is a Fm.n format 

57 # Exponential notation, right fill 

58 m, n = type_bytes[1:].split(".") 

59 value = float(data_bytes.decode("ascii").strip()) 

60 if expected is not None: 

61 assert value == expected 

62 

63 return value 

64 

65 

66def parse_from_format( 

67 fh, obj, descriptor_format, num_records, record_length, base_offset 

68): 

69 if num_records == 1: 

70 for ( 

71 field_name, 

72 start_byte, 

73 num_bytes, 

74 type_bytes, 

75 expected, 

76 ) in descriptor_format: 

77 value = read_field( 

78 fh, base_offset + start_byte, num_bytes, type_bytes, expected 

79 ) 

80 obj[field_name] = value 

81 fh.seek(base_offset + record_length) 

82 return base_offset + record_length 

83 else: 

84 offset = base_offset 

85 for _ in range(num_records): 

86 record = {} 

87 for ( 

88 field_name, 

89 start_byte, 

90 num_bytes, 

91 type_bytes, 

92 expected, 

93 ) in descriptor_format: 

94 value = read_field( 

95 fh, offset + start_byte, num_bytes, type_bytes, expected 

96 ) 

97 record[field_name] = value 

98 obj.append(record) 

99 offset += record_length 

100 fh.seek(offset) 

101 return offset