modelx icon indicating copy to clipboard operation
modelx copied to clipboard

Writing models with such objects as DataFrames as refs or as Cells values

Open alebaran opened this issue 6 years ago • 16 comments

I need to create a cell with no arguments and some data inside. Is there a way to do it so that "write_model" will capture it?

alebaran avatar Oct 14 '19 08:10 alebaran

Are you referring to no-arg cells with simple values like the attached? Currently no way to write cells with values that are instances of user-defined classes as I wrote here: https://github.com/fumitoh/modelx/issues/8#issuecomment-536200063

NoArgCells.xlsx

fumitoh avatar Oct 14 '19 14:10 fumitoh

I'm looking for no-arg cells with pandas (DataFrame/Series) content. The same as new_cells_from_pandas, but without argument

alebaran avatar Oct 14 '19 14:10 alebaran

You can assign your pandas object to a ref, and refer to the ref from your cells. That's the only way available now. I'm working on dependency tracking on refs as well. Once it's available then you don't have much incentive to store constants in cells.

fumitoh avatar Oct 14 '19 14:10 fumitoh

I actually like the concept of storing all the data in cells regardless if these are cells with or without args. Why would to prohibit storing data in cells without args?

alebaran avatar Oct 14 '19 15:10 alebaran

I'm not prohibiting storing data in cells without args. The bottom line is, currently it not possible to store values of arbitrary cells with write_model, so need some work to enhance write_model. I'll put that on the requested feature stack.

fumitoh avatar Oct 15 '19 10:10 fumitoh

Thanks. At the moment I'm storing data in the cells without args using low level calls: space.new_cells(name= name, formula=mx.core.util.get_param_func(cells_params))

alebaran avatar Oct 15 '19 11:10 alebaran

Not sure I'm following you. You want to do something like below correct? How does the mx.core.util.get_param_func help?

import pandas as pd
import modelx as mx

m, s = mx.new_model(), mx.new_space()

s.new_cells(name="NoArg") # creating a non-arg cells

df = pd.DataFrame({"A": [1,2,3], "B":[4,5,6]})

s.NoArg = df # assign df to NoArg

s.NoArg() # returns df

mx.write_model(m, "NoArg")
m2 = mx.read_model("NoArg")

m2.Space1.NoArg # non-arg cells is restored

m2.Space1.NoArg() # You want this to return df 

fumitoh avatar Oct 15 '19 11:10 fumitoh

I'm not using new_cells_from_pandas function yet. Instead I've built my own function using mx.core.util.get_param_func, which does something very close to new_cells_from_pandas. I would like to migrate to using new_cells_from_pandas and checking if it covers all my needs. That's where the question above comes from. The approach you are describing above is a good workaround.

alebaran avatar Oct 15 '19 14:10 alebaran

(correction) The comment below was for #13 , posted here by mistake.

v0.0.25 is released and the stack tracing feature is introduced. See https://modelx.readthedocs.io/en/latest/releases/relnotes_v0_0_25.html

fumitoh avatar Oct 18 '19 15:10 fumitoh

The new version is awesome!!! It allows me to fully track the number of calls to the function and the time it takes to evaluate each call. Thank you very much!

alebaran avatar Oct 18 '19 16:10 alebaran

Not sure I'm following you. You want to do something like below correct? How does the mx.core.util.get_param_func help?

import pandas as pd
import modelx as mx

m, s = mx.new_model(), mx.new_space()

s.new_cells(name="NoArg") # creating a non-arg cells

df = pd.DataFrame({"A": [1,2,3], "B":[4,5,6]})

s.NoArg = df # assign df to NoArg

s.NoArg() # returns df

mx.write_model(m, "NoArg")
m2 = mx.read_model("NoArg")

m2.Space1.NoArg # non-arg cells is restored

m2.Space1.NoArg() # You want this to return df 

Sorry, I misunderstood you earlier: I thought the approach described above works. It seems that even single value stored in the cell without arguments can't be restored:

import modelx as mx
m, s = mx.new_model(), mx.new_space()
s.new_cells(name="NoArg") 
s.NoArg = 1
mx.write_model(m, "NoArg")
m2 = mx.read_model("NoArg")
m2.Space1.NoArg
m2.Space1.NoArg()

It would be really helpful to have this functionality including pandas objects. I won't be able to use write_model feature without it.

alebaran avatar Oct 28 '19 13:10 alebaran

I've also noticed that it isn't possible to have DataFrame as a ref. Is it intentional this way?

alebaran avatar Oct 28 '19 13:10 alebaran

You can have DFs as refs. You mean you can't write_model DataFrame as ref?

fumitoh avatar Oct 28 '19 13:10 fumitoh

You can have DFs as refs. You mean you can't write_model DataFrame as ref?

Using your earlier example:

import pandas as pd
import modelx as mx

m, s = mx.new_model(), mx.new_space()


df = pd.DataFrame({"A": [1,2,3], "B":[4,5,6]})

s.NoArg = df # assign df to NoArg

mx.write_model(m, "NoArg")

alebaran avatar Oct 28 '19 13:10 alebaran

Another thing is that the following code, which works with model.save doesn't work with mx.write_model:

import modelx as mx
from datetime import time

m, s = mx.new_model(), mx.new_space()

s.time = time

mx.write_model(m, "NoArg")

There is an easy workaround for this one, hence not important.

alebaran avatar Oct 28 '19 13:10 alebaran

modelx version 0.1.0 is released, and input values of a cells are distinguished from calculated values, and they are saved by write_model function. See the release note for the details. https://modelx.readthedocs.io/en/latest/releases/relnotes_v0_1_0.html

fumitoh avatar Dec 01 '19 07:12 fumitoh