Commit 9d5bd769 authored by François  LAURENT's avatar François LAURENT
Browse files

save json labels to trxmat

parent dda4017a
Pipeline #81285 passed with stage
in 3 minutes and 44 seconds
......@@ -21,3 +21,11 @@ lazy = true
[[sample_trxmat_file_small.download]]
url = "https://gitlab.pasteur.fr/nyx/artefacts/-/raw/master/PlanarLarvae/trx_small.tgz?inline=false"
sha256 = "806ac7c2e51d2ed5535e336a44bc33cad4396850318ab771f8c9057ceda9a0d8"
[sample_labels_small]
git-tree-sha1 = "73e3d5b91e5c8541012d128e094837e1e28239e5"
lazy = true
[[sample_labels_small.download]]
url = "https://gitlab.pasteur.fr/nyx/artefacts/-/raw/master/PlanarLarvae/labels_small.tgz?inline=false"
sha256 = "b6be8a281644726f09ef7ebd0ac3976c388e042838e7258a1d407d1f48abec53"
function [trx] = remove_behavior_tags(trx, varargin)
% REMOVE_BEHAVIOR_TAGS Remove fields from trx structure array known to be
% behavior tags.
%
% TRX = REMOVE_BEHAVIOR_TAGS(TRX) removes tags such as "run", "run_large"
% etc.
%
% TRX = REMOVE_BEHAVIOR_TAGS(TRX, TAG1, TAG2, ...) additionally removes
% trailing TAG1, TAG2, ...
std_tags = [ ...
"back" "back_large" "back_strong" "back_weak" ...
"cast" "cast_large" "cast_strong" "cast_weak" ...
"hunch" "hunch_large" "hunch_strong" "hunch_weak" ...
"roll" "roll_large" "roll_strong" "roll_weak" ...
"run" "run_large" "run_strong" "run_weak" ...
"small_motion" ...
"stop" "stop_large" "stop_strong" "stop_weak" ...
];
for tag = std_tags
if isfield(trx, tag)
trx = rmfield(trx, tag);
end
end
for tag = varargin
tag = tag{1};
trx = rmfield(trx, tag);
end
end
\ No newline at end of file
function [updated_trx] = update_trx_with_labels(trx, labels_file, varargin)
% UPDATE_TRX_WITH_LABELS Update trx structure array with json labels.
%
% TRX = UPDATE_TRX_WITH_LABELS(TRX, LABELS_FILE) loads json-encoded
% labels from file LABELS_FILE, and add corresponding fields in
% structure array TRX.
%
% TRX = UPDATE_TRX_WITH_LABELS(TRX_FILE, LABELS_FILE) loads TRX from file
% TRX_FILE.
%
% TRX = UPDATE_TRX_WITH_LABELS(..., "outputfile", OUTPUT_TRX_FILE) saves
% the updated TRX to file OUTPUT_TRX_FILE.
%
% TRX = UPDATE_TRX_WITH_LABELS(..., "removetags", 1) removes all known
% tags from TRX prior to adding tags from LABELS_FILE.
%
% See also REMOVE_BEHAVIOR_TAGS.
if ~isa(trx, "struct")
load(trx, "trx");
end
args = struct();
if 2<nargin
for n = 3:2:nargin
args.(varargin{n-2}) = varargin{n-1};
end
end
if isfield(args, "removetags")
if args.removetags
trx = remove_behavior_tags(trx);
end
end
data = jsondecode(fileread(labels_file));
labelnames = data.labels.names;
updated_trx = struct([]);
% backward indexing trick to preallocate the updated_trx array
for k = length(trx):-1:1
for f = fieldnames(trx)'
f = f{1};
updated_trx(k,1).(f) = trx(k).(f);
end
tag = -ones(size(trx(k).t));
for label = labelnames'
label = label{1};
updated_trx(k,1).(label) = tag;
end
end
data = data.data;
for larva = 1:length(data)
labels = data(larva).labels;
larvaid = data(larva).id;
larva = find([trx.numero_larva_num]==str2num(larvaid));
if isempty(larva)
disp("skipping larva " + larvaid);
else
for label = unique(labels)'
labelname = labelnames{label};
field = updated_trx(larva).(labelname);
field(labels==label) = 1;
updated_trx(larva).(labelname) = field;
end
end
end
if isfield(args, "outputfile")
trx = updated_trx;
save(args.outputfile, "trx");
end
end
\ No newline at end of file
......@@ -210,30 +210,30 @@ function read_behavior_tags(tags, file::TrxMatFile)
end
behavior_tags = [
:small_motion,
:stop_large,
:back,
:back_large,
:back_strong,
:back_weak,
:roll_weak,
:cast,
:cast_large,
:cast_strong,
:cast_weak,
:hunch,
:back,
:hunch_large,
:hunch_strong,
:hunch_weak,
:roll,
:roll_large,
:roll_strong,
:cast_weak,
:roll_weak,
:run,
:run_large,
:stop_weak,
:run_strong,
:run_weak,
:hunch_strong,
:run,
:cast_large,
:hunch_large,
:small_motion,
:stop_large,
:stop_strong,
:back_large,
:roll_large,
:cast_strong,
:roll,
:back_strong,
:cast,
:run_strong,
:stop_weak,
]
read_behavior_tags(file::TrxMatFile) = read_behavior_tags(behavior_tags, file)
......
......@@ -24,6 +24,15 @@ function make_test_data(target="chore_sample_outline")
elseif target == "sample_trxmat_file"
artefact = artifact"sample_trxmat_file"
filename = "trx.mat"
elseif target == "sample_trxmat_file_small"
artefact = artifact"sample_trxmat_file_small"
filename = "trx_small.mat"
elseif target == "sample_labels_file_trx_small"
artefact = artifact"sample_labels_small"
filename = "jbm_tagger.labels"
elseif target == "sample_labels_file_nyx_small"
artefact = artifact"sample_labels_small"
filename = "maggotuba.labels"
else
throw(DomainError("unknown artefact: " * target))
end
......
......@@ -311,7 +311,7 @@ if all_tests || "Trxmat" in ARGS
data_with_selected_tags = read_trxmat((:tags=>Tags([:hunch, :back, :run, :roll, :cast]),), target″)
# example from README
larvastate = first(eachstate(data_with_tags))
@test string(larvastate.tags) == "24-element Tag{\"small_motion\", \"run_weak\", \"run\"}"
@test string(larvastate.tags) == "24-element Tag{\"run\", \"run_weak\", \"small_motion\"}"
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment