Top Level Namespace

Defined Under Namespace

Classes: CTable, Matrix, RCell, RColumn, RVideoController

Instance Method Summary collapse

Instance Method Details

#add_codes_to_column(var, *args) ⇒ RColumn Also known as: add_args_to_var, addCodesToColumn, addArgsToVar

Add a new code to a column

Examples:

test = add_codes_to_column("test", "arg1", "arg2", "arg3")
set_column("test", test)

Parameters:

  • var (String, RColumn)

    The variable to add args to. This can be a name or a variable object.

  • args (Array<String>)

    A list of the arguments to add to var (can be any number of args)

Returns:

  • (RColumn)

    the new Ruby representation of the variable. Write it back to the database to save it.



1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
# File 'datavyu_api.rb', line 1116

def add_codes_to_column(var, *args)
  if var.class == "".class
    var = get_column(var)
  end

  var_new = createVariable(var.name, var.arglist + args)

  for cell in var.cells
    new_cell = var_new.make_new_cell()
    new_cell.onset = cell.onset
    new_cell.offset = cell.offset
    for arg in var.arglist
      v = eval "cell.#{arg}"
      new_cell.change_code(arg, v)
    end
  end

  return var_new
end

#check_datavyu_version(minVersion, maxVersion = nil) ⇒ true, false Also known as: checkDatavyuVersion

Check whether current Datavyu version falls within the specified minimum and maximum versions (inclusive)

Parameters:

  • minVersion (String)

    Minimum version (e.g. 'v:1.3.6')

  • maxVersion (String) (defaults to: nil)

    Maximum version. If unspecified, no upper bound is checked.

Returns:

  • (true, false)

    true if min,max version check passes; false otherwise.



2700
2701
2702
2703
2704
2705
2706
# File 'datavyu_api.rb', line 2700

def check_datavyu_version(minVersion, maxVersion = nil)
  currentVersion = get_datavyu_version()
  minCheck = (minVersion <=> currentVersion) <= 0
  maxCheck = (maxVersion.nil?)? true : (currentVersion <=> maxVersion) <= 0

  return minCheck && maxCheck
end

#check_reliability(main_col, rel_col, match_arg, time_tolerance, *dump_file) ⇒ nil Also known as: checkReliability, check_rel, checkRel

Do a quick, in Datavyu, check of reliability errors.

Examples:

check_reliability("trial", "rel.trial", "trialnum", 100, "/Users/motoruser/Desktop/Relcheck.txt")
check_reliability("trial", "rel.trial", "trialnum", 100)

Parameters:

  • main_col (String, RColumn)

    Either the string name or the Ruby column from get_column of the primary column to compare against.

  • rel_col (String, RColumn)

    Either the string name or the Ruby column from get_column of the reliability column to compare to the primary column.

  • match_arg (String)

    The string of the argument to use to match the relability cells to the primary cells. This must be a unique identifier between the cells.

  • time_tolerance (Integer)

    The amount of slack you allow, in milliseconds, for difference between onset and offset before it is considered an error. Set to 0 for no difference allowed and to a very large number for infinite distance allowed.

  • dump_file (String, File)

    (optional): The full string path to dump the relability output to. This can be used for multi-file dumps or just to keep a log. You can also give it a Ruby File object if a file is already started.

Returns:

  • (nil)


2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
# File 'datavyu_api.rb', line 2197

def check_reliability(main_col, rel_col, match_arg, time_tolerance, *dump_file)
  # Make the match_arg conform to the method format that is used
  match_arg = RColumn.sanitize_codename(match_arg)

  # Set up our method variables
  dump_file = dump_file[0]
  if main_col.class == "".class
    main_col = get_column(main_col)
  end
  if rel_col.class == "".class
    rel_col = get_column(rel_col)
  end

  printing = false
  if dump_file != nil
    if dump_file.class == "".class
      dump_file = open(dump_file, 'a')
    end
    printing = true
  end

  # Define interal function for printing errors
  def print_err(m_cell, r_cell, arg, dump_file, main_col, rel_col)
    main_val = eval "m_cell.#{arg}"
    rel_val = eval "r_cell.#{arg}"
    err_str = "ERROR in " + main_col.name + " at Ordinal " + m_cell.ordinal.to_s + ", rel ordinal " + r_cell.ordinal.to_s + " in argument " + arg + ": " + main_val.to_s + ", " + rel_val.to_s + "\n"
    if dump_file != nil
      dump_file.write(err_str)
    end
    print err_str
  end

  # Build error array
  errors = Hash.new
  for arg in main_col.arglist
    errors[arg] = 0
  end
  errors["onset"] = 0
  errors["offset"] = 0

  # Now check the cells
  for mc in main_col.cells
    main_bind = eval "mc.#{match_arg}"
    for rc in rel_col.cells
      rel_bind = eval "rc.#{match_arg}"
      if main_bind == rel_bind
        # Then check these cells match, check them for errors
        if (mc.onset - rc.onset).abs >= time_tolerance
          print_err(mc, rc, "onset", dump_file, main_col, rel_col)
          errors["onset"] = errors["onset"] + 1
        end
        if (mc.offset - rc.offset).abs >= time_tolerance
          print_err(mc, rc, "offset", dump_file, main_col, rel_col)
          errors["offset"] = errors["offset"] + 1
        end

        for arg in main_col.arglist
          main_val = eval "mc.#{arg}"
          rel_val = eval "rc.#{arg}"
          if main_val != rel_val
            print_err(mc, rc, arg, dump_file, main_col, rel_col)
            errors[arg] = errors[arg] + 1
          end
        end
      end
    end
  end

  for arg, errs in errors
    str = "Total errors for " + arg + ": " + errs.to_s + ", Agreement:" + "%.2f" % (100 * (1.0 - (errs / rel_col.cells.length.to_f))) + "%\n"
    print str
    if dump_file != nil
      dump_file.write(str)
      dump_file.flush()
    end
  end

  return errors, rel_col.cells.length.to_f
end

#check_reliability_continuous(pri_col, rel_col, block_col, time_threshold, *codes_to_check) ⇒ RColumn

Check reliability for a continuous coding pass. Merge the two coders' columns and filter out the agreements. Returns a column with cells for regions where coders disagreed.

Examples:

check_reliability_continuous('reach', 'reach_rel', 'reach_blocks', 100, 'hand', 'touch')

Parameters:

  • pri_col (String, RColumn)

    Primary's coder's column

  • rel_col (String, RColumn)

    Reliability coder's column

  • block_col (String, RColumn)

    Column containing coding block. Use nil if none.

  • time_threshold (Integer)

    The amount of slack you allow, in milliseconds, for difference between onset and offset before it is considered a disagreement.

  • codes_to_check (Array<String>)

    List of codes to compare between the coders

Returns:

  • (RColumn)

    column with cells for disagreements

Since:

  • 1.4.0



2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
# File 'datavyu_api.rb', line 2292

def check_reliability_continuous(pri_col, rel_col, block_col, time_threshold, *codes_to_check)
  p_col = (pri_col.class == String)? get_column(pri_col) : pri_col
  r_col = (rel_col.class == String)? get_column(rel_col) : rel_col
  unless (block_col.nil? || block_col == '')
    b_col = (block_col.class == String)? get_column(block_col) : block_col
  end

  raise "Invalid columns" unless p_col.class == RColumn && r_col.class == RColumn

  # Build a set to store tuples of (onset,offset).  We need this to find cells with duration
  # less than mutex_threshold that have been entirely missed by one of the coders.
  interval_map = (p_col.cells + r_col.cells).map{ |x| [x.onset.to_i,x.offset.to_i] }.uniq

  m_col = merge_columns('disagreements', pri_col, rel_col)

  # Remove cells not contained by block column
  unless b_col.nil?
    m_col.cells.reject! do |slice|
      !(b_col.cells.any?{ |x| x.contains(slice) } )
    end
  end

  p_prefix = p_col.name.downcase
  r_prefix = r_col.name.downcase
  # Iterate over merged column cells and filter out agreements.
  m_col.cells.reject! do |slice|
    dur = slice.duration
    num_coders = [slice.get_code("#{p_prefix}_ordinal"), slice.get_code("#{r_prefix}_ordinal")].reject{ |x| x == '' }.size

    whole_cell = interval_map.include?([slice.onset, slice.offset])

    pri_codes = codes_to_check.map{ |x| slice.get_code("#{p_prefix}_#{x}") }
    rel_codes = codes_to_check.map{ |x| slice.get_code("#{r_prefix}_#{x}") }
    code_differs = pri_codes.zip(rel_codes).any?{ |pair| pair.first != pair.last }

    flag_duration = (num_coders == 1) && (dur >= time_threshold)
    flag_missed = (num_coders == 1) && whole_cell
    flag_code = (num_coders == 2) && code_differs
    # p [flag_duration, flag_missed, flag_code]
    !(flag_duration || flag_missed || flag_code)
  end

  return m_col
