MAIN_Estim_Long_Axon_por_Esferas_EJEMPLOS.m 13 KB

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