data_io.py 1.1 KB
'''
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