Skip to content

Comparison of analytical spectra - #270

Open
Nicolass67 wants to merge 6 commits into
masterfrom
comparison-of-analytical-spectra
Open

Comparison of analytical spectra#270
Nicolass67 wants to merge 6 commits into
masterfrom
comparison-of-analytical-spectra

Conversation

@Nicolass67

Copy link
Copy Markdown
Contributor

Summary

Combined spectrum views (multi-file transform and BagIt) now show annotations for the active spectrum, not just raw curves.

  • Extract overlay plotting from NIComposer.tf_img() into reusable plot_overlays() (peaks, integrations, multiplicity, info box)
  • Load integration/multiplicity data from JCAMP tables when needed (_ensure_itg_mpy_from_core_tables())
  • Apply overlays only to the active spectrum (jcamp_idx) in TransformerModel.tf_combine() and BagItBaseConverter.__combine_images()

- Set figure DPI to 200 for improved image quality.
- Introduced global x-axis limits based on data range.
- Added handling for peak plotting with error management.
- Improved marker handling for different plot types.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the “combined spectrum” rendering paths so that annotations/overlays (peaks, integrations, multiplicity, info box) are drawn only for the active spectrum (jcamp_idx) rather than for all stacked curves, and factors that overlay logic into a reusable NIComposer.plot_overlays() helper.

Changes:

  • Extract overlay plotting from NIComposer.tf_img() into NIComposer.plot_overlays() and add _ensure_itg_mpy_from_core_tables() to hydrate integration/multiplicity from JCAMP tables as needed.
  • Update TransformerModel.tf_combine() and BagItBaseConverter.__combine_images() to apply overlays only to the active spectrum and to use higher DPI output.
  • Improve combined plot axis handling (global x-limits/orientation) and remove ad-hoc cyclic voltammetry ref-peak plotting from the combined transformer path.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
chem_spectra/model/transformer.py Combined-image generation now selects an active spectrum and applies overlays only to that curve; adds global x-limit/orientation logic.
chem_spectra/lib/converter/bagit/base.py BagIt combined-image generation now selects an active composer and applies overlays only to that spectrum.
chem_spectra/lib/composer/ni.py Refactors overlay rendering into plot_overlays() and adds lazy loading of integration/multiplicity data from core tables.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 278 to 284
plt.rcParams['figure.figsize'] = [16, 9]
plt.rcParams['figure.dpi'] = 200
plt.rcParams['font.size'] = 14
plt.rcParams['legend.loc'] = 'upper left'
curve_idx = self.params.get('jcamp_idx', 0)

xlabel, ylabel = '', ''
Comment thread chem_spectra/lib/converter/bagit/base.py
Comment thread chem_spectra/lib/composer/ni.py Outdated
Comment on lines 393 to 408
if (len(self.mpys) == 0 and len(self.core.mpy_itg_table) > 0
and not self.core.params['integration'].get('edited')
and ('originStack' not in self.core.params['integration'])):
tmp_dic_mpy_peaks = {}
core_mpy_pks_table = self.core.mpy_pks_table[0]
for peak in core_mpy_pks_table.split('\n'):
clear_peak = peak.replace('(', '').replace(')', '')
split_peak = clear_peak.split(',')
idx_peakStr = split_peak[0].strip()
if idx_peakStr not in tmp_dic_mpy_peaks:
tmp_dic_mpy_peaks[idx_peakStr] = []
tmp_dic_mpy_peaks[idx_peakStr].append({
'x': float(split_peak[1].strip()),
'y': float(split_peak[2].strip()),
})

Resolve conflict in NIComposer: keep the plot_overlays() extraction and
fold in master's changes — __draw_integrals no longer takes itg_h, and
multiplicity hydration/plotting is NMR-only.
Address review findings on the overlay extraction:

- Skip multiplets without matching peaks when hydrating from JCAMP
  tables: calc_mpy_center divides by the peak count, so they would
  crash overlay drawing with ZeroDivisionError.
- Guard against a missing/empty $OBSERVEDMULTIPLETSPEAKS table and
  skip malformed/blank table rows instead of raising IndexError.
- Restore cyclic voltammetry ref-peak markers in combined images when
  the CV state arrives only via the extras form field: backfill
  list_max_min_peaks on the active composer from the parsed cv_state.
- Log exceptions from overlay drawing and x-limit handling instead of
  swallowing them silently.
- Coerce jcamp_idx to int in parse_params and at the combined-image
  read sites so string form params cannot break active-spectrum
  selection.
- Add tests for table hydration edge cases, plot_overlays boundaries,
  and combined images with an active spectrum index.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment thread tests/model/test_transformer.py Outdated
Comment on lines +71 to +77
molfile = open(source_dir_molfile, "r")
# jcamp_idx picks the active spectrum whose overlays are drawn; a string
# value must be tolerated (form params arrive untyped)
params = {**params_1h_jcamp, 'jcamp_idx': '1'}
tranform_model = TransformerModel(None, molfile=molfile, params=params, multiple_files=[file_1, file_2])

tf = tranform_model.tf_combine(list_file_names=['a.dx', 'b.dx'])
Comment thread tests/lib/composer/test_ni_composer.py Outdated
Comment on lines +1 to +6
import matplotlib.pyplot as plt
import pytest

from chem_spectra.lib.converter.jcamp.base import JcampBaseConverter
from chem_spectra.lib.converter.jcamp.ni import JcampNIConverter
from chem_spectra.lib.composer.ni import NIComposer
Comment thread tests/lib/composer/test_ni_composer.py Outdated
Comment on lines +85 to +93
plt.figure()
try:
y_boundary_min, y_boundary_max = ni_composer.plot_overlays(
plt, adjust_xlim=False,
)
finally:
plt.clf()
plt.cla()

- Open the molfile with a context manager in the combined-overlay test
  so the descriptor is closed deterministically.
- Import pyplot after NIComposer (which selects the Agg backend on
  import) to keep the test module order-independent.
- Close the figure created by the plot_overlays test instead of only
  clearing it, so figures don't accumulate across the suite.

@harivyasi harivyasi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. Merged master into the branch and resolved the conflicts, so the PR is mergeable again.
  2. Fixed the issues from review: two crashes when spectra files contain incomplete multiplet data, missing peak markers for cyclic voltammetry in combined images, errors that were silently ignored are now logged, and safer handling of the active-spectrum index (covers the Copilot comments). Also added tests for all of this.
  3. Small test cleanups from Copilot's second review.

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