-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
All groups for output #314
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad41dce
Refactor handling of enums in inputs
jnthn 38977f2
Provide a way to get all possible filter outputs
jnthn 5753f56
Sort by keys
zaucker c0be941
Add --include-all-filters for text output
zaucker 437fbcc
Sort modules on something more useful
jnthn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -21,8 +21,9 @@ class Agrammon::Outputs::FilterGroupCollection { | |
} | ||
|
||
has Numeric %!values-by-filter{FilterKey}; | ||
has Set $.provenance; | ||
|
||
submethod BUILD(:@instances!) { | ||
submethod BUILD(:$!provenance = ∅, :@instances!) { | ||
for @instances { | ||
%!values-by-filter{.key} += .value; | ||
} | ||
|
@@ -35,8 +36,10 @@ class Agrammon::Outputs::FilterGroupCollection { | |
|
||
#| Create from a list of pairs where the key is a hash of filter values for an instance | ||
#| and the value is the instance's value. | ||
method from-filter-to-value-pairs(@instances) { | ||
self.bless: instances => @instances.map({ FilterKey.new(filters => .key) => +.value }) | ||
method from-filter-to-value-pairs(@instances, Set :$provenance = ∅) { | ||
self.bless: | ||
:instances(@instances.map({ FilterKey.new(filters => .key) => +.value })), | ||
:$provenance | ||
} | ||
|
||
#| Check if the collection has any filters. | ||
|
@@ -54,30 +57,57 @@ class Agrammon::Outputs::FilterGroupCollection { | |
self.Numeric.Real | ||
} | ||
|
||
#| Get a list of pairs mapping filter groups into the total value for that group. | ||
method results-by-filter-group() { | ||
[%!values-by-filter.map({ .key.filters => .value })] | ||
#| Get a list of pairs mapping filter groups into the total value for that | ||
#| group. If :all is passed, then all filters that could possibly have been | ||
#| selected will be included, even if no instance used them, and they will | ||
#| have a zero value. | ||
method results-by-filter-group(Bool :$all = False) { | ||
if $all { | ||
if %!values-by-filter && !$!provenance { | ||
die "Can only get all values when provenance of the filter group was provided"; | ||
} | ||
my @results; | ||
for $!provenance.keys.sort(*.module.taxonomy).reverse -> $filter-set { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sort does almost what we need. Will make a separate PR for it, |
||
for $filter-set.all-possible-filter-keys -> %filters { | ||
my $key = FilterKey.new(:%filters); | ||
@results.push(%filters => %!values-by-filter{$key} // 0); | ||
} | ||
} | ||
with %!values-by-filter{FilterKey.empty} { | ||
@results.push({} => $_); | ||
} | ||
@results | ||
} | ||
else { | ||
[%!values-by-filter.map({ .key.filters => .value })] | ||
} | ||
} | ||
|
||
#| Produce a new filter group collection which has the values of this one scaled by | ||
#| the specified factor. This can be used to implement `scalar * group`, `group * scalar` | ||
#| (these two just commute), and `group / scalar` (by passing in `1 / scalar` as the | ||
#| factor). | ||
method scale(Numeric $factor --> Agrammon::Outputs::FilterGroupCollection) { | ||
self.bless: instances => %!values-by-filter.map({ .key => $factor * .value }) | ||
self.bless: | ||
:instances(%!values-by-filter.map({ .key => $factor * .value })), | ||
:$!provenance | ||
} | ||
|
||
#| Produce a new filter group collection which has the values of this one added to | ||
#| a specified additor. This can be used to implement `scalar + group`, `group + scalar` | ||
#| (these two just commute), and `group - scalar` (by passing in `- scalar` as the | ||
#| additor). | ||
method add(Numeric $factor --> Agrammon::Outputs::FilterGroupCollection) { | ||
self.bless: instances => %!values-by-filter.map({ .key => $factor + .value }) | ||
self.bless: | ||
:instances(%!values-by-filter.map({ .key => $factor + .value })), | ||
:$!provenance | ||
} | ||
|
||
#| Produce a new filter group collection which has the sign of this one. | ||
method sign() { | ||
self.bless: instances => %!values-by-filter.map({ .key => sign( .value ) }) | ||
self.bless: | ||
:instances(%!values-by-filter.map({ .key => sign( .value ) })), | ||
:$!provenance | ||
} | ||
|
||
#| Apply an operation pairwise between this group collection and another one, returning a | ||
|
@@ -109,7 +139,9 @@ class Agrammon::Outputs::FilterGroupCollection { | |
} | ||
} | ||
|
||
Agrammon::Outputs::FilterGroupCollection.new(instances => @result-instances) | ||
Agrammon::Outputs::FilterGroupCollection.new: | ||
instances => @result-instances, | ||
provenance => $!provenance ∪ $other.provenance | ||
} | ||
|
||
#| Check in the other filter group collection for values that are greater than a threshold | ||
|
@@ -130,17 +162,18 @@ class Agrammon::Outputs::FilterGroupCollection { | |
else { | ||
# Push 0 if their value > threshold, but key is not existing | ||
# 0 because of missing flow (zero flow) | ||
@result-instances.push: $their-key => 0; | ||
} | ||
} | ||
@result-instances.push: $their-key => 0; | ||
} | ||
} | ||
elsif $push-all { | ||
# Push threshold if their value <= threshold. Could also be 0, | ||
# but it might become handy for limiting ratio to 1, i.e. threshold = 1? | ||
@result-instances.push: $their-key => $thresh; | ||
@result-instances.push: $their-key => $thresh; | ||
} | ||
} | ||
|
||
Agrammon::Outputs::FilterGroupCollection.new(instances => @result-instances) | ||
Agrammon::Outputs::FilterGroupCollection.new: | ||
:instances(@result-instances), :$!provenance | ||
} | ||
|
||
#| Private accessor to get internal representation of another collection. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cute, finally a useful application of the ∅ ... after having being introduced to it in 5th grade almost 50 years ago :-)