Skip to content

Make autocomplete_metadata.py emit schema-compliant molecule metadata and improve CI tests - #506

Merged
comcon1 merged 16 commits into
mainfrom
copilot/fix-autocomplete-metadata-issue
Jun 14, 2026
Merged

Make autocomplete_metadata.py emit schema-compliant molecule metadata and improve CI tests#506
comcon1 merged 16 commits into
mainfrom
copilot/fix-autocomplete-metadata-issue

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

autocomplete_metadata.py could emit invalid metadata.yaml entries (notably malformed strings/identifiers and invalid NMRlipids field types), causing schema validation failures for newly created molecule metadata. This update hardens output normalization so autogenerated metadata conforms to metadata_schema.json under partial/dirty upstream API payloads.

  • Output sanitization for schema-facing fields

    • Added text normalization (clean_text) to strip HTML tags/entities from API-derived values before writing bioschema_properties and alternate names.
    • Added safe numeric parsing (safe_float) for molecularWeight to avoid invalid numeric output.
  • sameAs identifier hardening

    • Added sanitize_sameas to keep only schema-valid identifier values/patterns (e.g., ChEBI, ChEMBL, cas, slm, pubchem.compound as int).
    • Prevents invalid external IDs from being persisted into generated YAML.
  • NMRlipids schema compliance

    • Ensured NMRlipids.name is always populated via fallback chain (existing value → API names → molecule id).
    • Stopped emitting empty-string charge (schema expects integer when present).
  • Focused regression coverage

    • Added tests/test_autocomplete_metadata.py to execute the autocomplete flow with mocked API responses and assert generated YAML validates against metadata_schema.json, including cleaned smiles/alternateName.
def clean_text(value):
    if not isinstance(value, str):
        return value
    return re.sub(r"<[^>]+>", "", unescape(value)).strip()

sameas = sanitize_sameas(extract_sameas(sources))
alternate_names = [clean_text(name) for name in alternate_names if clean_text(name)]

-Clena up test environments and CI
During fixing, it was discovered that the tox test environments should be restructured.
See #506 (review) below

Copilot AI changed the title [WIP] Fix schema compliance issue in autocomplete_metadata.py Make autocomplete_metadata.py emit schema-compliant molecule metadata Jun 10, 2026
Copilot AI requested a review from mdondrup June 10, 2026 07:39
@mdondrup
mdondrup requested a review from Copilot June 10, 2026 07:43

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 hardens developer/autocomplete_metadata.py so it generates metadata.yaml that conforms to src/fairmd/lipids/schema_validation/schema/metadata_schema.json, even when upstream API payloads contain dirty/HTML-ish strings or non-numeric weights. It also adds a regression test to validate the generated YAML against the JSON schema.

Changes:

  • Added normalization helpers (clean_text, safe_float) and applied them to schema-facing fields (names, SMILES, InChI/InChIKey, molecular weight).
  • Added sanitize_sameas to keep sameAs identifiers schema-pattern compliant.
  • Added tests/test_autocomplete_metadata.py to mock API responses and assert schema validation passes and cleaned fields match expectations.

Reviewed changes

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

File Description
developer/autocomplete_metadata.py Adds sanitization/parsing helpers and applies them to emitted metadata fields to improve schema compliance.
tests/test_autocomplete_metadata.py Adds a mocked end-to-end autocomplete test that validates generated YAML against metadata_schema.json.

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

Comment thread developer/autocomplete_metadata.py
Comment thread developer/autocomplete_metadata.py
Comment thread developer/autocomplete_metadata.py
Comment thread tests/develop/test_autocomplete_metadata.py
mdondrup added 3 commits June 10, 2026 10:45
Added an option for cas retrieval from CAS commonchemistry.cas.org
Needs an API key in env CAS_API_KEY
@mdondrup

Copy link
Copy Markdown
Collaborator

Have tested and debugged this. Now the output is formatted correctly. Also added a test.

@mdondrup

mdondrup commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

