Skip to content

Commit

Permalink
Fix #1076: suppress NumPy division warnings from analysis classes (#1079
Browse files Browse the repository at this point in the history
)

* Fix 1076 by suppressing 2 types of NumPy warnings in selected analysis classes.
* Satisfy mypy re: KnowledgeBaseEcoli has no attribute "compartments". mypy can't track the reflection like `setattr(self, attr_name, rows)` used in `KnowledgeBaseEcoli`.
  • Loading branch information
1fish2 authored Jun 2, 2021
1 parent 8b3d11d commit aaf6646
Show file tree
Hide file tree
Showing 28 changed files with 68 additions and 3 deletions.
18 changes: 15 additions & 3 deletions models/ecoli/analysis/analysisPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import os

import matplotlib as mp

from matplotlib import pyplot as plt
import numpy as np

from wholecell.utils import memory_debug, parallelization
from wholecell.utils import filepath as fp

Expand All @@ -33,6 +34,10 @@ class AnalysisPlot(metaclass=abc.ABCMeta):
cpus: allotted number of CPU cores; default (0) => all available cores
"""

#: Whether to suppress NumPy "divide" and "invalid" warnings like Theano
#: used to do. Override this in subclasses as needed.
_suppress_numpy_warnings = False

def __init__(self, cpus=0):
self.cpus = parallelization.cpus(cpus)
self._axeses = {}
Expand Down Expand Up @@ -98,14 +103,21 @@ def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
def plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile,
validationDataFile, metadata):
"""Public method to set up, make a plot, and cleanup."""
def do_plot():
self.do_plot(inputDir, plotOutDir, plotOutFileName, simDataFile,
validationDataFile, metadata)

if not os.path.isdir(inputDir):
raise RuntimeError('Input directory ({}) does not currently exist.'
.format(inputDir))
fp.makedirs(plotOutDir)

with memory_debug.detect_leaks(), mp.rc_context():
self.do_plot(inputDir, plotOutDir, plotOutFileName, simDataFile,
validationDataFile, metadata)
if self._suppress_numpy_warnings:
with np.errstate(divide='ignore'), np.errstate(invalid='ignore'):
do_plot()
else:
do_plot()

self._axeses = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


class Plot(cohortAnalysisPlot.CohortAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
plt.figure(figsize = (8.5, 11))

Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/cohort/kinetics_flux_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@


class Plot(cohortAnalysisPlot.CohortAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# Get all cells
ap = AnalysisPaths(variantDir, cohort_plot = True)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/cohort/promoter_probabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@


class Plot(cohortAnalysisPlot.CohortAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, variantDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = pickle.load(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
validation_data = cPickle.load(open(validationDataFile, "rb"))
sim_data = cPickle.load(open(simDataFile, "rb"))
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/charging_molecules.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def post_plot_formatting(ax, division_times, y_label, draw_horizontal=None, y_li


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = cPickle.load(f)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/growthAffectingPolymerases.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)

Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/kineticsFluxComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# Get all cells
ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
sim_data = cPickle.load(open(simDataFile, "rb"))
genomeLength = len(sim_data.process.replication.genome_sequence)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/ribosomeProduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)

Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/tf_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def subplot(gs, legend, title, tf_id, gene_id, t, active, bound, inactive, promo


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = pickle.load(f)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/multigen/transcriptionFrequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class Plot(multigenAnalysisPlot.MultigenAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, seedOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# Get all cells
ap = AnalysisPaths(seedOutDir, multi_gen_plot = True)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/allReactionFluxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
time = TableReader(os.path.join(simOutDir, "Main")).readColumn("time")
fbaResults = TableReader(os.path.join(simOutDir, "FBAResults"))
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/cell_wall_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
}

class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName,
simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
validation_data = cPickle.load(open(validationDataFile, "rb"))
sim_data = cPickle.load(open(simDataFile, "rb"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(
self, simOutDir, plotOutDir, plotOutFileName,
simDataFile, validationDataFile, metadata):
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/cotranscriptional_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = cPickle.load(f)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/equilibriumComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# Load data from KB
sim_data = cPickle.load(open(simDataFile, "rb"))
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/flagella_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
}

class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = pickle.load(f)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/inter_rnap_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
sim_data = cPickle.load(f)
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/kineticsFluxComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
sim_data = cPickle.load(open(simDataFile, 'rb'))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# read constraint data
enzymeKineticsReader = TableReader(os.path.join(simOutDir, "EnzymeKinetics"))
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/outer_membrane_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
}

class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName,
simDataFile, validationDataFile, metadata):
with open(simDataFile, 'rb') as f:
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/single/transcriptional_attenuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class Plot(singleAnalysisPlot.SingleAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, simOutDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
# Listeners used
transcription_reader = TableReader(os.path.join(simOutDir, 'TranscriptElongationListener'))
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/variant/growthConditionComparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class Plot(variantAnalysisPlot.VariantAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
ap = AnalysisPaths(inputDir, variant_plot = True)
all_cells = ap.get_cells()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
] # type: Sequence


@np.errstate(invalid='ignore') # "invalid value encountered in true_divide" in its own process
def analyze_variant(args):
# type: (Tuple[int, AnalysisPaths, List[str], np.ndarray, List[bool]]) -> tuple
'''
Expand Down
2 changes: 2 additions & 0 deletions models/ecoli/analysis/variant/remove_aa_inhibition.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def heatmap(gs, aa_idx, data, enzyme_order):


class Plot(variantAnalysisPlot.VariantAnalysisPlot):
_suppress_numpy_warnings = True

def do_plot(self, inputDir, plotOutDir, plotOutFileName, simDataFile, validationDataFile, metadata):
ap = AnalysisPaths(inputDir, variant_plot=True)
variants = ap.get_variants()
Expand Down
2 changes: 2 additions & 0 deletions reconstruction/ecoli/knowledge_base_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class KnowledgeBaseEcoli(object):
""" KnowledgeBaseEcoli """

def __init__(self):
self.compartments = [] # mypy can't track setattr(self, attr_name, rows)

# Load raw data from TSV files
for filename in LIST_OF_DICT_FILENAMES:
self._load_tsv(FLAT_DIR, os.path.join(FLAT_DIR, filename))
Expand Down

0 comments on commit aaf6646

Please sign in to comment.