Please use our GitHub Issues page for all new support inquiries. If you need to contact us directly, please e-mail [email protected]

If I wanted to extract a subset of cells from a column how could I use the setVariable function to write the new subsetted variable to a new database? I may be subsetting the array incorrectly, something similar to below.

#Assume I imported original database
largeVar = getVariable("largeVariable")

#Take the first 10 cells (error at this point?)
smallVar = largeVar.cells[0..9]

#Open a blank database
$db, proj = load_db("newDB.opf")

#Write the new variable
setVariable("Name", smallVar)

#Save the database

>>undefined method `dirty' for #<Array:0x28c3fc>

require 'OpenSHAPA_API.rb'

  #Set to and from directories
  mainDir = File.expand_path("~/Desktop/Old")
  outputDir = File.expand_path("~/Desktop/New")

  #List of variables to extract from original .opf files
  variableList = ["total-items", "object-count", "hands-parent", "hands-child", "face-parent"]

  #Max number of cells per variable
  chunkSize = 250

  #Cycle through all .opf files
  Dir.foreach(mainDir) do |eachFile|
    next if eachFile == '.' or eachFile == '..' or File.extname(eachFile) != ".opf"

    #Cycle through variables within .opf file
    variableList.each {|eachVar|

      #String of current .opf location
      oldDB = mainDir + "/" + eachFile

      #Load main .opf file
      puts "--LOADING REFERENCE DB: " + oldDB
      $db, proj = load_db(oldDB)

      #Get the variables I always want, plus the current variable in cycle
      puts "---GETTING VARIABLES"
      rubyID = getVariable("ID")
      rubyCond = getVariable("condition")
      rubyVar = getVariable(eachVar)

      #Get length of cells for current variable of interest
      nCells = rubyVar.cells.length

      #Sequence of chunks based on length of variable
      cellSeq = (1..nCells).step(chunkSize).to_a

      #number of chunks in stepwise sequence
      nChunks = cellSeq.length

      #append exact number of cells to end of sequence
      cellSeq[nChunks] = nCells

      #return new sequence length
      nChunks = cellSeq.length

      #loop counter
      iter = 0

      #Start cycling through Sequence segments, stop at second to last index in sequence
      for chunk in cellSeq[0..(nChunks-2)]
        iter = iter+1

        #Set the minimum ordinal to current chunk value
        minCell = chunk

        #If second to to last, make max cell value the previosly appended value, else chunkSize
        if iter == nChunks-1
          maxCell = cellSeq[(nChunks-1)]
        else maxCell = chunk+(chunkSize-1)

        #Use the min and max values to subset the main variable
        #rubyVarSplit = rubyVar.cells[minCell-1..maxCell-1]

        #Use for adding range info to file name
        chunkRangeStr = minCell.to_s + "-" + maxCell.to_s
        chunkTimeRange = rubyVar.cells[minCell-1].onset.to_s + "-" + rubyVar.cells[maxCell-1].onset.to_s

        #New file location string name
        newDB = outputDir + "/" + eachFile[/.*(?=\..+$)/] + "_" + eachVar + "_" + chunkRangeStr + "_" + chunkTimeRange + ".opf"

        #I had to create a blank .opf file for some reason it was saving past info through each iteration
        #This clears any loaded data
        puts "LOADING BLANK DB"
        $db, proj = load_db(outputDir + "/" + "blankDB.opf")

        tempIDvar = createNewVariable("ID", "studyname", "subnum", "gender", "age", "bdate", "expdate", "pop", "loc")
        tempCondVar = createNewVariable("condition", "trialtype", "targetword")
        tempVar = createNewVariable(eachVar, "all", "cntr")

        #Create ID variable with one cell
        newIDcell = tempIDvar.make_new_cell()
        newIDcell.change_arg("ordinal", rubyID.cells[0].ordinal)
        newIDcell.change_arg("onset", rubyID.cells[0].onset)
        newIDcell.change_arg("offset", rubyID.cells[0].offset)
        #finish reset of args

        #Set ID variable
        setVariable("ID", tempIDvar)

        #Create condition variable and cycle through all cells
        for condCell in rubyCond.cells
          newCondCell = tempCondVar.make_new_cell()
          newCondCell.change_arg("ordinal", condCell.ordinal)
          newCondCell.change_arg("onset", condCell.onset)
          newCondCell.change_arg("offset", condCell.offset)
          newCondCell.change_arg("trialtype", condCell.trialtype)
          newCondCell.change_arg("targetword", condCell.targetword)

        #Set condition variable
        setVariable("condition", tempCondVar)

        #Create variable of interest and write only min max cell info
        for varCell in rubyVar.cells
          if varCell.ordinal >= minCell and varCell.ordinal <= maxCell
            newVarCell = tempVar.make_new_cell()
            newVarCell.change_arg("ordinal", varCell.ordinal)
            newVarCell.change_arg("onset", varCell.onset)
            newVarCell.change_arg("offset", varCell.offset)
            newVarCell.change_arg("all", varCell.all)
            newVarCell.change_arg("cntr", varCell.cntr)

        #Set variable


asked 17 Jun '12, 11:07

jmburling's gravatar image

accept rate: 0%

edited 19 Jun '12, 06:05

I believe you need to use the createNewVariable() command from the API to add a new variable to a spreadsheet. You might be able to copy the cells from largeVar to smallVar using the subset command, but I don't think you can set a variable that hasn't been created. Maybe Jesse can confirm this, but that's been my experience.

I'm also not exactly sure that you can copy cells from one var to another like that. I would try the code below and just see if it will work this way. I'd also try to see if you can copy the subset into the current spreadsheet before trying to save it to a new database, just in case that's giving you a problem.

largeVar = getVariable("largeVariable")
smallVar = createNewVariable("smallVar", ARGUMENT NAMES FROM largeVar)
for lcell in largeVar.cells
  if lcell.ordinal < 10
    newcell = smallVar.createNewCell()
    newcell.change_arg("onset", lcell.onset)
setVariable("smallVar", smallVar)
permanent link

answered 19 Jun '12, 02:26

JohnFranchak's gravatar image

JohnFranchak ♦
accept rate: 17%

Thanks, this method worked for me. I thought there might be an easier way than having to write all argument information cell by cell only because I have multiple variables with different argument names in each, and my goal was to just subset variables and create new files if they have greater than n cells.

(19 Jun '12, 06:04) jmburling jmburling's gravatar image
Your answer
toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 17 Jun '12, 11:07

question was seen: 4,606 times

last updated: 19 Jun '12, 06:05

powered by OSQA