Announcing the new Datavyu v1.4.0 with enhanced features, bug fixes, and a new video player for Windows!

We are in the process of migrating away from this forum—feel free to browse existing help topics. For new support inquiries, please reach out to us via email at [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:

Parameters

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

Functions

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 = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) } 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
  colEpisode.cells.select{ |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
    colBehavior.cells.select{ |bxcell| epcell.contains(bxcell) }.each do |bxcell|
      onsets << bxcell.onset/1000.0
      offsets << bxcell.offset/1000.0
      pos << bxcell.positive
      neg << bxcell.negative
    end

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

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

end

# 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] + eps.values.map{ |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 = File.open(File.expand_path(output_file), '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, 16:52

Penina%20Backer's gravatar image

Penina Backer
46449
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, 09:58

Shohan%20Hasan's gravatar image

Shohan Hasan ♦♦
531126
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

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:

×1

question asked: 18 Jan, 16:52

question was seen: 653 times

last updated: 22 Jan, 09:58

Related questions

powered by OSQA