Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'package:ml_linalg/src/vector/serialization/from_vector_json.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:ml_linalg/linalg.dart'; | ||
import 'package:ml_linalg/src/common/dtype_serializer/dtype_encoded_values.dart'; | ||
import 'package:ml_linalg/src/vector/vector_json_keys.dart'; | ||
|
||
/// Restores a vector instance from the given [json] | ||
Vector fromVectorJson(Map<String, dynamic> json) { | ||
final source = (json[vectorDataJsonKey] as List) | ||
.map((dynamic value) => double.parse(value.toString())) | ||
.toList(growable: false); | ||
|
||
switch(json[vectorDTypeJsonKey] as String) { | ||
case dTypeFloat32EncodedValue: | ||
return Vector.fromList(source, dtype: DType.float32); | ||
|
||
case dTypeFloat64EncodedValue: | ||
return Vector.fromList(source, dtype: DType.float64); | ||
|
||
default: | ||
throw UnsupportedError('Unknown dtype encoded value - ' | ||
'${json[vectorDTypeJsonKey]}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:ml_linalg/dtype_to_json.dart'; | ||
import 'package:ml_linalg/src/vector/vector_json_keys.dart'; | ||
import 'package:ml_linalg/vector.dart'; | ||
|
||
/// Returns a json-serializable map for the [vector] | ||
Map<String, dynamic> vectorToJson(Vector vector) { | ||
final encodedDType = dTypeToJson(vector.dtype); | ||
final encodedData = vector.toList(growable: false); | ||
|
||
return <String, dynamic>{ | ||
vectorDTypeJsonKey: encodedDType, | ||
vectorDataJsonKey: encodedData, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const vectorDTypeJsonKey = 'DT'; | ||
const vectorDataJsonKey = 'D'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'package:ml_linalg/src/vector/serialization/vector_to_json.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/integration_test/vector/constructors/from_json/from_json_constructor_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:ml_linalg/dtype.dart'; | ||
|
||
import 'from_json_constructor_test_group_factory.dart'; | ||
|
||
void main() { | ||
fromJsonConstructorTestGroupFactory(DType.float32); | ||
fromJsonConstructorTestGroupFactory(DType.float64); | ||
} |
36 changes: 36 additions & 0 deletions
36
...egration_test/vector/constructors/from_json/from_json_constructor_test_group_factory.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'package:ml_linalg/dtype.dart'; | ||
import 'package:ml_linalg/dtype_to_json.dart'; | ||
import 'package:ml_linalg/src/vector/vector_json_keys.dart'; | ||
import 'package:ml_linalg/vector.dart'; | ||
import 'package:ml_tech/unit_testing/matchers/iterable_almost_equal_to.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../dtype_to_title.dart'; | ||
|
||
void fromJsonConstructorTestGroupFactory(DType dtype) => | ||
group(dtypeToVectorTestTitle[dtype], () { | ||
group('fromJson constructor', () { | ||
final data = <double>[12, 23, 44.0, -1e10, 10007.888, -10, 0, 7]; | ||
final json = { | ||
vectorDTypeJsonKey: dTypeToJson(dtype), | ||
vectorDataJsonKey: data, | ||
}; | ||
final unknownJson = { | ||
vectorDTypeJsonKey: 'some_unknown_dtype', | ||
vectorDataJsonKey: data, | ||
}; | ||
|
||
test('should decode serialized vector', () { | ||
final vector = Vector.fromJson(json); | ||
|
||
expect(vector, iterableAlmostEqualTo(data, 1e-3)); | ||
expect(vector.dtype, dtype); | ||
}); | ||
|
||
test('should throw an error if unknown decoded value is passed', () { | ||
final actual = () => Vector.fromJson(unknownJson); | ||
|
||
expect(actual, throwsUnsupportedError); | ||
}); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
test/integration_test/vector/methods/to_json/to_json_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:ml_linalg/dtype.dart'; | ||
|
||
import 'to_json_test_group_factory.dart'; | ||
|
||
void main() { | ||
vectorToJsonTestGroupFactory(DType.float32); | ||
vectorToJsonTestGroupFactory(DType.float64); | ||
} |
24 changes: 24 additions & 0 deletions
24
test/integration_test/vector/methods/to_json/to_json_test_group_factory.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:ml_linalg/dtype.dart'; | ||
import 'package:ml_linalg/dtype_to_json.dart'; | ||
import 'package:ml_linalg/src/vector/vector_json_keys.dart'; | ||
import 'package:ml_linalg/vector.dart'; | ||
import 'package:ml_tech/unit_testing/matchers/iterable_almost_equal_to.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../../../dtype_to_title.dart'; | ||
|
||
void vectorToJsonTestGroupFactory(DType dtype) => | ||
group(dtypeToVectorTestTitle[dtype], () { | ||
group('toJson method', () { | ||
final data = <double>[100, 129, 3.555, 5.07, -600, 17, 84]; | ||
final vector = Vector.fromList(data, dtype: dtype); | ||
|
||
test('should return a json-serializable map', () { | ||
final encoded = vector.toJson(); | ||
|
||
expect(encoded[vectorDTypeJsonKey], dTypeToJson(dtype)); | ||
expect(encoded[vectorDataJsonKey], iterableAlmostEqualTo(data)); | ||
expect(encoded.keys, hasLength(2)); | ||
}); | ||
}); | ||
}); |