-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakeUMAPFigures.m
127 lines (92 loc) · 3.65 KB
/
MakeUMAPFigures.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
%% MAKEUMAPFIGURES.m
% This script generates the UMAP figures from
% "The manifold structure of limb coordination in walking Drosophila"
% Set data paths
SetDataPaths;
% Set minimum forward speed for yaw extremum detection
vfMin = 0.5;
% Define cache file paths
umapdatapath = fullfile(basepath, 'umap_data.h5');
umapresultpath = fullfile(basepath, 'umap_result.h5');
%% Load data from file and prepare data
load(wtdatapath, 'newData');
[ newData ] = PrepareTurningData(newData, vfMin);
%% Set UMAP sampling + embedding parameters
% Set whether to use smoothed or raw centroid kinematic data
smoothing = true;
% Set the half-window length
winlen = 15;
% Sample size
ns = 1e5;
% Set the sampling method (see contents of DefineRandomSampleOfTrajectorySegments)
samplingMethod = 'all';
% If turns are explicitly included, select whether to fold left and right
foldTurns = true;
% Sampling parameters
vfMin = 0.5;
vfMax = 40;
vfBin = 1;
% Set the target dimensionalities
centroid_components = 2;
limb_components = 3;
% Set whether to mean-subtract trajectories
meanSubtractTrajectories = true;
%% Prepare UMAP input data
% This script prepares everything
PrepareDataForUMAP;
%% Make PCA visualizations
MakeLimbDataPCAVisualizations;
%% Write cache paths to a text file so that the Python code can find them
% This is a terrible bodge, and assumes that this code is run with the base
% TurningPaperCode directory as the working directory.
tic;
cfgpath = fullfile('AnalysisUtilities','config.txt');
fid = fopen(cfgpath, 'w');
fprintf(fid, strrep([umapdatapath,',',umapresultpath], '\','\\'));
fclose(fid);
fprintf('Wrote UMAP cache file paths to configuration file in %f seconds.\n', toc);
%% Write data to file to run UMAP from Python
% This block creates an HDF5 cache file containing the data expected by the
% Python UMAP script
tic;
% If a temporary data file exists, delete it
if exist(umapdatapath, 'file') == 2
delete(umapdatapath);
end
% Write data arrays to the file
h5create(umapdatapath, '/x_v', size(X_V));
h5write(umapdatapath, '/x_v', X_V);
h5create(umapdatapath, '/x_l', size(X_L));
h5write(umapdatapath, '/x_l', X_L);
% Write embedding parameters to the file
h5create(umapdatapath, '/centroid_components', 1);
h5write(umapdatapath, '/centroid_components', centroid_components);
h5create(umapdatapath, '/limb_components', 1);
h5write(umapdatapath, '/limb_components', limb_components);
fprintf('Wrote data to HDF5 file in %f seconds.\nPath: %s\n', toc, umapdatapath);
%% Run the UMAP script in Python
% One could call Python from inside MATLAB, but it's generally easier to
% just run the Python script manually.
%
% The appropriate script is `/AnalysisUtilities/run_timeseries_umap.py`
%
% What you need to do is to cd into that directory, and call the script. It
% depends on https://umap-learn.readthedocs.io/
% To force users to do this, raise an error:
error('You must manually run /AnalysisUtilities/run_timeseries_umap.py in Python.');
%% Load UMAP results into MATLAB from file
% The Python UMAP script generates an HDF5 cache file containing the
% embeddings.
tic;
centroidUMAP = h5read(umapresultpath, '/embedding_v');
limbUMAP = h5read(umapresultpath, '/embedding_l');
centroidUMAP = centroidUMAP';
limbUMAP = limbUMAP';
fprintf('Read result from HDF5 file in %f seconds.\nPath: %s\n', toc, umapresultpath);
%% Make UMAP scatter plots
% It's probably advisible to run this script block-by-block, as the size of
% the plots in memory will be quite large.
warning('Generating UMAP plots. This should be run manually due to the size of the scatter plots in memory.');
MakeCentroidAndLimbUMAPScatterPlots;
%% Make cylindrical coordinate plots
MakeUMAPCylindricalCoordinatePlots;