您的位置:首页 > 房产 > 家装 > 【数据分析】通过python分析提取matlab工作区中的数据

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

2024/12/23 16:12:14 来源:https://blog.csdn.net/weixin_42932602/article/details/140161736  浏览:    关键词:【数据分析】通过python分析提取matlab工作区中的数据
#%%
import scipy.io
import pandas as pd
import numpy as np# Load the .mat file
file_path = 'clay_C.mat'
mat_data = scipy.io.loadmat(file_path)## 建立结果表格
# 定义行、列名
columns = ['0.1', '0.5', '1', '2', '3', '4', '5']
index = ['0.1', '0.5', '1', '2', '3', '4', '5']my_psnrs_mean = pd.DataFrame(np.nan,index = index, columns=columns)my_ssims_mean = pd.DataFrame(np.nan,index = index, columns=columns)my_fsims_mean = pd.DataFrame(np.nan,index = index, columns=columns)my_ergas_mean = pd.DataFrame(np.nan,index = index, columns=columns)
#%%
# 假设MAT文件中包含一个名为'C'的数据变量
data = mat_data['C']
#%%
# 将数据转换为DataFrame
df = pd.DataFrame(data)
df = df.transpose()
#%%
for i in range(len(df)):row = df.iloc[i][0]my_df = pd.DataFrame(row)psnrs_mean = my_df.iloc[0,0][0][-1]ssims_mean = my_df.iloc[0,1][0][-1]fsims_mean = my_df.iloc[0,2][0][-1]ergas_mean = my_df.iloc[0,3][0][-1]tau_1 = my_df.iloc[0,4][0][-1]tau_2 = my_df.iloc[0,5][0][-1]my_psnrs_mean.loc[str(tau_1),str(tau_2)] = psnrs_meanmy_ssims_mean.loc[str(tau_1),str(tau_2)] = ssims_meanmy_fsims_mean.loc[str(tau_1),str(tau_2)] = fsims_meanmy_ergas_mean.loc[str(tau_1),str(tau_2)] = ergas_mean
#%%
my_psnrs_mean*10
#%%
my_ssims_mean*10
#%%
my_fsims_mean*10
#%%
my_ergas_mean*10

代码详细分析

这段代码从MAT文件中读取数据,并将其转换为多个Pandas DataFrame,以便进行后续的分析和处理。以下是代码的详细分析:

1. 导入库和读取MAT文件

import scipy.io
import pandas as pd
import numpy as np# Load the .mat file
file_path = 'clay_C.mat'
mat_data = scipy.io.loadmat(file_path)

这里使用了scipy.io.loadmat函数来读取MAT文件,并将其存储在变量mat_data中。

2. 建立结果表格

# 定义行、列名
columns = ['0.1', '0.5', '1', '2', '3', '4', '5']
index = ['0.1', '0.5', '1', '2', '3', '4', '5']my_psnrs_mean = pd.DataFrame(np.nan,index = index, columns=columns)
my_ssims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
my_fsims_mean = pd.DataFrame(np.nan,index = index, columns=columns)
my_ergas_mean = pd.DataFrame(np.nan,index = index, columns=columns)

这里创建了四个空的DataFrame来存储结果,每个DataFrame的行和列都是['0.1', '0.5', '1', '2', '3', '4', '5']

3. 提取数据并转换为DataFrame

# 假设MAT文件中包含一个名为'C'的数据变量
data = mat_data['C']# 将数据转换为DataFrame
df = pd.DataFrame(data)
df = df.transpose()

从MAT文件中提取名为'C'的数据变量,将其转换为Pandas DataFrame,并对其进行转置。

4. 迭代每一行并提取指标

for i in range(len(df)):row = df.iloc[i][0]my_df = pd.DataFrame(row)psnrs_mean = my_df.iloc[0,0][0][-1]ssims_mean = my_df.iloc[0,1][0][-1]fsims_mean = my_df.iloc[0,2][0][-1]ergas_mean = my_df.iloc[0,3][0][-1]tau_1 = my_df.iloc[0,4][0][-1]tau_2 = my_df.iloc[0,5][0][-1]my_psnrs_mean.loc[str(tau_1),str(tau_2)] = psnrs_meanmy_ssims_mean.loc[str(tau_1),str(tau_2)] = ssims_meanmy_fsims_mean.loc[str(tau_1),str(tau_2)] = fsims_meanmy_ergas_mean.loc[str(tau_1),str(tau_2)] = ergas_mean