Currently test test_calc_ff_quality_sin_exp_curve is failling (on MacOS 15). Nothing to do with my changes.
Edit: this must have been a temp glitch, seems to work now.

@mdondrup
mdondrup marked this pull request as ready for review June 10, 2026 09:07
@mdondrup
mdondrup requested review from comcon1 and korbinib June 10, 2026 09:07

@korbinib korbinib left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks good to me. Do we want to have the additional cas API call?

Comment thread developer/autocomplete_metadata.py
@mdondrup
mdondrup requested a review from korbinib June 12, 2026 17:25

@comcon1 comcon1 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.

It's good that you decided to add out-of-package tests, but then they must be clearly isolated from the package tests.

  1. We need to make sure they are not running on release or other stories when we distribute the package. Because develop folder is not distributed with the package. It's conditioning stuff in the repo.
  2. We need to name this kind of tests develop, and not network. There are other tests that use direct internet access and that are run currently inside tests-all tox label.
  3. Tests develop tests should be accessible via tox. Currently, there is no tox label for them. And we should probably separate tests-all.

What is my suggestion.

  • 1. Let's remove tests-all environment and replace it with tests-package environment.
  • 2. Let's add tests-develop environment.
  • 3. Let's call proper tox labels from proper events.

@MagnusSletten was working a lot with this part. I would like to also ask his opinion.

- Added develop pytest marker; mark test_autocomplete_metadata.py module-level
- Replace tests-all tox env with tests-package (to be run: pytest -m "not develop")
- Addd tests-develop tox env (run: pytest -m develop --no-cov) to avoid
  coverage warnings
- Updated All Tests wf to run tests-package and add tests-develop
@mdondrup

Copy link
Copy Markdown
Collaborator

I added tests-package and replaced tests-all by it. Also added tests-develop. I am not sure if all events:call assignments are proper yet. Possibly need to discuss that.

@comcon1

comcon1 commented Jun 13, 2026

Copy link
Copy Markdown
Member

@mdondrup thanks. Now I cleaned CI a bit. I also would like to separate package and development tests and hang trigger to changes of different folders in PR. This is important I think..

@comcon1

comcon1 commented Jun 13, 2026

Copy link
Copy Markdown
Member

// I don't like that we had different coverage reporting from integration and unit-tests. I would like to have only coverage from unit tests because integration tests doesn't help do develop, so I'm not very interested in having this value despite it's large.

@MagnusSletten MagnusSletten left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice pr!

Separating developer tests also means we can run them specifically when the developer/ folder is changed in CI, while keeping them isolated from the normal package tests.

Small nitpick: I slightly prefer keeping Codecov/coverage reporting in CI rather than baking it into tests-min, but I do not think this is a blocker. The generated files are gitignored, and this will not affect normal development at all.

Comment thread tox.ini Outdated
Comment thread .github/workflows/tests-develop.yml Outdated
Comment thread .github/workflows/tests-min.yml Outdated
comcon1 and others added 2 commits June 14, 2026 00:01
Co-authored-by: MagnusSletten <magnus.elias.sletten@gmail.com>
Co-authored-by: MagnusSletten <magnus.elias.sletten@gmail.com>
@comcon1
comcon1 requested a review from MagnusSletten June 14, 2026 09:08

@comcon1 comcon1 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.

Now, we are ready! Thanks, all!

@mdondrup

Copy link
Copy Markdown
Collaborator

This PR has diverged from its original title and task. We should change the name and description.

@mdondrup mdondrup changed the title Make autocomplete_metadata.py emit schema-compliant molecule metadata Make autocomplete_metadata.py emit schema-compliant molecule metadata and improve CI tests Jun 14, 2026
@comcon1
comcon1 merged commit 249d62f into main Jun 14, 2026
8 of 9 checks passed
@mdondrup

Copy link
Copy Markdown
Collaborator

I think this was merged prematurely. Now the tests are not working

@comcon1
comcon1 deleted the copilot/fix-autocomplete-metadata-issue branch June 20, 2026 17:13
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.

autocomplete_metadata.py generated metadata not schema compliant

6 participants