From 434ec3f75dc275144ae8fc5e1e34b6377fa4eef8 Mon Sep 17 00:00:00 2001 From: Elias Vitali Date: Fri, 9 Jun 2023 17:22:45 +0200 Subject: [PATCH] cleaning before weekend --- SOPost.ipynb | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 SOPost.ipynb diff --git a/SOPost.ipynb b/SOPost.ipynb new file mode 100644 index 0000000..9d1e499 --- /dev/null +++ b/SOPost.ipynb @@ -0,0 +1,209 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 17, + "id": "4cd9e531", + "metadata": {}, + "outputs": [], + "source": [ + "acquisition_metadata = {\n", + "'acquisition.genericMetadata.program.programName': 'Auto Slice & View 4',\n", + " 'acquisition.genericMetadata.program.programVersion': '4.2.1.1982',\n", + " 'acquisition.genericMetadata.applicationId.identifierValue': 'ASV',\n", + " 'acquisition.genericMetadata.fileVersion': '1.2',\n", + " 'acquisition.genericMetadata.projectName': '20200818_AlSi13 XRM tomo2',\n", + " 'acquisition.genericMetadata.numberOfCuts': '719',\n", + "}\n", + "\n", + "dataset_metadata = [\n", + " {\n", + " 'acquisition.dataset.rows': '1',\n", + " 'acquisition.dataset.columns': '1',\n", + " },\n", + " {\n", + " 'acquisition.dataset.rows': '1',\n", + " 'acquisition.dataset.columns': '1',\n", + " },\n", + "]\n", + "\n", + "image_metadata = [\n", + " [\n", + " {\n", + " 'acquisition.dataset.images.creationTime': '18.08.2020 17:51:07',\n", + " 'acquisition.dataset.images.stage.workingDistance.value': '0.00403678',\n", + " },\n", + " {\n", + " 'acquisition.dataset.images.creationTime': '18.08.2020 18:09:06',\n", + " 'acquisition.dataset.images.stage.workingDistance.value': '0.00403773',\n", + " }\n", + " ],\n", + " [\n", + " {\n", + " 'acquisition.dataset.images.creationTime': '18.08.2020 17:51:07',\n", + " 'acquisition.dataset.images.stage.workingDistance.value': '0.00403678',\n", + " },\n", + " {\n", + " 'acquisition.dataset.images.creationTime': '18.08.2020 18:09:06',\n", + " 'acquisition.dataset.images.stage.workingDistance.value': '0.00403773',\n", + " }\n", + " ]\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "1c57c08f", + "metadata": {}, + "outputs": [], + "source": [ + "def combineMetadata(acquisition_metadata, dataset_metadata, image_metadata): \n", + " metadata = {}\n", + " # Combine acquisition metadata\n", + " for key, value in acquisition_metadata.items():\n", + " nested_keys = key.split('.')\n", + " current_dict = metadata\n", + "\n", + " for nested_key in nested_keys[:-1]:\n", + " if nested_key not in current_dict:\n", + " current_dict[nested_key] = {}\n", + " current_dict = current_dict[nested_key]\n", + "\n", + " current_dict[nested_keys[-1]] = value\n", + "\n", + " # Combine dataset metadata\n", + " metadata['acquisition']['dataset'] = []\n", + " for dataset in dataset_metadata:\n", + " dataset_dict = {}\n", + " for key, value in dataset.items():\n", + " nested_keys = key.split('.')\n", + " nested_keys.remove('acquisition')\n", + " nested_keys.remove('dataset')\n", + " current_dict = dataset_dict\n", + "\n", + " for nested_key in nested_keys[:-1]:\n", + " if nested_key not in current_dict:\n", + " current_dict[nested_key] = {}\n", + " current_dict = current_dict[nested_key]\n", + "\n", + " current_dict[nested_keys[-1]] = value\n", + "\n", + " metadata['acquisition']['dataset'].append(dataset_dict)\n", + "\n", + " # Combine image metadata\n", + " for i, images in enumerate(image_metadata):\n", + " metadata['acquisition']['dataset'][i]['images'] = []\n", + " for image in images:\n", + " image_dict = {}\n", + " for key, value in image.items():\n", + " nested_keys = key.split('.')\n", + " nested_keys.remove('acquisition')\n", + " nested_keys.remove('dataset')\n", + " nested_keys.remove('images')\n", + " current_dict = image_dict\n", + "\n", + " for nested_key in nested_keys[:-1]:\n", + " if nested_key not in current_dict:\n", + " current_dict[nested_key] = {}\n", + " current_dict = current_dict[nested_key]\n", + "\n", + " current_dict[nested_keys[-1]] = value\n", + "\n", + " metadata['acquisition']['dataset'][i]['images'].append(image_dict)\n", + " return metadata" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "07e28c03", + "metadata": {}, + "outputs": [], + "source": [ + "def save_metadata_as_json(metadata, save_path):\n", + " filename = os.path.join(save_path, \"combined.json\")\n", + " with open(filename, 'w') as file:\n", + " json.dump(metadata, file, indent=4)\n", + " print(f\"Metadata saved as {filename}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "0f181043", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Metadata saved as /Users/elias/Desktop/PP13_Mapping/pp13-mapper/result_jsons/combined.json\n" + ] + } + ], + "source": [ + "output_path = '/Users/elias/Desktop/PP13_Mapping/pp13-mapper/result_jsons'\n", + "combinedMetadata = combineMetadata(acquisition_metadata, dataset_metadata, image_metadata)\n", + "save_metadata_as_json(metadata, output_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "c84cd04c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'acquisition': {'genericMetadata': {'program': {'programName': 'Auto Slice & View 4',\n", + " 'programVersion': '4.2.1.1982'},\n", + " 'applicationId': {'identifierValue': 'ASV'},\n", + " 'fileVersion': '1.2',\n", + " 'projectName': '20200818_AlSi13 XRM tomo2',\n", + " 'numberOfCuts': '719'},\n", + " 'dataset': {'rows': '1',\n", + " 'columns': '1',\n", + " 'images': {'creationTime': '18.08.2020 18:09:06',\n", + " 'stage': {'workingDistance': {'value': '0.00403773'}}}}}}" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cad5a1c6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}