traj.py 7.1 KB

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