|
13 | 13 | from segmentation_skeleton_metrics import utils |
14 | 14 |
|
15 | 15 |
|
16 | | -def make_entry(node_id, parent_id, xyz): |
17 | | - """ |
18 | | - Makes an entry to be written in an swc file. |
| 16 | +def read(path, cloud_read=False): |
| 17 | + return read_from_cloud(path) if cloud_read else read_from_local(path) |
19 | 18 |
|
20 | | - Parameters |
21 | | - ---------- |
22 | | - graph : networkx.Graph |
23 | | - Graph that "node_id" and "parent_id" belong to. |
24 | | - node_id : int |
25 | | - Node that entry corresponds to. |
26 | | - parent_id : int |
27 | | - Parent of node "node_id". |
28 | | - xyz : ... |
29 | | - xyz coordinate of "node_id". |
30 | 19 |
|
31 | | - Returns |
32 | | - ------- |
33 | | - entry : str |
34 | | - Entry to be written in an swc file. |
35 | | -
|
36 | | - """ |
37 | | - x, y, z = tuple(xyz) |
38 | | - entry = f"{node_id} 2 {x} {y} {z} 8 {parent_id}" |
39 | | - return entry |
| 20 | +def read_from_cloud(path): |
| 21 | + pass |
40 | 22 |
|
41 | 23 |
|
42 | | -def save(path, xyz_1, xyz_2, color=None): |
| 24 | +def read_from_local(path): |
43 | 25 | """ |
44 | | - Writes an swc file. |
| 26 | + Reads swc file stored at "path" on local machine. |
45 | 27 |
|
46 | 28 | Parameters |
47 | 29 | ---------- |
48 | | - path : str |
49 | | - Path on local machine that swc file will be written to. |
50 | | - entry_list : list[list] |
51 | | - List of entries that will be written to an swc file. |
52 | | - color : str, optional |
53 | | - Color of nodes. The default is None. |
54 | | -
|
55 | | - Returns |
56 | | - ------- |
57 | | - None. |
58 | | -
|
59 | | - """ |
60 | | - with open(path, "w") as f: |
61 | | - # Preamble |
62 | | - if color is not None: |
63 | | - f.write("# COLOR " + color) |
64 | | - else: |
65 | | - f.write("# id, type, z, y, x, r, pid") |
66 | | - f.write("\n") |
67 | | - |
68 | | - # Entries |
69 | | - f.write(make_entry(1, -1, xyz_1)) |
70 | | - f.write("\n") |
71 | | - f.write(make_entry(2, 1, xyz_2)) |
72 | | - |
73 | | - |
74 | | -def to_graph(path, anisotropy=[1.0, 1.0, 1.0]): |
75 | | - """ |
76 | | - Reads an swc file and builds an undirected graph from it. |
77 | | -
|
78 | | - Parameters |
79 | | - ---------- |
80 | | - path : str |
| 30 | + Path : str |
81 | 31 | Path to swc file to be read. |
82 | | - anisotropy : list[float], optional |
83 | | - Image to real-world coordinates scaling factors for (x, y, z) that is |
84 | | - applied to swc files. The default is [1.0, 1.0, 1.0]. |
85 | 32 |
|
86 | 33 | Returns |
87 | 34 | ------- |
88 | | - networkx.Graph |
89 | | - Graph constructed from an swc file. |
| 35 | + list |
| 36 | + List such that each entry is a line from the swc file. |
90 | 37 |
|
91 | 38 | """ |
92 | | - graph = nx.Graph(swc_id=utils.get_swc_id(path)) |
93 | | - with open(path, "r") as f: |
94 | | - offset = [0, 0, 0] |
95 | | - for line in f.readlines(): |
96 | | - if line.startswith("# OFFSET"): |
97 | | - parts = line.split() |
98 | | - offset = read_xyz(parts[2:5]) |
99 | | - if not line.startswith("#"): |
100 | | - parts = line.split() |
101 | | - child = int(parts[0]) |
102 | | - parent = int(parts[-1]) |
103 | | - xyz = read_xyz( |
104 | | - parts[2:5], anisotropy=anisotropy, offset=offset |
105 | | - ) |
106 | | - graph.add_node(child, xyz=xyz) |
107 | | - if parent != -1: |
108 | | - graph.add_edge(parent, child) |
109 | | - return graph |
| 39 | + with open(path, "r") as file: |
| 40 | + return file.readlines() |
110 | 41 |
|
111 | 42 |
|
112 | 43 | def get_xyz_coords(path, anisotropy=[1.0, 1.0, 1.0]): |
@@ -167,3 +98,96 @@ def read_xyz(xyz, anisotropy=[1.0, 1.0, 1.0], offset=[0, 0, 0]): |
167 | 98 | """ |
168 | 99 | xyz = [float(xyz[i]) + offset[i] for i in range(3)] |
169 | 100 | return np.array([xyz[i] / anisotropy[i] for i in range(3)], dtype=int) |
| 101 | + |
| 102 | + |
| 103 | +def save(path, xyz_1, xyz_2, color=None): |
| 104 | + """ |
| 105 | + Writes an swc file. |
| 106 | +
|
| 107 | + Parameters |
| 108 | + ---------- |
| 109 | + path : str |
| 110 | + Path on local machine that swc file will be written to. |
| 111 | + entry_list : list[list] |
| 112 | + List of entries that will be written to an swc file. |
| 113 | + color : str, optional |
| 114 | + Color of nodes. The default is None. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + None. |
| 119 | +
|
| 120 | + """ |
| 121 | + with open(path, "w") as f: |
| 122 | + # Preamble |
| 123 | + if color is not None: |
| 124 | + f.write("# COLOR " + color) |
| 125 | + else: |
| 126 | + f.write("# id, type, z, y, x, r, pid") |
| 127 | + f.write("\n") |
| 128 | + |
| 129 | + # Entries |
| 130 | + f.write(make_entry(1, -1, xyz_1)) |
| 131 | + f.write("\n") |
| 132 | + f.write(make_entry(2, 1, xyz_2)) |
| 133 | + |
| 134 | + |
| 135 | +def make_entry(node_id, parent_id, xyz): |
| 136 | + """ |
| 137 | + Makes an entry to be written in an swc file. |
| 138 | +
|
| 139 | + Parameters |
| 140 | + ---------- |
| 141 | + graph : networkx.Graph |
| 142 | + Graph that "node_id" and "parent_id" belong to. |
| 143 | + node_id : int |
| 144 | + Node that entry corresponds to. |
| 145 | + parent_id : int |
| 146 | + Parent of node "node_id". |
| 147 | + xyz : ... |
| 148 | + xyz coordinate of "node_id". |
| 149 | +
|
| 150 | + Returns |
| 151 | + ------- |
| 152 | + entry : str |
| 153 | + Entry to be written in an swc file. |
| 154 | +
|
| 155 | + """ |
| 156 | + x, y, z = tuple(xyz) |
| 157 | + entry = f"{node_id} 2 {x} {y} {z} 8 {parent_id}" |
| 158 | + return entry |
| 159 | + |
| 160 | + |
| 161 | +def to_graph(path, anisotropy=[1.0, 1.0, 1.0]): |
| 162 | + """ |
| 163 | + Reads an swc file and builds an undirected graph from it. |
| 164 | +
|
| 165 | + Parameters |
| 166 | + ---------- |
| 167 | + path : str |
| 168 | + Path to swc file to be read. |
| 169 | + anisotropy : list[float], optional |
| 170 | + Image to real-world coordinates scaling factors for (x, y, z) that is |
| 171 | + applied to swc files. The default is [1.0, 1.0, 1.0]. |
| 172 | +
|
| 173 | + Returns |
| 174 | + ------- |
| 175 | + networkx.Graph |
| 176 | + Graph built from an swc file. |
| 177 | +
|
| 178 | + """ |
| 179 | + graph = nx.Graph(swc_id=utils.get_swc_id(path)) |
| 180 | + offset = [0, 0, 0] |
| 181 | + for line in read(path): |
| 182 | + if line.startswith("# OFFSET"): |
| 183 | + parts = line.split() |
| 184 | + offset = read_xyz(parts[2:5]) |
| 185 | + if not line.startswith("#"): |
| 186 | + parts = line.split() |
| 187 | + child = int(parts[0]) |
| 188 | + parent = int(parts[-1]) |
| 189 | + xyz = read_xyz(parts[2:5], anisotropy=anisotropy, offset=offset) |
| 190 | + graph.add_node(child, xyz=xyz) |
| 191 | + if parent != -1: |
| 192 | + graph.add_edge(parent, child) |
| 193 | + return graph |
0 commit comments