Skip to content

Commit

Permalink
updated unit tests to reflect recent code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinesands committed Feb 22, 2024
1 parent f525e26 commit 8d2b220
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
15 changes: 9 additions & 6 deletions Tests/test_msdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from nPYc.enumerations import VariableType
from generateTestDataset import generateTestDataset
import tempfile
from nPYc.utilities._errorHandling import npycToolboxError



Expand Down Expand Up @@ -1352,8 +1353,9 @@ def test_inferbatches_warns(self):
msData = copy.deepcopy(self.msData)
msData.sampleMetadata.drop('Run Order', axis=1, inplace=True)
msData.sampleMetadata.drop('Acquired Time', axis=1, inplace=True)
self.assertWarnsRegex(UserWarning, 'Unable to infer batches without complete run order or acquired time info, skipping.',
msData._inferBatches)
#self.assertWarnsRegex(UserWarning, 'Unable to infer batches without `Run Order` or `Acquired Time` columns in dataset.sampleMetadata',
# msData._inferBatches)
self.assertRaises(npycToolboxError, msData._inferBatches)

def test_amendbatches(self):

Expand Down Expand Up @@ -1535,6 +1537,7 @@ def test_dilutionlevels(self):
def test_feature_correlation(self):

self.msData.addSampleInfo(descriptionFormat='Raw Data', filePath=os.path.join('..','..','npc-standard-project','Raw_Data'))
self.msData.addSampleInfo(descriptionFormat='Infer Batches')

with self.subTest(msg='Testing Pearson correlations'):
correlations = numpy.array([0.99999997, 0.32017508, 1., -0.0693418])
Expand Down Expand Up @@ -1700,10 +1703,10 @@ def test_dilutionlevels(self):
def test_feature_correlation(self):

self.msData.addSampleInfo(descriptionFormat='Raw Data', filePath=os.path.join('..','..','npc-standard-project','Raw_Data'))
# self.msData.addSampleInfo(descriptionFormat='Batches')
self.msData.addSampleInfo(descriptionFormat='Infer Batches')

self.msData_PeakTable.addSampleInfo(descriptionFormat='Raw Data', filePath=os.path.join('..','..','npc-standard-project','Raw_Data'))
# self.msData_PeakTable.addSampleInfo(descriptionFormat='Batches')
self.msData_PeakTable.addSampleInfo(descriptionFormat='Infer Batches')

with self.subTest(msg='Testing Pearson correlations'):
correlations = numpy.array([0.99999997, 0.32017508, 1., -0.0693418])
Expand Down Expand Up @@ -1841,7 +1844,7 @@ def test_dilutionlevels(self):
def test_feature_correlation(self):

self.msData.addSampleInfo(descriptionFormat='Raw Data', filePath=os.path.join('..','..','npc-standard-project','Raw_Data'))
# self.msData.addSampleInfo(descriptionFormat='Batches')
self.msData.addSampleInfo(descriptionFormat='Infer Batches')

with self.subTest(msg='Testing Pearson correlations'):
correlations = numpy.array([0.99999997, 0.32017508, 1., -0.0693418])
Expand Down Expand Up @@ -2030,7 +2033,7 @@ def test_dilutionlevels(self):
def test_feature_correlation(self):

self.lcData.addSampleInfo(descriptionFormat='Raw Data', filePath=os.path.join('..','..','npc-standard-project','Raw_Data'))
# self.lcData.addSampleInfo(descriptionFormat='Batches')
self.lcData.addSampleInfo(descriptionFormat='Infer Batches')

with self.subTest(msg='Testing Pearson correlations'):
correlations = numpy.array([0.99999997, 0.32017508, 1., -0.0693418])
Expand Down
42 changes: 32 additions & 10 deletions Tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import sys
import unittest
import copy

sys.path.append("..")
import nPYc
from nPYc.utilities._errorHandling import npycToolboxError

"""
Tests for checking specific data values remain the same after report functionality changes
Expand All @@ -25,25 +27,45 @@ def setUp(self):
sop="GenericMS",
noFeatureParams=9)

self.data.addSampleInfo(descriptionFormat="Basic CSV",
filePath=os.path.join("..", "..",
"npc-standard-project",
"Regression_Testing_Data",
"DEVSET U RPOS Basic CSV_regressionTesting.csv"))


def test_setup(self):
self.assertIsNotNone(self.data)

def test_XCMS_metadata_report_correct(self):
def test_error_correct(self):
"""
Check error is thrown when sample metadata is missing from basic CSV
"""
msData = copy.deepcopy(self.data)

sample_summary = nPYc.reports._generateSampleReport(self.data, returnOutput=True)
# This is designed to succeed if the npycToolboxError is caught, otherwise to fail
try:
self.assertRaises(npycToolboxError, msData.addSampleInfo(descriptionFormat="Basic CSV",
filePath=os.path.join("..", "..",
"npc-standard-project",
"Regression_Testing_Data",
"DEVSET U RPOS Basic CSV_regressionTesting_sampleMissing.csv")))
except npycToolboxError:# as e:
#print(e)
return

self.fail()

def test_XCMS_metadata_report_correct(self):
"""
Check returns against expected. sample_summary is a dictionary of dataframes with keys:
for key in sample_summary.keys():
print(key)
print(sample_summary[key])
"""

# Add basic CSV info
self.data.addSampleInfo(descriptionFormat="Basic CSV",
filePath=os.path.join("..", "..",
"npc-standard-project",
"Regression_Testing_Data",
"DEVSET U RPOS Basic CSV_regressionTesting.csv"))

sample_summary = nPYc.reports._generateSampleReport(self.data, returnOutput=True)

# Acquired - Totals
self.assertEqual(sample_summary["Acquired"].loc["All", "Total"], 214)
self.assertEqual(sample_summary["Acquired"].loc["Study Sample", "Total"], 78)
Expand Down Expand Up @@ -71,7 +93,7 @@ def test_XCMS_metadata_report_correct(self):
self.assertEqual(sample_summary["Acquired"].loc["Blank", "Missing/Excluded"], 0)
self.assertEqual(sample_summary["Acquired"].loc["Unknown", "Missing/Excluded"], 0)

self.assertEqual(sample_summary["NoMetadata Details"].loc[0, "Sample File Name"], "PipelineTesting_RPOS_ToF10_U1W98")
#self.assertEqual(sample_summary["NoMetadata Details"].loc[0, "Sample File Name"], "PipelineTesting_RPOS_ToF10_U1W98")
self.assertEqual(sample_summary["UnknownType Details"].loc[0, "Sample File Name"], "PipelineTesting_RPOS_ToF10_U1W98")
self.assertEqual(sample_summary["NotAcquired"].loc[0, "Sample File Name"], "PipelineTesting_RPOS_ToF10_U1W97")
self.assertEqual(sample_summary["Excluded Details"].loc[0, "Sample File Name"], "PipelineTesting_RPOS_ToF10_U1W97")
Expand Down
1 change: 1 addition & 0 deletions Tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def test_reports_ms_correlationtodilution(self):
data.addSampleInfo(descriptionFormat='Raw Data',
filePath=os.path.join('..', '..', 'npc-standard-project', 'Raw_Data', 'ms',
'parameters_data'))
data.addSampleInfo(descriptionFormat='Infer Batches')
# data.sampleMetadata['Correction Batch'] = data.sampleMetadata['Batch']
data.corrExclusions = data.sampleMask

Expand Down
2 changes: 1 addition & 1 deletion nPYc/reports/_generateReportMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def _featureReport(dataset, destinationPath=None):
print('Figure 5: TIC of serial dilution (SRD) samples coloured by sample dilution.')

plotLRTIC(dataset,
sampleMask=LRmask,
sampleMask=acquiredMasks['SRDmask'],
savePath=saveAs,
figureFormat=dataset.Attributes['figureFormat'],
dpi=dataset.Attributes['dpi'],
Expand Down
17 changes: 8 additions & 9 deletions nPYc/reports/_generateSampleReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,16 @@ def _generateSampleReport(dataTrue, withExclusions=False, destinationPath=None,

# Determine if samples have been excluded
if hasattr(data, 'excludedFlag') and (len(data.excludedFlag) > 0):
excludedIX = [i for i, x in enumerate(data.excludedFlag) if x == 'Samples']
sampleMetadataExcluded = pandas.DataFrame(
columns=['Sample File Name', 'Sample ID', 'SampleType', 'AssayRole', 'Exclusion Details'])

# Stick info of all previously excluded samples together
for i in excludedIX:
temp = copy.deepcopy(data.sampleMetadataExcluded[i])
# Create dataframe with columns required
cols = ['Sample File Name', 'Sample ID', 'SampleType', 'AssayRole', 'Exclusion Details']
sampleMetadataExcluded = pandas.DataFrame(columns=cols)

sampleMetadataExcluded = sampleMetadataExcluded.append(temp[['Sample File Name', 'Sample ID',
'SampleType', 'AssayRole',
'Exclusion Details']], ignore_index=True)
# Add info of all previously excluded samples (keep only columns required)
excludedIX = [i for i, x in enumerate(data.excludedFlag) if x == 'Samples']
for i in excludedIX:
sampleMetadataExcluded = pandas.concat([sampleMetadataExcluded, data.sampleMetadataExcluded[i]], ignore_index=True)
sampleMetadataExcluded = sampleMetadataExcluded[sampleMetadataExcluded.columns.intersection(cols)]

# Sample type masks, and only those marked as 'sample' or 'unknown' flagged
excludedMasks = generateTypeRoleMasks(sampleMetadataExcluded)
Expand Down

0 comments on commit 8d2b220

Please sign in to comment.