diff --git a/open_wearable/lib/services/edge_ml/recording_importer.dart b/open_wearable/lib/services/edge_ml/recording_importer.dart new file mode 100644 index 000000000..a7dc4df5b --- /dev/null +++ b/open_wearable/lib/services/edge_ml/recording_importer.dart @@ -0,0 +1,255 @@ +import 'dart:convert'; + +import 'package:edge_ml_dart/edge_ml_dart.dart'; + +import '../../widgets/sensors/local_recorder/local_recorder_models.dart'; +import '../../widgets/sensors/local_recorder/local_recorder_storage.dart'; + +/// A conversion preview. No network requests are made by the importer. +class EdgeMlRecordingImport { + const EdgeMlRecordingImport({ + required this.recording, + required this.channelNames, + required this.warnings, + required this.firstTimestampMs, + required this.lastTimestampMs, + }); + + final DatasetRecording recording; + final List channelNames; + final List warnings; + final int firstTimestampMs; + final int lastTimestampMs; +} + +/// Converts completed local recordings, preserving each file's sensor channels. +/// +/// Legacy CSV files have no schema metadata. This reader accepts Unix epoch +/// milliseconds and microseconds, distinguished by magnitude. It never changes +/// sampling rates or assumes sensor files share timestamps. Units and quantities +/// are intentionally omitted when the source files do not specify them. +class EdgeMlRecordingImporter { + const EdgeMlRecordingImporter(); + + Future read( + LocalRecorderRecordingFolder folder, { + String? datasetName, + Future Function(LocalRecorderRecordingFile)? readText, + }) async { + final name = (datasetName ?? folder.name).trim(); + if (name.isEmpty) throw const FormatException('Enter a dataset name.'); + final recording = DatasetRecording( + name, + metadata: { + 'source': 'OpenWearables', + 'recordingFolder': folder.name, + 'timestampUnit': 'unix milliseconds', + }, + ); + final load = readText ?? readRecordingFileText; + final warnings = []; + final channels = {}; + final labelFiles = <_CsvFile>[]; + int? first; + int? last; + final files = [...folder.files]..sort((a, b) => a.name.compareTo(b.name)); + for (final file in files) { + if (!file.name.toLowerCase().endsWith('.csv')) { + warnings.add( + '${file.name}: skipped; only sensor and label CSVs are supported.', + ); + continue; + } + final csv = _CsvFile.parse(file.name, await load(file)); + if (file.name.startsWith('Label_')) { + labelFiles.add(csv); + continue; + } + if (csv.rows.isEmpty) { + warnings.add('${file.name}: skipped; no sensor samples.'); + continue; + } + final names = [ + for (final axis in csv.columns.skip(1)) '${csv.stem}_$axis', + ]; + for (final channel in names) { + if (!channels.add(channel)) { + throw FormatException( + 'Duplicate channel name: $channel. Rename the source files.', + ); + } + } + int? previous; + int? previousRaw; + var sharedMilliseconds = 0; + bool? microseconds; + for (final row in csv.rows) { + final raw = _timestamp(row.first, file.name); + final isMicroseconds = raw >= 100000000000000; + if (microseconds != null && microseconds != isMicroseconds) { + throw FormatException('${file.name}: mixed timestamp units.'); + } + microseconds = isMicroseconds; + final time = isMicroseconds ? raw ~/ 1000 : raw; + if (previousRaw != null && raw < previousRaw) { + throw FormatException( + '${file.name}: sensor timestamps are out of order.', + ); + } + if (previous == time) sharedMilliseconds++; + previousRaw = raw; + previous = time; + first = first == null || time < first ? time : first; + last = last == null || time > last ? time : last; + for (var i = 0; i < names.length; i++) { + final value = double.tryParse(row[i + 1]); + if (value == null || !value.isFinite) { + throw FormatException( + '${file.name}: invalid value for ${csv.columns[i + 1]}.', + ); + } + recording.add(names[i], value, timestamp: time); + } + } + if (sharedMilliseconds > 0) { + warnings.add( + '${file.name}: $sharedMilliseconds additional sample rows share ' + 'millisecond timestamps. All values are retained; timestamps are ' + 'limited to Edge ML’s millisecond precision.', + ); + } + } + if (first == null || last == null) { + throw const FormatException('This recording contains no sensor samples.'); + } + final groups = {}; + for (final csv in labelFiles) { + final group = csv.stem.substring('Label_'.length); + if (group.isEmpty || !groups.add(group)) { + throw FormatException('${csv.name}: empty or duplicate label group.'); + } + _addLabels(recording, csv, group, first, last, warnings); + } + warnings.add( + 'These CSVs do not store sensor units or configured sampling rates. ' + 'Units and quantities were left unspecified; sample timestamps were preserved ' + 'at millisecond precision.', + ); + return EdgeMlRecordingImport( + recording: recording, + channelNames: List.unmodifiable(channels), + warnings: List.unmodifiable(warnings), + firstTimestampMs: first, + lastTimestampMs: last, + ); + } + + void _addLabels( + DatasetRecording recording, + _CsvFile csv, + String group, + int first, + int last, + List warnings, + ) { + // Last state wins when several changes have the same millisecond timestamp. + final events = {}; + int? previous; + for (final row in csv.rows) { + final time = _timestamp(row.first, csv.name); + if (time >= 100000000000000) { + throw FormatException( + '${csv.name}: label timestamps must be Unix milliseconds.', + ); + } + if (previous != null && time < previous) { + throw FormatException( + '${csv.name}: label timestamps are out of order.', + ); + } + previous = time; + String? active; + for (var i = 1; i < row.length; i++) { + if (row[i] != 'true' && row[i] != 'false') { + throw FormatException( + '${csv.name}: label states must be true or false.', + ); + } + if (row[i] == 'true') { + if (active != null) { + throw FormatException( + '${csv.name}: multiple active labels in group $group.', + ); + } + active = csv.columns[i]; + } + } + events[time] = active; + } + final times = events.keys.toList(); + for (var i = 0; i < times.length; i++) { + final active = events[times[i]]; + if (active == null) continue; + final start = times[i] < first ? first : times[i]; + final nextEnd = i + 1 < times.length ? times[i + 1] - 1 : last; + final end = nextEnd > last ? last : nextEnd; + if (start <= end) { + recording.addLabel(DatasetLabel(group, active, start: start, end: end)); + } + } + if (csv.rows.isEmpty) { + warnings.add( + '${csv.name}: contains label names but no recorded label states.', + ); + } else if (times.any((t) => t < first || t > last)) { + warnings.add( + '${csv.name}: label ranges were clipped to the sensor recording bounds.', + ); + } + } +} + +int _timestamp(String text, String file) { + final value = int.tryParse(text); + // Current app recordings use absolute Unix timestamps, not relative ticks. + if (value == null || value < 100000000000 || value >= 100000000000000000) { + throw FormatException( + '$file: unsupported timestamp "$text". ' + 'Expected Unix milliseconds or microseconds.', + ); + } + return value; +} + +class _CsvFile { + const _CsvFile(this.name, this.columns, this.rows); + + final String name; + final List columns; + final List> rows; + String get stem => name.substring(0, name.length - 4); + + // Matches the recorder's unquoted, comma-separated output. The recorder does + // not escape commas or newlines in axis/label names; malformed rows fail here. + factory _CsvFile.parse(String name, String text) { + final lines = const LineSplitter().convert(text.replaceFirst('\uFEFF', '')); + if (lines.isEmpty) throw FormatException('$name: empty CSV file.'); + final columns = lines.first.split(',').map((s) => s.trim()).toList(); + if (columns.length < 2 || + columns.first != 'timestamp' || + columns.any((s) => s.isEmpty) || + columns.toSet().length != columns.length) { + throw FormatException('$name: invalid CSV header.'); + } + final rows = >[]; + for (var i = 1; i < lines.length; i++) { + if (lines[i].trim().isEmpty) continue; + final row = lines[i].split(',').map((s) => s.trim()).toList(); + if (row.length != columns.length) { + throw FormatException('$name: wrong column count on line ${i + 1}.'); + } + rows.add(row); + } + return _CsvFile(name, columns, rows); + } +} diff --git a/open_wearable/lib/widgets/sensors/local_recorder/edge_ml_upload_dialog.dart b/open_wearable/lib/widgets/sensors/local_recorder/edge_ml_upload_dialog.dart new file mode 100644 index 000000000..58af28e3b --- /dev/null +++ b/open_wearable/lib/widgets/sensors/local_recorder/edge_ml_upload_dialog.dart @@ -0,0 +1,264 @@ +import 'package:edge_ml_dart/edge_ml_dart.dart'; +import 'package:flutter/material.dart'; +import 'package:open_wearable/services/edge_ml/recording_importer.dart'; + +import 'local_recorder_models.dart'; + +/// Preview a completed recording and upload only after an explicit user action. +Future showEdgeMlUploadDialog( + BuildContext context, + LocalRecorderRecordingFolder folder, { + Future Function(LocalRecorderRecordingFolder)? prepare, + Future Function(DatasetRecording, String)? upload, +}) => showDialog( + context: context, + barrierDismissible: false, + builder: (_) => + _EdgeMlUploadDialog(folder: folder, prepare: prepare, upload: upload), +); + +Future _uploadRecording( + DatasetRecording recording, + String apiKey, +) async { + final client = EdgeML(apiKey: apiKey); + try { + return await client.datasets.upload(recording); + } finally { + client.close(); + } +} + +class _EdgeMlUploadDialog extends StatefulWidget { + const _EdgeMlUploadDialog({required this.folder, this.prepare, this.upload}); + + final LocalRecorderRecordingFolder folder; + final Future Function(LocalRecorderRecordingFolder)? + prepare; + final Future Function(DatasetRecording, String)? upload; + + @override + State<_EdgeMlUploadDialog> createState() => _EdgeMlUploadDialogState(); +} + +class _EdgeMlUploadDialogState extends State<_EdgeMlUploadDialog> { + final _key = TextEditingController(); + final _form = GlobalKey(); + late final TextEditingController _name; + EdgeMlRecordingImport? _preview; + String? _error; + String? _datasetId; + bool _preparing = true; + bool _uploading = false; + + @override + void initState() { + super.initState(); + _name = TextEditingController(text: widget.folder.name); + _prepare(); + } + + Future _prepare() async { + try { + final prepare = widget.prepare ?? const EdgeMlRecordingImporter().read; + final preview = await prepare(widget.folder); + if (mounted) setState(() => _preview = preview); + } on FormatException catch (e) { + if (mounted) setState(() => _error = e.message); + } catch (_) { + if (mounted) { + setState( + () => _error = + 'Could not read this recording. Close this window and try again.', + ); + } + } finally { + if (mounted) setState(() => _preparing = false); + } + } + + Future _upload() async { + if (_uploading || _preview == null || !_form.currentState!.validate()) { + return; + } + setState(() { + _uploading = true; + _error = null; + }); + final apiKey = _key.text.trim(); + try { + final source = _preview!.recording; + final recording = DatasetRecording( + _name.text.trim(), + metadata: source.metadata, + units: source.units, + quantities: source.quantities, + ); + for (final series in source.timeSeries.entries) { + for (final (time, value) in series.value) { + recording.add(series.key, value, timestamp: time); + } + } + for (final label in source.labels) { + recording.addLabel(label); + } + final upload = widget.upload ?? _uploadRecording; + final id = await upload(recording, apiKey); + if (mounted) { + setState(() => _datasetId = id); + _key.clear(); + } + } on AuthenticationException { + if (mounted) { + setState( + () => _error = + 'The key was rejected. Use the project’s Device API write key.', + ); + } + } catch (_) { + // API URLs contain the write key. Never display or log raw HTTP errors. + if (mounted) { + setState( + () => _error = + 'Upload did not complete. Check your connection and Edge ML ' + 'project before retrying: a partial dataset may have been created. ' + 'Retrying creates a new dataset.', + ); + } + } finally { + if (mounted) setState(() => _uploading = false); + } + } + + @override + void dispose() { + _key.dispose(); + _name.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final preview = _preview; + return PopScope( + canPop: !_uploading, + child: AlertDialog( + title: const Text('Upload to Edge ML'), + content: SizedBox( + width: 480, + child: SingleChildScrollView( + child: Form( + key: _form, + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (_preparing) ...[ + const LinearProgressIndicator(), + const SizedBox(height: 12), + const Text('Preparing sensor data and labels…'), + ], + if (_datasetId != null) ...[ + const Text('Sensor data and labels uploaded successfully.'), + const SizedBox(height: 12), + SelectableText('Dataset ID: $_datasetId'), + const SizedBox(height: 12), + const Text( + 'Open your project at app.edge-ml.org to review the dataset.', + ), + ] else if (preview != null) ...[ + Text( + '${preview.channelNames.length} channels · ' + '${preview.recording.length} values · ' + '${preview.recording.labels.length} label intervals', + ), + const SizedBox(height: 12), + ExpansionTile( + title: const Text('Channels'), + children: [ + SizedBox( + height: 110, + child: SingleChildScrollView( + child: SelectableText( + preview.channelNames.join('\n'), + ), + ), + ), + ], + ), + const SizedBox(height: 12), + for (final warning in preview.warnings) + Padding( + padding: const EdgeInsets.only(bottom: 8), + child: Text(warning), + ), + const SizedBox(height: 8), + TextFormField( + controller: _name, + enabled: !_uploading, + decoration: const InputDecoration( + labelText: 'Dataset name', + ), + validator: (value) => + value == null || value.trim().isEmpty + ? 'Enter a dataset name.' + : null, + ), + const SizedBox(height: 12), + TextFormField( + controller: _key, + enabled: !_uploading, + obscureText: true, + autocorrect: false, + enableSuggestions: false, + decoration: const InputDecoration( + labelText: 'Device API write key', + ), + validator: (value) => + value == null || value.trim().isEmpty + ? 'Enter the project’s write key.' + : null, + ), + const SizedBox(height: 8), + const Text( + 'Find the write key in Edge ML → project settings → Device API. ' + 'It is used for this upload and is not saved.', + ), + if (_uploading) ...[ + const SizedBox(height: 16), + const LinearProgressIndicator(), + const SizedBox(height: 8), + const Text( + 'Uploading to app.edge-ml.org… Keep this window open.', + ), + ], + ], + if (_error != null) ...[ + const SizedBox(height: 12), + Text( + _error!, + style: TextStyle( + color: Theme.of(context).colorScheme.error, + ), + ), + ], + ], + ), + ), + ), + ), + actions: [ + TextButton( + onPressed: _uploading ? null : () => Navigator.of(context).pop(), + child: Text(_datasetId != null ? 'Done' : 'Close'), + ), + if (preview != null && _datasetId == null) + FilledButton( + onPressed: _uploading ? null : _upload, + child: const Text('Upload to Edge ML'), + ), + ], + ), + ); + } +} diff --git a/open_wearable/lib/widgets/sensors/local_recorder/local_recorder_recording_folder_card.dart b/open_wearable/lib/widgets/sensors/local_recorder/local_recorder_recording_folder_card.dart index e368b8c73..328aabd33 100644 --- a/open_wearable/lib/widgets/sensors/local_recorder/local_recorder_recording_folder_card.dart +++ b/open_wearable/lib/widgets/sensors/local_recorder/local_recorder_recording_folder_card.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'edge_ml_upload_dialog.dart'; import 'package:open_wearable/widgets/recording_activity_indicator.dart'; import 'package:open_wearable/widgets/sensors/local_recorder/local_recorder_models.dart'; import 'package:open_wearable/widgets/sensors/sensor_page_spacing.dart'; @@ -125,9 +126,21 @@ class LocalRecorderRecordingFolderCard extends StatelessWidget { onTap: isCurrentRecording ? null : (selectionMode && onSelectionToggle != null - ? onSelectionToggle - : onToggleExpanded), + ? onSelectionToggle + : onToggleExpanded), ), + if (!isCurrentRecording && !selectionMode) + Padding( + padding: const EdgeInsets.fromLTRB(16, 0, 16, 8), + child: Align( + alignment: Alignment.centerLeft, + child: TextButton.icon( + onPressed: () => showEdgeMlUploadDialog(context, folder), + icon: const Icon(Icons.cloud_upload_outlined), + label: const Text('Upload to Edge ML'), + ), + ), + ), if (isExpanded) const Divider(height: 1), if (isExpanded && files.isEmpty) Padding( diff --git a/open_wearable/linux/flutter/generated_plugins.cmake b/open_wearable/linux/flutter/generated_plugins.cmake index 3727ee0d9..2ea39fe80 100644 --- a/open_wearable/linux/flutter/generated_plugins.cmake +++ b/open_wearable/linux/flutter/generated_plugins.cmake @@ -10,6 +10,7 @@ list(APPEND FLUTTER_PLUGIN_LIST ) list(APPEND FLUTTER_FFI_PLUGIN_LIST + executorch_flutter jni ) diff --git a/open_wearable/macos/Runner/DebugProfile.entitlements b/open_wearable/macos/Runner/DebugProfile.entitlements index 27acdb98a..ef7d4ce83 100644 --- a/open_wearable/macos/Runner/DebugProfile.entitlements +++ b/open_wearable/macos/Runner/DebugProfile.entitlements @@ -12,5 +12,7 @@ com.apple.security.network.server + com.apple.security.network.client + diff --git a/open_wearable/macos/Runner/Release.entitlements b/open_wearable/macos/Runner/Release.entitlements index f02ee084b..6eb22196f 100644 --- a/open_wearable/macos/Runner/Release.entitlements +++ b/open_wearable/macos/Runner/Release.entitlements @@ -10,5 +10,7 @@ com.apple.security.network.server + com.apple.security.network.client + diff --git a/open_wearable/pubspec.lock b/open_wearable/pubspec.lock index c42d3920e..4056248e4 100644 --- a/open_wearable/pubspec.lock +++ b/open_wearable/pubspec.lock @@ -113,6 +113,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + change_case: + dependency: transitive + description: + name: change_case + sha256: e41ef3df58521194ef8d7649928954805aeb08061917cf658322305e61568003 + url: "https://pub.dev" + source: hosted + version: "2.2.0" characters: dependency: transitive description: @@ -217,6 +225,14 @@ packages: url: "https://pub.dev" source: hosted version: "8.1.0" + edge_ml_dart: + dependency: "direct main" + description: + name: edge_ml_dart + sha256: "240a9631e209d1dba2c45aa1b01c0a36c98dcc96b506c7295ebea3b7e8802bca" + url: "https://pub.dev" + source: hosted + version: "1.1.0" equatable: dependency: transitive description: @@ -225,6 +241,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + executorch_dart: + dependency: transitive + description: + name: executorch_dart + sha256: "0bd81e2d65892ad84f449bedf524527d8aeeedada4e7d2f0ac79232d326c4e41" + url: "https://pub.dev" + source: hosted + version: "0.8.0" + executorch_flutter: + dependency: transitive + description: + name: executorch_flutter + sha256: "1dbc76e899b35ef824eac010f8e87024a1bf83993a914ecc9184ce143dbd445a" + url: "https://pub.dev" + source: hosted + version: "0.8.0" fake_async: dependency: transitive description: @@ -400,6 +432,14 @@ packages: description: flutter source: sdk version: "0.0.0" + glob: + dependency: transitive + description: + name: glob + sha256: "218aeb56050c714f62a3182775320dfa04602b55074873e24e31bbd39bda96fb" + url: "https://pub.dev" + source: hosted + version: "2.2.0" go_router: dependency: "direct main" description: @@ -524,10 +564,10 @@ packages: dependency: transitive description: name: matcher - sha256: "31bd099b47c10cd1aeb55146a2d46ce0277630ecef3f7dae54ad7873f36696cd" + sha256: dc0b7dc7651697ea4ff3e69ef44b0407ea32c487a39fff6a4004fa585e901861 url: "https://pub.dev" source: hosted - version: "0.12.20" + version: "0.12.19" material_color_utilities: dependency: transitive description: @@ -549,10 +589,10 @@ packages: dependency: transitive description: name: meta - sha256: "307249ce4ff29d58a18e97f6345f539382eb9c9c29ecda628900f31de0443dd9" + sha256: "1741988757a65eb6b36abe716829688cf01910bbf91c34354ff7ec1c3de2b349" url: "https://pub.dev" source: hosted - version: "1.19.0" + version: "1.18.0" mime: dependency: transitive description: @@ -561,6 +601,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + native_toolchain_cmake: + dependency: transitive + description: + name: native_toolchain_cmake + sha256: "8ba223410102665483e873b83d2156720233a6a0181c07f9dc41cb2e961336b0" + url: "https://pub.dev" + source: hosted + version: "0.3.2" nested: dependency: transitive description: @@ -1074,10 +1122,10 @@ packages: dependency: transitive description: name: test_api - sha256: "2a122cbe059f8b610d3a5415f42e255b6c17b1f21eee1d960f31080237fb4f11" + sha256: "949a932224383300f01be9221c39180316445ecb8e7547f70a41a35bf421fb9e" url: "https://pub.dev" source: hosted - version: "0.7.12" + version: "0.7.11" tuple: dependency: transitive description: @@ -1203,10 +1251,10 @@ packages: dependency: transitive description: name: vector_math - sha256: "92b9910f66ed1057fd4da7b040ae7c74cafacf885bdc81be496928d5049b032d" + sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b url: "https://pub.dev" source: hosted - version: "2.4.3" + version: "2.2.0" vm_service: dependency: transitive description: diff --git a/open_wearable/pubspec.yaml b/open_wearable/pubspec.yaml index bf476f350..e4031b165 100644 --- a/open_wearable/pubspec.yaml +++ b/open_wearable/pubspec.yaml @@ -19,7 +19,8 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.6.0 environment: - sdk: ^3.6.0 + sdk: ^3.10.0 + flutter: ">=3.38.0" # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions @@ -64,6 +65,7 @@ dependencies: record: ^7.0.0 path: ^1.9.1 web: ^1.1.1 + edge_ml_dart: ^1.1.0 dev_dependencies: flutter_test: diff --git a/open_wearable/test/services/edge_ml/recording_importer_test.dart b/open_wearable/test/services/edge_ml/recording_importer_test.dart new file mode 100644 index 000000000..c6d708d96 --- /dev/null +++ b/open_wearable/test/services/edge_ml/recording_importer_test.dart @@ -0,0 +1,168 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/services/edge_ml/recording_importer.dart'; +import 'package:open_wearable/widgets/sensors/local_recorder/local_recorder_models.dart'; + +const base = 1793989737000; + +Future convert(Map files) { + final folder = LocalRecorderRecordingFolder( + path: 'session', + name: 'session', + updatedAt: DateTime(2026), + files: [ + for (final name in files.keys) + LocalRecorderRecordingFile( + path: name, + name: name, + sizeBytes: files[name]!.length, + updatedAt: DateTime(2026), + ), + ], + ); + return const EdgeMlRecordingImporter().read( + folder, + readText: (file) async => files[file.name]!, + ); +} + +void main() { + test( + 'keeps left/right channels and independent timestamps without resampling', + () async { + final result = await convert({ + 'OpenEarable-L-ACCELEROMETER.csv': + 'timestamp,X,Y\n${base * 1000},1,2\n${(base + 20) * 1000},3,4\n', + 'OpenEarable-R-ACCELEROMETER.csv': + 'timestamp,X,Y\n${(base + 7) * 1000},5,6\n', + 'Phone_Pressure.csv': 'timestamp,Pressure\n${base + 12},1000\n', + 'Empty.csv': 'timestamp,X\n', + 'audio.wav': 'not CSV', + }); + expect(result.recording.length, 7); + expect(result.channelNames.length, 5); + expect(result.recording.timeSeries['OpenEarable-L-ACCELEROMETER_X'], [ + (base, 1.0), + (base + 20, 3.0), + ]); + expect(result.recording.timeSeries['OpenEarable-R-ACCELEROMETER_X'], [ + (base + 7, 5.0), + ]); + expect(result.recording.timeSeries['Phone_Pressure_Pressure'], [ + (base + 12, 1000.0), + ]); + expect(result.firstTimestampMs, base); + expect(result.lastTimestampMs, base + 20); + expect(result.recording.units, isEmpty); + expect( + result.warnings.any((w) => w.contains('no sensor samples')), + isTrue, + ); + expect(result.warnings.any((w) => w.contains('audio.wav')), isTrue); + }, + ); + + test( + 'converts label changes to inclusive ranges with unlabeled gaps', + () async { + final result = await convert({ + 'Sensor.csv': 'timestamp,X\n$base,1\n${base + 100},2\n', + 'Label_MyLabel.csv': + 'timestamp,Label1,Label2\n' + '${base + 10},true,false\n' + '${base + 30},false,true\n' + '${base + 40},false,false\n' + '${base + 60},true,false\n', + }); + final labels = result.recording.labels; + expect(labels.length, 3); + expect(labels.map((l) => (l.labeling, l.label, l.start, l.end)), [ + ('MyLabel', 'Label1', base + 10, base + 29), + ('MyLabel', 'Label2', base + 30, base + 39), + ('MyLabel', 'Label1', base + 60, base + 100), + ]); + }, + ); + + test( + 'clips labels to samples and uses last event at an identical timestamp', + () async { + final result = await convert({ + 'Sensor.csv': 'timestamp,X\n$base,1\n${base + 100},2\n', + 'Label_Group.csv': + 'timestamp,A,B\n' + '${base - 5},true,false\n' + '${base + 30},false,false\n' + '${base + 30},false,true\n' + '${base + 200},false,false\n', + }); + expect(result.recording.labels.map((l) => (l.label, l.start, l.end)), [ + ('A', base, base + 29), + ('B', base + 30, base + 100), + ]); + }, + ); + + test('does not fabricate labels from a header-only label file', () async { + final result = await convert({ + 'Sensor.csv': 'timestamp,X\n$base,1\n', + 'Label_MyLabel.csv': 'timestamp,Label1,Label2\n', + }); + expect(result.recording.labels, isEmpty); + expect( + result.warnings.any((w) => w.contains('no recorded label states')), + isTrue, + ); + }); + + test( + 'preserves fast and repeated samples at shared millisecond timestamps', + () async { + final result = await convert({ + 'Sensor.csv': + 'timestamp,X\n${base * 1000},1\n${base * 1000 + 39},2\n${base * 1000 + 39},3\n${(base + 20) * 1000},4\n', + }); + expect(result.recording.timeSeries['Sensor_X'], [ + (base, 1.0), + (base, 2.0), + (base, 3.0), + (base + 20, 4.0), + ]); + expect(result.recording.length, 4); + expect( + result.warnings.any((w) => w.contains('2 additional sample rows')), + isTrue, + ); + }, + ); + + test( + 'still rejects raw timestamps that move backwards within a millisecond', + () async { + await expectLater( + convert({ + 'Sensor.csv': + 'timestamp,X\n${base * 1000 + 500},1\n${base * 1000 + 39},2\n', + }), + throwsFormatException, + ); + }, + ); + + test('rejects corrupt values and simultaneous labels', () async { + await expectLater( + convert({'Sensor.csv': 'timestamp,X\n$base,NaN\n'}), + throwsFormatException, + ); + await expectLater( + convert({ + 'Sensor.csv': 'timestamp,X\n$base,1\n', + 'Label_Group.csv': 'timestamp,A,B\n$base,true,true\n', + }), + throwsFormatException, + ); + await expectLater( + convert({'Sensor.csv': 'timestamp,X\n'}), + throwsFormatException, + ); + }); +} diff --git a/open_wearable/test/widgets/edge_ml_upload_dialog_test.dart b/open_wearable/test/widgets/edge_ml_upload_dialog_test.dart new file mode 100644 index 000000000..4a2d4f9e9 --- /dev/null +++ b/open_wearable/test/widgets/edge_ml_upload_dialog_test.dart @@ -0,0 +1,136 @@ +import 'dart:async'; + +import 'package:edge_ml_dart/edge_ml_dart.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:open_wearable/services/edge_ml/recording_importer.dart'; +import 'package:open_wearable/widgets/sensors/local_recorder/edge_ml_upload_dialog.dart'; +import 'package:open_wearable/widgets/sensors/local_recorder/local_recorder_models.dart'; + +final folder = LocalRecorderRecordingFolder( + path: 'session', + name: 'session', + updatedAt: DateTime(2026), + files: [], +); + +Future openDialog( + WidgetTester tester, { + required Future Function(DatasetRecording, String) upload, + Future Function(LocalRecorderRecordingFolder)? prepare, +}) async { + final recording = DatasetRecording('session'); + recording.add('left_X', 1, timestamp: 1793989737000); + recording.addLabel( + const DatasetLabel( + 'MyLabel', + 'Label1', + start: 1793989737000, + end: 1793989737000, + ), + ); + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (context) => Scaffold( + body: TextButton( + onPressed: () => showEdgeMlUploadDialog( + context, + folder, + prepare: + prepare ?? + (_) async => EdgeMlRecordingImport( + recording: recording, + channelNames: ['left_X'], + warnings: [], + firstTimestampMs: 1793989737000, + lastTimestampMs: 1793989737000, + ), + upload: upload, + ), + child: const Text('Open'), + ), + ), + ), + ), + ); + await tester.tap(find.text('Open')); + await tester.pumpAndSettle(); +} + +void main() { + testWidgets( + 'requires key, uploads only on request and prevents duplicate submissions', + (tester) async { + final completed = Completer(); + var uploads = 0; + await openDialog( + tester, + upload: (recording, key) { + uploads++; + expect(recording.name, 'Renamed'); + expect(recording.labels.single.label, 'Label1'); + expect(key, 'write-key'); + return completed.future; + }, + ); + expect(uploads, 0); + await tester.tap(find.widgetWithText(FilledButton, 'Upload to Edge ML')); + await tester.pump(); + expect(find.text('Enter the project’s write key.'), findsOneWidget); + expect(uploads, 0); + await tester.enterText(find.byType(TextFormField).first, 'Renamed'); + await tester.enterText(find.byType(TextFormField).last, 'write-key'); + await tester.tap(find.widgetWithText(FilledButton, 'Upload to Edge ML')); + await tester.pump(); + expect(uploads, 1); + expect( + tester.widget(find.byType(FilledButton)).onPressed, + isNull, + ); + expect( + tester + .widget(find.widgetWithText(TextButton, 'Close')) + .onPressed, + isNull, + ); + completed.complete('dataset-123'); + await tester.pumpAndSettle(); + expect(find.text('Dataset ID: dataset-123'), findsOneWidget); + expect(find.text('Done'), findsOneWidget); + expect(find.byType(TextFormField), findsNothing); + }, + ); + + testWidgets('does not expose API keys through HTTP error text', ( + tester, + ) async { + await openDialog( + tester, + upload: (_, key) async => throw Exception('URL/$key'), + ); + await tester.enterText(find.byType(TextFormField).last, 'secret-key'); + await tester.tap(find.byType(FilledButton)); + await tester.pumpAndSettle(); + expect( + find.textContaining('partial dataset may have been created'), + findsOneWidget, + ); + expect(find.text('URL/secret-key'), findsNothing); + expect( + tester.widget(find.byType(FilledButton)).onPressed, + isNotNull, + ); + }); + + testWidgets('conversion errors prevent upload', (tester) async { + await openDialog( + tester, + upload: (_, _) async => throw StateError('must not upload'), + prepare: (_) async => throw const FormatException('No sensor samples.'), + ); + expect(find.text('No sensor samples.'), findsOneWidget); + expect(find.byType(FilledButton), findsNothing); + expect(find.text('Close'), findsOneWidget); + }); +} diff --git a/open_wearable/windows/flutter/generated_plugins.cmake b/open_wearable/windows/flutter/generated_plugins.cmake index dc47854ef..79a5d5003 100644 --- a/open_wearable/windows/flutter/generated_plugins.cmake +++ b/open_wearable/windows/flutter/generated_plugins.cmake @@ -12,6 +12,7 @@ list(APPEND FLUTTER_PLUGIN_LIST ) list(APPEND FLUTTER_FFI_PLUGIN_LIST + executorch_flutter jni )