MAIN_Analisis_Esferas_Neuronas_EJEMPLO_Maria.m 13 KB

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