-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.py
44 lines (34 loc) · 1.11 KB
/
helpers.py
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
from __future__ import annotations
from datetime import datetime
from functools import wraps
from pathlib import Path
from typing import TYPE_CHECKING, Tuple
import numpy as np
import pandas as pd
if TYPE_CHECKING:
from corpora import Matrix
def timing(f):
@wraps(f)
def wrap(*args, **kw):
ts = datetime.now()
result = f(*args, **kw)
te = datetime.now()
print(f"func: {f.__name__} took: {te-ts}")
return result
return wrap
def write(path: Path, *args: Tuple[pd.DataFrame | Matrix, str]):
for table, name in args:
if isinstance(table, pd.DataFrame):
table.to_csv(path / f"{name}.csv", index=False)
else:
with open(path / f"{name}.npy", "wb") as f:
np.save(f, table.matrix)
print(f"wrote {name}")
def read(path: Path, name_with_ext: str) -> pd.DataFrame | np.array:
if name_with_ext[-3:] == "npy":
return np.load(path / name_with_ext)
else:
return pd.read_csv(
path / name_with_ext,
parse_dates=(["date"] if name_with_ext != "dvr.csv" else []),
)