nanopyx.core.analysis.parameter_sweep

  1import numpy as np
  2from tqdm import tqdm
  3from nanopyx.core.transform.error_map import ErrorMap
  4from nanopyx.core.analysis.frc import FIRECalculator
  5from nanopyx.core.analysis.decorr import DecorrAnalysis
  6from nanopyx.core.transform._le_esrrf import eSRRF
  7from nanopyx.core.transform.sr_temporal_correlations import calculate_eSRRF_temporal_correlations
  8
  9
 10# TODO double check this implementation and confirm that this gives the same results as NanoJ-eSRRF
 11class ParameterSweep:
 12    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
 13        self.doErrorMapping = doErrorMapping
 14        self.doFRCMapping = doFRCMapping
 15
 16    # check for image dimensions in the method
 17    def run(
 18        self,
 19        im: np.array,
 20        magnification: int,
 21        sensitivity_array: list,
 22        radius_array: list,
 23        temporal_correlation: str = "AVG",
 24        use_decorr: bool = False,
 25        n_frames=None
 26    ):
 27        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
 28        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
 29        s_size = len(sensitivity_array)
 30        r_size = len(radius_array)
 31
 32        with tqdm(total=s_size*r_size, desc="Parameters pairs", unit="pairs") as progress_bar:
 33            for s in range(s_size):
 34                for r in range(r_size):
 35                    rgc_map = eSRRF(verbose=True).run(
 36                        im, magnification=magnification, radius=radius_array[r], sensitivity=sensitivity_array[s]
 37                    )
 38                    if n_frames is None:
 39                        if self.doErrorMapping:
 40                            reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
 41                            RSP_map[s, r] = self.calculate_rsp(im, reconstruction)
 42                        if self.doFRCMapping:
 43                            if use_decorr:
 44                                decorr = DecorrAnalysis()
 45                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
 46                                FRC_map[s, r] = decorr.resolution
 47                            else:
 48                                rgc_map_odd = rgc_map[1::2, :, :]
 49                                rgc_map_even = rgc_map[::2, :, :]
 50                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 51                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 52                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 53                    else:
 54                        if im.shape[0] % n_frames != 0:
 55                            n_slices = im.shape[0] // n_frames + 1
 56                        else:
 57                            n_slices = im.shape[0] // n_frames
 58
 59                        RSP_map = np.zeros((n_slices, len(sensitivity_array), len(radius_array)))
 60                        for i in range(n_slices):
 61                            if self.doErrorMapping:
 62                                reconstruction = calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation)
 63                                sliced_image = im[i*n_frames:(i+1)*n_frames]
 64                                print(sliced_image.shape, rgc_map[i*n_frames:(i+1)*n_frames].shape)
 65                                RSP_map[i, s, r] = self.calculate_rsp(sliced_image, reconstruction)
 66                                
 67                            # if self.doFRCMapping:
 68                            #     if use_decorr:
 69                            #         decorr = DecorrAnalysis()
 70                            #         decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation))
 71                            #         FRC_map[i, s, r] = decorr.resolution
 72                            #     else:
 73                            #         rgc_map_odd = rgc_map[i*n_frames+1:(i+1)*n_frames:2, :, :]
 74                            #         rgc_map_even = rgc_map[i*n_frames:(i+1)*n_frames:2, :, :]
 75                            #         reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 76                            #         reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 77                            #         FRC_map[i, s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 78                        RSP_map = np.mean(RSP_map, axis=0)
 79                    
 80                        if self.doFRCMapping:
 81                            if use_decorr:
 82                                decorr = DecorrAnalysis()
 83                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
 84                                FRC_map[s, r] = decorr.resolution
 85                            else:
 86                                rgc_map_odd = rgc_map[1::2, :, :]
 87                                rgc_map_even = rgc_map[::2, :, :]
 88                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 89                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 90                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 91                    progress_bar.update()
 92
 93        print(RSP_map)
 94        print(FRC_map)
 95        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
 96
 97        return QnR
 98
 99    def calculate_rsp(self, im, reconstruction):
