Skip to content

Commit cd4c9e8

Browse files
committed
add NMF of data table
1 parent 3189c74 commit cd4c9e8

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
classdef Nmf < imagem.gui.Action
2+
% Non-negative matrix factorization of current table.
3+
%
4+
% Class Nmf
5+
%
6+
% Example
7+
% Nmf
8+
%
9+
% See also
10+
%
11+
%
12+
% ------
13+
% Author: David Legland
14+
15+
% Created: 2021-04-09, using Matlab 7.9.0.529 (R2009b)
16+
% Copyright 2012 INRA - Cepia Software Platform.
17+
18+
19+
%% Properties
20+
properties
21+
end % end properties
22+
23+
24+
%% Constructor
25+
methods
26+
function obj = Nmf()
27+
% Constructor for TablePcaAction class
28+
29+
end
30+
31+
end % end constructors
32+
33+
34+
%% Methods
35+
methods
36+
function run(obj, frame) %#ok<INUSL>
37+
38+
if isempty(frame.Doc)
39+
return;
40+
end
41+
table = frame.Doc.Table;
42+
43+
% default number of components
44+
nComps = min(5, size(table, 2));
45+
46+
% open a dialog to choose number of components
47+
prompt = {'Number of components:'};
48+
title = 'Non-Negative Matrix Factorization';
49+
dims = [1 35];
50+
definput = {num2str(nComps)};
51+
answer = inputdlg(prompt, title, dims, definput);
52+
53+
% check user did not cancel
54+
if isempty(answer)
55+
return;
56+
end
57+
58+
% parse user input
59+
nComps = str2double(answer{1});
60+
if isnan(nComps)
61+
error('Could not interpret the number of components from string: %s', answer{1});
62+
end
63+
64+
[W, H] = nmf(table, nComps);
65+
66+
% create a new frame for weights
67+
[frameW, docW] = createTableFrame(frame.Gui, W); %#ok<ASGLU>
68+
69+
% create a new frame for coefficients
70+
[frameH, docH] = createTableFrame(frame.Gui, H); %#ok<ASGLU>
71+
72+
73+
% update history
74+
historyString = sprintf('[%s, %s] = nmf(%s, %d);\n', ...
75+
docW.Tag, docH.Tag, doc.Tag, nComps);
76+
addToHistory(frame, historyString);
77+
78+
end
79+
end % end methods
80+
81+
end % end classdef
82+

ImageM/+imagem/+gui/FrameMenuBuilder.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ function buildTableFrameMenu(obj, hf)
402402

403403
% Process menu
404404
processMenu = addMenu(obj, hf, 'Process');
405-
addMenuItem(obj, processMenu, imagem.actions.table.pca.Pca(), 'PCA...');
405+
addMenuItem(obj, processMenu, imagem.actions.table.pca.Pca(), 'Principal Components Analysis (PCA)...');
406+
addMenuItem(obj, processMenu, imagem.actions.table.process.Nmf(), 'Non-Negative Matrix Factorization (NMF)...');
406407
addMenuItem(obj, processMenu, imagem.actions.table.TableKMeans(), 'K-Means...', 'Separator', 'On');
407408

408409

0 commit comments

Comments
 (0)