Skip to content

Commit

Permalink
Format; Elaborate Makefile; Edit README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Sep 14, 2022
1 parent dd357ee commit 3e2f2f4
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 124 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ jobs:
run: dart pub get

- name: Run tests
run: |
make test-with-coverage
run: make test

- name: Upload coverage to codecov
run: |
Expand Down
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,5 @@ example/ios/Flutter/flutter_export_environment.sh
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

# Coverage report
test/.test_coverage.dart
# Coverage
/coverage/
/coverage-html/

# Test output
/test/output/
3 changes: 1 addition & 2 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/test/
Makefile
/coverage/
/coverage-html/
/coverage/
26 changes: 22 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
SHELL=/bin/bash

test-with-coverage:
# $$$$$$$$$ Testing $$$$$$$$$$

test-and-show-coverage: test coverage-html

run-tests:
dart pub global activate coverage
dart pub global run coverage:test_with_coverage

coverage:
genhtml coverage/lcov.info -o coverage-html
firefox coverage-html/index.html
coverage-html:
genhtml coverage/lcov.info -o coverage/html
firefox coverage/html/index.html

# $$$$$$$$$ Releasing $$$$$$$$$$

pre-release:
dart format lib
dart analyze
dart pub publish --dry-run

patch-version:
dart pub global activate pubversion
pubversion patch

release:
dart pub publish
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final fromCsv = Dataframe.fromCsv(
path: 'path/to/file.csv',
eolToken: '\n',
maxRows: 40,
skipColumns: ['date'],
skipColumns: ['some-irrelevant-column'],
convertDates: true,
datePattern: 'dd-MM-yyyy'
);
Expand Down
59 changes: 26 additions & 33 deletions lib/src/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@ import 'list_extensions/extended_list_base.dart';

typedef Mask = List<bool>;

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

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

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

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

Column<T> cast<T>() =>
Column(super.cast<T>());
Column<T> cast<T>() => Column(super.cast<T>());

// ************* count *****************

/// Count number of occurrences of [element] of the column [colName].
int count(E object) =>
where((element) => element == object).length;
int count(E object) => where((element) => element == object).length;

/// Count number of occurrences of values, corresponding to the column [colName],
/// equaling any element contained by [pool].
Expand All @@ -37,35 +35,31 @@ class Column<E> extends ExtendedListBase<E>{
Column<E> nullFreed({E? replaceWith = null}) =>
Column<E>(nullFreedIterable(replaceWith: replaceWith).toList());

Iterable<E> nullFreedIterable({E? replaceWith = null}) =>
replaceWith == null
? where((element) => element != null)
: map((e) => e ?? replaceWith);
Iterable<E> nullFreedIterable({E? replaceWith = null}) => replaceWith == null
? where((element) => element != null)
: map((e) => e ?? replaceWith);

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

List<num> cumSum() => _nullFreedNums().fold(
[],
(sums, element) =>
sums..add(sums.isEmpty ? element : sums.last + element));
(sums, element) =>
sums..add(sums.isEmpty ? element : sums.last + element));

// **************** accumulation ****************

double mean({bool treatNullsAsZeros = true}) =>
_nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).average;
_nullFreedNums(treatNullsAsZeros: treatNullsAsZeros).average;

num max() =>
_nullFreedNums().max;
num max() => _nullFreedNums().max;

num min() =>
_nullFreedNums().min;
num min() => _nullFreedNums().min;

num sum() =>
_nullFreedNums().sum;
num sum() => _nullFreedNums().sum;

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

// ***************** masks *******************

Expand All @@ -81,20 +75,19 @@ class Column<E> extends ExtendedListBase<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 toMask(bool Function(E) test) => map(test).toList().cast<bool>();

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

Mask operator<(num reference) =>
Mask operator <(num reference) =>
cast<num>().map((element) => element < reference).toList().cast<bool>();

Mask operator>(num reference) =>
Mask operator >(num reference) =>
cast<num>().map((element) => element > reference).toList().cast<bool>();

Mask operator<=(num reference) =>
Mask operator <=(num reference) =>
cast<num>().map((element) => element <= reference).toList().cast<bool>();

Mask operator>=(num reference) =>
Mask operator >=(num reference) =>
cast<num>().map((element) => element >= reference).toList().cast<bool>();
}
}
17 changes: 7 additions & 10 deletions lib/src/dataframe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,12 @@ class DataFrame extends ExtendedListBase<RecordRow> {
return Column(column.toList());
}

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

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

/// Grab a (typed) record sitting at dataframe[rowIndex][colName].
T record<T>(int rowIndex, String colName) =>
Expand All @@ -263,7 +260,7 @@ class DataFrame extends ExtendedListBase<RecordRow> {
DataFrame._copied(_columnNames, indices.map((e) => this[e]).toList());

DataFrame rowsWhere(List<bool> mask) =>
DataFrame._copied(_columnNames, applyMask(mask).toList());
DataFrame._copied(_columnNames, applyMask(mask).toList());

// **************** manipulation ******************

Expand Down Expand Up @@ -355,7 +352,7 @@ class DataFrame extends ExtendedListBase<RecordRow> {
DataFrame sortedBy(String colName,
{bool ascending = true,
bool nullsFirst = true,
Comparator<Record>? compareRecords}) =>
Comparator<Record>? compareRecords}) =>
DataFrame._copied(
_columnNames,
_sort(colName,
Expand Down Expand Up @@ -448,4 +445,4 @@ class DataFrame extends ExtendedListBase<RecordRow> {
.join(consecutiveElementDelimiter))
]).map((e) => e.join(indexColumnDelimiter)).join('\n');
}
}
}
2 changes: 1 addition & 1 deletion lib/src/utils/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension IterableExtensions<E> on Iterable<E> {
whereIndexed((index, _) => mask[index]);
}

extension IterableIterableExtensions<E> on Iterable<Iterable<E>>{
extension IterableIterableExtensions<E> on Iterable<Iterable<E>> {
List<List<E>> transposed() =>
IterableZip(this).map((e) => e.toList()).toList();
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: koala
description: A poor man's version of a pandas Dataframe. Collect, access & manipulate related data.
homepage: https://github.com/w2sv/koala
repository: https://github.com/w2sv/koala
version: 0.0.3

environment:
Expand All @@ -13,4 +13,4 @@ dependencies:

dev_dependencies:
coverage: ^1.6.0
test: ^1.21.5
test: ^1.21.5
Loading

0 comments on commit 3e2f2f4

Please sign in to comment.