Skip to content

Update code quality configuration #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Include *.ipynb in ruff format checking
Format 2 files.
  • Loading branch information
khaeru committed Jan 20, 2025
commit 48989cc7e1d6e017ebe6f634dd69a184123eddba
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -112,10 +112,6 @@ markers = [
]
tmp_path_retention_policy = "none"

[tool.ruff]
# TEMPORARY Exclude tutorial files
extend-exclude = ["*.ipynb"]

[tool.ruff.lint]
select = ["C9", "E", "F", "I", "W"]
# FIXME the following exceed this limit
74 changes: 42 additions & 32 deletions tutorial/transport/py_transport.ipynb
Original file line number Diff line number Diff line change
@@ -54,8 +54,9 @@
"metadata": {},
"outputs": [],
"source": [
"# load required packages \n",
"# Import required packages\n",
"import pandas as pd\n",
"\n",
"import ixmp"
]
},
@@ -65,7 +66,7 @@
"metadata": {},
"outputs": [],
"source": [
"# launch the ix modeling platform using the default back end\n",
"# Launch the ix modeling platform using the default back end\n",
"mp = ixmp.Platform()\n",
"\n",
"# The following lines have the same effect:\n",
@@ -79,14 +80,14 @@
"metadata": {},
"outputs": [],
"source": [
"# details for creating a new scenario in the ix modeling platform \n",
"# details for creating a new scenario in the ix modeling platform\n",
"model = \"transport problem\"\n",
"scenario = \"standard\"\n",
"annot = \"Dantzig's transportation problem for illustration and testing\" \n",
"annot = \"Dantzig's transportation problem for illustration and testing\"\n",
"\n",
"# initialize a new ixmp.Scenario\n",
"# the parameter version='new' indicates that this is a new scenario instamce\n",
"scen = ixmp.Scenario(mp, model, scenario, version='new', annotation=annot)"
"scen = ixmp.Scenario(mp, model, scenario, version=\"new\", annotation=annot)"
]
},
{
@@ -114,11 +115,11 @@
"metadata": {},
"outputs": [],
"source": [
"# define the sets of locations of canning plants and markets \n",
"# define the sets of locations of canning plants and markets\n",
"scen.init_set(\"i\")\n",
"scen.add_set(\"i\", [\"seattle\", \"san-diego\"])\n",
"scen.init_set(\"j\")\n",
"scen.add_set(\"j\", [\"new-york\", \"chicago\", \"topeka\"]) "
"scen.add_set(\"j\", [\"new-york\", \"chicago\", \"topeka\"])"
]
},
{
@@ -128,7 +129,7 @@
"outputs": [],
"source": [
"# display the set 'i'\n",
"scen.set('i')"
"scen.set(\"i\")"
]
},
{
@@ -161,19 +162,19 @@
"metadata": {},
"outputs": [],
"source": [
"# capacity of plant i in cases \n",
"# add parameter elements one-by-one (string and value) \n",
"# capacity of plant i in cases\n",
"# add parameter elements one-by-one (string and value)\n",
"scen.init_par(\"a\", idx_sets=\"i\")\n",
"scen.add_par(\"a\", \"seattle\", 350, \"cases\")\n",
"scen.add_par(\"a\", \"san-diego\", 600, \"cases\")\n",
"\n",
"# demand at market j in cases \n",
"# add parameter elements as dataframe (with index names) \n",
"# demand at market j in cases\n",
"# add parameter elements as dataframe (with index names)\n",
"scen.init_par(\"b\", idx_sets=\"j\")\n",
"b_data = [\n",
" {'j': \"new-york\", 'value': 325, 'unit': \"cases\"},\n",
" {'j': \"chicago\", 'value': 300, 'unit': \"cases\"},\n",
" {'j': \"topeka\", 'value': 275, 'unit': \"cases\"}\n",
" {\"j\": \"new-york\", \"value\": 325, \"unit\": \"cases\"},\n",
" {\"j\": \"chicago\", \"value\": 300, \"unit\": \"cases\"},\n",
" {\"j\": \"topeka\", \"value\": 275, \"unit\": \"cases\"},\n",
"]\n",
"b = pd.DataFrame(b_data)\n",
"scen.add_par(\"b\", b)"
@@ -185,7 +186,7 @@
"metadata": {},
"outputs": [],
"source": [
"scen.par('b')"
"scen.par(\"b\")"
]
},
{
@@ -204,14 +205,14 @@
"metadata": {},
"outputs": [],
"source": [
"# distance in thousands of miles \n",
"# distance in thousands of miles\n",
"scen.init_par(\"d\", idx_sets=[\"i\", \"j\"])\n",
"# add more parameter elements as dataframe by index names \n",
"# add more parameter elements as dataframe by index names\n",
"d_data = [\n",
" {'i': \"seattle\", 'j': \"new-york\", 'value': 2.5, 'unit': \"km\"},\n",
" {'i': \"seattle\", 'j': \"chicago\", 'value': 1.7, 'unit': \"km\"},\n",
" {'i': \"seattle\", 'j': \"topeka\", 'value': 1.8, 'unit': \"km\"},\n",
" {'i': \"san-diego\", 'j': \"new-york\", 'value': 2.5, 'unit': \"km\"},\n",
" {\"i\": \"seattle\", \"j\": \"new-york\", \"value\": 2.5, \"unit\": \"km\"},\n",
" {\"i\": \"seattle\", \"j\": \"chicago\", \"value\": 1.7, \"unit\": \"km\"},\n",
" {\"i\": \"seattle\", \"j\": \"topeka\", \"value\": 1.8, \"unit\": \"km\"},\n",
" {\"i\": \"san-diego\", \"j\": \"new-york\", \"value\": 2.5, \"unit\": \"km\"},\n",
"]\n",
"d = pd.DataFrame(d_data)\n",
"scen.add_par(\"d\", d)\n",
@@ -223,9 +224,13 @@
},
{
"cell_type": "raw",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"Scalar f freight in dollars per case per thousand miles /90/ ; "
"Scalar f freight in dollars per case per thousand miles /90/ ;"
]
},
{
@@ -234,8 +239,8 @@
"metadata": {},
"outputs": [],
"source": [
"# cost per case per 1000 miles \n",
"# initialize scalar with a value and a unit (and optionally a comment) \n",
"# cost per case per 1000 miles\n",
"# initialize scalar with a value and a unit (and optionally a comment)\n",
"scen.init_scalar(\"f\", 90.0, \"USD/km\")"
]
},
@@ -255,8 +260,8 @@
"# commit new scenario to the database\n",
"# no changes can then be made to the scenario data until a check-out is performed\n",
"comment = \"importing Dantzig's transport problem for illustration\"\n",
"comment += \" and testing of the Python interface using a generic datastructure\" \n",
"scen.commit(comment) \n",
"comment += \" and testing of the Python interface using a generic datastructure\"\n",
"scen.commit(comment)\n",
"\n",
"# set this new scenario as the default version for the model/scenario name\n",
"scen.set_as_default()"
@@ -273,12 +278,16 @@
},
{
"cell_type": "raw",
"metadata": {},
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"Variables\n",
" x(i,j) shipment quantities in cases\n",
" z total transportation costs in thousands of dollars ;\n",
" \n",
"\n",
"Equations\n",
" cost define objective function\n",
" supply(i) observe supply limit at plant i\n",
@@ -321,7 +330,7 @@
"metadata": {},
"outputs": [],
"source": [
"scen.solve(model='dantzig')"
"scen.solve(model=\"dantzig\")"
]
},
{
@@ -357,7 +366,8 @@
"metadata": {},
"outputs": [],
"source": [
"# display the quantities and marginals (=shadow prices) of the demand balance constraints\n",
"# Display the quantities and marginals (=shadow prices) of the demand balance\n",
"# constraints\n",
"scen.equ(\"demand\")"
]
},
64 changes: 37 additions & 27 deletions tutorial/transport/py_transport_scenario.ipynb
Original file line number Diff line number Diff line change
@@ -38,8 +38,7 @@
"metadata": {},
"outputs": [],
"source": [
"# load required packages\n",
"import pandas as pd\n",
"# Import required packages\n",
"import ixmp"
]
},
@@ -60,8 +59,8 @@
"outputs": [],
"source": [
"# Model and scenario name for Dantzig's transport problem\n",
"model = 'canning problem'\n",
"scenario = 'standard'"
"model = \"canning problem\"\n",
"scenario = \"standard\""
]
},
{
@@ -106,7 +105,8 @@
"outputs": [],
"source": [
"from ixmp.testing import make_dantzig\n",
"scen = make_dantzig(mp, solve='.')"
"\n",
"scen = make_dantzig(mp, solve=\".\")"
]
},
{
@@ -135,8 +135,9 @@
"metadata": {},
"outputs": [],
"source": [
"# show only the distances for connections from Seattle by filtering the pandas.DataFrame returned above\n",
"d[d['i'] == \"seattle\"]"
"# Show only the distances for connections from Seattle by filtering the pandas.DataFrame\n",
"# returned above\n",
"d[d[\"i\"] == \"seattle\"]"
]
},
{
@@ -148,8 +149,8 @@
"# for faster access or more complex filtering,\n",
"# it may be easier to only load specific parameter elements using a dictionary\n",
"ele_filter = {}\n",
"ele_filter['i'] = ['seattle']\n",
"ele_filter['j'] = ['chicago', 'topeka']\n",
"ele_filter[\"i\"] = [\"seattle\"]\n",
"ele_filter[\"j\"] = [\"chicago\", \"topeka\"]\n",
"\n",
"d_filtered = scen.par(\"d\", ele_filter)\n",
"d_filtered"
@@ -172,7 +173,12 @@
"outputs": [],
"source": [
"# create a new scenario by cloning the scenario (without keeping the solution)\n",
"scen_detroit = scen.clone(model=model, scenario='detroit', annotation='extend the Transport problem by a new city', keep_solution=False)"
"scen_detroit = scen.clone(\n",
" model=model,\n",
" scenario=\"detroit\",\n",
" annotation=\"extend the Transport problem by a new city\",\n",
" keep_solution=False,\n",
")"
]
},
{
@@ -192,13 +198,13 @@
"outputs": [],
"source": [
"# reduce demand in chicago\n",
"scen_detroit.add_par('b', 'chicago', 200, 'cases')\n",
"scen_detroit.add_par(\"b\", \"chicago\", 200, \"cases\")\n",
"\n",
"# add a new city with demand and distances\n",
"scen_detroit.add_set('j', 'detroit')\n",
"scen_detroit.add_par('b', 'detroit', 150, 'cases')\n",
"scen_detroit.add_par('d', ['seattle', 'detroit'], 1.7, 'cases')\n",
"scen_detroit.add_par('d', ['san-diego', 'detroit'], 1.9, 'cases')"
"scen_detroit.add_set(\"j\", \"detroit\")\n",
"scen_detroit.add_par(\"b\", \"detroit\", 150, \"cases\")\n",
"scen_detroit.add_par(\"d\", [\"seattle\", \"detroit\"], 1.7, \"cases\")\n",
"scen_detroit.add_par(\"d\", [\"san-diego\", \"detroit\"], 1.9, \"cases\")"
]
},
{
@@ -226,7 +232,7 @@
"metadata": {},
"outputs": [],
"source": [
"scen_detroit.solve(model='dantzig')"
"scen_detroit.solve(model=\"dantzig\")"
]
},
{
@@ -249,21 +255,21 @@
"outputs": [],
"source": [
"# display the objective value of the solution in the baseline scenario\n",
"scen.var('z')"
"scen.var(\"z\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"name": "scen-detroit-z"
}
"jupyter": {
"name": "scen-detroit-z"
}
},
"outputs": [],
"source": [
"# display the objective value of the solution in the \"detroit\" scenario\n",
"scen_detroit.var('z')"
"scen_detroit.var(\"z\")"
]
},
{
@@ -272,8 +278,9 @@
"metadata": {},
"outputs": [],
"source": [
"# display the quantities transported from canning plants to demand locations in the baseline scenario\n",
"scen.var('x')"
"# Display the quantities transported from canning plants to demand locations in the\n",
"# baseline scenario\n",
"scen.var(\"x\")"
]
},
{
@@ -282,8 +289,9 @@
"metadata": {},
"outputs": [],
"source": [
"# display the quantities transported from canning plants to demand locations in the \"detroit\" scenario\n",
"scen_detroit.var('x')"
"# Display the quantities transported from canning plants to demand locations in the\n",
"# \"detroit\" scenario\n",
"scen_detroit.var(\"x\")"
]
},
{
@@ -292,7 +300,8 @@
"metadata": {},
"outputs": [],
"source": [
"# display the quantities and marginals (=shadow prices) of the demand balance constraints in the baseline scenario\n",
"# Display the quantities and marginals (=shadow prices) of the demand balance\n",
"# constraints in the baseline scenario\n",
"scen.equ(\"demand\")"
]
},
@@ -302,7 +311,8 @@
"metadata": {},
"outputs": [],
"source": [
"# display the quantities and marginals (=shadow prices) of the demand balance constraints in the \"detroit\" scenario\n",
"# Display the quantities and marginals (=shadow prices) of the demand balance\n",
"# constraints in the \"detroit\" scenario\n",
"scen_detroit.equ(\"demand\")"
]
},