function [] = assemblevideo(folder, gangmat, maxmove, nGang, nNeuron, seed) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Creates the simulation data used in figure 3 % Inputs: % - folder: The folder to save the data to; location of gangmat and % background png files % - gangmat: string of the name of the .mat that contains the % identified groups of neurons that form ganglia % - maxmove: The peak movement in pixels for the video % - nGang: The number of ganglia to have within the field of view % - nNeuron: The maxium number of neurons each ganglia can have % - seed: The seed for the run % Output: %Saves a .mat file that can be used to generate the simulation data along %with an h5 file of simulated data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% cd(folder) load(gangmat) % Generate the random seed from the input values if isa(maxmove, 'char') maxmove = str2num(maxmove); nGang = str2num(nGang); nNeuron = str2num(nNeuron); seed = str2num(seed); end rng(str2num(strcat(num2str(maxmove), num2str(seed), num2str(nGang), num2str(nNeuron)))); ID = datestr(datetime('now')); sind = strfind(ID, ' '); ID(sind) = '-'; cind = strfind(ID, ':'); ID(cind) = '-'; ID = strcat(ID, 'Amplitude', num2str(maxmove), 'nGanglia', num2str(nGang), 'nNeuron', num2str(nNeuron), 'seed', num2str(seed)); tic % Randomly select which ganglia to use in the field of view and place them % in a roughly grid like formation in the field of view delay = 40; % set the time before a large motor complex occurs totaltime = 119; % Set length fo the simulation t = 0.05:0.05:totaltime; % Determine time point of the motor complex enddelay = find(t > delay, 1, 'first'); d1 = 600; d2 = 600; % Calculate movement for the X and Y dimensions, the propagation angle of % the movement, and the frame at which a large motor complex begins class(maxmove) [movement] = genMovement_fit(t, enddelay, maxmove); MoveXt = movement.MoveXmajor+movement.MoveXminor; MoveYt = movement.MoveYmajor+movement.MoveYminor; movefac = 0.98; c = [ceil(max([max(MoveXt) max(MoveXt*movefac)])) floor(d2+min([0 min([min(MoveXt) min(MoveXt*movefac)])]))]; r = [ceil(max([max(MoveYt) max(MoveYt*movefac)])) floor(d1+min([0 min([min(MoveYt) min(MoveYt*movefac)])]))]; % Place the ganglia in a random lattice across the field of view [newgang, gangCOM, neighGanglia, gangMask] = placeGanglia(gangmat, r, c, nNeuron, nGang); % Smooth out the edges of the neurons so that they look more like realistic % neurons [filtgang] = smoothNeuron(newgang, 1); masks = cat(3, filtgang{:}); [fluo] = genSimulatedF(t, enddelay-20, masks); fluo = fluo'; toc % Generate a video of the ganglia and internodal strands moving without % background [svid, MoveXt, MoveYt, offset, useorder, saveMove] = moveGanglia(filtgang, gangCOM, neighGanglia, gangMask, fluo, movement, t); saveName = strcat('simulated data_', ID, '.mat'); A = dir('*_Green_background.png'); useback = randsample(length(A), 1); offsetbg = 0; save(saveName, 'newgang', 'gangCOM', 'neighGanglia', 'gangMask', 'saveMove',... 'offset', 'useorder', 'svid', 'MoveXt', 'MoveYt', 'movefac', 'maxmove', 'nGang', 'nNeuron', '-v7.3') % Apply the data to the pre-filtered background images for vid = 1:length(useback) img = imread(A(useback(vid)).name); img = single(img); avgval = mean(img(:)); % Randomly select how much brighter than the background the entire % simulated set of ganglia and internodal strands is compared to the % background scalefac = 1.15; scalevid = svid*(avgval*scalefac(vid)-avgval); % Make the background move at a lower amplitude than the overlying % ganglia and internodal strands simvid = []; for fn = 1:size(svid, 3) if fn - offsetbg >= 1 ind = fn - offsetbg; moveX = MoveXt(ind); moveY = MoveYt(ind); tCOMn = [moveX*movefac moveY*movefac]; newback = imtranslate(img, tCOMn); else newback = img; end framemed = median(newback(:)); frame = scalevid(:, :, fn); ind = find(frame(:)); fback = imgaussfilt(newback, 2); gnoise = normrnd(0, mad(newback(:), 1)/2, size(fback)); simvid(:, :, fn) = frame + fback + gnoise ; end [d1 d2 d3] = size(simvid); simvid = simvid(r(1):r(2), c(1):c(2), :); vidname = strcat(A(useback(vid)).name(1:end-4), ID, 'uint8.h5'); simvid = uint8(simvid); h5create(vidname, '/video', size(simvid), 'Datatype', 'uint8'); h5write(vidname, '/video', simvid); end end