Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Building a loop in python

Hi,

In my file pool I have a bunch of images, all called <name>.jpg. I want to use each image with its corresponding name in a loop (basically, this is for the familiarisation phase of an experiment).

Given a list called filename and one called name, made like this:

# get all images in the file pool (all .jpg format)
filename = [image for image in pool if ".jpg" in image]

# images are named after their content, so these are the image names
name = [image.rsplit(".",1)[0] for image in filename

is there a way I can populate a loop datamatrix so that it looks as below, and then use it?

name filename
apple apple.jpg
banana banana.jpg
... ...

Thanks in advance!

Comments

  • Hello,

    Yes you can do it with DataMatrix:

    http://osdoc.cogsci.nl/3.1/manual/structure/loop/

    http://datamatrix.cogsci.nl/

    The strategy I use (but there might be something better) is to get an inline at the very beginning of my script where I do stuffs to list (randomizing, name... whatever) and populate.

    from datamatrix import DataMatrix
    
    # do stuffs to lists
    filename = range(10)
    filename = [ str(x)+ '.png' for x in filename]
    name = range(10)
    
    # Populate
    
    dm_loop = DataMatrix(length=len(filename))
    # fill the first column with the randomized list
    dm_loop.filename = filename
    # initialize and fill the two other columns
    dm_loop.name = name
    
    items['new_loop'].dm = dm_loop
    

    Then having a loop that is empty and sequential. Insert in this loop the sequence (classical).

    Please find the script attached. Not sure if it is the best strategy but that is the one I use. I hope it helps.

    Best,

    Sylvain

    Thanked by 1martinc
  • Thanks Sylvain, that works perfectly :smile:

Sign In or Register to comment.