% For in vivo spine data analyzed using MapManager V_20171028 
% Author: Richard Roth (rhroth@stanford.edu)
% Date: October 2019
% Based on MapManager-Matlab-1.0.1-alpha by Robert Cudmore, 20171016

function retPool = getmyMapListValuesCond(myMapList, mySegmentTable, Condition, SessionConditions, StatName, channel, PersCond, MinSpines)


% only include segments that are not excluded
rows=mySegmentTable.Include == 1;
myGoodSegmentTable = mySegmentTable(rows,:);
clearvars rows

% only include segments that are part of condition to plot
rows=strcmp(myGoodSegmentTable.MouseCondition,Condition);
myCondSegmentTable = myGoodSegmentTable(rows,:);
clearvars SegmentNumber i rows values valuesgeo meanvalues stdev val valneurons valneuronsSegmentsmeans valmice valmiceSegmentsmeans valmiceNeuronsmeans valmiceNeuronsSegmentsmeans valneuronsSegmentsgeomeans
clearvars valmiceSegmentsgeomeans valmiceNeuronsSegmentsgeomeans valmiceNeuronsgeomeans se retPool NeuronNames NeuronName geomeanvalues currentvalues currentmousename count MapName MouseName

val=struct('values',[],'mean',[],'geomean',[],'count',[],'std',[],'se',[],'segment',[],'branchorder',[]);
valneurons=struct();
valneuronsSegmentsmeans=struct();
valneuronsSegmentsgeomeans=struct();
valmice = struct();
valmiceSegmentsmeans = struct();
valmiceSegmentsgeomeans = struct();

for i = 1:size(myCondSegmentTable,1)
   
MapName = myCondSegmentTable.MapName{i};
MouseName = myCondSegmentTable.MouseName{i};
MouseName = char(MouseName); % Added by RR 2021/09/19

SegmentNumber = myCondSegmentTable.SegmentNumber(i);

currentvalues = segmentCall(myMapList, MapName, SegmentNumber, SessionConditions, StatName, channel, PersCond, MinSpines);

val.values = [val.values; currentvalues.values];
val.valuesSpinePerSegment.([MapName num2str(SegmentNumber)]) = currentvalues.values;
val.valuesSpinePerSegmentNanRemoved.([MapName num2str(SegmentNumber)]) = removeNanRows(currentvalues.values);
val.mean = [val.mean; currentvalues.mean];
val.geomean = [val.geomean; currentvalues.geomean];
val.count = [val.count; currentvalues.count];
val.countSpinePerSegment.([MapName num2str(SegmentNumber)])= currentvalues.count;
val.std = [val.std; currentvalues.std];
val.se = [val.se; currentvalues.se];
val.segment = [val.segment; {[MapName '_' num2str(SegmentNumber)]}];

if isempty(MouseName) == 0 % skip if MouseName is empty (as in not defined in mySegmentTable)
    if isfield (valmice,MouseName) == 0 % make new field in valmice structure if it's first time this mouse comes up
        valmice.(MouseName) = [];
        valmiceSegmentsmeans.(MouseName) = [];
        valmiceSegmentsgeomeans.(MouseName) = [];
        valmice.(MouseName) = [valmice.(MouseName); currentvalues.values]; % concatenate values to this new field
        valmiceSegmentsmeans.(MouseName) = [valmiceSegmentsmeans.(MouseName); currentvalues.mean]; % concatenate values to this new field
        valmiceSegmentsgeomeans.(MouseName) = [valmiceSegmentsgeomeans.(MouseName); currentvalues.geomean]; % concatenate values to this new field
    else 
        valmice.(MouseName) = [valmice.(MouseName); currentvalues.values]; % concatenate values to existing field
        valmiceSegmentsmeans.(MouseName) = [valmiceSegmentsmeans.(MouseName); currentvalues.mean]; % concatenate values to this new field
        valmiceSegmentsgeomeans.(MouseName) = [valmiceSegmentsgeomeans.(MouseName); currentvalues.geomean]; % concatenate values to this new field
    end
end
end

% Values at spine level
values.Spines = val.values;
values.SpinePerSegment = val.valuesSpinePerSegment;
values.SpinePerSegmentNanRemoved = val.valuesSpinePerSegmentNanRemoved;
meanvalues.Spines = nanmean(val.values,1);
geomeanvalues.Spines = nangeomean(val.values,1);
count.Spines = nansum(val.count,1);
count.SpinePerSegment = val.countSpinePerSegment;
stdev.Spines = std(values.Spines,0,1,'omitnan');
se.Spines = stdev.Spines ./ sqrt(count.Spines);

% Values at segment level
values.SegmentsSpines = val.mean;
meanvalues.SegmentsSpines = nanmean(val.mean,1);
geomeanvalues.SegmentsSpines = nangeomean(val.geomean,1);
count.SegmentsSpines = sum(~isnan(val.mean),1);
stdev.SegmentsSpines = std(values.SegmentsSpines,0,1,'omitnan');
se.SegmentsSpines = stdev.SegmentsSpines ./ sqrt(count.SegmentsSpines);


% Values at mouse level
values.MiceSpines = structfun(@(x) nanmean(x,1),valmice,'UniformOutput',false);
meanvalues.MiceSpines = nanmean(cell2mat(struct2cell(values.MiceSpines)),1);
valuesgeo.MiceSpines = structfun(@(x) nangeomean(x,1),valmice,'UniformOutput',false);
geomeanvalues.MiceSpines = nangeomean(cell2mat(struct2cell(valuesgeo.MiceSpines)),1);
count.MiceSpines = sum(~isnan(cell2mat(struct2cell(values.MiceSpines))),1);
stdev.MiceSpines = std(cell2mat(struct2cell(values.MiceSpines)),0,1,'omitnan');
se.MiceSpines = stdev.MiceSpines ./ sqrt(count.MiceSpines);

values.MiceSegmentsSpines = structfun(@(x) nanmean(x,1),valmiceSegmentsmeans,'UniformOutput',false);
meanvalues.MiceSegmentsSpines = nanmean(cell2mat(struct2cell(values.MiceSegmentsSpines)),1);
valuesgeo.MiceSegmentsSpines = structfun(@(x) nangeomean(x,1),valmiceSegmentsgeomeans,'UniformOutput',false);
geomeanvalues.MiceSegmentsSpines = nangeomean(cell2mat(struct2cell(valuesgeo.MiceSegmentsSpines)),1);
count.MiceSegmentsSpines = sum(~isnan(cell2mat(struct2cell(values.MiceSegmentsSpines))),1);
stdev.MiceSegmentsSpines = std(cell2mat(struct2cell(values.MiceSegmentsSpines)),0,1,'omitnan');
se.MiceSegmentsSpines = stdev.MiceSegmentsSpines ./ sqrt(count.MiceSegmentsSpines);


values.segmentnames = val.segment;

retPool.values = values;
retPool.meanvalues = meanvalues;
retPool.valuesgeo = valuesgeo;
retPool.geomeanvalues = geomeanvalues;
retPool.count = count;
retPool.stdev = stdev;
retPool.se = se;
retPool.stat = StatName;
retPool.condition = Condition;
retPool.SessionConditions = SessionConditions;
end