end

#check_valid_codes(var, dump_file, *arg_code_pairs) ⇒ Object Also known as: checkValidCodes

Do a quick, in Datavyu, check of valid codes.

Examples:

check_valid_codes("trial", "", "hand", ["l","r","b","n"], "turn", ["l","r"], "unit", [1,2,3])

Parameters:

  • var (String, RColumn)

    name of column to check

  • dump_file (String, File)

    output file to print messages to. Use '' to print to console.

  • arg_code_pairs (Array<String>)

    A list of the argument names and valid codes in the following format: “argument_name”, [“y”,“n”], “argument2”, [“j”,“k”,“m”]



2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
# File 'datavyu_api.rb', line 2344

def check_valid_codes(var, dump_file, *arg_code_pairs)
  if var.class == "".class
    var = get_column(var)
  end

  if dump_file != ""
    if dump_file.class == "".class
      dump_file = open(dump_file, 'a')
    end
  end

  # Make the argument/code hash
  arg_code = Hash.new
  for i in 0...arg_code_pairs.length
    if i % 2 == 0
      if arg_code_pairs[i].class != "".class
        print_debug 'FATAL ERROR in argument/valid code array.  Exiting.  Please check to make sure it is in the format "argumentname", ["valid","codes"]'
        exit
      end
      arg = arg_code_pairs[i]
      arg = RColumn.sanitize_codename(arg)

      arg_code[arg] = arg_code_pairs[i+1]
    end
  end

  errors = false
  for cell in var.cells
    for arg, code in arg_code
      val = eval "cell.#{arg}"
      if not code.include?(val)
        errors = true
        str = "Code ERROR: Var: " + var.name + "\tOrdinal: " + cell.ordinal.to_s + "\tArg: " + arg + "\tVal: " + val + "\n"
        print str
        if dump_file != ""
          dump_file.write(str)
        end
      end
    end
  end
  if not errors
    print_debug "No errors found."
  end
end

#check_valid_codes2(data, outfile, *arg_filt_pairs) ⇒ Object Also known as: checkValidCodes2

Check valid codes on cells in a column using regex. Backwards-compatible with checkValidCodes

Examples:

checkValidCodes2("trial", "", "hand", ["l","r","b","n"], "turn", ["l","r"], "unit", /\A\d+\Z/)

Parameters:

  • data (String, RColumn, Hash)

    When this parameter is a String or a column object from get_column(), the function operates on codes within this column. If the parameter is a Hash (associative array), the function ignores the arg_code_pairs arguments and uses data from this Hash. The Hash must be structured as a nested mapping from columns (either as Strings or RColumns) to Hashes. These nested hashes must be mappings from code names (as Strings) to valid code values (as either lists (Arrays) or patterns (Regexp)).

  • outfile (String, File)

    The full path of the file to print output to. Use '' to print to scripting console.

  • arg_filt_pairs

    Pairs of code name and acceptable values either as an array of values or regexp. Ignored if first parameter is a Hash.

Returns:

  • nothing

Since:

  • 1.3.6



2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
# File 'datavyu_api.rb', line 2399

def check_valid_codes2(data, outfile, *arg_filt_pairs)
	if data.class == "".class
		data = get_column(data)
  elsif data.class == Hash
    # data is already a hashmap
    map = data
	end

	unless outfile == ""
		if outfile.class == "".class
	    	outfile = open(File.expand_path(outfile), 'a')
	  	end
	end

  # Create a map if a mapping wasn't passed in. Mostly for backwards compatibility with checkValidCodes().
  if map.nil?
    map = Hash.new

  	# Make the argument/code hash
  	arg_code = Hash.new
  	for i in 0...arg_filt_pairs.length
  	  if i % 2 == 0
    		if arg_filt_pairs[i].class != "".class
    			print_debug 'FATAL ERROR in argument/valid code array.  Exiting.  Please check to make sure it is in the format "argumentname", ["valid","codes"]'
    			exit
    		end

    		arg = arg_filt_pairs[i]
                arg = RColumn.sanitize_codename(arg)

  	    # Add the filter for this code.  If the given filter is an array, convert it to a regular expression using Regex.union
        arg_code[arg] = arg_filt_pairs[i+1]
  	  end
  	end

    map[data] = arg_code
  end

	errors = false
  # Iterate over key,entry (column, valid code mapping) in map
  map.each_pair do |var, col_map|
    var = get_column(var) if var.class == String

    # Iterate over cells in var and check each code's value
  	for cell in var.cells
      for arg, filt in col_map
      	val = eval "cell.#{arg}"
        # Check whether value is valid — different functions depending on filter type
        valid = case # note: we can't use case on filt.class because case uses === for comparison
        when filt.class == Regexp
        	!(filt.match(val).nil?)
        when filt.class == Array
          filt.include?(val)
        else
          raise "Unhandled filter type: #{filt.class}"
        end

        if !valid
          errors = true
          str = "Code ERROR: Var: " + var.name + "\tOrdinal: " + cell.ordinal.to_s + "\tArg: " + arg + "\tVal: " + val + "\n"
          print str
          outfile.write(str) unless outfile == ""
        end
      end
  	end
  end
	unless errors
  	print_debug "No errors found."
	end
end

#check_valid_codes3(map, outfile = nil) ⇒ Object

Check valid codes on cells in a column using regex. Not backwards-compatible with check_valid_codes().

Parameters:

  • map (Hash)

    The Hash must be structured as a nested mapping from columns (either as Strings or RColumns) to Hashes. These nested hashes must be mappings from code names (as Strings) to valid code values (as either lists (Arrays) or patterns (Regexp)).

  • outfile (String, File) (defaults to: nil)

    The full path of the file to print output to. Omit to print only to console.

Returns:

  • number of detected errors

  • a list containing all error messages

Since:

  • 1.3.6



2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
# File 'datavyu_api.rb', line 2477

def check_valid_codes3(map, outfile = nil)
  # Open outfile if given
  unless outfile.nil?
    outfile = open(File.expand_path(outfile), 'a') if outfile.class == ''.class
  end

  errors = []
  err_count = 0
  # Iterate over key,entry (column, valid code mapping) in map
  map.each_pair do |var, col_map|
    var = get_column(var) if var.class == String

    # Iterate over cells in var and check each code's value
  	var.cells.each do |cell|
      col_map.each_pair do |code, filt|
      	val = cell.get_code(code)
        # Check whether value is valid — different functions depending on filter type
        valid = case # note: we can't use case on filt.class because case uses === for comparison
        when filt.class == Regexp
        	!(filt.match(val).nil?)
        when filt.class == Array
          filt.include?(val)
        when filt.class == Proc
          filt.call(val)
        else
          raise "Unhandled filter type: #{filt.class}"
        end

        if !valid
          err_count += 1
          row = [var.name, cell.ordinal, code, val].join("\t")
          errors << row
        end
      end
  	end
  end

  if(err_count > 0)
    print_debug "Found #{errors} errors."
    header = (%w(COLUMN CELL_ORDINAL CODE VALUE)).join("\t")
    puts header
    puts errors
    unless outfile.nil?
      outfile.puts header
      outfile.puts errors
      outfile.close
    end
  end

  return [err_count, errors]
end

#column_to_videos(column = 'tracks__', file_code = 'file', plugin_code = 'plugin') ⇒ Object



2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
# File 'datavyu_api.rb', line 2863

def column_to_videos(column = 'tracks__', file_code = 'file',
                     plugin_code = 'plugin')
  tracks_col = column.class == String ? get_column(column) : column
  raise "Invalid column: #{column}" if tracks_col.nil?

  tracks_col.cells.each do |cell|
    new_video(cell.get_code(file_code),
              cell.get_code(plugin_code),
              cell.onset)
  end
end

#combine_columns(name, varnames) ⇒ Object

TODO:

verify this works

Note:

Not thoroughly tested. Only merges first and last columns.

Combine cells of different columns into a new column. Iteratively runs #create_mutually_exclusive on additional columns.



1472
1473
1474
1475
1476
1477
1478
1479
# File 'datavyu_api.rb', line 1472

def combine_columns(name, varnames)
  stationary_var = varnames[0]
  for i in 1...varnames.length
    next_var = varnames[i]
    var = create_mutually_exclusive(name, stationary_var, next_var)
  end
  return var
end

#compute_kappa(pri_col, rel_col, *codes) ⇒ Hash<String, Fixnum>, Hash<String, Matrix> Also known as: computeKappa

Compute Cohen's kappa from the given primary and reliability columns.

Examples:

primary_column_name = 'trial'
reliability_column_name = 'trial_rel'
codes_to_compute = ['condition', 'result']
kappas, tables = compute_kappa(colPri, colRel, codes_to_compute)
kappas.each_pair { |code, k| puts "#{code}: #{k}" }

Parameters:

  • pri_col (RColumn, String)

    primary coder's column

  • rel_col (RColumn, String)

    reliability coder's column

  • codes (Array<String>)

    codes to compute scores for

Returns:

  • (Hash<String, Fixnum>)

    mapping from code names to kappa values

  • (Hash<String, Matrix>)

    mapping fromm code names to contingency tables



662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'datavyu_api.rb', line 662

def compute_kappa(pri_col, rel_col, *codes)
  codes = pri_col.arglist if codes.nil? || codes.empty?
  raise "No codes!" if codes.empty?

  pri_col = get_column(pri_col) if pri_col.class == String
  rel_col = get_column(rel_col) if rel_col.class == String
  codes.flatten!

  raise "Invalid parameters for getKappa()" unless (pri_col.class==RColumn && rel_col.class==RColumn)

  # Get the list of observed values in each cell, per code
  cells = pri_col.cells + rel_col.cells

  # Build a hashmap from the list of codes to all observed values for that code
  # across primary and reliability cells.
  observed_values = Hash.new{ |h, k| h[k] = [] }
  cells.each do |cell|
    codes.each do |code|
      observed_values[code] << cell.get_code(code)
    end
  end

  # Take the unique values for each code.
  # Filter out codes that do not have minimum number of required values to compute kappa.
  observed_values.delete_if do |c, vs|
    vs.uniq!
    if vs.size < 2
      puts "Cannot compute score for #{c} (less than 2 values observed): #{v.join(',')}"
      true
    else
      false
    end
  end

  # Init contingency tables for each code name
  tables = Hash.new
  observed_values.each_pair do |codename, codevalues|
    tables[codename] = CTable.new(*codevalues)
  end

  # Get the pairs of corresponding primary and reliability cells
  cellPairs = Hash.new
  rel_col.cells.each do |relcell|
    cellPairs[relcell] = pri_col.cells.find{ |pricell| pricell.onset == relcell.onset} # match by onset times
  end

  cellPairs.each_pair do |pricell, relcell|
    tables.keys.each do |x|
      tables[x].add(pricell.get_code(x), relcell.get_code(x))
    end
  end


  kappas = Hash.new
  tables.each_pair do |codename, ctable|
    kappas[codename] = ctable.kappa
  end

  return kappas, tables
end

#create_mutually_exclusive(name, var1name, var2name, var1_argprefix = nil, var2_argprefix = nil) ⇒ RColumn Also known as: createMutuallyExclusive

Combine two columns into a third column. The new column's code list is a union of the original two columns with a prefix added to each code name. The default prefix is the name of the source column (e.g. column “task” code “ordinal” becomes “task_ordinal”) Create a new column from two others, mixing their cells together such that the new variable has all of the arguments of both other variables and a new cell for each overlap and mixture of the two cells.

Examples:

test = create_mutually_exclusive("test", "var1", "var2")
set_column("test",test)

Parameters:

  • name

    name of the new variable.

  • var1name

    name of the first variable to be mutexed.

  • var2name

    name of the second variable to be mutexed.

  • var1_argprefix (String) (defaults to: nil)

    (optional) String to prepend to codes of column 1; defaults to name of column 1

  • var2_argprefix (String) (defaults to: nil)

    (optional) String to prepend to codes of column 2; defaults to name of column 2

Returns:

  • (RColumn)

    The new Ruby representation of the variable. Write it back to the database to save it.



1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
# File 'datavyu_api.rb', line 1233

def create_mutually_exclusive(name, var1name, var2name, var1_argprefix=nil, var2_argprefix=nil)
  if var1name.class == "".class
    var1 = get_column(var1name)
  else
    var1 = var1name
  end
  if var2name.class == "".class
    var2 = get_column(var2name)
  else
    var2 = var2name
  end

  scan_for_bad_cells(var1)
  scan_for_bad_cells(var2)

  for cell in var1.cells
    if cell.offset == 0
      puts "ERROR: CELL IN " + var1.name + " ORD: " + cell.ordinal.to_s + "HAS BLANK OFFSET, EXITING"
      exit
    end
  end

  for cell in var2.cells
    if cell.offset == 0
      puts "ERROR: CELL IN " + var2.name + " ORD: " + cell.ordinal.to_s + "HAS BLANK OFFSET, EXITING"
      exit
    end
  end

  # TODO Handle special cases where one or both of columns have no cells

  # TODO Handle special case where column has a cell with negative time

  # Get the earliest time between the two cols
  time1_on = 9999999999
  time2_on = 9999999999

  time1_off = 0
  time2_off = 0
  if var1.cells.length > 0
    time1_on = var1.cells[0].onset
    time1_off = var1.cells[var1.cells.length-1].offset
  end
  if var2.cells.length > 0
    time2_on = var2.cells[0].onset
    time2_off = var2.cells[var2.cells.length-1].offset
  end
  start_time = [time1_on, time2_on].min

  # And the end time
  end_time = [time1_off, time2_off].max


  # Create the new variable
  if var1_argprefix == nil
    var1_argprefix = var1.name.gsub(/(\W)+/, "").downcase + "___"
    var1_argprefix.gsub(".", "")
  end
  if var2_argprefix == nil
    var2_argprefix = var2.name.gsub(/(\W)+/, "").downcase + "___"
    var2_argprefix.gsub(".", "")
  end

  v1arglist = var1.arglist.map { |arg| var1_argprefix + arg }
  v2arglist = var2.arglist.map { |arg| var2_argprefix + arg }

  # puts "NEW ARGUMENT NAMES:", v1arglist, v2arglist
  args = Array.new
  args << (var1_argprefix + "ordinal")
  args += v1arglist

  args << (var2_argprefix + "ordinal")
  args += v2arglist

  # puts "Creating mutex var", var1.arglist
  mutex = createVariable(name, args)
  # puts "Mutex var created"

  # And finally begin creating new cells
  v1cell = nil
  v2cell = nil
  next_v1cell_ind = nil
  next_v2cell_ind = nil

  time = start_time
  # puts "Start time", start_time
  # puts "End time", end_time

  flag = false

  count = 0

  #######################
  # BEGIN NEW MUTEX
  # Idea here: gather all of the time changes.
  # For each time change get the corresponding cells involved in that change.
  # Create the necessary cell at each time change.
  #######################

  time_changes = Set.new
  v1_cells_at_time = Hash.new
  v2_cells_at_time = Hash.new


  # Preprocess relevant cells and times
  for cell in var1.cells + var2.cells
    time_changes.add(cell.onset)
    time_changes.add(cell.offset)
  end


  time_changes = time_changes.to_a.sort
  if $debug
    p time_changes
  end
  # p time_changes


  mutex_cell = nil
  mutex_cell_parent = nil

  # TODO: make these handle empty cols
  v1cell = var1.cells[0]
  prev_v1cell = nil
  prev_v2cell = nil
  v2cell = var2.cells[0]
  v1idx = 0
  v2idx = 0

  #
  for i in 0..time_changes.length-2
    t0 = time_changes[i]
    t1 = time_changes[i+1]

    # Find the cells that are active during these times
    for j in v1idx..var1.cells.length-1
      c = var1.cells[j]
      v1cell = nil
      if $debug
        p "---", "T1", t0, t1, c.onset, c.offset, "---"
      end
      if c.onset <= t0 and c.offset >= t1 and (t1-t0 > 1 or (c.onset==t0 and c.offset==t1))
        v1cell = c
        v1idx = j
        # p t0, t1, "Found V1"
        break
        # elsif c.onset > t1
        #   break
      else
        v1cell = nil
      end
    end

    for j in v2idx..var2.cells.length-1
      c = var2.cells[j]
      v2cell = nil
      # p "---", "T2", t0, t1, c.onset, c.offset, "---"
      if c.onset <= t0 and c.offset >= t1 and (t1-t0 > 1 or (c.onset==t0 and c.offset==t1))
        v2cell = c
        v2idx = j
        # p t0, t1, "Found V2"
        break
        # elsif c.onset > t1
        #   break
      else
        v2cell = nil
      end
    end

    if v1cell != nil or v2cell != nil
      mutex_cell = mutex.create_cell

      mutex_cell.onset = t0
      mutex_cell.offset = t1
      fillMutexCell(v1cell, v2cell, mutex_cell, mutex, var1_argprefix, var2_argprefix)
    end

  end


  # Now that we have all of the necessary temporal information
  # go through each time in the list and create a cell

  for arg in mutex.arglist
    mutex.change_code_name(arg, arg.gsub("___", "_"))
  end
  for i in 0..mutex.cells.length-1
    c = mutex.cells[i]
    c.ordinal = i+1
  end
  puts "Created a column with #{mutex.cells.length} cells."

  return mutex
