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

I have several scripts that work well for a specific coding scheme but are difficult to adapt for new schemes. The code names (i.e., positive affect, negative affect) are currently written in to the code body. I would like instead to be able to set the code names as a parameter at the beginning of the script; this way, it would be easier to adapt scripts when code names change. I am having trouble finding a method for calling code names. Any suggestions?

This is an example of one of the scripts I am trying to edit:


data_folder = '~/Merged Files'

output_file = '~/Outputs/CheckRel.csv'

id_column_name = 'ID'

episode_column_name = 'Episode'

behavior_column_name = 'Rel_Affect'

missing_value = ''

Code Body


def getDatavyuFiles(path, recurse=false) path = File.expand_path(path) filter = (recurse)? '/.opf' : '.opf' return Dir.chdir(path){ |dir| Dir.glob(filter) } end

Main routine

require 'Datavyu_API.rb' begin # Initialize array for our data data ={ |h,k| h[k] = } maxEpisodeLen = 0 # maximum number of behavior cells in an episode

# Get datavyu files data_folder = File.expand_path(data_folder) files = getDatavyuFiles(data_folder)

# Iterate over files files.each do |file| # Load the file $db,$proj = loadDB(File.join(data_folder, file)) puts "Loaded file #{file}" # Load columns from spreadsheet colID = getVariable(id_column_name) colEpisode = getVariable(episode_column_name) colBehavior = getVariable(behavior_column_name)

# Iterate over ID cells
colID.cells.each do |idcell|
  # Get ID codes
  id_codes = [idcell.idnum]
  temp = []  # store data for rest of this row
  # Iterate over nested episode cells{ |epcell| idcell.contains(epcell) }.each do |epcell|
    # Initialize arrays for storing data for behavior cells in this episode
    onsets = []
    offsets = []
    pos = []
    neg = []

    # Iterate over nested behavior cells{ |bxcell| epcell.contains(bxcell) }.each do |bxcell|
      onsets << bxcell.onset/1000.0
      offsets << bxcell.offset/1000.0
      pos << bxcell.positive
      neg << bxcell.negative

    # Store to hash
    data[file][epcell.ordinal] = {
      :onsets => onsets,
      :offsets => offsets,
      :pos => pos,
      :neg => neg
    maxEpisodeLen = [maxEpisodeLen, onsets.size].max

  # Store to hash
  data[file][:id] = id_codes


# Process the dataset to pad to maximum episode length rows = [] data.each_pair do |key, val| eps = val.reject{ |x, y| x == :id} eps.each_pair do |jnk, ep| ep.each_pair do |k,v| if v.size < maxEpisodeLen ep[k] += [missing_value] * (maxEpisodeLen-v.size) end end end rows << val[:id] +{ |x| x[:onsets] + x[:offsets] + x[:pos] + x[:neg] }.flatten end

# Create header (assume 3 episodes) header = ['ID'] for i in 1..3 for col in ['Ons', 'Off', 'Pos', 'Neg'] for j in 1..maxEpisodeLen header << "E#{i}#{col}#{j}" end end end

# Open output file outfile =, 'w+') outfile.puts header.join(',')

# Iterate over rows of data in cache and print to output file rows.each{ |row| outfile.puts(row.join(',')) }

outfile.close rescue StandardError => e puts e.message puts e.backtrace end

asked 18 Jan '18, 16:52

Penina%20Backer's gravatar image

Penina Backer
accept rate: 0%

I think for this specific script the easiest way to change it is to create variables at the top of the script (i.e., the parameter section) for each of the two codes. For example:

first_code_name = 'positive'
second_code_name = 'negative'

Then you would want to update the lines in the body section where the values are fetched from the cell:

pos << bxcell.positive
neg << bxcell.negative

to this instead:

pos << bxcell.get_code(first_code_name)
neg << bxcell.get_code(second_code_name)
permanent link

answered 22 Jan '18, 09:58

Shohan%20Hasan's gravatar image

Shohan Hasan ♦♦
accept rate: 14%

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: 18 Jan '18, 16:52

question was seen: 1,863 times

last updated: 22 Jan '18, 09:58

Related questions

powered by OSQA