【数据分析】通过python分析提取matlab工作区中的数据

打印 上一主题 下一主题

主题 648|帖子 648|积分 1944

  1. #%%
  2. import scipy.io
  3. import pandas as pd
  4. import numpy as np
  5. # Load the .mat file
  6. file_path = 'clay_C.mat'
  7. mat_data = scipy.io.loadmat(file_path)
  8. ## 建立结果表格
  9. # 定义行、列名
  10. columns = ['0.1', '0.5', '1', '2', '3', '4', '5']
  11. index = ['0.1', '0.5', '1', '2', '3', '4', '5']
  12. my_psnrs_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  13. my_ssims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  14. my_fsims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  15. my_ergas_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  16. #%%
  17. # 假设MAT文件中包含一个名为'C'的数据变量
  18. data = mat_data['C']
  19. #%%
  20. # 将数据转换为DataFrame
  21. df = pd.DataFrame(data)
  22. df = df.transpose()
  23. #%%
  24. for i in range(len(df)):
  25.     row = df.iloc[i][0]
  26.    
  27.     my_df = pd.DataFrame(row)
  28.    
  29.     psnrs_mean = my_df.iloc[0,0][0][-1]
  30.     ssims_mean = my_df.iloc[0,1][0][-1]
  31.     fsims_mean = my_df.iloc[0,2][0][-1]
  32.     ergas_mean = my_df.iloc[0,3][0][-1]
  33.     tau_1 = my_df.iloc[0,4][0][-1]
  34.     tau_2 = my_df.iloc[0,5][0][-1]
  35.    
  36.     my_psnrs_mean.loc[str(tau_1),str(tau_2)] = psnrs_mean
  37.    
  38.     my_ssims_mean.loc[str(tau_1),str(tau_2)] = ssims_mean
  39.    
  40.     my_fsims_mean.loc[str(tau_1),str(tau_2)] = fsims_mean
  41.    
  42.     my_ergas_mean.loc[str(tau_1),str(tau_2)] = ergas_mean
  43. #%%
  44. my_psnrs_mean*10
  45. #%%
  46. my_ssims_mean*10
  47. #%%
  48. my_fsims_mean*10
  49. #%%
  50. my_ergas_mean*10
复制代码
代码具体分析

这段代码从MAT文件中读取数据,并将其转换为多个Pandas DataFrame,以便进行后续的分析和处置惩罚。以下是代码的具体分析:
1. 导入库和读取MAT文件

  1. import scipy.io
  2. import pandas as pd
  3. import numpy as np
  4. # Load the .mat file
  5. file_path = 'clay_C.mat'
  6. mat_data = scipy.io.loadmat(file_path)
复制代码
这里使用了scipy.io.loadmat函数来读取MAT文件,并将其存储在变量mat_data中。
2. 建立结果表格

  1. # 定义行、列名
  2. columns = ['0.1', '0.5', '1', '2', '3', '4', '5']
  3. index = ['0.1', '0.5', '1', '2', '3', '4', '5']
  4. my_psnrs_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  5. my_ssims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  6. my_fsims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
  7. my_ergas_mean = pd.DataFrame(np.nan,index = index, columns=columns)
复制代码
这里创建了四个空的DataFrame来存储结果,每个DataFrame的行和列都是['0.1', '0.5', '1', '2', '3', '4', '5']。
3. 提取数据并转换为DataFrame

  1. # 假设MAT文件中包含一个名为'C'的数据变量
  2. data = mat_data['C']
  3. # 将数据转换为DataFrame
  4. df = pd.DataFrame(data)
  5. df = df.transpose()
