Skip to content

Commit c9ce88b

Browse files
committed
Updates to @antgonza comments
1 parent 7fb202a commit c9ce88b

File tree

2 files changed

+73
-80
lines changed

2 files changed

+73
-80
lines changed

qiita_db/meta_util.py

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -555,58 +555,42 @@ def generate_plugin_releases():
555555
f(redis_key, v)
556556

557557

558-
def _build_software_commands_col_names_object(active):
559-
'''
560-
Helper function for update_resource_allocation_redis
558+
def update_resource_allocation_redis(active=True, verbose=False):
559+
"""Updates redis with plots and information about current software.
561560
562-
Constructs a nested dictionary containing software commands and associated
563-
column names from the database.
564-
'''
561+
Parameters
562+
----------
563+
active: boolean, optional
564+
Defaults to True. Should only be False when testing.
565565
566-
col_name_list = []
567-
with qdb.sql_connection.TRN:
568-
sql = ''' SELECT * FROM qiita.resource_allocation_column_names; '''
569-
qdb.sql_connection.TRN.add(sql)
570-
res = qdb.sql_connection.TRN.execute_fetchindex()
566+
verbose: boolean, optional
567+
Defaults to False. Prints status on what functin
571568
572-
for col_name in res:
573-
col_name_list.append(col_name[1])
569+
"""
570+
time = datetime.now().strftime('%m-%d-%y')
574571

575-
software_list = [s for s in qdb.software.Software.iter(active=active)]
576-
software_commands = defaultdict(lambda: defaultdict(
577-
lambda: defaultdict(list)))
572+
# Retreave available col_name for commands
573+
with qdb.sql_connection.TRN:
574+
sql = 'SELECT col_name FROM qiita.resource_allocation_column_names;'
575+
qdb.sql_connection.TRN.add(sql)
576+
col_names_list = qdb.sql_connection.TRN.execute_fetchflatten()
578577

578+
# Retreave available software
579+
software_list = list(qdb.software.Software.iter(active=active))
580+
scommands = {}
579581
for software in software_list:
580582
sname = software.name
581583
sversion = software.version
582584

583-
for command in software.commands:
584-
cmd_name = command.name
585-
for col in col_name_list:
586-
software_commands[sname][sversion][cmd_name].append(col)
587-
588-
final_obj = {
589-
sname: {
590-
sversion: dict(commands)
591-
for sversion, commands in dict(versions).items()
592-
}
593-
for sname, versions in dict(software_commands).items()
594-
}
595-
return final_obj
596-
597-
598-
def update_resource_allocation_redis(active=True):
599-
"""Updates redis with plots and information about current software.
600-
601-
Parameters
602-
----------
603-
active: boolean, optional
604-
Defaults to True. Should only be False when testing.
585+
if sname not in scommands:
586+
scommands[sname] = {}
605587

606-
"""
607-
time = datetime.now().strftime('%m-%d-%y')
588+
if sversion not in scommands[sname]:
589+
scommands[sname][sversion] = {}
608590

609-
scommands = _build_software_commands_col_names_object(active)
591+
for command in software.commands:
592+
cmd_name = command.name
593+
scommands[sname][sversion][cmd_name] = col_names_list
610594

611595
redis_key = 'resources:commands'
612596
r_client.set(redis_key, str(scommands))
@@ -615,38 +599,41 @@ def update_resource_allocation_redis(active=True):
615599
for version, commands in versions.items():
616600
for cname, col_name_list in commands.items():
617601
df = retrieve_resource_data(cname, sname, version, COLUMNS)
618-
print(("Retrieving allocation resources for " +
619-
f" software: {sname}" +
620-
f" version: {version}" +
621-
f" command: {cname}"))
622-
if len(df) == 0:
623-
print(("No allocation resources available for" +
602+
if verbose:
603+
print(("Retrieving allocation resources for " +
624604
f" software: {sname}" +
625605
f" version: {version}" +
626606
f" command: {cname}"))
607+
if len(df) == 0:
608+
if verbose:
609+
print(("No allocation resources available for" +
610+
f" software: {sname}" +
611+
f" version: {version}" +
612+
f" command: {cname}"))
627613
continue
628614
# column_name_str looks like col1*col2*col3, etc
629615
for col_name_str in col_name_list:
630-
df_copy = df.copy()
631616
new_column = None
632617
col_name_split = col_name_str.split('*')
633-
df_copy.dropna(subset=col_name_split, inplace=True)
618+
df_copy = df.dropna(subset=col_name_split)
634619

635620
# Create a column with the desired columns
636621
for curr_column in col_name_split:
637622
if new_column is None:
638623
new_column = df_copy[curr_column]
639624
else:
640625
new_column *= df_copy[curr_column]
641-
print(("Building resource allocation plot for " +
642-
f" software: {sname}" +
643-
f" version: {version}" +
644-
f" command: {cname}" +
645-
f" column name: {col_name_str}"))
626+
if verbose:
627+
print(("Building resource allocation plot for " +
628+
f" software: {sname}" +
629+
f" version: {version}" +
630+
f" command: {cname}" +
631+
f" column name: {col_name_str}"))
646632

647633
fig, axs = resource_allocation_plot(df_copy,
648634
col_name_str,
649-
new_column)
635+
new_column,
636+
verbose=verbose)
650637
titles = [0, 0]
651638
images = [0, 0]
652639

@@ -697,11 +684,12 @@ def update_resource_allocation_redis(active=True):
697684
("title_mem", titles[0], r_client.set),
698685
("title_time", titles[1], r_client.set)
699686
]
700-
print(("Saving resource allocation image for " +
701-
f" software: {sname}" +
702-
f" version: {version}" +
703-
f" command: {cname}" +
704-
f" column name: {col_name_str}"))
687+
if verbose:
688+
print(("Saving resource allocation image for " +
689+
f" software: {sname}" +
690+
f" version: {version}" +
691+
f" command: {cname}" +
692+
f" column name: {col_name_str}"))
705693

706694
for k, v, f in values:
707695
redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (

qiita_db/util.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,7 @@ def send_email(to, subject, body):
23262326
smtp.close()
23272327

23282328

2329-
def resource_allocation_plot(df, col_name_str, curr_column):
2329+
def resource_allocation_plot(df, col_name_str, curr_column, verbose=False):
23302330
"""Builds resource allocation plot for given filename and jobs
23312331
23322332
Parameters
@@ -2350,22 +2350,27 @@ def resource_allocation_plot(df, col_name_str, curr_column):
23502350
df[col_name_str] = curr_column
23512351