end

#delete_cell(cell) ⇒ nil Also known as: deleteCell

Delete a cell from the spreadsheet

Parameters:

  • cell (RCell)

    Cell to delete

Returns:

  • (nil)


2635
2636
2637
# File 'datavyu_api.rb', line 2635

def delete_cell(cell)
  cell.db_cell.getVariable.removeCell(cell.db_cell)
end

#delete_column(colname) ⇒ Object Also known as: deleteColumn, delete_variable, deleteVariable

Note:

This change is immediately reflected in the spreadsheet and is irreversible.

Deletes a column from the spreadsheet.

Parameters:

  • colname (RColumn, String)

    column to delete

Returns:

  • nil



1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
# File 'datavyu_api.rb', line 1651

def delete_column(colname)
  if colname.class != "".class
    colname = colname.name
  end
  col = $db.getVariable(colname)
  if (col == nil)
    printNoColumnFoundWarning(colname.to_s)
  end
  $db.removeVariable(col)
end

#get_cell_from_time(col, time) ⇒ RCell Also known as: getCellFromTime

Finds the first cell in the specified column that overlaps the given time.

Parameters:

  • col (RColumn)

    column to find cell from

  • time (Integer)

    time in milliseconds

Returns:

  • (RCell)

    Cell that spans the given time; nil if none found.



2605
2606
2607
2608
2609
2610
2611
2612
# File 'datavyu_api.rb', line 2605

def get_cell_from_time(col, time)
  for cell in col.cells
    if cell.onset <= time and cell.offset >= time
      return cell
    end
  end
  return nil
end

#get_column(name) ⇒ RColumn Also known as: getVariable, getColumn

Note:

Prints warning message to console if column name is not found in spreadsheet.

Construct a Ruby representation of the Datavyu column, if it exists.

Examples:

trial = get_column("trial")

Parameters:

  • name (String)

    the name of the column in the spreadsheet

Returns:

  • (RColumn)

    Ruby object representation of the variable inside Datavyu or nil if the named column does not exist



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'datavyu_api.rb', line 730

def get_column(name)

  var = $db.getVariable(name)
  if (var == nil)
    printNoColumnFoundWarning(name.to_s)
    return nil
  end

  # Convert each cell into an array and store in an array of arrays
  cells = var.getCells()
  arg_names = Array.new

  # Now get the arguments for each of the cells

  # For matrix vars only
  type = var.getRootNode.type
  if type == Argument::Type::MATRIX
    # Matrix var
    arg_names = Array.new
    for arg in var.getRootNode.childArguments
      arg_names << arg.name
    end
  else
    # Nominal or text
    arg_names = ["var"]
  end

  v = RColumn.new
  v.name = name
  v.old_args = arg_names
  v.type = type
  v.set_cells(cells, arg_names)
  v.sort_cells
  v.dirty = false
  v.db_var = var

  return v
end

#get_column_listArray Also known as: getColumnList, getVariableList

Return a list of columns from the current spreadsheet.

Returns:

  • (Array)


2531
2532
2533
2534
2535
2536
2537
2538
2539
# File 'datavyu_api.rb', line 2531

def get_column_list()
  name_list = Array.new
  vars = $db.getAllVariables()
  for v in vars
    name_list << v.name
  end

  return name_list
end

#get_datavyu_files_from(dir, recurse = false) ⇒ Array<String>

Note:

When recurse is set true, names of Datavyu files in nested folders will be the relative path from the starting directory; e.g. 'folder1/folder2/my_datavyu_file.opf'

Return list of *.opf files from given directory.

Examples:

input_files = get_datavyu_files_from('~/Desktop/input', true)

Parameters:

  • dir (String)

    directory to check

  • recurse (true, false) (defaults to: false)

    true to check subfolders, false to only check given folder

Returns:

  • (Array<String>)

    list containing names of .opf files.



2716
2717
2718
2719
2720
2721
# File 'datavyu_api.rb', line 2716

def get_datavyu_files_from(dir, recurse=false)
  dir = File.expand_path(dir)
  pat = recurse ? '**/*.opf' : '*.opf'
  files = Dir.chdir(dir){ Dir.glob(pat) }
  return files
end

#get_datavyu_versionString Also known as: getDatavyuVersion

Return Datavyu version string.

Returns:

  • (String)

    Version string in the fromat “v.:#.#”



2662
2663
2664
# File 'datavyu_api.rb', line 2662

def get_datavyu_version
  return org.datavyu.util.DatavyuVersion.getLocalVersion.getVersion
end

#get_osString Also known as: getOS

Return the OS version

Examples:

filepath = (getOS() == 'windows')? 'C:\data' : '~/data'

Returns:

  • (String)

    'windows', 'mac', or 'linux'



2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
# File 'datavyu_api.rb', line 2644

def get_os
	host_os = RbConfig::CONFIG['host_os']
	case host_os
	when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
		os = 'windows'
	when /darwin|mac os/
		os = 'mac'
	when /linux|solaris|bsd/
		os = 'linux'
	else
		raise "Unknown OS: #{host_os.inspect}"
	end
	return os
end

#hide_columns(*names) ⇒ Object

Hide the given columns in the spreadsheet

Parameters:

  • names (Array<String>)

    names of columns to hide



2725
2726
2727
2728
2729
# File 'datavyu_api.rb', line 2725

def hide_columns(*names)
  names.flatten!
  valid_names = names & get_column_list
  valid_names.each{ |x| $db.getVariable(x).setHidden(true)}
end

#load_db(filename) ⇒ Array Also known as: loadDB

Note:

DOES NOT ALTER THE GUI.

Note:

Use #File.expand_path and related methods to convert from relative to absolute path.

Loads a new database from a file.

Examples:

$db,$pj = load_db("/Users/username/Desktop/test.opf")

Parameters:

  • filename

    The FULL PATH to the saved Datavyu file.

Returns:

  • (Array)

    An array containing two items: dataStore, the spreadsheet data, and pj the project data. Set dataStore and pj to $db and $pj, respectively (see example)



1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
# File 'datavyu_api.rb', line 1563

def load_db(filename)
  # Raise file not found error unless file exists
  unless File.exist?(filename)
    raise "File does not exist. Please make sure to put the full path to the file."
  end

  infile = java.io.File.new(filename)

  print_debug "Opening Project: #{infile}"

  # Create the controller that holds all the logic for opening projects and
  # databases.
  open_c = OpenController.new

  # Opens a project and associated database (i.e. either compressed or
  # uncompressed .shapa files). If you want to just open a standalone database
  # (i.e .odb or .csv file) call open_c.open_database("filename") instead. These
  # methods do *NOT* open the project within the Datavyu UI.
  db = nil
  proj = nil
  if filename.include?(".csv")
    db = open_c.open_datastore(filename)
  else
    db = open_c.open_project(infile)
    # Get the project that was opened (if you want).
    proj = open_c.get_project
  end

  # Get the database that was opened.
  db = open_c.get_data_store

  # If the open went well - query the database, do calculations or whatever
  unless db.nil?
    # This just prints the number of columns in the database.
    print_debug "SUCCESSFULLY Opened a project with #{db.get_all_variables.length.to_s} columns!"
  else
    raise "Unable to open file '#{filename}'"
  end

  print_debug "#{filename} has been loaded."

  return db, proj
end

#load_macshapa_db(filename, write_to_gui, *ignore_vars) ⇒ Array Also known as: loadMacshapaDB

TODO:

fix linter warnings

Opens an old, closed database format MacSHAPA file and loads it into the current open database. NOTE This will only read in matrix and string variables. Predicates are not yet supported. Queries will not be read in. Times are translated to milliseconds for compatibility with Datavyu.

Examples:

$db,$pj = load_db("/Users/username/Desktop/test.opf")

Parameters:

  • filename (String)

    The FULL PATH to the saved MacSHAPA file.

  • write_to_gui (true, false)

    Whether the MacSHAPA file should be read into the database currently open in the GUI or whether it should just be read into the Ruby interface. After this script is run $db and $pj are now the MacSHAPA file.

Returns:

  • (Array)

    An array containing two items: the spreadsheet data and the project information. Set to $db and $pj, respectively (see example).



1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
# File 'datavyu_api.rb', line 1679

