trident-project / trident

A Synthetic Spectral Generation Suite
Other
21 stars 25 forks source link

Max num comps #195

Closed claytonstrawn closed 1 year ago

claytonstrawn commented 1 year ago

When fitting very complex spectra, such as with impact parameters < 10 kpc, some students of mine would occasionally have significant components that would not be detected. We eventually decided it was because we had "run out of components" and after some digging we found that this was because there was a maximum of 8 components per complex.

While normally this has no serious impact, and generally using more than 8 components is running against the error limit of the fitter, it was frustrating to not be able to explicitly see and mess with this parameter. I added an optional argument maxNumComps to generate_total_fit to allow this to be changed by the user.

Here's some examples below, showing this does come into play with different numbers. 8 is a perfectly good default, but I would prefer if it wasn't hard-coded.

mnc5 mnc10 mnc20

In looking into the source code and comparing with the documentation, I found what might be a more important problem also. The docs say https://trident.readthedocs.io/en/latest/absorption_spectrum_fit.html

There are several other conditions under which the cycle of adding and optimizing lines will halt. If the error of the optimized fit from adding a line is an order of magnitude worse than the error of the fit without that line, then it is assumed that the fitting has become unstable and the latest line is removed. Lines are also prevented from being added if the total number of lines is greater than the number of elements in the flux array being fit divided by 3. This is because there must not be more free parameters in a fit than the number of points to constrain them. (emphasis mine)

However, in line 355, instead of breaking if np.size(linesP)*3>=len(x): it said to break if np.size(linesP)+3>=len(x):

Which I think was just a typo.

PS: I would use the normal style guide and name this new argument max_num_comps, but the other arguments in this file are in camelcase so I followed that. Maybe the whole file needs to be restyled?

coveralls commented 1 year ago

Pull Request Test Coverage Report for Build 120a1677-fb8a-4b7d-89b0-72a2c64bd6e9


Files with Coverage Reduction New Missed Lines %
/home/circleci/trident/trident/absorption_spectrum/absorption_spectrum_fit.py 166 16.43%
<!-- Total: 166 -->
Totals Coverage Status
Change from base Build 6dcb6d69-c336-4434-aa65-ec6cd87c1122: 0.0%
Covered Lines: 1745
Relevant Lines: 2306

💛 - Coveralls