aiida-core icon indicating copy to clipboard operation
aiida-core copied to clipboard

ProjectionData bug

Open superstar54 opened this issue 1 year ago • 1 comments

Describe the bug

When I want to create a ProjectionData for testing, I find there are several bugs in the code.

from aiida import load_profile
import numpy as np
from aiida.orm import ProjectionData, BandsData
from aiida.plugins import OrbitalFactory

load_profile()
# create a BandsData
kpoints = np.array([[0.0, 0.0, 0.0]])
bands = np.array([[-5.64024889]])
bands_data = BandsData()
bands_data.set_kpoints(kpoints)
bands_data.set_bands(bands, units="eV")
bands_data.store()

# Create a ProjectionData

OrbitalCls = OrbitalFactory("core.realhydrogen")
state_dict = {
"kind_name": "C",
"angular_momentum": 0,
"magnetic_number": 0,
"radial_nodes": 1,
"position": [0.0, 0.0, 0.0],
}
orbitals = [OrbitalCls(**state_dict)]
# projections = np.array([[1]])
energy_arrays = np.array([1])
pdos_arrays = np.array([1])

projection_data = ProjectionData()
bands_data = generate_bands_data()
projection_data.set_reference_bandsdata(bands_data)
projection_data.set_projectiondata(
orbitals,
# list_of_projections=projections,
list_of_energy=energy_arrays,
list_of_pdos=pdos_arrays,
bands_check=True,
)

Expected behavior

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.9/site-packages/aiida/orm/nodes/data/array/projection.py", line 277, in set_projectiondata
    self._check_projections_bands(this_projection)
UnboundLocalError: local variable 'this_projection' referenced before assignment

Bugs

Looking at the source code

  • Bug 1. Indeed, the local variable 'this_projection' referenced before assignment
  • Bug 2, in line 47, it tries to get the shape of a BandsData, which is always (0, 0).
            shape_bands = np.shape(self.get_reference_bandsdata())

Looking at the phaser in aiida-quantumespresso, it set the bands_check = False to skip this code, while the default value is True.

superstar54 avatar Jun 28 '23 15:06 superstar54

This may also be related to #4529, because there is no doc or example for the ProjectionData yet.

superstar54 avatar Jun 28 '23 15:06 superstar54