def load_macshapa_db(filename, write_to_gui, *ignore_vars)

  # Create a new DB for us to use so we don't touch the GUI... some of these
  # files can be huge.
  # Since I don't know how to make a whole new project, lets just load a blank file.
  # TODO why is this section commented out??
  if not write_to_gui
    #$db,$pj = load_db("/Users/j4lingeman/Desktop/blank.opf")
    # $db = Datastore.new
    # $pj = Project.new()
  end


  puts "Opening file"
  f = File.open(filename, 'r')

  puts "Opened file"
  # Read and split file by lines.  '\r' is used because that is the default
  # format for OS9 files.
  lines = ""
  while (line = f.gets)
    lines += line
  end
  lines = lines.split(/[\r\n]/)

  # Find the variable names in the file and use these to create and set up
  # our columns.
  predIndex = lines.index("***Predicates***")
  varIndex = lines.index("***Variables***")
  spreadIndex = lines.index("***SpreadPane***")
  predIndex += 2

  variables = Hash.new
  varIdent = Array.new

  while predIndex < varIndex
    l = lines[predIndex].split(/ /)[5]
    varname = l[0..l.index("(") - 1]
    if varname != "###QueryVar###" and varname != "div" and varname != "qnotes" \
			and not ignore_vars.include?(varname)
      print_debug varname

      # Replace non-alphabet with underscores
      vname2 = varname.gsub(/\W+/, '_')
      if vname2 != varname
        puts "Replacing #{varname} with #{vname2}"
        varname = vname2
      end

      variables[varname] = l[l.index("(")+1..l.length-2].split(/,/)
      varIdent << l
    end
    predIndex += 1
  end

  puts "Got predicate index"

  # Create the columns for the variables
  variables.each do |key, value|
    # Create column
    if getColumnList().include?(key)
      deleteVariable(key)
    end


    args = Array.new
    value.each { |v|
      # Strip out the ordinal, onset, and offset.  These will be handled on a
      # cell by cell basis.
      if v != "<ord>" and v != "<onset>" and v != "<offset>"
        v1 = v.gsub(/\<|\>/, '').gsub('#', 'number').gsub('&', 'and')
        v2 = RColumn.sanitize_codename(v1)
        puts "Changing code #{v1} in column #{key} to: #{v2}" if(v2 != v1)
        # args << v.sub("<", "").sub(">", "")
        args << v2
      end
    }

    setVariable(createVariable(key, args))
  end

  # Search for where in the file the var's cells are, create them, then move
  # on to the next variable.
  varSection = lines[varIndex..spreadIndex]

  varIdent.each do |id|

    # Search the variable section for the above id
    varSection.each do |l|
      line = l.split(/[\t\s]/) # @todo linter error : dup char class
      if line[2] == id

        print_debug id
        varname = id.slice(0, id.index("(")).gsub(/\W+/,'_')
        if get_column_list.include?(varname)
          col = get_column(varname)
        else
          puts "Column #{varname} not found. Skipping."
          next
        end

        #print_debug varname
        start = varSection.index(l) + 1

        stringCol = false

        if varSection[start - 2].index("strID") != nil
          stringCol = true
        end

        #Found it!  Now build the cells
        while varSection[start] != "0"

          if stringCol == false
            cellData = varSection[start].split(/[\t]/)
            cellData[cellData.length - 1] = cellData[cellData.length-1][cellData[cellData.length-1].index("(")..cellData[cellData.length-1].length]
          else
            cellData = varSection[start].split(/[\t]/)
          end

          # Init cell to null

          cell = col.create_cell

          # Convert onset/offset from 60 ticks/sec to milliseconds
          onset = cellData[0].to_i / 60.0 * 1000
          offset = cellData[1].to_i / 60.0 * 1000

          # Set onset/offset of cell
          cell.onset = onset.round
          cell.offset = offset.round

          # Split up cell data
          data = cellData[cellData.length - 1]
          print_debug data
          if stringCol == false
            data = data[1..data.length-2]
            data = data.gsub(/[() ]*/, "")
            data = data.split(/,/)
          elsif data != nil #Then this is a string var
            data = data.strip()
            if data.split(" ").length > 1
              data = data[data.index(" ")..data.length] # Remove the char count
              data = data.gsub("/", " or ")
              data = data.gsub(/[^\w ]*/, "")
              data = data.gsub(/  /, " ")
            else
              data = ""
            end
          else
            data = Array.new
            data << nil
          end
          # Cycle thru cell data arguments and fill them into the cell matrix
          if data.is_a?(String)
            argname = cell.arglist.last
            cell.change_code(argname, data)
          elsif data.is_a?(Array)
            data.each_with_index do |d, i|
              print_debug cell.arglist[1]
              argname = cell.arglist[i]
              if d == nil
                cell.change_code(argname, "")
              elsif d == "" or d.index("<") != nil
                cell.change_code(argname, "")
              else
                cell.change_code(argname, d)
              end
            end
          end
          start += 1
        end
        setVariable(col)
      end
    end
  end

  f.close()

  return $db, $pj
end

#load_macshapa_db2(filename, write_to_gui, *ignore_vars) ⇒ Object

Updated function. Keeping original as is in case edits break previously working imports.



1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
# File 'datavyu_api.rb', line 1863

def load_macshapa_db2(filename, write_to_gui, *ignore_vars)
  # Create a new DB for us to use so we don't touch the GUI... some of these
  # files can be huge.
  # Since I don't know how to make a whole new project, lets just load a blank file.
  if not write_to_gui
    #$db,$pj = load_db("/Users/j4lingeman/Desktop/blank.opf")
    # $db = Datastore.new
    # $pj = Project.new()
  end


  puts "Opening file"
  f = File.open(filename, 'r')

  puts "Opened file"
  # Read and split file by lines.  '\r' is used because that is the default
  # format for OS9 files.
  lines = ""
  while (line = f.gets)
    lines += line
  end
  lines = lines.split(/[\r\n]/)

  # Find the variable names in the file and use these to create and set up
  # our columns.
  predIndex = lines.index("***Predicates***")
  varIndex = lines.index("***Variables***")
  spreadIndex = lines.index("***SpreadPane***")
  predIndex += 2

  variables = Hash.new
  varIdent = Array.new

  while predIndex < varIndex
    line = lines[predIndex].strip
    parser = /(?<d1>\d+)\s*(?<d2>\d+)\s*(?<d3>\d+)\s*(?<vardata>(?<varname>.*)\((?<codes>.*)\))/
    matches = parser.match(line)
    # l = lines[predIndex].split(/ /)[5]
    # varname = l[0..l.index("(") - 1]
    varname = matches['varname']
    if varname != "###QueryVar###" and varname != "div" and varname != "qnotes" \
			and not ignore_vars.include?(varname)
      print_debug varname

      # Replace non-alphabet with underscores
      vname2 = varname.gsub(/\W+/, '_')
      if vname2 != varname
        puts "Replacing #{varname} with #{vname2}"
        varname = vname2
      end

      variables[varname] = matches['codes'].split(/,/)
      varIdent << matches['vardata']
    end
    predIndex += 1
  end

  puts "Got predicate index"

  # Create the columns for the variables
  variables.each do |key, value|
    puts "Creating column: #{key}"
    # Create column
    args = Array.new
    value.each { |v|
      # Strip out the ordinal, onset, and offset.  These will be handled on a
      # cell by cell basis.
      if v != "<ord>" and v != "<onset>" and v != "<offset>"
        args << v.sub("<", "").sub(">", "")
      end
    }

    set_column(new_column(key, args))
  end

  # Search for where in the file the var's cells are, create them, then move
  # on to the next variable.
  varSection = lines[varIndex..spreadIndex]

  varIdent.each do |id|

    # Search the variable section for the above id
    varSection.each do |l|
      line = l.split(/[\t\s]/)
      if line[2] == id

        print_debug id
        colname = id.slice(0, id.index("(")).gsub(/\W+/,'_')
        col = get_column(colname)
        #print_debug varname
        start = varSection.index(l) + 1

        stringCol = false

        if varSection[start - 2].index("strID") != nil
          stringCol = true
        end

        #Found it!  Now build the cells
        while varSection[start] != "0"

          if stringCol == false
            cellData = varSection[start].split(/[\t]/)
            cellData[cellData.length - 1] = cellData[cellData.length-1][cellData[cellData.length-1].index("(")..cellData[cellData.length-1].length]
          else
            cellData = varSection[start].split(/[\t]/)
          end

          # Init cell to null

          cell = col.create_cell

          # Convert onset/offset from 60 ticks/sec to milliseconds
          onset = cellData[0].to_i / 60.0 * 1000
          offset = cellData[1].to_i / 60.0 * 1000

          # Set onset/offset of cell
          cell.onset = onset.round
          cell.offset = offset.round

          # Split up cell data
          data = cellData[cellData.length - 1]
          print_debug data
          if stringCol == false
            data = data[1..data.length-2]
            data = data.gsub(/[() ]*/, "")
            data = data.split(/,/)
          elsif data != nil #Then this is a string var
            data = data.strip()
            if data.split(" ").length > 1
              data = data[data.index(" ")..data.length] # Remove the char count
              data = data.gsub("/", " or ")
              data = data.gsub(/[^\w ]*/, "")
              data = data.gsub(/  /, " ")
            else
              data = ""
            end
          else
            data = Array.new
            data << nil
          end
          # Cycle thru cell data arguments and fill them into the cell matrix
          if data.is_a?(String)
            argname = cell.arglist.last
            cell.change_code(argname, data)
          elsif data.is_a?(Array)
            data.each_with_index do |d, i|
              print_debug cell.arglist[1]
              argname = cell.arglist[i]
              if d == nil
                cell.change_code(argname, "")
              elsif d == "" or d.index("<") != nil
                cell.change_code(argname, "")
              else
                cell.change_code(argname, d)
              end
            end
          end
          start += 1
        end
        setVariable(col)
      end
    end
  end

  f.close()

  return $db, $pj
