-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetaMapper.py
320 lines (265 loc) · 12.3 KB
/
metaMapper.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from acquisitionMapper import extract_metadata_addresses, xml_to_dict, extract_values
from imageMapper import readFile, formatMetadata, extractImageMappings, extractImageData, headerMapping
from datasetMapper import extract_metadata_addresses_dataset
import os
import json
import zipfile
import tempfile
import shutil
import time
import sys
import logging
def assign_nested_value(nested_dict, keys_string, value):
keys = keys_string.split('.')
current_dict = nested_dict
for key in keys[:-1]: # We iterate until the penultimate key
if key.endswith('[]'): # The key refers to a list
key = key.rstrip('[]') # Remove the '[]' sign from the key
if key not in current_dict or not isinstance(current_dict[key], list): # If the key doesn't exist or it's not a list, we create an empty list
current_dict[key] = []
if not current_dict[key] or not isinstance(current_dict[key][-1], dict): # If the list is empty or the last element is not a dictionary, we append an empty dictionary
current_dict[key].append({})
current_dict = current_dict[key][-1] # We go one level down to the last dictionary in the list
else:
if key not in current_dict: # If the key doesn't exist, we create an empty dict
current_dict[key] = {}
current_dict = current_dict[key] # We go one level down
if keys[-1].endswith('[]'): # The last key refers to a list
keys[-1] = keys[-1].rstrip('[]') # Remove the '[]' sign from the key
if keys[-1] not in current_dict or not isinstance(current_dict[keys[-1]], list): # If the key doesn't exist or it's not a list, we create an empty list
current_dict[keys[-1]] = []
current_dict[keys[-1]].append(value) # We append the value to the list
else:
current_dict[keys[-1]] = value # We assign the value to the last key
return nested_dict
def extract_zip_file(zip_file_path):
temp_dir = tempfile.mkdtemp()
start_time = time.time() # Start time
logging.info("Extracting {zip_file_path}...")
target_dir = None
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
total_items = len(zip_ref.namelist())
for index, file_name in enumerate(zip_ref.namelist(), start=1):
# if index%10 == 0:
# print(f"Extracting file {index}/{total_items}...")
file_path = os.path.join(temp_dir, file_name)
zip_ref.extract(file_name, temp_dir)
# Look for file has the .emxml extension and designate the directory it's within as the target directory
if file_name.endswith('.emxml') and target_dir is None:
target_dir = os.path.dirname(file_path)
if target_dir is None:
logging.info("No .emxml file found in the zip file.")
return None, None
end_time = time.time() # End time
total_time = end_time - start_time
logging.info(f"Total time taken to process: {total_time:.2f} seconds. The target directory is {target_dir}.")
return target_dir, temp_dir
mapFile = sys.argv[1]
inputZip = sys.argv[2]
outputFile = sys.argv[3]
def getExampleImage(directory):
for file in os.listdir(directory):
if file.endswith('.tif'):
return os.path.join(directory, file)
mainDir, tempDir = extract_zip_file(inputZip)
imgFile = getExampleImage(os.path.join(mainDir, 'Images/SEM Image'))
imgDirectory = os.path.join(mainDir, 'Images')
xmlFile = os.path.join(mainDir, 'EMproject.emxml')
xmlMap, imgMap = extract_metadata_addresses(mapFile)
xmlMetadata = xml_to_dict(xmlFile)
acqXmlMetadata = extract_values(xmlMap, xmlMetadata)
# Read an image for acquisition metadata
imgMetadata = readFile(imgFile)
formattedImgMetadata = formatMetadata(imgMetadata)
extractedImgMetadata = extractImageData(formattedImgMetadata, imgMap)
acqImgMetadata = headerMapping(extractedImgMetadata, imgMap)
# The metadata for the acquisition is then the combined metadata from the xml file and an image
acqMetadata = {**acqXmlMetadata, **acqImgMetadata}
# Read and format dataset metadata
datasetXmlMap, datasetImgMap = extract_metadata_addresses_dataset(mapFile)
datasets = xmlMetadata['EMProject']['Datasets']['Dataset']
# print(f'len = {len(datasets)}, datasets: {datasets}')
if isinstance(datasets, list):
datasetNames = [d['Name'] for d in datasets]
else:
datasetNames = [datasets['Name']]
def processDatasets(datasetNum, imageDirectory):
# Extract xml data for this dataset
mappedEMMetadata = extract_values(datasetXmlMap, xmlMetadata, datasetNum)
# Read data from image in proper folder
datasetName = datasetNames[datasetNum - 1]
for root, dirs, files in os.walk(imageDirectory):
if os.path.basename(root) == datasetName:
for file in files:
if file.endswith('.tif'):
imgPath = os.path.join(root, file)
break
break
imageData = readFile(imgPath)
formattedMetadata = formatMetadata(imageData)
imageMetadata = extractImageData(formattedMetadata, datasetImgMap)
mappedImgMetadata = headerMapping(imageMetadata, datasetImgMap)
return {**mappedEMMetadata, **mappedImgMetadata}
datasetMetadata = []
for i, dataset in enumerate(datasetNames[:2]):
logging.info(i, dataset)
datasetMetadata.append(processDatasets(i+1, imgDirectory))
# Read and format image metadata
imgMappings = extractImageMappings(mapFile)
def processImage(imgPath):
# read image file
rawImgMetadata = readFile(imgPath)
formattedMetadata = formatMetadata(rawImgMetadata)
imageMetadata = extractImageData(formattedMetadata, imgMappings)
mappedImgMetadata = headerMapping(imageMetadata, imgMappings)
return mappedImgMetadata
def processDatasets(datasetNum, imageDirectory):
# Extract xml data for this dataset
mappedEMMetadata = extract_values(datasetXmlMap, xmlMetadata, datasetNum)
# Read data from image in proper folder
datasetName = datasetNames[datasetNum - 1]
for root, dirs, files in os.walk(imageDirectory):
if os.path.basename(root) == datasetName:
for file in files:
if file.endswith('.tif'):
imgPath = os.path.join(root, file)
break
break
imageData = readFile(imgPath)
formattedMetadata = formatMetadata(imageData)
imageMetadata = extractImageData(formattedMetadata, datasetImgMap)
mappedImgMetadata = headerMapping(imageMetadata, datasetImgMap)
# Repeat to produce list of image metadata dictionaries
imageMetadataList = []
for root, dirs, files in os.walk(imageDirectory):
if os.path.basename(root) == datasetName:
for file in files:
if file.endswith('.tif'):
imgPath = os.path.join(root, file)
imageMetadataList.append(processImage(imgPath))
return {**mappedEMMetadata, **mappedImgMetadata}, imageMetadataList
datasetMetadata = []
imageMetadata = []
for i, dataset in enumerate(datasetNames[:-1]):
logging.info(i, dataset)
datasetMetadataDict, ImageMetadataDict = processDatasets(i+1, imgDirectory)
print(f'This is the current dataset: {dataset}.')
datasetMetadataDict['acquisition.dataset[].datasetType'] = dataset
# Determine number of images in each dataset
datasetMetadataDict['acquisition.dataset[].numberOfItems'] = acqMetadata['acquisition.genericMetadata.numberOfCuts']
print(datasetMetadataDict)
datasetMetadata.append(datasetMetadataDict)
imageMetadata.append(ImageMetadataDict)
def combineMetadata(acquisition_metadata, dataset_metadata, image_metadata):
metadata = {}
# Combine acquisition metadata
for key, value in acquisition_metadata.items():
nested_keys = key.split('.')
current_dict = metadata
for nested_key in nested_keys[:-1]:
if nested_key not in current_dict:
current_dict[nested_key] = {}
current_dict = current_dict[nested_key]
current_dict[nested_keys[-1]] = value
# Combine dataset metadata
metadata['acquisition']['dataset'] = []
for dataset in dataset_metadata:
dataset_dict = {}
for key, value in dataset.items():
nested_keys = key.split('.')
nested_keys.remove('acquisition')
try:
nested_keys.remove('dataset')
except:
nested_keys.remove('dataset[]')
current_dict = dataset_dict
for nested_key in nested_keys[:-1]:
if nested_key not in current_dict:
current_dict[nested_key] = {}
current_dict = current_dict[nested_key]
current_dict[nested_keys[-1]] = value
metadata['acquisition']['dataset'].append(dataset_dict)
# Combine image metadata
for i, images in enumerate(image_metadata):
metadata['acquisition']['dataset'][i]['images'] = []
for image in images:
image_dict = {}
for key, value in image.items():
nested_keys = key.split('.')
nested_keys.remove('acquisition')
nested_keys.remove('dataset')
nested_keys.remove('images')
current_dict = image_dict
for nested_key in nested_keys[:-1]:
if nested_key not in current_dict:
current_dict[nested_key] = {}
current_dict = current_dict[nested_key]
current_dict[nested_keys[-1]] = value
metadata['acquisition']['dataset'][i]['images'].append(image_dict)
return metadata
def parseNumericValues(metadata):
for key, value in metadata.items():
if isinstance(value, dict):
# Recursive call for nested dictionaries
parseNumericValues(value)
elif isinstance(value, list):
# Iterate through the list, applying parseNumericValues to each item if it's a dictionary
for i, item in enumerate(value):
if isinstance(item, dict):
parseNumericValues(item)
else:
# Attempt conversion for non-dictionary items in the list
value[i] = convertToNumeric(item)
else:
# Attempt conversion for non-list, non-dictionary items
metadata[key] = convertToNumeric(value)
def convertToNumeric(value):
try:
# Try converting to float first
numeric_value = float(value)
# If the float is actually an int, convert it to int
if numeric_value.is_integer():
return int(numeric_value)
else:
return numeric_value
except (ValueError, TypeError):
# If conversion fails, return the original value
return value
def save_metadata_as_json(metadata, save_path):
with open(save_path, 'w') as file:
json.dump(metadata, file, indent=4)
logging.info(f"Metadata saved as {save_path}")
# # For local tests
# def save_metadata_as_json(metadata, save_path):
# with open(os.path.join(save_path, 'output.json'), 'w') as file:
# json.dump(metadata, file, indent=4)
# logging.info(f"Metadata saved as {save_path}")
def fixBooleans(d):
if isinstance(d, dict):
for k, v in d.items():
if isinstance(v, str):
v = v.lower().strip()
if v == 'on' or v == 'yes':
d[k] = True
elif v == 'off' or v == 'no':
d[k] = False
elif isinstance(v, list):
for i in range(len(v)):
v[i] = fixBooleans(v[i])
else:
d[k] = fixBooleans(v)
elif isinstance(d, list):
for i in range(len(d)):
d[i] = fixBooleans(d[i])
return d
def cleanMetadata(nestedDict):
x1 = assign_nested_value(nestedDict, 'acquisition.dataset[].definition', 'acquisition_dataset')
x2 = assign_nested_value(x1, 'acquisition.dataset[].numberOfItems', '')
x3 = assign_nested_value(x2, 'acquisition.dataset[].images[].definition', 'acquisition_image')
x4 = fixBooleans(x3)
return x4
combinedMetadata = combineMetadata(acqMetadata, datasetMetadata, imageMetadata)
parseNumericValues(combinedMetadata)
cleanedMetadataDict = cleanMetadata(combinedMetadata)
save_metadata_as_json(cleanedMetadataDict, outputFile)
shutil.rmtree(tempDir)