23522352
# models for memory
2353-
print("\tCalculating best model for memory")
2353+
if verbose:
2354+
print("\tCalculating best model for memory")
23542355
_resource_allocation_plot_helper(
2355-
df, ax, "MaxRSSRaw", mem_models, col_name_str)
2356+
df, ax, "MaxRSSRaw", mem_models, col_name_str, verbose=verbose)
23562357
ax = axs[1]
23572358

23582359
# models for time
2359-
print("\tCalculating best model for time")
2360+
if verbose:
2361+
print("\tCalculating best model for time")
23602362
_resource_allocation_plot_helper(
2361-
df, ax, "ElapsedRaw", time_models, col_name_str)
2363+
df, ax, "ElapsedRaw", time_models, col_name_str, verbose=verbose)
23622364
return fig, axs
23632365

23642366

23652367
def _retrieve_equations():
23662368
'''
23672369
Helper function for resource_allocation_plot.
23682370
Retrieves equations from db. Creates dictionary for memory and time models.
2371+
This function is needed because it utilizes np as a part of eval() below.
2372+
In test_util.py we need to retrieve equations without importing np to
2373+
comply with PEP8 styling standard.
23692374
23702375
Returns
23712376
-------
@@ -2379,19 +2384,20 @@ def _retrieve_equations():
23792384
time_models = {}
23802385
res = []
23812386
with qdb.sql_connection.TRN:
2382-
sql = ''' SELECT * FROM qiita.resource_allocation_equations; '''
2387+
sql = '''SELECT equation_name, expression
2388+
FROM qiita.resource_allocation_equations;'''
23832389
qdb.sql_connection.TRN.add(sql)
23842390
res = qdb.sql_connection.TRN.execute_fetchindex()
23852391
for models in res:
2386-
if 'mem' in models[1]:
2387-
memory_models[models[1]] = {
2388-
"equation_name": models[2],
2389-
"equation": lambda x, k, a, b: eval(models[2])
2392+
if 'mem' in models[0]:
2393+
memory_models[models[0]] = {
2394+
"equation_name": models[1],
2395+
"equation": lambda x, k, a, b: eval(models[1])
23902396
}
23912397
else:
2392-
time_models[models[1]] = {
2393-
"equation_name": models[2],
2394-
"equation": lambda x, k, a, b: eval(models[2])
2398+
time_models[models[0]] = {
2399+
"equation_name": models[1],
2400+
"equation": lambda x, k, a, b: eval(models[1])
23952401
}
23962402
return (memory_models, time_models)
23972403

@@ -2452,7 +2458,7 @@ def retrieve_resource_data(cname, sname, version, columns):
24522458

24532459

24542460
def _resource_allocation_plot_helper(
2455-
df, ax, curr, models, col_name):
2461+
df, ax, curr, models, col_name, verbose=False):
24562462
"""Helper function for resource allocation plot. Builds plot for MaxRSSRaw
24572463
and ElapsedRaw
24582464
@@ -2517,15 +2523,14 @@ def _resource_allocation_plot_helper(
25172523
ax.set_ylabel(curr)
25182524
ax.set_xlabel(col_name)
25192525

2520-
print(f"\t\tFitting best model for {curr}; column {col_name}")
2526+
if verbose:
2527+
print(f"\t\tFitting best model for {curr}; column {col_name}")
25212528
# 50 - number of maximum iterations, 3 - number of failures we tolerate
25222529
best_model_name, best_model, options = _resource_allocation_calculate(
25232530
df, x_data, y_data, models, curr, col_name, 50, 3)
2524-
if options is None:
2525-
print(df)
2526-
print(x_data)
2527-
print(y_data)
2528-
print(f"\t\tSuccessfully chose best model for {curr}; column {col_name}")
2531+
if verbose:
2532+
print(
2533+
f"\t\tSuccessfully chose best model for {curr}; column {col_name}")
25292534
k, a, b = options.x
25302535
x_plot = np.array(sorted(df[col_name].unique()))
25312536
y_plot = best_model(x_plot, k, a, b)

0 commit comments

Comments
 (0)