lume-model 1.7.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. lume_model-1.7.1/.gitattributes +1 -0
  2. lume_model-1.7.1/.github/workflows/build_docs.yml +29 -0
  3. lume_model-1.7.1/.github/workflows/publish.yml +31 -0
  4. lume_model-1.7.1/.github/workflows/test_build.yml +45 -0
  5. lume_model-1.7.1/.github/workflows/test_build_docs.yml +27 -0
  6. lume_model-1.7.1/.gitignore +138 -0
  7. lume_model-1.7.1/.pre-commit-config.yaml +46 -0
  8. lume_model-1.7.1/LICENSE +42 -0
  9. lume_model-1.7.1/PKG-INFO +320 -0
  10. lume_model-1.7.1/README.md +243 -0
  11. lume_model-1.7.1/dev-environment.yml +21 -0
  12. lume_model-1.7.1/docs/index.md +121 -0
  13. lume_model-1.7.1/docs/models.md +16 -0
  14. lume_model-1.7.1/docs/utils.md +8 -0
  15. lume_model-1.7.1/docs/variables.md +11 -0
  16. lume_model-1.7.1/environment.yml +9 -0
  17. lume_model-1.7.1/examples/custom_model.ipynb +131 -0
  18. lume_model-1.7.1/examples/torch_model.ipynb +266 -0
  19. lume_model-1.7.1/lume_model/__init__.py +4 -0
  20. lume_model-1.7.1/lume_model/_version.py +16 -0
  21. lume_model-1.7.1/lume_model/base.py +379 -0
  22. lume_model-1.7.1/lume_model/models/__init__.py +46 -0
  23. lume_model-1.7.1/lume_model/models/torch_model.py +399 -0
  24. lume_model-1.7.1/lume_model/models/torch_module.py +179 -0
  25. lume_model-1.7.1/lume_model/utils.py +224 -0
  26. lume_model-1.7.1/lume_model/variables.py +460 -0
  27. lume_model-1.7.1/lume_model.egg-info/PKG-INFO +320 -0
  28. lume_model-1.7.1/lume_model.egg-info/SOURCES.txt +51 -0
  29. lume_model-1.7.1/lume_model.egg-info/dependency_links.txt +1 -0
  30. lume_model-1.7.1/lume_model.egg-info/requires.txt +15 -0
  31. lume_model-1.7.1/lume_model.egg-info/top_level.txt +1 -0
  32. lume_model-1.7.1/mkdocs.yml +61 -0
  33. lume_model-1.7.1/pyproject.toml +68 -0
  34. lume_model-1.7.1/requirements.txt +3 -0
  35. lume_model-1.7.1/setup.cfg +4 -0
  36. lume_model-1.7.1/tests/__init__.py +0 -0
  37. lume_model-1.7.1/tests/conftest.py +159 -0
  38. lume_model-1.7.1/tests/models/test_models.py +16 -0
  39. lume_model-1.7.1/tests/models/test_torch_model.py +267 -0
  40. lume_model-1.7.1/tests/models/test_torch_module.py +223 -0
  41. lume_model-1.7.1/tests/test_base.py +85 -0
  42. lume_model-1.7.1/tests/test_files/california_regression/input_transformers_0.pt +0 -0
  43. lume_model-1.7.1/tests/test_files/california_regression/model.pt +0 -0
  44. lume_model-1.7.1/tests/test_files/california_regression/model_info.json +48 -0
  45. lume_model-1.7.1/tests/test_files/california_regression/normalization.json +28 -0
  46. lume_model-1.7.1/tests/test_files/california_regression/output_transformers_0.pt +0 -0
  47. lume_model-1.7.1/tests/test_files/california_regression/test_input_tensor.pt +0 -0
  48. lume_model-1.7.1/tests/test_files/california_regression/torch_model.yml +50 -0
  49. lume_model-1.7.1/tests/test_files/california_regression/torch_module.yml +54 -0
  50. lume_model-1.7.1/tests/test_files/california_regression/variables.yml +43 -0
  51. lume_model-1.7.1/tests/test_files/example_input_image.npy +0 -0
  52. lume_model-1.7.1/tests/test_utils.py +40 -0
  53. lume_model-1.7.1/tests/test_variables.py +398 -0
