traj.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # from PyQt5.QtWidgets.QWidget import sizeHint
  2. from scipy.ndimage import rotate, map_coordinates
  3. import numpy as np
  4. from scipy.special.cython_special import inv_boxcox
  5. '''
  6. тут собираются по различным траекториям несортированные данные в к-пространство
  7. на выходе хочется получать 5-мерный массив [x, y, z, contrast, coil], чтобы дальнейшие взаимодействие происходило одинаково
  8. '''
  9. def gather_data_along_trajectory(mat_data, nX, nY, nZ, nContr, nCoils=1):
  10. # out: [x, y, z, contrast, coil]
  11. gathered_data = np.zeros((nX, nY, nZ, nContr, nCoils), dtype=np.complex128)
  12. i = 0
  13. for coil in range(nCoils):
  14. for y in range(0, nY):
  15. for z in range(0, nZ):
  16. for contrast in range(0, nContr):
  17. for x in range(0, nX):
  18. gathered_data[x, y, z, contrast, coil] = mat_data[i]
  19. i += 1
  20. return gathered_data
  21. def gather_data_along_trajectory_nonlinear(mat_data, order, nX, nY, nZ, nContr, nCoils=1):
  22. # out: [x, y, z, contrast, coil]
  23. gathered_data = np.zeros((nX, nY, nZ, nContr, nCoils), dtype=np.complex128)
  24. N_center = (nY - 1) // 2
  25. if nY % 2 == 0:
  26. N_center += 1
  27. # print('N_center', N_center)
  28. step_y = order
  29. # print("STEP TSE", step_y)
  30. i = 0
  31. print(len(step_y))
  32. index_y = len(step_y)
  33. for coil in range(nCoils):
  34. for z in range(0, nZ):
  35. for ind in range(index_y):
  36. # for z in range(0, nZ):
  37. for y in step_y[ind]:
  38. # print(y, "step", step_y[ind])
  39. if not isinstance(y, str):
  40. # for z in range(0, nZ):
  41. for x in range(0, nX):
  42. gathered_data[x, y, z, 0, coil] = mat_data[i]
  43. i += 1
  44. # print(x, " ", y, " ", z)
  45. # print("TET")
  46. return gathered_data
  47. def gather_data_along_trajectory_linear_epi(mat_data, nX, nY, nZ, nContr, nCoils=1):
  48. # out: [x, y, z, contrast, coil]
  49. gathered_data = np.zeros((nX, nY, nZ, nContr, nCoils), dtype=np.complex128)
  50. i = 0
  51. for coil in range(nCoils):
  52. for z in range(0, nZ):
  53. for y in range(0, nY):
  54. for contrast in range(0, nContr):
  55. if y % 2 == 1:
  56. for x in range(0, nX):
  57. gathered_data[y, x, z, contrast, coil] = mat_data[i]
  58. i += 1
  59. else:
  60. for x in range(nX - 1, -1, -1):
  61. gathered_data[y, x, z, contrast, coil] = mat_data[i]
  62. i += 1
  63. return gathered_data
  64. def gather_data_along_trajectory_radial(mat_data, order, nX, nY, nZ, phi_wing, N_wings, nCoils=1):
  65. # out: [x, y, z, contrast, coil]
  66. print("traj_01")
  67. k_space_not_rotate = read_raw_data_radial(mat_data, order, nX, N_wings, nCoils)
  68. print("traj_02")
  69. k_space = rotate_radial(k_space_not_rotate, phi_wing, N_wings)
  70. print("traj_03")
  71. k_spaces_with_coils = np.stack(k_space, axis=0)
  72. print("traj_04")
  73. # k_space = interpolation_radial(arr_k_spaces)
  74. k_spaces_with_contrast = np.expand_dims(k_spaces_with_coils, axis=1)
  75. print("traj_05")
  76. k_spaces = np.transpose(k_spaces_with_contrast, (3, 4, 2, 1, 0))
  77. print(k_spaces.shape)
  78. return k_spaces
  79. ########################################## RADIAL ###############################################################
  80. def rotate_radial(k_space_not_rotate_slices, phi_wing, N_wings):
  81. # k_space_not_rotate_slices = [coil, slice, n_wing, x, y]
  82. # print("ROTATE")
  83. k_spaces = []
  84. # result = []
  85. # result_slices = []
  86. # ind = 0
  87. # print(len(k_space_not_rotate_slices)) # 4
  88. for coil in range(k_space_not_rotate_slices.shape[0]):
  89. result = []
  90. result_slices = []
  91. for iSlice in range(k_space_not_rotate_slices.shape[1]):
  92. # print("iSlice: ", iSlice)
  93. # k_space_not_rotate = k_space_not_rotate_slices[iSlice]
  94. k_space_not_rotate = k_space_not_rotate_slices[coil, iSlice]
  95. ind = 0
  96. for i in range(0, phi_wing * N_wings, phi_wing):
  97. # print(ind)
  98. result.append(rotate(k_space_not_rotate[ind], angle=i, reshape=False, order=2))
  99. ind += 1
  100. result_slices.append(result.copy())
  101. k_space = interpolation_radial(result_slices)
  102. k_spaces.append(k_space.copy())
  103. print("len ",len(result))
  104. return k_spaces
  105. def read_raw_data_radial(mat_data, order, nX, N_wings, nCoils):
  106. # out: [coil, slice, n_wing, x, y]
  107. # print(len(mat_data), " ", N_wings, " ", order)
  108. nSlices = int(len(mat_data) / N_wings / len(order[0]) / nX / nCoils)
  109. k_space_size = nX
  110. k_space = np.zeros((nCoils, nSlices, N_wings, k_space_size, k_space_size), dtype=np.complex128)
  111. center_k_space = k_space_size // 2 # положение по Х, центр меняться не будет, можно учитывать как Мх
  112. step_lines = order
  113. # k_spaces_not_rotate = []
  114. ind = 0
  115. for coil in range(nCoils):
  116. # for iSlice in range(nSlices):
  117. for blade_number in range(0, N_wings, 1):
  118. for iSlice in range(nSlices):
  119. for arr_line_number in step_lines:
  120. # print(arr_line_number, "-arr_line_number")
  121. for line_number in arr_line_number:
  122. # print(line_number, "-line_number")
  123. if not isinstance(line_number, str):
  124. for index in range(nX):
  125. # print(index, "-index")
  126. radius = center_k_space + line_number - (len(arr_line_number) // 2)
  127. k_space[coil, iSlice, blade_number, radius, index] = mat_data[ind]
  128. ind += 1
  129. # k_spaces_not_rotate.append(k_space.copy())
  130. # k_space_with_slices[iSlice].append(k_spaces_not_rotate.copy())
  131. # print("k_space: ", len(k_space))
  132. return k_space
  133. def interpolation_radial(arr_k_spaces_slices):
  134. # print("INTERPOL")
  135. result_slices = []
  136. for iSlice in range(len(arr_k_spaces_slices)):
  137. arr_k_spaces = arr_k_spaces_slices[iSlice]
  138. max_shape = tuple(np.max([arr.shape for arr in arr_k_spaces], axis=0))
  139. result = np.zeros(max_shape, dtype=np.complex128)
  140. for array in arr_k_spaces:
  141. scale_factors = [float(s1) / s2 for s1, s2 in zip(max_shape, array.shape)]
  142. grid = [np.linspace(0, dim - 1, num=int(dim * sf))[:max_shape[i]] for i, (dim, sf) in
  143. enumerate(zip(array.shape, scale_factors))]
  144. coords = np.meshgrid(*grid, indexing="ij")
  145. interpolated_array = map_coordinates(array, coords, order=1, mode="nearest")
  146. result += np.nan_to_num(interpolated_array)
  147. result_slices.append(result.copy())
  148. # print(result)
  149. return result_slices