• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tonegas / nnodely / 14359872492

09 Apr 2025 02:33PM UTC coverage: 97.602% (+0.6%) from 97.035%
14359872492

Pull #86

github

web-flow
Merge ec719935a into e9c323c4f
Pull Request #86: Smallclasses

2291 of 2418 new or added lines in 54 files covered. (94.75%)

3 existing lines in 1 file now uncovered.

11683 of 11970 relevant lines covered (97.6%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.67
/nnodely/operators/exporter.py
1
from nnodely.exporter.standardexporter import StandardExporter
1✔
2
from nnodely.exporter.emptyexporter import EmptyExporter
1✔
3
from nnodely.basic.model import Model
1✔
4
from nnodely.basic.modeldef import ModelDef
1✔
5
from nnodely.support.utils import check, enforce_types
1✔
6

7

8
class Exporter:
1✔
9
    @enforce_types
1✔
10
    def __init__(self, exporter:EmptyExporter|str|None=None, workspace:str|None=None, *, save_history:bool=False):
1✔
11
        check(type(self) is not Exporter, TypeError, "Exporter class cannot be instantiated directly")
1✔
12

13
        # Exporter
14
        if exporter == 'Standard':
1✔
15
            self.exporter = StandardExporter(workspace, self.visualizer, save_history=save_history)
1✔
NEW
16
        elif exporter != None:
×
NEW
17
            self.exporter = exporter
×
18
        else:
NEW
19
            self.exporter = EmptyExporter()
×
20

21
    @enforce_types
1✔
22
    def getWorkspace(self):
1✔
23
        return self.exporter.getWorkspace()
1✔
24

25
    @enforce_types
1✔
26
    def saveTorchModel(self, name:str='net', model_folder:str|None=None, *, models:str|None=None) -> None:
1✔
27
        """
28
        Saves the neural network model in PyTorch format.
29

30
        Parameters
31
        ----------
32
        name : str, optional
33
            The name of the saved model file. Default is 'net'.
34
        model_folder : str or None, optional
35
            The folder to save the model file in. Default is None.
36
        models : list or None, optional
37
            A list of model names to save. If None, the entire model is saved. Default is None.
38

39
        Raises
40
        ------
41
        RuntimeError
42
            If the model is not neuralized.
43

44
        Examples
45
        --------
46
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
47
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
48
            :alt: Open in Colab
49

50
        Example usage:
51
            >>> model = Modely()
52
            >>> model.neuralizeModel()
53
            >>> model.saveTorchModel(name='example_model', model_folder='path/to/save')
54
        """
55
        check(self.neuralized == True, RuntimeError, 'The model is not neuralized yet!')
1✔
56
        if models is not None:
1✔
NEW
57
            if name == 'net':
×
NEW
58
                name += '_' + '_'.join(models)
×
NEW
59
            model_def = ModelDef()
×
NEW
60
            model_def.update(model_dict={key: self._model_dict[key] for key in models if key in self._model_dict})
×
NEW
61
            model_def.setBuildWindow(self._model_def['Info']['SampleTime'])
×
NEW
62
            model_def.updateParameters(self._model)
×
NEW
63
            model = Model(model_def.__json)
×
64
        else:
65
            model = self._model
1✔
66
        self.exporter.saveTorchModel(model, name, model_folder)
1✔
67

68
    @enforce_types
1✔
69
    def loadTorchModel(self, name:str='net', model_folder:str|None=None) -> None:
1✔
70
        """
71
        Loads a neural network model from a PyTorch format file.
72

73
        Parameters
74
        ----------
75
        name : str, optional
76
            The name of the model file to load. Default is 'net'.
77
        model_folder : str or None, optional
78
            The folder to load the model file from. Default is None.
79

80
        Raises
81
        ------
82
        RuntimeError
83
            If the model is not neuralized.
84

85
        Examples
86
        --------
87
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
88
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
89
            :alt: Open in Colab
90

91
        Example usage:
92
            >>> model = Modely()
93
            >>> model.neuralizeModel()
94
            >>> model.loadTorchModel(name='example_model', model_folder='path/to/load')
95
        """
96
        check(self.neuralized == True, RuntimeError, 'The model is not neuralized yet.')
1✔
97
        self.exporter.loadTorchModel(self._model, name, model_folder)
1✔
98

99
    @enforce_types
1✔
100
    def saveModel(self, name:str='net', model_path:str|None=None, *, models:str|None=None) -> None:
1✔
101
        ## TODO: Add tests passing the attribute 'models'
102
        """
103
        Saves the neural network model definition in a json file.
104

105
        Parameters
106
        ----------
107
        name : str, optional
108
            The name of the saved model file. Default is 'net'.
109
        model_path : str or None, optional
110
            The path to save the model file. Default is None.
111
        models : list or None, optional
112
            A list of model names to save. If None, the entire model is saved. Default is None.
113

114
        Raises
115
        ------
116
        RuntimeError
117
            If the network has not been defined.
118

119
        Examples
120
        --------
121
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
122
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
123
            :alt: Open in Colab
124

125
        Example usage:
126
            >>> model = Modely()
127
            >>> model.neuralizeModel()
128
            >>> model.saveModel(name='example_model', model_path='path/to/save')
129
        """
130
        if models is not None:
1✔
NEW
131
            if name == 'net':
×
NEW
132
                name += '_' + '_'.join(models)
×
NEW
133
            model_def = ModelDef()
×
NEW
134
            model_def.update(
×
135
                model_dict={key: self._model_def.__model_dict[key] for key in models if key in self._model_def.__model_dict})
NEW
136
            model_def.setBuildWindow(self._model_def['Info']['SampleTime'])
×
NEW
137
            model_def.updateParameters(self._model)
×
138
        else:
139
            model_def = self._model_def
1✔
140
        check(model_def.isDefined(), RuntimeError, "The network has not been defined.")
1✔
141
        self.exporter.saveModel(model_def.getJson(), name, model_path)
1✔
142

143
    @enforce_types
1✔
144
    def loadModel(self, name:str='net', model_folder:str|None=None) -> None:
1✔
145
        """
146
        Loads a neural network model from a json file containing the model definition.
147

148
        Parameters
149
        ----------
150
        name : str or None, optional
151
            The name of the model file to load. Default is 'net'.
152
        model_folder : str or None, optional
153
            The folder to load the model file from. Default is None.
154

155
        Raises
156
        ------
157
        RuntimeError
158
            If there is an error loading the network.
159

160
        Examples
161
        --------
162
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
163
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
164
            :alt: Open in Colab
165

166
        Example usage:
167
            >>> model = Modely()
168
            >>> model.loadModel(name='example_model', model_folder='path/to/load')
169
        """
170
        model_def = self.exporter.loadModel(name, model_folder)
1✔
171
        check(model_def, RuntimeError, "Error to load the network.")
1✔
172
        self._model_def = ModelDef(model_def)
1✔
173
        self._model = None
1✔
174
        self._neuralized = False
1✔
175
        self._traced = False
1✔
176

177
    @enforce_types
1✔
178
    def exportPythonModel(self, name:str='net', model_path:str|None=None, *, models:str|None=None) -> None:
1✔
179
        """
180
        Exports the neural network model as a standalone PyTorch Module class.
181

182
        Parameters
183
        ----------
184
        name : str, optional
185
            The name of the exported model file. Default is 'net'.
186
        model_path : str or None, optional
187
            The path to save the exported model file. Default is None.
188
        models : list or None, optional
189
            A list of model names to export. If None, the entire model is exported. Default is None.
190

191
        Raises
192
        ------
193
        RuntimeError
194
            If the network has not been defined.
195
            If the model is traced and cannot be exported to Python.
196
            If the model is not neuralized.
197

198
        Examples
199
        --------
200
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
201
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
202
            :alt: Open in Colab
203

204
        Example usage:
205
            >>> model = Modely(name='example_model')
206
            >>> model.neuralizeModel()
207
            >>> model.exportPythonModel(name='example_model', model_path='path/to/export')
208
        """
209
        if models is not None:
1✔
NEW
210
            if name == 'net':
×
NEW
211
                name += '_' + '_'.join(models)
×
NEW
212
            model_def = ModelDef()
×
NEW
213
            model_def.update(
×
214
                model_dict={key: self._model_def.__model_dict[key] for key in models if key in self._model_def.__model_dict})
NEW
215
            model_def.setBuildWindow(self._model_def['Info']['SampleTime'])
×
NEW
216
            model_def.updateParameters(self._model)
×
NEW
217
            model = Model(model_def.__json)
×
218
        else:
219
            model_def = self._model_def
1✔
220
            model = self._model
1✔
221
        # check(model_def['States'] == {}, TypeError, "The network has state variables. The export to python is not possible.")
222
        check(model_def.isDefined(), RuntimeError, "The network has not been defined.")
1✔
223
        check(self._traced == False, RuntimeError,
1✔
224
              'The model is traced and cannot be exported to Python.\n Run neuralizeModel() to recreate a standard model.')
225
        check(self.neuralized == True, RuntimeError, 'The model is not neuralized yet.')
1✔
226
        self.exporter.saveModel(model_def.getJson(), name, model_path)
1✔
227
        self.exporter.exportPythonModel(model_def, model, name, model_path)
1✔
228

229
    @enforce_types
1✔
230
    def importPythonModel(self, name:str='net', model_folder:str|None=None) -> None:
1✔
231
        """
232
        Imports a neural network model from a standalone PyTorch Module class.
233

234
        Parameters
235
        ----------
236
        name : str or None, optional
237
            The name of the model file to import. Default is 'net'.
238
        model_folder : str or None, optional
239
            The folder to import the model file from. Default is None.
240

241
        Raises
242
        ------
243
        RuntimeError
244
            If there is an error loading the network.
245

246
        Examples
247
        --------
248
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
249
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
250
            :alt: Open in Colab
251

252
        Example usage:
253
            >>> model = Modely()
254
            >>> model.importPythonModel(name='example_model', model_folder='path/to/import')
255
        """
256
        model_def = self.exporter.loadModel(name, model_folder)
1✔
257
        check(model_def is not None, RuntimeError, "Error to load the network.")
1✔
258
        self.neuralizeModel(model_def=model_def)
1✔
259
        self._model = self.exporter.importPythonModel(name, model_folder)
1✔
260
        self._traced = True
1✔
261
        self._model_def.updateParameters(self._model)
1✔
262

263
    @enforce_types
1✔
264
    def exportONNX(self, inputs_order:list|None=None, outputs_order:list|None=None, name:str='net', model_folder:str|None=None, *, models:str|list|None=None) -> None:
1✔
265
        """
266
        Exports the neural network model to an ONNX file.
267

268
        .. note::
269
            The input_order may contain all the inputs and states of the model in the order that you want to export them.
270

271
        Parameters
272
        ----------
273
        inputs_order : list
274
            The order of the input and state variables.
275
        outputs_order : list
276
            The order of the output variables.
277
        models : list or None, optional
278
            A list of model names to export. If None, the entire model is exported. Default is None.
279
        name : str, optional
280
            The name of the exported ONNX file. Default is 'net'.
281
        model_folder : str or None, optional
282
            The folder to save the exported ONNX file. Default is None.
283

284
        Raises
285
        ------
286
        RuntimeError
287
            If the network has not been defined.
288
            If the model is traced and cannot be exported to ONNX.
289
            If the model is not neuralized.
290
            If the model is loaded and not created.
291

292
        Examples
293
        --------
294
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
295
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
296
            :alt: Open in Colab
297

298
        Example usage:
299
            >>> input1 = Input('input1').last()
300
            >>> input2 = Input('input2').last()
301
            >>> out = Output('output1', input1+input2)
302

303
            >>> model = Modely()
304
            >>> model.neuralizeModel()
305
            >>> model.exportONNX(inputs_order=['input1', 'input2'], outputs_order=['output1'], name='example_model', model_folder='path/to/export')
306
        """
307
        check(self._model_def.isDefined(), RuntimeError, "The network has not been defined.")
1✔
308
        check(self.traced == False, RuntimeError,
1✔
309
              'The model is traced and cannot be exported to ONNX.\n Run neuralizeModel() to recreate a standard model.')
310
        check(self.neuralized == True, RuntimeError, 'The model is not neuralized yet.')
1✔
311
        # TODO replace with getJson(model = models) # generate the subtree json of a model
312
        # From here --------------
313
        model_dict = self._model_def.getModelDict()
1✔
314
        check(model_dict != {}, RuntimeError, 'The model is loaded but is not .')
1✔
315
        model_def = ModelDef()
1✔
316
        if models is not None:
1✔
317
            if name == 'net':
1✔
318
                name += '_' + '_'.join(models)
1✔
319
            model_def.update(
1✔
320
                model_dict={key: model_dict[key] for key in models if key in model_dict})
321
        else:
322
            model_def.update(model_dict=model_dict)
1✔
323
        model_def.setBuildWindow(self._model_def['Info']['SampleTime'])
1✔
324
        model_def.updateParameters(self._model)
1✔
325
        # To here -------------- Are removed
326
        model = Model(model_def.getJson())
1✔
327
        model.update()
1✔
328
        self.exporter.exportONNX(model_def, model, inputs_order, outputs_order, name, model_folder)
1✔
329

330
    @enforce_types
1✔
331
    def onnxInference(self, inputs:dict, path:str) -> dict:
1✔
332
        """
333
        Run an inference session using an onnx model previously exported using the nnodely framework.
334

335
        .. note:: Feed-Forward ONNX model
336
            For feed-forward models, the onnx model expect all the inputs and states to have 3 dimensions. The first dimension is the batch size, the second is the time window and the third is the feature dimension.
337
        .. note:: Recurrent ONNX model
338
            For recurrent models, the onnx model expect all the inputs to have 4 dimensions. The first dimension is the prediction horizon, the second is the batch size, the third is the time window and the fourth is the feature dimension.
339
            For recurrent models, the onnx model expect all the States to have 3 dimensions. The first dimension is the batch size, the second is the time window, the third is the feature dimension
340

341
        Parameters
342
        ----------
343
        inputs : dict
344
            A dictionary containing the input and state variables to be used to make the inference.
345
            State variables are mandatory and are used to initialize the states of the model.
346
        path : str
347
            The path to the ONNX file to use.
348

349
        Raises
350
        ------
351
        RuntimeError
352
            If the shape of the inputs are not equals to the ones defined in the onnx model.
353
            If the batch size is not equal for all the inputs and states.
354

355
        Examples
356
        --------
357
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
358
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
359
            :alt: Open in Colab
360

361
        Example - Feed-Forward:
362
            >>> x = Input('x')
363

364
            >>> onnx_model_path = path/to/net.onnx
365
            >>> dummy_input = {'x':np.ones(shape=(3, 1, 1)).astype(np.float32)}
366
            >>> predictions = Modely().onnxInference(dummy_input, onnx_model_path)
367
        Example - Recurrent:
368
            >>> x = Input('x')
369
            >>> y = State('y')
370

371
            >>> onnx_model_path = path/to/net.onnx
372
            >>> dummy_input = {'x':np.ones(shape=(3, 1, 1, 1)).astype(np.float32)
373
                                'y':np.ones(shape=(1, 1, 1)).astype(np.float32)}
374
            >>> predictions = Modely().onnxInference(dummy_input, onnx_model_path)
375
        """
376
        return self.exporter.onnxInference(inputs, path)
1✔
377

378
    @enforce_types
1✔
379
    def exportReport(self, name:str='net', model_folder:None=None) -> None:
1✔
380
        """
381
        Generates a PDF report with plots containing the results of the training and validation of the neural network.
382

383
        Parameters
384
        ----------
385
        name : str, optional
386
            The name of the exported report file. Default is 'net'.
387
        model_folder : str or None, optional
388
            The folder to save the exported report file. Default is None.
389

390
        Examples
391
        --------
392
        .. image:: https://colab.research.google.com/assets/colab-badge.svg
393
            :target: https://colab.research.google.com/github/tonegas/nnodely/blob/main/examples/export.ipynb
394
            :alt: Open in Colab
395

396
        Example usage:
397
            >>> model = Modely()
398
            >>> model.neuralizeModel()
399
            >>> model.trainModel(train_dataset='train_dataset', validation_dataset='val_dataset', num_of_epochs=10)
400
            >>> model.exportReport(name='example_model', model_folder='path/to/export')
401
        """
402
        self.exporter.exportReport(self, name, model_folder)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc