import numpy as np def read_swc(file_path): raw = np.loadtxt(file_path, comments='#') return { "id": raw[:, 0].astype(np.int32), "type": raw[:, 1].astype(np.int32), "coords": raw[:, 2:5], "radius": raw[:, 5], "parent": raw[:, 6].astype(np.int32), } def write_swc(file_path, data): with open(file_path, 'w') as f: # Combine data into columns n_nodes = len(data['id']) swc_data = np.column_stack([ data['id'], data['type'], data['coords'], data['radius'], data['parent'] ]) # Write data rows for i in range(n_nodes): f.write(f"{int(swc_data[i, 0])} {int(swc_data[i, 1])} " f"{swc_data[i, 2]:.4f} {swc_data[i, 3]:.4f} {swc_data[i, 4]:.4f} " f"{swc_data[i, 5]:.4f} {int(swc_data[i, 6])}\n")