{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/usnistgov/AFL-agent/blob/main/docs/source/how-to/saving_pipelines.ipynb)\n", "\n", "# Reading and Writing Pipelines \n", "\n", "Its" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Google Colab Setup\n", "\n", "Only uncomment and run the next cell if you are running this notebook in Google Colab or if don't already have the AFL-agent package installed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install git+https://github.com/usnistgov/AFL-agent.git" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing A Pipeline\n", "\n", "To begin, let's load the necessary libraries and define a short pipeline" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PipelineOp input_variable ---> output_variable\n", "---------- -----------------------------------\n", "0 ) measurement ---> derivative\n", "1 ) derivative ---> similarity\n", "2 ) similarity ---> labels\n", "\n", "Input Variables\n", "---------------\n", "0) measurement\n", "\n", "Output Variables\n", "----------------\n", "0) labels\n" ] } ], "source": [ "from AFL.double_agent import *\n", "\n", "with Pipeline('MyPipeline') as my_important_pipeline:\n", "\n", " SavgolFilter(\n", " input_variable='measurement', \n", " output_variable='derivative', \n", " dim='x', \n", " derivative=1\n", " )\n", "\n", " Similarity(\n", " input_variable='derivative', \n", " output_variable='similarity', \n", " sample_dim='sample',\n", " params={'metric': 'laplacian','gamma':1e-4}\n", " )\n", " \n", " SpectralClustering(\n", " input_variable='similarity',\n", " output_variable='labels',\n", " dim='sample',\n", " params={'n_phases': 2}\n", " )\n", "\n", "my_important_pipeline.print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can write the pipeline by simply calling the `.write_json()` method" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pipeline successfully written to pipeline.json.\n" ] } ], "source": [ "my_important_pipeline.write_json('pipeline.json')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Excellent! Let's take a look at the json file that was written. We can inspect it using the json module" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'name': 'MyPipeline',\n", " 'date': '03/04/25 19:59:19-576491',\n", " 'ops': [{'class': 'AFL.double_agent.Preprocessor.SavgolFilter',\n", " 'args': {'input_variable': 'measurement',\n", " 'output_variable': 'derivative',\n", " 'dim': 'x',\n", " 'xlo': None,\n", " 'xhi': None,\n", " 'xlo_isel': None,\n", " 'xhi_isel': None,\n", " 'pedestal': None,\n", " 'npts': 250,\n", " 'derivative': 1,\n", " 'window_length': 31,\n", " 'polyorder': 2,\n", " 'apply_log_scale': True,\n", " 'name': 'SavgolFilter'}},\n", " {'class': 'AFL.double_agent.PairMetric.Similarity',\n", " 'args': {'input_variable': 'derivative',\n", " 'output_variable': 'similarity',\n", " 'sample_dim': 'sample',\n", " 'params': {'metric': 'laplacian', 'gamma': 0.0001},\n", " 'constrain_same': [],\n", " 'constrain_different': [],\n", " 'name': 'SimilarityMetric'}},\n", " {'class': 'AFL.double_agent.Labeler.SpectralClustering',\n", " 'args': {'input_variable': 'similarity',\n", " 'output_variable': 'labels',\n", " 'dim': 'sample',\n", " 'params': {'n_phases': 2},\n", " 'name': 'SpectralClustering',\n", " 'use_silhouette': False}}]}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import json\n", "\n", "with open('pipeline.json','r') as f:\n", " display(json.load(f))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this, we can see that all of the `PipelineOps` are stored in the `ops` keyword with the keyword arguments we specified above. Also included are any default arguments that we didn't explicitly specify. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading a Pipeline\n", "\n", "So the next and final step is to load the pipeline from disk." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PipelineOp input_variable ---> output_variable\n", "---------- -----------------------------------\n", "0 ) measurement ---> derivative\n", "1 ) derivative ---> similarity\n", "2 ) similarity ---> labels\n", "\n", "Input Variables\n", "---------------\n", "0) measurement\n", "\n", "Output Variables\n", "----------------\n", "0) labels\n" ] } ], "source": [ "loaded_pipeline = Pipeline.read_json('pipeline.json')\n", "loaded_pipeline.print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Success!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusion" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.11.10" } }, "nbformat": 4, "nbformat_minor": 2 }