Skip to content

Commit

Permalink
fix: support more than one initial letters in variable and constraint…
Browse files Browse the repository at this point in the history
… naming (#397)
  • Loading branch information
FabianHofmann authored Dec 18, 2024
1 parent e5cc814 commit 3fb5b6c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 5 additions & 3 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Release Notes
=============

.. Upcoming Version
.. ----------------
Upcoming Version
----------------

* Solution files that following a different naming scheme of variables and constraints using more than on initial letter in the prefix (e.g. `col123`, `row456`) are now supported.

Version 0.4.3
--------------

* When creating slices for variables and constraints (important for the `solve` function), the slicing is now fixed in case now dimension to slice is available.
* When creating slices for variables and constraints (important for the `solve` function), the slicing is now fixed in case no dimension to slice is available.
* Added a pandas priority attribute. With this change, the operation with pandas objects is now prioritizing linopy objects over pandas objects. This is useful when the using linopy objects in arithmetic operations with pandas objects, e.g. `a * x` where `a` is a pandas Series/DataFrame and `x` is a linopy variable.
* The method :meth:`model.to_file <linopy.model.Model.to_file>` now includes a progress argument to enable or disable the progress bar while writing.

Expand Down
13 changes: 13 additions & 0 deletions linopy/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,19 @@ def _remap(array, mapping):
return mapping[array.ravel()].reshape(array.shape)


def count_initial_letters(word: str):
"""
Count the number of initial letters in a word.
"""
count = 0
for char in word:
if char.isalpha():
count += 1
else:
break
return count


def replace_by_map(ds, mapping):
"""
Replace values in a DataArray by a one-dimensional mapping.
Expand Down
5 changes: 4 additions & 1 deletion linopy/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pandas as pd
from pandas.core.series import Series

from linopy.common import count_initial_letters
from linopy.constants import (
Result,
Solution,
Expand Down Expand Up @@ -138,7 +139,9 @@ def set_int_index(series: Series) -> Series:
"""
Convert string index to int index.
"""
series.index = series.index.str[1:].astype(int)
if not series.empty:
cutoff = count_initial_letters(str(series.index[0]))
series.index = series.index.str[cutoff:].astype(int)
return series


Expand Down

0 comments on commit 3fb5b6c

Please sign in to comment.