Skip to content

Commit 976fbec

Browse files
authored
Merge pull request #3 from vferat/dev
spi rework
2 parents cef5a7e + a9c25a3 commit 976fbec

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ docs/_build/
5858

5959
# PyBuilder
6060
target/
61+
test.sef
62+
pycartool/tests/data/sample_test_write_spi.spi
63+
pycartool/tests/data/sample_test_write_ris.ris

pycartool/io/roi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ def read_roi(filename):
1818
rois : list, shape(n_roi, 2)
1919
A list of ROIs which each element is a list [roi name, elements]
2020
21+
Warning
22+
-------
23+
Indexes start from 1, not 0 as Cartools does. When using
24+
with combination of source space, you way need to tranform
25+
to 0 base indices.
26+
2127
"""
2228
with open(filename) as f:
2329
Roi_type = f.readline().strip()

pycartool/io/source_space.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@ def read_spi(filename):
2929
coord = [elem[:-1] for elem in d]
3030
coord = np.array(coord)
3131
coord = coord.astype(np.float)
32-
return(coord, names)
32+
solution_points = {'names': names,
33+
'coordinates': coord}
34+
return(solution_points)
35+
36+
37+
def write_spi(filename, solution_points):
38+
"""Write Cartool spi file.
39+
40+
Parameters
41+
----------
42+
filename : str
43+
The spi file to write.
44+
solution_points : dict of str
45+
The solution points info. Keys are:
46+
names : list of str
47+
the solutions point names.
48+
coordinates : np.array, shape (n_solutions_points, 3)
49+
the x,y,z coordinates of each solution point.
50+
"""
51+
names = solution_points['names']
52+
x, y, z = solution_points['coordinates'].T
53+
with open(filename, 'w', newline='') as f:
54+
writer = csv.writer(f, delimiter='\t')
55+
for s in enumerate(zip(x, y, z, names)):
56+
writer.writerow(s[1])

pycartool/tests/test_spi.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,33 @@
44
# License: BSD (3-clause)
55

66
import os
7-
from ..io.source_space import read_spi
7+
import numpy as np
8+
from ..io.source_space import read_spi, write_spi
89

910
dir_path = os.path.dirname(os.path.realpath(__file__))
10-
data_path = os.path.join(dir_path, "data")
11+
data_path = os.path.join(dir_path, 'data')
1112

1213

1314
def test_read_spi():
14-
file_path = os.path.join(data_path, "sample_test_spi.spi")
15-
spi = read_spi(file_path)
16-
if not (len(spi[0]) == 5006):
15+
"""Test read_spi"""
16+
file_path = os.path.join(data_path, 'sample_test_spi.spi')
17+
solution_points = read_spi(file_path)
18+
if not len(solution_points['names']) == 5006:
1719
raise AssertionError()
18-
if not (len(spi[1]) == 5006):
20+
if not solution_points['coordinates'].shape[0] == 5006:
1921
raise AssertionError()
22+
23+
24+
def test_write_spi():
25+
"""Test write_spi"""
26+
filename = os.path.join(data_path, 'sample_test_write_spi.spi')
27+
coordinates = np.random.rand(5000, 3)
28+
names = [('S' + str(i)) for i in range(0, 5000)]
29+
write_solution_points = {'names': names,
30+
'coordinates': coordinates}
31+
write_spi(filename, write_solution_points)
32+
read_solution_points = read_spi(filename)
33+
if names != read_solution_points['names']:
34+
raise AssertionError
35+
if (coordinates != read_solution_points['coordinates']).any():
36+
raise AssertionError

0 commit comments

Comments
 (0)