Skip to content

Instantly share code, notes, and snippets.

@nden
Last active November 28, 2017 14:20
Show Gist options
  • Save nden/53c17aac8cf32800040198e2a42c77b8 to your computer and use it in GitHub Desktop.
Save nden/53c17aac8cf32800040198e2a42c77b8 to your computer and use it in GitHub Desktop.
memory_comparison_modeling

Script for memory profiling astropy.modeling

from astropy.modeling.models import Mapping, Tabular1D
from astropy.modeling.functional_models import Shift, Scale
from astropy.modeling.polynomial import Polynomial2D
import gc
from memory_profiler import profile
import numpy as np

@profile
def test_models(x):
    tab = Tabular1D(lookup_table = np.arange(62500000), bounds_error=False)
    offx = Shift(1)
    scl = Scale(2)
    p2 = Polynomial2D(5)
    p2.parameters = np.random.rand(21)
    p22 = Polynomial2D(5)
    p22.parameters = np.random.rand(21)
    mapping = Mapping((0, 1, 0, 1))
    m = tab & scl | mapping | p2 & p22
    del offx, scl, p2, p22, mapping, tab
    gc.collect()
    #print(gc.garbage)
    return m(x, x)

Results for python 3.5 and modeling/master

Line #    Mem usage    Increment   Line Contents
================================================
    29  11536.2 MiB      0.0 MiB   @profile
    30                             def test_models(x):
    31  12489.9 MiB    953.6 MiB       tab = Tabular1D(lookup_table = np.arange(62500000), bounds_error=False)
    32  12489.9 MiB      0.0 MiB       offx = Shift(1)
    33  12489.9 MiB      0.0 MiB       scl = Scale(2)
    34  12489.9 MiB      0.0 MiB       p2 = Polynomial2D(5)
    35  12489.9 MiB      0.0 MiB       p2.parameters = np.random.rand(21)
    36  12489.9 MiB      0.0 MiB       p22 = Polynomial2D(5)
    37  12489.9 MiB      0.0 MiB       p22.parameters = np.random.rand(21)
    38  12489.9 MiB      0.0 MiB       mapping = Mapping((0, 1, 0, 1))
    39  15351.1 MiB   2861.3 MiB       m = tab & scl | mapping | p2 & p22
    40  14397.5 MiB   -953.7 MiB       del offx, scl, p2, p22, mapping, tab
    41  14397.5 MiB      0.0 MiB       gc.collect()
    42                                 #print(gc.garbage)
    43  14397.5 MiB      0.0 MiB       return m(x, x)
    

If Parameter.model is a weakref reference to model then the memory after deleting a model is cleaned


Line #    Mem usage    Increment   Line Contents
================================================
    29   1045.0 MiB      0.0 MiB   @profile
    30                             def test_models(x):
    31   1998.6 MiB    953.6 MiB       tab = Tabular1D(lookup_table = np.arange(62500000), bounds_error=False)
    32   1998.6 MiB      0.0 MiB       offx = Shift(1)
    33   1998.6 MiB      0.0 MiB       scl = Scale(2)
    34   1998.6 MiB      0.0 MiB       p2 = Polynomial2D(5)
    35   1998.6 MiB      0.0 MiB       p2.parameters = np.random.rand(21)
    36   1998.6 MiB      0.0 MiB       p22 = Polynomial2D(5)
    37   1998.6 MiB      0.0 MiB       p22.parameters = np.random.rand(21)
    38   1998.6 MiB      0.0 MiB       mapping = Mapping((0, 1, 0, 1))
    39   3906.0 MiB   1907.5 MiB       m = tab & scl | mapping | p2 & p22
    40   2952.4 MiB   -953.6 MiB       del offx, scl, p2, p22, mapping, tab
    41   1045.0 MiB  -1907.4 MiB       gc.collect()
    42                                 #print(gc.garbage)
    43   1045.0 MiB      0.0 MiB       return m(x, x)

Python 3.6 seems to collect cyclic references although there's nothing in the release notes about garbage collection improvements.

Results with python 3.6 and modeling/master

Line #    Mem usage    Increment   Line Contents
================================================
    29   1031.7 MiB   1031.7 MiB   @profile
    30                             def test_models(x):
    31   1985.2 MiB    953.5 MiB       tab = Tabular1D(lookup_table = np.arange(62500000), bounds_error=False)
    32   1985.2 MiB      0.0 MiB       offx = Shift(1)
    33   1985.2 MiB      0.0 MiB       scl = Scale(2)
    34   1985.2 MiB      0.0 MiB       p2 = Polynomial2D(5)
    35   1985.2 MiB      0.0 MiB       p2.parameters = np.random.rand(21)
    36   1985.2 MiB      0.0 MiB       p22 = Polynomial2D(5)
    37   1985.2 MiB      0.0 MiB       p22.parameters = np.random.rand(21)
    38   1985.2 MiB      0.0 MiB       mapping = Mapping((0, 1, 0, 1))
    39   3892.8 MiB   1907.5 MiB       m = tab & scl | mapping | p2 & p22
    40   2939.1 MiB   -953.6 MiB       del offx, scl, p2, p22, mapping, tab
    41   1031.7 MiB  -1907.4 MiB       gc.collect()
    42                                 #print(gc.garbage)
    43   1031.7 MiB      0.0 MiB       return m(x, x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment