-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_io.py
More file actions
376 lines (298 loc) · 11.5 KB
/
file_io.py
File metadata and controls
376 lines (298 loc) · 11.5 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from __future__ import annotations
from pathlib import Path
import json
import re
from typing import Iterable, Sequence, Iterator, Dict
from itertools import chain
from core_utils import ensure_dir
def write_json(path: Path | str, data) -> None:
"""Write *data* as JSON to *path*."""
ensure_dir(path)
with Path(path).open("w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
def write_value(path: Path | str, value) -> None:
"""Write ``value`` to ``path`` followed by a newline."""
ensure_dir(path)
with Path(path).open("w+") as f:
f.write(f"{value}\n")
def read_values(path: Path | str) -> list[float]:
"""Return a list of floats from the first line of ``path``."""
with Path(path).open() as f:
return [float(x) for x in f.readline().split()]
def read_value(path: Path | str) -> float:
"""Return the first float found in ``path`` or ``0.0``."""
values = read_values(path)
return values[0] if values else 0.0
_def_replace = re.compile(r"\s(\S+)")
def list_text_files(directory: Path) -> list[Path]:
"""Return ``.txt`` files within ``directory`` sorted alphabetically."""
if not directory.is_dir():
raise NotADirectoryError(directory)
return sorted(p for p in directory.iterdir() if p.suffix == ".txt")
def read_lines(path: Path) -> list[str]:
"""Return lines from ``path`` stripped of trailing newlines."""
with path.open(encoding="utf-8") as fh:
return [line.rstrip("\n") for line in fh]
def zero_line(reference: str) -> str:
"""Return ``reference`` with the value column replaced by ``0``."""
return _def_replace.sub(" 0", reference, count=1)
def read_sanitised_lines(path: Path) -> list[str]:
"""Return lines from ``path`` without ``[]`` or commas."""
remove_chars = str.maketrans("", "", "[],")
with path.open(encoding="utf-8") as f:
return [line.translate(remove_chars).rstrip("\n") for line in f]
def zero_pad(line: str) -> str:
"""Return ``line`` with the second column replaced by ``0``."""
return _def_replace.sub(" 0", line)
def write_swc(directory: Path | str, file_name: str, lines, comment: str = "", use_temp_file: bool = False) -> Path:
"""Write an edited SWC file to ``directory`` and return its path."""
dir_path = Path(directory) if use_temp_file else Path(directory) / "downloads" / "files"
ensure_dir(dir_path)
suffix = "_new_tmp.swc" if use_temp_file else "_new.swc"
out_path = dir_path / (file_name.replace(".swc", "") + suffix)
with out_path.open("w", encoding="utf-8") as f:
if comment:
f.write(comment + "\n")
f.write("\n".join(lines) + "\n")
return out_path
def write_lines(path: Path | str, lines: Iterable[Sequence]) -> None:
"""Write space-separated ``lines`` to ``path``."""
ensure_dir(path)
with Path(path).open("w", encoding="utf-8") as f:
for line in lines:
print(*line, file=f)
def write_dict(path: Path | str, data: dict) -> None:
"""Write the contents of ``data`` as whitespace-separated rows."""
ensure_dir(path)
with Path(path).open("w", encoding="utf-8") as f:
for key in sorted(data):
value = data[key]
if isinstance(value, (list, tuple)):
print(key, *value, file=f)
else:
print(key, value, file=f)
def write_plot(
path: Path | str,
before: list[Sequence],
after: list[Sequence],
skip: int = 2,
) -> None:
"""Write combined plot data to ``path``."""
ensure_dir(path)
with Path(path).open("w", encoding="utf-8") as f:
for x, y, z, d in before[skip:]:
f.write(f"{x[0]} {y[0]} {z[0]} {x[1]} {y[1]} {z[1]} {d} 0x0000FF\n")
for x, y, z, d in after[skip:]:
f.write(f"{x[0]} {y[0]} {z[0]} {x[1]} {y[1]} {z[1]} {d} 0xFF0000\n")
def read_single_value(path: Path) -> float:
"""Return the single float stored in ``path`` or ``0.0``."""
try:
return float(read_value(path))
except (OSError, ValueError):
return 0.0
def read_table_data(path: Path, with_error: bool = False):
"""Return parsed columns from ``path`` or empty lists on failure."""
import numpy as np
if not path.is_file():
return [], [], [] if with_error else []
try:
data = np.loadtxt(path)
except (OSError, ValueError):
return [], [], [] if with_error else []
data = np.atleast_2d(data)
labels = data[:, 0].astype(int).tolist()
means = data[:, 1].astype(float).tolist()
if with_error and data.shape[1] > 2:
errors = data[:, 2].astype(float).tolist()
return labels, means, errors
return labels, means
def read_compare_values(path: Path) -> tuple[float, float, float, float]:
"""Return means and errors for two groups stored in ``path``."""
try:
a_mean, a_err, b_mean, b_err = read_values(path)[:4]
except (OSError, ValueError):
return 0.0, 0.0, 0.0, 0.0
return a_mean, a_err, b_mean, b_err
def read_bulk_files(directory: Path, files: Sequence[str], reader):
"""Return ``reader`` applied to each file in ``directory``."""
return [reader(directory / name) for name in files]
def write_pickle(path: Path | str, data) -> None:
"""Serialise ``data`` using :mod:`pickle` to ``path``."""
import pickle
ensure_dir(path)
with Path(path).open("wb") as f:
pickle.dump(data, f)
# ---------------------------------------------------------------------------
# Neuron export utilities previously kept in ``neuron_export.py``
# ---------------------------------------------------------------------------
Color = str
DEFAULT_COLOR: Color = "0x0000FF"
def _soma_connections(soma_points: Sequence[Sequence]) -> Iterator[list]:
"""Yield line segments connecting soma samples to their parent."""
lookup = {p[0]: p for p in soma_points}
for p in soma_points:
parent = lookup.get(p[6])
if parent:
yield [
p[2],
p[3],
p[4],
parent[2],
parent[3],
parent[4],
p[5],
DEFAULT_COLOR,
]
def _dendrite_connections(
dendrite_roots: Iterable[int],
dendrite_samples: Dict[int, Sequence[Sequence]],
samples: Dict[int, Sequence],
parents: Dict[int, int],
) -> Iterator[list]:
"""Yield line segments between dendrite samples and their parents."""
for dend in dendrite_roots:
for point in dendrite_samples[dend]:
parent_idx = parents.get(point[6], -1)
if parent_idx == -1 or point[1] == 2:
continue
parent = samples[parent_idx]
yield [
point[2],
point[3],
point[4],
parent[2],
parent[3],
parent[4],
point[5],
DEFAULT_COLOR,
]
def _collect_segments(
dendrite_roots: Iterable[int],
dendrite_samples: Dict[int, Sequence[Sequence]],
samples: Dict[int, Sequence],
parents: Dict[int, int],
soma_samples: Sequence[Sequence],
) -> list[list]:
"""Gather all line segments for soma and dendrites."""
return list(
chain(
_soma_connections(soma_samples),
_dendrite_connections(dendrite_roots, dendrite_samples, samples, parents),
)
)
def _export_graph(
abs_path: Path | str,
file_name: str,
dendrite_roots: Iterable[int],
dendrite_samples: Dict[int, Sequence[Sequence]],
samples: Dict[int, Sequence],
parents: Dict[int, int],
soma_samples: Sequence[Sequence],
suffix: str,
verbose: bool = False,
) -> None:
"""Export coordinates for a morphology to ``*_suffix.txt``."""
segments = _collect_segments(
dendrite_roots, dendrite_samples, samples, parents, soma_samples
)
if verbose:
from logger_utils import log
log(f"{len(segments)} segments")
log(file_name)
out_path = Path(abs_path) / f"{file_name.replace('.swc', '')}_{suffix}.txt"
write_lines(out_path, segments)
def first_graph(
abs_path: Path | str,
file_name: str,
dendrite_roots: Iterable[int],
dendrite_samples: Dict[int, Sequence[Sequence]],
samples: Dict[int, Sequence],
parents: Dict[int, int],
soma_samples: Sequence[Sequence],
) -> None:
"""Write coordinates of the original morphology to ``*_before.txt``."""
_export_graph(
abs_path,
file_name,
dendrite_roots,
dendrite_samples,
samples,
parents,
soma_samples,
"before",
)
def second_graph(
abs_path: Path | str,
file_name: str,
dendrite_roots: Iterable[int],
dendrite_samples: Dict[int, Sequence[Sequence]],
samples: Dict[int, Sequence],
parents: Dict[int, int],
soma_samples: Sequence[Sequence],
) -> None:
"""Write coordinates of the edited morphology to ``*_after.txt``."""
_export_graph(
abs_path,
file_name,
dendrite_roots,
dendrite_samples,
samples,
parents,
soma_samples,
"after",
verbose=True,
)
PlotEntry = list[Sequence[float]]
def build_plot_from_lines(lines: Iterable[str]) -> list[PlotEntry]:
"""Return plot segments for ``lines`` of an SWC file."""
from swc_parser import parse_swc_lines
from core_utils import round_to
_, samples = parse_swc_lines(lines)
plot: list[PlotEntry] = []
for idx, vals in samples.items():
parent = int(vals[6])
if parent == -1 or parent not in samples:
continue
x = [round_to(float(vals[2]), 0.01), round_to(float(samples[parent][2]), 0.01)]
y = [round_to(float(vals[3]), 0.01), round_to(float(samples[parent][3]), 0.01)]
z = [round_to(float(vals[4]), 0.01), round_to(float(samples[parent][4]), 0.01)]
plot.append([x, y, z, float(vals[5])])
return plot
def graph(
initial_file: Iterable[str],
modified_file: Iterable[str],
action: str,
directory: str | Path,
file_name: str,
) -> None:
"""Create an overlay plot describing the changes between two SWC files."""
before_plot: list[PlotEntry] = build_plot_from_lines(initial_file)
after_plot: list[PlotEntry] = build_plot_from_lines(modified_file)
if action in {"shrink", "remove"}:
after_plot = [p for p in before_plot if p not in after_plot]
elif action in {"extend", "branch"}:
after_plot = [p for p in after_plot if p not in before_plot]
fname = Path(directory) / f"{file_name.replace('.swc', '')}_neuron.txt"
write_plot(fname, before_plot, after_plot)
class FileIO:
"""Convenient namespace for common file helpers."""
write_json = staticmethod(write_json)
write_value = staticmethod(write_value)
read_values = staticmethod(read_values)
read_value = staticmethod(read_value)
list_text_files = staticmethod(list_text_files)
read_lines = staticmethod(read_lines)
zero_line = staticmethod(zero_line)
read_sanitised_lines = staticmethod(read_sanitised_lines)
zero_pad = staticmethod(zero_pad)
write_swc = staticmethod(write_swc)
write_lines = staticmethod(write_lines)
write_dict = staticmethod(write_dict)
write_plot = staticmethod(write_plot)
read_single_value = staticmethod(read_single_value)
read_table_data = staticmethod(read_table_data)
read_compare_values = staticmethod(read_compare_values)
read_bulk_files = staticmethod(read_bulk_files)
write_pickle = staticmethod(write_pickle)
build_plot_from_lines = staticmethod(build_plot_from_lines)
graph = staticmethod(graph)