12
12
13
13
import numpy as np
14
14
15
+ from .util import (array_index_order_codes , gifti_encoding_codes ,
16
+ gifti_endian_codes , KIND2FMT )
15
17
from .. import xmlutils as xml
16
18
from ..filebasedimages import FileBasedHeader , FileBasedImage
17
19
from ..nifti1 import data_type_codes , xform_codes , intent_codes
18
- from .util import (array_index_order_codes , gifti_encoding_codes ,
19
- gifti_endian_codes , KIND2FMT )
20
20
21
21
# {en,de}codestring in deprecated in Python3, but
22
22
# {en,de}codebytes not available in Python2.
@@ -40,7 +40,12 @@ def from_dict(klass, data_dict):
40
40
meda .data .append (nv )
41
41
return meda
42
42
43
+ @np .deprecate_with_doc ("Use the metadata property instead." )
43
44
def get_metadata (self ):
45
+ return self .metadata
46
+
47
+ @property
48
+ def metadata (self ):
44
49
""" Returns metadata as dictionary """
45
50
self .data_as_dict = {}
46
51
for ele in self .data :
@@ -58,7 +63,7 @@ def _to_xml_element(self):
58
63
return metadata
59
64
60
65
def print_summary (self ):
61
- print (self .get_metadata () )
66
+ print (self .metadata )
62
67
63
68
64
69
class GiftiNVPairs (object ):
@@ -118,10 +123,28 @@ def __init__(self, key=0, label='', red=None, green=None, blue=None,
118
123
self .blue = blue
119
124
self .alpha = alpha
120
125
126
+ @np .deprecate_with_doc ("Use the rgba property instead." )
121
127
def get_rgba (self ):
128
+ return self .rgba
129
+
130
+ @property
131
+ def rgba (self ):
122
132
""" Returns RGBA as tuple """
123
133
return (self .red , self .green , self .blue , self .alpha )
124
134
135
+ @rgba .setter
136
+ def rgba (self , rgba ):
137
+ """ Set RGBA via tuple
138
+
139
+ Parameters
140
+ ----------
141
+ rgba : tuple (red, green, blue, alpha)
142
+
143
+ """
144
+ if len (rgba ) != 4 :
145
+ raise ValueError ('rgba must be length 4.' )
146
+ self .red , self .green , self .blue , self .alpha = rgba
147
+
125
148
126
149
def _arr2txt (arr , elem_fmt ):
127
150
arr = np .asarray (arr )
@@ -163,6 +186,17 @@ def print_summary(self):
163
186
print ('Affine Transformation Matrix: \n ' , self .xform )
164
187
165
188
189
+ @np .deprecate_with_doc ("This is an internal API that will be discontinued." )
190
+ def data_tag (dataarray , encoding , datatype , ordering ):
191
+ class DataTag (xml .XmlSerializable ):
192
+ def __init__ (self , * args ):
193
+ self .args = args
194
+ def _to_xml_element (self ):
195
+ return _data_tag_element (* self .args )
196
+
197
+ return DataTag (dataarray , encoding , datatype , ordering ).to_xml ()
198
+
199
+
166
200
def _data_tag_element (dataarray , encoding , datatype , ordering ):
167
201
""" Creates the data tag depending on the required encoding,
168
202
returns as XML element"""
@@ -297,6 +331,34 @@ def _to_xml_element(self):
297
331
298
332
return data_array
299
333
334
+ @np .deprecate_with_doc ("Use the to_xml() function instead." )
335
+ def to_xml_open (self ):
336
+ out = """<DataArray Intent="%s"
337
+ \t DataType="%s"
338
+ \t ArrayIndexingOrder="%s"
339
+ \t Dimensionality="%s"
340
+ %s\t Encoding="%s"
341
+ \t Endian="%s"
342
+ \t ExternalFileName="%s"
343
+ \t ExternalFileOffset="%s">\n """
344
+ di = ""
345
+ for i , n in enumerate (self .dims ):
346
+ di = di + '\t Dim%s=\" %s\" \n ' % (str (i ), str (n ))
347
+ return out % (intent_codes .niistring [self .intent ],
348
+ data_type_codes .niistring [self .datatype ],
349
+ array_index_order_codes .label [self .ind_ord ],
350
+ str (self .num_dim ),
351
+ str (di ),
352
+ gifti_encoding_codes .specs [self .encoding ],
353
+ gifti_endian_codes .specs [self .endian ],
354
+ self .ext_fname ,
355
+ self .ext_offset ,
356
+ )
357
+
358
+ @np .deprecate_with_doc ("Use the to_xml() function instead." )
359
+ def to_xml_close (self ):
360
+ return "</DataArray>\n "
361
+
300
362
def print_summary (self ):
301
363
print ('Intent: ' , intent_codes .niistring [self .intent ])
302
364
print ('DataType: ' , data_type_codes .niistring [self .datatype ])
@@ -313,13 +375,15 @@ def print_summary(self):
313
375
print ('Coordinate System:' )
314
376
print (self .coordsys .print_summary ())
315
377
378
+ @np .deprecate_with_doc ("Use the metadata property instead." )
316
379
def get_metadata (self ):
317
- """ Returns metadata as dictionary """
318
- return self .meta .get_metadata ()
380
+ return self .meta .metadata
319
381
382
+ @property
383
+ def metadata (self ):
384
+ """ Returns metadata as dictionary """
385
+ return self .meta .metadata
320
386
321
- class GiftiHeader (FileBasedHeader ):
322
- pass
323
387
324
388
class GiftiImage (FileBasedImage , xml .XmlSerializable ):
325
389
valid_exts = ('.gii' ,)
@@ -332,38 +396,52 @@ def __init__(self, header=None, extra=None, file_map=None, meta=None,
332
396
333
397
if darrays is None :
334
398
darrays = []
335
- self .darrays = darrays
336
399
if meta is None :
337
- self .meta = GiftiMetaData ()
338
- else :
339
- self .meta = meta
400
+ meta = GiftiMetaData ()
340
401
if labeltable is None :
341
- self .labeltable = GiftiLabelTable ()
342
- else :
343
- self .labeltable = labeltable
344
- self .numDA = len (self .darrays )
402
+ labeltable = GiftiLabelTable ()
403
+
404
+ self ._labeltable = labeltable
405
+ self ._meta = meta
406
+
407
+ self .darrays = darrays
345
408
self .version = version
346
409
347
- def get_labeltable (self ):
348
- return self .labeltable
410
+ @property
411
+ def numDA (self ):
412
+ return len (self .darrays )
349
413
350
- def set_labeltable (self , labeltable ):
414
+ @property
415
+ def labeltable (self ):
416
+ return self ._labeltable
417
+
418
+ @labeltable .setter
419
+ def labeltable (self , labeltable ):
351
420
""" Set the labeltable for this GiftiImage
352
421
353
422
Parameters
354
423
----------
355
424
labeltable : GiftiLabelTable
356
425
357
426
"""
358
- if isinstance (labeltable , GiftiLabelTable ):
359
- self .labeltable = labeltable
360
- else :
361
- print ("Not a valid GiftiLabelTable instance" )
427
+ if not isinstance (labeltable , GiftiLabelTable ):
428
+ raise TypeError ("Not a valid GiftiLabelTable instance" )
429
+ self ._labeltable = labeltable
362
430
363
- def get_metadata (self ):
364
- return self .meta
431
+ @np .deprecate_with_doc ("Use the gifti_img.labeltable property instead." )
432
+ def set_labeltable (self , labeltable ):
433
+ self .labeltable = labeltable
365
434
366
- def set_metadata (self , meta ):
435
+ @np .deprecate_with_doc ("Use the gifti_img.labeltable property instead." )
436
+ def get_labeltable (self ):
437
+ return self .labeltable
438
+
439
+ @property
440
+ def meta (self ):
441
+ return self ._meta
442
+
443
+ @meta .setter
444
+ def meta (self , meta ):
367
445
""" Set the metadata for this GiftiImage
368
446
369
447
Parameters
@@ -374,12 +452,17 @@ def set_metadata(self, meta):
374
452
-------
375
453
None
376
454
"""
377
- if isinstance (meta , GiftiMetaData ):
378
- self .meta = meta
379
- print ("New Metadata set. Be aware of changing "
380
- "coordinate transformation!" )
381
- else :
382
- print ("Not a valid GiftiMetaData instance" )
455
+ if not isinstance (meta , GiftiMetaData ):
456
+ raise TypeError ("Not a valid GiftiMetaData instance" )
457
+ self ._meta = meta
458
+
459
+ @np .deprecate_with_doc ("Use the gifti_img.labeltable property instead." )
460
+ def set_metadata (self , meta ):
461
+ self .meta = meta
462
+
463
+ @np .deprecate_with_doc ("Use the gifti_img.labeltable property instead." )
464
+ def get_meta (self ):
465
+ return self .meta
383
466
384
467
def add_gifti_data_array (self , dataarr ):
385
468
""" Adds a data array to the GiftiImage
@@ -388,24 +471,20 @@ def add_gifti_data_array(self, dataarr):
388
471
----------
389
472
dataarr : GiftiDataArray
390
473
"""
391
- if isinstance (dataarr , GiftiDataArray ):
392
- self .darrays .append (dataarr )
393
- self .numDA += 1
394
- else :
395
- print ("dataarr paramater must be of tzpe GiftiDataArray" )
474
+ if not isinstance (dataarr , GiftiDataArray ):
475
+ raise TypeError ("Not a valid GiftiDataArray instance" )
476
+ self .darrays .append (dataarr )
396
477
397
478
def remove_gifti_data_array (self , ith ):
398
479
""" Removes the ith data array element from the GiftiImage """
399
480
self .darrays .pop (ith )
400
- self .numDA -= 1
401
481
402
482
def remove_gifti_data_array_by_intent (self , intent ):
403
483
""" Removes all the data arrays with the given intent type """
404
484
intent2remove = intent_codes .code [intent ]
405
485
for dele in self .darrays :
406
486
if dele .intent == intent2remove :
407
487
self .darrays .remove (dele )
408
- self .numDA -= 1
409
488
410
489
def get_arrays_from_intent (self , intent ):
411
490
""" Returns a a list of GiftiDataArray elements matching
@@ -438,7 +517,6 @@ def print_summary(self):
438
517
print (da .print_summary ())
439
518
print ('----end----' )
440
519
441
-
442
520
def _to_xml_element (self ):
443
521
GIFTI = xml .Element ('GIFTI' , attrib = {
444
522
'Version' : self .version ,
0 commit comments