-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoxel2density.py
More file actions
75 lines (61 loc) · 2.17 KB
/
voxel2density.py
File metadata and controls
75 lines (61 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np
import csv
import argparse
import os
def parse_dx(filename):
with open(filename, 'r') as f:
lines = f.readlines()
origin = None
deltas = []
grid_size = []
data = []
reading_data = False
for line in lines:
if line.startswith("origin"):
origin = np.array([float(x) for x in line.split()[1:]])
elif line.startswith("delta"):
deltas.append(float(line.split()[1]))
elif line.startswith("object 1 class gridpositions counts"):
grid_size = [int(x) for x in line.split()[5:]]
elif line.startswith("object 3 class array type double"):
reading_data = True
elif reading_data:
tokens = line.split()
try:
floats = [float(x) for x in tokens]
data.extend(floats)
except ValueError:
break
data = np.array(data).reshape(grid_size[::-1]) # z, y, x
return data, origin, deltas, grid_size
def main():
parser = argparse.ArgumentParser(description="Parse a .dx voxel file and write fractional coordinates + densities to CSV.")
parser.add_argument("input_dx", help="Input .dx file path")
args = parser.parse_args()
input_path = os.path.abspath(args.input_dx)
input_dir = os.path.dirname(input_path)
output_csv = os.path.join(input_dir, "density_fractional.csv")
# Parse DX file
data, origin, deltas, grid_size = parse_dx(input_path)
# Generate fractional coordinates
nz, ny, nx = data.shape
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
z = np.linspace(0, 1, nz)
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
X = X.flatten()
Y = Y.flatten()
Z = Z.flatten()
D = data.flatten()
# Mask near-zero densities
mask = D > 1e-6
X, Y, Z, D = X[mask], Y[mask], Z[mask], D[mask]
# Write CSV
with open(output_csv, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["x_frac", "y_frac", "z_frac", "density"])
for x, y, z, d in zip(X, Y, Z, D):
writer.writerow([x, y, z, d])
print(f"✅ Done. Data written to '{output_csv}'")
if __name__ == "__main__":
main()