Search in sources :

Example 1 with UnicodeReader

use of gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class SMLMTools method createFrame.

private boolean createFrame() {
    // Locate all the GDSC SMLM plugins using the plugins.config:
    InputStream readmeStream = getToolsPluginsConfig();
    ij.Menus.installPlugin("", ij.Menus.PLUGINS_MENU, "-", "", IJ.getInstance());
    // Read into memory
    ArrayList<String[]> plugins = new ArrayList<String[]>();
    int gaps = 0;
    BufferedReader input = null;
    try {
        input = new BufferedReader(new UnicodeReader(readmeStream, null));
        String line;
        while ((line = input.readLine()) != null) {
            if (line.startsWith("#"))
                continue;
            String[] tokens = line.split(",");
            if (tokens.length == 3) {
                // Only copy the entries from the Plugins menu
                if (!ignore(tokens)) {
                    if (!plugins.isEmpty()) {
                        // Multiple gaps indicates a new column
                        if (gaps > 1) {
                            plugins.add(new String[] { "next", "" });
                        }
                    }
                    gaps = 0;
                    plugins.add(new String[] { tokens[1].trim(), tokens[2].trim() });
                }
            } else
                gaps++;
            // Put a spacer between plugins if specified
            if ((tokens.length == 2 && tokens[0].startsWith("Plugins") && tokens[1].trim().equals("\"-\"")) || line.length() == 0) {
                plugins.add(new String[] { "spacer", "" });
            }
        }
    } catch (IOException e) {
    // Ignore 
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    if (plugins.isEmpty())
        return false;
    // Arrange on a grid
    Panel mainPanel = new Panel();
    GridBagLayout grid = new GridBagLayout();
    mainPanel.setLayout(grid);
    add(mainPanel);
    addSpacer = false;
    int col = 0, row = 0;
    for (String[] plugin : plugins) {
        if (plugin[0].equals("next")) {
            col++;
            row = 0;
        } else if (plugin[0].equals("spacer"))
            addSpacer = true;
        else
            row = addPlugin(mainPanel, grid, plugin[0], plugin[1], col, row);
    }
    return true;
}
Also used : Panel(java.awt.Panel) GridBagLayout(java.awt.GridBagLayout) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) Point(java.awt.Point)

Example 2 with UnicodeReader

use of gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class CreateData method createPhotonDistribution.

/**
	 * @return A photon distribution loaded from a file of floating-point values with the specified population mean.
	 */
private RealDistribution createPhotonDistribution() {
    if (PHOTON_DISTRIBUTION[PHOTON_CUSTOM].equals(settings.photonDistribution)) {
        // Get the distribution file
        String filename = Utils.getFilename("Photon_distribution", settings.photonDistributionFile);
        if (filename != null) {
            settings.photonDistributionFile = filename;
            try {
                InputStream is = new FileInputStream(new File(settings.photonDistributionFile));
                BufferedReader in = new BufferedReader(new UnicodeReader(is, null));
                StoredDataStatistics stats = new StoredDataStatistics();
                try {
                    String str = null;
                    double val = 0.0d;
                    while ((str = in.readLine()) != null) {
                        val = Double.parseDouble(str);
                        stats.add(val);
                    }
                } finally {
                    in.close();
                }
                if (stats.getSum() > 0) {
                    // Update the statistics to the desired mean.
                    double scale = (double) settings.photonsPerSecond / stats.getMean();
                    double[] values = stats.getValues();
                    for (int i = 0; i < values.length; i++) values[i] *= scale;
                    // TODO - Investigate the limits of this distribution. 
                    // How far above and below the input data will values be generated.
                    // Create the distribution using the recommended number of bins
                    final int binCount = stats.getN() / 10;
                    EmpiricalDistribution dist = new EmpiricalDistribution(binCount, createRandomGenerator());
                    dist.load(values);
                    return dist;
                }
            } catch (IOException e) {
            // Ignore
            } catch (NullArgumentException e) {
            // Ignore 
            } catch (NumberFormatException e) {
            // Ignore
            }
        }
        Utils.log("Failed to load custom photon distribution from file: %s. Default to fixed.", settings.photonDistributionFile);
    } else if (PHOTON_DISTRIBUTION[PHOTON_UNIFORM].equals(settings.photonDistribution)) {
        if (settings.photonsPerSecond < settings.photonsPerSecondMaximum) {
            UniformRealDistribution dist = new UniformRealDistribution(createRandomGenerator(), settings.photonsPerSecond, settings.photonsPerSecondMaximum);
            return dist;
        }
    } else if (PHOTON_DISTRIBUTION[PHOTON_GAMMA].equals(settings.photonDistribution)) {
        final double scaleParameter = settings.photonsPerSecond / settings.photonShape;
        GammaDistribution dist = new GammaDistribution(createRandomGenerator(), settings.photonShape, scaleParameter, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
        return dist;
    } else if (PHOTON_DISTRIBUTION[PHOTON_CORRELATED].equals(settings.photonDistribution)) {
        // No distribution required
        return null;
    }
    settings.photonDistribution = PHOTON_DISTRIBUTION[PHOTON_FIXED];
    return null;
}
Also used : EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StoredDataStatistics(gdsc.core.utils.StoredDataStatistics) UniformRealDistribution(org.apache.commons.math3.distribution.UniformRealDistribution) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) CustomGammaDistribution(org.apache.commons.math3.distribution.CustomGammaDistribution)

