-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_and_save_losta.py
133 lines (116 loc) · 4.84 KB
/
load_and_save_losta.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from datasets import load_dataset
import pandas as pd
import datetime
from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor
import pdb
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import numpy as np
def generate_datetime_list(start_datetime, increase, num_steps):
# Ensure start_datetime is a pandas Timestamp and has time components
if not isinstance(start_datetime, pd.Timestamp):
raise ValueError("start_datetime must be a pandas Timestamp.")
# Set time to 00:00:00 if start_datetime does not include hours, minutes, and seconds
if start_datetime.hour == 0 and start_datetime.minute == 0 and start_datetime.second == 0:
start_datetime = start_datetime.replace(hour=0, minute=0, second=0)
# Extract the number and unit from 'increase'
if increase[:-1].isdigit():
n = int(increase[:-1])
unit = increase[-1]
else:
n = 1 # Default increment if no number is provided
unit = increase
# Determine the increment based on the unit
if unit == 'H':
increment = timedelta(hours=n)
elif unit == 'T':
increment = timedelta(minutes=n)
elif unit == 'D':
increment = timedelta(days=n)
elif unit == 'M':
increment = relativedelta(months=n)
elif unit == 'A-DEC':
increment = relativedelta(years=n)
elif unit == 'W-SUN':
increment = relativedelta(weeks=n)
else:
raise ValueError("Invalid increase value. Must be in ['H', 'T', 'D', 'M', 'A-DEC', 'W-SUN'] with optional 'n' prefix.")
# Generate the list of datetime values
datetime_list = []
for i in range(num_steps):
datetime_list.append(start_datetime + i * increment)
return datetime_list
def process_dataset(name):
ds = load_dataset("Salesforce/lotsa_data", name)
dataset = ds['train']
print('Working on dataset ', name)
dataset = dataset.to_pandas()[:1000]
if name == 'london_smart_meters_with_missing':
dataset = dataset[:800]
elif name == 'kaggle_web_traffic_weekly':
dataset = dataset[:1000]
_df = pd.DataFrame()
for i in tqdm(range(len(dataset))):
start_datetime = dataset['start'][i]
increase = dataset['freq'][i]
if isinstance(dataset['target'][i][0], np.ndarray):
num_steps = len(dataset['target'][i][0])
datetime_list = generate_datetime_list(start_datetime, increase, num_steps)
temp_df = pd.DataFrame({
'date': datetime_list,
'OT': dataset['target'][i][0],
'source': [name] * num_steps
})
else:
num_steps = len(dataset['target'][i])
datetime_list = generate_datetime_list(start_datetime, increase, num_steps)
temp_df = pd.DataFrame({
'date': datetime_list,
'OT': dataset['target'][i],
'source': [name] * num_steps
})
# formatted_string = start_datetime.strftime('%Y-%m-%d %H:%M:%S')
# if formatted_string != str(start_datetime):
# pdb.set_trace()
if temp_df['OT'].isnull().any():
temp_df['OT'] = temp_df['OT'].interpolate(method='linear')
_df = pd.concat([_df, temp_df], ignore_index=True)
return _df
if __name__ == '__main__':
dataset_list = [
'london_smart_meters_with_missing', # Energy - 30T
'bdg-2_bear', # Energy - H
'bdg-2_fox', # Energy - H
'era5_1989', # Climate - H
'era5_1990', # Climate - H
'era5_1991', # Climate - H
'cmip6_2005', # Climate - 6H
'cmip6_2010', # Climate - 6H
'uber_tlc_daily', # Transport - D
'SZ_TAXI', # Transport - 15T
'PEMS03', # Transport - 5T
'weather', # weather - D
'oikolab_weather', # weather - H
'hospital', # Healthcare - M
'kaggle_web_traffic_weekly', # web - W-SUN
'tourism_yearly', # eco - A-DEC
'tourism_monthly', # eco - M
]
# for name in dataset_list:
# ds = load_dataset("Salesforce/lotsa_data", name)
# ds = ds['train'].to_pandas()
# # print(name, ds['freq'][0], ds['start'][0], ds['target'][0], isinstance(ds['target'][0][0], np.ndarray))
# if isinstance(ds['target'][0][0], np.ndarray):
# print(name, len(ds), len(ds)*len(ds['target'][0][0]))
# else:
# print(name, len(ds), len(ds)*len(ds['target'][0]))
# pdb.set_trace()
# for d in dataset_list:
# process_dataset(d)
merged_df = pd.DataFrame()
with ProcessPoolExecutor(max_workers=4) as executor:
results = list(tqdm(executor.map(process_dataset, dataset_list), total=len(dataset_list)))
for result in results:
merged_df = pd.concat([merged_df, result], ignore_index=True)
merged_df.to_csv("all_domain_m2.csv", index=False)