@@ -0,0 +1 @@
1
+ lume_model/_version.py export-subst
@@ -0,0 +1,29 @@
1
+ name: Build docs
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+
8
+ jobs:
9
+ build:
10
+ if: ${{ github.repository == 'slaclab/lume-model' }}
11
+ runs-on: ubuntu-latest
12
+ name: Build docs
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: conda-incubator/setup-miniconda@v3
16
+ with:
17
+ python-version: 3.9
18
+ mamba-version: "*"
19
+ channels: conda-forge
20
+ activate-environment: lume-model-dev
21
+ - name: Build package
22
+ shell: bash -l {0}
23
+ run: |
24
+ pip install -e ".[docs]"
25
+ - name: Build docs
26
+ shell: bash -l {0}
27
+ run: |
28
+ mkdocs build
29
+ mkdocs gh-deploy --force
@@ -0,0 +1,31 @@
1
+ name: Publish Python distributions to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ build-n-publish:
9
+ if: ${{ github.repository == 'slaclab/lume-model' }}
10
+ name: Build and publish Python distributions to PyPI
11
+ runs-on: ubuntu-latest
12
+ environment: deployment
13
+ permissions:
14
+ id-token: write
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ with:
19
+ fetch-depth: 0
20
+ - name: Set up Python 3.12
21
+ uses: actions/setup-python@v5
22
+ with:
23
+ python-version: "3.12"
24
+ - name: Install build
25
+ run: |
26
+ python -m pip install build --user
27
+ - name: Build a source tarball
28
+ run: |
29
+ python -m build
30
+ - name: Publish distribution to PyPI
31
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,45 @@
1
+ name: Test build lume-model
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+ types: [opened, synchronize, reopened]
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ${{ matrix.os }}
11
+ strategy:
12
+ matrix:
13
+ os: [ubuntu-latest]
14
+ python-version: ["3.9", "3.11", "3.12"]
15
+
16
+ name: Python ${{ matrix.python-version }}, ${{ matrix.os }} build
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - uses: conda-incubator/setup-miniconda@v3
20
+ with:
21
+ python-version: ${{ matrix.python-version }}
22
+ miniforge-version: latest
23
+ use-mamba: true
24
+ channels: conda-forge
25
+ activate-environment: lume-model-dev
26
+
27
+ - name: Install lume-model
28
+ shell: bash -l {0}
29
+ run: |
30
+ pip install -e ".[dev]"
31
+
32
+ - name: Run Tests
33
+ shell: bash -l {0}
34
+ run: |
35
+ pytest -ra --pyargs tests
36
+
37
+ - name: Install build
38
+ shell: bash -l {0}
39
+ run: |
40
+ python -m pip install build --user
41
+
42
+ - name: Build a source tarball
43
+ shell: bash -l {0}
44
+ run: |
45
+ python -m build
@@ -0,0 +1,27 @@
1
+ name: Build docs
2
+
3
+ on:
4
+ pull_request:
5
+ types: [opened, synchronize, reopened]
6
+
7
+ jobs:
8
+ build:
9
+ if: ${{ github.repository == 'slaclab/lume-model' }}
10
+ runs-on: ubuntu-latest
11
+ name: Build docs
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: conda-incubator/setup-miniconda@v3
15
+ with:
16
+ python-version: 3.9
17
+ mamba-version: "*"
18
+ channels: conda-forge
19
+ activate-environment: lume-model-dev
20
+ - name: Build package
21
+ shell: bash -l {0}
22
+ run: |
23
+ pip install -e ".[docs]"
24
+ - name: Build docs
25
+ shell: bash -l {0}
26
+ run: |
27
+ mkdocs build
@@ -0,0 +1,138 @@
1
+ # Auto-generated version file
2
+ lume_model/_version.py
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ pip-wheel-metadata/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ target/
79
+
80
+ # Jupyter Notebook
81
+ .ipynb_checkpoints
82
+
83
+ # IPython
84
+ profile_default/
85
+ ipython_config.py
86
+
87
+ # pyenv
88
+ .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
98
+ __pypackages__/
99
+
100
+ # Celery stuff
101
+ celerybeat-schedule
102
+ celerybeat.pid
103
+
104
+ # SageMath parsed files
105
+ *.sage.py
106
+
107
+ # Environments
108
+ .env
109
+ .venv
110
+ env/
111
+ venv/
112
+ ENV/
113
+ env.bak/
114
+ venv.bak/
115
+
116
+ # Spyder project settings
117
+ .spyderproject
118
+ .spyproject
119
+
120
+ # PyCharm project settings
121
+ .idea
122
+
123
+ # Rope project settings
124
+ .ropeproject
125
+
126
+ # mkdocs documentation
127
+ /site
128
+
129
+ # mypy
130
+ .mypy_cache/
131
+ .dmypy.json
132
+ dmypy.json
133
+
134
+ # Pyre type checker
135
+ .pyre/
136
+
137
+ # macOS file system
138
+ .DS_Store
@@ -0,0 +1,46 @@
1
+ # See https://pre-commit.com for more information
2
+ # See https://pre-commit.com/hooks.html for more hooks
3
+ repos:
4
+ - repo: https://github.com/pre-commit/pre-commit-hooks.git
5
+ rev: v4.6.0
6
+ hooks:
7
+ - id: no-commit-to-branch
8
+ - id: trailing-whitespace
9
+ - id: end-of-file-fixer
10
+ - id: check-ast
11
+ - id: check-case-conflict
12
+ - id: check-json
13
+ - id: check-merge-conflict
14
+ - id: check-symlinks
15
+ - id: check-xml
16
+ - id: check-yaml
17
+ exclude: "^(python/lume_model/_version.py)$"
18
+
19
+ - repo: https://github.com/astral-sh/ruff-pre-commit
20
+ rev: v0.5.0
21
+ hooks:
22
+ - id: ruff
23
+ args: [--fix]
24
+ - id: ruff-format
25
+
26
+ #- repo: local
27
+ # hooks:
28
+ # - id: black
29
+ # name: black
30
+ # entry: black
31
+ # language: python
32
+ # language_version: python3
33
+ # types: [python]
34
+ # args: # arguments to configure black
35
+ # - --line-length=88
36
+ # - id: flake8
37
+ # name: flake8
38
+ # entry: flake8
39
+ # language: python
40
+ # language_version: python3
41
+ # types: [python]
42
+ # # only in lume_services
43
+ # files: ^lume_services
44
+ # args: # arguments to configure flake8
45
+ # # making isort line length compatible with black
46
+ # - "--max-line-length=88"
@@ -0,0 +1,42 @@
1
+ Copyright (c) 2017-2020, The Board of Trustees of the Leland Stanford
2
+ Junior University, through SLAC National Accelerator Laboratory
3
+ (subject to receipt of any required approvals from the U.S. Dept. of
4
+ Energy). All rights reserved. Redistribution and use in source and
5
+ binary forms, with or without modification, are permitted provided
6
+ that the following conditions are met:
7
+
8
+ (1) Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ (2) Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ (3) Neither the name of the Leland Stanford Junior University, SLAC
16
+ National Accelerator Laboratory, U.S. Dept. of Energy nor the names of
17
+ its contributors may be used to endorse or promote products derived
18
+ from this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER, THE UNITED STATES GOVERNMENT, OR CONTRIBUTORS BE LIABLE FOR ANY
25
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
+
33
+ You are under no obligation whatsoever to provide any bug fixes,
34
+ patches, or upgrades to the features, functionality or performance of
35
+ the source code ("Enhancements") to anyone; however, if you choose to
36
+ make your Enhancements available either publicly, or directly to SLAC
37
+ National Accelerator Laboratory, without imposing a separate written
38
+ license agreement for such Enhancements, then you hereby grant the
39
+ following license: a non-exclusive, royalty-free perpetual license to
40
+ install, use, modify, prepare derivative works, incorporate into other
41
+ computer software, distribute, and sublicense such Enhancements or
42
+ derivative works thereof, in binary and source code form.
@@ -0,0 +1,320 @@
1
+ Metadata-Version: 2.1
2
+ Name: lume-model
3
+ Version: 1.7.1
4
+ Summary: Data structures used in the LUME modeling toolset.
5
+ Author: SLAC National Accelerator Laboratory
6
+ License: Copyright (c) 2017-2020, The Board of Trustees of the Leland Stanford
7
+ Junior University, through SLAC National Accelerator Laboratory
8
+ (subject to receipt of any required approvals from the U.S. Dept. of
9
+ Energy). All rights reserved. Redistribution and use in source and
10
+ binary forms, with or without modification, are permitted provided
11
+ that the following conditions are met:
12
+
13
+ (1) Redistributions of source code must retain the above copyright
14
+ notice, this list of conditions and the following disclaimer.
15
+
16
+ (2) Redistributions in binary form must reproduce the above copyright
17
+ notice, this list of conditions and the following disclaimer in the
18
+ documentation and/or other materials provided with the distribution.
19
+
20
+ (3) Neither the name of the Leland Stanford Junior University, SLAC
21
+ National Accelerator Laboratory, U.S. Dept. of Energy nor the names of
22
+ its contributors may be used to endorse or promote products derived
23
+ from this software without specific prior written permission.
24
+
25
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
+ OWNER, THE UNITED STATES GOVERNMENT, OR CONTRIBUTORS BE LIABLE FOR ANY
30
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
35
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+
38
+ You are under no obligation whatsoever to provide any bug fixes,
39
+ patches, or upgrades to the features, functionality or performance of
40
+ the source code ("Enhancements") to anyone; however, if you choose to
41
+ make your Enhancements available either publicly, or directly to SLAC
42
+ National Accelerator Laboratory, without imposing a separate written
43
+ license agreement for such Enhancements, then you hereby grant the
44
+ following license: a non-exclusive, royalty-free perpetual license to
45
+ install, use, modify, prepare derivative works, incorporate into other
46
+ computer software, distribute, and sublicense such Enhancements or
47
+ derivative works thereof, in binary and source code form.
48
+
49
+ Project-URL: Homepage, https://github.com/slaclab/lume-model
50
+ Project-URL: Documentation, https://slaclab.github.io/lume-model/
51
+ Project-URL: Bug Tracker, https://github.com/slaclab/lume-model/issues
52
+ Keywords: machine learning,accelerator physics
53
+ Classifier: Natural Language :: English
54
+ Classifier: Programming Language :: Python :: 3 :: Only
55
+ Classifier: Programming Language :: Python :: 3.9
56
+ Classifier: Programming Language :: Python :: 3.10
57
+ Classifier: Programming Language :: Python :: 3.11
58
+ Classifier: Programming Language :: Python :: 3.12
59
+ Classifier: Intended Audience :: Science/Research
60
+ Classifier: Topic :: Scientific/Engineering
61
+ Requires-Python: >=3.9
62
+ Description-Content-Type: text/markdown
63
+ License-File: LICENSE
64
+ Requires-Dist: pydantic
65
+ Requires-Dist: numpy
66
+ Requires-Dist: pyyaml
67
+ Provides-Extra: dev
68
+ Requires-Dist: botorch<0.11; extra == "dev"
69
+ Requires-Dist: pre-commit; extra == "dev"
70
+ Requires-Dist: ruff; extra == "dev"
71
+ Requires-Dist: pytest; extra == "dev"
72
+ Provides-Extra: docs
73
+ Requires-Dist: mkdocs; extra == "docs"
74
+ Requires-Dist: mkdocstrings; extra == "docs"
75
+ Requires-Dist: mkdocstrings-python; extra == "docs"
76
+ Requires-Dist: mkdocs-material; extra == "docs"
77
+
78
+ # LUME-model
79
+
80
+ LUME-model holds data structures used in the LUME modeling toolset. Variables and models built using LUME-model will be compatible with other tools. LUME-model uses [pydantic](https://pydantic-docs.helpmanual.io/) models to enforce typed attributes upon instantiation.
81
+
82
+ ## Requirements
83
+
84
+ * Python >= 3.9
85
+ * pydantic
86
+ * numpy
87
+
88
+ ## Install
89
+
90
+ LUME-model can be installed with conda using the command:
91
+
92
+ ``` $ conda install lume-model -c conda-forge ```
93
+
94
+ ## Developer
95
+
96
+ A development environment may be created using the packaged `dev-environment.yml` file.
97
+
98
+ ```
99
+ conda env create -f dev-environment.yml
100
+ ```
101
+
102
+ ## Variables
103
+
104
+ The lume-model variables are intended to enforce requirements for input and output variables by variable type. For now, only scalar variables (floats) are supported.
105
+
106
+ Minimal example of scalar input and output variables:
107
+
108
+ ```python
109
+ from lume_model.variables import ScalarInputVariable, ScalarOutputVariable
110
+
111
+ input_variable = ScalarInputVariable(
112
+ name="example_input",
113
+ default=0.1,
114
+ value_range=[0.0, 1.0],
115
+ )
116
+ output_variable = ScalarOutputVariable(name="example_output")
117
+ ```
118
+
119
+ All input variables may be made into constants by passing the `is_constant=True` keyword argument. Value assingments on these constant variables will raise an error message.
120
+
121
+ ## Models
122
+
123
+ The lume-model base class `lume_model.base.LUMEBaseModel` is intended to guide user development while allowing for flexibility and customizability. It is used to enforce LUME tool compatible classes for the execution of trained models.
124
+
125
+ Requirements for model classes:
126
+
127
+ * input_variables: A list defining the input variables for the model. Variable names must be unique. Required for use with lume-epics tools.
128
+ * output_variables: A list defining the output variables for the model. Variable names must be unique. Required for use with lume-epics tools.
129
+ * evaluate: The evaluate method is called by the serving model. Subclasses must implement this method, accepting and returning a dictionary.
130
+
131
+ Example model implementation and instantiation:
132
+
133
+ ```python
134
+ from lume_model.base import LUMEBaseModel
135
+ from lume_model.variables import ScalarInputVariable, ScalarOutputVariable
136
+
137
+
138
+ class ExampleModel(LUMEBaseModel):
139
+ def evaluate(self, input_dict):
140
+ output_dict = {
141
+ "output1": input_dict[self.input_variables[0].name] ** 2,
142
+ "output2": input_dict[self.input_variables[1].name] ** 2,
143
+ }
144
+ return output_dict
145
+
146
+
147
+ input_variables = [
148
+ ScalarInputVariable(name="input1", default=0.1, value_range=[0.0, 1.0]),
149
+ ScalarInputVariable(name="input2", default=0.2, value_range=[0.0, 1.0]),
150
+ ]
151
+ output_variables = [
152
+ ScalarOutputVariable(name="output1"),
153
+ ScalarOutputVariable(name="output2"),
154
+ ]
155
+
156
+ m = ExampleModel(input_variables=input_variables, output_variables=output_variables)
157
+ ```
158
+
159
+ ## Configuration files
160
+
161
+ Models and variables may be constructed using a YAML configuration file. The configuration file consists of three sections:
162
+
163
+ * model (optional, can alternatively pass a custom model class into the `model_from_yaml` method)
164
+ * input_variables
165
+ * output_variables
166
+
167
+ The model section is used for the initialization of model classes. The `model_class` entry is used to specify the model class to initialize. The `model_from_yaml` method will attempt to import the specified class. Additional model-specific requirements may be provided. These requirements will be checked before model construction. Model keyword arguments may be passed via the config file or with the function kwarg `model_kwargs`. All models are assumed to accept `input_variables` and `output_variables` as keyword arguments.
168
+
169
+ For example, `m.dump("example_model.yml")` writes the following to file
170
+
171
+ ```yaml
172
+ model_class: ExampleModel
173
+ input_variables:
174
+ input1:
175
+ variable_type: scalar
176
+ default: 0.1
177
+ is_constant: false
178
+ value_range: [0.0, 1.0]
179
+ input2:
180
+ variable_type: scalar
181
+ default: 0.2
182
+ is_constant: false
183
+ value_range: [0.0, 1.0]
184
+ output_variables:
185
+ output1: {variable_type: scalar}
186
+ output2: {variable_type: scalar}
187
+ ```
188
+
189
+ and can be loaded by simply passing the file to the model constructor:
190
+
191
+ ```python
192
+ from lume_model.base import LUMEBaseModel
193
+
194
+
195
+ class ExampleModel(LUMEBaseModel):
196
+ def evaluate(self, input_dict):
197
+ output_dict = {
198
+ "output1": input_dict[self.input_variables[0].name] ** 2,
199
+ "output2": input_dict[self.input_variables[1].name] ** 2,
200
+ }
201
+ return output_dict
202
+
203
+
204
+ m = ExampleModel("example_model.yml")
205
+ ```
206
+
207
+ ## PyTorch Toolkit
208
+
209
+ In the same way as the KerasModel, a PyTorchModel can also be loaded using the `lume_model.utils.model_from_yaml` method, specifying `PyTorchModel` in the `model_class` of the configuration file.
210
+
211
+ ```yaml
212
+ model:
213
+ kwargs:
214
+ model_file: /path/to/california_regression.pt
215
+ model_class: lume_model.torch.PyTorchModel
216
+ model_info: path/to/model_info.json
217
+ output_format:
218
+ type: tensor
219
+ requirements:
220
+ torch: 1.12
221
+ ```
222
+
223
+ In addition to the model_class, we also specify the path to the pytorch model (saved using `torch.save()`) and additional information about the model through the `model_info.json` file such as the order of the feature names and outputs of the model:
224
+
225
+ ```json
226
+ {
227
+ "train_input_mins": [
228
+ 0.4999000132083893,
229
+ ...
230
+ -124.3499984741211
231
+ ],
232
+ "train_input_maxs": [
233
+ 15.000100135803223,
234
+ ...
235
+ -114.30999755859375
236
+ ],
237
+ "model_in_list": [
238
+ "MedInc",
239
+ ...
240
+ "Longitude"
241
+ ],
242
+ "model_out_list": [
243
+ "MedHouseVal"
244
+ ],
245
+ "loc_in": {
246
+ "MedInc": 0,
247
+ ...
248
+ "Longitude": 7
249
+ },
250
+ "loc_out": {
251
+ "MedHouseVal": 0
252
+ }
253
+ }
254
+ ```
255
+
256
+ The `output_format` specification indicates which form the outputs of the model's `evaluate()` function should take, which may vary depending on the application. PyTorchModels working with the [LUME-EPICS](https://github.com/slaclab/lume-epics) service will require an `OutputVariable` type, while [Xopt](https://github.com/ChristopherMayes/Xopt) requires either a dictionary of float values or tensors as output.
257
+
258
+ It is important to note that currently the **transformers are not loaded** into the model when using the `model_from_yaml` method. These need to be created separately and added either:
259
+
260
+ * to the model's `kwargs` before instantiating
261
+
262
+ ```python
263
+ import torch
264
+ import json
265
+ from lume_model.torch import PyTorchModel
266
+
267
+ # load the model class and kwargs
268
+ with open(f"california_variables.yml","r") as f:
269
+ yaml_model, yaml_kwargs = model_from_yaml(f, load_model=False)
270
+
271
+ # construct the transformers
272
+ with open("normalization.json", "r") as f:
273
+ normalizations = json.load(f)
274
+
275
+ input_transformer = AffineInputTransform(
276
+ len(normalizations["x_mean"]),
277
+ coefficient=torch.tensor(normalizations["x_scale"]),
278
+ offset=torch.tensor(normalizations["x_mean"]),
279
+ )
280
+ output_transformer = AffineInputTransform(
281
+ len(normalizations["y_mean"]),
282
+ coefficient=torch.tensor(normalizations["y_scale"]),
283
+ offset=torch.tensor(normalizations["y_mean"]),
284
+ )
285
+
286
+ model_kwargs["input_transformers"] = [input_transformer]
287
+ model_kwargs["output_transformers"] = [output_transformer]
288
+
289
+ model = PyTorchModel(**model_kwargs)
290
+ ```
291
+
292
+ * using the setters for the transformer attributes in the model.
293
+
294
+ ```python
295
+ # load the model
296
+ with open("california_variables.yml", "r") as f:
297
+ model = model_from_yaml(f, load_model=True)
298
+
299
+ # construct the transformers
300
+ with open("normalization.json", "r") as f:
301
+ normalizations = json.load(f)
302
+
303
+ input_transformer = AffineInputTransform(
304
+ len(normalizations["x_mean"]),
305
+ coefficient=torch.tensor(normalizations["x_scale"]),
306
+ offset=torch.tensor(normalizations["x_mean"]),
307
+ )
308
+ output_transformer = AffineInputTransform(
309
+ len(normalizations["y_mean"]),
310
+ coefficient=torch.tensor(normalizations["y_scale"]),
311
+ offset=torch.tensor(normalizations["y_mean"]),
312
+ )
313
+
314
+ # use the model's setter to add the transformers. Here we use a tuple
315
+ # to tell the setter where in the list the transformer should be inserted.
316
+ # In this case because we only have one, we add them at the beginning
317
+ # of the lists.
318
+ model.input_transformers = (input_transformer, 0)
319
+ model.output_transformers = (output_transformer, 0)
320
+ ```