From 46108e7e2fa6ae632f1736cbd57eccd18453d70c Mon Sep 17 00:00:00 2001
From: Mariska Wesseling <m.g.h.wesseling@tudelft.nl>
Date: Tue, 27 Feb 2024 08:30:24 +0100
Subject: [PATCH] changes to fit tibia ssm to CT

---
 fitSSM.py     | 45 +++++++++++++++++++++++----------------------
 fitSSM_mri.py | 10 +++++-----
 2 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/fitSSM.py b/fitSSM.py
index e63ce15..4c04990 100644
--- a/fitSSM.py
+++ b/fitSSM.py
@@ -10,9 +10,9 @@ from shutil import copyfile
 import glob
 
 subjects = [9,13,19,23,26,29,32,35,37,41]  #[9,13,19,23,26,29,32,35,37,41]
-segments = ['femur']  #'femur',
-short = 0
-run = 1
+segments = ['tibia']  #'femur',
+short = 1
+run = 0
 
 occurances=[]
 all_occ = []
@@ -109,7 +109,7 @@ for segment in segments:
                 com_file = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\groomed\com_aligned\Segmentation_' + segment + '_' + side + '_short_' + str(subject) + reflect + '.isores.pad.com.nrrd'
                 particle_file = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r"_bone\new_bone\shape_models/" + points + '\Segmentation_' + segment + '_' + side + '_short_' + str(subject) + reflect + '.isores.pad.com.center.aligned.clipped.cropped.tpSmoothDT_local.particles'
                 xyz_file = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\shape_models\Segmentation_' + segment + '_' + side + '_short_' + str(subject) + reflect + '.isores.pad.com.center.aligned.clipped.cropped.tpSmoothDT_local.xyz'
-                # align_file = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output\femur_bone\new_bone\groomed\aligned\Segmentation_femur_' + side + '_short_' + str(subject) + reflect + '.isores.pad.com.center.aligned.nrrd'
+                # align_file = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output\femur_bone\new_bone\groomed\aligned\Segmentation_femur_' + side + str(subject) + reflect + '.isores.pad.com.center.aligned.nrrd'
                 path_bones = r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\input'
 
             # get change in position from nrrd header files
@@ -168,13 +168,13 @@ for segment in segments:
                 iters=iters+1
             # ms6.apply_filter('transform_translate_center_set_origin', traslmethod=0, axisx=0, axisy=0, axisz=-222)
             ms6.apply_filter('transform_translate_center_set_origin', traslmethod =0, axisx=0, axisy=0, axisz=translate[2]+max_val*iters)
-            ms6.save_current_mesh(path + '\SSM_' + segment + '_transform.xyz')
+            ms6.save_current_mesh(path + '\SSM_' + segment + short_name + '_transform.xyz')
 
             # run ICP to get final position SSM point cloud on original mesh
-            mesh = trimesh.load_mesh(path + '\Segmentation_' + segment + '_resample.stl')
+            mesh = trimesh.load_mesh(path + '\Segmentation_' + segment + short_name + '_resample.stl')
             # mesh = trimesh.load_mesh(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\input\Segmentation_' + segment + '_' + side + '_short_' + str(subject) + '_remesh.stl')
             # mesh = trimesh.load_mesh(path_bones + '\Segmentation_' + segment + '_' + side + '_short_' + str(subject) + '.STL')
-            points = trimesh.load_mesh(path + '\SSM_' + segment + '_transform.xyz')
+            points = trimesh.load_mesh(path + '\SSM_' + segment + short_name + '_transform.xyz')
             if reflect == '.reflect':
                 M = trimesh.transformations.scale_and_translate((-1,1,1))
                 points.apply_transform(M)
@@ -190,7 +190,7 @@ for segment in segments:
             # icp = trimesh.registration.icp(points.vertices, mesh, initial=np.identity(4), threshold=1e-5, max_iterations=20,**kwargs)
             # points.apply_transform(icp[0])
 
-            np.savetxt(path + '\SSM_' + segment + '_transform_icp.xyz', points.vertices, delimiter=" ")
+            np.savetxt(path + '\SSM_' + segment + short_name + '_transform_icp.xyz', points.vertices, delimiter=" ")
 
             # ms5 = pymeshlab.MeshSet()
             # ms5.load_new_mesh(path + '\SSM_' + segment + '_transform_icp.xyz')
