def test_probabilisticQuotientNormaliser(self):
        """
		Check that the ProbabilisticQuotientNormaliser 
		"""

        X = numpy.copy(self.X)
        reference = numpy.nanmedian(X, axis=0)
        pqn_norm = ProbabilisticQuotientNormaliser()

        fold_change_matrix = X / reference
        pqn_norm_coefs = numpy.absolute(
            numpy.median(fold_change_matrix, axis=1))
        pqn_normed = X / pqn_norm_coefs[:, None]

        numpy.testing.assert_array_almost_equal(
            pqn_normed,
            pqn_norm.normalise(self.X),
            err_msg=
            "PQN normaliser not working correctly - mismatching normalised data"
        )
        numpy.testing.assert_array_almost_equal(
            pqn_norm_coefs,
            pqn_norm.normalisation_coefficients,
            err_msg=
            "PQN normaliser not working correctly - non-matching PQN coefficients"
        )
        numpy.testing.assert_array_equal(reference, pqn_norm.reference)
    def test_delete_reference(self):

        pqn_norm = ProbabilisticQuotientNormaliser()
        pqn_norm.normalise(self.X)

        del pqn_norm.reference

        self.assertIsNone(pqn_norm.normalisation_coefficients)
    def test_probabilisticQuotientNormaliser_nans(self):
        ##
        # Check we dont crash with NaNs
        ##
        self.X[0, 0] = numpy.nan

        pqn_norm = ProbabilisticQuotientNormaliser()

        pqn_norm.normalise(self.X)
    def test_raises(self):

        pqn_norm = ProbabilisticQuotientNormaliser()
        with self.subTest(msg='1D X matrix'):
            X = numpy.array([2])
            self.assertRaises(ValueError, pqn_norm.normalise, X)

        with self.subTest(msg='3D X matrix'):
            X = numpy.array([2, 2, 2])
            self.assertRaises(ValueError, pqn_norm.normalise, X)

        with self.subTest(msg='Reference wrong size'):
            X = numpy.array([5, 5])
            pqn_norm.reference = numpy.array([4])
            self.assertRaises(ValueError, pqn_norm.normalise, X)
    def test_repr(self):

        with self.subTest(msg='Default reference profile'):
            pqn_norm = ProbabilisticQuotientNormaliser()
            strform = str(pqn_norm)
            self.assertEqual(
                strform,
                'Normalised to median fold-change, reference profile was the median profile.'
            )
    def test_pass_reference(self):

        X = numpy.copy(self.X)
        reference = numpy.abs(numpy.random.randn(self.noFeat))
        pqn_norm = ProbabilisticQuotientNormaliser(reference=reference)
        pqn_norm.normalise(X)

        featureMask = numpy.logical_and(numpy.isfinite(reference),
                                        reference != 0)
        fold_change_matrix = X[:, featureMask] / reference[featureMask]
        fold_change_matrix[fold_change_matrix == 0] = numpy.nan
        pqn_norm_coefs = numpy.absolute(
            numpy.median(fold_change_matrix, axis=1))
        # Set 0 cofficients to 1
        pqn_norm_coefs[pqn_norm_coefs == 0] = 1

        numpy.testing.assert_array_almost_equal(
            pqn_norm_coefs,
            pqn_norm.normalisation_coefficients,
            err_msg="Change of reference does not work")
    def test_nullNormaliser_eq_(self):
        """
		Check that the NullNormaliser equality testing works
		"""
        with self.subTest():
            norm = NullNormaliser()
            norm2 = NullNormaliser()

            self.assertEqual(norm, norm2)

        pqn = ProbabilisticQuotientNormaliser()
        tanorm = TotalAreaNormaliser(keepMagnitude=False)
        tanorm2 = TotalAreaNormaliser(keepMagnitude=True)

        notEqualList = [
            1, True, 'str', 1.1,
            list(), dict(), tanorm, tanorm2, pqn
        ]
        norm = NullNormaliser()
        for comparison in notEqualList:
            with self.subTest(msg=comparison):
                self.assertNotEqual(norm, comparison)
    def test_totalAreaNormaliser_eq_(self):
        """
		Check that the TotalAreaNormaliser equality testing works
		"""
        with self.subTest(msg='Test keepMagnitude is respected'):
            tanorm = TotalAreaNormaliser(keepMagnitude=False)
            tanorm2 = TotalAreaNormaliser(keepMagnitude=False)
            tanorm3 = TotalAreaNormaliser(keepMagnitude=True)
            tanorm4 = TotalAreaNormaliser(keepMagnitude=True)

            self.assertEqual(tanorm, tanorm2)
            self.assertEqual(tanorm3, tanorm3)
            self.assertNotEqual(tanorm, tanorm3)

        pqn = ProbabilisticQuotientNormaliser()
        notEqualList = [
            1, True, 'str', 1.1,
            list(), dict(),
            NullNormaliser(), pqn
        ]
        tanorm = TotalAreaNormaliser()
        for comparison in notEqualList:
            with self.subTest(msg=comparison):
                self.assertNotEqual(tanorm, comparison)
    def test_nans(self):

        self.X[0, 0] = numpy.nan
        pqn_norm = ProbabilisticQuotientNormaliser()

        pqn_norm.normalise(self.X)