def target_groupwise_registration(self, inverse=False):
        """
        Register the target image into the model space

        Affine (or rigid + affine) registration of the target on the mean model image --> pairwise

        :param inverse: if True, apply the inverse warping field of the registration target -> model space
        to the result gray matter segmentation of the target
        (put it back in it's original space)
        """
        if not inverse:
            # Registration target --> model space
            from scipy.optimize import minimize

            # Initialisation
            target_m = []

            def to_minimize(t_param, n_slice):
                """

                :param :

                :return sum_norm:

                """
                moved_target_slice = Image(param=np.asarray(apply_2d_transformation(self.target.data[n_slice],
                                                                                    tx=t_param[0], ty=t_param[1],
                                                                                    theta=t_param[2],
                                                                                    s=t_param[3])[0]))

                coord_moved_target = self.model.pca.project_array(moved_target_slice.data.flatten())
                coord_moved_target = coord_moved_target.reshape(coord_moved_target.shape[0],)

                beta = self.compute_beta(coord_moved_target, tau=self.tau)
                mu = self.compute_mu(beta)
                sigma = self.compute_sigma(beta, mu)

                target_th_slice = np.sum((np.asarray([dic_slice.im_M for dic_slice in self.model.dictionary.slices]).T
                                          * beta[n_slice]).T, axis=0)
                sq_norm = np.linalg.norm(target_th_slice - moved_target_slice.data, 2)**2

                gauss = np.sum(((coord_moved_target - mu)/sigma)**2)
                return sq_norm*gauss

            r_target_to_m = []
            for i_slice, target_slice in enumerate(self.target.data):
                x0 = [0, 0, 0, 1]
                est_transfo = minimize(to_minimize, x0, args=i_slice, method='Nelder-Mead', options={'xtol': 0.00005})
                target_m_slice, r_slice = apply_2d_transformation(target_slice, tx=est_transfo.x[0],
                                                                  ty=est_transfo.x[1], theta=est_transfo.x[2],
                                                                  s=est_transfo.x[3])
                print est_transfo.x
                target_m.append(target_m_slice)
                r_target_to_m.append(r_slice)
            return Image(param=np.asarray(target_m)), r_target_to_m

        else:
            # Inverse registration result in model space --> target original space
            moved_res = []
            for i_slice, res_m_slice in enumerate(self.target_GM_seg_M.data):
                moved_res.append(apply_2d_transformation(res_m_slice, transfo=self.R_target_to_M[i_slice].inverse)[0])
            return Image(np.asarray(moved_res).astype('uint8'))

    # ------------------------------------------------------------------------------------------------------------------
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/sct_segment_gray_matter_asman/sct_asman_old.py/TargetSegmentationGroupwise.target_groupwise_registration/original