MAIN_Analisis_Planos_Neuronas.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. clear all;
  2. % %%
  3. clc
  4. % Leemos el archivo de Matlab conteniendo las neuronas
  5. % Num_File = 6;
  6. % load(['Cortes_Muestra_Axon_', num2str(Num_File), '.mat']);
  7. clear all;
  8. clc
  9. Path_Data_1 = '/Users/pepo/Google Drive (1)/Cesar Mario Axones/Neuronas/Selección de NeuroMorpho 23-2/Específicas - tipo 1/';
  10. Path_Data_File_1 = '/Neuronas Especificas - Tipo 1.txt';
  11. % Path_Data_1 = '/Users/pepo/Google Drive (1)/Cesar Mario Axones/Neuronas/Selección de NeuroMorpho 23-2/Multiespecíficas - tipo 2/';
  12. % Path_Data_File_1 = '/Neuronas Multiespecificas - Tipo 2.txt';
  13. % Path_Data_1 = '/Users/pepo/Google Drive (1)/Cesar Mario Axones/Neuronas/Selección de NeuroMorpho 23-2/Inespecíficas - tipo 3/';
  14. % Path_Data_File_1 = '/Neuronas Inespecificas - Tipo 3.txt';
  15. % Path_Data_1 = '/Users/pepo/Google Drive (1)/Cesar Mario Axones/Neuronas/Selección de NeuroMorpho 23-2/Locales- tipo 4/';
  16. % Path_Data_File_1 = '/Neuronas Locales - Tipo 4.txt';
  17. % Read the names of files with raw data:
  18. fileID = fopen([Path_Data_1, Path_Data_File_1]); % Spanish Data by Carlos
  19. %
  20. names_1 = textscan(fileID,'%s', 'delimiter', '\n', 'whitespace', '');
  21. fclose(fileID);
  22. names_1 = names_1{1};
  23. %%
  24. h_Cortes = 1; % Analizamos un corte cada 'h_Cortes' cortes de la muestra
  25. Grosor_Corte = 50; % En micras
  26. % El 'frame' es la sampling box en la que se introduce la esfera; esa caja luego se introduce en la celda de la rejilla en que se ha dividido el corte.
  27. sampling_box_height = Grosor_Corte; % en micras
  28. lado_x_Frame = Grosor_Corte; % en micras
  29. lado_y_Frame = Grosor_Corte; % en micras
  30. Diams_Sonda = [10:5:50]; % Vector con los diámetros de la sonda a considerar. En micras
  31. Step_Lengths = [70:10:150]; % Vector con los step lengths a considerar. En micras
  32. MATRIZ_Q = zeros( length( Step_Lengths ), length( Diams_Sonda ), length(names_1) ); % Filas para los step_lengths y columnas para los diámetros de la sonda
  33. MATRIZ_ERROR_LENGTH = zeros( length( Step_Lengths ), length( Diams_Sonda ), length(names_1) ); % Filas para los step_lengths y columnas para los diámetros de la sonda
  34. AXON_REAL_LENGTH = zeros(1, length(names_1));
  35. for rr = 1:length(names_1)
  36. % for rr = [101, 102, 106, 110]
  37. tic
  38. % Leemos los archivos
  39. fname=fullfile(Path_Data_1, [names_1{rr}]);
  40. fname_new = [fname(1:end-4), '.mat'];
  41. load( fname_new );
  42. [Matriz_Q, Matriz_Error_Length, Axon_Real_Length] = Sgript_Estim_Long_Axon_Estereo ( AXON_Cell, AXON, Diams_Sonda, Step_Lengths, Grosor_Corte, h_Cortes, lado_x_Frame, lado_y_Frame, sampling_box_height );
  43. MATRIZ_Q( :, :, rr ) = Matriz_Q;
  44. MATRIZ_ERROR_LENGTH( :, :, rr ) = Matriz_Error_Length;
  45. AXON_REAL_LENGTH(1, rr) = Axon_Real_Length;
  46. clc
  47. toc
  48. disp(rr)
  49. end
  50. % %% Salvamos los resultados
  51. %
  52. % clc
  53. %
  54. % Path_Data_Save = '/Users/pepo/Google Drive (1)/Cesar Mario Axones/Resultados/';
  55. % fname_Save=fullfile(Path_Data_Save, ['Result_Estim_Long_Axon_por_Estereo_h_Cortes=', num2str(h_Cortes),'_', Path_Data_File_1(2:end-4)]);
  56. % save( fname_Save );
  57. %%
  58. %% Exploramos la distribución de los errores e intersecciones en diferentes corrdenadas [e, d]
  59. ind_e = 1;
  60. ind_d = 2;
  61. num_bins = 15;
  62. Distrib_Error_Length = MATRIZ_ERROR_LENGTH(ind_e, ind_d, :);
  63. Distrib_Q = MATRIZ_Q(ind_e, ind_d, :);
  64. figure('color', 'w', 'position', [50, 200, 1100, 400]);
  65. subplot(1, 2, 1);
  66. histogram( Distrib_Error_Length, num_bins, 'facecolor', 'k', 'normalization', 'probability' );
  67. xlabel('Error Length (%)', 'fontsize', 16);
  68. % ylim([0, 0.2]);
  69. % xlim([-25, 25]);
  70. title(['Steph Length = ', num2str(Step_Lengths(ind_e)), '. Probe Diameter = ', num2str(Diams_Sonda(ind_d))]);
  71. subplot(1, 2, 2);
  72. histogram( Distrib_Q, num_bins, 'facecolor', 'b', 'normalization', 'probability' );
  73. xlabel('Intersections', 'fontsize', 16);
  74. % ylim([0, 0.2]);
  75. % xlim([-25, 25]);
  76. title(['Steph Length = ', num2str(Step_Lengths(ind_e)), '. Probe Diameter = ', num2str(Diams_Sonda(ind_d))]);
  77. %% Ploteamos las medias y std de los errores
  78. clc
  79. Saturacion = 100; % A partir de este error (%) saturamos la matriz
  80. Matriz_Error_Length_MEAN_SATURADA = mean(abs(MATRIZ_ERROR_LENGTH), 3);
  81. Matriz_Error_Length_MEAN_SATURADA( Matriz_Error_Length_MEAN_SATURADA >= Saturacion ) = Saturacion;
  82. Matriz_Error_Length_STD_SATURADA = std(abs(MATRIZ_ERROR_LENGTH), 0, 3);
  83. Matriz_Error_Length_STD_SATURADA( Matriz_Error_Length_STD_SATURADA >= Saturacion ) = Saturacion;
  84. figure('color', 'w', 'position', [50, 200, 1100, 400]);
  85. suptitle(Path_Data_File_1(2:end-4));
  86. subplot(1, 2, 1);
  87. imagesc(Matriz_Error_Length_MEAN_SATURADA);
  88. xticklabels({ '10', '15', '20', '25', '30', '35', '40', '45', '50' });
  89. yticklabels({ '70', '80', '90', '100', '110', '120', '130', '140', '150' });
  90. xlabel('Probe Diameter', 'fontsize', 18);
  91. ylabel('Step Length', 'fontsize', 18);
  92. title('Mean Error Length (%)', 'fontsize', 15);
  93. colorbar
  94. subplot(1, 2, 2);
  95. imagesc(Matriz_Error_Length_STD_SATURADA);
  96. xticklabels({ '10', '15', '20', '25', '30', '35', '40', '45', '50' });
  97. yticklabels({ '70', '80', '90', '100', '110', '120', '130', '140', '150' });
  98. xlabel('Probe Diameter', 'fontsize', 18);
  99. ylabel('Step Length', 'fontsize', 18);
  100. title('STD Error Length (%)', 'fontsize', 15);
  101. colorbar
  102. %%
  103. %% Ploteamos
  104. rr = 46;
  105. fname=fullfile(Path_Data_1, [names_1{rr}]);
  106. fname_new = [fname(1:end-4), '.mat'];
  107. load( fname_new );
  108. %%
  109. clc
  110. corte = 6;
  111. colores = {'r', 'b', 'm', 'k', 'g'};
  112. figure;
  113. suptitle(names_1{rr});
  114. hold on;
  115. for i = 1:corte
  116. Puntos_Axon = AXON_Cell{1, i};
  117. plot3( Puntos_Axon(:, 1), Puntos_Axon(:, 2), Puntos_Axon(:, 3), '-r', 'linewidth', 1.5 );
  118. end
  119. for i = corte+1:length(AXON_Cell)
  120. Puntos_Axon = AXON_Cell{1, i};
  121. plot3( Puntos_Axon(:, 1), Puntos_Axon(:, 2), Puntos_Axon(:, 3), '-k', 'linewidth', 1.5 );
  122. xlabel('X');
  123. ylabel('Y');
  124. zlabel('Z');
  125. % zlim([150 200]);
  126. % view([-96, -90]);
  127. axis equal
  128. end
  129. %%
  130. clc
  131. h_Cortes = 1; % Analizamos un corte cada 'h_Cortes' cortes de la muestra
  132. Grosor_Corte = 50; % En micras
  133. % El 'frame' es la sampling box en la que se introduce la esfera; esa caja luego se introduce en la celda de la rejilla en que se ha dividido el corte.
  134. sampling_box_height = Grosor_Corte; % en micras
  135. lado_x_Frame = Grosor_Corte; % en micras
  136. lado_y_Frame = Grosor_Corte; % en micras
  137. Diams_Sonda = 50; % Vector con los diámetros de la sonda a considerar. En micras
  138. Step_Lengths = 150; % Vector con los step lengths a considerar. En micras
  139. AXON_Cell_0 = AXON_Cell(1, 7:end);
  140. AXON_0 = AXON(88:end, :);
  141. % for rr = [101, 102, 106, 110]
  142. tic
  143. % Leemos los archivos
  144. fname=fullfile(Path_Data_1, [names_1{rr}]);
  145. fname_new = [fname(1:end-4), '.mat'];
  146. load( fname_new );
  147. [Matriz_Q, Matriz_Error_Length, Axon_Real_Length, Estimated_Axon_Length, Filas_Centros, Columnas_Centros, Z_Centros] = Sgript_Estim_Long_Axon_Estereo ( AXON_Cell_0, AXON_0, Diams_Sonda, Step_Lengths, Grosor_Corte, h_Cortes, lado_x_Frame, lado_y_Frame, sampling_box_height );
  148. MATRIZ_Q( :, :, rr ) = Matriz_Q;
  149. MATRIZ_ERROR_LENGTH( :, :, rr ) = Matriz_Error_Length;
  150. AXON_REAL_LENGTH(1, rr) = Axon_Real_Length;
  151. clc
  152. toc
  153. %%
  154. rr = 46;
  155. Saturacion = 100; % A partir de este error (%) saturamos la matriz
  156. Matriz_Error_Length_SATURADA = abs(MATRIZ_ERROR_LENGTH(:, :, rr));
  157. Matriz_Error_Length_SATURADA( Matriz_Error_Length_SATURADA >= Saturacion ) = Saturacion;
  158. Matriz_Q_Dummy = abs(MATRIZ_Q(:, :, rr));
  159. figure('color', 'w', 'position', [50, 200, 1100, 400]);
  160. suptitle(names_1{rr});
  161. subplot(1, 2, 1);
  162. imagesc(Matriz_Error_Length_SATURADA);
  163. xticklabels({ '10', '15', '20', '25', '30', '35', '40', '45', '50' });
  164. yticklabels({ '70', '80', '90', '100', '110', '120', '130', '140', '150' });
  165. xlabel('Probe Diameter', 'fontsize', 18);
  166. ylabel('Step Length', 'fontsize', 18);
  167. title('Error Length (%)', 'fontsize', 15);
  168. colorbar
  169. subplot(1, 2, 2);
  170. imagesc(Matriz_Q_Dummy);
  171. xticklabels({ '10', '15', '20', '25', '30', '35', '40', '45', '50' });
  172. yticklabels({ '70', '80', '90', '100', '110', '120', '130', '140', '150' });
  173. xlabel('Probe Diameter', 'fontsize', 18);
  174. ylabel('Step Length', 'fontsize', 18);
  175. title('Number of Intersections', 'fontsize', 15);
  176. colorbar
  177. %% Ploteamos el axón con las esferas
  178. clc
  179. [x,y,z] = sphere;
  180. rad_sonda = Diams_Sonda/2;
  181. figure;
  182. hold on;
  183. for i = 1:length(AXON_Cell)
  184. Puntos_Axon = AXON_Cell{1, i};
  185. plot3( Puntos_Axon(:, 1), Puntos_Axon(:, 2), Puntos_Axon(:, 3), '-k', 'markersize', 10, 'linewidth', 1.5 );
  186. xlabel('X');
  187. ylabel('Y');
  188. zlabel('Z');
  189. % zlim([150 200]);
  190. % view([-96, -90]);
  191. axis equal
  192. end
  193. % Esferas
  194. for i_x = Columnas_Centros % Columna de la rejilla
  195. for i_y = Filas_Centros % Fila de la rejilla
  196. for i_z = 1:h_Cortes:length(Z_Centros) % Corte
  197. surf( rad_sonda*x + i_x, rad_sonda*y + i_y, rad_sonda*z + Z_Centros(i_z),'FaceAlpha',0.9,'EdgeColor','none' );
  198. end
  199. end
  200. end
  201. %% Para chequear
  202. clc
  203. for j=ind_CORTE
  204. % j=437
  205. [row_a, col_a] = find(~cellfun(@isempty, dummy_CORTE{1, j})); % para cada rama estos son los índices de las celdas de ese corte en el que aparecen dichas ramas
  206. % disp([row_a, col_a]);
  207. %
  208. k = 2;
  209. if length(row_a)>1
  210. dummy_a = dummy_CORTE{1, j}{row_a(k), col_a(k)}; % Coordenadas de los puntos de la rama cuya intersección estamos estudiando
  211. % Obtenemos las distancias al centro de la sonda esférica:
  212. dist_centro_sonda = sqrt( ( dummy_a(:, 1) - Columnas_Centros(col_a(k)) ).^2 + ( dummy_a(:, 2) - Filas_Centros(row_a(k)) ).^2 +...
  213. ( dummy_a(:, 3) - Z_Centros(i) ).^2 );
  214. num_inters = sum( abs( diff( dist_centro_sonda <= rad_sonda ) ) );
  215. if num_inters > 3
  216. disp(j)
  217. end
  218. end
  219. end
  220. %% Comprobamos que está bien, ploteando rama a rama:
  221. [x,y,z] = sphere;
  222. % clc
  223. i_x = col_a(k); % Columna de la rejilla
  224. i_y = row_a(k); % Fila de la rejilla
  225. i_z = i; % Corte
  226. colores = {'r', 'b', 'm', 'k', 'g'};
  227. figure('color', 'w', 'position', [250, 200, 700, 600]);
  228. hold on
  229. % for iii = 1:length(Cortes_XY_Axon{i_z, 1})
  230. for iii = ind_CORTE
  231. if ~isempty( Cortes_XY_Axon{i_z, 1}{1, iii} )
  232. dummy = Cortes_XY_Axon{i_z, 1}{1, iii};
  233. for i_fila = 1:size(dummy, 1)
  234. for i_columna = 1:size(dummy, 2)
  235. if ~isempty(dummy{i_fila, i_columna})
  236. Puntos_Axon = dummy{i_fila, i_columna};
  237. plot3( Puntos_Axon(:, 1), Puntos_Axon(:, 2), Puntos_Axon(:, 3), '-o', 'color', colores{mod(i, 5)+1}, 'markersize', 7, 'linewidth', 1.5 );
  238. end
  239. end
  240. end
  241. end
  242. axis equal
  243. xlim([Columnas_Rejilla(i_x), Columnas_Rejilla(i_x) + Rejilla_X]);
  244. ylim([Filas_Rejilla(i_y), Filas_Rejilla(i_y) + Rejilla_Y]);
  245. zlim([Cortes(i_z), Cortes(i_z) + Grosor_Corte]);
  246. view([32, 20]);
  247. end
  248. surf( rad_sonda*x + Columnas_Centros(col_a(k)), rad_sonda*y + Filas_Centros(row_a(k)), rad_sonda*z + Z_Centros(i) );
  249. alpha 0.7
  250. % surf( rad_sonda*x + Columnas_Centros(i_x), rad_sonda*y + Filas_Centros(i_y), rad_sonda*z + Z_Centros(i_z) );
  251. %% Comprobamos que está bien, ploteando ramas enteras:
  252. [x,y,z] = sphere;
  253. % clc
  254. i_x = 2; % Columna de la rejilla
  255. i_y = 1; % Fila de la rejilla
  256. i_z = 2; % Corte
  257. ind_CORTE = find(~cellfun(@isempty, Cortes_XY_Axon{i_z, 1}));
  258. colores = {'r', 'b', 'm', 'k', 'g'};
  259. figure('color', 'w', 'position', [250, 200, 700, 600]);
  260. hold on
  261. % for iii = 1:length(Cortes_XY_Axon{i_z, 1})
  262. for iii = ind_CORTE
  263. if ~isempty( Cortes_XY_Axon{i_z, 1}{1, iii} )
  264. dummy = Cortes_XY_Axon{i_z, 1}{1, iii};
  265. for i_fila = 1:size(dummy, 1)
  266. for i_columna = 1:size(dummy, 2)
  267. if ~isempty(dummy{i_fila, i_columna})
  268. Puntos_Axon = dummy{i_fila, i_columna};
  269. plot3( Puntos_Axon(:, 1), Puntos_Axon(:, 2), Puntos_Axon(:, 3), '-', 'color', colores{mod(i, 5)+1}, 'markersize', 7, 'linewidth', 1.5 );
  270. end
  271. end
  272. end
  273. end
  274. axis equal
  275. view([32, 20]);
  276. end
  277. surf( rad_sonda*x + Columnas_Centros(i_x), rad_sonda*y + Filas_Centros(i_y), rad_sonda*z + Z_Centros(i_z) );
  278. alpha 0.7
  279. xlabel('X');
  280. ylabel('Y');
  281. zlabel('Z');
  282. % xlim([Columnas_Rejilla(i_x), Columnas_Rejilla(i_x) + Rejilla_X]);
  283. % ylim([Filas_Rejilla(i_y), Filas_Rejilla(i_y) + Rejilla_Y]);
  284. % zlim([Cortes(i_z), Cortes(i_z) + Grosor_Corte]);
  285. % % Para la neurona 5
  286. % xlim([-100, 100]);
  287. % ylim([-200, 0]);
  288. % zlim([-80, 60]);
  289. % % Para la neurona 4
  290. % xlim([-600, 400]);
  291. % ylim([-600, 800]);
  292. % zlim([-200, 700]);
  293. % Para la neurona 3
  294. xlim([-250, 100]);
  295. ylim([-300, 200]);
  296. zlim([-100, 80]);
  297. %%
  298. figure
  299. hold on
  300. surf(peaks(30))
  301. alpha 0.5
  302. plot3(10,10,10,'r*')
  303. hold off
  304. %%