use of gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method saveFilterSet.
private static void saveFilterSet(FilterSet filterSet, String filename) {
OutputStreamWriter out = null;
try {
List<FilterSet> list = new ArrayList<FilterSet>(1);
list.add(filterSet);
FileOutputStream fos = new FileOutputStream(filename);
out = new OutputStreamWriter(fos, "UTF-8");
// Use the instance so we can catch the exception
out.write(XmlUtils.prettyPrintXml(XStreamWrapper.getInstance().toXML(list)));
} catch (Exception e) {
IJ.log("Unable to save the filter sets to file: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// Ignore
}
}
}
}
use of gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method expandFilters.
/**
* If filters have been provided in FiltersSets of 3 then expand the filters into a set assuming the three represent
* min:max:increment.
*/
private void expandFilters() {
// Do not clear these when reading a new set of filters.
// The filters may be the same with slight modifications and so it is useful to keep the last settings.
//searchRangeMap.clear();
//stepSizeMap.clear();
long[] expanded = new long[filterList.size()];
String[] name = new String[expanded.length];
int c = 0;
boolean doIt = false;
for (FilterSet filterSet : filterList) {
if (filterSet.size() == 3 && filterSet.allSameType()) {
name[c] = filterSet.getName();
// Check we have min:max:increment by counting the combinations
Filter f1 = filterSet.getFilters().get(0);
Filter f2 = filterSet.getFilters().get(1);
Filter f3 = filterSet.getFilters().get(2);
int n = f1.getNumberOfParameters();
double[] parameters = new double[n];
double[] parameters2 = new double[n];
double[] increment = new double[n];
for (int i = 0; i < n; i++) {
parameters[i] = f1.getParameterValue(i);
parameters2[i] = f2.getParameterValue(i);
increment[i] = f3.getParameterValue(i);
}
long combinations = countCombinations(parameters, parameters2, increment);
if (combinations > 1) {
expanded[c] = combinations;
doIt = true;
}
}
c++;
}
if (!doIt)
return;
GenericDialog gd = new GenericDialog(TITLE);
gd.hideCancelButton();
StringBuilder sb = new StringBuilder("The filter file contains potential triples of min:max:increment.\n \n");
for (c = 0; c < expanded.length; c++) {
if (expanded[c] > 0) {
sb.append("Expand set [").append((c + 1)).append("]");
if (!Utils.isNullOrEmpty(name[c]))
sb.append(" ").append(name[c]);
sb.append(" to ").append(expanded[c]).append(" filters\n");
}
}
gd.addMessage(sb.toString());
gd.addCheckbox("Expand_filters", expandFilters);
gd.showDialog();
if (!gd.wasCanceled()) {
if (!(expandFilters = gd.getNextBoolean()))
return;
}
IJ.showStatus("Expanding filters ...");
List<FilterSet> filterList2 = new ArrayList<FilterSet>(filterList.size());
for (FilterSet filterSet : filterList) {
c = filterList2.size();
if (expanded[c] == 0) {
filterList2.add(filterSet);
continue;
}
Filter f1 = filterSet.getFilters().get(0);
Filter f2 = filterSet.getFilters().get(1);
Filter f3 = filterSet.getFilters().get(2);
final int n = f1.getNumberOfParameters();
double[] parameters = new double[n];
double[] parameters2 = new double[n];
double[] increment = new double[n];
for (int i = 0; i < n; i++) {
parameters[i] = f1.getParameterValue(i);
parameters2[i] = f2.getParameterValue(i);
increment[i] = f3.getParameterValue(i);
}
List<Filter> list = expandFilters(f1, parameters, parameters2, increment);
filterList2.add(new FilterSet(filterSet.getName(), list));
}
IJ.showStatus("");
filterList = filterList2;
Utils.log("Expanded input to %d filters in %s", countFilters(filterList), Utils.pleural(filterList.size(), "set"));
}
use of gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkSpotFit method saveFilters.
private boolean saveFilters(String filename, ArrayList<Filter> filters) {
ArrayList<FilterSet> filterList = new ArrayList<FilterSet>(1);
// Add Range keyword to identify as a range filter set
filterList.add(new FilterSet("Range", filters));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filename);
// Use the instance (not .toXML() method) to allow the exception to be caught
XStreamWrapper.getInstance().toXML(filterList, fos);
return true;
} catch (Exception e) {
IJ.log("Unable to save the filter set to file: " + e.getMessage());
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// Ignore
}
}
}
return false;
}
use of gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method score.
public SearchResult<FilterScore>[] score(double[][] points) {
ga_iteration++;
SimpleFilterScore max = es_optimum;
final FilterScoreResult[] scoreResults = scoreFilters(setStrength(new FilterSet(searchSpaceToFilters(points))), false);
if (scoreResults == null)
return null;
@SuppressWarnings("unchecked") SearchResult<FilterScore>[] scores = new SearchResult[scoreResults.length];
for (int index = 0; index < scoreResults.length; index++) {
final FilterScoreResult scoreResult = scoreResults[index];
final SimpleFilterScore result = new SimpleFilterScore(scoreResult, true, scoreResult.criteria >= minCriteria);
if (result.compareTo(max) < 0) {
max = result;
}
scores[index] = new SearchResult<FilterScore>(result.r.filter.getParameters(), result);
}
es_optimum = max;
// Add the best filter to the table
// This filter may not have been part of the scored subset so use the entire results set for reporting
DirectFilter filter = max.r.filter;
FractionClassificationResult r = scoreFilter(filter, minimalFilter, ga_resultsList, coordinateStore);
final StringBuilder text = createResult(filter, r);
add(text, ga_iteration);
gaWindow.append(text.toString());
return scores;
}
use of gdsc.smlm.results.filter.FilterSet in project GDSC-SMLM by aherbert.
the class BenchmarkFilterAnalysis method runAnalysis.
private void runAnalysis(List<FilterSet> filterSets, ComplexFilterScore optimum, double rangeReduction) {
plots.clear();
plots.ensureCapacity(plotTopN);
bestFilter.clear();
bestFilterOrder.clear();
getCoordinateStore();
filterAnalysisStopWatch = StopWatch.createStarted();
IJ.showStatus("Analysing filters ...");
int setNumber = 0;
DirectFilter currentOptimum = (optimum != null) ? optimum.r.filter : null;
for (FilterSet filterSet : filterSets) {
setNumber++;
if (filterAnalysis(filterSet, setNumber, currentOptimum, rangeReduction) < 0)
break;
}
filterAnalysisStopWatch.stop();
IJ.showProgress(1);
IJ.showStatus("");
final String timeString = filterAnalysisStopWatch.toString();
IJ.log("Filter analysis time : " + timeString);
}
Aggregations