From 232053bb87933fbba675991b640a653e5af774d6 Mon Sep 17 00:00:00 2001
From: Eric Wait <eric@waitphoto.com>
Date: Fri, 8 Apr 2011 22:49:01 -0500
Subject: [PATCH] LEVer 4.2

---
 bin/CompileLEVer.m                  |  5 +++--
 src/MATLAB/AddFields.m              | 12 +++++++++++
 src/MATLAB/AddHull.m                |  2 +-
 src/MATLAB/ContextChangeParent.m    |  2 +-
 src/MATLAB/CreateContextMenuCells.m | 33 +++++++++++------------------
 src/MATLAB/CreateContextMenuTree.m  |  4 ++--
 src/MATLAB/CreateMenuBar.m          |  2 +-
 src/MATLAB/GetDarkConnectedHulls.m  |  8 +++----
 src/MATLAB/GetTrackID.m             | 29 ++++++++++++++++---------
 src/MATLAB/OpenData.m               |  3 ++-
 src/MATLAB/SegAndTrack.m            |  8 ++++---
 src/MATLAB/Segmentor.m              |  7 +++---
 src/MATLAB/TestDataIntegrity.m      | 33 +++++++++++++++--------------
 src/MATLAB/WriteSegData.m           |  2 +-
 14 files changed, 83 insertions(+), 67 deletions(-)
 create mode 100644 src/MATLAB/AddFields.m

diff --git a/bin/CompileLEVer.m b/bin/CompileLEVer.m
index aa9094db..0abcfb46 100644
--- a/bin/CompileLEVer.m
+++ b/bin/CompileLEVer.m
@@ -1,5 +1,6 @@
-mcc -m ../LEVer.m
-mcc -m ../Segmentor.m
+mcc -m ..\LEVer.m
+mcc -m ..\Segmentor.m
+system('copy Segmentor.exe ..\.');
 if(isempty(dir('./MTC.exe')))
     warndlg('Make sure that MTC.exe is in the same dir as LEVer.exe');
 end
\ No newline at end of file
diff --git a/src/MATLAB/AddFields.m b/src/MATLAB/AddFields.m
new file mode 100644
index 00000000..d6bd5763
--- /dev/null
+++ b/src/MATLAB/AddFields.m
@@ -0,0 +1,12 @@
+function AddFields()
+
+global HashedCells
+
+if(~isfield([HashedCells{:}],'editedFlag'))
+    for i=1:length(HashedCells)
+        for j = 1:length(HashedCells{i})
+            HashedCells{i}(j).editedFlag = 0;
+        end
+    end
+end
+end
\ No newline at end of file
diff --git a/src/MATLAB/AddHull.m b/src/MATLAB/AddHull.m
index 090b6e82..48f4a5b5 100644
--- a/src/MATLAB/AddHull.m
+++ b/src/MATLAB/AddHull.m
@@ -17,7 +17,7 @@ if(~isempty(trackID))
     % Try to split the existing hull    
     History('Push');
     try
-        newTracks = SplitHull(hullID,num+1);%adding one to the number so that the original hull is accounted for
+        newTracks = SplitHull(hullID,num);
         if(isempty(newTracks))
             msgbox(['Unable to split ' num2str(trackID) ' any further in this frame'],'Unable to Split','help','modal');
             return
diff --git a/src/MATLAB/ContextChangeParent.m b/src/MATLAB/ContextChangeParent.m
index 83c0db2d..e38608b4 100644
--- a/src/MATLAB/ContextChangeParent.m
+++ b/src/MATLAB/ContextChangeParent.m
@@ -18,7 +18,7 @@ if(CellTracks(newParentID).startTime > time)
     msgbox(['Parent ' num2str(newParentID) ' comes after ' num2str(trackID) ' consider a different edit.'],'Parent Change','warn');
     return
 elseif(CellTracks(trackID).endTime < CellTracks(newParentID).startTime)
-    msgbox(['Sibling ' num2str(trackID) ' exists completely before ' num2str(newParentID) ' consider a rename instead.'],'Parent Change','warn');
+    msgbox(['Sister Cell ' num2str(trackID) ' exists completely before ' num2str(newParentID) ' consider a rename instead.'],'Parent Change','warn');
     return
 end
 
