docstr: Align two images keeping sizes the same
docstr_tok: ["Align","two","images","keeping","sizes","the","same"]
code: def test_02_03_same_size(self):
      """Align two images keeping sizes the same"""
      np.random.seed(0)
      shape = (50, 45)
      i, j = np.mgrid[0 : shape[0], 0 : shape[1]]
      for offset in (
          (1, 0),
          (0, 1),
          (1, 1),
          (3, 5),
          (-3, 5),
          (3, -5),
          (-3, -5),
          (0, 5),
          (3, 0),
          (0, 0),
      ):
          for mask1 in (None, np.random.uniform(size=shape) > 0.1):
              for mask2 in (None, np.random.uniform(size=shape) > 0.1):
                  for method in (
                      M_MUTUAL_INFORMATION,
                      M_CROSS_CORRELATION,
                  ):
                      if method == M_CROSS_CORRELATION and (
                          (mask1 is not None) or (mask2 is not None)
                      ):
                          continue
                      image1 = (
                          np.random.randint(0, 10, size=shape).astype(
                              float
                          )
                          / 10.0
                      )
                      image1[
                          np.sqrt(
                              (
                                  (i - shape[0] / 2) ** 2
                                  + (j - shape[1] / 2) ** 2
                              )
                          )
                          < 20
                      ] = 0.5
                      si1, si2 = self.slice_helper(
                          offset[0], image1.shape[0]
                      )
                      sj1, sj2 = self.slice_helper(
                          offset[1], image1.shape[1]
                      )
                      image2 = np.zeros(image1.shape)
                      if method == M_MUTUAL_INFORMATION:
                          image2[si2, sj2] = 1 - image1[si1, sj1]
                      else:
                          image2 = image1[
                              (i + shape[0] - offset[0]) % shape[0],
                              (j + shape[1] - offset[1]) % shape[1],
                          ]
                          image2 += (
                              (np.random.uniform(size=shape) - 0.5)
                              * 0.1
                              * np.std(image2)
                          )
                      if mask1 is not None:
                          image1[~mask1] = np.random.uniform(
                              size=np.sum(~mask1)
                          )
                      if mask2 is not None:
                          image2[~mask2] = np.random.uniform(
                              size=np.sum(~mask2)
                          )
                      workspace, module = self.make_workspace(
                          (image1, image2), (mask1, mask2)
                      )
                      assert isinstance(module, Align)
                      module.alignment_method.value = method
                      module.crop_mode.value = C_SAME_SIZE
                      module.run(workspace)
                      output = workspace.image_set.get_image("Aligned0")
                      m = workspace.measurements
                      assert isinstance(m, Measurements)
                      off_i0 = -m.get_current_image_measurement(
                          "Align_Yshift_Aligned0"
                      )
                      off_j0 = -m.get_current_image_measurement(
                          "Align_Xshift_Aligned0"
                      )
                      off_i1 = -m.get_current_image_measurement(
                          "Align_Yshift_Aligned1"
                      )
                      off_j1 = -m.get_current_image_measurement(
                          "Align_Xshift_Aligned1"
                      )
  
                      assert off_i0 - off_i1 == offset[0]
                      assert off_j0 - off_j1 == offset[1]
  
                      si_in, si_out = self.slice_same(off_i0, shape[0])
                      sj_in, sj_out = self.slice_same(off_j0, shape[1])
                      np.testing.assert_almost_equal(
                          image1[si_in, sj_in],
                          output.pixel_data[si_out, sj_out],
                      )
                      if mask1 is not None:
                          assert np.all(
                              output.mask[si_out, sj_out]
                              == mask1[si_in, sj_in]
                          )
  
                      temp = output.mask.copy()
                      temp[si_out, sj_out] = False
                      assert np.all(~temp)
  
                      output = workspace.image_set.get_image("Aligned1")
                      si_in, si_out = self.slice_same(off_i1, shape[0])
                      sj_in, sj_out = self.slice_same(off_j1, shape[1])
                      np.testing.assert_almost_equal(
                          image2[si_in, sj_in],
                          output.pixel_data[si_out, sj_out],
                      )
                      if mask2 is not None:
                          assert np.all(
                              mask2[si_in, sj_in]
                              == output.mask[si_out, sj_out]
                          )
                      temp = output.mask.copy()
                      temp[si_out, sj_out] = False
                      assert np.all(~temp)
  
imports: from io import StringIO
  import numpy
  from cellprofiler_core.constants.measurement import COLTYPE_INTEGER
  from cellprofiler_core.measurement import Measurements
  from cellprofiler_core.modules.align import Align, A_SIMILARLY, A_SEPARATELY, M_MUTUAL_INFORMATION, C_SAME_SIZE, C_CROP, C_PAD, M_CROSS_CORRELATION, MEASUREMENT_FORMAT, C_ALIGN
  from cellprofiler_core.object import ObjectSet
  from cellprofiler_core.pipeline import Pipeline, LoadException
  from cellprofiler_core.preferences import set_headless
  from cellprofiler_core.workspace import Workspace
  from tests.core.modules import read_example_image
  from cellprofiler_core.image import ImageSetList, Image
label_desc: Use image node_id 8 for calling the TestAlign obj's underlying member method code with example usage: obj.test_02_03_same_size() without return types