Spaces:
Sleeping
Sleeping
robin-courant
commited on
Commit
•
337c106
1
Parent(s):
dd9e77d
Update file_utils
Browse files- utils/file_utils.py +0 -46
utils/file_utils.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import json
|
2 |
import os
|
3 |
import os.path as osp
|
4 |
import pickle
|
@@ -7,10 +6,7 @@ from typing import Any
|
|
7 |
|
8 |
import h5py
|
9 |
import numpy as np
|
10 |
-
import pandas as pd
|
11 |
import torch
|
12 |
-
import torchaudio
|
13 |
-
from torchtyping import TensorType
|
14 |
|
15 |
num_channels, num_frames, height, width = None, None, None, None
|
16 |
|
@@ -73,45 +69,3 @@ def load_pth(pth_path: str) -> Any:
|
|
73 |
def save_pth(data: Any, pth_path: str):
|
74 |
"""Save data in a pth (PyTorch) file."""
|
75 |
torch.save(data, pth_path)
|
76 |
-
|
77 |
-
|
78 |
-
def load_csv(csv_path: str, header: Any = None) -> pd.DataFrame:
|
79 |
-
"""Load a csv file."""
|
80 |
-
try:
|
81 |
-
data = pd.read_csv(csv_path, header=header)
|
82 |
-
except pd.errors.EmptyDataError:
|
83 |
-
data = pd.DataFrame()
|
84 |
-
return data
|
85 |
-
|
86 |
-
|
87 |
-
def save_csv(data: Any, csv_path: str):
|
88 |
-
"""Save data in a csv file."""
|
89 |
-
pd.DataFrame(data).to_csv(csv_path, header=False, index=False)
|
90 |
-
|
91 |
-
|
92 |
-
def load_json(json_path: str, header: Any = None) -> pd.DataFrame:
|
93 |
-
"""Load a json file."""
|
94 |
-
with open(json_path, "r") as f:
|
95 |
-
data = json.load(f)
|
96 |
-
return data
|
97 |
-
|
98 |
-
|
99 |
-
def save_json(data: Any, json_path: str):
|
100 |
-
"""Save data in a json file."""
|
101 |
-
with open(json_path, "w") as json_file:
|
102 |
-
json.dump(data, json_file)
|
103 |
-
|
104 |
-
|
105 |
-
def load_audio(audio_path: str, **kwargs):
|
106 |
-
"""Load an audio file."""
|
107 |
-
waveform, sample_rate = torchaudio.load(audio_path, **kwargs)
|
108 |
-
return waveform, sample_rate
|
109 |
-
|
110 |
-
|
111 |
-
def save_audio(
|
112 |
-
data: TensorType["num_channels", "num_frames"],
|
113 |
-
audio_path: str,
|
114 |
-
sample_rate: int = 44100,
|
115 |
-
):
|
116 |
-
"""Save data in an audio file."""
|
117 |
-
torchaudio.save(audio_path, data, sample_rate)
|
|
|
|
|
1 |
import os
|
2 |
import os.path as osp
|
3 |
import pickle
|
|
|
6 |
|
7 |
import h5py
|
8 |
import numpy as np
|
|
|
9 |
import torch
|
|
|
|
|
10 |
|
11 |
num_channels, num_frames, height, width = None, None, None, None
|
12 |
|
|
|
69 |
def save_pth(data: Any, pth_path: str):
|
70 |
"""Save data in a pth (PyTorch) file."""
|
71 |
torch.save(data, pth_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|