@@ -200,7 +200,7 @@ for segment in segments:
         else:
             short_name = ''
         if run == 2:
-            points = trimesh.load_mesh(path + '\8192\SSM_' + segment + short_name + '_transform_icp.xyz') #_short
+            points = trimesh.load_mesh(path + '\SSM_' + segment + short_name + '_transform_icp.xyz') #_short
             if segment == 'fibula':
                 segment_temp = 'tibia'
                 Counter = len(glob.glob1(path, 'Segmentation_' + segment_temp + '_area' + str(ligaments_fib[9][ind]) + '*.stl'))
@@ -271,30 +271,31 @@ for segment in segments:
             all_occ.append(np.asarray([i[0] for i in order]))
             occurances.append(np.asarray([i[0] for i in most_occ]))
             orders.append(np.asarray([i[1] for i in order]))
+
     elif run == 0:
-        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances_8192' + segment + '.npz')
+        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances_' + segment + short_name + '.npz')
         occurances.append(bla['PCL']); occurances.append(bla['MCLp']); occurances.append(bla['MCLd']); occurances.append(bla['post_obl']); occurances.append(bla['ACL']);\
             occurances.append(bla['LCL']); occurances.append(bla['pop'])
-        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_8192' + segment + '.npz')
+        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_' + segment + short_name + '.npz')
         all_occ.append(bla['PCL']); all_occ.append(bla['MCLp']); all_occ.append(bla['MCLd']); all_occ.append(bla['post_obl']); all_occ.append(bla['ACL']);
         all_occ.append(bla['LCL']); all_occ.append(bla['pop'])