Example 3 with UnicodeReader

use of gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class BenchmarkFilterAnalysis method readFilterSets.

@SuppressWarnings("unchecked")
private List<FilterSet> readFilterSets() {
    if (extraOptions && BenchmarkSpotFit.multiFilter != null) {
        IDirectFilter f = BenchmarkSpotFit.multiFilter.getFilter();
        if (f instanceof DirectFilter) {
            GenericDialog gd = new GenericDialog(TITLE);
            gd.addMessage("Use an identical filter to " + BenchmarkSpotFit.TITLE);
            gd.enableYesNoCancel();
            gd.hideCancelButton();
            gd.showDialog();
            if (gd.wasOKed()) {
                setLastFile(null);
                List<FilterSet> filterSets = new ArrayList<FilterSet>(1);
                List<Filter> filters = new ArrayList<Filter>(1);
                filters.add((DirectFilter) f);
                FilterSet filterSet = new FilterSet(filters);
                filterSets.add(filterSet);
                resetParametersFromFitting();
                createResultsPrefix2();
                return filterSets;
            }
        }
    }
    GlobalSettings gs = SettingsManager.loadSettings();
    FilterSettings filterSettings = gs.getFilterSettings();
    String filename = Utils.getFilename("Filter_File", filterSettings.filterSetFilename);
    if (filename != null) {
        IJ.showStatus("Reading filters ...");
        filterSettings.filterSetFilename = filename;
        // Allow the filters to be cached
        if (isSameFile(filename)) {
            GenericDialog gd = new GenericDialog(TITLE);
            gd.hideCancelButton();
            gd.addMessage("The same filter file was selected.");
            gd.addCheckbox("Re-use_filters", reUseFilters);
            gd.showDialog();
            if (!gd.wasCanceled()) {
                if ((reUseFilters = gd.getNextBoolean())) {
                    SettingsManager.saveSettings(gs);
                    return filterList;
                }
            }
        }
        BufferedReader input = null;
        setLastFile(null);
        try {
            FileInputStream fis = new FileInputStream(filename);
            input = new BufferedReader(new UnicodeReader(fis, null));
            Object o = XStreamWrapper.getInstance().fromXML(input);
            if (o != null && o instanceof List<?>) {
                SettingsManager.saveSettings(gs);
                List<FilterSet> filterSets = (List<FilterSet>) o;
                if (containsStandardFilters(filterSets)) {
                    IJ.log("Filter sets must contain 'Direct' filters");
                    return null;
                }
                // Check they are not empty lists
                List<FilterSet> filterSets2 = new LinkedList<FilterSet>();
                for (FilterSet filterSet : filterSets) {
                    if (filterSet.size() != 0) {
                        filterSets2.add(filterSet);
                    } else {
                        IJ.log("Filter set empty: " + filterSet.getName());
                    }
                }
                if (filterSets2.isEmpty()) {
                    IJ.log("All Filter sets are empty");
                    return null;
                }
                // Maintain the same list type
                filterSets.clear();
                filterSets.addAll(filterSets2);
                filterList = filterSets;
                // Option to enumerate filters
                expandFilters();
                setLastFile(filename);
                return filterList;
            }
            IJ.log("No filter sets defined in the specified file: " + filename);
        } catch (Exception e) {
            IJ.log("Unable to load the filter sets from file: " + e.getMessage());
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                // Ignore
                }
            }
            IJ.showStatus("");
        }
    }
    return null;
}
Also used : FilterSet(gdsc.smlm.results.filter.FilterSet) IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) ArrayList(java.util.ArrayList) IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) GlobalSettings(gdsc.smlm.ij.settings.GlobalSettings) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LinkedList(java.util.LinkedList) IOException(java.io.IOException) IDirectFilter(gdsc.smlm.results.filter.IDirectFilter) DirectFilter(gdsc.smlm.results.filter.DirectFilter) Filter(gdsc.smlm.results.filter.Filter) MultiPathFilter(gdsc.smlm.results.filter.MultiPathFilter) MaximaSpotFilter(gdsc.smlm.filters.MaximaSpotFilter) GenericDialog(ij.gui.GenericDialog) NonBlockingGenericDialog(ij.gui.NonBlockingGenericDialog) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) FilterSettings(gdsc.smlm.ij.settings.FilterSettings)

Example 4 with UnicodeReader

use of gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class PeakResultsReader method readRapidSTORM.

private MemoryPeakResults readRapidSTORM() {
    MemoryPeakResults results = createResults();
    results.setName(new File(filename).getName());
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(filename);
        FileChannel channel = fis.getChannel();
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int errors = 0;
        // Skip the header
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) != '#') {
                // This is the first record
                if (!addRapidSTORMResult(results, line))
                    errors = 1;
                break;
            }
        }
        int c = 0;
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) == '#')
                continue;
            if (!addRapidSTORMResult(results, line)) {
                if (++errors >= 10) {
                    break;
                }
            }
            if (++c % 512 == 0)
                showProgress(channel);
        }
    } catch (IOException e) {
    // ignore
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    return results;
}
Also used : FileChannel(java.nio.channels.FileChannel) BufferedReader(java.io.BufferedReader) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with UnicodeReader

use of gdsc.core.utils.UnicodeReader in project GDSC-SMLM by aherbert.

the class PeakResultsReader method readText.

private MemoryPeakResults readText() {
    MemoryPeakResults results = createResults();
    BufferedReader input = null;
    try {
        FileInputStream fis = new FileInputStream(filename);
        FileChannel channel = fis.getChannel();
        input = new BufferedReader(new UnicodeReader(fis, null));
        String line;
        int errors = 0;
        // Read different versions
        int version = (readAmplitude) ? 1 : 2;
        // Skip the header
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) != '#') {
                // This is the first record
                if (!addPeakResult(results, line, version))
                    errors = 1;
                break;
            }
        }
        int c = 0;
        while ((line = input.readLine()) != null) {
            if (line.length() == 0)
                continue;
            if (line.charAt(0) == '#')
                continue;
            if (!addPeakResult(results, line, version)) {
                if (++errors >= 10) {
                    break;
                }
            }
            if (++c % 512 == 0)
                showProgress(channel);
        }
    } catch (IOException e) {
    // ignore
    } finally {
        try {
            if (input != null)
                input.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    return results;
}
Also used : FileChannel(java.nio.channels.FileChannel) BufferedReader(java.io.BufferedReader) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

UnicodeReader (gdsc.core.utils.UnicodeReader)16 BufferedReader (java.io.BufferedReader)16 IOException (java.io.IOException)16 FileInputStream (java.io.FileInputStream)14 File (java.io.File)6 FileChannel (java.nio.channels.FileChannel)5 ArrayList (java.util.ArrayList)5 InputStream (java.io.InputStream)4 Pattern (java.util.regex.Pattern)4 GenericDialog (ij.gui.GenericDialog)3 InputMismatchException (java.util.InputMismatchException)3 LinkedList (java.util.LinkedList)3 NoSuchElementException (java.util.NoSuchElementException)3 Scanner (java.util.Scanner)3 FilterSettings (gdsc.smlm.ij.settings.FilterSettings)2 GlobalSettings (gdsc.smlm.ij.settings.GlobalSettings)2 Point (java.awt.Point)2 List (java.util.List)2 ClusterPoint (gdsc.core.clustering.ClusterPoint)1 Statistics (gdsc.core.utils.Statistics)1