Skip to content

feat: add script to generate python scripts from xml#466

Merged
sanjayankur31 merged 27 commits into
developmentfrom
feat-xml2py
May 21, 2026
Merged

feat: add script to generate python scripts from xml#466
sanjayankur31 merged 27 commits into
developmentfrom
feat-xml2py

Conversation

@sanjayankur31
Copy link
Copy Markdown
Member

@sanjayankur31 sanjayankur31 commented Apr 22, 2026

This will be useful in multiple cases:

a) creating python scripts from xml models as starting templates for users
b) for llms/agents---where we want them to write python, not xml

This will be useful in multiple cases:

a) creating python scripts from xml models as starting templates for
users
b) for llms/agents---where we want them to write python, not xml
@sanjayankur31 sanjayankur31 marked this pull request as ready for review May 13, 2026 14:37
@sanjayankur31 sanjayankur31 moved this from 🆕 New to 🔖 Ready in NeuroML backlog May 13, 2026
@sanjayankur31
Copy link
Copy Markdown
Member Author

@HengyeZhu: would you have time to review this please?

@HengyeZhu
Copy link
Copy Markdown

No problem! I’ll start working on it soon.

@sanjayankur31 sanjayankur31 changed the title WIP: feat: add script to generate python scripts from xml feat: add script to generate python scripts from xml May 14, 2026
@HengyeZhu
Copy link
Copy Markdown

Hi @sanjayankur31,

Sorry for the delayed reply. This PR may be fine, except for inline-cell cases such as https://github.com/OpenSourceBrain/262670/blob/master/NeuroML2/KC.net.nml.

from pyneuroml import pynml

def test_inline_cell_validate():
    model = read_neuroml2_file("HH_example_net.nml", include_includes=True)

    with tempfile.NamedTemporaryFile("w", dir=".") as f:
        write_neuroml2_file(model, f.name)

        script = NmlPythonizer(f.name, "temp_nml2py_1").write()[-1]
        exec(Path(script).read_text(), {})

        assert pynml.validate_neuroml2(Path(f.name).name)

With the current code, this fails because the generated Python includes the extracted inline cell as:

nml_doc.add("IncludeType", href='hh_cell.cell.nml')

while the extracted inline cell file is written to:

temp_nml2py_1/hh_cell.cell.nml

After generating the extracted-component href relative to the output directory, the test passes.

Comment thread pyneuroml/utils/nml2py.py Outdated
Comment thread pyneuroml/utils/nml2py.py
@sanjayankur31 sanjayankur31 moved this from 🔖 Ready to 👀 In review in NeuroML backlog May 20, 2026
@sanjayankur31
Copy link
Copy Markdown
Member Author

Hi @sanjayankur31,

Sorry for the delayed reply. This PR may be fine, except for inline-cell cases such as https://github.com/OpenSourceBrain/262670/blob/master/NeuroML2/KC.net.nml.

from pyneuroml import pynml

def test_inline_cell_validate():
    model = read_neuroml2_file("HH_example_net.nml", include_includes=True)

    with tempfile.NamedTemporaryFile("w", dir=".") as f:
        write_neuroml2_file(model, f.name)

        script = NmlPythonizer(f.name, "temp_nml2py_1").write()[-1]
        exec(Path(script).read_text(), {})

Shouldn't the script be run after changing into the temp_nml2py_1 directory?

The idea is that all the new files are generated in a new folder and must be run there. A few reasons for this:

  • keep the original model files completely separate from the new generated files
  • do not risk overwriting original files (if the id etc. matches in your test, for example, the HH_example_net.nml file could be overwritten)
  • simplifying paths: we could technically add folders/relative paths to the include hrefs, but that then means that whenever these extracted files are to be used, the same folder structure will have to be followed. That's not a bad thing, necessarily, but i thought it's better to use a flat directory structure, and then users can move files around and update paths as they wish.

What do you think?

@sanjayankur31 sanjayankur31 requested a review from HengyeZhu May 21, 2026 12:39
@HengyeZhu
Copy link
Copy Markdown

The idea is that all the new files are generated in a new folder and must be run there.

I think this is completely fine! I realize why I could not run https://github.com/OpenSourceBrain/262670/blob/master/NeuroML2/KC.net.nml: I was launching it directly from the CLI without setting include_includes=True, so the include paths were wrong in that case.

@sanjayankur31
Copy link
Copy Markdown
Member Author

Cool, do you think it's OK to merge the PR?

Copy link
Copy Markdown

@HengyeZhu HengyeZhu left a comment

Choose a reason for hiding this comment

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

No problem!

@sanjayankur31 sanjayankur31 merged commit 16ff1c5 into development May 21, 2026
7 checks passed
@sanjayankur31 sanjayankur31 deleted the feat-xml2py branch May 21, 2026 14:45
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in NeuroML backlog May 21, 2026
@sanjayankur31
Copy link
Copy Markdown
Member Author

@all-contributors please add @HengyeZhu for code, design, review

@allcontributors
Copy link
Copy Markdown
Contributor

@sanjayankur31

I've put up a pull request to add @HengyeZhu! 🎉

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

Labels

T: enhancement Type: enhancement

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

2 participants