use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.
the class ImageJImagePeakResultsTest method addPeakResults.
private static void addPeakResults(ImageJImagePeakResults results, float[] x, float[] y, float[] value) {
final LocalList<PeakResult> list = new LocalList<>(x.length);
for (int i = 0; i < x.length; i++) {
list.add(new PeakResult(x[i], y[i], value[i]));
}
results.addAll(list);
}
use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.
the class PsfCombiner method createImageList.
/**
* Creates the image list. Images must be greyscale, square and a stack of a single channel.
*
* @return the list
*/
public static List<String> createImageList() {
final List<String> titles = new LocalList<>();
final int[] ids = WindowManager.getIDList();
if (ids != null) {
for (final int id : ids) {
final ImagePlus imp = WindowManager.getImage(id);
if (imp != null && // Image must be greyscale
(imp.getType() == ImagePlus.GRAY8 || imp.getType() == ImagePlus.GRAY16 || imp.getType() == ImagePlus.GRAY32) && // Image must be square and a stack of a single channel
(imp.getWidth() == imp.getHeight() && imp.getNChannels() == 1) && // Check if these are PSF images created by the SMLM plugins
containsPsf(imp)) {
titles.add(imp.getTitle());
}
}
}
return titles;
}
use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.
the class TraceExporter method exportVbSpt.
@SuppressWarnings("resource")
private void exportVbSpt(MemoryPeakResults results) {
// vbSPT file format:
// https://sourceforge.net/projects/vbspt/
// Matlab matrix file (.mat) containing at least one variable that is a cell
// array where each element, representing a trajectory, is a matrix
// where the rows define the coordinates in one, two or three dimensions
// in subsequent timesteps. The number of dimensions to be used for the
// analysis will be set by the runinputfile.
// The units are arbitrary but vbSPT starting estimates must be in the same
// units. Either nm or μm are recommended.
// 3 columns for n rows of localisations
// 1. x coordinate (μm)
// 2. y coordinate (μm)
// 3. z coordinate (μm)
//
// Note: An extra column is added containing the frame. This allows results to
// be uniquely identified using frame,x,y,z
// Count the IDs. Each new result ID will increment the count.
final FrameCounter idCounter = new FrameCounter(results.getFirst().getId() - 1);
results.forEach((PeakResultProcedure) result -> {
if (idCounter.advance(result.getId())) {
idCounter.increment();
}
});
// Create the cell array as 1xN
final Cell out = Mat5.newCell(1, idCounter.getCount());
// This will reset the counter to zero and ensure the current frame does not match
// in the event of a single track
idCounter.advanceAndReset(idCounter.currentFrame() + 1);
final boolean is3d = results.is3D();
// Write the tracks
final LocalList<double[]> list = new LocalList<>();
results.forEach(DistanceUnit.UM, (XyzrResultProcedure) (x, y, z, result) -> {
if (idCounter.advance(result.getId())) {
addTrack(out, idCounter.getCount() - 1, list, is3d);
idCounter.increment();
list.clear();
}
list.add(new double[] { x, y, z, result.getFrame() });
});
addTrack(out, idCounter.getCount() - 1, list, is3d);
try (MatFile matFile = Mat5.newMatFile()) {
matFile.addArray("tracks", out);
Mat5.writeToFile(matFile, Paths.get(settings.directory, results.getName() + ".mat").toFile());
} catch (final IOException ex) {
handleException(ex);
}
}
use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.
the class TcPalmAnalysis method analyseRois.
/**
* Analyses all the ROIs in the ROI manager.
*
* @param event the event
*/
private void analyseRois(ActionEvent event) {
final RoiManager manager = RoiManager.getInstance();
if (manager == null) {
IJ.error(TITLE, "ROI manager is not open");
return;
}
final LocalList<Roi> rois = Arrays.stream(manager.getRoisAsArray()).filter(Roi::isArea).collect(LocalCollectors.toLocalList());
if (rois.isEmpty()) {
IJ.error(TITLE, "No area ROIs");
return;
}
// Check for overlaps.
if (!settings.getDisableOverlapCheck() && anyOverlap(rois)) {
final GenericDialog gd = new GenericDialog(TITLE);
gd.addMessage(TextUtils.wrap("WARNING - Bounding rectangles of ROIs overlap. You can verify " + "the ROIs on the image using the ROI manager 'Show all' function.", 80));
gd.setOKLabel("Continue");
gd.showDialog();
if (gd.wasCanceled()) {
return;
}
}
// For each ROI:
// - Extract the current groups
// - Build the cumulative count plot
// - Identify the bursts
// - Extract ClusterData for each burst
final TcPalmAnalysisSettings settings = this.settings.build();
final LocalList<ClusterData> allClusters = rois.parallelStream().map(roi -> {
final Rectangle2D scaledBounds = createScaledBounds(roi);
final BiPredicate<ClusterData, Rectangle2D> filter = createSelectionFilter(roi, settings);
// Filter all cluster groups
final LocalList<ClusterData> clusters = new LocalList<>();
clusterData.forEach(c -> {
if (filter.test(c, scaledBounds)) {
clusters.add(c);
}
});
// Extract activation bursts
final CumulativeCountData countData = createCumulativeCountData(clusters, false);
final LocalList<int[]> bursts = runBurstAnalysis(settings, countData);
final LocalList<LocalList<PeakResult>> burstLocalisations = createBurstLocalisations(clusters, bursts);
clusters.clear();
burstLocalisations.forEach(list -> {
final ClusterData d = new ClusterData(clusters.size() + 1, list);
// Save this for analysis
d.sourceRoi = roi;
d.getArea();
clusters.add(d);
});
return clusters;
}).collect(LocalList::new, LocalList::addAll, LocalList::addAll);
// Reorder
final Counter count = new Counter();
allClusters.forEach(c -> c.id = count.incrementAndGet());
// Display in a table
final ClusterDataTableModelFrame frame = createAllClustersTable();
frame.getModel().setData(allClusters, dataCalibration);
// Allow the results to be repeated
frame.selectedAction = clusters -> {
// Expecting a single cluster. No clusters occurs when the table (and selection) is cleared.
if (clusters.size() == 1) {
final ClusterData c = clusters.get(0);
// Push the correct ROI and settings to the analysis action.
// We do not directly update the ROI or dialog settings as
// these trigger events that are processed to add work with a delay.
// Updating them at the end should generate events that are
// ignored when finally executed as the ROI/settings should be the same.
addWork(0, c.sourceRoi, settings, () -> {
// When analysis has finished update the settings and image ROI.
image.getImagePlus().setRoi(c.sourceRoi);
darkTimeToleranceTextField.setText(Integer.toString(settings.getDarkTimeTolerance()));
minClusterSizeTextField.setText(Integer.toString(settings.getMinClusterSize()));
// When analysis has finished the cluster should be selected in the
// current clusters table.
final ClusterDataTableModelFrame currentClusters = currentClustersTable.get();
if (currentClusters != null) {
currentClusters.select(c);
}
});
}
};
// Show histogram of cluster size/duration
reportAnalysis(settings, allClusters, dataCalibration);
// Save clusters to memory
final Trace[] traces = allClusters.stream().map(c -> {
final Trace t = new Trace();
t.setId(c.id);
c.results.forEach(t::add);
return t;
}).toArray(Trace[]::new);
TraceMolecules.saveResults(results, traces, "TC PALM");
IJ.showStatus(TITLE + ": " + TextUtils.pleural(allClusters.size(), "cluster"));
}
use of uk.ac.sussex.gdsc.core.utils.LocalList in project GDSC-SMLM by aherbert.
the class TcPalmAnalysis method createClusterData.
@SuppressWarnings("null")
private static LocalList<ClusterData> createClusterData(MemoryPeakResults results) {
results.sort(IdFramePeakResultComparator.INSTANCE);
final LocalList<ClusterData> clusterData = new LocalList<>();
final FrameCounter counter = new FrameCounter(results.getFirst().getId() - 1);
ClusterData data = null;
final int size = results.size();
for (int i = 0; i < size; i++) {
final PeakResult result = results.get(i);
if (counter.advance(result.getId())) {
clusterData.add(data);
data = new ClusterData(result);
} else {
data.add(result);
}
}
// Final cluster
clusterData.add(data);
// Remove the first null object and compact the frame arrays
clusterData.remove(0);
clusterData.forEach(ClusterData::finish);
// Sort by time then cluster ID
clusterData.sort((c1, c2) -> {
final int result = Integer.compare(c1.start, c2.start);
if (result != 0) {
return result;
}
// }
return Integer.compare(c1.id, c2.id);
});
return clusterData;
}
Aggregations