File size: 8,475 Bytes
e4890d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import Papa from 'papaparse';
import { DataTable } from 'simple-datatables';

const languageMap = {
  'Arabic': 'ar',
  'Turkish': 'tr',
  'Swahili': 'sw',
  'Russian': 'ru',
  'Telugu': 'te',
  'Thai': 'th',
  'Chinese': 'zh',
  'French': 'fr',
  'Hindi': 'hi',
};

const metricTypes = [
  { value: 'max_score', label: 'Max Score' },
  { value: 'avg_snr', label: 'Low Noise' },
  { value: 'avg_spearman', label: 'Monotonicity' },
  { value: 'max_n_std', label: 'Non-Randomness' },
  { value: 'avg_kendall_tau_a', label: 'Ordering Consistency' }
];

const tableTypes = [
  { value: 'gen', label: 'Generative' },
  { value: 'mc', label: 'Multichoice' }
];

const taskFolders = [
  { value: 'selected', label: 'FineTasks' },
  { value: 'non_selected', label: 'Non-Selected' }
];

function createDropdown(options, onChange) {
  const select = document.createElement('select');
  options.forEach(option => {
    const optionElement = document.createElement('option');
    if (typeof option === 'object' && option.value && option.label) {
      optionElement.value = option.value;
      optionElement.textContent = option.label;
    } else {
      optionElement.value = option;
      optionElement.textContent = option;
    }
    select.appendChild(optionElement);
  });
  select.addEventListener('change', onChange);
  return select;
}

function createPerTaskResultsTable(data, tableType, metric) {
  const tableWrapper = document.createElement('div');
  tableWrapper.className = 'table-wrapper fine-tasks-table-wrapper';

  const table = document.createElement('table');
  table.className = 'results-table fine-tasks-results-table';

  const columns = ['Task', 'Type', ...(tableType === 'gen' ? ['f1', 'prefix_match'] : ['acc', 'acc_norm', 'acc_norm_token', 'acc_norm_pmi'])];
  
  const columnNameMap = {
    // 'Task': 'Task',
    // 'Type': 'Type',
    // 'f1': 'f1',
    // 'prefix_match': 'prefix_match',
    // 'acc': 'acc',
    'acc_norm': 'acc_char',
    'acc_norm_token': 'acc_token',
    'acc_norm_pmi': 'acc_pmi',
    'prefix_match': 'prefix'
  };

  const taskMetricMap = {
    'max_score': 'score',
    'avg_snr': 'snr',
    'avg_spearman': 'monotonicity',
    'max_n_std': 'non-randomness',
    'avg_kendall_tau_a': 'ordering'
    // 'avg_spearman': 'monotonicity',
  }

  const header = table.createTHead();
  const headerRow = header.insertRow();
  columns.forEach(column => {
    const th = document.createElement('th');
    th.textContent = columnNameMap[column] || column;

    if (th.textContent !== "Task" && th.textContent !== "Type") {
        th.textContent += " " + (taskMetricMap[metric] || metric);
    }
    th.title = th.textContent;
    if (column === 'Type')
      th.style.width = '40px';
    headerRow.appendChild(th);
  });

  const body = table.createTBody();
  data.forEach(row => {
    if (Object.values(row).every(value => value === '' || value === undefined || value === null)) {
      return;
    }
    
    const tr = body.insertRow();
    columns.forEach(column => {
      const td = tr.insertCell();
      let value = row[column];
      if (column === 'Task') {
        const fullTaskName = value; // Store the full task name
        const parts = value.split('|');
        value = parts.length > 1 ? parts[1] : value;
        value = value.split('_mcf')[0].split('_cf')[0];
        td.title = fullTaskName; // Set the title attribute to show the full name on hover
      } else if (column === 'Type') {
        // Keep the task type as is
      } else if (typeof value === 'number') {
        value = value.toFixed(2);
      } else if (value && !isNaN(parseFloat(value))) {
        value = parseFloat(value).toFixed(2);
      } else {
        value = '';
      }
      td.textContent = value;
    });
  });

  tableWrapper.appendChild(table);
  return tableWrapper;
}

export function initFineTasks(containerId) {
  const container = document.getElementById(containerId);
  if (!container) return;

  const perTaskTitleElement = document.createElement('h3');
  perTaskTitleElement.textContent = 'Task Results';
  perTaskTitleElement.className = 'fine-tasks-title';

  const perTaskTableContainer = document.createElement('div');
  perTaskTableContainer.className = 'table-container';

  let perTaskDataTable;

  function updatePerTaskResults() {
    const language = languageDropdownPerTask.value;
    const metric = metricDropdownPerTask.value;
    const tableType = tableTypeDropdownPerTask.value;
    const taskFolder = taskFolderDropdownPerTask.value;

    const languageCode = languageMap[language];

    if (!languageCode) {
      console.error(`Language code not found for ${language}`);
      perTaskTableContainer.innerHTML = `<p>Error: Language code not found for ${language}</p>`;
      return;
    }

    let url = `data/tasks/${taskFolder}/${languageCode}/${metric}/${tableType}_stats.csv`;

    fetch(url)
      .then(response => {
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return response.text();
      })
      .then(csvText => {
        const results = Papa.parse(csvText, { header: true }).data;
        perTaskTableContainer.innerHTML = '';
        const tableWrapper = createPerTaskResultsTable(results, tableType, metric);
        perTaskTableContainer.appendChild(tableWrapper);

        if (perTaskDataTable) {
          perTaskDataTable.destroy();
        }

        perTaskDataTable = new DataTable('.fine-tasks-results-table', {
          perPage: 10,
          perPageSelect: false,
          searchable: false,
          sortable: true,
          fixedHeight: true,
          labels: {
            info: ''  // This removes the "Showing 1 to X of Y entries" text
          }
        });

      })
      .catch(error => {
        console.error('Error fetching CSV:', error);
        perTaskTableContainer.innerHTML = `<p>Error loading data: ${error.message}</p>`;
      });
  }

  const perTaskControls = document.createElement('div');
  perTaskControls.className = 'controls fine-tasks-controls';

  // Task folder control group
  const taskFolderControlGroup = document.createElement('div');
  taskFolderControlGroup.className = 'control-group';
  const taskFolderLabelPerTask = document.createElement('label');
  taskFolderLabelPerTask.textContent = 'Task Set: ';
  const taskFolderDropdownPerTask = createDropdown(taskFolders, updatePerTaskResults);
  taskFolderDropdownPerTask.value = 'selected'; // Set default to FineTasks
  taskFolderControlGroup.appendChild(taskFolderLabelPerTask);
  taskFolderControlGroup.appendChild(taskFolderDropdownPerTask);

  // Language control group
  const languageControlGroup = document.createElement('div');
  languageControlGroup.className = 'control-group';
  const languageLabelPerTask = document.createElement('label');
  languageLabelPerTask.textContent = 'Language: ';
  const languageDropdownPerTask = createDropdown(Object.keys(languageMap), updatePerTaskResults);
  languageControlGroup.appendChild(languageLabelPerTask);
  languageControlGroup.appendChild(languageDropdownPerTask);

  // Table type control group
  const tableTypeControlGroup = document.createElement('div');
  tableTypeControlGroup.className = 'control-group';
  const tableTypeLabelPerTask = document.createElement('label');
  tableTypeLabelPerTask.textContent = 'Type: ';
  const tableTypeDropdownPerTask = createDropdown(tableTypes, updatePerTaskResults);
  tableTypeControlGroup.appendChild(tableTypeLabelPerTask);
  tableTypeControlGroup.appendChild(tableTypeDropdownPerTask);

  // Metric control group
  const metricControlGroup = document.createElement('div');
  metricControlGroup.className = 'control-group';
  const metricLabelPerTask = document.createElement('label');
  metricLabelPerTask.textContent = 'Criteria: ';
  const metricDropdownPerTask = createDropdown(metricTypes, updatePerTaskResults);
  metricDropdownPerTask.value = 'max_score'; // Set default to Max Score
  metricControlGroup.appendChild(metricLabelPerTask);
  metricControlGroup.appendChild(metricDropdownPerTask);

  perTaskControls.appendChild(taskFolderControlGroup);
  perTaskControls.appendChild(languageControlGroup);
  perTaskControls.appendChild(tableTypeControlGroup);
  perTaskControls.appendChild(metricControlGroup);

  container.appendChild(perTaskControls);
  // container.appendChild(perTaskTitleElement);
  container.appendChild(perTaskTableContainer);

  // Initialize with default values
  updatePerTaskResults();
}