diff --git a/NeuNorm/normalization.py b/NeuNorm/normalization.py index cb480ec..eea51ce 100644 --- a/NeuNorm/normalization.py +++ b/NeuNorm/normalization.py @@ -123,7 +123,7 @@ def load(self, file='', folder='', data=None, data_type='sample', auto_gamma_fil takes_its_going_to_take = self.calculate_how_long_its_going_to_take(index_we_are=_index + 1, time_it_took_so_far=end_time - start_time, total_number_of_loop=len( - file)) + file)) time_remaining_ui.value = "{}".format(takes_its_going_to_take) if notebook: @@ -160,7 +160,7 @@ def load(self, file='', folder='', data=None, data_type='sample', auto_gamma_fil takes_its_going_to_take = self.calculate_how_long_its_going_to_take(index_we_are=_index + 1, time_it_took_so_far=end_time - start_time, total_number_of_loop=len( - list_images)) + list_images)) time_remaining_ui.value = "{}".format(takes_its_going_to_take) if notebook: @@ -412,18 +412,21 @@ def save_or_check_shape(self, data=None, data_type='sample'): if (not (_prev_width == width)) or (not (_prev_height == height)): raise IOError("Shape of {} do not match previous loaded data set!".format(data_type)) - def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=False, use_only_sample=False): + def normalization(self, roi=None, force=False, force_mean_ob=False, force_median_ob=False, notebook=False, + use_only_sample=False): """normalization of the data Parameters: roi: ROI object or list of ROI objects - object defines the region of the sample and OB that have to match - in intensity + in intensity force: boolean - True will force the normalization to occur, even if it had been run before with the same data set (default False) - notebook: boolean - turn on this option if you run the library from a - notebook to have a progress bar displayed showing you the progress of the loading (default False) - use_only_sample - turn on this option to normalize the sample data using the ROI on the sample. each pixel - counts will be divided by the average counts of all the ROI of the same image + force_mean_ob: boolean - True will force using only 1 OB that is the mean of all the OBs + force_median_ob: boolean - True will force using only 1 OB that is the median of all the OBs + notebook: boolean - turn on this option if you run the library from a + notebook to have a progress bar displayed showing you the progress of the loading (default False) + use_only_sample: turn on this option to normalize the sample data using the ROI on the sample. each pixel + counts will be divided by the average counts of all the ROI of the same image Return: True - status of the normalization (True if every went ok, this is mostly used for the unit test) @@ -435,6 +438,7 @@ def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=Fal IOError: if size of sample and OB do not match """ + if not force: # does nothing if normalization has already been run if self.__exec_process_status['normalization']: @@ -480,9 +484,9 @@ def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=Fal _x1 = roi.x1 _y1 = roi.y1 - _sample_corrected_normalized = [_sample / np.mean(_sample[_y0:_y1 + 1, _x0:_x1 + 1]) + _sample_corrected_normalized = [_sample / np.median(_sample[_y0:_y1 + 1, _x0:_x1 + 1]) for _sample in self.data['sample']['data']] - _ob_corrected_normalized = [_ob / np.mean(_ob[_y0:_y1 + 1, _x0:_x1 + 1]) + _ob_corrected_normalized = [_ob / np.median(_ob[_y0:_y1 + 1, _x0:_x1 + 1]) for _ob in self.data['ob']['data']] else: @@ -493,10 +497,12 @@ def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=Fal self.data[DataType.sample]['data'] = _sample_corrected_normalized self.data[DataType.ob]['data'] = _ob_corrected_normalized - # if the number of sample and ob do not match, use mean of obs + # if the number of sample and ob do not match, use median of obs nbr_sample = len(self.data['sample']['file_name']) nbr_ob = len(self.data['ob']['file_name']) - if (nbr_sample != nbr_ob) or force_mean_ob: # work with mean ob + + # will be deprecated soon! + if force_mean_ob: # work with mean ob _ob_corrected_normalized = np.mean(_ob_corrected_normalized, axis=0) self.data['ob']['data_mean'] = _ob_corrected_normalized _working_ob = copy.deepcopy(_ob_corrected_normalized) @@ -521,6 +527,31 @@ def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=Fal if notebook: w1.value = _index + 1 + elif (nbr_sample != nbr_ob) or force_median_ob: # work with median ob + _ob_corrected_normalized = np.median(_ob_corrected_normalized, axis=0) + self.data['ob']['data_mean'] = _ob_corrected_normalized + _working_ob = copy.deepcopy(_ob_corrected_normalized) + _working_ob[_working_ob == 0] = np.NaN + + if notebook: + # turn on progress bar + _message = "Normalization" + box1 = widgets.HBox([widgets.Label(_message, + layout=widgets.Layout(width='20%')), + widgets.IntProgress(max=len(self.data['sample']['data']))]) + display(box1) + w1 = box1.children[1] + + normalized_data = [] + for _index, _sample in enumerate(self.data['sample']['data']): + _norm = np.divide(_sample, _working_ob) + _norm[np.isnan(_norm)] = 0 + _norm[np.isinf(_norm)] = 0 + normalized_data.append(_norm) + + if notebook: + w1.value = _index + 1 + else: # 1 ob for each sample # produce normalized data sample_ob = zip(self.data['sample']['data'], self.data['ob']['data']) @@ -584,7 +615,7 @@ def calculate_corrected_normalized_without_ob(self, roi=None): _x1 = roi.x1 _y1 = roi.y1 - normalized_data = [_sample / np.mean(_sample[_y0:_y1 + 1, _x0:_x1 + 1]) + normalized_data = [_sample / np.median(_sample[_y0:_y1 + 1, _x0:_x1 + 1]) for _sample in self.data['sample']['data']] return normalized_data diff --git a/tests/NeuNorm/normalizing_test.py b/tests/NeuNorm/normalizing_test.py index 32ec741..2572a7a 100644 --- a/tests/NeuNorm/normalizing_test.py +++ b/tests/NeuNorm/normalizing_test.py @@ -23,7 +23,7 @@ def test_loading_list_of_files(self): o_norm.load(file=list_files, auto_gamma_filter=False) data_returned = o_norm.data[DataType.sample]['data'] assert (3, 5, 5) == np.shape(data_returned) - + def test_initialization_using_array_with_data(self): """assert initialization using arrays with data""" sample_01 = self.data_path + '/tif/sample/image001.tif' @@ -36,7 +36,7 @@ def test_initialization_using_array_with_data(self): data_returned = o_norm.data[DataType.sample]['data'] assert (2, 5, 5) == np.shape(data_returned) - + def test_initialization_using_array_with_data_one_by_one(self): """assert initialization using arrays with data one by one""" o_norm = Normalization() @@ -44,7 +44,7 @@ def test_initialization_using_array_with_data_one_by_one(self): sample_01 = self.data_path + '/tif/sample/image001.tif' _data = np.asarray(Image.open(sample_01)) o_norm.load(data=_data, auto_gamma_filter=False) - + sample_02 = self.data_path + '/tif/sample/image002.tif' _data = np.asarray(Image.open(sample_01)) o_norm.load(data=_data, auto_gamma_filter=False) @@ -64,7 +64,7 @@ def test_initialization_using_array_with_ob(self): data_returned = o_norm.data['ob']['data'] assert (2, 5, 5) == np.shape(data_returned) - + def test_normalization_raises_error_if_no_ob_or_sample(self): """assert error raises when no ob or sample provided""" path = self.data_path + '/tif/sample' @@ -72,25 +72,25 @@ def test_normalization_raises_error_if_no_ob_or_sample(self): o_norm.load(folder=path, data_type=DataType.sample, auto_gamma_filter=False) with pytest.raises(IOError): o_norm.normalization() - + path = self.data_path + '/tif/ob' o_norm = Normalization() o_norm.load(folder=path, data_type='ob', auto_gamma_filter=False) with pytest.raises(IOError): o_norm.normalization() - + sample_path = self.data_path + '/tif/sample' ob_path = self.data_path + '/tif/ob' o_norm = Normalization() o_norm.load(folder=sample_path, data_type=DataType.sample, auto_gamma_filter=False) o_norm.load(folder=ob_path, data_type='ob', auto_gamma_filter=False) assert o_norm.normalization() - + def test_normalization_ran_only_once(self): """assert normalization is only once if force switch not turn on""" sample_tif_folder = self.data_path + '/tif/sample' ob_tif_folder = self.data_path + '/tif/ob' - + # testing sample with norm_roi o_norm = Normalization() o_norm.load(folder=sample_tif_folder, auto_gamma_filter=False) @@ -106,7 +106,7 @@ def test_normalization_ran_twice_with_force_flag(self): """assert normalization can be ran twice using force flag""" sample_tif_folder = self.data_path + '/tif/sample' ob_tif_folder = self.data_path + '/tif/ob' - + # testing sample with norm_roi o_norm = Normalization() o_norm.load(folder=sample_tif_folder, auto_gamma_filter=False) @@ -118,24 +118,24 @@ def test_normalization_ran_twice_with_force_flag(self): o_norm.normalization(roi=roi, force=True) _returned_second_time = o_norm.data[DataType.sample]['data'][0] assert not ((_returned_first_time == _returned_second_time).all()) - + def test_normalization_works_if_input_arrays_are_type_int(self): """assert normalization works when input arrays are type int""" o_norm = Normalization() - + sample_01 = self.data_path + '/tif/sample/image001.tif' _data = np.asarray(Image.open(sample_01), dtype=int) o_norm.load(data=_data, auto_gamma_filter=False) - + sample_02 = self.data_path + '/tif/sample/image002.tif' _data = np.asarray(Image.open(sample_01), dtype=int) o_norm.load(data=_data, auto_gamma_filter=False) ob_01 = self.data_path + '/tif/ob/ob001.tif' _data = np.asarray(Image.open(ob_01), dtype=int) - _data[0,0] = 0 + _data[0, 0] = 0 o_norm.load(data=_data, data_type='ob', auto_gamma_filter=False) - + ob_02 = self.data_path + '/tif/ob/ob002.tif' _data = np.asarray(Image.open(ob_01), dtype=int) o_norm.load(data=_data, data_type='ob', auto_gamma_filter=False) @@ -153,9 +153,10 @@ def test_normalization_works(self): o_norm.load(folder=ob_tif_folder, data_type='ob', auto_gamma_filter=False) roi = ROI(x0=0, y0=0, x1=3, y1=2) _sample = o_norm.data[DataType.sample]['data'][0] - _expected = _sample / np.mean(_sample[0:3, 0:4]) + _expected = _sample / np.median(_sample[0:3, 0:4]) o_norm.normalization(roi=roi) _returned = o_norm.data[DataType.sample]['data'][0] + assert (_expected == _returned).all() # testing sample without norm_roi @@ -166,7 +167,7 @@ def test_normalization_works(self): o_norm1.normalization() _returned = o_norm1.data[DataType.sample]['data'][0] assert (_expected == _returned).all() - + # testing ob with norm_roi o_norm = Normalization() o_norm.load(folder=sample_tif_folder, auto_gamma_filter=False) @@ -174,10 +175,10 @@ def test_normalization_works(self): norm_roi = ROI(x0=0, y0=0, x1=3, y1=2) o_norm.normalization(roi=norm_roi) _ob = o_norm.data['ob']['data'][0] - _expected = _ob / np.mean(_ob[0:3, 0:4]) + _expected = _ob / np.median(_ob[0:3, 0:4]) _returned = o_norm.data['ob']['data'][0] assert (_expected == _returned).all() - + # testing ob without norm_roi o_norm = Normalization() o_norm.load(folder=sample_tif_folder, auto_gamma_filter=False) @@ -186,10 +187,10 @@ def test_normalization_works(self): o_norm.normalization() _returned = o_norm.data['ob']['data'][0] assert (_expected == _returned).all() - + def test_normalization_with_same_ob_and_sample_but_forced_mean_ob(self): """assert normalization with same ob and sample number of files force to use mean ob when flag used""" - samples_path = self.data_path + '/tif/sample/' # 3 files + samples_path = self.data_path + '/tif/sample/' # 3 files ob1 = self.data_path + '/tif/ob/ob001.tif' ob2 = self.data_path + '/tif/ob/ob002.tif' ob3 = self.data_path + '/tif/ob/ob003.tif' @@ -197,17 +198,35 @@ def test_normalization_with_same_ob_and_sample_but_forced_mean_ob(self): o_norm.load(folder=samples_path, auto_gamma_filter=False) o_norm.load(file=[ob1, ob2, ob3], data_type='ob', auto_gamma_filter=False) o_norm.normalization(force_mean_ob=True) - expected_normalized_array = np.ones((5,5)) - expected_normalized_array[:,2] = 2 - expected_normalized_array[:,3] = 3 - expected_normalized_array[:,4] = 4 + expected_normalized_array = np.ones((5, 5)) + expected_normalized_array[:, 2] = 2 + expected_normalized_array[:, 3] = 3 + expected_normalized_array[:, 4] = 4 + assert (o_norm.data['normalized'][0] == expected_normalized_array).all() + + def test_normalization_with_same_ob_and_sample_but_forced_median_ob(self): + """assert normalization with same ob and sample number of files force to use mean ob when flag used""" + samples_path = self.data_path + '/tif/sample/' # 3 files + ob1 = self.data_path + '/tif/ob/ob001.tif' + ob2 = self.data_path + '/tif/ob/ob002.tif' + ob3 = self.data_path + '/tif/ob/ob003.tif' + o_norm = Normalization() + o_norm.load(folder=samples_path, auto_gamma_filter=False) + o_norm.load(file=[ob1, ob2, ob3], data_type='ob', auto_gamma_filter=False) + # double value of last OB + o_norm.data['ob']['data'][2] *= 3 + o_norm.normalization(force_median_ob=True) + expected_normalized_array = np.ones((5, 5)) + expected_normalized_array[:, 2] = 2 + expected_normalized_array[:, 3] = 3 + expected_normalized_array[:, 4] = 4 assert (o_norm.data['normalized'][0] == expected_normalized_array).all() def test_normalization_with_fewer_ob_than_sample_works(self): """assert normalization works when number of ob and sample is different""" samples_path = self.data_path + '/tif/sample/' # 3 files ob1 = self.data_path + '/tif/ob/ob001.tif' - ob2 = self.data_path + '/tif/ob/ob002.tif' + ob2 = self.data_path + '/tif/ob/ob002.tif' df1 = self.data_path + '/tif/df/df001.tif' o_norm = Normalization() o_norm.load(folder=samples_path, auto_gamma_filter=False) @@ -221,14 +240,14 @@ def test_normalization_with_fewer_ob_than_sample_works(self): def test_nbr_data_files_same_after_normalization_by_list_roi(self): """assert the number of data files is the same after normalization by a list of ROI""" - samples_path = self.data_path + '/tif/sample/' # 3 files + samples_path = self.data_path + '/tif/sample/' # 3 files ob1 = self.data_path + '/tif/ob/ob001.tif' ob2 = self.data_path + '/tif/ob/ob002.tif' o_norm = Normalization() o_norm.load(folder=samples_path, auto_gamma_filter=False) o_norm.load(file=[ob1, ob2], data_type='ob', auto_gamma_filter=False) - _roi1 = ROI(x0=0,y0=0,x1=2,y1=2) - _roi2 = ROI(x0=1,y0=1,x1=3,y1=3) + _roi1 = ROI(x0=0, y0=0, x1=2, y1=2) + _roi2 = ROI(x0=1, y0=1, x1=3, y1=3) _list_roi = [_roi1, _roi2] nbr_data_before = len(o_norm.data[DataType.sample]['data']) o_norm.normalization(roi=_list_roi) @@ -237,7 +256,7 @@ def test_nbr_data_files_same_after_normalization_by_list_roi(self): def test_normalization_works_with_only_1_df(self): """assert using 1 df in normalization works""" - samples_path = self.data_path + '/tif/sample/' # 3 files + samples_path = self.data_path + '/tif/sample/' # 3 files ob1 = self.data_path + '/tif/ob/ob001.tif' ob2 = self.data_path + '/tif/ob/ob002.tif' df1 = self.data_path + '/tif/df/df001.tif' @@ -303,15 +322,36 @@ def setup_method(self): self.data_path = os.path.abspath(os.path.join(_file_path, '../data/')) def test_ob_median(self): - pass + """make sure combining the OB will use the median""" + + # without normalization roi + sample_path = self.data_path + '/tif/sample' + ob_path = self.data_path + '/tif/ob' + df_path = self.data_path + '/tif/df' + + o_norm = Normalization() + o_norm.load(folder=sample_path, auto_gamma_filter=False) + o_norm.load(folder=ob_path, data_type=DataType.ob, auto_gamma_filter=False) + + # replace last ob with crazy one, that shouldn't be use when using median (instead of mean) + o_norm.data['ob']['data'][-1] = np.ones((5, 5)) * 1000 + + o_norm.load(folder=df_path, data_type=DataType.df, auto_gamma_filter=False) + o_norm.normalization(force_median_ob=True) + _norm_expected = np.ones((5, 5)) + _norm_expected[:, 2] = 2 + _norm_expected[:, 3] = 3 + _norm_expected[:, 4] = 4 + _norm_returned = o_norm.data['normalized'] + assert (_norm_expected == _norm_returned).all() class TestDFCorrection: def setup_method(self): _file_path = os.path.dirname(__file__) - self.data_path = os.path.abspath(os.path.join(_file_path, '../data/')) - + self.data_path = os.path.abspath(os.path.join(_file_path, '../data/')) + def test_df_correction_when_no_df(self): """assert sample and ob are inchanged if df is empty""" @@ -323,8 +363,8 @@ def test_df_correction_when_no_df(self): o_norm.df_correction() data_after = o_norm.data[DataType.sample]['data'][0] assert (data_before == data_after).all() - - #ob + + # ob path = self.data_path + '/tif/ob' o_norm = Normalization() o_norm.load(folder=path, data_type='ob', auto_gamma_filter=False) @@ -332,18 +372,18 @@ def test_df_correction_when_no_df(self): o_norm.df_correction() data_after = o_norm.data['ob']['data'][0] assert (data_before == data_after).all() - + def test_df_fails_when_not_identical_data_shape(self): o_norm = Normalization() - sample_1 = np.ones([5,5]) - df_1 = np.ones([6,6]) + sample_1 = np.ones([5, 5]) + df_1 = np.ones([6, 6]) o_norm.data[DataType.sample]['data'] = sample_1 o_norm.data[DataType.df]['data'] = df_1 with pytest.raises(IOError): o_norm.df_correction() - + o_norm = Normalization() - ob_1 = np.ones([6,6]) + ob_1 = np.ones([6, 6]) o_norm.data['ob']['data'] = sample_1 o_norm.data[DataType.df]['data'] = ob_1 with pytest.raises(IOError): @@ -360,16 +400,16 @@ def test_df_averaging_only_run_the_first_time(self): df_file_2 = self.data_path + '/tif/df/df003.tif' o_norm.load(file=df_file_1, data_type=DataType.df, auto_gamma_filter=False) o_norm.load(file=df_file_2, data_type=DataType.df, auto_gamma_filter=False) - + df_average_data = o_norm.data[DataType.df]['data_average'] assert not df_average_data - - #sample + + # sample o_norm.df_correction() df_average_data = o_norm.data[DataType.df]['data_average'] assert df_average_data.size != 0 - - #ob + + # ob o_norm.df_correction() expected_df_average = df_average_data df_average = o_norm.data[DataType.df]['data_average'] @@ -386,18 +426,18 @@ def test_df_correction(self): df_file_2 = self.data_path + '/tif/df/df003.tif' o_norm.load(file=df_file_1, data_type=DataType.df, auto_gamma_filter=False) o_norm.load(file=df_file_2, data_type=DataType.df, auto_gamma_filter=False) - - #sample + + # sample o_norm.df_correction() - _expected_data = np.zeros([5,5]) - _expected_data[:,2] = 1 - _expected_data[:,3] = 2 - _expected_data[:,4] = 3 + _expected_data = np.zeros([5, 5]) + _expected_data[:, 2] = 1 + _expected_data[:, 3] = 2 + _expected_data[:, 4] = 3 _sample_data = o_norm.data[DataType.sample]['data'][0] assert (_expected_data == o_norm.data[DataType.sample]['data'][0]).all() - - #ob - _expected_data = np.zeros([5,5]) + + # ob + _expected_data = np.zeros([5, 5]) _ob_data = o_norm.data['ob']['data'][0] assert (_expected_data == _ob_data).all() @@ -412,20 +452,20 @@ def test_df_correction_locked_when_run_twice_without_force_flag(self): df_file_2 = self.data_path + '/tif/df/df003.tif' o_norm.load(file=df_file_1, data_type=DataType.df, auto_gamma_filter=False) o_norm.load(file=df_file_2, data_type=DataType.df, auto_gamma_filter=False) - + # first iteration o_norm.df_correction() _sample_first_run = o_norm.data[DataType.sample]['data'][0] _ob_first_run = o_norm.data['ob']['data'][0] - + # second iteration o_norm.df_correction() _sample_second_run = o_norm.data[DataType.sample]['data'][0] _ob_second_run = o_norm.data['ob']['data'][0] - + assert (_sample_first_run == _sample_second_run).all() assert (_ob_first_run == _ob_second_run).all() - + def test_df_correction_run_twice_with_force_flag(self): """assert df corrction run more than once with force flag""" sample_path = self.data_path + '/tif/sample/' @@ -437,13 +477,13 @@ def test_df_correction_run_twice_with_force_flag(self): df_file_2 = self.data_path + '/tif/df/df003.tif' o_norm.load(file=df_file_1, data_type=DataType.df, auto_gamma_filter=False) o_norm.load(file=df_file_2, data_type=DataType.df, auto_gamma_filter=False) - + # first iteration o_norm.df_correction() _sample_first_run = o_norm.data[DataType.sample]['data'][0] _ob_first_run = o_norm.data['ob']['data'][0] _average_df = o_norm.data[DataType.df]['data_average'] - + # second iteration o_norm.df_correction(force=True) _sample_second_run = o_norm.data[DataType.sample]['data'][0] @@ -452,17 +492,17 @@ def test_df_correction_run_twice_with_force_flag(self): # expected _expected_sample_after_second_run = _sample_first_run - _average_df _expected_ob_after_second_run = _ob_first_run - _average_df - + assert (_sample_second_run == _expected_sample_after_second_run).all() assert (_ob_second_run == _expected_ob_after_second_run).all() - - + + class TestApplyingROI: def setup_method(self): _file_path = os.path.dirname(__file__) - self.data_path = os.path.abspath(os.path.join(_file_path, '../data/')) - + self.data_path = os.path.abspath(os.path.join(_file_path, '../data/')) + def test_roi_type_in_normalization(self): """assert error is raised when type of norm roi are not ROI in normalization""" sample_tif_file = self.data_path + '/tif/sample/image001.tif' @@ -470,15 +510,15 @@ def test_roi_type_in_normalization(self): o_norm = Normalization() o_norm.load(file=sample_tif_file, data_type=DataType.sample, auto_gamma_filter=False) o_norm.load(file=ob_tif_file, data_type='ob', auto_gamma_filter=False) - roi = {'x0':0, 'y0':0, 'x1':2, 'y1':2} + roi = {'x0': 0, 'y0': 0, 'x1': 2, 'y1': 2} with pytest.raises(ValueError): o_norm.normalization(roi=roi) - + def test_roi_fit_images(self): """assert norm roi do fit the images""" sample_tif_file = self.data_path + '/tif/sample/image001.tif' ob_tif_file = self.data_path + '/tif/ob/ob001.tif' - + # x0 < 0 or x1 > image_width o_norm = Normalization() o_norm.load(file=sample_tif_file, data_type=DataType.sample, auto_gamma_filter=False) @@ -486,14 +526,14 @@ def test_roi_fit_images(self): roi = ROI(x0=0, y0=0, x1=20, y1=4) with pytest.raises(ValueError): o_norm.normalization(roi=roi) - + o_norm = Normalization() o_norm.load(file=sample_tif_file, data_type=DataType.sample, auto_gamma_filter=False) o_norm.load(file=ob_tif_file, data_type='ob', auto_gamma_filter=False) roi = ROI(x0=-1, y0=0, x1=4, y1=4) with pytest.raises(ValueError): o_norm.normalization(roi=roi) - + # y0 < 0 or y1 > image_height o_norm = Normalization() o_norm.load(file=sample_tif_file, data_type=DataType.sample, auto_gamma_filter=False) @@ -512,7 +552,7 @@ def test_roi_fit_images(self): def test_error_raised_when_data_shape_of_different_type_do_not_match(self): """assert shape of data must match to allow normalization""" - + # sample and ob image1 = self.data_path + '/tif/sample/image001.tif' ob1 = self.data_path + '/different_format/ob001_4_by_4.tif' @@ -687,7 +727,7 @@ def test_full_normalization_sample_divide_by_ob_works(self): _norm_expected[:, 4] = 4 _norm_returned = o_norm.data['normalized'] assert (_norm_expected == _norm_returned).all() - + # with normalization roi sample_path = self.data_path + '/tif/sample' ob_path = self.data_path + '/tif/ob' @@ -698,14 +738,11 @@ def test_full_normalization_sample_divide_by_ob_works(self): o_norm.load(folder=df_path, data_type=DataType.df, auto_gamma_filter=False) _roi = ROI(x0=0, y0=0, x1=2, y1=2) o_norm.normalization(roi=_roi) - _norm_expected = np.ones((5, 5)) - _norm_expected.fill(0.8125) - _norm_expected[:, 2] = 1.625 - _norm_expected[:, 3] = 2.4375 - _norm_expected[:, 4] = 3.25 + _norm_expected = o_norm.data['sample']['data'][0] + _norm_expected[0, 0] = 1 _norm_returned = o_norm.data['normalized'] - assert (_norm_expected == _norm_returned).all() - + assert (_norm_expected == _norm_returned[0]).all() + def test_various_data_type_correctly_returned(self): """assert normalized, sample, ob and df data are correctly returned""" sample_path = self.data_path + '/tif/sample' @@ -715,28 +752,28 @@ def test_various_data_type_correctly_returned(self): o_norm.load(folder=sample_path, auto_gamma_filter=False) o_norm.load(folder=ob_path, data_type=DataType.ob, auto_gamma_filter=False) o_norm.load(folder=df_path, data_type=DataType.df, auto_gamma_filter=False) - + # sample _data_expected = o_norm.data[DataType.sample]['data'][0] _data_returned = o_norm.get_sample_data()[0] assert (_data_expected == _data_returned).all() - + # ob _ob_expected = o_norm.data[DataType.ob]['data'] _ob_returned = o_norm.get_ob_data()[0] assert (_ob_expected == _ob_returned).all() - + # df _df_expected = o_norm.data[DataType.df]['data'] _df_returned = o_norm.get_df_data() assert _df_expected == _df_returned - + # normalized is empty before normalization assert o_norm.get_normalized_data() is None - + # run normalization o_norm.normalization() - + _norm_expected = o_norm.data['normalized'][0] _norm_returned = o_norm.get_normalized_data()[0] assert (_norm_expected == _norm_returned).all() @@ -760,11 +797,11 @@ def test_normalization_using_roi_of_sample_only(self): o_norm.normalization(roi=_roi, use_only_sample=True) _norm_expected = np.ones((5, 5)) - _norm_expected[1:, 0] = 1./10 - _norm_expected[:, 1] = 2./10 - _norm_expected[:, 2] = 3./10 - _norm_expected[:, 3] = 4./10 - _norm_expected[:-1, 4] = 5./10 + _norm_expected[1:, 0] = 1. / 10 + _norm_expected[:, 1] = 2. / 10 + _norm_expected[:, 2] = 3. / 10 + _norm_expected[:, 3] = 4. / 10 + _norm_expected[:-1, 4] = 5. / 10 _norm_returned = o_norm.get_normalized_data()[0] nbr_col, nbr_row = np.shape(_norm_expected)