-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (123 loc) · 4.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import struct
class Node:
def __init__(self, name, type):
if type == 2:
self.name = name
else:
self.name = name[2:-1]
self.type = type
self.values = []
def __repr__(self):
return str(self.name)
def __iter__(self):
return iter(self.values)
def __getitem__(self, key):
if isinstance(key, slice):
return self.values[key]
if isinstance(key, int):
return self.values[key]
else:
raise TypeError("Key must be of type int")
def __len__(self):
return len(self.values)
class File:
def __init__(self, data, index):
self._data = data
self.name = ""
self.date = ""
self.analysis = ""
self.nodes_count = 0
self.timepoints_count = 0
self._index = index
self._nodes = []
self._parse_file()
def _parse_file(self):
self._parse_header()
self._parse_nodes()
self._parse_values()
def _extract_values(self, data, chunk_size, unpack_type):
result = []
for i in range(0, len(data), chunk_size):
chunk = data[i:i + chunk_size]
if len(chunk) == chunk_size:
value = struct.unpack(unpack_type, chunk)
result.append(value[0])
return result
def _extract_double_values(self, data):
return self._extract_values(data, 8, "d")
def _extract_integer_values(self, data):
return self._extract_values(data, 4, "i")
def _parse_header(self):
self.name = self._extract_name()
self.date = self._extract_name()
self.analysis = self._extract_name()
self._index += 100
extracted_data = self._extract_integer_values(self._data[self._index:self._index + 8])
self.nodes_count = extracted_data[0]
self.timepoints = extracted_data[1]
self._index += 8
def _extract_name(self):
name_length = self._extract_integer_values(self._data[self._index:self._index + 4])[0]
self._index += 4
name = self._data[self._index:self._index + name_length].decode("utf-8")
self._index += name_length
return name
def _parse_nodes(self):
for i in range(self.nodes_count):
name_length = self._extract_integer_values(self._data[self._index:self._index + 4])[0]
self._index += 4
name = self._data[self._index:self._index + name_length].decode("utf-8")
self._index += name_length
type = self._extract_integer_values(self._data[self._index:self._index + 4])[0]
self._index += 4
self._nodes.append(Node(name, type))
def _parse_values(self):
for i in range(self.timepoints):
for j in range(self.nodes_count):
value = self._extract_double_values(self._data[self._index:self._index + 8])[0]
self._index += 8
self._nodes[j].values.append(value)
def __iter__(self):
return iter(self._nodes)
def __getitem__(self, key):
if isinstance(key, str):
for node in self._nodes:
if node.name == key:
return node
raise KeyError(f"Node: {key} not found")
else:
raise TypeError("Key must be of type str")
def __len__(self):
return len(self._nodes)
def __repr__(self):
return str(self.name)
class Aimspice:
def __init__(self, file_path):
self._file_path = file_path
self.file_count = 0
self._files = []
self._index = 0
self._parse_file()
def _parse_file(self):
with open(self._file_path, "rb") as f:
data = f.read()
self.file_count = struct.unpack("i", data[self._index:self._index + 4])[0]
self._index += 4
for i in range(self.file_count):
file = File(data, self._index)
self._index = file._index
self._files.append(file)
def __iter__(self):
return iter(self._files)
def __getitem__(self, key):
if isinstance(key, str):
for file in self._files:
if file.name == key:
return file
raise KeyError(f"File: {key} not found")
elif isinstance(key, int):
return self._files[key]
else:
raise TypeError("Key must be of type str or int")
def __len__(self):
return len(self._files)