diff --git a/src/MATLAB/CreateContextMenuCells.m b/src/MATLAB/CreateContextMenuCells.m
index 426a271c..52b6dca5 100644
--- a/src/MATLAB/CreateContextMenuCells.m
+++ b/src/MATLAB/CreateContextMenuCells.m
@@ -32,28 +32,23 @@ uimenu(Figures.cells.contextMenuHandle,...
 %     'CallBack',     @changeParent);
 
 addHull = uimenu(Figures.cells.contextMenuHandle,...
-    'Label',        'Add Hull',...
+    'Label',        'Add Segmentation',...
     'Separator',    'on');
 
 uimenu(addHull,...
-    'Label',        'Number of Hulls to add');
-
-uimenu(addHull,...
-    'Label',        '1',...
-    'Separator',    'on',...
-    'CallBack',     @addHull1);
+    'Label',        'Number of Cells');
 
 uimenu(addHull,...
     'Label',        '2',...
-    'CallBack',     @addHull2);
+    'CallBack',     @addHull1);
 
 uimenu(addHull,...
     'Label',        '3',...
-    'CallBack',     @addHull3);
+    'CallBack',     @addHull1);
 
 uimenu(addHull,...
     'Label',        '4',...
-    'CallBack',     @addHull4);
+    'CallBack',     @addHull3);
 
 uimenu(addHull,...
     'Label',        'Other',...
@@ -61,7 +56,7 @@ uimenu(addHull,...
     'CallBack',     @addHullOther);
 
 uimenu(Figures.cells.contextMenuHandle,...
-    'Label',        'Remove Hull',...
+    'Label',        'Remove Cell',...
     'CallBack',     @removeHull);
 
 uimenu(Figures.cells.contextMenuHandle,...
@@ -141,7 +136,7 @@ if(isempty(trackID)),return,end
 % answer = inputdlg({['Enter new sibling of ' num2str(trackID)],'Enter Time of Mitosis'},...
 %     'Add Mitosis',1,{'',num2str(Figures.time)});
 
-answer = inputdlg({['Enter new sibling of ' num2str(trackID)]},...
+answer = inputdlg({['Enter new sister of cell' num2str(trackID)]},...
     'Add Mitosis',1,{''});
 
 if(isempty(answer)),return,end
@@ -154,11 +149,11 @@ if(siblingTrack>length(CellTracks) || isempty(CellTracks(siblingTrack).hulls))
     return
 end
 if(CellTracks(siblingTrack).endTime<time || siblingTrack==trackID)
-    msgbox([answer(1) ' is not a valid sibling'],'Not a valid sibling','error');
+    msgbox([answer(1) ' is not a valid sister cell'],'Not a valid sister cell','error');
     return
 end
 if(CellTracks(trackID).startTime>time)
-    msgbox([num2str(trackID) ' exists after ' answer(1)],'Not a valid child','error');
+    msgbox([num2str(trackID) ' exists after ' answer(1)],'Not a valid daughter cell','error');
     return
 end
 if(~isempty(CellTracks(siblingTrack).timeOfDeath) && CellTracks(siblingTrack).timeOfDeath<=time)
@@ -201,7 +196,7 @@ elseif(CellTracks(siblingTrack).startTime==time && CellTracks(trackID).startTime
 elseif(CellTracks(siblingTrack).startTime==time && CellTracks(trackID).startTime==time)
     valid = 0;
     while(~valid)
-        answer = inputdlg({'Enter parent of these siblings '},'Parent',1,{''});
+        answer = inputdlg({'Enter parent of these daughter cells '},'Parent',1,{''});
         if(isempty(answer)),return,end
         parentTrack = str2double(answer(1));
         
@@ -297,10 +292,6 @@ if(isempty(trackID)),return,end
 ContextChangeParent(trackID,Figures.time);
 end
 
-function addHull1(src,evnt)
-AddHull(1);
-end
-
 function addHull2(src,evnt)
 AddHull(2);
 end
@@ -314,7 +305,7 @@ AddHull(4);
 end
 
 function addHullOther(src,evnt)
-num = inputdlg('Enter Number of Hulls to Add','Add Hulls',1,{'1'});
+num = inputdlg('Enter Number of Cells Present','Add Hulls',1,{'1'});
 if(isempty(num)),return,end;
 num = str2double(num(1));
 AddHull(num);
@@ -352,7 +343,7 @@ if(isempty(CellFamilies(Figures.tree.familyID).tracks))
     end
     DrawTree(Figures.tree.familyID);
     DrawCells();
-    msgbox(['By removing this hull, the complete tree is no more. Displaying tree rooted at ' num2str(CellFamilies(i).rootTrackID) ' instead'],'Displaying Tree','help');
+    msgbox(['By removing this cell, the complete tree is no more. Displaying clone rooted at ' num2str(CellFamilies(i).rootTrackID) ' instead'],'Displaying Tree','help');
     return
 end
 
diff --git a/src/MATLAB/CreateContextMenuTree.m b/src/MATLAB/CreateContextMenuTree.m
index 25cb6f83..cad163c6 100644
--- a/src/MATLAB/CreateContextMenuTree.m
+++ b/src/MATLAB/CreateContextMenuTree.m
@@ -140,7 +140,7 @@ trackID = get(gco,'UserData');
 time = get(gca,'CurrentPoint');
 time = round(time(1,2));
 
-answer = inputdlg({'Enter Time of Mitosis',['Enter new sibling of ' num2str(trackID)]},...
+answer = inputdlg({'Enter Time of Mitosis',['Enter new sister cell of ' num2str(trackID)]},...
     'Add Mitosis',1,{num2str(time),''});
 
 if(isempty(answer)),return,end
@@ -153,7 +153,7 @@ if(siblingTrack>length(CellTracks) || isempty(CellTracks(siblingTrack).hulls))
     return
 end
 if(CellTracks(trackID).startTime>time)
-    msgbox([num2str(trackID) ' exists after ' answer(1)],'Not a valid child','error');
+    msgbox([num2str(trackID) ' exists after ' answer(1)],'Not a valid daughter cell','error');
     return
 end
 
diff --git a/src/MATLAB/CreateMenuBar.m b/src/MATLAB/CreateMenuBar.m
index f0a735a2..5f33810c 100644
--- a/src/MATLAB/CreateMenuBar.m
+++ b/src/MATLAB/CreateMenuBar.m
@@ -97,7 +97,7 @@ labelsMenu = uimenu(...
 
 siblingsMenu = uimenu(...
     'Parent',           viewMenu,...
-    'Label',            'Show Sibling Relationships',...
+    'Label',            'Show Sister Cell Relationships',...
     'HandleVisibility', 'callback',...
     'Callback',         @toggleSiblings,...
     'Checked',          'off',...
diff --git a/src/MATLAB/GetDarkConnectedHulls.m b/src/MATLAB/GetDarkConnectedHulls.m
index 6ffd5ffc..2d2e2dd0 100644
--- a/src/MATLAB/GetDarkConnectedHulls.m
+++ b/src/MATLAB/GetDarkConnectedHulls.m
@@ -36,10 +36,10 @@ end
 
 
     function objsSegment=GetDistances(objsSegment,HashedHulls,t1,t2)
-        bwHalo1=GetHalo(t1);
-        bwHalo2=GetHalo(t2);
-        bwCombo=GetCombo(bwHalo1,bwHalo2,objsSegment,HashedHulls,t1,t2);
-        LCombo=bwlabel(bwCombo);
+%         bwHalo1=GetHalo(t1);
+%         bwHalo2=GetHalo(t2);
+%         bwCombo=GetCombo(bwHalo1,bwHalo2,objsSegment,HashedHulls,t1,t2);
+%         LCombo=bwlabel(bwCombo);
         for i=1:length(HashedHulls{t1})
             hi=HashedHulls{t1}(i);
             ipix=objsSegment(hi).indPixels;
diff --git a/src/MATLAB/GetTrackID.m b/src/MATLAB/GetTrackID.m
index 8da8ff2a..260906d7 100644
--- a/src/MATLAB/GetTrackID.m
+++ b/src/MATLAB/GetTrackID.m
@@ -1,18 +1,27 @@
-function trackID = GetTrackID(hullID)
+function trackIDs = GetTrackID(hullIDs,time)
 %Given a hull ID a track ID will be returned or [] if none found
 
 %--Eric Wait
 
 global CellHulls HashedCells
 
-trackID = [];
+trackIDs = [];
 
-if(hullID>length(CellHulls))
-    return
+if(~exist('time','var'))
+    for i=1:length(hullIDs)
+        if(hullIDs(i)>length(CellHulls))
+            continue
+        else
+            hullTime = CellHulls(hullIDs(i)).time;
+            hashedCellIndex = [HashedCells{hullTime}.hullID] == hullIDs(i);
+            if(isempty(hashedCellIndex)),continue,end
+            trackIDs = [trackIDs HashedCells{hullTime}(hashedCellIndex).trackID];
+        end
+    end
 else
-    hullTime = CellHulls(hullID).time;
-    hashedCellIndex = [HashedCells{hullTime}.hullID] == hullID;
-    if(isempty(hashedCellIndex)),return,end
-    trackID = HashedCells{hullTime}(hashedCellIndex).trackID;
-end
-end
+    if(time>length(HashedCells))
+        return
+    else
+        trackIDs = [HashedCells{time}(ismember([HashedCells{time}.hullID],hullIDs)).trackID];
+    end
+end
\ No newline at end of file
diff --git a/src/MATLAB/OpenData.m b/src/MATLAB/OpenData.m
index 7aad96c8..9a164f78 100644
--- a/src/MATLAB/OpenData.m
+++ b/src/MATLAB/OpenData.m
@@ -7,7 +7,7 @@ function opened = OpenData()
 
 global Figures Colors CONSTANTS CellFamilies CellHulls HashedCells Costs CellTracks
 if(isempty(Figures))
-    fprintf('LEVer ver 4.1\n***DO NOT DISTRIBUTE***\n\n');
+    fprintf('LEVer ver 4.2\n***DO NOT DISTRIBUTE***\n\n');
 end
 
 if(exist('ColorScheme.mat','file'))
@@ -141,6 +141,7 @@ switch answer
                 fprintf(['New file saved as:\n' CONSTANTS.datasetName '_LEVer.mat']);
                 goodLoad = 1;
             elseif(exist('CellHulls','var'))
+                TestDataIntegrity(1);
                 goodLoad = 1;
             else
                 errordlg('Data either did not load properly or is not the right format for LEVer.');
diff --git a/src/MATLAB/SegAndTrack.m b/src/MATLAB/SegAndTrack.m
index ddb2799e..e597b55c 100644
--- a/src/MATLAB/SegAndTrack.m
+++ b/src/MATLAB/SegAndTrack.m
@@ -24,6 +24,8 @@ for timeStart=1:step:numberOfImages
     system(['start Segmentor ' num2str(timeStart) ' ' num2str(step-1) ' "' ...
         CONSTANTS.rootImageFolder(1:end-1) '" ' CONSTANTS.datasetName ' ' ...
         num2str(CONSTANTS.imageAlpha) ' ' num2str(CONSTANTS.imageSignificantDigits) ' && exit']);
+    %use line below instead of the 3 lines above for non-parallel or to debug
+%     Segmentor(timeStart,step-1,CONSTANTS.rootImageFolder(1:end-1),CONSTANTS.datasetName,CONSTANTS.imageAlpha,CONSTANTS.imageSignificantDigits);
 end
 
 for i=1:step:numberOfImages
@@ -46,13 +48,13 @@ end
 fprintf('Please wait...');
 
 cellSegments = GetDarkConnectedHulls(cellSegments);
-save ( ['SegObjs_' CONSTANTS.datasetName '.mat'],'cellSegments');
+save ( ['.\segmentationData\SegObjs_' CONSTANTS.datasetName '.mat'],'cellSegments');
 WriteSegData(cellSegments,CONSTANTS.datasetName);
 
 fprintf(1,'\nDone\n');
 
-fnameIn=['SegObjs_' CONSTANTS.datasetName '.txt'];
-fnameOut=['Tracked_' CONSTANTS.datasetName '.txt'];
+fnameIn=['.\segmentationData\SegObjs_' CONSTANTS.datasetName '.txt'];
+fnameOut=['.\segmentationData\Tracked_' CONSTANTS.datasetName '.txt'];
 tSeg=toc;
 
 %% Tracking
diff --git a/src/MATLAB/Segmentor.m b/src/MATLAB/Segmentor.m
index b14d8ca7..8f130880 100644
--- a/src/MATLAB/Segmentor.m
+++ b/src/MATLAB/Segmentor.m
@@ -30,10 +30,7 @@ for t = tStart:tStart + tLength
     fname=[rootImageFolder '\' datasetName '_t' frameT '.TIF'];
     if(isempty(dir(fname))),continue,end
     
-    fprintf('%d, ', t);
-    if ( mod(t,20) == 0 )
-        fprintf('\n');
-    end
+    fprintf('%d%%...',ceil((t-tStart)/tLength*100));
     
     [im map]=imread(fname);
     im=mat2gray(im);
@@ -254,4 +251,6 @@ end
 fileName = ['.\segmentationData\objs_' num2str(tStart) '.mat'];
 if(isempty(dir('.\segmentationData'))),system('mkdir .\segmentationData');end
 save(fileName,'objs');
+
+fprintf('\tDone\n');
 end
\ No newline at end of file
diff --git a/src/MATLAB/TestDataIntegrity.m b/src/MATLAB/TestDataIntegrity.m
index 1f0aedd4..7d3088eb 100644
--- a/src/MATLAB/TestDataIntegrity.m
+++ b/src/MATLAB/TestDataIntegrity.m
@@ -1,6 +1,6 @@
 function TestDataIntegrity(correct)
 %TestDataIntegrity(correct) tests to make sure that the database is consistant.
-%Takes the CellTracks as the most accurate.  If correct=='yes', this
+%Takes the CellTracks as the most accurate.  If correct==1, this
 %function will attempt to correct the error using the data from CellTracks
 %***USE SPARINGLY, TAKES A LOT OF TIME***
 
@@ -10,6 +10,7 @@ global CellTracks CellHulls CellFamilies HashedCells
 
 hullsList = [];
 fprintf('Checking CellTracks...');
+AddFields();
 progress = 0;
 iterations = length(CellTracks);
 for i=1:length(CellTracks)
@@ -117,21 +118,21 @@ end
 %% check CellHulls
 % if(length(hullsList)~=length(find([CellHulls.deleted]==0)))
 missingHulls = find(ismember(find([CellHulls.deleted]==0),hullsList')==0);
-% if(~isempty(missingHulls))
-%     if(correct)
-%         progress = 0;
-%         iterations = length(missingHulls); 
-%         for i=1:length(missingHulls)
-%             progress = progress+1;
-%             Progressbar(progress/iterations);
-%             if(isempty(CellHulls(missingHulls(i)).points))
-%                 CellHulls(missingHulls(i)).deleted = 1;
-%             end
-%         end
-%     else
-%         error('HullsList ~= CellHulls');
-%     end
-% end
+if(~isempty(missingHulls))
+    if(correct)
+        progress = 0;
+        iterations = length(missingHulls); 
+        for i=1:length(missingHulls)
+            progress = progress+1;
+            Progressbar(progress/iterations);
+            if(isempty(CellHulls(missingHulls(i)).points))
+                CellHulls(missingHulls(i)).deleted = 1;
+            end
+        end
+    else
+        error('HullsList ~= CellHulls');
+    end
+end
 Progressbar(1);%clear it out
 
 fprintf('\nDone\n');
diff --git a/src/MATLAB/WriteSegData.m b/src/MATLAB/WriteSegData.m
index fd44da27..6c285d4f 100644
--- a/src/MATLAB/WriteSegData.m
+++ b/src/MATLAB/WriteSegData.m
@@ -7,7 +7,7 @@ HashedHulls=cell(th,1);
 for n=1:length(objsSegment)
     HashedHulls{objsSegment(n).t}=[HashedHulls{objsSegment(n).t};n];
 end
-fname=['SegObjs_' DatasetName '.txt'];
+fname=['.\segmentationData\SegObjs_' DatasetName '.txt'];
 fid=fopen(fname,'wt');
 fprintf(fid,'%d %d\n',th,length(objsSegment) );
 for i=1:length(HashedHulls)
-- 
GitLab