Commit a556561b2f29098bf54d022e59ad47f04199a7a8
1 parent
9c2436a4f7
Exists in
master
Basic data management functionalities added, with an init file empty.
Showing 1 changed file with 44 additions and 0 deletions Side-by-side Diff
volia/data_io.py
| 1 | +''' | |
| 2 | +Data management input/output | |
| 3 | +''' | |
| 4 | + | |
| 5 | +# Import packages and modules | |
| 6 | +import numpy as np | |
| 7 | + | |
| 8 | +# Defining some types | |
| 9 | +from typing import List, Dict | |
| 10 | +KeyToList = Dict[str, List[str]] | |
| 11 | +KeyToFeatures = Dict[str, List[float]] | |
| 12 | + | |
| 13 | + | |
| 14 | +def read_lst(file_path: str) -> KeyToList: | |
| 15 | + ''' | |
| 16 | + Read lst file with this structure: | |
| 17 | + [id] [value1] [value2] ... [value n] | |
| 18 | + | |
| 19 | + This is a basic function reused by others like read_features. | |
| 20 | + returns a dictionary with id as key and a list of value as corresponding values | |
| 21 | + ''' | |
| 22 | + # KeyToList type variable | |
| 23 | + key_to_list = dict() | |
| 24 | + with open(file_path, "r") as f: | |
| 25 | + for line in f: | |
| 26 | + splited = line.replace("\n", "").split(" ") | |
| 27 | + id = splited[0] | |
| 28 | + values = splited[1:] | |
| 29 | + key_to_list[id] = values | |
| 30 | + return key_to_list | |
| 31 | + | |
| 32 | + | |
| 33 | +def read_features(file_path: str) -> KeyToFeatures: | |
| 34 | + ''' | |
| 35 | + ''' | |
| 36 | + # KeyToFeatures type variable | |
| 37 | + key_to_features = dict() | |
| 38 | + # and the KeyToList | |
| 39 | + key_to_list = read_lst(file_path) | |
| 40 | + | |
| 41 | + for key_, list_ in key_to_list.items(): | |
| 42 | + key_to_features[key_] = np.asarray(list_, dtype=float) | |
| 43 | + | |
| 44 | + return key_to_features |