-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_transform.py
More file actions
191 lines (179 loc) · 8.46 KB
/
metadata_transform.py
File metadata and controls
191 lines (179 loc) · 8.46 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import json
from datetime import datetime
def transform_metadata_to_dataverse_format(datasets_root):
for folder_name in os.listdir(datasets_root):
print(folder_name)
folder_path = os.path.join(datasets_root, folder_name)
if not os.path.isdir(folder_path):
continue
metadata_path = os.path.join(folder_path, f"{folder_name}_metadata.json")
if not os.path.exists(metadata_path):
continue
with open(metadata_path, 'r', encoding='utf-8') as f:
original_metadata = json.load(f)
# Extract fields
title = original_metadata.get('name', folder_name)
description = original_metadata.get('description', '')
country = original_metadata.get('country', '')
domain = original_metadata.get('domain', '')
# Check file presence
extensions_present = {
'Image': any(f.endswith('.png') for f in os.listdir(folder_path)),
'Audio': any(f.endswith('.wav') for f in os.listdir(folder_path)),
'Haptic': any(f.endswith('.txt') for f in os.listdir(folder_path))
}
# Construct original art_form_types based on file extensions present
art_form_types = []
for modality, present in extensions_present.items():
if present:
art_form_types.append(modality)
# Retain authorName field with value "KCL"
author_name = "KCL"
# Get current date for publication date
publication_date = datetime.now().strftime('%Y-%m-%d')
# Construct new metadata structure
transformed = {
"datasetVersion": {
"license": "CC BY 4.0",
"metadataBlocks": {
"citation": {
"displayName": "Citation Metadata",
"name": "citation",
"fields": [
{
"typeName": "title",
"multiple": False,
"typeClass": "primitive",
"value": title
},
{
"typeName": "dsDescription",
"multiple": True,
"typeClass": "compound",
"value": [
{
"dsDescriptionValue": {
"typeName": "dsDescriptionValue",
"multiple": False,
"typeClass": "primitive",
"value": description
}
}
]
},
{
"typeName": "author",
"multiple": True,
"typeClass": "compound",
"value": [
{
"authorName": {
"typeName": "authorName",
"multiple": False,
"typeClass": "primitive",
"value": author_name
}
}
]
},
{
"typeName": "subject",
"multiple": True,
"typeClass": "controlledVocabulary",
"value": [
"Arts and Humanities"
]
},
{
"typeName": "keyword",
"multiple": True,
"typeClass": "compound",
"value": [
{
"keywordValue": {
"typeName": "keywordValue",
"multiple": False,
"typeClass": "primitive",
"value": keyword
}
} for keyword in [country, domain] if keyword
]
},
# Retain datasetContact fields as is
{
"typeName": "datasetContact",
"multiple": True,
"typeClass": "compound",
"value": [
{
"datasetContactEmail": {
"typeName": "datasetContactEmail",
"multiple": False,
"typeClass": "primitive",
"value": "info@hb.se"
}
}
]
}
]
},
"MuseIT": {
"displayName": "MuseIT Metadata",
"name": "MuseIT",
"fields": [
{
"typeName": "project",
"multiple": False,
"typeClass": "primitive",
"value": "MuseIT"
},
{
"typeName": "artFormType",
"multiple": True,
"typeClass": "controlledVocabulary",
"value": [
"Image"
]
},
{
"typeName": "ObjectVersion",
"multiple": False,
"typeClass": "primitive",
"value": "1.0"
},
{
"typeName": "accessibilityModality",
"multiple": True,
"typeClass": "controlledVocabulary",
"value": [
"Audio",
"Haptic",
"Visual"
]
},
{
"typeName": "originalityStatus",
"multiple": False,
"typeClass": "controlledVocabulary",
"value": "Representation"
},
{
"typeName": "productionDate",
"multiple": False,
"typeClass": "primitive",
"value": publication_date
}
]
}
}
}
}
# Save to {base_name}.json in the same folder
output_file_path = os.path.join(folder_path, f"{folder_name}.json")
with open(output_file_path, 'w', encoding='utf-8') as out_f:
json.dump(transformed, out_f, indent=2, ensure_ascii=False)
print(f"Saved transformed metadata to: {output_file_path}")
#break # Process only one folder for now
#transform_metadata_to_dataverse_format('./Datasets')
transform_metadata_to_dataverse_format('./Datasets_withmusic')