Spaces:
Runtime error
Runtime error
Fix cube file parsing to follow standard format specification
Browse files- app.py +26 -30
- force_rebuild.txt +1 -1
app.py
CHANGED
|
@@ -11,41 +11,37 @@ def parse_cube_file(cube_file):
|
|
| 11 |
if len(lines) < 6:
|
| 12 |
raise ValueError("Cube file too short")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
except ValueError:
|
| 23 |
-
continue
|
| 24 |
-
else:
|
| 25 |
-
raise ValueError("Could not find natoms in cube file")
|
| 26 |
-
|
| 27 |
-
# Now i is the line with natoms
|
| 28 |
-
origin = np.array([float(x) for x in lines[i].split()[1:4]])
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
data_start =
|
| 49 |
data = []
|
| 50 |
for line in lines[data_start:]:
|
| 51 |
data.extend([float(x) for x in line.split()])
|
|
|
|
| 11 |
if len(lines) < 6:
|
| 12 |
raise ValueError("Cube file too short")
|
| 13 |
|
| 14 |
+
# Standard cube format:
|
| 15 |
+
# Line 0-1: comments
|
| 16 |
+
# Line 2: natoms, origin_x, origin_y, origin_z
|
| 17 |
+
# Line 3: nx, voxel_x, 0, 0
|
| 18 |
+
# Line 4: ny, 0, voxel_y, 0
|
| 19 |
+
# Line 5: nz, 0, 0, voxel_z
|
| 20 |
+
# Lines 6 to 6+natoms-1: atom data
|
| 21 |
+
# Remaining lines: volumetric data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Parse line 2 (natoms and origin)
|
| 24 |
+
parts = lines[2].split()
|
| 25 |
+
natoms = abs(int(float(parts[0]))) # abs() handles negative natoms (sometimes used)
|
| 26 |
+
origin = np.array([float(parts[1]), float(parts[2]), float(parts[3])])
|
| 27 |
|
| 28 |
+
# Parse line 3 (nx and voxel spacing)
|
| 29 |
+
parts = lines[3].split()
|
| 30 |
+
nx = abs(int(float(parts[0])))
|
| 31 |
+
dx = float(parts[1])
|
| 32 |
|
| 33 |
+
# Parse line 4 (ny and voxel spacing)
|
| 34 |
+
parts = lines[4].split()
|
| 35 |
+
ny = abs(int(float(parts[0])))
|
| 36 |
+
dy = float(parts[2])
|
| 37 |
|
| 38 |
+
# Parse line 5 (nz and voxel spacing)
|
| 39 |
+
parts = lines[5].split()
|
| 40 |
+
nz = abs(int(float(parts[0])))
|
| 41 |
+
dz = float(parts[3])
|
| 42 |
|
| 43 |
+
# Data starts after atom lines
|
| 44 |
+
data_start = 6 + natoms
|
| 45 |
data = []
|
| 46 |
for line in lines[data_start:]:
|
| 47 |
data.extend([float(x) for x in line.split()])
|
force_rebuild.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
# Force rebuild
|
| 2 |
-
2025-11-08
|
|
|
|
| 1 |
# Force rebuild
|
| 2 |
+
2025-11-08 v7
|