data_io.py
1.1 KB
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
'''
Data management input/output
'''
# Import packages and modules
import numpy as np
# Defining some types
from typing import List, Dict
KeyToList = Dict[str, List[str]]
KeyToFeatures = Dict[str, List[float]]
def read_lst(file_path: str) -> KeyToList:
'''
Read lst file with this structure:
[id] [value1] [value2] ... [value n]
This is a basic function reused by others like read_features.
returns a dictionary with id as key and a list of value as corresponding values
'''
# KeyToList type variable
key_to_list = dict()
with open(file_path, "r") as f:
for line in f:
splited = line.replace("\n", "").split(" ")
id = splited[0]
values = splited[1:]
key_to_list[id] = values
return key_to_list
def read_features(file_path: str) -> KeyToFeatures:
'''
'''
# KeyToFeatures type variable
key_to_features = dict()
# and the KeyToList
key_to_list = read_lst(file_path)
for key_, list_ in key_to_list.items():
key_to_features[key_] = np.asarray(list_, dtype=float)
return key_to_features