-        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_8192' + segment + '.npz')
+        bla = np.load(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_' + segment + short_name + '.npz')
         orders.append(bla['PCL']); orders.append(bla['MCLp']); orders.append(bla['MCLd']); orders.append(bla['post_obl']); orders.append(bla['ACL']);
         orders.append(bla['LCL']); orders.append(bla['pop'])
 
-    points = trimesh.load_mesh(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\shape_models\mean_shape_8192.xyz')
-    np.savetxt(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\shape_models\meanshape_ligs_8192.xyz', points.vertices[np.hstack(occurances).astype(int)], delimiter=" ")
+    points = trimesh.load_mesh(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone' + short_name + r'\new_bone\shape_models\mean_shape.xyz')
+    np.savetxt(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone' + short_name + r'\new_bone\shape_models\meanshape_ligs.xyz', points.vertices[np.hstack(occurances).astype(int)], delimiter=" ")
 
     pred_lig_points_color = np.c_[points.vertices[np.hstack(all_occ).astype(int)], np.hstack(orders).astype(int)]
     np.savetxt(
-        r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\shape_models\meanshape_ligs_color_8192.xyz',
+        r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone' + short_name + r'\new_bone\shape_models\meanshape_ligs_color.xyz',
         pred_lig_points_color, delimiter=" ")
 
     mask = np.ones(len(points.vertices), dtype=bool)
     mask[np.hstack(occurances).astype(int)] = False
     # np.savetxt(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + r'_bone\new_bone\shape_models\meanshape_bone_no_lig.xyz',
     #            points.vertices[np.where(mask)], delimiter=" ")
-    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances_8192' + segment + '_order.npy', PCL=orders[0],MCLp=orders[1],
+    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances' + segment + short_name + '_order.npy', PCL=orders[0],MCLp=orders[1],
              MCLd=orders[2],post_obl=orders[3],ACL=orders[4],LCL=orders[5],pop=orders[6])
 
 
@@ -315,11 +316,11 @@ for segment in segments:
             side = 'L'
             reflect = '.reflect'
 
-        points = trimesh.load_mesh(path + '\8192\SSM_' + segment + short_name +'_transform_icp.xyz') #_short
+        points = trimesh.load_mesh(path + '\SSM_' + segment + short_name +'_transform_icp.xyz') #_short
         pred_lig_points = points.vertices[np.hstack(occurances).astype(int)]
-        np.savetxt(path + '\8192\SSM_' + segment + '_pred_points_8192.xyz', np.asarray(pred_lig_points), delimiter=" ")
+        np.savetxt(path + '\SSM_' + segment + short_name + '_pred_points.xyz', np.asarray(pred_lig_points), delimiter=" ")
         pred_lig_points_color = np.c_[points.vertices[np.hstack(all_occ).astype(int)],np.hstack(orders).astype(int)]
-        np.savetxt(path + '\8192\SSM_' + segment + '_pred_points_color_8192.xyz', np.asarray(pred_lig_points_color), delimiter=" ")
+        np.savetxt(path + '\SSM_' + segment + short_name + '_pred_points_color.xyz', np.asarray(pred_lig_points_color), delimiter=" ")
 
     # for modes in range(1,4):
     #     points = trimesh.load_mesh(r'C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/' + segment + '_bone\mode' + str(modes) + '_+2sd.xyz')
@@ -340,9 +341,9 @@ for segment in segments:
 
 
 
-    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances_8192' + segment ,PCL=occurances[0],MCLp=occurances[1],
+    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\occurances_' + segment + short_name ,PCL=occurances[0],MCLp=occurances[1],
              MCLd=occurances[2],post_obl=occurances[3],ACL=occurances[4],LCL=occurances[5],pop=occurances[6])
-    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_8192' + segment, PCL=all_occ[0],MCLp=all_occ[1],
+    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_' + segment + short_name, PCL=all_occ[0],MCLp=all_occ[1],
              MCLd=all_occ[2],post_obl=all_occ[3],ACL=all_occ[4],LCL=all_occ[5],pop=all_occ[6])
-    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_8192' + segment, PCL=orders[0],
+    np.savez(r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_' + segment + short_name, PCL=orders[0],
              MCLp=orders[1], MCLd=orders[2], post_obl=orders[3], ACL=orders[4], LCL=orders[5], pop=orders[6])
diff --git a/fitSSM_mri.py b/fitSSM_mri.py
index 1026396..edc0546 100644
--- a/fitSSM_mri.py
+++ b/fitSSM_mri.py
@@ -38,9 +38,9 @@ def csv2xyz(csv_path):
 
 subjects = ['1L','2L','3L','4L','5L','6L','8L','9L','1R','2R','3R','4R','5R','6R','8R','9R']  # ['1']  # ['S0']  # [9,13,19,23,26,29,32,35,37,41]
 sides = ['L','L','L','L','L','L','L','L', 'R','R','R','R','R','R','R','R']
-segments = ['femur','tibia', 'fibula']  #['tibia']  #
-short_ssm = [0,1,0]  #[1]  #
-no_particles = [4096,4096,2048]  #[4096]  #
+segments = ['tibia']  # ['femur','tibia', 'fibula']  #['tibia']  #
+short_ssm = [1]  # [0,1,0]  #[1]  #
+no_particles = [4096]  # [4096,4096,2048]  #[4096]  #
 data_folder = r"C:\Users\mariskawesseli\Documents\GitLab\knee_ssm\OAI\Output/"
 
 for seg_ind, segment in enumerate(segments):
@@ -55,11 +55,11 @@ for seg_ind, segment in enumerate(segments):
     occurances = [occ['PCL'], occ['MCLp'], occ['MCLd'], occ['post_obl'], occ['ACL'], occ['LCL'], occ['pop']]
 
     all_occ = np.load(
-        r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_' + segment + '.npz')
+        r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_' + segment + short + '.npz')
     all_occ = [all_occ['PCL'], all_occ['MCLp'], all_occ['MCLd'], all_occ['post_obl'], all_occ['ACL'], all_occ['LCL'], all_occ['pop']]
 
     order = np.load(
-        r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_' + segment + '.npz')
+        r'C:\Users\mariskawesseli\Documents\LigamentStudy\ImageData\all_occurances_orders_' + segment + short + '.npz')
     orders = [order['PCL'], order['MCLp'], order['MCLd'], order['post_obl'], order['ACL'], order['LCL'],
                order['pop']]
 
-- 
GitLab