forked from NSAPH-Projects/space-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_spaceenv.py
More file actions
161 lines (134 loc) · 4.92 KB
/
upload_spaceenv.py
File metadata and controls
161 lines (134 loc) · 4.92 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import logging
import os
import shutil
import hydra
import yaml
from omegaconf import DictConfig
from pyDataverse.api import NativeApi
from pyDataverse.models import Datafile
from utils import double_zip_folder
def upload_dataverse_data(
data_path: str,
data_description: str,
dataverse_baseurl: str,
dataverse_pid: str,
token: str,
publish: bool = False,
):
"""
Upload data to the collection
Args:
file_path (str): Filename
description (str): Data file description.
token (str): Dataverse API Token.
"""
status = "Failed"
api = NativeApi(dataverse_baseurl, token)
filename = os.path.basename(data_path)
dataset = api.get_dataset(dataverse_pid)
logging.info("Dataverse APIs created.")
files_list = dataset.json()["data"]["latestVersion"]["files"]
file2id = {f["dataFile"]["filename"]: f["dataFile"]["id"] for f in files_list}
filename_ = filename.replace(".zip.zip", ".zip")
if filename_ not in file2id: # new file
logging.info("File does not exist in selected dataverse. Creating it.")
dataverse_datafile = Datafile()
dataverse_datafile.set(
{
"pid": dataverse_pid,
"filename": filename,
"description": data_description,
}
)
logging.info("File basename: " + filename)
resp = api.upload_datafile(dataverse_pid, data_path, dataverse_datafile.json())
if resp.json()["status"] == "OK":
logging.info("Dataset uploaded.")
status = "OK"
else:
logging.error("Dataset not uploaded.")
logging.error(resp.json())
else:
logging.info("File already exists. Replacing it.")
file_id = file2id[filename_]
json_dict = {
"description": data_description,
"forceReplace": True,
"filename": filename,
}
json_str = json.dumps(json_dict)
resp = api.replace_datafile(file_id, data_path, json_str, is_filepid=False)
if resp.json()["status"] == "ERROR":
logging.error(f"An error at replacing the file: {resp.content}")
else:
logging.info("Dataset replaced.")
status = "OK"
if publish:
resp = api.publish_dataset(dataverse_pid, release_type="major")
if resp.json()["status"] == "OK":
logging.info("Dataset published.")
return status
@hydra.main(config_path="conf", config_name="upload_spaceenv", version_base=None)
def main(cfg: DictConfig):
# verify inputs
assert not cfg.upload or cfg.token is not None, "Token must be provided for upload."
# load metadata
train_dir = f"{hydra.utils.get_original_cwd()}/trained_spaceenvs/{cfg.base_name}"
contents_dir = cfg.base_name
assert os.path.exists(train_dir), f"Train directory {train_dir} not found."
if os.path.exists(contents_dir):
logging.info(f"Target directory {contents_dir} already exists.")
else:
os.mkdir(contents_dir)
# copy relevant files to working directory
shutil.copy(f"{train_dir}/metadata.yaml", contents_dir)
# get ext from filename "{train_dir}/synthetic_data*"
files = os.listdir(train_dir)
ext = None
for f in files:
if "synthetic_data" in f:
ext = ".".join(f.split(".")[1:])
break
if ext is None:
raise ValueError("Synthetic data not found.")
shutil.copy(f"{train_dir}/synthetic_data.{ext}", contents_dir)
shutil.copy(f"{train_dir}/leaderboard.csv", contents_dir)
shutil.copy(f"{train_dir}/counterfactuals.png", contents_dir)
shutil.copy(f"{train_dir}/.hydra/config.yaml", f"{contents_dir}/config.yaml")
# get ext from filename "{train_dir}/graph*"
ext = None
for f in files:
if "graph" in f:
ext = ".".join(f.split(".")[1:])
break
if ext is None:
raise ValueError("Graph not found.")
shutil.copy(f"{train_dir}/graph.{ext}", contents_dir)
# compress folder and double zip
zipfile = double_zip_folder(contents_dir, f"{cfg.base_name}")
# upload to dataverse
with open(f"{contents_dir}/config.yaml", "r") as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
data_description = f"""
This is a synthetic dataset generated for the space project.\n
The dataset was generated using the following configuration:\n
{json.dumps(config)}
"""
if cfg.upload:
status = upload_dataverse_data(
zipfile,
data_description,
cfg.dataverse.baseurl,
cfg.dataverse.pid,
token=cfg.token,
publish=cfg.publish,
)
else:
status = "Upload disabled"
# save a text file with the upload status, used in the pipeline
logging.info(f"Upload status: {status}")
with open("upload_status.txt", "w") as f:
f.write(status)
if __name__ == "__main__":
main()