17
17
[1]: https://groups.google.com/a/humanconnectome.org/g/hcp-users/c/EGuwdaTVFuw/m/tg7a_-7mAQAJ
18
18
"""
19
19
import xml .etree .ElementTree as et
20
+ from urllib .parse import urlparse
20
21
21
- from .. import xmlutils as xml
22
- from ..caret import CaretMetaData
22
+ import nibabel as nb
23
+ from nibabel import pointset as ps
24
+ from nibabel import xmlutils as xml
25
+ from nibabel .caret import CaretMetaData
23
26
24
27
25
28
class CaretSpecDataFile (xml .XmlSerializable ):
@@ -56,6 +59,9 @@ def __init__(self, structure=None, data_file_type=None, selected=None, uri=None)
56
59
self .selected = selected
57
60
self .uri = uri
58
61
62
+ if data_file_type == 'SURFACE' :
63
+ self .__class__ = SurfaceDataFile
64
+
59
65
def _to_xml_element (self ):
60
66
data_file = xml .Element ('DataFile' )
61
67
data_file .attrib ['Structure' ] = str (self .structure )
@@ -68,6 +74,35 @@ def __repr__(self):
68
74
return self .to_xml ().decode ()
69
75
70
76
77
+ class SurfaceDataFile (ps .TriangularMesh , CaretSpecDataFile ):
78
+ _gifti = None
79
+ _coords = None
80
+ _triangles = None
81
+
82
+ def _get_gifti (self ):
83
+ if self ._gifti is None :
84
+ parts = urlparse (self .uri )
85
+ if parts .scheme == 'file' :
86
+ self ._gifti = nb .load (parts .path )
87
+ elif parts .scheme == '' :
88
+ self ._gifti = nb .load (self .uri )
89
+ else :
90
+ self ._gifti = nb .GiftiImage .from_url (self .uri )
91
+ return self ._gifti
92
+
93
+ def get_triangles (self , name = None ):
94
+ if self ._triangles is None :
95
+ gifti = self ._get_gifti ()
96
+ self ._triangles = gifti .agg_data ('triangle' )
97
+ return self ._triangles
98
+
99
+ def get_coords (self , name = None ):
100
+ if self ._coords is None :
101
+ gifti = self ._get_gifti ()
102
+ self ._coords = gifti .agg_data ('pointset' )
103
+ return self ._coords
104
+
105
+
71
106
class CaretSpecFile (xml .XmlSerializable ):
72
107
"""Class for CaretSpecFile XML documents
73
108
0 commit comments