Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Sep 14, 2022
1 parent 08ec9bf commit d1b6fe3
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ example/ios/Flutter/flutter_export_environment.sh
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

# Coverage
/coverage/
/coverage/
34 changes: 17 additions & 17 deletions lib/src/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ import 'package:collection/collection.dart';

import 'utils/list_base.dart';

typedef Mask = List<bool>;

extension MaskExtensions on Mask {
Mask operator &(Mask other) =>
IterableZip([this, other]).map((e) => e.first && e.last).toList();

Mask operator |(Mask other) =>
IterableZip([this, other]).map((e) => e.first | e.last).toList();

Mask operator ^(Mask other) =>
IterableZip([this, other]).map((e) => e.first ^ e.last).toList();
}

class Column<E> extends ListBase<E> {
Column(List<E> records) : super(records);

Expand All @@ -27,7 +14,7 @@ class Column<E> extends ListBase<E> {

/// Count number of occurrences of values, corresponding to the column [colName],
/// equaling any element contained by [pool].
int countElementOccurrencesOf(Set<E?> pool) =>
int countElementOccurrencesOf(Set<E> pool) =>
where((element) => pool.contains(element)).length;

// ************* null freeing *************
Expand All @@ -41,7 +28,7 @@ class Column<E> extends ListBase<E> {

// ****************** transformation ******************

List<num> cumSum() => _nullFreedNums().fold(
List<num> cumSum({bool treatNullsAsZeros = true}) => _nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).fold(
[],
(sums, element) =>
sums..add(sums.isEmpty ? element : sums.last + element));
Expand All @@ -58,7 +45,7 @@ class Column<E> extends ListBase<E> {
num sum() => _nullFreedNums().sum;

Iterable<num> _nullFreedNums({bool treatNullsAsZeros = false}) => cast<num?>()
.nullFreedIterable(replaceWith: treatNullsAsZeros ? 0.0 : null)
.nullFreedIterable(replaceWith: treatNullsAsZeros ? 0 : null)
.cast<num>();

// ***************** masks *******************
Expand All @@ -75,7 +62,7 @@ class Column<E> extends ListBase<E> {
Mask isNotIn(Set<E> pool) =>
map((element) => !pool.contains(element)).toList().cast<bool>();

Mask toMask(bool Function(E) test) => map(test).toList().cast<bool>();
Mask maskFrom(bool Function(E) test) => map(test).toList().cast<bool>();

// ****************** numerical column masks *********************

Expand All @@ -91,3 +78,16 @@ class Column<E> extends ListBase<E> {
Mask operator >=(num reference) =>
cast<num>().map((element) => element >= reference).toList().cast<bool>();
}

typedef Mask = List<bool>;

extension MaskExtensions on Mask {
Mask operator &(Mask other) =>
IterableZip([this, other]).map((e) => e.first && e.last).toList();

Mask operator |(Mask other) =>
IterableZip([this, other]).map((e) => e.first | e.last).toList();

Mask operator ^(Mask other) =>
IterableZip([this, other]).map((e) => e.first ^ e.last).toList();
}
25 changes: 10 additions & 15 deletions lib/src/dataframe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DataFrame extends ListBase<RecordRow> {

/// Build a dataframe from specified [columnNames] and [data].
/// The [data] is expected to be of the shape (rows x columns).
DataFrame.fromNamesAndData(List<String> columnNames, DataMatrix data)
DataFrame(List<String> columnNames, DataMatrix data)
: this._columnNames = ElementPositionTrackingList(columnNames),
super(data) {
if (data.isEmpty) {
Expand Down Expand Up @@ -141,7 +141,7 @@ class DataFrame extends ListBase<RecordRow> {
}

// instantiate DataFrame
final df = DataFrame.fromNamesAndData(columnNames, fields);
final df = DataFrame(columnNames, fields);

// skip columns if required
if (skipColumns != null) {
Expand Down Expand Up @@ -234,23 +234,18 @@ class DataFrame extends ListBase<RecordRow> {

/// Enables (typed) column access.
///
/// If [start] and/or [end] are specified the column will be sliced respectively,
/// after which [includeRecord], if specified, may determine which elements are to be included.
Column<T> call<T>(String colName,
{int start = 0, int? end, bool Function(T)? includeRecord}) {
Iterable<T> column =
sublist(start, end).map((row) => row[columnIndex(colName)]).cast<T>();
if (includeRecord != null) {
column = column.where(includeRecord);
}
return Column(column.toList());
}
/// If [start] and/or [end] are specified, the column will be sliced respectively.
Column<T> call<T>(String colName, {int start = 0, int? end}) =>
Column(columnIterable<T>(colName, start: start, end: end).toList());

Iterable<T> columnIterable<T>(String colName, {int start = 0, int? end}) =>
sublist(start, end).map((row) => row[columnIndex(colName)]).cast<T>();

DataFrame fromColumns(List<String> columnNames) => DataFrame._copied(
DataFrame withColumns(List<String> columnNames) => DataFrame._copied(
ElementPositionTrackingList(columnNames),
columnNames.map((e) => this(e)).transposed());

/// Returns an iterable over the column data.
/// Returns an iterable over the columns.
Iterable<Column> columns() => _columnNames.map((e) => this(e));

/// Grab a (typed) record sitting at dataframe[rowIndex][colName].
Expand Down
Empty file added test/output/.gitkeep
Empty file.
3 changes: 0 additions & 3 deletions test/output/out.csv

This file was deleted.

3 changes: 0 additions & 3 deletions test/output/out1.csv

This file was deleted.

3 changes: 0 additions & 3 deletions test/output/out3.csv

This file was deleted.

2 changes: 0 additions & 2 deletions test/output/out4.csv

This file was deleted.

52 changes: 52 additions & 0 deletions test/src/column_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:koala/koala.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main(){
final column = Column([1, 3, 7, null, 2, 0, 87, 34, 3, null]);

test('counting', (){
expect(column.count(3), 2);
expect(column.countElementOccurrencesOf({3, 87, 69}), 3);
});

test('null freeing', (){
expect(column.nullFreed(), Column([1, 3, 7, 2, 0, 87, 34, 3]));
expect(column.nullFreed(replaceWith: 420), Column([1, 3, 7, 420, 2, 0, 87, 34, 3, 420]));
});

test('.cumSum', (){
expect(column.cumSum(treatNullsAsZeros: true), Column([1, 4, 11, 11, 13, 13, 100, 134, 137, 137]));
expect(column.cumSum(treatNullsAsZeros: false), Column([1, 4, 11, 13, 13, 100, 134, 137]));
});

test('accumulation', (){
expect(column.sum(), 137);
expect(column.min(), 0);
expect(column.max(), 87);
expect(column.mean(treatNullsAsZeros: false), 17.125);
expect(column.mean(treatNullsAsZeros: true), 13.7);
});

test('masks', (){
expect(column.equals(3), [false, true, false, false, false, false, false, false, true, false]);
expect(column.unequals(3), [true, false, true, true, true, true, true, true, false, true]);
expect(column.isIn({2, 7, 87}), [false, false, true, false, true, false, true, false, false, false]);
expect(column.isNotIn({2, 7, 87}), [true, true, false, true, false, true, false, true, true, true]);
expect(column.maskFrom((p0) => p0 == null ? true : p0.isEven), [false, false, false, true, true, true, false, true, false, true]);

final nullFreed = column.nullFreed(replaceWith: 97);
expect(nullFreed > 20, [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed < 20, [true, true, true, false, true, true, false, false, true, false]);
expect(nullFreed >= 20, [false, false, false, true, false, false, true, true, false, true]);
expect(nullFreed <= 20, [true, true, true, false, true, true, false, false, true, false]);
});

test('mask concatenations', (){
final mask = [true, true, false];
final referenceMask = [true, false, false];
expect(mask & referenceMask, [true, false, false]);
expect(mask | referenceMask, [true, true, false]);
expect(mask ^ referenceMask, [false, true, false]);
});
}
Loading

0 comments on commit d1b6fe3

Please sign in to comment.