Skip to content

Commit

Permalink
Small fixes (#484)
Browse files Browse the repository at this point in the history
* Fix errors when loading strain designs

* New FBA error message with community editions

* Better handling of community edition errors

* Add XLSX generation with all in and out fluxes

* Fix missing newline character in net conversion

* Refactoring; More community errors; Qt fix

* More to core_gui

* Remove special Gurobi handling; Point size fix

* Better language

* Remove edge error case; Import fix

* simplification

* Fix RGB color function argument typing

---------

Co-authored-by: axelvonkamp <[email protected]>
  • Loading branch information
Paulocracy and axelvonkamp authored Sep 22, 2023
1 parent b4cc359 commit 46e7a36
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cnapy/appdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ def compute_color_heat(self, value: Tuple[float, float], low, high):
h = 255
else:
h = mean * 255 / high
return QColor.fromRgb(255-h, 255, 255 - h)
return QColor.fromRgbF(255-h, 255, 255 - h)
else:
if low == 0.0:
h = 255
else:
h = mean * 255 / low
return QColor.fromRgb(255, 255 - h, 255 - h)
return QColor.fromRgbF(255, 255 - h, 255 - h)

def low_and_high(self) -> Tuple[int, int]:
low = 0
Expand Down
2 changes: 1 addition & 1 deletion cnapy/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self):
self.qapp.setStyle("fusion")
config_file_version = self.read_config()
font = self.qapp.font()
font.setPointSize(self.appdata.font_size)
font.setPointSizeF(self.appdata.font_size)
self.qapp.setFont(font)
self.window = MainWindow(self.appdata)
self.appdata.window = self.window
Expand Down
16 changes: 0 additions & 16 deletions cnapy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
from collections import defaultdict
from typing import Dict, Tuple, List
from collections import Counter
import gurobipy
import numpy
import cobra
from cobra.util.array import create_stoichiometric_matrix
from cobra.core.dictlist import DictList
from optlang.symbolics import Zero, Add
from qtpy.QtWidgets import QMessageBox

import efmtool_link.efmtool4cobra as efmtool4cobra
import efmtool_link.efmtool_extern as efmtool_extern
Expand Down Expand Up @@ -369,17 +367,3 @@ def replace_ids(dict_list: DictList, annotation_key: str, unambiguous_only: bool
pass
if len(candidates) > 0 and old_id == entry.id:
print("Could not find a new ID for", entry.id, "in", candidates)

# TODO: should not be in the core module
def model_optimization_with_exceptions(model: cobra.Model):
try:
return model.optimize()
except gurobipy.GurobiError as error:
msgBox = QMessageBox()
msgBox.setWindowTitle("Gurobi Error!")
msgBox.setText("Calculation failed due to the following Gurobi solver error " +\
"(if this error cannot be resolved,\ntry using a different solver by changing " +\
"it under 'Config->Configure cobrapy'):\n"+error.message+\
"\nNOTE: Another error message will follow, you can safely ignore it.")
msgBox.setIcon(QMessageBox.Warning)
msgBox.show()
37 changes: 37 additions & 0 deletions cnapy/core_gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import gurobipy
import io
import traceback
import cobra
from qtpy.QtWidgets import QMessageBox


def except_likely_community_model_error() -> None:
"""Shows a message in the case that using a (size-limited) community edition solver version probably caused an error."""
community_error_text = "Solver error. One possible reason: You set CPLEX or Gurobi as solver although you only use their\n"+\
"Community edition which only work for small models. To solve this, either follow the instructions under\n"+\
"'Config->Configure IBM CPLEX full version' or 'Config->Configure Gurobi full version', or use a different solver such as GLPK."
msgBox = QMessageBox()
msgBox.setWindowTitle("Error")
msgBox.setText(community_error_text)
msgBox.setIcon(QMessageBox.Warning)
msgBox.exec()


def get_last_exception_string() -> str:
output = io.StringIO()
traceback.print_exc(file=output)
return output.getvalue()


def has_community_error_substring(string: str) -> bool:
return ("Model too large for size-limited license" in string) or ("1016: Community Edition" in string)


def model_optimization_with_exceptions(model: cobra.Model) -> None:
try:
return model.optimize()
except Exception:
exstr = get_last_exception_string()
# Check for substrings of Gurobi and CPLEX community edition errors
if has_community_error_substring(exstr):
except_likely_community_model_error()
Loading

0 comments on commit 46e7a36

Please sign in to comment.