Here is a draft for exception system.
Sometimes, we comes to a situation that should stop the process and inform the users are defined as exceptions in python.
Built-in Exceptions
The error is included in exception.
The pyStoNED brings in at least 2 types of exceptions here.
Basic exception:
Additive CNLS with CRS
The additive CNLS model with CRS does not exist (or be needed).
Hence when creating and additive CNLS model with CRS should raise an exception, since it is not an error but an exception of existing model.
The following codes may halt and bring out an exception
# import packages
from pystoned import CNLS
from pystoned.constant import CET_ADDI, FUN_PROD, OPT_LOCAL, RTS_CRS
from pystoned.dataset import load_Finnish_electricity_firm
# import Finnish electricity distribution firms data
data = load_Finnish_electricity_firm(x_select=['Energy', 'Length', 'Customers'],
y_select=['TOTEX'])
# define and solve the CNLS model
model = CNLS.CNLS(y=data.y, x=data.x, z=None,
cet = CET_ADDI, fun = FUN_PROD, rts = RTS_CRS)
Please help me justify the discussion above and polish the exception message.
Retrieving variables without optimization
User should optimize the model before retrieving and printing any variables.
If not, the program will halt the program and inform the users to optimize the model.
The following codes may halt and bring out an exception
# import packages
from pystoned import CNLS
from pystoned.constant import CET_ADDI, FUN_PROD, OPT_LOCAL, RTS_VRS
from pystoned.dataset import load_Finnish_electricity_firm
# import Finnish electricity distribution firms data
data = load_Finnish_electricity_firm(x_select=['Energy', 'Length', 'Customers'],
y_select=['TOTEX'])
# define and solve the CNLS model
model = CNLS.CNLS(y=data.y, x=data.x, z=None,
cet = CET_ADDI, fun = FUN_PROD, rts = RTS_VRS)
model.display_alpha()
Value error:
Construct model with unknown parameters
User should construct a model with constant labels in pystoned.constant
.
If a random string is giving, the program will halt the program and inform the users the model parameter is not defined.
This example construct a model with a random string as cet, causing the value error.
from pystoned import CNLS
from pystoned.constant import CET_ADDI, FUN_PROD, OPT_LOCAL, RTS_VRS
from pystoned.dataset import load_Finnish_electricity_firm
# import Finnish electricity distribution firms data
data = load_Finnish_electricity_firm(x_select=['Energy', 'Length', 'Customers'],
y_select=['TOTEX'])
# define and solve the CNLS model
model = CNLS.CNLS(y=data.y, x=data.x, z=None,
cet = "Not an CET label", fun = FUN_PROD, rts = RTS_VRS)
Note: This does not affect the default setting.
Invalid email address
When users using remote optimization, the user may use incorrect string(not an email address and OPT_LOCAL label).
This should leads to a halt and informs the user.
# import packages
from pystoned import CNLS
from pystoned.constant import CET_ADDI, FUN_PROD, OPT_LOCAL, RTS_VRS
from pystoned.dataset import load_Finnish_electricity_firm
# import Finnish electricity distribution firms data
data = load_Finnish_electricity_firm(x_select=['Energy', 'Length', 'Customers'],
y_select=['TOTEX'])
# define and solve the CNLS model
model = CNLS.CNLS(y=data.y, x=data.x, z=None,
cet = CET_ADDI, fun = FUN_PROD, rts = RTS_VRS)
model.optimize(email="NotAnEmailAddress")
Optimization multiplicative model without specifying solvers.
When users using local optimization, the user should specify the solver for optimization.
This should leads to a halt and informs the user to choose a installed solver.
# import packages
from pystoned import CNLS
from pystoned.constant import CET_MULT, FUN_PROD, OPT_LOCAL, RTS_VRS
from pystoned.dataset import load_Finnish_electricity_firm
# import Finnish electricity distribution firms data
data = load_Finnish_electricity_firm(x_select=['Energy', 'Length', 'Customers'],
y_select=['TOTEX'])
# define and solve the CNLS model
model = CNLS.CNLS(y=data.y, x=data.x, z=None,
cet = CET_MULT, fun = FUN_PROD, rts = RTS_VRS)
model.optimize(email=OPT_LOCAL)
These modification is a draft for discussing error/exception types, situation, and the messages.
Hence only the CNLS and the utils/tools module are modified as examples.
Any other exceptions and errors can be included and discussed!
Thanks!
Note: This part may not be included in the document. Since the document should indicate the right way to use the program, and here is for prevention of wrong ways.