IDAES / examples-pse

(ARCHIVED - use IDAES/examples) Example Python code, Jupyter Notebooks, and other files for the IDAES PSE
https://idaes.github.io/examples-pse/
Other
20 stars 36 forks source link

Examples/SurrMod/FlowsheetOptimization/ALAMO_flowsheet_optimization.ipynb fails with FileNotFoundError #169

Closed lbianchi-lbl closed 1 year ago

lbianchi-lbl commented 1 year ago
examples-pse-2.0.0b2 | 2022-12-07 19:11:59,108 INFO - [Worker 0] Convert notebook name=/home/runner/examples-pse/src/Examples/SurrMod/FlowsheetOptimization/ALAMO_flowsheet_optimization.ipynb: begin
examples-pse-2.0.0b2 | 2022-12-07 19:11:59,108 INFO - [Worker 0] Converting: ALAMO_flowsheet_optimization.ipynb
examples-pse-2.0.0b2 | 2022-12-07 19:11:59,118 INFO - [Worker 0] Running notebook: ALAMO_flowsheet_optimization.ipynb
examples-pse-2.0.0b2 | 2022-12-07 19:12:02,647 ERROR - [Worker 0] Notebook execution failed: execution error for '/tmp/tmp918j3uts/ALAMO_flowsheet_optimization.ipynb': An error occurred while executing the following cell:
examples-pse-2.0.0b2 | ------------------
examples-pse-2.0.0b2 | # capture long output (not required to use surrogate API)
examples-pse-2.0.0b2 | from io import StringIO
examples-pse-2.0.0b2 | import sys
examples-pse-2.0.0b2 | stream = StringIO()
examples-pse-2.0.0b2 | oldstdout = sys.stdout
examples-pse-2.0.0b2 | sys.stdout = stream
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # Create ALAMO trainer object
examples-pse-2.0.0b2 | trainer = AlamoTrainer(input_labels=input_labels,
examples-pse-2.0.0b2 |                        output_labels=output_labels,
examples-pse-2.0.0b2 |                        training_dataframe=data_training)
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # Set ALAMO options
examples-pse-2.0.0b2 | trainer.config.constant = True
examples-pse-2.0.0b2 | trainer.config.linfcns = True
examples-pse-2.0.0b2 | trainer.config.multi2power = [1, 2]
examples-pse-2.0.0b2 | trainer.config.monomialpower = [2, 3]
examples-pse-2.0.0b2 | trainer.config.ratiopower = [1, 2]
examples-pse-2.0.0b2 | trainer.config.maxterms = [10] * len(output_labels)  # max for each surrogate
examples-pse-2.0.0b2 | trainer.config.filename = os.path.join(os.getcwd(), 'alamo_run.alm')
examples-pse-2.0.0b2 | trainer.config.overwrite_files = True
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # Train surrogate (calls ALAMO through IDAES ALAMOPy wrapper)
examples-pse-2.0.0b2 | success, alm_surr, msg = trainer.train_surrogate()
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # save model to JSON
examples-pse-2.0.0b2 | model = alm_surr.save_to_file('alamo_surrogate.json', overwrite=True)
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # create callable surrogate object
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | surrogate_expressions = trainer._results['Model']
examples-pse-2.0.0b2 | input_labels = trainer._input_labels
examples-pse-2.0.0b2 | output_labels = trainer._output_labels
examples-pse-2.0.0b2 | xmin, xmax = [0.1, 0.8], [0.8, 1.2]
examples-pse-2.0.0b2 | input_bounds = {input_labels[i]: (xmin[i], xmax[i])
examples-pse-2.0.0b2 |                 for i in range(len(input_labels))}
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | alm_surr = AlamoSurrogate(surrogate_expressions,
examples-pse-2.0.0b2 |                           input_labels,
examples-pse-2.0.0b2 |                           output_labels,
examples-pse-2.0.0b2 |                           input_bounds)
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # revert back to normal output capture
examples-pse-2.0.0b2 | sys.stdout = oldstdout
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | # display first 50 lines and last 50 lines of output
examples-pse-2.0.0b2 | celloutput = stream.getvalue().split('\n')
examples-pse-2.0.0b2 | for line in celloutput[:50]:
examples-pse-2.0.0b2 |     print(line)
examples-pse-2.0.0b2 | print('.')
examples-pse-2.0.0b2 | print('.')
examples-pse-2.0.0b2 | print('.')
examples-pse-2.0.0b2 | for line in celloutput[-50:]:
examples-pse-2.0.0b2 |     print(line)
examples-pse-2.0.0b2 | ------------------
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | ---------------------------------------------------------------------------
examples-pse-2.0.0b2 | FileNotFoundError                         Traceback (most recent call last)
examples-pse-2.0.0b2 | ~/.conda/envs/idaes-env/lib/python3.8/site-packages/idaes/core/surrogate/alamopy.py in _read_trace_file(self, trcfile, has_validation_data)
examples-pse-2.0.0b2 |    1027         try:
examples-pse-2.0.0b2 | -> 1028             with open(trcfile, "r") as f:
examples-pse-2.0.0b2 |    1029                 lines = f.readlines()
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmp918j3uts/alamo_run.trc'
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | During handling of the above exception, another exception occurred:
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | FileNotFoundError                         Traceback (most recent call last)
examples-pse-2.0.0b2 | /tmp/ipykernel_11348/2642844276.py in <cell line: 24>()
examples-pse-2.0.0b2 |      22 
examples-pse-2.0.0b2 |      23 # Train surrogate (calls ALAMO through IDAES ALAMOPy wrapper)
examples-pse-2.0.0b2 | ---> 24 success, alm_surr, msg = trainer.train_surrogate()
examples-pse-2.0.0b2 |      25 
examples-pse-2.0.0b2 |      26 # save model to JSON
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | ~/.conda/envs/idaes-env/lib/python3.8/site-packages/idaes/core/surrogate/alamopy.py in train_surrogate(self)
examples-pse-2.0.0b2 |     700 
examples-pse-2.0.0b2 |     701             # Read back results
examples-pse-2.0.0b2 | --> 702             trace_dict = self._read_trace_file(self._trcfile)
examples-pse-2.0.0b2 |     703 
examples-pse-2.0.0b2 |     704             # Populate results and SurrogateModel object
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | ~/.conda/envs/idaes-env/lib/python3.8/site-packages/idaes/core/surrogate/alamopy.py in _read_trace_file(self, trcfile, has_validation_data)
examples-pse-2.0.0b2 |    1031         except FileNotFoundError:
examples-pse-2.0.0b2 |    1032             # Trace file does not exist
examples-pse-2.0.0b2 | -> 1033             raise FileNotFoundError(
examples-pse-2.0.0b2 |    1034                 "Error occured when trying to read the ALAMO trace file - this probably "
examples-pse-2.0.0b2 |    1035                 "indicates that a trace file was not created by the ALAMO executable. "
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | FileNotFoundError: Error occured when trying to read the ALAMO trace file - this probably indicates that a trace file was not created by the ALAMO executable. Please check the ALAMO output logs.
examples-pse-2.0.0b2 | FileNotFoundError: Error occured when trying to read the ALAMO trace file - this probably indicates that a trace file was not created by the ALAMO executable. Please check the ALAMO output logs.
examples-pse-2.0.0b2 | 
examples-pse-2.0.0b2 | 2022-12-07 19:12:02,647 ERROR - [Worker 0] Execution failed: generating partial output for '/home/runner/examples-pse/src/Examples/SurrMod/FlowsheetOptimization/ALAMO_flowsheet_optimization.ipynb'
examples-pse-2.0.0b2 | 2022-12-07 19:12:02,647 INFO - [Worker 0] Convert notebook name=/home/runner/examples-pse/src/Examples/SurrMod/FlowsheetOptimization/ALAMO_flowsheet_optimization.ipynb: end, ok=False duration=3.5s
bpaul4 commented 1 year ago

@lbianchi-lbl what environment is this occurring in? I suspect either the file was altered/removed (appears to be fine in this repository), or the CI routine is not carrying along the correct file path to the temporary directory. This sometimes happens when it ignores certain extensions (.trc), have there been changes to the build or workflow scripts recently?

lbianchi-lbl commented 1 year ago

Yeah, I imagine it would be something related to that, since I don't see .trc listed here: https://github.com/IDAES/examples-pse/blob/c1625b4869ab80894814c5500569b5d30fe87763/build.py#L173

I assume that this notebook is not being run in CI because it requires ALAMO to be installed. The error happened during the release process for this repository, where notebooks are run in an on-premise containerized environment where a wider selection of third-party tools is available.

lbianchi-lbl commented 1 year ago

It seems that my initial guess above might be incorrect, as the trace file should be generated during the notebook run. The current best guess is that a) either some error in the ALAMO run prevents the creation of the file, or b) the file is created but somehow ends up in a different directory.

eslickj commented 1 year ago

I ran this example, but it worked for me.

andrewlee94 commented 1 year ago

@eslickj My best guess is that this has something to do with the environment the CI tests are run it - I suspect the files are not ending up in the expected locations for some reason when run through Github Actions resulting in the test failure.