예제 #1
0
	def test_referenceToSinglet(self):

		from nPYc.utilities._calibratePPMscale import referenceToSinglet

		spectrum = numpy.array([0, 5, 6, 9, 6, 5, 0, 0, 0, 7, 8, 6, 0, 0, 0, 0, 0, 0, 0, 0])
		ppm = numpy.linspace(-10, 10, 20)

		with self.subTest(msg='Using whole spectrum'):

			obtainedPPM = referenceToSinglet(spectrum, ppm, (-10, 10))
			expectedDeltaPPM = 3

			self.assertEqual(obtainedPPM, expectedDeltaPPM)

		with self.subTest(msg='Using masked spectrum'):

			obtainedPPM = referenceToSinglet(spectrum, ppm, (-2, 8))
			expectedDeltaPPM = 10

			self.assertEqual(obtainedPPM, expectedDeltaPPM)
예제 #2
0
	def test_calibratePPM(self):

		from nPYc.utilities._calibratePPMscale import calibratePPM, referenceToSinglet, referenceToResolvedMultiplet

		with self.subTest(msg='Singlet'):

			specSize = numpy.random.randint(100, 1000)
			ppm = numpy.linspace(-1, 10, specSize)

			shift = ppm[numpy.random.randint(specSize - 20) + 10]
			targetShift = ppm[numpy.random.randint(specSize - 20) + 10]

			spectrum = L(ppm, shift, 0.001)

			spectrum = spectrum + L(ppm, shift + 3, 0.001) * 2

			(alignedSpectrum, alignedPPM, ppmShift) = calibratePPM('Singlet', targetShift, (shift - 1, shift + 1), ppm, spectrum, spectrumType='1D')

			obtainedPPM = referenceToSinglet(alignedSpectrum, alignedPPM, (targetShift - 1, targetShift + 1))

			self.assertEqual(alignedPPM[obtainedPPM], targetShift)


		with self.subTest(msg='Singlet, reversed'):

			specSize = numpy.random.randint(100, 1000)
			ppm = numpy.linspace(10, -1, specSize)

			shift = ppm[numpy.random.randint(specSize -20) + 10]
			targetShift = ppm[numpy.random.randint(specSize  - 20) + 10]

			spectrum = L(ppm, shift, 0.001)

			spectrum = spectrum + L(ppm, shift + 3, 0.001) * 2

			(alignedSpectrum, alignedPPM, ppmShift) = calibratePPM('Singlet', targetShift, (shift - 1, shift + 1), ppm, spectrum, spectrumType='1D')

			obtainedPPM = referenceToSinglet(alignedSpectrum, alignedPPM, (targetShift - 1, targetShift + 1))

			self.assertEqual(alignedPPM[obtainedPPM], targetShift)


		with self.subTest(msg='Doublet'):

			specSize = numpy.random.randint(500, 1000)
			ppm = numpy.linspace(4.8, 5.7, specSize)

			# Reserve below 5 ppm for decoy peaks
			shift = ppm[numpy.random.randint(numpy.max(numpy.where(ppm<5.1)), high=numpy.min(numpy.where(ppm>5.5)), size=1)[0]]
			targetShift = ppm[numpy.random.randint(numpy.max(numpy.where(ppm<5.1)), high=numpy.min(numpy.where(ppm>5.5)), size=1)[0]]

			baseline = L(ppm, 5.3, .09)

			peakOne = L(ppm, shift, 0.001)
			peakTwo = L(ppm, shift + 0.01, 0.001)

			peakThree = L(ppm, 4.81, 0.001)
			peakFour = L(ppm, 4.83, 0.001)

			spectrum = (baseline * 300) + peakOne + peakTwo + (peakThree + peakFour) * 5

			(alignedSpectrum, alignedPPM, ppmShift) = calibratePPM('Doublet', targetShift, (shift - 0.2, shift + 0.2), ppm, spectrum, spectrumType='1D')

			obtainedPPM = referenceToResolvedMultiplet(alignedSpectrum, alignedPPM, (targetShift - 0.2, targetShift + 0.2), 2)
			obtainedPPM = int(round(numpy.mean(obtainedPPM)))

			# Use all close because midpoint of pleaks may fall between the ppm grid
			numpy.testing.assert_allclose(alignedPPM[obtainedPPM], targetShift, atol=ppm[1]-ppm[0])