-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead_String_Data.py
64 lines (56 loc) · 2.34 KB
/
Read_String_Data.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
import os
import re
import numpy as np
from typing import List, Tuple
class Read_String_Data:
def __init__(self,folder,parameters):
self.folder = folder
self.parameters = parameters
self.pattern = r'Time= (\d+)\s+(\d+)\s+(.+)'
def _parse_line(self, line: str, pattern: str) -> Tuple[int, int, np.ndarray]:
"""Parse a line using the given regex pattern."""
match = re.match(pattern, line)
if match:
time = int(match.group(1))
length = int(match.group(2))
atoms = np.array([int(x) for x in match.group(3).split('-->') if x.strip().isdigit()])
return time, length, atoms
return None
def _read_file(self, filepath):
"""Read lines from a file if it exists."""
if os.path.exists(filepath):
with open(filepath) as file:
return file.readlines()
print(f'{filepath} does not exist')
return []
def read_string_file(self,filename):
string_length = []
string_atom = []
string_time = []
lines = self._read_file(filename)
for line in lines:
parsed_data = self._parse_line(line.strip(), self.pattern)
if parsed_data:
t, length, atoms = parsed_data
string_time.append(t)
string_length.append(length)
string_atom.append(atoms)
return string_time,string_length,string_atom
def build_string_library(self,string_time, string_atom):
string_library = {}
for tt, atom_indices in zip(string_time, string_atom):
if len(atom_indices) >= self.parameters['minimum_length']:
if tt in string_library:
string_library[tt].append(atom_indices)
else:
string_library[tt] = [atom_indices]
return string_library
def read_array_string(self):
string_dictionaries = {}
for i in self.parameters['steps_read']:
for j in self.parameters['initial_read']:
filepath = f'{self.folder}/{i}/{j}/STRINGS'
string_time,string_length,string_atom = self.read_string_file(filepath)
if string_time:
string_dictionaries[filepath] = self.build_string_library(string_time, string_atom)
return string_dictionaries