Source code for utility.data

"""
data.py
====================================
Utility data functions.
"""

from dataclasses import dataclass
import pandas as pd
from io import StringIO, BytesIO

from pandas.core.frame import DataFrame
from tools import file_reader, file_writer
from pathlib import Path
import shutil

[docs]def write_csv(data: pd.DataFrame, file_path: Path, file_name: str) -> None: """ Writes a CSV to a file :param data: data to be written :param file_path: path of the file :param file_name: name of the file """ outfile = StringIO() with open(Path.joinpath(file_path, file_name), 'w', newline='') as fd: file_writer.write_csv(data, outfile) outfile.seek(0) shutil.copyfileobj(outfile, fd)
[docs]def read_csv(file_path: Path, file_name: str, converters: dict=None) -> pd.DataFrame: """ Reads a CSV from a file :param data: data to be written :param file_path: path of the file :param file_name: name of the file :param converters: converters to use """ infile = StringIO() with open(Path.joinpath(file_path, file_name), 'r', encoding='utf8') as fd: shutil.copyfileobj(fd, infile) infile.seek(0) return file_reader.read_csv(infile, converters=converters)
[docs]def read_pickle(file_path: Path, file_name: str) -> any: """ Reads a pickle from a file :param file_path: path of the file :param file_name: name of the file """ infile = BytesIO() with open(Path.joinpath(file_path, file_name), 'rb') as fd: shutil.copyfileobj(fd, infile) infile.seek(0) return file_reader.read_pickle(infile)
[docs]def write_pickle(data: any, file_path: Path, file_name: str) -> None: """ Writes a pickle to a file :param data: data to be written :param file_path: path of the file :param file_name: name of the file """ outfile = BytesIO() with open(Path.joinpath(file_path, file_name), 'wb') as fd: file_writer.write_pickle(data, outfile) outfile.seek(0) shutil.copyfileobj(outfile, fd)
[docs]def write_embedding(mapping: dict, file_path: Path, file_name: str) -> None: """ Writes an embedding to a CSV file :param mapping: the embedded mapping :param file_path: path of the file :param file_name: name of the file """ outfile = StringIO() with open(Path.joinpath(file_path, file_name), 'w', newline='', encoding='utf8') as fd: file_writer.write_embedding(mapping, outfile) outfile.seek(0) shutil.copyfileobj(outfile, fd)
[docs]@dataclass class Data: """ Dto to keep track of feature data """ sc: pd.DataFrame ss: pd.DataFrame td: pd.DataFrame tc: pd.DataFrame ats: pd.DataFrame