Skip to content

Conversation

@rwest
Copy link
Member

@rwest rwest commented May 15, 2024

Motivation or Problem

When there are issues in a database file, such as nan values like uncertainty=RateUncertainty(mu=nan, var=nan, in a rules.py file (which may be due to a bug in the tree fitting), it was very hard to find the cause of the error because the stack trace wouldn't tell you where the actual error was in the file, and the file handle had been read by the time you caught the exception, making it misleading in a debugger.

Description of Changes

With this change, we first read the file to a variable, then try executing it, and catch and report any exceptions properly, finding the line within the database file that caused the problem.

Testing

I got this working and it helped debug something, providing this error message:

ERROR:root:Error while reading database /Users/rwest/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic/rules.py.
Traceback (most recent call last):
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/base.py", line 242, in load
    exec(content, global_context, local_context)
  File "<string>", line 3732, in <module>
TypeError: 'NoneType' object is not subscriptable
ERROR:root:Error occurred at line 3732 of /Users/rwest/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic/rules.py.
ERROR:root:Line:     kinetics = ArrheniusBM(A=(1.17041e-45,'s^-1'), n=16.4585, w0=(289.5,'kJ/mol'), E0=(7.90103,'kJ/mol'), Tmin=(300,'K'), Tmax=(2000,'K'), uncertainty=RateUncertainty(mu=nan, var=nan, Tref=1000.0, N=2, data_mean=0.0, correlation='Root_N-1R-inRing_N-3R->O_Ext-3CS-R_N-Sp-4R!H=3CCSS_Ext-3CS-R_Ext-5R!H-R_N-Sp-6R!H=5R!H_3CS-inRing_N-Sp-2R=1R',), comment="""BM rule fitted to 2 training reactions at node Root_N-1R-inRing_N-3R->O_Ext-3CS-R_N-Sp-4R!H=3CCSS_Ext-3CS-R_Ext-5R!H-R_N-Sp-6R!H=5R!H_3CS-inRing_N-Sp-2R=1R

before continuing with the rest of the stack trace

ERROR:root:Error when loading reaction family '/Users/rwest/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic'
Traceback (most recent call last):
  File "/Users/rwest/miniconda3/envs/rmg_env7/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/rwest/miniconda3/envs/rmg_env7/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 322, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 136, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/Users/rwest/.vscode/extensions/ms-python.debugpy-2024.6.0-darwin-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/Users/rwest/Code/blahblahblah.py", line 54, in <module>
    depository = False,
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/rmg.py", line 103, in load
    kinetics_depositories
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/rmg.py", line 172, in load_kinetics
    depositories=kinetics_depositories
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/kinetics/database.py", line 111, in load
    self.load_recommended_families(os.path.join(path, 'families', 'recommended.py')),
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/kinetics/database.py", line 215, in load_families
    try:
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/kinetics/family.py", line 761, in load
    index += 1
  File "/Users/rwest/Code/RMG-Py/rmgpy/data/base.py", line 242, in load
    exec(content, global_context, local_context)
  File "<string>", line 3732, in <module>
TypeError: 'NoneType' object is not subscriptable

Before, it would just point you to File "/Users/rwest/Code/RMG-Py/rmgpy/data/base.py", line 242, in load which is the exec(content, global_context, local_context) line, and not very informative.

Reviewer Tips

Not much to check here.

When there are issues in a database file, such as nan values here:
  uncertainty=RateUncertainty(mu=nan, var=nan,
which may be due to a bug in the tree fitting, it was very hard to 
find the cause of the error because the stack trace wouldn't tell you
where the error was, and the file handle had been read by the time you
caught the exception.
With this change, we read the file to a variable, then try executing it,
and catch and report any exceptions properly.
@rwest rwest requested a review from Nora-Khalil May 15, 2024 18:49
@github-actions
Copy link

Regression Testing Results

WARNING:root:Initial mole fractions do not sum to one; normalizing.
WARNING:root:Initial mole fractions do not sum to one; normalizing.
WARNING:root:Initial mole fractions do not sum to one; normalizing.
⚠️ One or more regression tests failed.
Please download the failed results and run the tests locally or check the log to see why.

Detailed regression test results.

Regression test aromatics:

Reference: Execution time (DD:HH:MM:SS): 00:00:01:06
Current: Execution time (DD:HH:MM:SS): 00:00:01:05
Reference: Memory used: 2768.76 MB
Current: Memory used: 2807.53 MB

aromatics Passed Core Comparison ✅

Original model has 15 species.
Test model has 15 species. ✅
Original model has 11 reactions.
Test model has 11 reactions. ✅

aromatics Failed Edge Comparison ❌

Original model has 106 species.
Test model has 106 species. ✅
Original model has 358 reactions.
Test model has 358 reactions. ✅

Non-identical thermo! ❌
original: C=CC1C=CC2=CC1C=C2
tested: C=CC1C=CC2=CC1C=C2

Hf(300K) S(300K) Cp(300K) Cp(400K) Cp(500K) Cp(600K) Cp(800K) Cp(1000K) Cp(1500K)
83.22 84.16 35.48 45.14 53.78 61.40 73.58 82.20 95.08
83.22 82.78 35.48 45.14 53.78 61.40 73.58 82.20 95.08

Identical thermo comments:
thermo: Thermo group additivity estimation: group(Cs-(Cds-Cds)(Cds-Cds)CsH) + group(Cs-(Cds-Cds)(Cds-Cds)CsH) + group(Cds-Cds(Cds-Cds)(Cds-Cds)) + group(Cds- CdsCsH) + group(Cds-CdsCsH) + group(Cds-CdsCsH) + group(Cds-CdsCsH) + group(Cds-Cds(Cds-Cds)H) + group(Cds-Cds(Cds-Cds)H) + group(Cds-CdsHH) + Estimated bicyclic component: polycyclic(s3_5_6_ane) - ring(Cyclohexane) - ring(Cyclopentane) + ring(1,3-Cyclohexadiene) + ring(Cyclopentadiene)

Details Observables Test Case: Aromatics Comparison

✅ All Observables varied by less than 0.500 on average between old model and new model in all conditions!

aromatics Passed Observable Testing ✅

Regression test liquid_oxidation:

Reference: Execution time (DD:HH:MM:SS): 00:00:02:11
Current: Execution time (DD:HH:MM:SS): 00:00:02:08
Reference: Memory used: 2912.56 MB
Current: Memory used: 2907.09 MB

liquid_oxidation Passed Core Comparison ✅

Original model has 37 species.
Test model has 37 species. ✅
Original model has 215 reactions.
Test model has 215 reactions. ✅

liquid_oxidation Failed Edge Comparison ❌

Original model has 202 species.
Test model has 202 species. ✅
Original model has 1613 reactions.
Test model has 1613 reactions. ✅

Non-identical kinetics! ❌
original:
rxn: CCCCCO[O](104) + CC(CC(C)OO)O[O](103) <=> oxygen(1) + CCCCC[O](127) + CC([O])CC(C)OO(129) origin: Peroxyl_Disproportionation
tested:
rxn: CCCCCO[O](103) + CC(CC(C)OO)O[O](104) <=> oxygen(1) + CCCCC[O](128) + CC([O])CC(C)OO(127) origin: Peroxyl_Disproportionation

k(1bar) 300K 400K 500K 600K 800K 1000K 1500K 2000K
k(T): 7.79 7.46 7.21 7.00 6.67 6.41 5.94 5.60
k(T): 3.52 4.27 4.71 5.01 5.39 5.61 5.91 6.06

kinetics: Arrhenius(A=(3.18266e+20,'cm^3/(mol*s)'), n=-2.694, Ea=(0.053,'kcal/mol'), T0=(1,'K'), comment="""Estimated from node Root_Ext-5R-R_7R!H->C_N-7C-inRing Ea raised from 0.0 to 0.2 kJ/mol to match endothermicity of reaction.""")
kinetics: Arrhenius(A=(3.2e+12,'cm^3/(mol*s)'), n=0, Ea=(4.096,'kcal/mol'), T0=(1,'K'), comment="""Estimated from node Root_Ext-5R-R_7R!H->C_N-7C-inRing_Ext-5R-R""")
kinetics: Estimated from node Root_Ext-5R-R_7R!H->C_N-7C-inRing
Ea raised from 0.0 to 0.2 kJ/mol to match endothermicity of reaction.
kinetics: Estimated from node Root_Ext-5R-R_7R!H->C_N-7C-inRing_Ext-5R-R

Details Observables Test Case: liquid_oxidation Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

liquid_oxidation Passed Observable Testing ✅

Regression test nitrogen:

Reference: Execution time (DD:HH:MM:SS): 00:00:01:24
Current: Execution time (DD:HH:MM:SS): 00:00:01:22
Reference: Memory used: 2902.70 MB
Current: Memory used: 2907.64 MB

nitrogen Failed Core Comparison ❌

Original model has 41 species.
Test model has 41 species. ✅
Original model has 359 reactions.
Test model has 360 reactions. ❌
The tested model has 1 reactions that the original model does not have. ❌
rxn: HNO(48) + HCO(13) <=> NO(38) + CH2O(18) origin: H_Abstraction

nitrogen Failed Edge Comparison ❌

Original model has 132 species.
Test model has 132 species. ✅
Original model has 995 reactions.
Test model has 997 reactions. ❌
The tested model has 2 reactions that the original model does not have. ❌
rxn: HNO(48) + HCO(13) <=> NO(38) + CH2O(18) origin: H_Abstraction
rxn: HON(T)(83) + HCO(13) <=> NO(38) + CH2O(18) origin: Disproportionation

Details Observables Test Case: NC Comparison

✅ All Observables varied by less than 0.200 on average between old model and new model in all conditions!

nitrogen Passed Observable Testing ✅

Regression test oxidation:

Reference: Execution time (DD:HH:MM:SS): 00:00:02:23
Current: Execution time (DD:HH:MM:SS): 00:00:02:21
Reference: Memory used: 2765.50 MB
Current: Memory used: 2763.83 MB

oxidation Passed Core Comparison ✅

Original model has 59 species.
Test model has 59 species. ✅
Original model has 694 reactions.
Test model has 694 reactions. ✅

oxidation Passed Edge Comparison ✅

Original model has 230 species.
Test model has 230 species. ✅
Original model has 1526 reactions.
Test model has 1526 reactions. ✅

Details Observables Test Case: Oxidation Comparison

✅ All Observables varied by less than 0.500 on average between old model and new model in all conditions!

oxidation Passed Observable Testing ✅

Regression test sulfur:

Reference: Execution time (DD:HH:MM:SS): 00:00:00:54
Current: Execution time (DD:HH:MM:SS): 00:00:00:53
Reference: Memory used: 2870.89 MB
Current: Memory used: 2872.82 MB

sulfur Passed Core Comparison ✅

Original model has 27 species.
Test model has 27 species. ✅
Original model has 74 reactions.
Test model has 74 reactions. ✅

sulfur Failed Edge Comparison ❌

Original model has 89 species.
Test model has 89 species. ✅
Original model has 227 reactions.
Test model has 227 reactions. ✅
The original model has 1 reactions that the tested model does not have. ❌
rxn: O(4) + SO2(15) (+N2) <=> SO3(16) (+N2) origin: primarySulfurLibrary
The tested model has 1 reactions that the original model does not have. ❌
rxn: O(4) + SO2(15) (+N2) <=> SO3(16) (+N2) origin: primarySulfurLibrary

Details Observables Test Case: SO2 Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

sulfur Passed Observable Testing ✅

Regression test superminimal:

Reference: Execution time (DD:HH:MM:SS): 00:00:00:35
Current: Execution time (DD:HH:MM:SS): 00:00:00:35
Reference: Memory used: 2956.42 MB
Current: Memory used: 3009.05 MB

superminimal Passed Core Comparison ✅

Original model has 13 species.
Test model has 13 species. ✅
Original model has 21 reactions.
Test model has 21 reactions. ✅

superminimal Passed Edge Comparison ✅

Original model has 18 species.
Test model has 18 species. ✅
Original model has 28 reactions.
Test model has 28 reactions. ✅

Regression test RMS_constantVIdealGasReactor_superminimal:

Reference: Execution time (DD:HH:MM:SS): 00:00:02:21
Current: Execution time (DD:HH:MM:SS): 00:00:02:22
Reference: Memory used: 3435.75 MB
Current: Memory used: 3447.23 MB

RMS_constantVIdealGasReactor_superminimal Passed Core Comparison ✅

Original model has 13 species.
Test model has 13 species. ✅
Original model has 19 reactions.
Test model has 19 reactions. ✅

RMS_constantVIdealGasReactor_superminimal Passed Edge Comparison ✅

Original model has 13 species.
Test model has 13 species. ✅
Original model has 19 reactions.
Test model has 19 reactions. ✅

Details Observables Test Case: RMS_constantVIdealGasReactor_superminimal Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

RMS_constantVIdealGasReactor_superminimal Passed Observable Testing ✅

Regression test RMS_CSTR_liquid_oxidation:

Reference: Execution time (DD:HH:MM:SS): 00:00:05:56
Current: Execution time (DD:HH:MM:SS): 00:00:05:53
Reference: Memory used: 3375.71 MB
Current: Memory used: 3366.54 MB

RMS_CSTR_liquid_oxidation Failed Core Comparison ❌

Original model has 37 species.
Test model has 37 species. ✅
Original model has 232 reactions.
Test model has 233 reactions. ❌
The tested model has 1 reactions that the original model does not have. ❌
rxn: CCO[O](35) <=> [OH](22) + CC=O(62) origin: intra_H_migration

RMS_CSTR_liquid_oxidation Failed Edge Comparison ❌

Original model has 206 species.
Test model has 206 species. ✅
Original model has 1508 reactions.
Test model has 1508 reactions. ✅
The original model has 1 reactions that the tested model does not have. ❌
rxn: CCO[O](34) <=> C[CH]OO(63) origin: intra_H_migration
The tested model has 1 reactions that the original model does not have. ❌
rxn: CCO[O](35) <=> [OH](22) + CC=O(62) origin: intra_H_migration

Details Observables Test Case: RMS_CSTR_liquid_oxidation Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

RMS_CSTR_liquid_oxidation Passed Observable Testing ✅

Regression test fragment:

Reference: Execution time (DD:HH:MM:SS): 00:00:00:42
Current: Execution time (DD:HH:MM:SS): 00:00:00:40
Reference: Memory used: 2697.61 MB
Current: Memory used: 2703.04 MB

fragment Passed Core Comparison ✅

Original model has 10 species.
Test model has 10 species. ✅
Original model has 2 reactions.
Test model has 2 reactions. ✅

fragment Passed Edge Comparison ✅

Original model has 33 species.
Test model has 33 species. ✅
Original model has 47 reactions.
Test model has 47 reactions. ✅

Details Observables Test Case: fragment Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

fragment Passed Observable Testing ✅

Regression test RMS_constantVIdealGasReactor_fragment:

Reference: Execution time (DD:HH:MM:SS): 00:00:03:05
Current: Execution time (DD:HH:MM:SS): 00:00:03:00
Reference: Memory used: 3578.21 MB
Current: Memory used: 3624.13 MB

RMS_constantVIdealGasReactor_fragment Passed Core Comparison ✅

Original model has 10 species.
Test model has 10 species. ✅
Original model has 2 reactions.
Test model has 2 reactions. ✅

RMS_constantVIdealGasReactor_fragment Passed Edge Comparison ✅

Original model has 27 species.
Test model has 27 species. ✅
Original model has 24 reactions.
Test model has 24 reactions. ✅

Details Observables Test Case: RMS_constantVIdealGasReactor_fragment Comparison

✅ All Observables varied by less than 0.100 on average between old model and new model in all conditions!

RMS_constantVIdealGasReactor_fragment Passed Observable Testing ✅

Regression test minimal_surface:

Reference: Execution time (DD:HH:MM:SS): 00:00:01:06
Current: Execution time (DD:HH:MM:SS): 00:00:01:06
Reference: Memory used: 2901.55 MB
Current: Memory used: 2884.67 MB

minimal_surface Passed Core Comparison ✅

Original model has 30 species.
Test model has 30 species. ✅
Original model has 136 reactions.
Test model has 136 reactions. ✅

minimal_surface Passed Edge Comparison ✅

Original model has 131 species.
Test model has 131 species. ✅
Original model has 576 reactions.
Test model has 576 reactions. ✅

Details Observables Test Case: minimal_surface Comparison

✅ All Observables varied by less than 0.500 on average between old model and new model in all conditions!

minimal_surface Passed Observable Testing ✅

beep boop this comment was written by a bot 🤖

@Nora-Khalil
Copy link
Contributor

Just used this branch to see how my previous unhelpful error message changed. Now I get a helpful error of:

ERROR:root:Error while reading database file /home/khalil.nor/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic/rules.py.
Traceback (most recent call last):
  File "/home/khalil.nor/Code/RMG-Py/rmgpy/data/base.py", line 242, in load
    exec(content, global_context, local_context)
  File "<string>", line 3732, in <module>
TypeError: 'NoneType' object is not subscriptable
ERROR:root:Error occurred at or near line 3732 of /home/khalil.nor/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic/rules.py.
ERROR:root:Line:     kinetics = ArrheniusBM(A=(1.17041e-45,'s^-1'), n=16.4585, w0=(289.5,'kJ/mol'), E0=(7.90103,'kJ/mol'), Tmin=(300,'K'), Tmax=(2000,'K'), uncertainty=RateUncertainty(mu=nan, var=nan, Tref=1000.0, N=2, data_mean=0.0, correlation='Root_N-1R-inRing_N-3R->O_Ext-3CS-R_N-Sp-4R!H=3CCSS_Ext-3CS-R_Ext-5R!H-R_N-Sp-6R!H=5R!H_3CS-inRing_N-Sp-2R=1R',), comment="""BM rule fitted to 2 training reactions at node Root_N-1R-inRing_N-3R->O_Ext-3CS-R_N-Sp-4R!H=3CCSS_Ext-3CS-R_Ext-5R!H-R_N-Sp-6R!H=5R!H_3CS-inRing_N-Sp-2R=1R
ERROR:root:Error when loading reaction family '/home/khalil.nor/Code/RMG-database/input/kinetics/families/Intra_R_Add_Endocyclic'

as opposed to a non-helpful TypeError with no information. Thanks for the fix!

Copy link
Contributor

@Nora-Khalil Nora-Khalil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay looks great

@rwest
Copy link
Member Author

rwest commented May 17, 2024

@JacksonBurns I'm hesitant to set the precedent of merging my own code. Would you like to check this and do the honors?

@JacksonBurns JacksonBurns self-requested a review May 17, 2024 21:00
Copy link
Contributor

@JacksonBurns JacksonBurns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very handy, thanks!

@JacksonBurns JacksonBurns merged commit d426379 into ReactionMechanismGenerator:main May 17, 2024
@rwest rwest deleted the debugdatabase branch June 10, 2024 16:56
@Nora-Khalil
Copy link
Contributor

If there's a bug in a network.py file, a similar unhelpful error is raised when you use load_input_file() in arkane/input.py. I used this fix on that portion of the code too, and it worked great.

arkane/input.py in load_input_file()

with open(path, 'r') as f:
        content = f.read()
        try:
            exec(content, global_context, local_context)
        except (NameError, TypeError, SyntaxError) as e:
            logging.error('The input file {0!r} was invalid:'.format(path))
            line_number = e.__traceback__.tb_next.tb_lineno
            logging.error(f'Error occurred at or near line {line_number} of {path}.')
            lines = content.splitlines()
            logging.error(f'Line: {lines[line_number - 1]}')
            raise

@rwest
Copy link
Member Author

rwest commented Apr 3, 2025

Open a PR?

@Nora-Khalil
Copy link
Contributor

in the process of doing that right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants