diff --git a/dap/algos/spiana.py b/dap/algos/spiana.py index 5a8e935..5a80484 100644 --- a/dap/algos/spiana.py +++ b/dap/algos/spiana.py @@ -1,25 +1,42 @@ +import numpy as np -def calc_spi_analysis(results): + +def calc_spi_analysis(results, data): do_spi_analysis = results.get("do_spi_analysis", False) if not do_spi_analysis: return - for k in ("spi_limit", "roi_intensities_normalised"): + for k in ("spi_threshold_photon", "spi_threshold_hit", "roi_x1", "roi_x2", "roi_y1", "roi_y2"): if k not in results: return - spi_limit = results["spi_limit"] - roi_intensities_normalised = results["roi_intensities_normalised"] + spi_threshold_photon = results["spi_threshold_photon"] + spi_threshold_hit_percentage = results["spi_threshold_hit_percentage"] - number_of_spots = 0 - for index, (rin, sl) in enumerate(zip(roi_intensities_normalised, spi_limit)): - if rin >= sl: - number_of_spots += 25 * (index+1) + roi_x1 = results["roi_x1"] + roi_x2 = results["roi_x2"] + roi_y1 = results["roi_y1"] + roi_y2 = results["roi_y2"] - results["number_of_spots"] = number_of_spots + if len(roi_x1) == 0: + return - if number_of_spots > 0: - results["is_hit_frame"] = True + if not (len(roi_x1) == len(roi_x2) == len(roi_y1) == len(roi_y2)): + return + + nphotons = 0 + npixels = 0 + + for ix1, ix2, iy1, iy2 in zip(roi_x1, roi_x2, roi_y1, roi_y2): + data_roi = data[iy1:iy2, ix1:ix2] + nphotons += np.sum(data_roi > spi_threshold_photon) + npixels += data_roi.size + + photon_percentage = nphotons / npixels * 100 + hit = (photon_percentage > spi_threshold_hit_percentage) + + results["number_of_spots"] = photon_percentage + results["is_hit_frame"] = hit diff --git a/dap/worker.py b/dap/worker.py index 63fdca3..d32f0d4 100644 --- a/dap/worker.py +++ b/dap/worker.py @@ -111,7 +111,7 @@ def work(backend_address, accumulator_host, accumulator_port, visualisation_host calc_mask_pixels(pfimage, pixel_mask_pf) # changes pfimage in place calc_apply_threshold(results, pfimage) # changes pfimage in place calc_roi(results, pfimage, pixel_mask_pf) - calc_spi_analysis(results) + calc_spi_analysis(results, pfimage) calc_peakfinder_analysis(results, pfimage, pixel_mask_pf) # ???