fumitoh / modelx

Use Python like a spreadsheet!
https://modelx.io
GNU Lesser General Public License v3.0
90 stars 20 forks source link

Importing modules #8

Closed defrule closed 4 years ago

defrule commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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

alexeybaran commented 4 years ago

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 commented 4 years ago

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

alexeybaran commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

What do you call input values?

On Sat, 28 Sep 2019, 16:38 Fumito Hamamura, notifications@github.com wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/fumitoh/modelx/issues/8?email_source=notifications&email_token=AMOA4S3ZGKSKAK5TYQGAPP3QL5277A5CNFSM4G5QDQPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD724G7Y#issuecomment-536200063, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOA4S362IR3UDI4NCIT3H3QL5277ANCNFSM4G5QDQPA .

fumitoh commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

This really helps. Thank you very much

On Thu, 3 Oct 2019, 16:30 Fumito Hamamura, notifications@github.com wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/fumitoh/modelx/issues/8?email_source=notifications&email_token=AMOA4S6RCMHPFRDDDKK3PLDQMYFYVA5CNFSM4G5QDQPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAIS4CI#issuecomment-537996809, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOA4S5NVDW7XCKWF24KPHDQMYFYVANCNFSM4G5QDQPA .

fumitoh commented 4 years ago

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 commented 4 years ago

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