Skip to content

Instantly share code, notes, and snippets.

@dangunter
Created February 11, 2022 04:01
Show Gist options
  • Save dangunter/5f675578b87a55c11efd31ca1883512f to your computer and use it in GitHub Desktop.
Save dangunter/5f675578b87a55c11efd31ca1883512f to your computer and use it in GitHub Desktop.
Pysmo serialization code example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "bbe96d7e",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import pyomo.environ as pyo\n",
"from idaes.surrogate.pysmo.polynomial_regression import PolynomialRegression\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bc4bdc5d",
"metadata": {},
"outputs": [],
"source": [
"def serialize_terms(feature_vector, terms):\n",
" columns = list(feature_vector)\n",
" termlist = [serialize_term(t) for t in terms]\n",
" result = {\"data_columns\": columns, \"terms\": termlist}\n",
" return result\n",
"\n",
"def serialize_term(t):\n",
" args = [str(p.index()) for p in t.args]\n",
" return (t.name, args)\n",
"\n",
"def deserialize_terms(j):\n",
" result = []\n",
" columns = j[\"data_columns\"]\n",
" param = build_param(columns)\n",
" for term in j[\"terms\"]:\n",
" func_name, index = term\n",
" func = getattr(pyo, func_name)\n",
" # XXX: this can only handle one index\n",
" result.append(func(param[index[0]]))\n",
" return result\n",
"\n",
"def build_param(columns):\n",
" # XXX: we want IndexedParam, but this is returning instead\n",
" # XXX: the type 'Unattached _ParamData'\n",
" p = pyo.Param(columns, mutable=True, initialize=0)\n",
" p.index_set().construct()\n",
" p.construct()\n",
" return p\n",
" \n",
"def print_terms(terms):\n",
" termlist = [t.to_string() for t in terms]\n",
" print(\",\".join(termlist))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d09089c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"===========================Polynomial Regression===============================================\n",
"\n",
"The number of cross-validation cases (3) is used.\n",
"The default training/cross-validation split of 0.75 is used.\n",
"No iterations will be run.\n",
"Default parameter estimation method is used.\n",
"Parameter estimation method: pyomo \n",
"\n"
]
}
],
"source": [
"xy_data = pd.DataFrame.from_dict({'A': [1,2,3], 'B': [4,5,6], 'C': [4,3,9], \n",
" 'D':[1,12,24]}, orient='index', \n",
" columns=['X1', 'X2', 'Y'])\n",
"A = PolynomialRegression(xy_data, xy_data,\n",
" maximum_polynomial_order=2)\n",
"p = A.get_feature_vector()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6f9add47",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"data_columns\": [\"X1\", \"X2\"], \"terms\": [[\"sin\", [\"X1\"]], [\"cos\", [\"X1\"]]]}\n",
"sin(IndexedParam[X1]),cos(IndexedParam[X1])\n",
"sin([Unattached _ParamData]),cos([Unattached _ParamData])\n"
]
}
],
"source": [
"add_terms = [ pyo.sin(p['X1']) , pyo.cos(p['X1']) ]\n",
"j = serialize_terms(p, add_terms)\n",
"print(json.dumps(j))\n",
"rt_terms = deserialize_terms(j)\n",
"print_terms(add_terms)\n",
"print_terms(rt_terms)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "396d5383",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment