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

Add Model.copy() #131

Closed
JamesPHoughton opened this issue Aug 9, 2017 · 2 comments
Closed

Add Model.copy() #131

JamesPHoughton opened this issue Aug 9, 2017 · 2 comments
Assignees

Comments

@JamesPHoughton
Copy link
Collaborator

JamesPHoughton commented Aug 9, 2017

It would be good to have a deep copy operation that can give you a copy of the current model to mess with. This could take the place of reloading a model after making changes, so you could take the copy after some changes, make other changes, and then drop them.

This could actually be pretty difficult, not sure.

I think a good syntax would be similar to that in networkx in that the copy function is a member of the object that it is copying. However, I expect the deepcopy is really what will be most useful.

@enekomartinmartinez
Copy link
Collaborator

Thinking about including this in 3.8.0

Model.copy() should be used as well for select_submodel. This function should create a copy and modify it, instead of mutating the original model.

@enekomartinmartinez
Copy link
Collaborator

I have been doing some test with the copy and I think it will not be so easy to implement as the calls inside the components are kept to the original data, e.g.:

 a -> b

c = copy(a)
d = copy (b)

we would like to c -> d, but we will have c -> b, running:

from copy import copy, deepcopy

def a():
    return b()

def b():
    return 2

c = copy(a)
d = deepcopy(a)
print(f"a = {a()}")
print(f"b = {b()}")
print(f"c = {c()}")
print(f"d = {d()}")
print("-"*5)

b = lambda: 5
print(f"a = {a()}")
print(f"b = {b()}")
print(f"c = {c()}")
print(f"d = {d()}")

returns

a = 2
b = 2
c = 2
d = 2
-----
a = 5
b = 5
c = 5
d = 5

The only option I see is that PySD tracks the changes done to the components, and the copy function loads a model again and applies the same changes.

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