end

#make_duration_block_rel(relname, var_to_copy, binding_column, block_dur, skip_blocks) ⇒ Object Also known as: makeDurationBlockRel

Deprecated.
Note:

Column is written to spreadsheet.

Makes a duration based reliability column based on John's method. It will create two new columns, one that contains a cell with a number for that block, and another blank column for the free coding within that block.

Parameters:

  • relname (String)

    name of the rel column to be made

  • var_to_copy (String)

    name of column being copied

  • binding_column (String)

    name of column to bind copy to

  • block_dur (Integer)

    duration, in seconds, for each block

  • skip_blocks (Integer)

    multiple of block_dur to skip between coding blocks

Returns:

  • nil



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'datavyu_api.rb', line 1066

def make_duration_block_rel(relname, var_to_copy, binding_column, block_dur, skip_blocks)
  block_var = new_column(relname + "_blocks", "block_num")
  rel_var = make_rel(relname, var_to_copy, 0)

  var_to_copy = get_column(var_to_copy)
  binding_col = get_column(binding_column)


  block_dur = block_dur * 1000 # Convert to milliseconds
  block_num = 1
  for bindcell in binding_col.cells
    cell_dur = bindcell.offset - bindcell.onset
    if cell_dur <= block_dur
      cell = block_var.new_cell()
      cell.change_code("block_num", block_num.to_s)
      cell.onset = bindcell.onset
      cell.offset = bindcell.offset
      block_num += 1
    else
      num_possible_blocks = cell_dur / block_dur #Integer division
      if num_possible_blocks > 0
        for i in 0..num_possible_blocks
          if i % skip_blocks == 0
            cell = block_var.new_cell()
            cell.change_code("block_num", block_num.to_s)
            cell.onset = bindcell.onset + i * block_dur
            if bindcell.onset + (i + 1) * block_dur <= bindcell.offset
              cell.offset =  bindcell.onset + (i + 1) * block_dur
            else
              cell.offset = bindcell.offset
            end
            block_num += 1
          end
        end
      end
    end
  end
  set_column(relname + "_blocks", block_var)
end

#make_reliability(relname, var_to_copy, multiple_to_keep, *args_to_keep) ⇒ RColumn Also known as: makeReliability, make_rel

Create a reliability column that is a copy of another column in the database, copying every nth cell and carrying over some of the arguments from the original, if wanted.

Examples:

rel_trial = make_rel("rel.trial", "trial", 2, "onset", "trialnum", "unit")

Parameters:

  • relname (String, RColumn)

    the name of the reliability column to be created

  • var_to_copy (String)

    the name of the variable in the database you wish to copy

  • multiple_to_keep (Integer)

    the number of cells to skip. For every other cell, use 2

  • args_to_keep (Array<String>)

    : names of codes to keep from original column

Returns:

  • (RColumn)

    Ruby object representation of the rel column



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
# File 'datavyu_api.rb', line 956

def make_reliability(relname, var_to_copy, multiple_to_keep, *args_to_keep)
  # Get the primary variable from the DB

  if var_to_copy.class == String
    var_to_copy = get_column(var_to_copy)
  else
    var_to_copy = get_column(var_to_copy.name)
  end

  if args_to_keep[0].class == Array
    args_to_keep = args_to_keep[0]
  end

  # Clip down cells to fit multiple to keep
  for i in 0..var_to_copy.cells.length-1
    if multiple_to_keep == 0
      var_to_copy.cells[i] = nil
    elsif var_to_copy.cells[i].ordinal % multiple_to_keep != 0
      var_to_copy.cells[i] = nil
    else
      var_to_copy.cells[i].ordinal = var_to_copy.cells[i].ordinal / multiple_to_keep
    end
  end
  # Clear out the nil cells
  var_to_copy.cells.compact!

  var_to_copy.cells.each do |cell|
    if !args_to_keep.include?("onset")
      cell.onset = 0
    end
    if !args_to_keep.include?("offset")
      cell.offset = 0
    end
    cell.arglist.each do |arg|
      if !args_to_keep.include?(arg)
        cell.change_code(arg, "")
      end
    end
  end
  setVariable(relname, var_to_copy)
  return var_to_copy
end

#merge_columns(name, *cols) ⇒ RColumn

Combine cells of different columns into a new column. Intended to mimic functionality of create_mutually_exclusive() and combine_columns but not guaranteed to produce equivalent results.

Parameters:

  • name (String)

    name of result column

Returns:



1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
# File 'datavyu_api.rb', line 1486

def merge_columns(name, *cols)
  # Handle degenerate cases
  return nil if cols.nil? || cols.size == 0

  # Ensure cols contains RColumns
  cols.map! do |x|
    case x
    when String
      get_column(x)
    when RColumn
      x
    else
      raise "Unhandled column value or class: #{x}, #{x.class}."
    end
  end

  # Do nothing if only one column given.
  return cols.first if cols.size == 1

	# Concatenate arglists and cells.
  # Codes have their column name and an underscore prepended to them.
	my_args = []
	all_cells = []
	cols.each{
		|x|
		my_args << x.name.downcase+"_ordinal"
		my_args << x.arglist.map{ |y| x.name.downcase+"_"+y}
		all_cells << x.cells
	}
	my_args.flatten!
	all_cells.flatten!

	ncol = new_column(name, *my_args)

  # Convert point cells to have offsets = onset + 1
  all_cells.each{ |x| x.offset = x.onset + 1 if x.onset == x.offset }

	# Gather onsets and offsets and collect unique times into a single array
	onsets = all_cells.map(&:onset)
	offsets = all_cells.map(&:offset)
	times = (onsets+offsets).uniq.sort

	# For each consecutive time in times, create a new cell over that interval.
	if times.size>0
		onset = times.first
		for offset in times[1..-1]
			ncell = ncol.make_new_cell()
			ncell.onset = onset
			ncell.offset = offset

      # Find an enclosing cell (if any) from each of the columns for this time region
			ocells = cols.map do |col|
				col.cells.find{ |y| y.contains(ncell) }
			end

			for c in ocells
				unless c.nil?
					ncell.change_code(c.parent.downcase+"_ordinal", c.ordinal)
					c.arglist.each do |a|
						ncell.change_code(c.parent.downcase+"_"+a, c.get_code(a))
					end
				end
			end
			onset = offset
		end
	end

	return ncol
end

#new_column(name, *args) ⇒ RColumn Also known as: create_new_column, createNewColumn, createVariable, createNewVariable, create_column, createColumn

Note:

Column does not exist in Datavyu spreadsheet unless saved with #set_column.

Note:

Code names should be all lower-case and contain no special characters other than underscores.

Create blank column.

Examples:

trial = new_column("trial", "trialnum", "unit")
blank_cell = trial.new_cell()
set_column(trial)

Parameters:

  • name (String)

    name of the column

  • args (Array<String>)

    list of codes to add to column; must specify at least one code name

Returns:



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'datavyu_api.rb', line 1011

def new_column(name, *args)
  print_debug "Creating new variable"

  # Use default code when no codes are specified.
  args = ['code01'] if args.empty?

  v = RColumn.new

  v.name = name

  v.dirty = true

  print_debug args[0].class
  print_debug args
  if args[0].class == Array
    args = args[0]
  end
  print_debug args

  # Set the argument names in arg_names and set the database internal style with <argname> in old_args
  arg_names = Array.new
  old_args = Array.new
  for arg in args
    print_debug arg
    arg_names << arg
    old_args << arg.to_s
  end
  c = Array.new
  v.old_args = old_args
  v.set_cells(nil, arg_names)

  # Return reference to this variable for the user
  print_debug "Finished creating variable"
  return v
end

#new_video(filepath, plugin, onset = 0) ⇒ Object



2829
2830
2831
# File 'datavyu_api.rb', line 2829

def new_video(filepath, plugin, onset = 0)
  RVideoController.new_video(filepath, plugin, onset)
end

Returns ordinal, onset, offset, and the values of all codes from the given cell. TODO change method name to something more appropriate

Parameters:

  • cell (RCell)

    cell whose codes to print

Returns:

  • (Array<String>)

    array of values for all codes in cell



2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
# File 'datavyu_api.rb', line 2619

def print_cell_codes(cell)
  s = Array.new
  s << cell.ordinal.to_s
  s << cell.onset.to_s
  s << cell.offset.to_s
  for arg in cell.arglist
    s << cell.get_arg(arg)
  end
  return s
end

Outputs the values of all codes specified from the given cell to the given output file. Row is delimited by tabs.

Parameters:

  • cell (RCell)

    cell to print codes from

  • file (File)

    output file

Returns:

  • nil



2592
2593
2594
2595
2596
2597
2598
# File 'datavyu_api.rb', line 2592

def print_codes(cell, file, args)
  for a in args
    #puts "Printing: " + a
    val = eval "cell.#{a}"
    file.write(val.to_s + "\t")
  end
end

Prints the specified message if global variable #debug set true.

Parameters:

  • s

    item to print to console

Returns:

  • nil



52
53
54
# File 'datavyu_api.rb', line 52

def print_debug(*str)
  p str if $debug
end

Define interal function for printing errors



2219
2220
2221
2222
2223
2224
2225
2226
2227
# File 'datavyu_api.rb', line 2219

def print_err(m_cell, r_cell, arg, dump_file, main_col, rel_col)
  main_val = eval "m_cell.#{arg}"
  rel_val = eval "r_cell.#{arg}"
  err_str = "ERROR in " + main_col.name + " at Ordinal " + m_cell.ordinal.to_s + ", rel ordinal " + r_cell.ordinal.to_s + " in argument " + arg + ": " + main_val.to_s + ", " + rel_val.to_s + "\n"
  if dump_file != nil
    dump_file.write(err_str)
  end
  print err_str
end

Let the user know that a given column was not found. Error is confusing, this should clarify.



1666
1667
1668
# File 'datavyu_api.rb', line 1666

def print_no_column_found_warning(colName)
  puts "WARNING: No column with name '" + colName + "' was found!"
end

#save_db(filename) ⇒ Object Also known as: saveDB

Note:

Use #File.expand_path and related methods to convert from relative to absolute path.

Saves the current $db and $pj variables to filename. If filename ends with .csv, it saves a .csv file. Otherwise it saves it as a .opf.

Examples:

save_db("/Users/username/Desktop/test.opf")

Parameters:

  • filename (String)

    The FULL PATH to where the Datavyu file should be saved.

Returns:

  • nil



1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
# File 'datavyu_api.rb', line 1616

def save_db(filename)
  print_debug "Saving Database: " + filename

  filename = File.expand_path(filename)

  # Create the controller that holds all the logic for opening projects and
  # databases.
  save_c = SaveController.new

  #
  # Saves a database (i.e. a .odb or .csv file). If you want to save a project
  # call save_project("project file", project, database) instead.
  # These methods do *NOT* alter the Datavyu UI.
  #
  if filename.include?('.csv')
    save_c.save_database(filename, $db)
  else
    if $pj == nil or $pj.getDatabaseFileName == nil
        $pj = Project.new()
        $pj.setDatabaseFileName("dataStore")
        dbname = filename[filename.rindex("/")+1..filename.length]
        $pj.setProjectName(dbname)
    end
    save_file = java.io.File.new(filename)
    save_c.save_project(save_file, $pj, $db)
  end

  print_debug "Save successful."
end

#set_column(name, column, sanitize_codes) ⇒ Object #set_column(column, sanitize_codes) ⇒ Object Also known as: setVariable, setColumn

Note:

This function will overwrite existing spreadsheet columns with the same name as specified column / name.

Translate a Ruby column object into a Datavyu column and saves it to the spreadsheet. If two parameters are specified, the first parameter is the name under which the column will be saved.

Examples:

trial = get_column("trial")
  ... Do some modification to trial ...
set_column(trial)

Overloads:

  • #set_column(name, column, sanitize_codes) ⇒ Object

    Saves column to spreadsheet with given name

    Parameters:

    • name (String)

      Name to save column as

    • column (RColumn)

      Column object to save

    • sanitize_codes (Boolean)

      When true, uses sanitized names for codes

Parameters:

  • args (String, RColumn)

    the name and RColumn object to save; the name parameter may be omitted

Returns:

  • nil



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'datavyu_api.rb', line 787

def set_column(*args, sanitize_codes: true)

  if args.length == 1
    var = args[0]
    name = var.name
  elsif args.length == 2
    var = args[1]
    name = args[0]
  end

  # If substantial changes have been made to the structure of the column,
  # just delete the whole thing first.
  # If the column was dirty, redo the vocab too
  if var.db_var == nil or var.db_var.get_name != name

    if getColumnList().include?(name)
      deleteVariable(name)
    end
    # Create a new variable
    v = $db.createVariable(name, Argument::Type::MATRIX)
    var.db_var = v

    if var.arglist.length > 0
      var.db_var.removeArgument("code01")
    end

    # Set variable's vocab
    var.arglist.zip(var.old_args).each do |arg, old_arg|
      new_arg = v.addArgument(Argument::Type::NOMINAL)
      an = sanitize_codes ? arg : old_arg
      new_arg.name = an
      main_arg = var.db_var.getRootNode()
      child_args = main_arg.childArguments

      child_args.get(child_args.length-1).name = arg

      var.db_var.setRootNode(main_arg)
    end
    var.db_var = v
  end

  #p var
  if var.dirty
    # deleteVariable(name)
    # If the variable is dirty, then we have to do something to the vocab.
    # Compare the variable's vocab and the Ruby cell version to see
    # what is different.

    #p var.db_var
    if var.db_var.getRootNode.type == Argument::Type::MATRIX
      values = var.db_var.getRootNode.childArguments
      #p values
      for arg in var.old_args
        #p var.old_args
        flag = false
        for dbarg in values
          if arg == dbarg.name
            flag = true
            break
          end
        end
        # If we didn't find it in dbarg, we have to create it
        if flag == false
          # Add the argument
          new_arg = var.db_var.addArgument(Argument::Type::NOMINAL)

          # Make sure argument doesn't have < or > in it.
          arg = arg.delete("<").delete(">")
          # Change the argument's name by getting the variable back,
          # and then setting it. This hoop jumping is annoying.
          new_arg.name = arg
          main_arg = var.db_var.getRootNode()
          child_args = main_arg.childArguments

          child_args.get(child_args.length-1).name = arg

          var.db_var.setVariableType(main_arg)
        end
      end

      # Now see if we have deleted any arguments
      deleted_args = values.map { |x| x.name } - var.old_args
      deleted_args.each do |arg|
        puts "DELETING ARG: #{arg}"
        var.db_var.removeArgument(arg)
      end
    end


  end

  # Create new cells and fill them in for each cell in the variable
  for cell in var.cells
    # Copy the information from the ruby variable to the new cell

    if cell.db_cell == nil or cell.parent != name
      cell.db_cell = var.db_var.createCell()
    end

    value = cell.db_cell.getCellValue()

    if cell.onset != cell.db_cell.getOnset
      cell.db_cell.setOnset(cell.onset)
    end

    if cell.offset != cell.db_cell.getOffset
      cell.db_cell.setOffset(cell.offset)
    end

    # Matrix cell
    if cell.db_cell.getVariable.getRootNode.type == Argument::Type::MATRIX
      values = cell.db_cell.getCellValue().getArguments()
      for arg in var.old_args
        # Find the arg in the dataStore's arglist that we are looking for
        for i in 0...values.size
          dbarg = values[i]
          dbarg_name = dbarg.getArgument.name
          if dbarg_name == arg and not ["", nil].include?(cell.get_arg(var.convert_argname(arg)))
            dbarg.set(cell.get_arg(var.convert_argname(arg)))
            break
          end
        end
      end

      # Non-matrix cell
    else
      value = cell.db_cell.getCellValue()
      value.set(cell.get_arg("var"))
    end

    # Save the changes back to the DB

  end
  # if var.hidden
  var.db_var.setHidden(var.hidden)
  # end
end

#set_column!(*args, sanitize_codes: true) ⇒ Object Also known as: setVariable!

Deletes a variable from the spreadsheet and rebuilds it from the given RColumn object. Behaves similar to setVariable(), but this will ALWAYS delete and rebuild the spreadsheet colum and its vocab.



932
933
934
935
936
937
938
939
940
941
942
943
# File 'datavyu_api.rb', line 932

