Skip to content

Commit

Permalink
define functions as const @charles-cowart
Browse files Browse the repository at this point in the history
  • Loading branch information
Gossty committed Mar 26, 2024
1 parent 6f8fa1c commit bf8ca21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
34 changes: 4 additions & 30 deletions qiita_db/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from matplotlib.figure import Figure
from matplotlib.axes import Axes
import matplotlib.pyplot as plt
import numpy as np


@qiita_test_checker()
Expand Down Expand Up @@ -1311,31 +1310,6 @@ def test_quick_mounts_purge(self):
class ResourceAllocationPlotTests(TestCase):
def setUp(self):

self.mem_model1 = (
lambda x, k, a, b: k * np.log(x) + x * a + b)
self.mem_model2 = (
lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 + a)
self.mem_model3 = (
lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**3)
self.mem_model4 = (
lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**2.5)
self.model_mem = [self.mem_model1, self.mem_model2,
self.mem_model3, self.mem_model4]

self.time_model1 = (
lambda x, k, a, b: a + b + np.log(x) * k)
self.time_model2 = (
lambda x, k, a, b: a + b * x + np.log(x) * k)
self.time_model3 = (
lambda x, k, a, b: a + b * np.log(x)**2 + np.log(x) * k)
self.time_model4 = (
lambda x, k, a, b: a * np.log(x)**3 + b * np.log(x)**2
+ np.log(x) * k)

self.model_time = [self.time_model1, self.time_model2,
self.time_model3, self.time_model4]
self.PATH_TO_DATA = ('./qiita_db/test/test_data/'
'jobs_2024-02-21.tsv.gz')
self.CNAME = "Validate"
Expand Down Expand Up @@ -1365,28 +1339,28 @@ def test_minimize_const(self):

bm, options = qdb.util._resource_allocation_plot_helper(
self.df, axs[0], self.CNAME, self.SNAME, 'MaxRSSRaw',
self.model_mem, self.col_name)
qdb.util.MODELS_MEM, self.col_name)
# check that the algorithm chooses correct model for MaxRSSRaw and
# has 0 failures
k, a, b = options.x
failures_df = qdb.util._resource_allocation_failures(
self.df, k, a, b, bm, self.col_name, 'MaxRSSRaw')
failures = failures_df.shape[0]
self.assertEqual(bm, self.mem_model4, msg="""Best memory model
self.assertEqual(bm, qdb.util.mem_model4, msg="""Best memory model
doesn't match""")
self.assertEqual(failures, 0, "Number of failures must be 0")

# check that the algorithm chooses correct model for ElapsedRaw and
# has 1 failure
bm, options = qdb.util._resource_allocation_plot_helper(
self.df, axs[1], self.CNAME, self.SNAME, 'ElapsedRaw',
self.model_time, self.col_name)
qdb.util.MODELS_TIME, self.col_name)
k, a, b = options.x
failures_df = qdb.util._resource_allocation_failures(
self.df, k, a, b, bm, self.col_name, 'ElapsedRaw')
failures = failures_df.shape[0]

self.assertEqual(bm, self.time_model1, msg="""Best time model
self.assertEqual(bm, qdb.util.time_model1, msg="""Best time model
doesn't match""")
self.assertEqual(failures, 1, "Number of failures must be 1")

Expand Down
35 changes: 20 additions & 15 deletions qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@
import numpy as np
from scipy.optimize import minimize

# memory constant functions defined for @resource_allocation_plot
mem_model1 = (lambda x, k, a, b: k * np.log(x) + x * a + b)
mem_model2 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 + a)
mem_model3 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**3)
mem_model4 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**2.5)
MODELS_MEM = [mem_model1, mem_model2, mem_model3, mem_model4]

# time constant functions defined for @resource_allocation_plot
time_model1 = (lambda x, k, a, b: a + b + np.log(x) * k)
time_model2 = (lambda x, k, a, b: a + b * x + np.log(x) * k)
time_model3 = (lambda x, k, a, b: a + b * np.log(x)**2 + np.log(x) * k)
time_model4 = (lambda x, k, a, b: a * np.log(x)**3 + b * np.log(x)**2
+ np.log(x) * k)

MODELS_TIME = [time_model1, time_model2, time_model3, time_model4]


def scrub_data(s):
r"""Scrubs data fields of characters not allowed by PostgreSQL
Expand Down Expand Up @@ -2350,26 +2368,13 @@ def resource_allocation_plot(file, cname, sname, col_name):

ax = axs[0]
# models for memory
mem_model1 = (lambda x, k, a, b: k * np.log(x) + x * a + b)
mem_model2 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 + a)
mem_model3 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**3)
mem_model4 = (lambda x, k, a, b: k * np.log(x) + b * np.log(x)**2 +
a * np.log(x)**2.5)
models = [mem_model1, mem_model2, mem_model3, mem_model4]
_resource_allocation_plot_helper(
df, ax, cname, sname, "MaxRSSRaw", models, col_name)
df, ax, cname, sname, "MaxRSSRaw", MODELS_MEM, col_name)

ax = axs[1]
# models for time
time_model1 = (lambda x, k, a, b: a + b + np.log(x) * k)
time_model2 = (lambda x, k, a, b: a + b * x + np.log(x) * k)
time_model3 = (lambda x, k, a, b: a + b * np.log(x)**2 + np.log(x) * k)
time_model4 = (lambda x, k, a, b: a * np.log(x)**3 + b * np.log(x)**2
+ np.log(x) * k)
models = [time_model1, time_model2, time_model3, time_model4]
_resource_allocation_plot_helper(
df, ax, cname, sname, "ElapsedRaw", models, col_name)
df, ax, cname, sname, "ElapsedRaw", MODELS_TIME, col_name)

return fig, axs

Expand Down

0 comments on commit bf8ca21

Please sign in to comment.