Skip to content

Commit

Permalink
Updated handling of RegionOfInterest in post processing techniques
Browse files Browse the repository at this point in the history
Previously attempting to perform PCA (or other MVA) technique on a ROI would fail. If the ROI had different pixels to those present within the data, this would also fail. Now the correct data is extracted for PCA (or other MVA) and a new (correct) ROI is generated from the combination of the original data ROI and the user selected ROI.
  • Loading branch information
AlanRace committed Apr 9, 2019
1 parent 21606de commit fc9f729
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 58 deletions.
31 changes: 6 additions & 25 deletions src/core/DataInMemory.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

function setData(obj, data, regionOfInterest, isRowMajor, spectralChannels, name)
obj.data = data;
obj.regionOfInterest = regionOfInterest;
obj.spectralChannels = spectralChannels;
obj.spectralChannelRange = [min(spectralChannels) max(spectralChannels)];
obj.name = name;
Expand All @@ -41,8 +40,8 @@ function setData(obj, data, regionOfInterest, isRowMajor, spectralChannels, name
% Ensure that no NaN exist in the data to avoid issues with
% display and subsequent processing
obj.data(isnan(obj.data)) = 0;

obj.createPixelList();
obj.setRegionOfInterest(regionOfInterest);
end

function loadData(obj, parser, regionOfInterest, spectralChannelRange, zeroFilling)
Expand Down Expand Up @@ -154,24 +153,15 @@ function loadData(obj, parser, regionOfInterest, spectralChannelRange, zeroFilli
selectionData = pixelMask(obj.minY:obj.maxY, obj.minX:obj.maxX);

if(isempty(preprocessingWorkflow) || preprocessingWorkflow.numberOfMethods == 0)
pixels = obj.regionOfInterest.getPixelList();

if(obj.isRowMajor)
% Sort by y column, then by x column
pixels = sortrows(pixels, [2 1]);
else
% Sort by x column, then by y column
pixels = sortrows(pixels, [1 2]);
end

for imageIndex = 1:length(spectralChannelList)
indicies = obj.spectralChannels >= (spectralChannelList(imageIndex)-spectralWidthList(imageIndex)) ...
& obj.spectralChannels <= (spectralChannelList(imageIndex) + spectralWidthList(imageIndex));

d = sum(obj.data(:, indicies), 2);

for i = 1:length(pixels)
images(pixels(i, 2), pixels(i, 1), imageIndex) = d(i);
for i = 1:length(obj.pixels)
images(obj.pixels(i, 2), obj.pixels(i, 1), imageIndex) = d(i);
end
end

Expand Down Expand Up @@ -223,18 +213,9 @@ function loadData(obj, parser, regionOfInterest, spectralChannelRange, zeroFilli
% image(selectionData == 1) = sum(obj.data, 2);

d = sum(obj.data, 2);
pixels = obj.regionOfInterest.getPixelList();

if(obj.isRowMajor)
% Sort by y column, then by x column
pixels = sortrows(pixels, [2 1]);
else
% Sort by x column, then by y column
pixels = sortrows(pixels, [1 2]);
end

for i = 1:length(pixels)
image(pixels(i, 2), pixels(i, 1)) = d(i);
for i = 1:length(obj.pixels)
image(obj.pixels(i, 2), obj.pixels(i, 1)) = d(i);
end

image = Image(image);
Expand Down
2 changes: 1 addition & 1 deletion src/core/DataOnDisk.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

methods
function obj = DataOnDisk(parser)
if(~isa(parser, 'Parser'))
if(~isa(parser, 'Parser'))
exception = MException('DataOnDisk:InvalidArgument', 'Must supply an instance of the Parser class.');
throw(exception);
end
Expand Down
32 changes: 30 additions & 2 deletions src/core/DataRepresentation.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,42 @@ function createPixelList(obj)
obj.height = (obj.maxY - obj.minY) + 1;

if(obj.isRowMajor)
% Sort by y column, then by x column
% Sort by rows and then columns
[obj.pixels, obj.pixelIndicies] = sortrows(obj.pixels, [2 1]);
else
% Sort by x column, then by y column
% Sort by columns and then rows
[obj.pixels, obj.pixelIndicies] = sortrows(obj.pixels, [1 2]);
end
end

% Returns a list of the pixel coordinates in the order that the
% data is stored in. I.e. if the data is row major, then the
% returned pixel list will also be row major.
%
% If no regionOfInterest is supplied then the base pixel list is
% returned
function pixelList = getDataOrderedPixelList(this, regionOfInterest)
if(~exist('regionOfInterest', 'var'))
pixelList = this.pixels;
else
pixelList = regionOfInterest.getPixelList();

if(this.isRowMajor)
pixelList = sortrows(pixelList, [2 1]);
else
pixelList = sortrows(pixelList, [1 2]);
end
end
end

function roiIndexList = getDataIndiciesForROI(this, regionOfInterest)
roiPixelList = this.getDataOrderedPixelList(regionOfInterest);

[c, index_A, index_B] = intersect(this.pixels, roiPixelList, 'stable', 'rows');

roiIndexList = this.pixelIndicies(index_A);
end

function exportToWorkspace(obj)
variableName = inputdlg('Please specifiy a variable name:', 'Variable name', 1, {'dataRepresentation'});

Expand Down
13 changes: 2 additions & 11 deletions src/core/ProjectedDataInMemory.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@ function setData(obj, data, projectionMatrix, pixelSelection, isRowMajor, spectr
% image(selectionData == 1) = obj.data(:, index);
size(obj.data)
d = obj.data(:, index);
pixels = obj.regionOfInterest.getPixelList();

if(obj.isRowMajor)
% Sort by y column, then by x column
pixels = sortrows(pixels, [2 1]);
else
% Sort by x column, then by y column
pixels = sortrows(pixels, [1 2]);
end
size(pixels)
for i = 1:length(pixels)
image(pixels(i, 2), pixels(i, 1)) = d(i);
for i = 1:length(obj.pixels)
image(obj.pixels(i, 2), obj.pixels(i, 1)) = d(i);
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions src/processing/postprocessing/InMemoryMAF.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
end

for roiIndex = 1:numel(rois)
pixelLists{end+1} = rois{roiIndex}.getPixelMask()';
pixelLists{end+1} = dataRepresentation.getDataIndiciesForROI(rois{roiIndex});
xyPos{end+1} = rois{roiIndex}.getPixelList();
end

Expand Down Expand Up @@ -106,7 +106,10 @@
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex-1}, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex-1}.getName() ' (MAF)']);
else
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex}, ...
dataROI = RegionOfInterest(dataRepresentation.width, dataRepresentation.height);
dataROI.addPixels(and(rois{pixelListIndex}.getPixelMask(), dataRepresentation.regionOfInterest.getPixelMask()));

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, dataROI, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex}.getName() ' (MAF)']);
end

