Skip to content
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

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

Open
alebaran opened this issue Oct 14, 2019 · 16 comments
Open

Comments

@alebaran
Copy link

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?

@fumitoh
Copy link
Owner

fumitoh commented Oct 14, 2019

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: #8 (comment)

NoArgCells.xlsx

@alebaran
Copy link
Author

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

@fumitoh
Copy link
Owner

fumitoh commented Oct 14, 2019

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.

@alebaran
Copy link
Author

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?

@fumitoh
Copy link
Owner

fumitoh commented Oct 15, 2019

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.

@alebaran
Copy link
Author

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))

@fumitoh
Copy link
Owner

fumitoh commented Oct 15, 2019

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 

@alebaran
Copy link
Author

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.

@fumitoh
Copy link
Owner

fumitoh commented Oct 18, 2019

(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

@alebaran
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@fumitoh
Copy link
Owner

fumitoh commented Oct 28, 2019

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

@alebaran
Copy link
Author

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
Copy link
Author

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.

@fumitoh
Copy link
Owner

fumitoh commented Dec 1, 2019

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 fumitoh changed the title how to create cell with no arguments and some data inside? Writing models with such objects as DataFrames as refs or as Cells values Dec 1, 2019
@fumitoh fumitoh added this to the v0.1.0 milestone Dec 1, 2019
@fumitoh fumitoh mentioned this issue Dec 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants