Created
September 2, 2018 17:15
-
-
Save d-chambers/1b736e1c49a45f3da6fca5bf507e0d38 to your computer and use it in GitHub Desktop.
simple catalog json read/write test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Tests for reading/writting JSON catalogs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import json\n", | |
"from functools import reduce\n", | |
"from operator import add\n", | |
"\n", | |
"import obspy\n", | |
"from obspy.core.util.testing import create_diverse_catalog\n", | |
"from obspy.core.event.dict import dict_to_catalog, catalog_to_dict\n", | |
"import xmltodict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def to_json(cat, path):\n", | |
" cat_dict = catalog_to_dict(cat)\n", | |
" with open(path, 'w') as fi:\n", | |
" json.dump(cat_dict, fi)\n", | |
"\n", | |
"def from_json(path):\n", | |
" with open(path) as fi:\n", | |
" return dict_to_catalog(json.load(fi))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# create a test catalog\n", | |
"cat = reduce(add, [create_diverse_catalog() for _ in range(100)])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"json_path = 'cat.json'\n", | |
"xml_path = 'cat.xml'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Compare speed of writing catalog" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 204 ms, sys: 8.03 ms, total: 212 ms\n", | |
"Wall time: 211 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time cat.write('cat2.json', 'json')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 169 ms, sys: 3.72 ms, total: 172 ms\n", | |
"Wall time: 171 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time to_json(cat, json_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 124 ms, sys: 23 µs, total: 124 ms\n", | |
"Wall time: 122 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time cat.write(xml_path, 'quakeml')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Compare speed of reading catalog" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 351 ms, sys: 66 µs, total: 351 ms\n", | |
"Wall time: 350 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%time cat1 = from_json(json_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.15 s, sys: 96 µs, total: 1.15 s\n", | |
"Wall time: 1.15 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time cat2 = obspy.read_events(xml_path, 'quakeml')" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment