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

Importing modules #8

Closed
defrule opened this issue Mar 12, 2019 · 12 comments
Closed

Importing modules #8

defrule opened this issue Mar 12, 2019 · 12 comments

Comments

@defrule
Copy link

defrule commented Mar 12, 2019

Not sure if this is an issue or intended. I wanted to use numpy so that each 'cell' represents an array.

In my example below 'unit_reserve_opening' is the opening account value at each projection timestep, but it get numpy to work, I had to import it within the function. Putting the import at the top of the module didn't work.

@defcells
def unit_reserve_opening(t):
    import numpy as np
    if t == 1:
        return np.array([200, 300, 100, 400])
    else:
        return unit_reserve_closing(t-1)
@fumitoh
Copy link
Owner

fumitoh commented Mar 13, 2019

You can do:

import modelx as mx
import numpy

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

m.np = numpy

@mx.defcells
def unit_reserve_opening(t):
    # your code

Or instead of m.np = numpy, s.np = numpy can limit the use of the name np within the space

@alexeybaran
Copy link

It won't be possible to save the model, if imported modules are added as refs.
In the example above if I add:
m.save('my_model.mx')

It breaks with message:

TypeError: can't pickle module objects

@fumitoh fumitoh added the bug label Sep 28, 2019
@alexeybaran
Copy link

Btw. it is possible to save it with "write_model" feature, which is an awesome functionality. The only limitation of it is that "read_model" doesn't have a model name as a second parameter (like "open_model").

@fumitoh
Copy link
Owner

fumitoh commented Sep 28, 2019

Thanks. I'll see if I can add renaming feature to read_model.

@alexeybaran
Copy link

I use model.save including custom class objects stored in the cells. E.g. bond class. Is there a way to do it with "write_model"?

@fumitoh
Copy link
Owner

fumitoh commented Sep 28, 2019

Currently it's not possible. write_model doesn't store cells values except for those cells that are created from new_cells_from_* or new_space_from_* methods, which are saved with their data source files.
In future, it may become possible to store input values with write_model.

@alexeybaran
Copy link

alexeybaran commented Sep 28, 2019 via email

@fumitoh
Copy link
Owner

fumitoh commented Sep 28, 2019

I meant by input values those values stored in cells by the user through value assignment operations as opposed to those that are calculated by the formulas of the cells. Currently no information is held to distinguish between them, so need some work.

fumitoh added a commit that referenced this issue Oct 1, 2019
@fumitoh fumitoh removed the bug label Oct 3, 2019
@fumitoh
Copy link
Owner

fumitoh commented Oct 3, 2019

Just released v0.0.24 and those features are made available.
See https://modelx.readthedocs.io/en/latest/releases/relnotes_v0_0_24.html

@alexeybaran
Copy link

alexeybaran commented Oct 4, 2019 via email

@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 modified the milestones: v0.1.0, v0.0.24 Dec 1, 2019
@fumitoh
Copy link
Owner

fumitoh commented Dec 1, 2019

Closing this issue as the discussion derailed from the title. Post on #15 for further discussion if any.

@fumitoh fumitoh closed this as completed 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

3 participants