I just got a new computer, and now a bunch of my python scripts dont work because they return the following error:

Traceback (most recent call last):
  File "simple1.py", line 65, in <module>
    time = np.array(simple_trajectories[0][:,0]) 
TypeError: unhashable type

Several commenters have helped to identify that the error arises because simple_trajectories[0] is a dictionary on the new computer and a numpy.ndarray on the old computer.

Is there a way to figure out why this is happening? or if not, is there a simple fix to change it back to the numpy ndarray form?

both computers are using python 2.7.12 and ubuntu 16.04

Any suggestions would be greatly appreciated.

The full code is pasted here:

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt

import sys
sys.path[:0] = ['..']

import gillespy

class Simple1(gillespy.Model):
    """
    This is a simple example for mass-action degradation of species S.
    """

    def __init__(self, parameter_values=None):

        # Initialize the model.
        gillespy.Model.__init__(self, name="simple1")

        # Parameters
        k1 = gillespy.Parameter(name='k1', expression=0.3)
        self.add_parameter(k1)

        # Species
        S = gillespy.Species(name='S', initial_value=100)
        self.add_species(S)

        # Reactions
        rxn1 = gillespy.Reaction(
                name = 'S degradation',
                reactants = {S:1},
                products = {},
                rate = k1 )
        self.add_reaction(rxn1)
        self.timespan(np.linspace(0,20,101))



if __name__ == '__main__':

    # Here, we create the model object.
    # We could pass new parameter values to this model here if we wished.
    simple_model = Simple1()

    # The model object is simulated with the StochKit solver, and 25 
    # trajectories are returned.
    num_trajectories = 250
    simple_trajectories = simple_model.run(number_of_trajectories = num_trajectories)




    # PLOTTING


    # here, we will plot all trajectories with the mean overlaid
    from matplotlib import gridspec

    gs = gridspec.GridSpec(1,1)


    ax0 = plt.subplot(gs[0,0])

    # extract time values
    time = np.array(simple_trajectories[0][:,0]) 

    # extract just the trajectories for S into a numpy array
    S_trajectories = np.array([simple_trajectories[i][:,1] for i in xrange(num_trajectories)]).T

    #plot individual trajectories
    ax0.plot(time, S_trajectories, 'gray', alpha = 0.1)

    #plot mean
    ax0.plot(time, S_trajectories.mean(1), 'k--', label = "Mean S")

    #plot min-max
    ax0.plot(time,S_trajectories.min(1), 'b--', label = "Minimum S")
    ax0.plot(time,S_trajectories.max(1), 'r--', label = "Maximum S")

    ax0.legend()
    ax0.set_xlabel('Time')
    ax0.set_ylabel('Species S Count')

    plt.tight_layout()
    plt.show()

Pip Freeze from old Computer

adium-theme-ubuntu==0.3.4
amqp==1.4.9
anyjson==0.3.3
Babel==1.3
backports.shutil-get-terminal-size==1.0.0
beautifulsoup4==4.4.1
billiard==3.3.0.22
boto==2.38.0
celery==3.1.20
chardet==2.3.0
configparser==3.5.0
cryptography==1.2.3
cvxopt==1.1.4
cycler==0.9.0
Cython==0.23.4
debtcollector==1.3.0
decorator==4.0.6
ecdsa==0.13
entrypoints==0.2.2
enum34==1.1.2
funcsigs==0.4
functools32==3.2.3.post2
future==0.16.0
gillespy==1.0
gmpy==1.17
h5py==2.6.0
html5lib==0.999
idna==2.0
ipaddress==1.0.16
ipykernel==4.5.2
ipython==5.1.0
ipython-genutils==0.1.0
ipywidgets==5.2.2
iso8601==0.1.11
jdcal==1.0
Jinja2==2.8
joblib==0.9.4
jsonschema==2.5.1
jupyter==1.0.0
jupyter-client==4.4.0
jupyter-console==5.0.0
jupyter-core==4.2.1
keyring==7.3
keystoneauth1==2.4.1
kombu==3.0.33
lxml==3.5.0
mailer==0.7
MarkupSafe==0.23
matplotlib==1.5.1
mistune==0.7.3
monotonic==0.6
mpmath==0.19
msgpack-python==0.4.6
mysql-connector-python==2.0.4
nbconvert==4.2.0
nbformat==4.2.0
ndg-httpsclient==0.4.0
netaddr==0.7.18
netifaces==0.10.4
nolds==0.3.2
nose==1.3.7
notebook==4.2.3
numexpr==2.4.3
numpy==1.13.1
openpyxl==2.3.0
oslo.i18n==3.5.0
oslo.serialization==2.4.0
oslo.utils==3.8.0
pandas==0.17.1
paramiko==1.16.0
pathlib2==2.1.0
patsy==0.4.1
pbr==1.8.0
PeakUtils==1.0.3
pexpect==4.0.1
pickleshare==0.7.4
Pillow==3.1.2
positional==1.0.1
prettytable==0.7.2
prompt-toolkit==1.0.9
ptyprocess==0.5
py==1.4.31
pyasn1==0.1.9
pycrypto==2.6.1
pycurl==7.43.0
pyeeg==0.4.0
pyentrp==0.3.0
pyglet==1.1.4
Pygments==2.1.3
pygobject==3.20.0
PyMySQL==0.7.2
PyOpenGL==3.0.2
pyOpenSSL==0.15.1
pyparsing==2.0.3
pysb==1.2.2
pytest==2.8.7
python-apt==1.1.0b1
python-dateutil==2.4.2
python-libsbml==5.13.0
python-memcached==1.53
python-novaclient==3.3.1
pytz==2014.10
pyurdme==1.1.1
PyYAML==3.11
pyzmq==15.2.0
qtconsole==4.2.1
requests==2.9.1
scikit-learn==0.18.1
scipy==0.19.1
scour==0.32
seaborn==0.7.1
SecretStorage==2.1.3
selenium==3.0.2
simplegeneric==0.8.1
simplejson==3.8.1
six==1.10.0
SQLAlchemy==1.0.11
statsmodels==0.6.1
stevedore==1.12.0
sympy==0.7.6.1
tables==3.2.2
terminado==0.6
tornado==4.2.1
traitlets==4.3.1
unity-lens-photos==1.0
urllib3==1.13.1
VTK==5.10.1
wcwidth==0.1.7
widgetsnbextension==1.2.6
wrapt==1.8.0
xlrd==0.9.4
xlwt==0.7.5

Pip Freeze from New Computer

adium-theme-ubuntu==0.3.4
amqp==1.4.9
anyjson==0.3.3
Babel==1.3
backports-abc==0.5
backports.shutil-get-terminal-size==1.0.0
beautifulsoup4==4.4.1
billiard==3.3.0.22
bleach==2.0.0
boto==2.38.0
celery==3.1.20
certifi==2017.4.17
chardet==2.3.0
configparser==3.5.0
cryptography==1.2.3
cycler==0.10.0
Cython==0.23.4
debtcollector==1.3.0
decorator==4.0.6
ecdsa==0.13
entrypoints==0.2.3
enum34==1.1.2
funcsigs==0.4
functools32==3.2.3.post2
gillespy==1.0
h5py==2.7.0
html5lib==0.999999999
idna==2.0
ipaddress==1.0.16
ipykernel==4.6.1
ipython==5.4.1
ipython-genutils==0.2.0
ipywidgets==6.0.0
iso8601==0.1.11
Jinja2==2.9.6
jsonschema==2.6.0
jupyter==1.0.0
jupyter-client==5.1.0
jupyter-console==5.1.0
jupyter-core==4.3.0
keyring==7.3
keystoneauth1==2.4.1
kombu==3.0.33
lxml==3.5.0
mailer==0.7
MarkupSafe==1.0
matplotlib==2.0.2
mistune==0.7.4
monotonic==0.6
msgpack-python==0.4.6
mysql-connector-python==2.0.4
nbconvert==5.2.1
nbformat==4.3.0
ndg-httpsclient==0.4.0
netaddr==0.7.18
netifaces==0.10.4
notebook==5.0.0
numpy==1.13.1
oslo.i18n==3.5.0
oslo.serialization==2.4.0
oslo.utils==3.8.0
pandas==0.17.0
pandocfilters==1.4.1
paramiko==1.16.0
pathlib2==2.3.0
pbr==1.8.0
PeakUtils==1.1.0
pexpect==4.0.1
pickleshare==0.7.4
positional==1.0.1
prettytable==0.7.2
prompt-toolkit==1.0.14
ptyprocess==0.5
pyasn1==0.1.9
pycrypto==2.6.1
pycurl==7.43.0
Pygments==2.2.0
pygobject==3.20.0
PyMySQL==0.7.11
pyOpenSSL==0.15.1
pyparsing==2.0.3
python-apt==1.1.0b1
python-dateutil==2.4.2
python-libsbml==5.15.0
python-memcached==1.53
python-novaclient==3.3.1
pytz==2014.10
pyurdme==1.1.1
PyYAML==3.11
pyzmq==16.0.2
qtconsole==4.3.0
requests==2.9.1
scandir==1.5
scipy==0.19.1
scour==0.32
seaborn==0.8
SecretStorage==2.1.3
simplegeneric==0.8.1
simplejson==3.8.1
singledispatch==3.4.0.3
six==1.10.0
SQLAlchemy==1.0.11
stevedore==1.12.0
terminado==0.6
testpath==0.3.1
tornado==4.5.1
traitlets==4.3.2
unity-lens-photos==1.0
urllib3==1.13.1
wcwidth==0.1.7
webencodings==0.5.1
widgetsnbextension==2.0.0
wrapt==1.8.0

I will bold the differences

    
can you post the full code? we don't have enough here to help you. what is simple_trajectories? – See Dart 4 hours ago
    
all of my scripts get this error when dealing with lists in the new computer – Bobby M 3 hours ago
    
Compare the output of "pip freeze" from the two computer. Perhaps you have different versions of numpy. – Ned Batchelder 3 hours ago
    
Numpy is the same. but i will make a list of the differences – Bobby M 3 hours ago
1  
So trajectories[0] is a dictionary? If so, that explains the error. You can't use a slice as the key of a dictionary, which is what trajectories[0][:,0] is trying to do. (Are you sure you copied the complete error message? I would expect to see TypeError: unhashable type: 'slice'.) – Warren Weckesser 3 hours ago

1 Answer 1

Add the argument show_labels=False to the run() call:

simple_trajectories = simple_model.run(number_of_trajectories=num_trajectories, show_labels=False)

When show_labels is True, the return value of the run() method is a list of dictionaries. When the argument is False, a list of numpy arrays is returned. Apparently the examples are based on show_labels=False.

You might not be able to depend on the version number of gillespy; it depends on how you installed it. In the call of setup() in the file setup.py, the version has been at "1.0" for a while. Changes have been made without changing the version. In particular, when the show_labels argument was added, the version was not changed.