Skip to content

Commit

Permalink
fix csv reading
Browse files Browse the repository at this point in the history
  • Loading branch information
yaramt committed Aug 13, 2024
1 parent 3410111 commit 3b049c3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 69 deletions.
119 changes: 65 additions & 54 deletions app/lib/screens/study/report/sections/results_textual_summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,78 +24,89 @@ class TextualSummaryWidget extends AverageSectionWidget {
// Two Sample t-test
final tTest = TTest(valuesInterventionA, valuesInterventionB);
// Determine the summary text based on t-test results
final summaryText = getTextualSummary(tTest.isSignificantlyDifferent());
return Row(
children: <Widget>[
Expanded(
child: Column(
return FutureBuilder<bool>(
future: tTest.isSignificantlyDifferent(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else {
final summaryText = getTextualSummary(snapshot.data!);
return Row(
children: <Widget>[
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12.0),
),
Expanded(
child: Column(
children: <Widget>[
Text(
nameInterventionA,
style: const TextStyle(
fontSize: 16,
color: Colors.blue,
fontWeight: FontWeight.bold,
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12.0),
),
),
const SizedBox(height: 4), // SizedBox for spacing
Text(
summaryText[0],
style: const TextStyle(
fontSize: 12,
color: Colors.black,
child: Column(
children: <Widget>[
Text(
nameInterventionA,
style: const TextStyle(
fontSize: 16,
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
summaryText[0],
style: const TextStyle(
fontSize: 12,
color: Colors.black,
),
),
],
),
),
],
),
),
],
),
),
Expanded(
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12.0),
),
Expanded(
child: Column(
children: <Widget>[
Text(
nameInterventionB,
style: const TextStyle(
fontSize: 16,
color: Colors.orange,
fontWeight: FontWeight.bold,
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(12.0),
),
),
const SizedBox(height: 4),
Text(
summaryText[1],
style: const TextStyle(
fontSize: 12,
color: Colors.black,
child: Column(
children: <Widget>[
Text(
nameInterventionB,
style: const TextStyle(
fontSize: 16,
color: Colors.orange,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
summaryText[1],
style: const TextStyle(
fontSize: 12,
color: Colors.black,
),
),
],
),
),
],
),
),
],
),
),
],
);
}
},
);
}

Expand Down
39 changes: 24 additions & 15 deletions app/lib/screens/study/report/sections/t_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';

import 'package:csv/csv.dart';
import 'package:flutter/services.dart';
import 'package:statistics/statistics.dart';

// This class implements the Welch's two sample t-test according to https://en.wikipedia.org/wiki/Student%27s_t-test
Expand Down Expand Up @@ -34,27 +35,35 @@ class TTest {
return numerator / denominator;
}

double getTCriticalFromCsv(double degreesOfFreedom, String filePath) {
final List<List<double>> rows =
const CsvToListConverter().convert(filePath);
for (final row in rows) {
if (row[0] == degreesOfFreedom) {
return row[1];
// Method to read CSV data from assets
Future<List<List<dynamic>>> loadCsvData() async {
final csvData =
await rootBundle.loadString('assets/data/t_critical_values.csv');
return const CsvToListConverter(eol: '\n').convert(csvData);
}

// Get t-critical value based on degrees of freedom
Future<double> getTCritical(double degreesOfFreedom) async {
final List<List<dynamic>> rows = await loadCsvData();
// Default alpha/2 = 0.05/2 = 0.025
final int roundedDegreesOfFreedom = degreesOfFreedom.round();
// Iterate over rows, skipping the header
for (int i = 1; i < rows.length; i++) {
final row = rows[i];
if (row.isNotEmpty) {
final degreeOfFreedom = row[0] as int;
final criticalValue = row[1] as double;
if (degreeOfFreedom == roundedDegreesOfFreedom) {
return criticalValue;
}
}
}
throw Exception(
"Degrees of Freedom not found in CSV, study too long, reduce number of days");
}

double getTCritical(double degreesOfFreedom) {
const String csvFilePath = 'assets/data/t_critical_values.csv';
// Default alpha/2 = 0.05/2 = 0.025
final double tCritical = getTCriticalFromCsv(degreesOfFreedom, csvFilePath);
return tCritical;
}

// Method to check if the result is significant based on alpha level 0.05
bool isSignificantlyDifferent() {
Future<bool> isSignificantlyDifferent() async {
final num meanA = sampleA.mean;
final num meanB = sampleB.mean;
final num varianceA = variance(sampleA);
Expand All @@ -65,7 +74,7 @@ class TTest {
calculateTStatistic(meanA, meanB, varianceA, varianceB, nA, nB);
final double degreesOfFreedom =
calculateDegreesOfFreedom(meanA, meanB, varianceA, varianceB, nA, nB);
final double tCritical = getTCritical(degreesOfFreedom);
final double tCritical = await getTCritical(degreesOfFreedom);
return tStatistic.abs() > tCritical;
}
}

0 comments on commit 3b049c3

Please sign in to comment.