def set_column!(*args, sanitize_codes: true)
  if args.length == 1
    var = args[0]
    name = var.name
  elsif args.length == 2
    var = args[1]
    name = args[0]
  end

  var.db_var = nil
  set_column(name, var, sanitize_codes: sanitize_codes)
end

#set_column_order(*column_list) ⇒ Object

Move a column to the desired destination. Pass in list, show only those columns, in that order List can be of vars or strings

Parameters:

  • column_list (Array(String))

    The list of columns that we want to show, in the order we want them shown.



2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
# File 'datavyu_api.rb', line 2671

def set_column_order(*column_list)
  column_list.flatten!
  return if column_list.empty?

  # Move the given columns to positions 0..n using shuffle_column()
  column_list.reverse.each_with_index do |col, i|
    vars = $sp.get_spreadsheet_panel().get_columns()
    for j in 0...vars.size()
      if vars[j].get_column_name() == col
        $sp.get_spreadsheet_panel().shuffle_column(j, i)
      end
    end
  end

  # Now get all variables, and if they are not in column list, hide them
  vars = $sp.get_spreadsheet_panel().get_columns()
  for v in vars
    if column_list.include?(v.get_column_name())
      show_columns(v.get_column_name())
    else
      hide_columns(v.get_column_name())
    end
  end
end

#show_columns(*names) ⇒ Object

Show the given columns in the spreadsheet

Parameters:

  • names (Array<String>)

    names of columns to show



2733
2734
2735
2736
2737
# File 'datavyu_api.rb', line 2733

def show_columns(*names)
  names.flatten!
  valid_names = names & get_column_list
  valid_names.each{ |x| $db.getVariable(x).setHidden(false) }
end

#smooth_column(colname, tol = 33) ⇒ Object Also known as: smoothColumn

Note:

Only the onset is changed; not the offset.

Makes temporally adjacent cells in a column continuous if the interval between them is below a given threshold.

Parameters:

  • colname (String)

    name of column to smooth

  • tol (Integer) (defaults to: 33)

    milliseconds below which cell onset should be changed to make continuous

Returns:

  • nil



2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
# File 'datavyu_api.rb', line 2573

def smooth_column(colname, tol=33)
  col = get_column(colname)
  for i in 0..col.cells.length-2
    curcell = col.cells[i]
    nextcell = col.cells[i+1]

    if nextcell.onset - curcell.offset < tol
      nextcell.onset = curcell.offset
    end
  end
  setVariable(colname, col)
end

#transfer_columns(db1, db2, remove, *varnames) ⇒ Object Also known as: transfer_column, transferColumns, transferColumn, transferVariables, transferVariable

Transfers columns between databases. If db1 or db2 are set to the empty string “”, then that database is the current database in $db (usually the GUI's database). So if you want to transfer a column into the GUI, set db2 to “”. If you want to tranfer a column from the GUI into a file, set db1 to “”. Setting remove to true will DELETE THE COLUMNS YOU ARE TRANSFERRING FROM DB1. Be careful!

Examples:

# Transfer column "idchange" from test.opf to the currently open spreadsheet in Datavyu. Do not delete "idchange" from test.opf.
transfer_columns("/Users/username/Desktop/test.opf", "", true, "idchange")

Parameters:

  • db1 (String)

    The FULL PATH toa Datavyu file or “” to use the currently opened database. Columns are transferred FROM here.

  • db2 (String)

    : The FULL PATH to the saved Datavyu file or “” to use the currently opened database. Columns are tranferred TO here.

  • remove (true, false)

    Set to true to delete columns in DB1 as they are moved to db2. Set to false to leave them intact.

  • varnames (Array<String>)

    column names (requires at least 1): You can specify as many column names as you like that will be retrieved from db1.

Returns:

  • nil



2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
# File 'datavyu_api.rb', line 2046

def transfer_columns(db1, db2, remove, *varnames)
  # Save the current $db and $pj global variables
  saved_db, saved_proj = $db, $pj

  # If varnames was specified as a hash, flatten it to an array
  varnames.flatten!

  # Display args when debugging
  print_debug("="*20)
  print_debug("#{__method__} called with following args:")
  print_debug(db1, db2, remove, varnames)
  print_debug("="*20)

  # Handle degenerate case of same source and destination
  if db1==db2
    puts "Warning: source and destination are identical.  No changes made."
    return nil
  end

  # Set the source database, loading from file if necessary.
  # Raises file not found error and returns nil if source database does not exist.
  db1path = ""
  begin
    if db1!=""
      db1path = File.expand_path(db1)
      if !File.readable?(db1path)
        raise "Error! File not readable : #{db1}"
      end
      print_debug("Loading source database from file : #{db1path}")
      from_db, from_proj = loadDB(db1path)
    else
      from_db, from_proj = $db, $pj
    end
  rescue StandardError => e
    puts e.message
    puts e.backtrace
    return nil
  end

  # Set the destination database, loading from file if necessary.
  # Raises file not found error and returns nil if destination database does not exist.
  db2path = ""
  begin
    if db2!=""
      db2path = File.expand_path(db2)
      if !File.writable?(db2path)
        raise "Error! File not writable : #{db2}"
      end
      print_debug("Loading destination database from file : #{db2path}")
      to_db, to_proj = loadDB(db2path)
      #$db,$pj = loadDB(db2path)
    else
      to_db, to_proj = $db, $pj
    end
  rescue StandardError => e
    puts e.message
    puts e.backtrace
    return nil
  end

  # Set working database to source database to prepare for reading
  $db, $pj = from_db, from_proj

  # Construct a hash to store columns and cells we are transferring
  print_debug("Fetching columns...")
  begin
    col_map = Hash.new
    cell_map = Hash.new
    for col in varnames
      c = getColumn(col.to_s)
      if c.nil?
        puts "Warning: column #{c} not found! Skipping..."
        next
      end
      col_map[col] = c
      cell_map[col] = c.cells
      print_debug("Read column : #{col.to_s}")
    end
  end

  # Set working database to destination database to prepare for writing
  $db, $pj = to_db, to_proj

  # Go through the hashmaps and reconstruct the columns
  begin
    for key in col_map.keys
      col = col_map[key]
      cells = cell_map[key]
      arglist = col.arglist

      # Construct a new variable and add all associated cells
      newvar = createVariable(key.to_s, arglist)
      for cell in cells
        c = newvar.make_new_cell()
        # Clone the existing cell arguments to the new cell.
        cell.arglist.each { |x|
          c.change_code(x, cell.get_arg(x))
        }
        c.ordinal = cell.ordinal
        c.onset = cell.onset
        c.offset = cell.offset
      end
      setVariable(key.to_s, newvar)
      print_debug("Wrote column : #{key.to_s} with #{newvar.cells.length} cells")
    end
  rescue StandardError => e
    puts "Failed trying to write column #{col}"
    puts e.message
    puts e.backtrace
    return nil
  end

  # Save the database to file if applicable
  saveDB(db2path) if db2path!=""

  # Final step: take care of deleting columns from source database if option is set.
  if remove
    $db, $pj = from_db, from_proj

    # Use our hashmap since it takes care of improper column names (returned nil from getColumn())
    col_map.keys.each { |x|
      delete_column(x.to_s)
    }

    saveDB(db1path) if db1path!=""
  end

  # Restore the saved database and project globals
  $db, $pj = saved_db, saved_proj

  puts "Transfer completed successfully!"
end

#videos_to_column(column = 'tracks__', file_code = 'file', plugin_code = 'plugin', plugin_uuid = false) ⇒ Object

Save video controller information to spreadsheet column. Each stream is saved to a different cell. Onset and offset of cells designate track positions.

Parameters:

  • column_name (String)

    name of the returned column

  • file_code (String) (defaults to: 'file')

    code name of source file

  • plugin_code (String) (defaults to: 'plugin')

    code name of player plugin

  • plugin_uuid? (Boolean)

    save plugin as uuid if true, name otherwise



2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
# File 'datavyu_api.rb', line 2840

def videos_to_column(column = 'tracks__',
                     file_code = 'file',
                     plugin_code = 'plugin', plugin_uuid = false)
  if column.class == String
    tracks_col = new_column(column, file_code, plugin_code)
  end

  RVideoController.videos.each do |stream|
    ncell = tracks_col.new_cell
    ncell.change_code(file_code,
                      stream.get_source_file.get_path)
    plugin = if plugin_uuid
               RVideoController.plugin_uuid_of(stream)
             else
               RVideoController.plugin_of(stream)
             end
    ncell.change_code(plugin_code, plugin)
    ncell.onset = stream.get_offset
    ncell.offset = ncell.onset + stream.get_duration
  end
  tracks_col
end