遍历DataFrame的每一行,并提取每行的各项指标值。具体操作如下:

  • 将每一行的第一个元素转换为新的DataFrame my_df
  • 提取 psnrs_meanssims_meanfsims_meanergas_meantau_1tau_2 值。
  • 将这些值填入相应的结果DataFrame中,使用tau_1tau_2作为索引。

5. 打印结果

my_psnrs_mean*10
my_ssims_mean*10
my_fsims_mean*10
my_ergas_mean*10

这里将每个结果DataFrame中的值乘以10并显示出来。

总结

该代码的主要目的是从MAT文件中读取数据,并将其中包含的各种性能指标提取出来,存储到多个Pandas DataFrame中。每个结果DataFrame的行和列是tau_1tau_2的组合,存储对应的psnrs_meanssims_meanfsims_meanergas_mean值。

原代码

%SpaTRPCA_BSDtest200_5method_Corr10
clear;clc;close all
addpath(genpath('lib'));
addpath(genpath('data'));
addpath(genpath('competing methods'));rhos = 0.1; %Percent of corrupted pixels% %% Options settings
% opts.mu = 1e-2;
% opts.tol = 1e-6;
% opts.rho = 1.1;
% opts.max_iter = 500;0
% opts.DEBUG = 0;
% opts.tau_F= 2;method='TCTV';datadir = 'data/CAVE_USED';
seqs = dir(datadir);
seq3 = seqs(3:end); 
n_img=length(seq3);temp=zeros(n_img,1);psnrs_TCTV=temp;psnrs_FTCTV=temp;psnrs_HTCTV=temp; psnrs_CTCTV=temp; psnrs_WTCTV=temp; psnrs_GTCTV=temp; ssims_TCTV=temp;ssims_FTCTV=temp;ssims_HTCTV=temp; ssims_CTCTV=temp; ssims_WTCTV=temp; ssims_GTCTV=temp; fsims_TCTV=temp;fsims_FTCTV=temp;fsims_HTCTV=temp; fsims_CTCTV=temp; fsims_WTCTV=temp; fsims_GTCTV=temp; ergas_TCTV=temp;ergas_FTCTV=temp;ergas_HTCTV=temp; ergas_CTCTV=temp; ergas_WTCTV=temp; ergas_GTCTV=temp; sams_TCTV=temp;sams_FTCTV=temp;sams_HTCTV=temp; sams_CTCTV=temp; sams_WTCTV=temp; sams_GTCTV=temp; times_TCTV=temp;times_FTCTV=temp;times_HTCTV=temp; times_CTCTV=temp; times_WTCTV=temp; times_GTCTV=temp; Temp=cell(length(seq3),1);
Orig_Multi=Temp;Noisy_Multi=Temp;TCTV_Multi=Temp;FTCTV_Multi=Temp;HTCTV_Multi=Temp;CTCTV_Multi=Temp;WTCTV_Multi=Temp;GTCTV_Multi=Temp;C = cell(1,20);
iter=1;
sum_iter = 50;
sum_time = 0;
% for tau_1 = [1,2,3,4,5,6,7,8,9,10]
% for tau_1 = [0.1,0.2,0.3,0.4,0.5]
for tau_1 = [0.1, 0.5, 1, 2, 3, 4, 5 ]
% for tau_2 = [1,2,3,4,5,6,7,8,9,10]
% for tau_2 = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
for tau_2 = [0.1, 0.5, 1, 2, 3, 4, 5]
start_tic = tic;
%% Perform recovery
for i = 5% 1 for balloons  2 for beads% 3 for cd        4 for chart% 5 for clay      6 for cloth% 7 for egyptian  8 for feathers% 9 for flowers   10 for glass%% load datafname = seq3(i).name;pic_TNN_Weight_Lame = [datadir '/' fname];load(fname)%% 0-1 Centralizationdata_min = min(X(:));data_max = max(X(:));X = (X - data_min) / (data_max - data_min);%% add noise
%     [n1,n2,n3] = size(X);
%     Xn = X; Yn=reshape(Xn,[n1*n2,n3]);
%     ind = find(rand(n1*n2,1)<rhos);
%     Yn(ind,:)=rand(length(ind),n3);
%     Xn=reshape(Yn,[n1,n2,n3]);[height, width, band] = size(X);for ii = 1:bandXn(:,:,ii) = imnoise(X(:,:,ii), 'salt & pepper', rhos);endmethods_index = 1;%% Method 1: TCTV
%     disp('......Run TCTV......')
%     opts = [];
%     opts.rho = 1.25;
%     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
%     tic
%     Xhat_TCTV = TCTV_TRPCA(Xn, opts);
%     times_TCTV(i) = toc;
%     [psnrs_TCTV(i), ssims_TCTV(i), fsims_TCTV(i), ergas_TCTV(i), sams_TCTV(i)] = HSI_QA(X * 255, Xhat_TCTV* 255);
% %     TCTV_psnr = psnr(X , Xhat_TCTV);
%     Xhat{methods_index}=Xhat_TCTV;
%     div{methods_index}=1-abs((X-Xhat_TCTV));
%     methods_index = methods_index+1;%     %% Method 2: FTCTV  Enhanced Fair 
%     disp('......Run FTCTV......')
%     opts = [];
%     opts.rho = 1.25;
%     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
%     opts.tau_F=[tau_1 tau_1, tau_1];
%     opts.tauL1=tau_2;
%     tic
%     Xhat_FTCTV = FTCTV_TRPCA(Xn, opts);
%     times_FTCTV(i) = toc;
%     [psnrs_FTCTV(i), ssims_FTCTV(i), fsims_FTCTV(i), ergas_FTCTV(i), sams_FTCTV(i)] = HSI_QA(X * 255, Xhat_FTCTV * 255);
%     Xhat{methods_index}=Xhat_FTCTV;
%     div{methods_index}=1-abs((X-Xhat_FTCTV));
%     methods_index = methods_index+1;
% 
%     %% Method 3: HTCTV  Enhanced Huber
%     disp('......Run HTCTV......')
%     opts = [];
%     opts.rho = 1.25;
%     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
%     opts.tau_F=[tau_1 tau_1, tau_1];
%     opts.tauL1=tau_2;
%     tic
%     Xhat_HTCTV = HTCTV_TRPCA(Xn, opts);
%     times_HTCTV(i) = toc;
%     [psnrs_HTCTV(i), ssims_HTCTV(i), fsims_HTCTV(i), ergas_HTCTV(i), sams_HTCTV(i)] = HSI_QA(X * 255, Xhat_HTCTV * 255);
%     Xhat{methods_index}=Xhat_HTCTV;
%     div{methods_index}=1-abs((X-Xhat_HTCTV));
%     methods_index = methods_index+1;
% 
%     %% Method 4: CTCTV  Enhanced Cauchy
%     disp('......Run CTCTV......')
%     opts = [];
%     opts.rho = 1.25;
%     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
%     opts.tau_F=[tau_1 tau_1, tau_1];
%     opts.tauL1=tau_2;
%     tic
%     Xhat_CTCTV = CTCTV_TRPCA(Xn, opts);
%     times_CTCTV(i) = toc;
%     [psnrs_CTCTV(i), ssims_CTCTV(i), fsims_CTCTV(i), ergas_CTCTV(i), sams_CTCTV(i)] = HSI_QA(X * 255, Xhat_CTCTV * 255);
%     Xhat{methods_index}=Xhat_CTCTV;
%     div{methods_index}=1-abs((X-Xhat_CTCTV));
%     methods_index = methods_index+1;
% 
%     %% Method 5: WTCTV  Enhanced Welsch
%     disp('......Run WTCTV......')
%     opts = [];
%     opts.rho = 1.25;
%     opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directions
%     opts.tau_F=[tau_1 tau_1, tau_1];
%     opts.tauL1=tau_2;
%     tic
%     Xhat_WTCTV = WTCTV_TRPCA(Xn, opts);
%     times_WTCTV(i) = toc;
%     [psnrs_WTCTV(i), ssims_WTCTV(i), fsims_WTCTV(i), ergas_WTCTV(i), sams_WTCTV(i)] = HSI_QA(X * 255, Xhat_WTCTV * 255);
%     Xhat{methods_index}=Xhat_WTCTV;
%     div{methods_index}=1-abs((X-Xhat_WTCTV));
%     methods_index = methods_index+1;%% Method 6: GTCTV  Enhanced General lossdisp('......Run GTCTV......')opts = [];opts.rho = 1.25;opts.directions = [1,2,3]; % consider the lowrankness and smoothness both along the spatial and spectral directionsopts.tau_F=[tau_1 tau_1, tau_1];opts.tauL1=tau_2;General_option = 2;% 2 for Enhanced Geman-McClure loss% 1 for Enhanced Cauchy lossticXhat_GTCTV = GTCTV_TRPCA(Xn, opts ,General_option);times_GTCTV(i) = toc;[psnrs_GTCTV(i), ssims_GTCTV(i), fsims_GTCTV(i), ergas_GTCTV(i), sams_GTCTV(i)] = HSI_QA(X * 255, Xhat_GTCTV * 255);Xhat{methods_index}=Xhat_GTCTV;div{methods_index}=1-abs((X-Xhat_GTCTV));methods_index = methods_index+1;%     fprintf('\n %d-th image, TCTV   psnr=%.4f, ssim=%.4f, fsim=%.4f\n',i, psnrs_TCTV(i),ssims_TCTV(i),fsims_TCTV(i));
%     fprintf('\n              FTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_FTCTV(i),ssims_FTCTV(i),fsims_FTCTV(i));
%     fprintf('\n              HTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_HTCTV(i),ssims_HTCTV(i),fsims_HTCTV(i));
%     fprintf('\n              CTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_CTCTV(i),ssims_CTCTV(i),fsims_CTCTV(i));
%     fprintf('\n              WTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_WTCTV(i),ssims_WTCTV(i),fsims_WTCTV(i));
%     fprintf('\n              GTCTV  psnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_GTCTV(i),ssims_GTCTV(i),fsims_GTCTV(i));psnrs_i=[psnrs_TCTV(i),psnrs_FTCTV(i),psnrs_HTCTV(i),psnrs_CTCTV(i),psnrs_WTCTV(i),psnrs_GTCTV(i)];ssims_i=[ssims_TCTV(i),ssims_FTCTV(i),ssims_HTCTV(i),ssims_CTCTV(i),ssims_WTCTV(i),ssims_GTCTV(i)];  ergas_i=[ergas_TCTV(i),ergas_FTCTV(i),ergas_HTCTV(i),ergas_CTCTV(i),ergas_WTCTV(i),ergas_GTCTV(i)]; disp(['PSNR_i=       ',num2str(psnrs_i)])  disp(['SSIM_i=       ',num2str(ssims_i)])disp(['ERGAS_i=       ',num2str(ergas_i)])%     Orig_Multi{i}=X;Noisy_Multi{i}=Xn;TCTV_Multi{i}=Xhat_TCTV;FTCTV_Multi{i}=Xhat_FTCTV;HTCTV_Multi{i}=Xhat_HTCTV;
%     j=1;
end
methodName={'TCTV','FTCTV','HTCTV','CTCTV','WTCTV','GTCTV'};
enList=[1,2,3,4,5,6];
psnrs_all=[psnrs_TCTV,psnrs_FTCTV,psnrs_HTCTV,psnrs_CTCTV,psnrs_WTCTV,psnrs_GTCTV]; 
psnrs_mean=mean(psnrs_all,1);
ssims_all=[ssims_TCTV,ssims_FTCTV,ssims_HTCTV,ssims_CTCTV,ssims_WTCTV,ssims_GTCTV];
ssims_mean=mean(ssims_all,1);
fsims_all=[fsims_TCTV,fsims_FTCTV,fsims_HTCTV,fsims_CTCTV,fsims_WTCTV,fsims_GTCTV]; 
fsims_mean=mean(fsims_all,1);
ergas_all=[ergas_TCTV,ergas_FTCTV,ergas_HTCTV,ergas_CTCTV,ergas_WTCTV,ergas_GTCTV]; 
ergas_mean=mean(ergas_all,1);
times_all=[times_TCTV,times_FTCTV,times_HTCTV,times_CTCTV,times_WTCTV,times_GTCTV]; 
times_mean=mean(times_all,1);C{iter} = {psnrs_mean, ssims_mean, fsims_mean, ergas_mean, tau_1, tau_2};
epoch_time = toc(start_tic);
sum_time = sum_time+epoch_time;
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);
iter = iter+1;
end
end%% Show results 
showHSIResult(Xhat,X,Xn,div,methodName,enList,31,31);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com