复制代码
从MAT文件中提取名为'C'的数据变量,将其转换为Pandas DataFrame,并对其进行转置。
4. 迭代每一行并提取指标

  1. for i in range(len(df)):
  2.     row = df.iloc[i][0]
  3.    
  4.     my_df = pd.DataFrame(row)
  5.    
  6.     psnrs_mean = my_df.iloc[0,0][0][-1]
  7.     ssims_mean = my_df.iloc[0,1][0][-1]
  8.     fsims_mean = my_df.iloc[0,2][0][-1]
  9.     ergas_mean = my_df.iloc[0,3][0][-1]
  10.     tau_1 = my_df.iloc[0,4][0][-1]
  11.     tau_2 = my_df.iloc[0,5][0][-1]
  12.    
  13.     my_psnrs_mean.loc[str(tau_1),str(tau_2)] = psnrs_mean
  14.     my_ssims_mean.loc[str(tau_1),str(tau_2)] = ssims_mean
  15.     my_fsims_mean.loc[str(tau_1),str(tau_2)] = fsims_mean
  16.     my_ergas_mean.loc[str(tau_1),str(tau_2)] = ergas_mean
复制代码
遍历DataFrame的每一行,并提取每行的各项指标值。具体操作如下:


  • 将每一行的第一个元素转换为新的DataFrame my_df。
  • 提取 psnrs_mean、ssims_mean、fsims_mean、ergas_mean、tau_1 和 tau_2 值。
  • 将这些值填入相应的结果DataFrame中,使用tau_1和tau_2作为索引。
5. 打印结果

  1. my_psnrs_mean*10
  2. my_ssims_mean*10
  3. my_fsims_mean*10
  4. my_ergas_mean*10
复制代码
这里将每个结果DataFrame中的值乘以10并显示出来。
总结

