Python Forum
Memory error in python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory error in python 2.7
#1

Here's my errpr message, last time I got this I had a logic error in a loop, but I can't find anything like that in this. Does anyone spot my problem?
Error:
runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') C:\Python27\lib\site-packages\IPython\core\formatters.py:239: FormatterWarning: Exception in image/png formatter: Could not allocate memory for image FormatterWarning, <matplotlib.figure.Figure at 0x2309ef0> Traceback (most recent call last): File "<ipython-input-28-225e42a16155>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py", line 100, in <module> ax.plot(x, yFunction(veganGrowth, initialPopOmni, popGrowthFactor), 'p-', lw=1, alpha=0.6, label='Major Graph') File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Main Program.py", line 62, in yFunction y = initialPop - totFunctions*y MemoryError
Here's the code it's based on.
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp
fig, ax = plt.subplots(1, 1)

initialPop = int(326300000)
initialPopOmni = 315532100
popGrowthFactor = 0.007


def runLognormal(start, current, mean, cumImpact, peakFactor):
    scale = exp(mean)
    return lognorm.pdf(current, peakFactor, start, scale)*cumImpact
    
def runLinear(start, stop, slope):
    numQRun = start-stop
    y=[]
    time = start
    while time <= stop:
        time = time + 1
        y.append(slope*(time-start))
    return y[1:numQRun]
    
    
def veganuary(x):
    veg = []
    tTime = 48
    while tTime <= 136:
        tTime = tTime + 4
        veg.append(runLognormal(tTime, x, 0.002885, 0.00577, 0.55)-runLognormal(tTime+1, x, 0.00173, 0.00376, 0.55))
    return sum(veg[1:22])
    
def veganuary2(x):
    veg2 = []
    tTime = 136
    while tTime <= 212:
        tTime = tTime + 4
        veg2.append(runLognormal(tTime, x, 0.0043275, 0.008655, 0.55)-runLognormal(tTime+1, x, 0.002592, 0.00519, 0.55))
    return sum(veg2[1:19])
    
        
def yFunction(totFunctions, initialPop, popGrowthFactor):
    x = 0
    Y = []
    y = initialPop * np.exp(popGrowthFactor * x/4)
    while x < 212:
        Y.append(y)
        y = initialPop - totFunctions*y
        x = x + 0.01
    return Y[1:21200]
        
        
    


     
x = np.linspace(0, 212, num=21200)
tax1 = runLognormal(92, x, 0.036, 0.071, 0.55)
tax2 = runLognormal(112, x, 0.036, 0.071, 0.55)
tax3 = runLognormal(132, x, 0.071, 0.142, 0.55)
tax4 = runLognormal(152, x, 0.036, 0.071, 0.55)
tax5 = runLognormal(200, x, 0.1065, 0.213, 0.55)
tariff = runLognormal(56, x, 0.018, 0.036, 0.55)
ad1 = runLognormal(4, x, 0.000575, 0.00115, 0.55)
ad2 = runLognormal(44, x, 0.00019, 0.00038, 0.55)
ad3 = runLognormal(84, x, 0.00019, 0.00038, 0.55)
ad4 = runLognormal(124, x, 0.000385, 0.00077, 0.55)
ad5 = runLognormal(144, x, 0.000575, 0.00115, 0.55)
ad6 = runLognormal(176, x, 0.00095, 0.0019, 0.55)
ad7 = runLognormal(200, x, 0.000575, 0.00115, 0.55)
news1 = runLognormal(1, x, 0.000345, 0.00069, 0.55)-runLognormal(2, x, 0.000207, 0.000414, 0.55)
veg = veganuary(x)
veg2 = veganuary2(x)
label1 = runLinear(80, 160, 0.0006875)
label2 = runLinear(160, 212, 0.003125)
#consumptionBase =
#popBase = 
#popOmniBase =
#popVegBase =
#popVeganBase =
veganGrowth = tax1 + tax2 + tax3 + tax4 + tax5 + tariff + ad1 + ad2 + ad3 + ad4 + ad5 + ad6 + ad7 + news1 + veg + veg2# + label1 + label2

ax.set_title('Decrease in Omnivore Population')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population Omnivores$')
ax.plot(x, yFunction(veganGrowth, initialPopOmni, popGrowthFactor), 'p-', lw=1, alpha=0.6, label='Major Graph')
Reply
#2
The error message doesn't match the code.
Error message shows error on line 62, but in your listing, it's line
49.
Please post error traceback that matches code.
Reply
#3
oh, sorry, that's just because this is abbreviated code. I cut out a bunch of repeat stuff so that it would be easier to read, the traceback error is the same though.
Reply
#4
When debugging code, you never use two versions. Get them in sync!
Reply
#5
They're the same code, but here's the error code for the abbreviated version I'm using to debug (it's the exact same code, just minus repeated functions that I know work fine).

First error
Error:
Traceback (most recent call last): File "<ipython-input-1-1ea6155a3286>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Linear function.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Linear function.py", line 37, in <module> lbl1.plot(label1X, runLinear(80, 160, 0.0006875, label1X)) File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot for line in self._get_lines(*args, **kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 282, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 223, in _xy_from_xy raise ValueError("x and y must have same first dimension") ValueError: x and y must have same first dimension
Second error (with the lbl1 print commented out
Error:
runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Linear function.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') Traceback (most recent call last): File "<ipython-input-2-1ea6155a3286>", line 1, in <module> runfile('C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Linear function.py', wdir='C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction') File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc) File "C:/Users/after/OneDrive/Distinction Project Files 2017/Python Scripts for Distinction/Distinction/Linear function.py", line 40, in <module> veganGrowth = tax1 + label1 ValueError: operands could not be broadcast together with shapes (21200,) (0,)
Here's the code:
import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt
from math import exp


def runLognormal(start, current, mean, cumImpact, peakFactor):
    scale = exp(mean)
    return lognorm.pdf(current, peakFactor, start, scale)*cumImpact
    
def runLinear(start, stop, changePerQ, x):
    y = np.array([])
    time = start
    while time <= stop:
        np.append(y, changePerQ)
        time = time + 0.01
    return y

     
x = np.linspace(0, 212, num=21200)
tax1 = runLognormal(92, x, 0.036, 0.071, 0.55)
label1 = runLinear(80, 160, 0.0006875, x)


############################################### GRAPH LABEL 1
fig, lbl1 = plt.subplots(1, 1)
label1X = np.linspace(80, 160, num = 8000)
lbl1.set_title('Text Based Labels')
lbl1.set_xlabel('Quarter')
lbl1.set_ylabel('Cumulative Effect')
lbl1.plot(label1X, runLinear(80, 160, 0.0006875, label1X))
##############################################################

veganGrowth = tax1 + label1

fig, ax = plt.subplots(1, 1)
ax.set_title('Instantaneous Rate of Change in Population')
ax.set_xlabel('$Quarter$')
ax.set_ylabel('$Population Omnivores$')
ax.plot(x, veganGrowth)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Memory Error jason413 6 7,958 Jun-21-2018, 06:35 PM
Last Post: nilamo
  Memory error while recursively adding np.arrays Afterdarkreader 0 4,007 Dec-22-2017, 04:02 PM
Last Post: Afterdarkreader
  Memory Error rajeev1729 1 31,102 Sep-26-2017, 09:50 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020