Skip to content

Commit a3dea57

Browse files
committed
Update to util.py and tests
1 parent 4163fe0 commit a3dea57

File tree

4 files changed

+276
-76
lines changed

4 files changed

+276
-76
lines changed

notebooks/resource-allocation/072023.ipynb

Lines changed: 25 additions & 20 deletions
Large diffs are not rendered by default.
7.17 MB
Binary file not shown.

qiita_db/test/test_util.py

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import qiita_db as qdb
2323

2424
from matplotlib.figure import Figure
25-
import numpy as np
25+
from matplotlib.axes import Axes
26+
import matplotlib.pyplot as plt
27+
28+
2629

2730
@qiita_test_checker()
2831
class DBUtilTestsBase(TestCase):
@@ -1305,28 +1308,65 @@ def test_quick_mounts_purge(self):
13051308
qdb.util.quick_mounts_purge()
13061309

13071310

1308-
class ResourceAllocationPlotTests():
1309-
def __init__(self) -> None:
1310-
self.PATH_TO_DATA = '''../../notebooks/resource-allocation/data/
1311-
jobs_2024-02-21.tsv.gz'''
1311+
class ResourceAllocationPlotTests(TestCase):
1312+
def setUp(self):
1313+
self.model_mem = [qdb.util.mem_model1, qdb.util.mem_model2,
1314+
qdb.util.mem_model3, qdb.util.mem_model4]
1315+
self.model_time = [qdb.util.time_model1, qdb.util.time_model2,
1316+
qdb.util.time_model3, qdb.util.time_model4]
1317+
self.PATH_TO_DATA = ('./qiita_db/test/test_data/jobs_2024-02-21.tsv.gz')
13121318
self.CNAME = "Validate"
13131319
self.SNAME = "Diversity types - alpha_vector"
1314-
1315-
1316-
def _get_return_value(self):
1317-
return qdb.util.resource_allocation_plot(self.PATH_TO_DATA, self.CNAME,
1318-
self.SNAME)
1319-
1320-
def _test_return_value_type(self):
1321-
fig, axs = self._get_return_value()
1322-
assert isinstance(fig, Figure), "Returned object is Matplotlib Figure"
1323-
1324-
# TODO test individual functions. E.g. constants returned by minimize.
1325-
1326-
1327-
1328-
1320+
self.COL_NAME = 'samples * columns'
1321+
self.df = pd.read_csv(self.PATH_TO_DATA, sep='\t',
1322+
dtype={'extra_info': str})
1323+
1324+
def test_plot_return(self):
1325+
# check the plot returns correct objects
1326+
fig1, axs1 = qdb.util.resource_allocation_plot(self.PATH_TO_DATA,
1327+
self.CNAME, self.SNAME)
1328+
self.assertIsInstance(
1329+
fig1, Figure,
1330+
"Returned object fig1 is not a Matplotlib Figure")
13291331

1332+
for ax in axs1:
1333+
self.assertIsInstance(
1334+
ax, Axes,
1335+
"Returned object axs1 is not a single Matplotlib Axes object")
1336+
1337+
def test_minimize_const(self):
1338+
_df = self.df[(self.df.cName == self.CNAME)
1339+
& (self.df.sName == self.SNAME)].copy()
1340+
_df.dropna(subset=['samples', 'columns'], inplace=True)
1341+
_df[self.COL_NAME] = _df.samples * _df['columns']
1342+
fig, axs = plt.subplots(ncols=2, figsize=(10, 4), sharey=False)
1343+
1344+
bm, options = qdb.util._resource_allocation_plot_helper(_df, axs[0], self.CNAME, self.SNAME, 'MaxRSSRaw', self.model_mem)
1345+
# check that the algorithm calculates correct constants and chooses
1346+
# correct model for MaxRSSRaw
1347+
k, a, b = options.x
1348+
kt, at, bt, = 1.0, 31054903.94825936, 92712486.20047534
1349+
1350+
self.assertEqual(bm, qdb.util.mem_model4, msg="""Best memory model
1351+
doesn't match""")
1352+
self.assertAlmostEqual(k, kt, msg="k not match expected in MaxRSSRaw")
1353+
self.assertAlmostEqual(a, at, msg="a not match expected in MaxRSSRaw")
1354+
self.assertAlmostEqual(b, bt, msg="b not match expected in MaxRSSRaw")
1355+
1356+
bm, options = qdb.util._resource_allocation_plot_helper(_df, axs[1], self.CNAME, self.SNAME, 'ElapsedRaw', self.model_time)
1357+
k, a, b = options.x
1358+
print(k, a, b)
1359+
kt = 19107.88377185
1360+
at = -36985.06461777
1361+
bt = -36985.06461796
1362+
1363+
# check that the algorithm calculates correct constants and chooses
1364+
# correct model for ElapsedRaw
1365+
self.assertEqual(bm, qdb.util.time_model1, msg="""Best time model
1366+
doesn't match""")
1367+
self.assertAlmostEqual(k, kt, msg="k not match expected in ElapsedRaw")
1368+
self.assertAlmostEqual(a, at, msg="a not match expected in ElapsedRaw")
1369+
self.assertAlmostEqual(b, bt, msg="b not match expected in ElapsedRaw")
13301370

13311371

13321372
STUDY_INFO = {

0 commit comments

Comments
 (0)