100        error_map = ErrorMap()
101        error_map.optimise(np.mean(im, axis=0), reconstruction)
102        return error_map.getRSP()
103
104    def calculate_frc(self, im_odd, im_even):
105        frc_calculator = FIRECalculator()
106        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
107        return fire_nb
108
109    def logistic_image_conversion(self, im, min_val=None, max_val=None):  # max and min in nm
110        # trying change to min and max vals to be taken from "im" instead of user defined
111
112        if min_val is None:
113            min_val = np.min(im)
114            print(min_val)
115        if max_val is None:
116            max_val = np.max(im)
117            print(max_val)
118
119        M1 = 0.075
120        M2 = 0.925
121        A1 = np.log((1 - M1) / M1)
122        A2 = np.log((1 - M2) / M2)
123        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
124        k = 1 / (x0 - max_val) * A1
125
126        im_out = []
127        for image in im:
128            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
129            im_out.append(normalized_image)
130
131        return np.asarray(im_out)
132
133    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
134        print(RSP.shape, FRC.shape)
135        assert RSP.shape == FRC.shape
136        nFRC = self.logistic_image_conversion(FRC)
137        print(nFRC)
138        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC))
139        return QnR
class ParameterSweep:
 12class ParameterSweep:
 13    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
 14        self.doErrorMapping = doErrorMapping
 15        self.doFRCMapping = doFRCMapping
 16
 17    # check for image dimensions in the method
 18    def run(
 19        self,
 20        im: np.array,
 21        magnification: int,
 22        sensitivity_array: list,
 23        radius_array: list,
 24        temporal_correlation: str = "AVG",
 25        use_decorr: bool = False,
 26        n_frames=None
 27    ):
 28        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
 29        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
 30        s_size = len(sensitivity_array)
 31        r_size = len(radius_array)
 32
 33        with tqdm(total=s_size*r_size, desc="Parameters pairs", unit="pairs") as progress_bar:
 34            for s in range(s_size):
 35                for r in range(r_size):
 36                    rgc_map = eSRRF(verbose=True).run(
 37                        im, magnification=magnification, radius=radius_array[r], sensitivity=sensitivity_array[s]
 38                    )
 39                    if n_frames is None:
 40                        if self.doErrorMapping:
 41                            reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
 42                            RSP_map[s, r] = self.calculate_rsp(im, reconstruction)
 43                        if self.doFRCMapping:
 44                            if use_decorr:
 45                                decorr = DecorrAnalysis()
 46                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
 47                                FRC_map[s, r] = decorr.resolution
 48                            else:
 49                                rgc_map_odd = rgc_map[1::2, :, :]
 50                                rgc_map_even = rgc_map[::2, :, :]
 51                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 52                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 53                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 54                    else:
 55                        if im.shape[0] % n_frames != 0:
 56                            n_slices = im.shape[0] // n_frames + 1
 57                        else:
 58                            n_slices = im.shape[0] // n_frames
 59
 60                        RSP_map = np.zeros((n_slices, len(sensitivity_array), len(radius_array)))
 61                        for i in range(n_slices):
 62                            if self.doErrorMapping:
 63                                reconstruction = calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation)
 64                                sliced_image = im[i*n_frames:(i+1)*n_frames]
 65                                print(sliced_image.shape, rgc_map[i*n_frames:(i+1)*n_frames].shape)
 66                                RSP_map[i, s, r] = self.calculate_rsp(sliced_image, reconstruction)
 67                                
 68                            # if self.doFRCMapping:
 69                            #     if use_decorr:
 70                            #         decorr = DecorrAnalysis()
 71                            #         decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation))
 72                            #         FRC_map[i, s, r] = decorr.resolution
 73                            #     else:
 74                            #         rgc_map_odd = rgc_map[i*n_frames+1:(i+1)*n_frames:2, :, :]
 75                            #         rgc_map_even = rgc_map[i*n_frames:(i+1)*n_frames:2, :, :]
 76                            #         reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 77                            #         reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 78                            #         FRC_map[i, s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 79                        RSP_map = np.mean(RSP_map, axis=0)
 80                    
 81                        if self.doFRCMapping:
 82                            if use_decorr:
 83                                decorr = DecorrAnalysis()
 84                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
 85                                FRC_map[s, r] = decorr.resolution
 86                            else:
 87                                rgc_map_odd = rgc_map[1::2, :, :]
 88                                rgc_map_even = rgc_map[::2, :, :]
 89                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
 90                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
 91                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
 92                    progress_bar.update()
 93
 94        print(RSP_map)
 95        print(FRC_map)
 96        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
 97
 98        return QnR
 99
