In [10]:
import glob
import pysd

documentation on how to sort the profile output is here: https://docs.python.org/2/library/profile.html#pstats.Stats.sort_stats


In [12]:
# load model files from system zoo: 
# http://www.usf.uni-kassel.de/cesr/index.php?option=com_remository&Itemid=141&func=fileinfo&id=109
mdl_files = glob.glob('vensim/ZOO MDL/*/*/*/*.mdl')
len(mdl_files)


Out[12]:
114

In [16]:
for mdl_file in mdl_files:
    print mdl_file
    try:
        model = pysd.read_vensim(mdl_file)
        try:
            model.run()
        except:
            print 'fail run'
    except:
        print 'fail import'


vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ101 Integration.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ102A State.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ102B State.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ102C State.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ102D State.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ103 Growth.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ104 Delay.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ105 Timefunction.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ106A Popdynamics.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ106B Popdynamics.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ107 Infect.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ108 Overflow.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ109A logistGrowth.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ109B logistGrowth.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ110 sustainHarvest.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ111 MichMentenGrowth.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ112 doubleIntegration.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ113 Transition.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ114 Oscillator2.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ115 State_oscillator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ115 StateDiagram.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ116 tripleIntegration.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 1/EZ117 Oscillator3.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ201 Pendulum.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ201 Z_Pendulum.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ202 vanderPol.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ203 Brusselator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ203 Z_Brusselator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ204 BiOscillator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ204 Z_BiOscillator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ205 ChaosOscillator.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ206 LorenzChaos.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ207 RoesslerChaos.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ208 DynamoChaos.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ209 Balancer.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ210 Thermal.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ211 FlightDynamics.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ212 HouseHeat.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ213 Heatflow.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 1 E models/System Zoo Pt 2/EZ214 FluidFlow.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ301 watershed.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ302 Cglobal.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ303 CO2dynamics.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ304 deforestation.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ305 CBudgetForest.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ306 Ctraffic.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ307 Photosyn.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ308 Forest.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ308 Z_Forest.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ309 Treedieback.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ310 Soilwater.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ311 Humus.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ312 Fieldcrop.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ313 Food.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 3/EZ314 Farms.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ401 predator nolimit.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ402A Predator limit.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ402B Predator limit.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ403 Predator 2 prey.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ404 Prey 2 predators.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ405 Kaibab.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ406 Forest & birds.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ407 Plant competition.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ408 Fishpond.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ409  Fishing.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ409  Z_Fishing.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ410 FishSonar_opt.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ410 Z_FishSonar.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ411 Tourist.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ412A Tourism_A.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ412B Tourism_B.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ412C Tourism_C.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ412D Tourism_D.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ413 Forest clearing.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ414 Resource.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ415 Resource use.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ416 Overuse.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ417 Commons.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ417 Z_Commons.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ418 Sustainable.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 2 E models/System Zoo Pt 4/EZ418 Z_Sustainable.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ501 Supply chain.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ502 Orders.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ503A Production & inventory.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ503B Production cycle.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ504 Market & price.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ505 Market competition.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ506 Resource competition.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ506 Z_Resource competition.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ507 Escalation.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ508 Dependence.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ508 Z_Dependence.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ509 Aggression.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ510 Life plan.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ511 Unemployment.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ512 Productivity.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ513 Production.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 5/EZ515 Ecomobile.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ601 Population3 Kenya.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ601 Population3.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ602 Population4.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ603 Pensions supplement.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ603 Pensions4.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ604 Community.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ605 Miniworld.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ606 Orientation.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ606 OriMiniworld.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ607 Debt crisis.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ607 Z_Debt crisis.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ608 Globalization.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ609 African village.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ610 World2.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ611 Orient World2.mdl
fail import
vensim/ZOO MDL/ZOO MDL/Zoo 3 E models/System Zoo Pt 6/EZ612 World3_03.mdl
fail import

In [6]:
%load_ext line_profiler
#import line_profiler as lp

In [4]:
import pysd
model = pysd.read_vensim('vensim/Teacup.mdl')

In [15]:
from pysd.translators import import_vensim
%lprun import_vensim('vensim/Teacup.mdl')

In [16]:
%lprun pysd.read_vensim('vensim/Teacup.mdl')

In [7]:
%prun model = pysd.read_vensim('vensim/Teacup.mdl')


 

In [26]:
%lprun -v model.run()


 

In [17]:
# look here http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.execution.html
%prun  -s cumulative model.run()


 

In [18]:
# look here http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.execution.html
%prun  -s time model.run()


 

In [20]:
import timeit
hi = 19
timeit.timeit('hi*3')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-f5d04a3e6b0c> in <module>()
      1 import timeit
      2 hi = 19
----> 3 timeit.timeit('hi*3')

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/timeit.pyc in timeit(stmt, setup, timer, number)
    228            number=default_number):
    229     """Convenience function to create Timer object and call timeit method."""
--> 230     return Timer(stmt, setup, timer).timeit(number)
    231 
    232 def repeat(stmt="pass", setup="pass", timer=default_timer,

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/timeit.pyc in timeit(self, number)
    193         gc.disable()
    194         try:
--> 195             timing = self.inner(it, self.timer)
    196         finally:
    197             if gcold:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/timeit.pyc in inner(_it, _timer)

NameError: global name 'hi' is not defined

In [22]:
from timeit import Timer
t = Timer(lambda: pysd.read_vensim('vensim/Teacup.mdl'))
print t.timeit(number=1)


  File "<ipython-input-22-b6af94de93ab>", line 2
    t = Timer(lambda: model = pysd.read_vensim('vensim/Teacup.mdl'))
SyntaxError: lambda cannot contain assignment

In [32]:
import pstats, cProfile
import pysd

model = pysd.read_vensim('vensim/Teacup.mdl')


#cProfile.run(pysd.read_vensim('vensim/Teacup.mdl'), 'prof_import.prof')
cProfile.runctx("model.run()", globals(), locals(), "prof_import.prof")

#s = pstats.Stats("Profile.prof")
#s.strip_dirs().sort_stats("time").print_stats()

In [35]:
! cprofilev -f prof_import.prof


[cProfileV]: cProfile output available at http://127.0.0.1:4000
Traceback (most recent call last):
  File "/usr/local/bin/cprofilev", line 9, in <module>
    load_entry_point('CProfileV==1.0.6', 'console_scripts', 'cprofilev')()
  File "/Library/Python/2.7/site-packages/cprofilev.py", line 203, in main
    cprofilev.start()
  File "/Library/Python/2.7/site-packages/cprofilev.py", line 171, in start
    self.app.run(host=self.address, port=self.port, quiet=True)
  File "/usr/local/bin/bottle.py", line 755, in run
    run(self, **kwargs)
  File "/usr/local/bin/bottle.py", line 3117, in run
    server.run(app)
  File "/usr/local/bin/bottle.py", line 2771, in run
    srv = make_server(self.host, self.port, app, server_cls, handler_cls)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 144, in make_server
    server = server_class((host, port), handler_class)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in __init__
    self.server_bind()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 48, in server_bind
    HTTPServer.server_bind(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 430, in server_bind
    self.socket.bind(self.server_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 48] Address already in use

In [11]:
python -m cprofilev profile_pysd.py


  File "<ipython-input-11-8f5139529268>", line 1
    python -m cprofilev profile_pysd.py
                      ^
SyntaxError: invalid syntax

In [23]:
from itertools import izip
keys = range(100)
values = range(100)

%timeit dict(zip(keys,values))


100000 loops, best of 3: 7.32 µs per loop

In [24]:
%timeit dict(izip(keys,values))


100000 loops, best of 3: 5.07 µs per loop

In [37]:
def hi():
    return 'hi'
    
def there():
    return 'there'
    
funclist = []
funclist.append(hi)
funclist.append(there)

In [38]:
[f() for f in funclist]


Out[38]:
['hi', 'there']

In [42]:
l = ['a']
l.index('a')


Out[42]:
0

In [45]:
l2 = []
l2.insert(0, 'hi')

In [44]:
l3 = [1, 2]
l3.insert(0'hi',0)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-cce8542cdcf2> in <module>()
      1 l3 = [1, 2]
----> 2 l3.insert('hi',0)

TypeError: an integer is required

In [40]:
index = funclist.index('chewie')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-687665f9ef48> in <module>()
----> 1 index = funclist.index('chewie')

ValueError: 'chewie' is not in list

In [41]:
model.components._stocknames


Out[41]:
[u'teacup_temperature']

In [47]:
model.run()


Out[47]:
teacup_temperature
0.000 180.000000
0.125 178.633556
0.250 177.284091
0.375 175.951387
0.500 174.635237
0.625 173.335438
0.750 172.051784
0.875 170.784077
1.000 169.532117
1.125 168.295709
1.250 167.074660
1.375 165.868779
1.500 164.677878
1.625 163.501771
1.750 162.340273
1.875 161.193204
2.000 160.060384
2.125 158.941636
2.250 157.836785
2.375 156.745659
2.500 155.668087
2.625 154.603901
2.750 153.552934
2.875 152.515023
3.000 151.490005
3.125 150.477720
3.250 149.478010
3.375 148.490718
3.500 147.515690
3.625 146.552775
... ...
26.250 77.968373
26.375 77.869389
26.500 77.771633
26.625 77.675093
26.750 77.579751
26.875 77.485594
27.000 77.392606
27.125 77.300774
27.250 77.210082
27.375 77.120517
27.500 77.032065
27.625 76.944711
27.750 76.858442
27.875 76.773246
28.000 76.689107
28.125 76.606014
28.250 76.523952
28.375 76.442910
28.500 76.362875
28.625 76.283834
28.750 76.205775
28.875 76.128686
29.000 76.052554
29.125 75.977368
29.250 75.903116
29.375 75.829786
29.500 75.757368
29.625 75.685849
29.750 75.615218
29.875 75.545464

240 rows × 1 columns


In [ ]: