Breakthrough-Energy/PowerSimData

Grid dataframe values are shared via the MemoryCache

danielolsen opened this issue · 3 comments

If we modify the values for a dataframe for a Grid object that's been loaded into the Memory cache, they seem to be modified in new instantiated grids of the same interconnect.

>>> from powersimdata import Grid
>>> grid1 = Grid("Texas")
Reading bus.csv
Reading plant.csv
Reading gencost.csv
Reading branch.csv
Reading dcline.csv
Reading sub.csv
Reading bus2sub.csv
Reading zone.csv
>>> grid1.plant.groupby("type").sum().Pmax
type
coal       14501.59
hydro        555.10
ng         68642.42
nuclear     5138.60
solar       2460.20
wind       19062.63
Name: Pmax, dtype: float64
>>> grid1.plant.Pmax = 0
>>> grid1.plant.groupby("type").sum().Pmax
type
coal       0
hydro      0
ng         0
nuclear    0
solar      0
wind       0
Name: Pmax, dtype: int64
>>> grid2 = Grid("Texas")
>>> grid2.plant.groupby("type").sum().Pmax
type
coal       0
hydro      0
ng         0
nuclear    0
solar      0
wind       0
Name: Pmax, dtype: int64
>>> grid1 is grid2
False
>>> grid1.plant is grid2.plant
False

The following test reproduces it in a minimum working example:

def test_mem_cache_put_version_never_changes():
    cache = MemoryCache()
    key = cache_key("foo", 4)
    obj = {"key1": 42}
    cache.put(key, obj)
    obj["key2"] = 8675309
    assert "key2" not in cache.get(key)

Another test:

>>> from powersimdata import Grid
>>> g1 = Grid("Texas")
Reading bus.csv
Reading plant.csv
Reading gencost.csv
Reading branch.csv
Reading dcline.csv
Reading sub.csv
Reading bus2sub.csv
Reading zone.csv
>>> g2 = Grid("Texas")
>>> g1.plant.Pmax = 0
>>> g2.plant.Pmax.sum()
110360.54000000001
>>> g3 = Grid("Texas")
>>> g3.plant.Pmax.sum()
110360.54000000001

Closed via #444