⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brainnmf.m

📁 非负矩阵分解的matlab代码
💻 M
📖 第 1 页 / 共 2 页
字号:

img = handles.img;
time = handles.time;
xAxisLabel = handles.xAxisLabel;
[x y slice frame] = size(img);
meanImg = reshape(mean(img,4),x*y,slice);
thresshold(1) = str2double(get(handles.lowerThress,'String'));
thresshold(2) = str2double(get(handles.upperThress,'String'));
indx = find(meanImg > thresshold(1) & meanImg < thresshold(2));
img = reshape(img,x*y*slice,frame);
img = img(indx,:);
K = str2double(get(handles.set_sources,'String'));
iterations = str2double(get(handles.set_iterations,'String'));%Not used yet

switch get(handles.choose_alg,'Value')
    case {1}
        alg = 'mm';
    case {2}
        alg = 'prob';
    case {3}
        alg = 'cjlin';
    case {4}
        alg = 'als';
    case {5}
        alg = 'alsobs';
end

[W H] = nmf(abs(img),K,alg,iterations,0);
if (get(handles.rescale,'Value') == get(handles.rescale,'Max'));
    [W H] = rescale(W,H);
    fprintf('Rescaled \n')
end
handles.rescaledFlag = get(handles.rescale,'Value');
figure
plot(time,H'), xlabel(xAxisLabel), ylabel('Activity')

handles.indx = indx;%Save indx's for plotting
handles.H = H;
handles.W = W;

set(handles.plot_spatial,'Enable', 'on');
set(handles.plot_error,'Enable', 'on');

guidata(hObject, handles);%save structure

% --- Executes on button press in plot_error.
function plot_error_Callback(hObject, eventdata, handles)
% hObject    handle to plot_error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'H')
    errordlg('Run NMF first','Error');
    return
end
if isfield(handles,'plotErr')
    err = calculatePlotError(handles,1,0);    
else
    err = calculatePlotError(handles,1,1);
    handles.plotErr = err;
end

% function calculates the plot error. If flag plot is set to zero, the
% error plot is not shown. For other positive number, it will be also
% plotted out.
function err = calculatePlotError(handles,plot,calc)

indx = handles.indx;
H = handles.H;
W = handles.W;
x = handles.imgSize(1);
y = handles.imgSize(2);
slice = handles.imgSize(3);
frame = handles.imgSize(4);

if(calc == 0)
    if(plot)
        plotError(handles.plotErr,indx,[x y slice frame]);   
    end
    err = handles.plotErr;
    return
end
img = handles.img;

img = reshape(img,x*y*slice,frame);
img = img(indx,:);%2D with only activ voxels

err = abs(mean(img-W*H,2));
size(err)
if(plot)
    plotError(err,indx,[x y slice frame]);
end

% --- Executes on button press in save_results.
function save_results_Callback(hObject, eventdata, handles)
% hObject    handle to save_results (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'H')
    errordlg('Run NMF first','Error');
    return
end
W = handles.W;
H = handles.H;
time = handles.time;
scanName = handles.scanName;

thresshold(1) = str2double(get(handles.lowerThress,'String'));
thresshold(2) = str2double(get(handles.upperThress,'String'));
indx = handles.indx;
iterations = str2double(get(handles.set_iterations,'String'));
imgSize = handles.imgSize;
rescaledFlag = handles.rescaledFlag;

if isfield(handles,'plotErr')
    plotErr = calculatePlotError(handles,0,0);    
else
    plotErr = calculatePlotError(handles,0,1);    
end

[file,path] = uiputfile('*.mat','Save Results As');

if file~=0
save([path file],'W','H','thresshold','indx','iterations','time','scanName','imgSize','plotErr','rescaledFlag');
end


function lowerThress_Callback(hObject, eventdata, handles)
% hObject    handle to lowerThress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of lowerThress as text
%        str2double(get(hObject,'String')) returns contents of lowerThress as a double

if ~isfield(handles, 'img')
    errordlg('Load file first','Error');
    return
end

value = str2double(get(hObject,'String'));
if value<str2double(get(handles.minMean,'String')) || isnan(value) || value>=str2double(get(handles.upperThress,'String'));
    errordlg('Not valid number','Error');
    set(handles.lowerThress,'String',get(handles.minMean,'String'));
    return
end


% --- Executes during object creation, after setting all properties.
function lowerThress_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lowerThress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function upperThress_Callback(hObject, eventdata, handles)
% hObject    handle to upperThress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of upperThress as text
%        str2double(get(hObject,'String')) returns contents of upperThress as a double

if ~isfield(handles, 'img')
    errordlg('Load file first','Error');
    return
end

value = str2double(get(hObject,'String'));
if value>str2double(get(handles.maxMean,'String')) || isnan(value) || value<=str2double(get(handles.lowerThress,'String'));
    errordlg('Not valid number','Error');
    set(handles.upperThress,'String',get(handles.maxMean,'String'));
    return
end

% --- Executes during object creation, after setting all properties.
function upperThress_CreateFcn(hObject, eventdata, handles)
% hObject    handle to upperThress (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in choose_alg.
function choose_alg_Callback(hObject, eventdata, handles)
% hObject    handle to choose_alg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns choose_alg contents as cell array
%        contents{get(hObject,'Value')} returns selected item from choose_alg


% --- Executes during object creation, after setting all properties.
function choose_alg_CreateFcn(hObject, eventdata, handles)
% hObject    handle to choose_alg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in loadResults.
function loadResults_Callback(hObject, eventdata, handles)
% hObject    handle to loadResults (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[fn2,pn2]=uigetfile('*.mat','Load results');
if(fn2~=0)
    if isfield(handles, 'img')
        handles = rmfield(handles, 'img');
    end
    try
    load([pn2 fn2]);
    handles.W = W;
    size(handles.W)
    handles.H = H;
    handles.indx = indx;
    set(handles.set_iterations,'String', num2str(iterations));
    set(handles.set_sources,'String',num2str(size(handles.H,1)));
    set(handles.lowerThress,'String',num2str(thresshold(1)));
    set(handles.upperThress,'String',num2str(thresshold(2)));
    handles.time = time;
    handles.scanName = scanName;
    handles.imgSize = imgSize;
    handles.plotErr = plotErr;
    handles.rescaledFlag = rescaledFlag;
    set(handles.rescale, 'Value', handles.rescaledFlag);
    
    set(handles.info_text, 'String', 'ONLY RESULTS LOADED for image:');
    set(handles.Choose_file, 'String', handles.scanName);
    
    set(handles.plot_samples,'Enable', 'off');
    set(handles.plot_thresshold,'Enable', 'off');
    set(handles.run_nmf,'Enable', 'off');
    set(handles.plot_spatial,'Enable', 'on');
    set(handles.plot_error,'Enable', 'on');
    
    guidata(hObject, handles);%save structure
       
    catch
        errordlg('Incorrect format of the MAT file!','Error');
        return
    end
end
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -