-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_recode_chi_cv2.rb
126 lines (102 loc) · 4.25 KB
/
batch_recode_chi_cv2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'Datavyu_API'
$percent = 0.10
$input_dir = "~/code/work/bergelsonlab/pho_checks/opf_chi_cv4/input_orig"
$output_dir_cprod = "~/code/work/bergelsonlab/pho_checks/opf_chi_cv4/recode_out_cprod"
$output_dir_attobj = "~/code/work/bergelsonlab/pho_checks/opf_chi_cv4/recode_out_attobj"
$original_out_cprod = "~/code/work/bergelsonlab/pho_checks/opf_chi_cv4/orig_out_cprod"
$original_out_attobj = "~/code/work/bergelsonlab/pho_checks/opf_chi_cv4/orig_out_attobj"
def recode(in_dir, file)
puts(file)
$db, $pj = load_db(File.join(in_dir, file))
vocalization_col = "Infant_vocalization"
col = get_column(vocalization_col)
annotation_cells = col.cells.select do |elem|
elem.type.to_s.start_with? "C"
end
if annotation_cells.empty?
next
end
n = annotation_cells.size * $percent
if n < 1
n = n.ceil
else
n = n.round
end
start = rand(0..annotation_cells.size-n)
randrange = annotation_cells.size-1-n
puts("full size: #{col.cells.size} annot_cells: #{annotation_cells.size} randrange: #{randrange} n: #{n} start: #{start} end: #{start+n} ")
recode_slice = annotation_cells[start..start+(n-1)]
###########################################
# output blank 10% child_prod annotations #
###########################################
new_column = create_new_column("recode_child_prod", "child_prod", "orig_ordinal")
for cell in recode_slice
newcell = new_column.make_new_cell()
newcell.change_code("onset", cell.onset)
newcell.change_code("offset", cell.offset)
newcell.change_code("child_prod", "NA")
newcell.change_code("orig_ordinal", cell.ordinal)
end
columns = get_column_list()
for colu in columns
delete_variable(colu)
end
set_column(new_column)
save_db(File.join(File.expand_path($output_dir_cprod), file.sub(".opf", "_cprod_recode.opf")))
delete_variable(new_column)
#########################################
# output blank 10% att_obj annotations #
#########################################
new_column = create_new_column("recode_att_obj", "attended_object", "orig_ordinal")
for cell in recode_slice
newcell = new_column.make_new_cell()
newcell.change_code("onset", cell.onset)
newcell.change_code("offset", cell.offset)
newcell.change_code("attended_object", "NA")
newcell.change_code("orig_ordinal", cell.ordinal)
end
columns = get_column_list()
for colu in columns
delete_variable(colu)
end
set_column(new_column)
save_db(File.join(File.expand_path($output_dir_attobj), file.sub(".opf", "_attobj_recode.opf")))
delete_variable(new_column)
##############################################
# output original 10% child_prod annotations #
##############################################
new_column = create_new_column("original_child_prod", "child_prod", "orig_ordinal")
for cell in recode_slice
newcell = new_column.make_new_cell()
newcell.change_code("child_prod", cell.ctype)
newcell.change_code("orig_ordinal", cell.ordinal)
newcell.change_code("onset", cell.onset)
newcell.change_code("offset", cell.offset)
end
set_column(new_column)
save_db(File.join(File.expand_path($original_out_cprod), file.sub(".opf", "_cprod_recode_orig.opf")))
delete_variable(new_column)
############################################
# output original 10% att_obj annotations #
############################################
new_column = create_new_column("original_att_object", "attended_object", "orig_ordinal")
for cell in recode_slice
newcell = new_column.make_new_cell()
newcell.change_code("attended_object", cell.object)
newcell.change_code("orig_ordinal", cell.ordinal)
newcell.change_code("onset", cell.onset)
newcell.change_code("offset", cell.offset)
end
set_column(new_column)
save_db(File.join(File.expand_path($original_out_attobj), file.sub(".opf", "_attobj_recode_orig.opf")))
delete_variable(new_column)
end
begin
in_dir = File.expand_path($input_dir)
filenames = Dir.new(in_dir).entries
for file in filenames
if file.end_with? ".opf"
recode(in_dir, file)
end
end
end