11
11
import re
12
12
import pandas as pd
13
13
import itertools
14
+ import os
14
15
15
16
import ants
16
17
from ants .internal import get_lib_fn , get_pointer_string , process_arguments
@@ -1254,7 +1255,8 @@ def label_image_registration(fixed_label_images,
1254
1255
moving_intensity_images = None ,
1255
1256
fixed_mask = None ,
1256
1257
moving_mask = None ,
1257
- type_of_linear_transform = 'affine' ,
1258
+ initial_transforms = 'affine' ,
1259
+ type_of_linear_transform = None ,
1258
1260
type_of_deformable_transform = 'antsRegistrationSyNQuick[so]' ,
1259
1261
label_image_weighting = 1.0 ,
1260
1262
output_prefix = '' ,
@@ -1287,9 +1289,16 @@ def label_image_registration(fixed_label_images,
1287
1289
Defines region for similarity metric calculation in the space
1288
1290
of the moving image.
1289
1291
1292
+ initial_transforms : string or list of files
1293
+ If specified, there are two options: 2) Use label images with
1294
+ the centers of mass to a calculate linear transform of type
1295
+ 'identity', 'rigid', 'similarity', or 'affine'. 2) Specify a
1296
+ list of transform files, e.g., the output of ants.registration().
1297
+
1290
1298
type_of_linear_transform : string
1291
1299
Use label images with the centers of mass to a calculate linear
1292
1300
transform of type 'identity', 'rigid', 'similarity', or 'affine'.
1301
+ Deprecated-subsumed by initial_transforms.
1293
1302
1294
1303
type_of_deformable_transform : string
1295
1304
Only works with deformable-only transforms, specifically the family
@@ -1336,14 +1345,22 @@ def label_image_registration(fixed_label_images,
1336
1345
[r64_seg1, r64_seg2],
1337
1346
fixed_intensity_images=r16,
1338
1347
moving_intensity_images=r64,
1339
- type_of_linear_transform ='affine',
1348
+ initial_transforms ='affine',
1340
1349
type_of_deformable_transform='antsRegistrationSyNQuick[bo]',
1341
1350
label_image_weighting=[1.0, 2.0],
1342
1351
verbose=True)
1343
1352
"""
1344
1353
1345
1354
# Perform validation check on the input
1346
1355
1356
+ if type_of_linear_transform is not None :
1357
+ print ( "\n " )
1358
+ print ( "*****************************************************************************************" )
1359
+ print ( "Deprecation warning. typeOfLinearTransform is deprecated. Please use initialTransforms." )
1360
+ print ( "*****************************************************************************************" )
1361
+ print ( "\n " )
1362
+ initial_transforms = type_of_linear_transform
1363
+
1347
1364
if isinstance (fixed_label_images , ants .ANTsImage ):
1348
1365
fixed_label_images = [ants .image_clone (fixed_label_images )]
1349
1366
if isinstance (moving_label_images , ants .ANTsImage ):
@@ -1374,10 +1391,6 @@ def label_image_registration(fixed_label_images,
1374
1391
if output_prefix == "" or output_prefix is None or len (output_prefix ) == 0 :
1375
1392
output_prefix = mktemp ()
1376
1393
1377
- allowable_linear_transforms = ['rigid' , 'similarity' , 'affine' , 'identity' ]
1378
- if not type_of_linear_transform in allowable_linear_transforms :
1379
- raise ValueError ("Unrecognized linear transform." )
1380
-
1381
1394
do_deformable = True
1382
1395
if type_of_deformable_transform is None or len (type_of_deformable_transform ) == 0 :
1383
1396
do_deformable = False
@@ -1401,14 +1414,15 @@ def label_image_registration(fixed_label_images,
1401
1414
if verbose :
1402
1415
print ("Total number of labels: " + str (total_number_of_labels ))
1403
1416
1417
+ initial_xfrm_files = list ()
1418
+
1404
1419
##############################
1405
1420
#
1406
- # Linear transform
1421
+ # Initial linear transform
1407
1422
#
1408
1423
##############################
1409
1424
1410
- linear_xfrm = None
1411
- if type_of_linear_transform != 'identity' :
1425
+ if isinstance (initial_transforms , str ) and initial_transforms in ['rigid' , 'similarity' , 'affine' ]:
1412
1426
1413
1427
if verbose :
1414
1428
print ("\n \n Computing linear transform.\n " )
@@ -1436,21 +1450,20 @@ def label_image_registration(fixed_label_images,
1436
1450
1437
1451
linear_xfrm = ants .fit_transform_to_paired_points (moving_centers_of_mass ,
1438
1452
fixed_centers_of_mass ,
1439
- transform_type = type_of_linear_transform ,
1453
+ transform_type = initial_transforms ,
1440
1454
verbose = verbose )
1455
+
1456
+ if do_deformable :
1457
+ linear_xfrm_file = output_prefix + "LandmarkBasedLinear" + initial_transforms + ".mat"
1458
+ else :
1459
+ linear_xfrm_file = output_prefix + "0GenericAffine.mat"
1441
1460
1442
- linear_xfrm_file = output_prefix + "0GenericAffine.mat"
1443
1461
ants .write_transform (linear_xfrm , linear_xfrm_file )
1462
+ initial_xfrm_files .append (linear_xfrm_file )
1444
1463
1445
- ##############################
1446
- #
1447
- # Deformable transform
1448
- #
1449
- ##############################
1450
-
1451
- if do_deformable :
1452
-
1453
- if type_of_linear_transform == "identity" :
1464
+ elif initial_transforms is not None or initial_transforms == 'identity' :
1465
+
1466
+ if do_deformable :
1454
1467
for i in range (len (common_label_ids )):
1455
1468
for j in range (len (common_label_ids [i ])):
1456
1469
label = common_label_ids [i ][j ]
@@ -1460,6 +1473,23 @@ def label_image_registration(fixed_label_images,
1460
1473
moving_single_label_image ,
1461
1474
label_image_weights [i ], 0 ])
1462
1475
1476
+ if initial_transforms != 'identity' :
1477
+ if not isinstance (initial_transforms , list ):
1478
+ initial_transforms = [initial_transforms ]
1479
+ for i in range (len (initial_transforms )):
1480
+ if not os .path .exists (initial_transforms [i ]):
1481
+ raise ValueError (initial_transforms [i ] + " does not exist." )
1482
+ else :
1483
+ initial_xfrm_files .append (initial_transforms [i ])
1484
+
1485
+ ##############################
1486
+ #
1487
+ # Deformable transform
1488
+ #
1489
+ ##############################
1490
+
1491
+ if do_deformable :
1492
+
1463
1493
if verbose :
1464
1494
print ("\n \n Computing deformable transform using images.\n " )
1465
1495
@@ -1561,14 +1591,14 @@ def label_image_registration(fixed_label_images,
1561
1591
syn_stage .insert (0 , "BSplineSyN[" + str (gradient_step ) + "," + str (spline_distance ) + ",0,3]" )
1562
1592
syn_stage .insert (0 , "--transform" )
1563
1593
1564
- args = None
1565
- if linear_xfrm is None :
1566
- args = [ "--dimensionality" , str ( image_dimension ),
1567
- "--output" , output_prefix ]
1568
- else :
1569
- args = ["--dimensionality " , str ( image_dimension ),
1570
- "-r" , linear_xfrm_file ,
1571
- "--output" , output_prefix ]
1594
+ args = [ "--dimensionality" , str ( image_dimension ),
1595
+ "--output" , output_prefix ]
1596
+
1597
+ if len ( initial_xfrm_files ) > 0 :
1598
+ for i in range ( len ( initial_xfrm_files )) :
1599
+ initial_args = ["-r " , initial_xfrm_files [ i ]]
1600
+ args . append ( initial_args )
1601
+
1572
1602
args .append (syn_stage )
1573
1603
1574
1604
fixed_mask_string = 'NA'
@@ -1614,22 +1644,20 @@ def label_image_registration(fixed_label_images,
1614
1644
1615
1645
all_xfrms = sorted (set (glob .glob (output_prefix + "*" + "[0-9]*" )))
1616
1646
1617
- find_inverse_warps = np .where ([re .search ("[0-9]InverseWarp.nii.gz" , ff ) for ff in all_xfrms ])[0 ]
1618
- find_forward_warps = np .where ([re .search ("[0-9]Warp.nii.gz" , ff ) for ff in all_xfrms ])[0 ]
1647
+ find_inverse_warps_idx = np .where ([re .search ("[0-9]InverseWarp.nii.gz" , ff ) for ff in all_xfrms ])[0 ]
1648
+ find_forward_warps_idx = np .where ([re .search ("[0-9]Warp.nii.gz" , ff ) for ff in all_xfrms ])[0 ]
1649
+ find_affines_idx = np .where ([re .search ("[0-9]GenericAffine.mat" , ff ) for ff in all_xfrms ])[0 ]
1619
1650
1620
- fwdtransforms = []
1621
- invtransforms = []
1622
- if linear_xfrm is not None :
1623
- if len (find_inverse_warps ) > 0 :
1624
- fwdtransforms = [all_xfrms [find_forward_warps [0 ]], linear_xfrm_file ]
1625
- invtransforms = [linear_xfrm_file , all_xfrms [find_inverse_warps [0 ]]]
1626
- else :
1627
- fwdtransforms = [linear_xfrm_file ]
1628
- invtransforms = [linear_xfrm_file ]
1629
- else :
1630
- if len (find_inverse_warps ) > 0 :
1631
- fwdtransforms = [all_xfrms [find_forward_warps [0 ]]]
1632
- invtransforms = [all_xfrms [find_inverse_warps [0 ]]]
1651
+ fwdtransforms = list ()
1652
+ invtransforms = list ()
1653
+
1654
+ if len (find_forward_warps_idx ) > 0 :
1655
+ fwdtransforms .append (all_xfrms [find_forward_warps_idx [0 ]])
1656
+ if len (find_affines_idx ) > 0 :
1657
+ fwdtransforms .append (all_xfrms [find_affines_idx [0 ]])
1658
+ invtransforms .append (all_xfrms [find_affines_idx [0 ]])
1659
+ if len (find_inverse_warps_idx ) > 0 :
1660
+ invtransforms .append (all_xfrms [find_inverse_warps_idx [0 ]])
1633
1661
1634
1662
if verbose :
1635
1663
print ("\n \n Resulting transforms" )
0 commit comments