Announcing the new Datavyu v1.3.6 with enhanced features and transcription support!

Are their any resources I can look into that will help me understand how to print nested variables using the API? One variable might contain multiple cells within the timeframe of a single cell from a different variable. The variables with the larger cells will just repeat information based on how many smaller cells are nested within that timeframe.

I currently have it set up to where it only prints cell information for each variable one at a time.

EXAMPLE:

begin

###assign Ruby variables to OpenShapa variables

  subid = getVariable("id")
  pspeech = getVariable("speechparent")

  ####print individual cell info for each variable

print "\n"
  print "ID Variable", "\n"
print "Ordinal", "\t", "Onset", "\t", "Offset", "\t", 
    "Time (sec)", "\t", "Name", "\t", "Tape", "\n"

  for id_cell in subid.cells
      print id_cell.ordinal, "\t", id_cell.onset, "\t", id_cell.offset, "\t",
      sprintf("%.3f",((id_cell.offset/1000.0)-(id_cell.onset/1000.0))), "\t",
      id_cell.name, "\t", id_cell.tape, "\n"
  end

  print "\n"
  print "Parent Speech Variable", "\n"
print "Ordinal", "\t", "Onset", "\t", "Offset", "\t", 
    "Time (sec)", "\t", "Phrase", "\n"

for psp_cell in pspeech.cells
  print psp_cell.ordinal, "\t", psp_cell.onset, "\t",
  psp_cell.offset, "\t", sprintf("%.3f",((psp_cell.offset/1000.0)-(psp_cell.onset/1000.0))), "\t", psp_cell.phrase, "\n"
end

end

SCRIPT OUTPUT:

Got column index. 212 Got column index. 229

ID Variable Ordinal Onset Offset Time (sec) Name Tape

1 0 10000 10.000 kid1 1

2 11000 20000 9.000 kid2 2

Parent Speech Variable

Ordinal Onset Offset Time (sec) Phrase

1 7000 7900 0.900 phrase1

2 7950 9000 1.050 phrase2

3 11500 17000 5.500 phrase3

asked 22 Sep '11, 13:41

Clinton's gravatar image

Clinton
111171924
accept rate: 36%


In order to print nested variables, you just use a nested for loop of the variables that you want to bring, starting with the outer-most variable. For example, using your data, if you wanted to print your parentspeech variable inside of id, you would do the following:


begin

###assign Ruby variables to OpenShapa variables

  subid = getVariable("id")
  pspeech = getVariable("speechparent")

  ####printing nested cell data
  for id_cell in subid.cells
    for psp_cell in pspeech.cells
        # Now we need something to let us know that these two cells are nested, such as whether or not the psp_cell is contained inside of the onset and offset of id
        if psp_cell.onset >= id_cell.onset and psp_cell.offset <= id_cell.offset
          # If we get in here then we having a matching cell, so lets print it
          print id_cell.ordinal, "\t", id_cell.onset, "\t", id_cell.offset, "\t", sprintf("%.3f",((id_cell.offset/1000.0)-(id_cell.onset/1000.0))), "\t", id_cell.name, "\t", id_cell.tape, print psp_cell.ordinal, "\t", psp_cell.onset, "\t", psp_cell.offset, "\t", sprintf("%.3f",((psp_cell.offset/1000.0) - (psp_cell.onset/1000.0))), "\t", psp_cell.phrase, "\n"
        end #end if statement
    end #end inner for loop
  end #end outer for loop

The most important part in there is the if statement. You need some way to bind the two cells together. The one that I've used simply checks to see whether or not the current pspeech cell that it is looking over is within the onset and offset of the id cell that it is looping over. If it is, then they are nested, so print it. If, however, it is on a different ID cell, and that pspeech cell that the forloop in on is not within that ID cell, that If check fails and it will not print.

Now, say you wanted to print a reliability column along with the primary column. The onsets and offsets could be different, but you could use an if statement to match the trial number, so you'd use something like:

if primary_cell.trialnum == rel_cell.trialnum print .... end

Please let me know if you have any other issues, or if this explanation is not sufficiently clear.

permanent link

answered 22 Sep '11, 13:42

Clinton's gravatar image

Clinton
111171924
accept rate: 36%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×18

question asked: 22 Sep '11, 13:41

question was seen: 3,461 times

last updated: 22 Sep '11, 13:42

powered by OSQA