Skip to content

Commit

Permalink
Merge d9ec140 into faf4d5a
Browse files Browse the repository at this point in the history
  • Loading branch information
gyrdym committed May 17, 2020
2 parents faf4d5a + d9ec140 commit a2ae6f8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 12.8.2
- `fromMatrixJson`: dynamic type support added to parsing

## 12.8.1
- `DType`:
- `dTypeToJson` helper function added to public API
Expand Down
13 changes: 10 additions & 3 deletions lib/src/matrix/serialization/from_matrix_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import 'package:ml_linalg/src/matrix/serialization/matrix_json_keys.dart';

/// Restores a matrix instance from given [json]
Matrix fromMatrixJson(Map<String, dynamic> json) {
final list = json[matrixDataJsonKey] as List<List<double>>;
final matrixSource = json[matrixDataJsonKey] as List<dynamic>;

if (list == null) {
if (matrixSource == null) {
throw Exception('Provided json is missing `$matrixDataJsonKey` field');
}

final double2dList = matrixSource
.map(
(dynamic row) => (row as List<dynamic>)
.map((dynamic element) => double.parse(element.toString()))
.toList(growable: false))
.toList(growable: false);

final encodedDType = json[matrixDTypeJsonKey] as String;

if (encodedDType == null) {
Expand All @@ -18,5 +25,5 @@ Matrix fromMatrixJson(Map<String, dynamic> json) {

final dType = fromDTypeJson(encodedDType);

return Matrix.fromList(list, dtype: dType);
return Matrix.fromList(double2dList, dtype: dType);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra and statistics for data science
version: 12.8.1
version: 12.8.2
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down
15 changes: 15 additions & 0 deletions test/unit_test/matrix/serialization/from_matrix_json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ void main() {
[33, -987, 90, 732],
];

final dataWithNull = <List<double>>[
[123.0009863, null, 11.777209, 90003.112],
[-93.5678, 12, null, -10e2],
];

final emptyData = <List<double>>[];
final dataWithEmptyRow = <List<double>>[[]];

Expand Down Expand Up @@ -44,6 +49,11 @@ void main() {
matrixDataJsonKey: dataWithEmptyRow,
};

final validFloat32JsonWithNullInData = {
matrixDTypeJsonKey: dTypeFloat32EncodedValue,
matrixDataJsonKey: dataWithNull,
};

test('should throw an error if data key is absent', () {
final actual = () => fromMatrixJson(jsonWithoutData);
expect(actual, throwsException);
Expand Down Expand Up @@ -76,5 +86,10 @@ void main() {
final matrix = fromMatrixJson(validFloat32JsonWithEmptyRow);
expect(matrix, emptyData);
});

test('should throw an error if there are nulls in the data', () {
final actual = () => fromMatrixJson(validFloat32JsonWithNullInData);
expect(actual, throwsFormatException);
});
});
}

0 comments on commit a2ae6f8

Please sign in to comment.