Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix "ValueError: could not convert string to float: 'ATOMIC_POSITIONS'" #721

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions dpdata/qe/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,67 @@
kbar2evperang3 = 1e3 / 1.602176621e6


def get_block(lines, keyword, skip=0):
ret = []
for idx, ii in enumerate(lines):
if keyword in ii:
blk_idx = idx + 1 + skip
while len(lines[blk_idx]) == 0:
blk_idx += 1
while len(lines[blk_idx]) != 0 and blk_idx != len(lines):
ret.append(lines[blk_idx])
blk_idx += 1
def get_block(lines, start_marker):
start_idx = None
for idx, line in enumerate(lines):
if start_marker in line:
start_idx = idx + 1
break
return ret
if start_idx is None:
raise RuntimeError(f"{start_marker} not found in the input lines.")

block = []
for line in lines[start_idx:]:
if line.strip() == "" or line.strip().startswith("&"):
break
block.append(line.strip())
return block


def get_block(lines, start_marker, skip=0):
Copy link

Choose a reason for hiding this comment

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

Remove the unused get_block function.

There is a redefinition of the get_block function at line 32, which is unused in the code.

Remove the unused function definition:

-def get_block(lines, start_marker, skip=0):
-    start_idx = None
-    for idx, line in enumerate(lines):
-        if start_marker in line:
-            start_idx = idx + 1 + skip
-            break
-    if start_idx is None:
-        raise RuntimeError(f"{start_marker} not found in the input lines.")
-
-    block = []
-    for line in lines[start_idx:]:
-        if line.strip() == "" or line.strip().startswith("&"):
-            break
-        block.append(line.strip())
-    return block

Committable suggestion was skipped due to low confidence.

Tools
Ruff

32-32: Redefinition of unused get_block from line 15

(F811)

Copy link
Contributor

Choose a reason for hiding this comment

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

plz avoid redefining functions.

start_idx = None
for idx, line in enumerate(lines):
if start_marker in line:
start_idx = idx + 1 + skip
break
if start_idx is None:
raise RuntimeError(f"{start_marker} not found in the input lines.")

block = []
for line in lines[start_idx:]:
if line.strip() == "" or line.strip().startswith("&"):
break
block.append(line.strip())
return block


def get_cell(lines):
ret = []
for idx, ii in enumerate(lines):
if "ibrav" in ii:
for idx, line in enumerate(lines):
Copy link

Choose a reason for hiding this comment

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

Rename the unused loop control variable idx to _idx.

The loop control variable idx is not used within the loop body.

Rename idx to _idx to indicate that it is intentionally unused:

-    for idx, line in enumerate(lines):
+    for _idx, line in enumerate(lines):
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for idx, line in enumerate(lines):
for _idx, line in enumerate(lines):
Tools
Ruff

50-50: Loop control variable idx not used within loop body

Rename unused idx to _idx

(B007)

if "ibrav" in line:
ibrav = int(line.replace(",", "").split("=")[-1])
break
blk = lines[idx : idx + 2]
ibrav = int(blk[0].replace(",", "").split("=")[-1])
else:
raise RuntimeError("ibrav not found in the input lines.")

if ibrav == 0:
for iline in lines:
if "CELL_PARAMETERS" in iline and "angstrom" not in iline.lower():
for line in lines:
if "CELL_PARAMETERS" in line and "angstrom" not in line.lower():
raise RuntimeError(
"CELL_PARAMETERS must be written in Angstrom. Other units are not supported yet."
)
blk = get_block(lines, "CELL_PARAMETERS")
for ii in blk:
ret.append([float(jj) for jj in ii.split()[0:3]])
ret = []
for line in blk:
ret.append([float(value) for value in line.split()[0:3]])
ret = np.array(ret)
elif ibrav == 1:
a = None
for iline in lines:
line = iline.replace("=", " ").replace(",", "").split()
for line in lines:
line = line.replace("=", " ").replace(",", "").split()
Comment on lines +70 to +71
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for changing the variable names?

if len(line) >= 2 and "a" == line[0]:
# print("line = ", line)
a = float(line[1])
if len(line) >= 2 and "celldm(1)" == line[0]:
a = float(line[1]) * bohr2ang
# print("a = ", a)
if not a:
raise RuntimeError("parameter 'a' or 'celldm(1)' cannot be found.")
ret = np.array([[a, 0.0, 0.0], [0.0, a, 0.0], [0.0, 0.0, a]])
Expand Down
Loading