-
Notifications
You must be signed in to change notification settings - Fork 0
/
KlustaKwik_java.m
88 lines (79 loc) · 3.2 KB
/
KlustaKwik_java.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function idx = KlustaKwik_java(features, args, KlustaKwik_jarfile)
%KLUSTAKWIK KlustaKwik wrapper
% idx = KLUSTAKWIK(features) runs the KlustaKwik utility to sort a matrix
% of features and returns clustering results. This wrapper performs the
% following steps:
%
% 1. Writes features to a temporary file (file.fet.1)
% 2. Runs the java system command.
% 3. Reads the temporarly cluster file (file.clus.1)
% 4. deletes temporary files
%
% Running this script requires KlustaKwik.jar. If the jar file is not
% located in the Matlab Path, you can provide the directory (or full path
% filename) of the jarfile containing the KlustaKwik code.
%
% Usage: idx = KlustaKwik(features)
%
% Input: features * WxD feature matrix; W observations, D
% variables (dimensions)
% args * (optional) argument list for KlustaKwik
% KlustaKwik_jarfile * (optional) location of KlustaKwik.jar
% Output: idx * Wx1 index matrix
%
% Example: idx = KlustaKwik(x,'-MinClusters 2');
%
% Written by Marshall Crumiller
% email: [email protected]
%
% Updates
% 2014-05-23: Created
%--------------------------------------------------------------------------
% determine whether java is installed
% determine existence of KlustaKwik.jar
jarfile = 'KlustaKwik.jar';
if(exist('KlustaKwik_jarfile','var') && ~isempty(KlustaKwik_jarfile))
expr = regexp(KlustaKwik_jarfile,'(?<basedir>.*[\\/])*(?<jarfile>.*\.jar)*','names');
if(isempty(expr)), error('Invalid filename.'); end
basedir = expr.basedir;
if(~isempty(expr.jarfile)), jarfile = expr.jarfile; end
jarfile = sprintf('%s%s',basedir,jarfile);
end
if(~exist(jarfile,'file'))
error('Cannot locate KlustaKwik.jar. Try providing it as a second argument.');
end
jarfile=which(jarfile);
% Step 1: Make Feature file
% create temp name. Note: must be in current directory
base_name=tempname(pwd);
feature_file = sprintf('%s.fet.1', base_name);
f = fopen(feature_file,'w');
fprintf(f,'%g\n',size(features,2)); fclose(f);
%precision=ceil(log10(max(max(max(double(features))))));
precision = 5;
dlmwrite(feature_file,features,'-append','newline','unix','precision',precision,'delimiter','\t');
% Step 2: run KlustaKwik
% Determine KlustaKwik command
% grab argument list if user provided
arglist = '-MinClusters 2';
if(exist('args','var') && ~isempty(args))
arglist = sprintf('%s ',args);
end
% Execute command
%LOGFILE=false; % enable this if you want to generate a log file
%if(LOGFILE), logfile = sprintf('Klustakwik-%s.txt',strrep(strrep(datestr(now),' ','-'),':','.'));
%else logfile = 'NUL'; end
logfile='NUL';
cmd = sprintf('java -jar %s -ElecNo 1 -FileBase "%s" -UseFeatures ALL %s > %s',jarfile, base_name, arglist, logfile);
%cmd = sprintf('KlustaKwik "%s" 1 -MinClusters 2 -MaxClusters 6 > %s', base_name, logfile);
system(cmd);
% Step 3: read Clusters file
cluster_file = sprintf('%s.clu.1',base_name);
f_clus = fopen(cluster_file);
if(f_clus==-1), error('Cannot open cluster file.'); end
idx=fscanf(f_clus,'%g')';
idx=idx(2:end); % ignore first line
fclose(f_clus);
% Step 4: delete clusters file and feature file
delete(cluster_file);
delete(feature_file);