Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 13, 2026

math.isclose() requires scalar arguments but was receiving 1D numpy arrays, causing TypeError: only 0-dimensional arrays can be converted to Python scalars in CP2K AIMD output parsing.

Changes

  • Extract scalar values from energies arrays using [0] indexing before comparison in Cp2kSystems.__next__()
# Before
assert math.isclose(
    log_info_dict["energies"], xyz_info_dict["energies"], abs_tol=1.0e-6
)

# After  
assert math.isclose(
    log_info_dict["energies"][0], xyz_info_dict["energies"][0], abs_tol=1.0e-6
)

Both log_info_dict["energies"] and xyz_info_dict["energies"] are shape (1,) arrays created by np.asarray([energy]), so [0] extracts the scalar energy value for comparison while preserving the original arrays in the assertion message for debugging.

Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] cp2k: TypeError: only 0-dimensional arrays can be converted to Python scalars</issue_title>
<issue_description>### Bug summary

The recent CI runs reported an error on CP2K tests: TypeError: only 0-dimensional arrays can be converted to Python scalars. See the log below.

dpdata Version

devel

Input Files, Running Commands, Error Log, etc.

======================================================================
ERROR: test_nframs (test_cp2k_aimd_output.TestCp2kAimdStressOutput.test_nframs)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/dpdata/dpdata/tests/test_cp2k_aimd_output.py", line 23, in setUp
    self.system_1 = dpdata.LabeledSystem("cp2k/aimd_stress", fmt="cp2k/aimd_output")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/dpdata/dpdata/dpdata/system.py", line 197, in __init__
    self.from_fmt(
  File "/home/runner/work/dpdata/dpdata/dpdata/system.py", line 234, in from_fmt
    return self.from_fmt_obj(load_format(fmt), file_name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/dpdata/dpdata/dpdata/system.py", line 1224, in from_fmt_obj
    data = fmtobj.from_labeled_system(file_name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/dpdata/dpdata/dpdata/plugins/cp2k.py", line 25, in from_labeled_system
    return tuple(Cp2kSystems(log_file, xyz_file, restart))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/dpdata/dpdata/dpdata/cp2k/output.py", line 67, in __next__
    assert math.isclose(
           ^^^^^^^^^^^^^
TypeError: only 0-dimensional arrays can be converted to Python scalars

Steps to Reproduce

Run the UT.

Further Information, Files, and Links

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: njzjz <9496702+njzjz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError in CP2K tests related to array conversion fix(cp2k): extract scalar from numpy array for math.isclose() comparison Jan 13, 2026
Copilot AI requested a review from njzjz January 13, 2026 05:31
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 13, 2026

CodSpeed Performance Report

Merging this PR will improve performance by 17.32%

Comparing copilot/fix-cp2k-typeerror-bug (83b82bd) with devel (6edbd09)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

⚡ 2 improved benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_import 11.2 ms 9.6 ms +17.32%
WallTime test_cli 280.3 ms 253 ms +10.78%

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link

codecov bot commented Jan 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.06%. Comparing base (6edbd09) to head (83b82bd).
⚠️ Report is 1 commits behind head on devel.

Additional details and impacted files
@@           Coverage Diff           @@
##            devel     #924   +/-   ##
=======================================
  Coverage   86.06%   86.06%           
=======================================
  Files          83       83           
  Lines        7886     7886           
=======================================
  Hits         6787     6787           
  Misses       1099     1099           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@njzjz njzjz marked this pull request as ready for review January 13, 2026 05:53
Copilot AI review requested due to automatic review settings January 13, 2026 05:53
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jan 13, 2026
@njzjz njzjz requested a review from robinzyb January 13, 2026 05:54
@dosubot dosubot bot added bug Something isn't working cp2k labels Jan 13, 2026
Copy link

Copilot AI left a comment

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 fixes a TypeError that occurred when math.isclose() received 1D numpy arrays instead of scalar values during CP2K AIMD output parsing.

Changes:

  • Extract scalar values from energies arrays using [0] indexing before passing to math.isclose() in the Cp2kSystems.__next__() method

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

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 14, 2026
@njzjz njzjz merged commit c396ce7 into devel Jan 14, 2026
29 checks passed
@njzjz njzjz deleted the copilot/fix-cp2k-typeerror-bug branch January 14, 2026 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cp2k lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] cp2k: TypeError: only 0-dimensional arrays can be converted to Python scalars

3 participants