input_output.py 1.81 KB
Newer Older
1
import os
2 3
import pickle
from evaluation import TotalResults
4
from helpers import timingDecorator
Josef Brandt's avatar
Josef Brandt committed
5
from chemometrics.chemometricMethods import TrainedSubsampling
6 7 8 9 10 11 12 13 14 15

def load_results(fname: str) -> TotalResults:
    res: TotalResults = None
    if os.path.exists(fname):
        with open(fname, "rb") as fp:
            res = pickle.load(fp)
    return res


def save_results(fname: str, result: TotalResults) -> None:
16 17 18 19
    storedDsets: dict = {}
    for sampleRes in result.sampleResults:
        storedDsets[sampleRes.sampleName] = sampleRes.dataset
        sampleRes.dataset = None
Josef Brandt's avatar
Josef Brandt committed
20 21 22 23
        for subsamplingRes in sampleRes.results:
            subsamplingRes.method.particleContainer = None
            if type(subsamplingRes.method) == TrainedSubsampling:
                subsamplingRes.method.clf = None
24

25 26
    with open(fname, "wb") as fp:
        pickle.dump(result, fp, protocol=-1)
27

28 29 30
    for sampleRes in result.sampleResults:
        sampleRes.dataset = storedDsets[sampleRes.sampleName]

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

def get_pkls_from_directory(dirPath: str) -> dict:
    """
    Takes a directory and finds all pkl files in there. The result is returned in a dictionary, where
    each subfolder is present as a key and the actual pkl paths in a list as values.
    :param dirPath:
    :return:
    """
    resultDict: dict = {}
    subFolders = [x[1] for x in os.walk(dirPath)][0]

    for subFolder in subFolders:
        if subFolder.find('ignore') == -1:
            subFolderPath: str = os.path.join(dirPath, subFolder)
            filesInFolder: list = os.listdir(subFolderPath)
            pkls: list = [os.path.join(subFolderPath, file) for file in filesInFolder if file.endswith('.pkl')]
            resultDict[subFolder] = pkls

    return resultDict


def get_attributes_from_foldername(foldername: str) -> list:
    return [name.strip() for name in foldername.split(',')]