# Making a deep copy of an OPF model

**URL:** <https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016>\
**Category:** NAB\
**Tags:** question, nupic, python\
**Created:** [November 2, 2017, 5:13pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016 "2017-11-02T17:13:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkutt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/bkutt/32/1574_2.png) [@bkutt](https://discourse.numenta.org/u/bkutt)\
**Post date:** [November 2, 2017, 5:13pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/1 "2017-11-02T17:13:26Z")

</div>

Hello!

I have an instance of an OPF model (the same one used in the numenta detector in NAB, actually) that I’d like to make a deep copy of in my code. The simplest solution (trying “model\_copy = copy.deepcopy(model)”) does not work. When trying to do model\_copy.run(), it breaks because self.\_\_restoringFromState gets set to true inside model\_copy which doesn’t pass an assert statement.

Could somebody explain or point me in the direction of documentation which explains how to make deep copies of models?

Best,  
Brody

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [November 2, 2017, 5:15pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/2 "2017-11-02T17:15:23Z")

</div>

You probably want to use the [old way of saving](http://nupic.docs.numenta.org/1.0.3/guides/serialization.html#deprecated-serialization-methods), which pickles things.

---

<div class="post-metadata">

**Author:** ![bkutt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/bkutt/32/1574_2.png) [@bkutt](https://discourse.numenta.org/u/bkutt)\
**Post date:** [November 2, 2017, 5:36pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/3 "2017-11-02T17:36:32Z")

</div>

Interesting…is there a new way of saving/loading models that is under development? Is that why it’s deprecated?

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [November 2, 2017, 5:38pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/4 "2017-11-02T17:38:01Z")

</div>

Yes, see the rest of the doc I sent. But to use this method you need to get hold of the algorithm instances.

---

<div class="post-metadata">

**Author:** ![bkutt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/bkutt/32/1574_2.png) [@bkutt](https://discourse.numenta.org/u/bkutt)\
**Post date:** [November 2, 2017, 6:39pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/5 "2017-11-02T18:39:57Z")

</div>

Thanks for the quick responses…I have to create and load many copies and unfortunately writing to the file system is exceedingly slow. I’m going to keep trying to deep copy a model all within memory.

---

<div class="post-metadata">

**Author:** ![bkutt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/bkutt/32/1574_2.png) [@bkutt](https://discourse.numenta.org/u/bkutt)\
**Post date:** [November 2, 2017, 6:45pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/6 "2017-11-02T18:45:46Z")

</div>

I notice the **eq** method is not overwritten in the base model class or the htm\_prediction\_model. Is there any code out there to test equality between two model instances?

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [November 2, 2017, 6:46pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/7 "2017-11-02T18:46:58Z")

</div>

I’ll have to defer to @scott on that question.

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/scott/32/170_2.png) [@scott](https://discourse.numenta.org/u/scott)\
**Post date:** [November 2, 2017, 7:20pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/8 "2017-11-02T19:20:21Z")

</div>

I think you can probably use the new or old serialization completely in memory. The old method requires separate steps for the Python state (pickle) and C++ state so the new method is probably easier (and faster).

For the new method, you can see how to do an in-memory copy here:

> <https://github.com/numenta/nupic/blob/1aea72abde4457878a16288d6786ffb088f69164/tests/integration/nupic/opf/htmpredictionmodel_serialization_test.py#L243-L251>

It might be faster to use to\_bytes\_packed and from\_bytes\_packed instead of the unpacked version used in the example code. See pycapnp reference here:  
[http://jparyani.github.io/pycapnp/capnp.html#capnp.\_DynamicStructBuilder.to\_bytes\_packed](http://jparyani.github.io/pycapnp/capnp.html#capnp._DynamicStructBuilder.to_bytes_packed)  
[http://jparyani.github.io/pycapnp/capnp.html#capnp.\_StructModule.from\_bytes\_packed](http://jparyani.github.io/pycapnp/capnp.html#capnp._StructModule.from_bytes_packed)

---

<div class="post-metadata">

**Author:** ![scott](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/scott/32/170_2.png) [@scott](https://discourse.numenta.org/u/scott)\
**Post date:** [November 2, 2017, 7:21pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/9 "2017-11-02T19:21:26Z")

</div>

And you probably want to save the result of `builderProto.to_bytes()` into a variable that you can call `from_bytes` on multiple times to create multiple copies of the model.

---

<div class="post-metadata">

**Author:** ![bkutt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/bkutt/32/1574_2.png) [@bkutt](https://discourse.numenta.org/u/bkutt)\
**Post date:** [November 2, 2017, 7:29pm UTC](https://discourse.numenta.org/t/making-a-deep-copy-of-an-opf-model/3016/10 "2017-11-02T19:29:11Z")

</div>

Excellent. You’re right, that method is much faster. For determining equality I’m just checking that they are giving the same output over time which is the case. Thanks so much for your help.
