Skip to content

Instantly share code, notes, and snippets.

@raco
Created June 2, 2019 21:19
Show Gist options
  • Save raco/13dce3f9de58a38e23211973d9415b5f to your computer and use it in GitHub Desktop.
Save raco/13dce3f9de58a38e23211973d9415b5f to your computer and use it in GitHub Desktop.
algoritmo_muse
try {
$rows = Excel::toArray(new ResultsImport, $csvFile);
// $rows = $results[0];
$rowsCount = count($rows);
$baseline_rows = 0;
$baseline_alphasum = 0;
$baseline_betasum = 0;
$tempArray = array();
$normalisedArray = array();
for ($i = 0; $i < $rowsCount; $i++) {
$row = $rows[$i];
if ($row['activity'] == "PREPARATION") {
$baseline_rows += 1;
$baseline_alphasum += $row['alpha_relative'];
$baseline_betasum += $row['beta_relative'];
}
}
if ($baseline_alphasum == 0) {
$baseline_alphasum = 1;
}
if ($baseline_betasum == 0) {
$baseline_betasum = 1;
}
$baseline_alpha = round(($baseline_alphasum / $baseline_rows), 3);
$baseline_beta = round(($baseline_betasum / $baseline_rows), 3);
$preparation_ratio = round(($baseline_alpha / $baseline_beta), 3);
for ($i = 0; $i < $rowsCount; $i++) {
$row = $rows[$i];
if ($row['activity'] != "PREPARATION" && $row['activity'] != "ACTIVITY") {
$tempArray = array(
'competenceid' => $row['competence'],
'activity' => $row['activity'],
'alphanormalised' => round(((($row['alpha_relative'] - $baseline_alpha) / $baseline_alpha) * 100), 3),
'betanormalised' => round(((($row['beta_relative'] - $baseline_beta) / $baseline_beta) * 100), 3),
'alpharelative' => $row['alpha_relative'],
'betarelative' => $row['beta_relative'],
);
array_push($normalisedArray, $tempArray);
}
}
$normalisedRows = [];
$normalisedArrayCount = count($normalisedArray);
for ($i = 0; $i < $normalisedArrayCount; $i++) {
$value = $normalisedArray[$i];
$competenceActivity = &$normalisedRows[$value['competenceid'] . '_' . $value['activity']];
if (!isset($competenceActivity['alpha_normalised'])) {
$competenceActivity['alpha_normalised'] = $value['alphanormalised'];
} else {
$competenceActivity['alpha_normalised'] += $value['alphanormalised'];
}
if (!isset($competenceActivity['beta_normalised'])) {
$competenceActivity['beta_normalised'] = $value['betanormalised'];
} else {
$competenceActivity['beta_normalised'] += $value['betanormalised'];
}
if (!isset($competenceActivity['alpha_normalised_q'])) {
$competenceActivity['alpha_normalised_q'] = 1;
} else {
$competenceActivity['alpha_normalised_q']++;
}
if (!isset($competenceActivity['beta_normalised_q'])) {
$competenceActivity['beta_normalised_q'] = 1;
} else {
$competenceActivity['beta_normalised_q']++;
}
// RESPONDING
if (!isset($competenceActivity['alpha_sum'])) {
$competenceActivity['alpha_sum'] = $value['alpharelative'];
} else {
$competenceActivity['alpha_sum'] += $value['alpharelative'];
}
if (!isset($competenceActivity['beta_sum'])) {
$competenceActivity['beta_sum'] = $value['betarelative'];
} else {
$competenceActivity['beta_sum'] += $value['betarelative'];
}
}
foreach ($normalisedRows as $key => $row) {
if (($row['alpha_normalised'] / $row['alpha_normalised_q']) > ($row['beta_normalised'] / $row['beta_normalised_q'])) {
$normalisedRows[$key]['predominant'] = 'Alpha';
} elseif (($row['alpha_normalised'] / $row['alpha_normalised_q']) == ($row['beta_normalised'] / $row['beta_normalised_q'])) {
$normalisedRows[$key]['predominant'] = 'Error';
} else {
$normalisedRows[$key]['predominant'] = 'Beta';
}
// RESPONDING
$normalisedRows[$key]['responding_ratio'] = round(($row['alpha_sum'] / $row['beta_sum']), 3);
$normalisedRows[$key]['preparation_ratio'] = $preparation_ratio;
unset($normalisedRows[$key]['alpha_normalised']);
unset($normalisedRows[$key]['beta_normalised']);
unset($normalisedRows[$key]['alpha_normalised_q']);
unset($normalisedRows[$key]['beta_normalised_q']);
unset($normalisedRows[$key]['alpha_normalised_d']);
unset($normalisedRows[$key]['beta_normalised_d']);
unset($normalisedRows[$key]['alpha_sum']);
unset($normalisedRows[$key]['beta_sum']);
}
logger(json_encode($normalisedRows));
return $normalisedRows;
} catch (\Exception $e) {
\App\Entities\Log::create(['name' => 'DATA_CONTENT_FAILURE']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment