Error in unpickling namedtuple

810 views Asked by At

The global variable Agree is a named tuple defined outside all the functions:

Agree = collections.namedtuple('Agree', ['kappa', 'alpha','avg_ao'], verbose=True)

The named tuple is returned from this function:

def getagreement(task):
    return Agree(kappa=task.kappa(),alpha=task.alpha(),avg_ao=task.avg_Ao())

is called here and pickled:

     future_dict[executor.submit(getagreement,task)]=frozenset(annotators)
       ... 
       detaildata[future_dict[future]]=future.result()

 cPickle.dump(detaildata,open(os.path.dirname(jsonflist[0])+'\\out.picl','w'))

Unpickling gives error:

c=cPickle.load(open(subsdir))
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  AttributeError: 'module' object has no attribute 'Agree'

Disassembly of the file:

 pickletools.dis(f)
  126: c    GLOBAL     '__builtin__ tuple'
  147: p    PUT        9
  151: (    MARK
  152: F        FLOAT      0.22320438764335693
  174: F        FLOAT      0.21768346003098427
  196: F        FLOAT      0.7004133685136325
  218: t        TUPLE      (MARK at 151)
  219: t    TUPLE      no MARK exists on stack
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\Lib\pickletools.py", line 2009, in dis
    raise ValueError(errormsg)
ValueError: no MARK exists on stack

Both pickle and cPickle give similar errors.

1

There are 1 answers

0
zero323 On

I would guess that you defined Agree in one module and try to load data in different module in which Agree is not defined. Try something like below and if it works import defined named tuple into module from which you load data.

import collections
import cPickle
Agree = collections.namedtuple('Agree', ['kappa', 'alpha','avg_ao'], verbose=True)
c = cPickle.load(open(subsdir))