-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.py
89 lines (75 loc) · 2.81 KB
/
media.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pandas as pd
from abc import ABC, abstractmethod
# faceQs modules
from dataset import DatasetFS
from dataset_utils import *
from utils import *
class Media(ABC):
def __init__(self, config, dataset: DatasetFS, filename, store, store_info=True, verbose=0):
self.args = config
self.filename = filename
self.verbose = verbose
self.store_info = store_info
self.dataset = dataset
self.data_frame = dataset.data_frame # main data container (Pandas)
if store_info:
self.merge_info_dataset()
if store:
self.store_media_data()
else:
self.load_media_data()
def info(self):
"""
Dataset description.
"""
verbatimT(1, 1, f"Dataset: {self.args.DATASET_NAME}")
verbatimT(1, 1, f"size: {self.dataset.size()}:", deep=1)
verbatimT(1, 1, f"series: {list(self.data_frame.keys())}:", deep=1)
verbatimT(1, 1, "done.\n")
def set_dataset(
self,
dataset: DatasetFS,
store_info=True,
) -> None:
"""
Sets a new dataset to process for the Media object.
"""
self.dataset = dataset
self.store_info = store_info
self.data_frame = dataset.data_frame # main container (Pandas)
if store_info:
self.merge_info_dataset()
def load_media_data(self) -> None:
"""
Load dataset descriptor form file
Args:
- store_path: full or relative path to store dataset frame
- media_type: the media type
- filename: a file to load data from
"""
assert self.filename is not None, "Filename must be provided"
assert self.filename.endswith(".pkl"), "Filename must be a .pkl file"
pathout = os.path.join(self.args.ROOT, self.args.OUTPUT_DIR, self.filename)
if set_path(pathout, task="check"):
self.data_frame = pd.read_pickle(pathout)
verbatimT(self.verbose, 1, "Dataset descriptor loaded from file: " + pathout)
verbatimO(self.verbose, 1, self.data_frame)
def store_media_data(self):
"""
Stores dataset descriptor to file
- data (self.data_frame) is stored as Pandas
Args:
- store_path: full or relative path to store dataset frame
- media_type: the media type
- filename: a file in which to store data
"""
pathout = os.path.join(self.args.ROOT, self.args.OUTPUT_DIR, self.filename)
self.data_frame.to_pickle(pathout)
verbatimT(self.verbose, 1, "Media descriptor stored into file: " + pathout)
@abstractmethod
def merge_info_dataset():
"""
Merges all Media info in the dataset Dataset as a dict.
The main container to merge is self.data_frame
"""
pass