Problems using deepcopy to copy an image list

523 views
Skip to first unread message

aa93...@gmail.com

unread,
Feb 7, 2016, 8:22:31 PM2/7/16
to psychopy-users
Hello,

I am having some trouble using deepcopy and I can't find much help with this online. I am trying to make a deepcopy of an image list because I need to keep track of randomised images so I can compare back to the copy to make future decisions on what is displayed. I seem to be getting a whole host of error messages that look to be related to the copy module. Is there a known problem with using deepcopy and Psychopy? I have tried using the [:] to make a copy but I had problems there too.

Code:

from psychopy import core, visual, gui, event
import glob, os, numpy
import random
from copy import deepcopy

#create global window & clock
window = visual.Window([800,600],color=(-1, -1, -1), colorSpace='rgb', rgb=None, allowGUI=True, monitor='testMonitor', units='deg')
myClock = core.Clock()

imgList1 = glob.glob(os.path.join('C:\Users\S\Documents\stim','*.png'))    #list of stim

set_one = [visual.ImageStim(window, img) for img in imgList1[:6]]           #create small image set
set_one_copy = deepcopy(set_one)                                                     #deepcopy of set

set_one[0].draw()                                                                               #draw target image
window.flip()
core.wait(1)

d1 = set_one[0]                                                                                 #set d1 image to begin if statements
if set_one[0] == set_one_copy[0]:                                                       #if displayed image matches an image from the deepcopy then display appropriate image from copy list
    d1 = set_one_copy[0]
    if set_one[0] == set_one_copy[1]:
        d1 = set_one_copy[1]
        if set_one[0] == set_one_copy[2]:
            d1 = set_one_copy[2]
            if set_one[0] == set_one_copy[3]:
                d1 = set_one_copy[3]
                if set_one[0] == set_one_copy[4]:
                    d1 = set_one_copy[4]
                    if set_one[0] == set_one_copy[5]:
                        d1 = set_one_copy[5]
d1.draw()                                                                                          #based on if statements, display appropriate image
window.flip()
core.wait(1)
window.close()


Here are my error messages:

Traceback (most recent call last):
  File "C:\Users\S\Desktop\stim\gaga.py", line 13, in <module>
    set_one_copy = deepcopy(set_one)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 230, in _deepcopy_list
    y.append(deepcopy(a, memo))
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 190, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 334, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 163, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 257, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files (x86)\PsychoPy2\lib\copy.py", line 182, in deepcopy
    rv = reductor(2)
ValueError: ctypes objects containing pointers cannot be pickled
Exception AttributeError: "'ImageStim' object has no attribute '_texID'" in <bound method ImageStim.__del__ of <psychopy.visual.image.ImageStim object at 0x0758E210>> ignored
Exception AttributeError: "'Window' object has no attribute '_closed'" in <bound method Window.__del__ of <psychopy.visual.window.Window object at 0x0758E2B0>> ignored

Cheers,
S

Michael MacAskill

unread,
Feb 8, 2016, 12:59:56 AM2/8/16
to psychop...@googlegroups.com
Hello S?,

> Is there a known problem with using deepcopy and Psychopy?
No. But there is a whole bag of hurt awaiting people who play with deepcopy at all, without a very good conceptual understanding of the myriad hierarchical issues that arise.

I don't understand this stuff enough to give useful advice. But I would question why you want to deepcopy the image stimuli at all, when you could just deal with a list of the corresponding names of the stimulus files?

I can't follow the logic of the code below, but if you tell us what you are trying to achieve, perhaps we could suggest a way of achieving it that is a bit more Pythonic and PsychoPy-friendly.

Regards,

Michael
> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
> To post to this group, send email to psychop...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/04caeca5-8d58-49af-a7e8-12aa6710ff92%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
Michael R. MacAskill, PhD 66 Stewart St
Research Director, Christchurch 8011
New Zealand Brain Research Institute NEW ZEALAND