该代码的紧张目的是从MAT文件中读取数据,并将此中包罗的各种性能指标提取出来,存储到多个Pandas DataFrame中。每个结果DataFrame的行和列是tau_1和tau_2的组合,存储对应的psnrs_mean、ssims_mean、fsims_mean和ergas_mean值。
原代码

  1. %SpaTRPCA_BSDtest200_5method_Corr10
  2. clear;clc;close all
  3. addpath(genpath('lib'));
  4. addpath(genpath('data'));
  5. addpath(genpath('competing methods'));
  6. rhos = 0.1; %Percent of corrupted pixels
  7. % %% Options settings
  8. % opts.mu = 1e-2;
  9. % opts.tol = 1e-6;
  10. % opts.rho = 1.1;
  11. % opts.max_iter = 500;0
  12. % opts.DEBUG = 0;
  13. % opts.tau_F= 2;
  14.   
  15. method='TCTV';
  16. datadir = 'data/CAVE_USED';
  17. seqs = dir(datadir);
  18. seq3 = seqs(3:end);
  19. n_img=length(seq3);
  20. temp=zeros(n_img,1);
  21. psnrs_TCTV=temp;psnrs_FTCTV=temp;psnrs_HTCTV=temp; psnrs_CTCTV=temp; psnrs_WTCTV=temp; psnrs_GTCTV=temp;
  22. ssims_TCTV=temp;ssims_FTCTV=temp;ssims_HTCTV=temp; ssims_CTCTV=temp; ssims_WTCTV=temp; ssims_GTCTV=temp;
  23. fsims_TCTV=temp;fsims_FTCTV=temp;fsims_HTCTV=temp; fsims_CTCTV=temp; fsims_WTCTV=temp; fsims_GTCTV=temp;
  24. ergas_TCTV=temp;ergas_FTCTV=temp;ergas_HTCTV=temp; ergas_CTCTV=temp; ergas_WTCTV=temp; ergas_GTCTV=temp;
  25. sams_TCTV=temp;sams_FTCTV=temp;sams_HTCTV=temp; sams_CTCTV=temp; sams_WTCTV=temp; sams_GTCTV=temp;
  26. times_TCTV=temp;times_FTCTV=temp;times_HTCTV=temp; times_CTCTV=temp; times_WTCTV=temp; times_GTCTV=temp;
  27. Temp=cell(length(seq3),1);
  28. Orig_Multi=Temp;Noisy_Multi=Temp;TCTV_Multi=Temp;FTCTV_Multi=Temp;HTCTV_Multi=Temp;CTCTV_Multi=Temp;WTCTV_Multi=Temp;GTCTV_Multi=Temp;
  29. C = cell(1,20);
  30. iter=1;
  31. sum_iter = 50;
  32. sum_time = 0;
  33. % for tau_1 = [1,2,3,4,5,6,7,8,9,10]
  34. % for tau_1 = [0.1,0.2,0.3,0.4,0.5]
  35. for tau_1 = [0.1, 0.5, 1, 2, 3, 4, 5 ]
  36. % for tau_2 = [1,2,3,4,5,6,7,8,9,10]
  37. % for tau_2 = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
  38. for tau_2 = [0.1, 0.5, 1, 2, 3, 4, 5]
  39. start_tic = tic;
  40. %% Perform recovery
  41. for i = 5
  42.     % 1 for balloons  2 for beads
  43.     % 3 for cd        4 for chart
  44.     % 5 for clay      6 for cloth
  45.     % 7 for egyptian  8 for feathers
  46.     % 9 for flowers   10 for glass
  47.     %% load data
  48.     fname = seq3(i).name;
  49.     pic_TNN_Weight_Lame = [datadir '/' fname];
  50.     load(fname)
  51.     %% 0-1 Centralization
  52.     data_min = min(X(:));
  53.     data_max = max(X(:));
  54.     X = (X - data_min) / (data_max - data_min);
  55.     %% add noise
  56. %     [n1,n2,n3] = size(X);
  57. %     Xn = X; Yn=reshape(Xn,[n1*n2,n3]);
  58. %     ind = find(rand(n1*n2,1)<rhos);
  59. %     Yn(ind,:)=rand(length(ind),n3);
  60. %     Xn=reshape(Yn,[n1,n2,n3]);
  61.     [height, width, band] = size(X);
  62.     for ii = 1:band
  63.         Xn(:,:,ii) = imnoise(X(:,:,ii), 'salt & pepper', rhos);
  64.     end
  65.     methods_index = 1;
  66.     %% Method 1: TCTV
  67. %     disp('......Run TCTV......')
  68. %     opts = [];
  69. %     opts.rho = 1.25;
  70. %     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  71. %     tic
  72. %     Xhat_TCTV = TCTV_TRPCA(Xn, opts);
  73. %     times_TCTV(i) = toc;
  74. %     [psnrs_TCTV(i), ssims_TCTV(i), fsims_TCTV(i), ergas_TCTV(i), sams_TCTV(i)] = HSI_QA(X * 255, Xhat_TCTV* 255);
  75. % %     TCTV_psnr = psnr(X , Xhat_TCTV);
  76. %     Xhat{methods_index}=Xhat_TCTV;
  77. %     div{methods_index}=1-abs((X-Xhat_TCTV));
  78. %     methods_index = methods_index+1;
  79. %     %% Method 2: FTCTV  Enhanced Fair
  80. %     disp('......Run FTCTV......')
  81. %     opts = [];
  82. %     opts.rho = 1.25;
  83. %     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  84. %     opts.tau_F=[tau_1 tau_1, tau_1];
  85. %     opts.tauL1=tau_2;
  86. %     tic
  87. %     Xhat_FTCTV = FTCTV_TRPCA(Xn, opts);
  88. %     times_FTCTV(i) = toc;
  89. %     [psnrs_FTCTV(i), ssims_FTCTV(i), fsims_FTCTV(i), ergas_FTCTV(i), sams_FTCTV(i)] = HSI_QA(X * 255, Xhat_FTCTV * 255);
  90. %     Xhat{methods_index}=Xhat_FTCTV;
  91. %     div{methods_index}=1-abs((X-Xhat_FTCTV));
  92. %     methods_index = methods_index+1;
  93. %
  94. %     %% Method 3: HTCTV  Enhanced Huber
  95. %     disp('......Run HTCTV......')
  96. %     opts = [];
  97. %     opts.rho = 1.25;
  98. %     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  99. %     opts.tau_F=[tau_1 tau_1, tau_1];
  100. %     opts.tauL1=tau_2;
  101. %     tic
  102. %     Xhat_HTCTV = HTCTV_TRPCA(Xn, opts);
  103. %     times_HTCTV(i) = toc;
  104. %     [psnrs_HTCTV(i), ssims_HTCTV(i), fsims_HTCTV(i), ergas_HTCTV(i), sams_HTCTV(i)] = HSI_QA(X * 255, Xhat_HTCTV * 255);
  105. %     Xhat{methods_index}=Xhat_HTCTV;
  106. %     div{methods_index}=1-abs((X-Xhat_HTCTV));
  107. %     methods_index = methods_index+1;
  108. %
  109. %     %% Method 4: CTCTV  Enhanced Cauchy
  110. %     disp('......Run CTCTV......')
  111. %     opts = [];
  112. %     opts.rho = 1.25;
  113. %     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  114. %     opts.tau_F=[tau_1 tau_1, tau_1];
  115. %     opts.tauL1=tau_2;
  116. %     tic
  117. %     Xhat_CTCTV = CTCTV_TRPCA(Xn, opts);
  118. %     times_CTCTV(i) = toc;
  119. %     [psnrs_CTCTV(i), ssims_CTCTV(i), fsims_CTCTV(i), ergas_CTCTV(i), sams_CTCTV(i)] = HSI_QA(X * 255, Xhat_CTCTV * 255);
  120. %     Xhat{methods_index}=Xhat_CTCTV;
  121. %     div{methods_index}=1-abs((X-Xhat_CTCTV));
  122. %     methods_index = methods_index+1;
  123. %
  124. %     %% Method 5: WTCTV  Enhanced Welsch
  125. %     disp('......Run WTCTV......')
  126. %     opts = [];
  127. %     opts.rho = 1.25;
  128. %     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  129. %     opts.tau_F=[tau_1 tau_1, tau_1];
  130. %     opts.tauL1=tau_2;
  131. %     tic
  132. %     Xhat_WTCTV = WTCTV_TRPCA(Xn, opts);
  133. %     times_WTCTV(i) = toc;
  134. %     [psnrs_WTCTV(i), ssims_WTCTV(i), fsims_WTCTV(i), ergas_WTCTV(i), sams_WTCTV(i)] = HSI_QA(X * 255, Xhat_WTCTV * 255);
  135. %     Xhat{methods_index}=Xhat_WTCTV;
  136. %     div{methods_index}=1-abs((X-Xhat_WTCTV));
  137. %     methods_index = methods_index+1;
  138.     %% Method 6: GTCTV  Enhanced General loss
  139.     disp('......Run GTCTV......')
  140.     opts = [];
  141.     opts.rho = 1.25;
  142.     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
  143.     opts.tau_F=[tau_1 tau_1, tau_1];
  144.     opts.tauL1=tau_2;
  145.     General_option = 2;
  146.     % 2 for Enhanced Geman-McClure loss
  147.     % 1 for Enhanced Cauchy loss
  148.     tic
  149.     Xhat_GTCTV = GTCTV_TRPCA(Xn, opts ,General_option);
  150.     times_GTCTV(i) = toc;
  151.     [psnrs_GTCTV(i), ssims_GTCTV(i), fsims_GTCTV(i), ergas_GTCTV(i), sams_GTCTV(i)] = HSI_QA(X * 255, Xhat_GTCTV * 255);
  152.     Xhat{methods_index}=Xhat_GTCTV;
  153.     div{methods_index}=1-abs((X-Xhat_GTCTV));
  154.     methods_index = methods_index+1;
  155. %     fprintf('\n %d-th image, TCTV   psnr=%.4f, ssim=%.4f, fsim=%.4f\n',i, psnrs_TCTV(i),ssims_TCTV(i),fsims_TCTV(i));
  156. %     fprintf('\n              FTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_FTCTV(i),ssims_FTCTV(i),fsims_FTCTV(i));
  157. %     fprintf('\n              HTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_HTCTV(i),ssims_HTCTV(i),fsims_HTCTV(i));
  158. %     fprintf('\n              CTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_CTCTV(i),ssims_CTCTV(i),fsims_CTCTV(i));
  159. %     fprintf('\n              WTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_WTCTV(i),ssims_WTCTV(i),fsims_WTCTV(i));
  160. %     fprintf('\n              GTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_GTCTV(i),ssims_GTCTV(i),fsims_GTCTV(i));
  161.    
  162.     psnrs_i=[psnrs_TCTV(i),psnrs_FTCTV(i),psnrs_HTCTV(i),psnrs_CTCTV(i),psnrs_WTCTV(i),psnrs_GTCTV(i)];
  163.     ssims_i=[ssims_TCTV(i),ssims_FTCTV(i),ssims_HTCTV(i),ssims_CTCTV(i),ssims_WTCTV(i),ssims_GTCTV(i)];  
  164.     ergas_i=[ergas_TCTV(i),ergas_FTCTV(i),ergas_HTCTV(i),ergas_CTCTV(i),ergas_WTCTV(i),ergas_GTCTV(i)];
  165.    
  166.     disp(['PSNR_i=       ',num2str(psnrs_i)])  
  167.     disp(['SSIM_i=       ',num2str(ssims_i)])
  168.     disp(['ERGAS_i=       ',num2str(ergas_i)])
  169.    
  170. %     Orig_Multi{i}=X;Noisy_Multi{i}=Xn;TCTV_Multi{i}=Xhat_TCTV;FTCTV_Multi{i}=Xhat_FTCTV;HTCTV_Multi{i}=Xhat_HTCTV;
  171. %     j=1;
  172. end
  173. methodName={'TCTV','FTCTV','HTCTV','CTCTV','WTCTV','GTCTV'};
  174. enList=[1,2,3,4,5,6];
  175. psnrs_all=[psnrs_TCTV,psnrs_FTCTV,psnrs_HTCTV,psnrs_CTCTV,psnrs_WTCTV,psnrs_GTCTV];
  176. psnrs_mean=mean(psnrs_all,1);
  177. ssims_all=[ssims_TCTV,ssims_FTCTV,ssims_HTCTV,ssims_CTCTV,ssims_WTCTV,ssims_GTCTV];
  178. ssims_mean=mean(ssims_all,1);
  179. fsims_all=[fsims_TCTV,fsims_FTCTV,fsims_HTCTV,fsims_CTCTV,fsims_WTCTV,fsims_GTCTV];
  180. fsims_mean=mean(fsims_all,1);
  181. ergas_all=[ergas_TCTV,ergas_FTCTV,ergas_HTCTV,ergas_CTCTV,ergas_WTCTV,ergas_GTCTV];
  182. ergas_mean=mean(ergas_all,1);
  183. times_all=[times_TCTV,times_FTCTV,times_HTCTV,times_CTCTV,times_WTCTV,times_GTCTV];
  184. times_mean=mean(times_all,1);
  185. C{iter} = {psnrs_mean, ssims_mean, fsims_mean, ergas_mean, tau_1, tau_2};
  186. epoch_time = toc(start_tic);
  187. sum_time = sum_time+epoch_time;
  188. fprintf('Progress:%d/%d. Epoch Time:%.2f. Progress Time:%.2f. Time needs:%.2f.\n',iter,sum_iter,epoch_time/60,sum_time/60,((sum_time)/iter)*(sum_iter-iter)/60);
  189. iter = iter+1;
  190. end
  191. end
  192. %% Show results
  193. showHSIResult(Xhat,X,Xn,div,methodName,enList,31,31);
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王海鱼

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表