Expand Down
7 changes: 5 additions & 2 deletions src/processing/postprocessing/InMemoryNMF.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
end

for roiIndex = 1:numel(rois)
pixelLists{end+1} = rois{roiIndex}.getPixelMask()';
pixelLists{end+1} = dataRepresentation.getDataIndiciesForROI(rois{roiIndex});
end

% Change L to now be the mean
Expand All @@ -96,7 +96,10 @@
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex-1}, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex-1}.getName() ' (NMF)']);
else
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex}, ...
dataROI = RegionOfInterest(dataRepresentation.width, dataRepresentation.height);
dataROI.addPixels(and(rois{pixelListIndex}.getPixelMask(), dataRepresentation.regionOfInterest.getPixelMask()));

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, dataROI, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex}.getName() ' (NMF)']);
end

Expand Down
9 changes: 6 additions & 3 deletions src/processing/postprocessing/InMemoryPCA.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
end

for roiIndex = 1:numel(rois)
pixelLists{end+1} = rois{roiIndex}.getPixelMask()';
pixelLists{end+1} = dataRepresentation.getDataIndiciesForROI(rois{roiIndex});
end

usePCA = false;
Expand Down Expand Up @@ -134,11 +134,14 @@
else
dataName = [rois{pixelListIndex}.getName() ' (PCA)'];

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex}, ...
dataROI = RegionOfInterest(dataRepresentation.width, dataRepresentation.height);
dataROI.addPixels(and(rois{pixelListIndex}.getPixelMask(), dataRepresentation.regionOfInterest.getPixelMask()));

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, dataROI, ...
dataRepresentation.isRowMajor, peakList, dataName);
end

latent{pixelListIndex}
% TODO: Incorporate this into the dataRepresentation
figure, plot(cumsum(latent{pixelListIndex})./sum(latent{pixelListIndex}));
title(['Explained ' dataName]);

Expand Down
7 changes: 5 additions & 2 deletions src/processing/postprocessing/InMemorypLSA.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
end

for roiIndex = 1:numel(rois)
pixelLists{end+1} = rois{roiIndex}.getPixelMask()';
pixelLists{end+1} = dataRepresentation.getDataIndiciesForROI(rois{roiIndex});
xyPos{end+1} = rois{roiIndex}.getPixelList();
end

Expand Down Expand Up @@ -195,7 +195,10 @@
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex-1}, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex-1}.getName() ' (pLSA)']);
else
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex}, ...
dataROI = RegionOfInterest(dataRepresentation.width, dataRepresentation.height);
dataROI.addPixels(and(rois{pixelListIndex}.getPixelMask(), dataRepresentation.regionOfInterest.getPixelMask()));

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, dataROI, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex}.getName() ' (pLSA)']);
end

Expand Down
10 changes: 1 addition & 9 deletions src/processing/postprocessing/KMeansClustering.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@
% curPixels = dataRepresentations{i}.regionOfInterest.pixelSelection;
kmeansImage = zeros(dataRepresentation.height, dataRepresentation.width); %size(curPixels));

pixels = dataRepresentations{i}.regionOfInterest.getPixelList();

if(dataRepresentations{i}.isRowMajor)
% Sort by y column, then by x column
pixels = sortrows(pixels, [2 1]);
else
% Sort by x column, then by y column
pixels = sortrows(pixels, [1 2]);
end
pixels = dataRepresentations{i}.getDataOrderedPixelList();

for j = 1:length(pixels)
kmeansImage(pixels(j, 2), pixels(j, 1)) = res(j);
Expand Down
5 changes: 4 additions & 1 deletion src/processing/postprocessing/MemoryEfficientPCA.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex-1}, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex-1}.getName() ' (ME PCA)']);
else
projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, rois{pixelListIndex}, ...
dataROI = RegionOfInterest(dataRepresentation.width, dataRepresentation.height);
dataROI.addPixels(and(rois{pixelListIndex}.getPixelMask(), dataRepresentation.regionOfInterest.getPixelMask()));

projectedDataRepresentation.setData(scores{pixelListIndex}, coeff{pixelListIndex}, dataROI, ...
dataRepresentation.isRowMajor, peakList, [rois{pixelListIndex}.getName() ' (ME PCA)']);
end

Expand Down

0 comments on commit fc9f729

Please sign in to comment.