From 292d237da57a3c845b4daf10e84110e765757a66 Mon Sep 17 00:00:00 2001 From: rizac Date: Mon, 19 Jun 2023 11:13:01 +0200 Subject: [PATCH] fix doc and make yaml_load for internal usage only --- stream2segment/process/__init__.py | 1 - .../templates/The-Segment-object.ipynb | 4 +-- ...g-Stream2segment-in-your-Python-code.ipynb | 7 +++--- .../resources/templates/paramtable.py | 25 +++++++++++-------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/stream2segment/process/__init__.py b/stream2segment/process/__init__.py index 92fd6080..a122d96d 100644 --- a/stream2segment/process/__init__.py +++ b/stream2segment/process/__init__.py @@ -2,7 +2,6 @@ import numpy as np from stream2segment.process.db import get_session -from stream2segment.io import yaml_load # utility functions for users: yaml_load(file) from stream2segment.io.db import close_session from stream2segment.process.db.models import (Segment, Channel, Event, Station, DataCenter, Download, Class) diff --git a/stream2segment/resources/templates/The-Segment-object.ipynb b/stream2segment/resources/templates/The-Segment-object.ipynb index 034b2274..5591808f 100644 --- a/stream2segment/resources/templates/The-Segment-object.ipynb +++ b/stream2segment/resources/templates/The-Segment-object.ipynb @@ -218,7 +218,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -232,7 +232,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.6" + "version": "3.11.0" } }, "nbformat": 4, diff --git a/stream2segment/resources/templates/Using-Stream2segment-in-your-Python-code.ipynb b/stream2segment/resources/templates/Using-Stream2segment-in-your-Python-code.ipynb index 1b902549..12916a0c 100644 --- a/stream2segment/resources/templates/Using-Stream2segment-in-your-Python-code.ipynb +++ b/stream2segment/resources/templates/Using-Stream2segment-in-your-Python-code.ipynb @@ -79,13 +79,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "from stream2segment.process import yaml_load\n", + "import yaml\n", "# uncomment the line below using an existing file on your OS:\n", - "# dburl = yaml_load(download_file.yaml)['dburl']" + "# with open('replace_with_your_download_config_path.yaml', 'r') as fpt:\n", + "# dburl = yaml.safe_load(fpt)['dburl']" ] }, { diff --git a/stream2segment/resources/templates/paramtable.py b/stream2segment/resources/templates/paramtable.py index c3c3ecb9..48bc337d 100644 --- a/stream2segment/resources/templates/paramtable.py +++ b/stream2segment/resources/templates/paramtable.py @@ -37,12 +37,14 @@ def main(segment, config): """Main processing function, called iteratively for any segment selected from `imap` - or `process` functions of stream2segment. See section `if __name__ == "__main__"` - at the end of the module for details. - - IMPORTANT: any exception raised here or from any sub-function will interrupt the - whole processing routine with one special case: `stream2segment.process.SkipSegment` - will resume from the next segment. Raise it to programmatically skip a segment, e.g.: + or `process` functions of stream2segment. If you just created this file with + `s2s init`, see section `if __name__ == "__main__"` at the end of the module for + details. + + IMPORTANT: Any exception raised here or from any sub-function will interrupt the + whole processing routine (`imap` or `process`) with one special case: + `stream2segment.process.SkipSegment` will resume from the next segment. + Raise it to programmatically skip a segment, e.g.: ``` if segment.sample_rate < 60: raise SkipSegment("segment sample rate too low")` @@ -537,15 +539,18 @@ def meanslice(trace, nptmin=100, starttime=None, endtime=None): # Example code: Check and customize before run # ------------------------------------ - from stream2segment.process import yaml_load # similar to yaml.safe_load - # Load config (below we assume to be a YAML file with the same name as this module): + # Setup config: you can build your own dict of parameters or load it from a YAML + # file as in the example below (change path according to your needs): + import yaml config_path = os.path.splitext(os.path.abspath(__file__))[0] + '.yaml' - config = yaml_load(config_path) + with open(config_path, 'r') as fpt: + config = yaml.safe_load(fpt) # get the database URL. Do NOT TYPE anywhere URLs with passwords (e.g. postgres), or # if you do, do not COMMIT the file and keep it local. A good solution is to read # the db URL used for downloading the data from its config. Example: download_path = os.path.join(os.path.dirname(__file__), 'download.yaml') - dburl = yaml_load(download_path)['dburl'] + with open(download_path, 'r') as fpt: + dburl = yaml.safe_load(fpt)['dburl'] # segments to process # For details, see {{ THE_SEGMENT_OBJECT_WIKI_URL_SEGMENT_SELECTION }} # The variable below can also be a list/numpy array of integers denoting the