100    def calculate_rsp(self, im, reconstruction):
101        error_map = ErrorMap()
102        error_map.optimise(np.mean(im, axis=0), reconstruction)
103        return error_map.getRSP()
104
105    def calculate_frc(self, im_odd, im_even):
106        frc_calculator = FIRECalculator()
107        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
108        return fire_nb
109
110    def logistic_image_conversion(self, im, min_val=None, max_val=None):  # max and min in nm
111        # trying change to min and max vals to be taken from "im" instead of user defined
112
113        if min_val is None:
114            min_val = np.min(im)
115            print(min_val)
116        if max_val is None:
117            max_val = np.max(im)
118            print(max_val)
119
120        M1 = 0.075
121        M2 = 0.925
122        A1 = np.log((1 - M1) / M1)
123        A2 = np.log((1 - M2) / M2)
124        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
125        k = 1 / (x0 - max_val) * A1
126
127        im_out = []
128        for image in im:
129            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
130            im_out.append(normalized_image)
131
132        return np.asarray(im_out)
133
134    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
135        print(RSP.shape, FRC.shape)
136        assert RSP.shape == FRC.shape
137        nFRC = self.logistic_image_conversion(FRC)
138        print(nFRC)
139        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC))
140        return QnR
ParameterSweep(doErrorMapping: bool = True, doFRCMapping: bool = True)
13    def __init__(self, doErrorMapping: bool = True, doFRCMapping: bool = True):
14        self.doErrorMapping = doErrorMapping
15        self.doFRCMapping = doFRCMapping
doErrorMapping
doFRCMapping
def run( self, im: <built-in function array>, magnification: int, sensitivity_array: list, radius_array: list, temporal_correlation: str = 'AVG', use_decorr: bool = False, n_frames=None):
18    def run(
19        self,
20        im: np.array,
21        magnification: int,
22        sensitivity_array: list,
23        radius_array: list,
24        temporal_correlation: str = "AVG",
25        use_decorr: bool = False,
26        n_frames=None
27    ):
28        RSP_map = np.zeros((len(sensitivity_array), len(radius_array)))
29        FRC_map = np.zeros((len(sensitivity_array), len(radius_array)))
30        s_size = len(sensitivity_array)
31        r_size = len(radius_array)
32
33        with tqdm(total=s_size*r_size, desc="Parameters pairs", unit="pairs") as progress_bar:
34            for s in range(s_size):
35                for r in range(r_size):
36                    rgc_map = eSRRF(verbose=True).run(
37                        im, magnification=magnification, radius=radius_array[r], sensitivity=sensitivity_array[s]
38                    )
39                    if n_frames is None:
40                        if self.doErrorMapping:
41                            reconstruction = calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation)
42                            RSP_map[s, r] = self.calculate_rsp(im, reconstruction)
43                        if self.doFRCMapping:
44                            if use_decorr:
45                                decorr = DecorrAnalysis()
46                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
47                                FRC_map[s, r] = decorr.resolution
48                            else:
49                                rgc_map_odd = rgc_map[1::2, :, :]
50                                rgc_map_even = rgc_map[::2, :, :]
51                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
52                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
53                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
54                    else:
55                        if im.shape[0] % n_frames != 0:
56                            n_slices = im.shape[0] // n_frames + 1
57                        else:
58                            n_slices = im.shape[0] // n_frames
59
60                        RSP_map = np.zeros((n_slices, len(sensitivity_array), len(radius_array)))
61                        for i in range(n_slices):
62                            if self.doErrorMapping:
63                                reconstruction = calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation)
64                                sliced_image = im[i*n_frames:(i+1)*n_frames]
65                                print(sliced_image.shape, rgc_map[i*n_frames:(i+1)*n_frames].shape)
66                                RSP_map[i, s, r] = self.calculate_rsp(sliced_image, reconstruction)
67                                
68                            # if self.doFRCMapping:
69                            #     if use_decorr:
70                            #         decorr = DecorrAnalysis()
71                            #         decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map[i*n_frames:(i+1)*n_frames], temporal_correlation))
72                            #         FRC_map[i, s, r] = decorr.resolution
73                            #     else:
74                            #         rgc_map_odd = rgc_map[i*n_frames+1:(i+1)*n_frames:2, :, :]
75                            #         rgc_map_even = rgc_map[i*n_frames:(i+1)*n_frames:2, :, :]
76                            #         reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
77                            #         reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
78                            #         FRC_map[i, s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
79                        RSP_map = np.mean(RSP_map, axis=0)
80                    
81                        if self.doFRCMapping:
82                            if use_decorr:
83                                decorr = DecorrAnalysis()
84                                decorr.run_analysis(calculate_eSRRF_temporal_correlations(rgc_map, temporal_correlation))
85                                FRC_map[s, r] = decorr.resolution
86                            else:
87                                rgc_map_odd = rgc_map[1::2, :, :]
88                                rgc_map_even = rgc_map[::2, :, :]
89                                reconstruction_odd = calculate_eSRRF_temporal_correlations(rgc_map_odd, temporal_correlation)
90                                reconstruction_even = calculate_eSRRF_temporal_correlations(rgc_map_even, temporal_correlation)
91                                FRC_map[s, r] = self.calculate_frc(reconstruction_odd, reconstruction_even)
92                    progress_bar.update()
93
94        print(RSP_map)
95        print(FRC_map)
96        QnR = self.calculate_qnr_score(RSP_map, FRC_map)
97
98        return QnR
def calculate_rsp(self, im, reconstruction):
100    def calculate_rsp(self, im, reconstruction):
101        error_map = ErrorMap()
102        error_map.optimise(np.mean(im, axis=0), reconstruction)
103        return error_map.getRSP()
def calculate_frc(self, im_odd, im_even):
105    def calculate_frc(self, im_odd, im_even):
106        frc_calculator = FIRECalculator()
107        fire_nb = frc_calculator.calculate_fire_number(np.asarray(im_odd), np.asarray(im_even))
108        return fire_nb
def logistic_image_conversion(self, im, min_val=None, max_val=None):
110    def logistic_image_conversion(self, im, min_val=None, max_val=None):  # max and min in nm
111        # trying change to min and max vals to be taken from "im" instead of user defined
112
113        if min_val is None:
114            min_val = np.min(im)
115            print(min_val)
116        if max_val is None:
117            max_val = np.max(im)
118            print(max_val)
119
120        M1 = 0.075
121        M2 = 0.925
122        A1 = np.log((1 - M1) / M1)
123        A2 = np.log((1 - M2) / M2)
124        x0 = (A2 * max_val - A1 * min_val) / (A2 - A1)
125        k = 1 / (x0 - max_val) * A1
126
127        im_out = []
128        for image in im:
129            normalized_image = 1 / (np.exp(-k * (image - x0)) + 1)
130            im_out.append(normalized_image)
131
132        return np.asarray(im_out)
def calculate_qnr_score(self, RSP: <built-in function array>, FRC: <built-in function array>):
134    def calculate_qnr_score(self, RSP: np.array, FRC: np.array):
135        print(RSP.shape, FRC.shape)
136        assert RSP.shape == FRC.shape
137        nFRC = self.logistic_image_conversion(FRC)
138        print(nFRC)
139        QnR = (2 * np.asarray(RSP) * np.asarray(nFRC)) / (np.asarray(RSP) + np.asarray(nFRC))
140        return QnR