-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_motl_intiailize_random_subset_classes.m
73 lines (49 loc) · 1.62 KB
/
sg_motl_intiailize_random_subset_classes.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
function sg_motl_intiailize_random_subset_classes(motl_name,n_classes,n_subtomos,output_name)
%% sg_motl_intiailize_random_subset_classes
% Generate a set of classes (n_classes) from random subsets of an input
% motivelist. The number of subtomograms (n_subtomos) for each class can be
% given as a number of subtomograms or as a fraction, which will be
% interpreted as a percentage of the motivelist.
%
% NOTE: Some subtomograms will likely appear in more than one class. As
% such, you probably want to use this script to seed random classes but NOT
% to generate a motivelist for classification.
%
% WW 09-2019
%% Check check
% Read motivelist
motl = sg_motl_read2(motl_name);
% Check type
if sg_motl_check_type(motl) == 3
motl = sg_motl_multientry_to_singlentry(motl);
end
%% Generate randomized indices
% Number of motivelist entires
n_motl = numel(motl.motl_idx);
% Check number of subtomograms
if n_subtomos < 1
n_subtomos = round(n_motl*n_subtomos);
end
% Generate randomized indices
rand_cell = cell(n_classes,1);
for i = 1:n_classes
temp_rand = randperm(n_motl)';
rand_cell{i} = temp_rand(1:n_subtomos);
end
rand_idx = cat(1,rand_cell{:});
%% Generate new motlivelist
% Get motl fields
fields = sg_get_motl_fields;
n_fields = size(fields,1);
% Initialize new motivelist
new_motl = struct();
% Fill struct
for i = 1:n_fields
new_motl.(fields{i,1}) = motl.(fields{i,1})(rand_idx);
end
% Renumber motl indices
new_motl.motl_idx = (1:numel(new_motl.motl_idx))';
% Apply classes
new_motl.class = int32(reshape(repmat(1:n_classes,n_subtomos,1),[],1));
% Write output
sg_motl_write2(output_name,new_motl);