王海鱼 发表于 2024-7-10 22:52:59

【数据分析】通过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
   
    my_df = pd.DataFrame(row)
   
    psnrs_mean = my_df.iloc[-1]
    ssims_mean = my_df.iloc[-1]
    fsims_mean = my_df.iloc[-1]
    ergas_mean = my_df.iloc[-1]
    tau_1 = my_df.iloc[-1]
    tau_2 = my_df.iloc[-1]
   
    my_psnrs_mean.loc = psnrs_mean
   
    my_ssims_mean.loc = ssims_mean
   
    my_fsims_mean.loc = fsims_mean
   
    my_ergas_mean.loc = 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
   
    my_df = pd.DataFrame(row)
   
    psnrs_mean = my_df.iloc[-1]
    ssims_mean = my_df.iloc[-1]
    fsims_mean = my_df.iloc[-1]
    ergas_mean = my_df.iloc[-1]
    tau_1 = my_df.iloc[-1]
    tau_2 = my_df.iloc[-1]
   
    my_psnrs_mean.loc = psnrs_mean
    my_ssims_mean.loc = ssims_mean
    my_fsims_mean.loc = fsims_mean
    my_ergas_mean.loc = ergas_mean
遍历DataFrame的每一行,并提取每行的各项指标值。具体操作如下:


[*]将每一行的第一个元素转换为新的DataFrame my_df。
[*]提取 psnrs_mean、ssims_mean、fsims_mean、ergas_mean、tau_1 和 tau_2 值。
[*]将这些值填入相应的结果DataFrame中,使用tau_1和tau_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_1和tau_2的组合,存储对应的psnrs_mean、ssims_mean、fsims_mean和ergas_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 =
% for tau_1 =
for tau_1 =
% for tau_2 =
% for tau_2 =
for tau_2 =
start_tic = tic;
%% Perform recovery
for i = 5
    % 1 for balloons2 for beads
    % 3 for cd      4 for chart
    % 5 for clay      6 for cloth
    % 7 for egyptian8 for feathers
    % 9 for flowers   10 for glass
    %% load data
    fname = seq3(i).name;
    pic_TNN_Weight_Lame = ;
    load(fname)
    %% 0-1 Centralization
    data_min = min(X(:));
    data_max = max(X(:));
    X = (X - data_min) / (data_max - data_min);
    %% add noise
%    = size(X);
%   Xn = X; Yn=reshape(Xn,);
%   ind = find(rand(n1*n2,1)<rhos);
%   Yn(ind,:)=rand(length(ind),n3);
%   Xn=reshape(Yn,);
    = size(X);
    for ii = 1:band
      Xn(:,:,ii) = imnoise(X(:,:,ii), 'salt & pepper', rhos);
    end
    methods_index = 1;

    %% Method 1: TCTV
%   disp('......Run TCTV......')
%   opts = [];
%   opts.rho = 1.25;
%   opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
%   tic
%   Xhat_TCTV = TCTV_TRPCA(Xn, opts);
%   times_TCTV(i) = toc;
%    = 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: FTCTVEnhanced Fair
%   disp('......Run FTCTV......')
%   opts = [];
%   opts.rho = 1.25;
%   opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
%   opts.tau_F=;
%   opts.tauL1=tau_2;
%   tic
%   Xhat_FTCTV = FTCTV_TRPCA(Xn, opts);
%   times_FTCTV(i) = toc;
%    = 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: HTCTVEnhanced Huber
%   disp('......Run HTCTV......')
%   opts = [];
%   opts.rho = 1.25;
%   opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
%   opts.tau_F=;
%   opts.tauL1=tau_2;
%   tic
%   Xhat_HTCTV = HTCTV_TRPCA(Xn, opts);
%   times_HTCTV(i) = toc;
%    = 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: CTCTVEnhanced Cauchy
%   disp('......Run CTCTV......')
%   opts = [];
%   opts.rho = 1.25;
%   opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
%   opts.tau_F=;
%   opts.tauL1=tau_2;
%   tic
%   Xhat_CTCTV = CTCTV_TRPCA(Xn, opts);
%   times_CTCTV(i) = toc;
%    = 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: WTCTVEnhanced Welsch
%   disp('......Run WTCTV......')
%   opts = [];
%   opts.rho = 1.25;
%   opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
%   opts.tau_F=;
%   opts.tauL1=tau_2;
%   tic
%   Xhat_WTCTV = WTCTV_TRPCA(Xn, opts);
%   times_WTCTV(i) = toc;
%    = 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: GTCTVEnhanced General loss
    disp('......Run GTCTV......')
    opts = [];
    opts.rho = 1.25;
    opts.directions = ; % consider the lowrankness and smoothness both along the spatial and spectral directions
    opts.tau_F=;
    opts.tauL1=tau_2;
    General_option = 2;
    % 2 for Enhanced Geman-McClure loss
    % 1 for Enhanced Cauchy loss
    tic
    Xhat_GTCTV = GTCTV_TRPCA(Xn, opts ,General_option);
    times_GTCTV(i) = toc;
    = 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            FTCTVpsnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_FTCTV(i),ssims_FTCTV(i),fsims_FTCTV(i));
%   fprintf('\n            HTCTVpsnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_HTCTV(i),ssims_HTCTV(i),fsims_HTCTV(i));
%   fprintf('\n            CTCTVpsnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_CTCTV(i),ssims_CTCTV(i),fsims_CTCTV(i));
%   fprintf('\n            WTCTVpsnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_WTCTV(i),ssims_WTCTV(i),fsims_WTCTV(i));
%   fprintf('\n            GTCTVpsnr=%.4f, ssim=%.4f, fsim=%.4f\n',   psnrs_GTCTV(i),ssims_GTCTV(i),fsims_GTCTV(i));
   
    psnrs_i=;
    ssims_i=;
    ergas_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=;
psnrs_all=;
psnrs_mean=mean(psnrs_all,1);
ssims_all=;
ssims_mean=mean(ssims_all,1);
fsims_all=;
fsims_mean=mean(fsims_all,1);
ergas_all=;
ergas_mean=mean(ergas_all,1);
times_all=;
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);

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【数据分析】通过python分析提取matlab工作区中的数据