Senior Research Fellow, michael....@nzbri.org
Te Whare Wānanga o Otāgo, Otautahi Ph: +64 3 3786 072
University of Otago, Christchurch http://www.nzbri.org/macaskill

Jonathan Peirce

unread,
Feb 8, 2016, 5:55:57 AM2/8/16
to psychop...@googlegroups.com
I would advise against using a deepcopy of a stimulus object ever. copy
and deepcopy can only copy memory objects on the main computer memory,
but image stimuli occupy memory on the graphics card as well.

What I would do instead is something like this:

stims = {}
for imName in imageList:
stims[imName] = ImageStim(win, imName,...)

namesCopy = copy.copy(imageList)
# play with names, shuffle and whatever you need
# ...
# then use one of the images by name
thisStim = stims[someImageName]
thisStim.draw()

cheers
Jon
Jonathan Peirce
University of Nottingham

http://www.peirce.org.uk





This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please send it back to me, and immediately delete it.

Please do not use, copy or disclose the information contained in this
message or in any attachment. Any views or opinions expressed by the
author of this email do not necessarily reflect the views of the
University of Nottingham.

This message has been checked for viruses but the contents of an
attachment may still contain software viruses which could damage your
computer system, you are advised to perform your own checks. Email
communications with the University of Nottingham may be monitored as
permitted by UK legislation.

aa93...@gmail.com

unread,
Feb 8, 2016, 2:12:15 PM2/8/16
to psychopy-users
Hi Jon & Michael,

Thanks for your help, I will avoid deepcopy from now on then.

Jon, as for your suggestion, I don't fully understand what is going on there. I was under the impression that copy.copy would be a shallow copy, meaning that when i shuffled the copy list, the original list would get shuffled too. I need to refer to images in a list by their names rather than element positions in the list, so i can track them after a shuffle has been done. Is that what your code is doing? If so, I can't see how they're being named.

Michael MacAskill

unread,
Feb 8, 2016, 3:28:54 PM2/8/16
to psychop...@googlegroups.com

> On 9/02/2016, at 08:12, aa93...@gmail.com wrote:
>
> Jon, as for your suggestion, I don't fully understand what is going on there. I was under the impression that copy.copy would be a shallow copy, meaning that when i shuffled the copy list, the original list would get shuffled too. I need to refer to images in a list by their names rather than element positions in the list, so i can track them after a shuffle has been done. Is that what your code is doing? If so, I can't see how they're being named.

When in doubt, try it out (in any Python terminal):

from numpy.random import shuffle
import copy

a = [1, 2, 3, 4, 5]
b = a
c = copy.copy(a)

shuffle(a)

>>> a
[3, 4, 2, 5, 1]

>>> b
[3, 4, 2, 5, 1]

>>> c
[1, 2, 3, 4, 5]


Note that copy does actually make a separate copy of the list (pointed to by the variable c), as opposed to just setting up another pointer to the original object (in the variable b). Deepness/shallowness refers to whether objects within the list (and objects attached to those objects…) are themselves copied.

Regardless, any copying anything at all seems like an unusual way to manage things. What about just setting up a dictionary which maps image names to indices?

Or probably much better, as Jon showed, a dictionary which directly maps image names to their corresponding objects?

This is a much more Pythonic way of doing things. Directly accessing images by name or index would certainly avoid your multiple if/then statements.

Regards,

Michael

parsnip 1

unread,
Feb 8, 2016, 3:35:20 PM2/8/16
to psychop...@googlegroups.com
Hi Michael,

Brilliant, thank you. I can see now that there was never any need to copy at all, but at one point I did think it was the optimal solution (beginner mistake-you live and learn). I have managed to sort it out by assigning names to each object at the beginning, as you (and Jon) suggested, but I've done it using lists, it seems to work perfectly.

Thank you for all the help, it really is invaluable having this supportive community available.

Cheers
Steve

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages