Skip to content

Commit

Permalink
Merge pull request #893 from shawnlaffan/issue_820_divergent_colours
Browse files Browse the repository at this point in the history
Support divergent and ratio colouring, as well as inversion of colour schemes
  • Loading branch information
shawnlaffan committed Dec 4, 2023
2 parents 670c6df + 288e035 commit 3de625a
Show file tree
Hide file tree
Showing 15 changed files with 663 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
which cpanm
- name: Install GDAL and its deps
run: brew install gdal
run: brew install --force --overwrite gdal

- name: perl -V
run: perl -V
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ biodiverse.iml
/xx.bps
/biodiverseb.iml
/.idea
/AGS_bin
9 changes: 9 additions & 0 deletions bin/ui/hboxClusteringPage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ Uses the min and max determined by the Colour stretch choice.</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="menu_dendro_colour_stretch_flip_mode">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Invert (flip) the colour range. Has no effect on categorical colouring.</property>
<property name="label" translatable="yes">Invert colour stretch</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menu_item_dendro_colour_stretch">
<property name="visible">True</property>
Expand Down
21 changes: 21 additions & 0 deletions bin/ui/hboxSpatialPage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="menu_colour_stretch_flip_mode">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="tooltip_text" translatable="yes">Invert (flip) the colour range. Has no effect on categorical colouring.</property>
<property name="label" translatable="yes">Invert colour stretch</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menu_item_colour_stretch">
<property name="visible">True</property>
Expand Down Expand Up @@ -490,10 +499,22 @@ Colours are scaled using percentiles defined in the colour stretch menu. </prope
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Log scale</property>
<property name="tooltip_text" translatable="yes">Log scale the colours.
Uses the min and max determined by the Colour stretch choice.</property>
<property name="use_underline">True</property>
<property name="active">True</property>
</object>
</child>
<child>
<object class="GtkCheckMenuItem" id="menuitem_spatial_tree_colour_stretch_flip_mode">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Invert colour stretch</property>
<property name="tooltip_text" translatable="yes">Invert (flip) the colour range. Has no effect on categorical colouring.</property>
<property name="use_underline">True</property>
<property name="active">False</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem_spatial_tree_colour_mode">
<property name="visible">True</property>
Expand Down
62 changes: 54 additions & 8 deletions lib/Biodiverse/GUI/Dendrogram.pm
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,19 @@ sub recolour_cluster_elements {

my $cluster_colour_mode = $self->get_cluster_colour_mode();
my $colour_callback;


my %list_and_index = (list => $list_name, index => $list_index);
my $is_canape = $list_name =~ />>CANAPE>>/ && $list_index =~ /CANAPE/;
my $is_zscore = eval {
$parent_tab->index_is_zscore(list => $list_name, index => $list_index);
$parent_tab->index_is_zscore(%list_and_index);
};
my $is_prank = $list_name =~ />>p_rank>>/;
my $is_ratio
= !$is_prank && !$is_zscore
&& eval {$parent_tab->index_is_ratio(%list_and_index)};
my $is_divergent
= !$is_prank && !$is_zscore && !$is_ratio
&& eval {$parent_tab->index_is_divergent(%list_and_index)};

if ($cluster_colour_mode eq 'palette') {
# sets colours according to palette
Expand Down Expand Up @@ -917,6 +924,18 @@ sub recolour_cluster_elements {
};
}
elsif ($cluster_colour_mode eq 'list-values') {
my $abs_extreme;
if ($is_ratio) {
$abs_extreme = exp (max (abs log $analysis_min, log $analysis_max));
$analysis_min = 1 / $abs_extreme;
$analysis_max = $abs_extreme;
}
elsif ($is_divergent) { # assumes zero - needs work
$abs_extreme = max(abs $analysis_min, abs $analysis_max);
$analysis_min = 0;
$analysis_max = $abs_extreme;
}

# sets colours according to (usually spatial)
# list value for the element's cluster
$colour_callback = sub {
Expand All @@ -936,6 +955,8 @@ sub recolour_cluster_elements {
return $is_canape ? $map->get_colour_canape ($val)
: $is_zscore ? $map->get_colour_zscore ($val)
: $is_prank ? $map->get_colour_prank ($val)
: $is_ratio ? $map->get_colour_ratio ($val, $abs_extreme)
: $is_divergent ? $map->get_colour_divergent ($val, 0, $abs_extreme)
: $map->get_colour($val, $analysis_min, $analysis_max);
}
else {
Expand All @@ -956,6 +977,8 @@ sub recolour_cluster_elements {
$map->get_legend->set_canape_mode($is_canape);
$map->get_legend->set_zscore_mode($is_zscore);
$map->get_legend->set_prank_mode($is_prank);
$map->get_legend->set_ratio_mode($is_ratio);
$map->get_legend->set_divergent_mode($is_divergent);

if ($cluster_colour_mode eq 'list-values') {
$map->set_legend_min_max($analysis_min, $analysis_max);
Expand Down Expand Up @@ -1235,12 +1258,32 @@ sub recolour_cluster_lines {
my $analysis_min = $self->{analysis_min};
my $analysis_max = $self->{analysis_max};
my $colour_mode = $self->get_cluster_colour_mode();


my %list_and_index = (list => $list_name, index => $list_index);
my $is_canape = $list_name =~ />>CANAPE>>/ && $list_index =~ /^CANAPE/;
my $is_zscore = eval {
$self->{parent_tab}->index_is_zscore (list => $list_name, index => $list_index);
$self->{parent_tab}->index_is_zscore (%list_and_index);
};
my $is_prank = $list_name =~ />>p_rank>>/;
my $is_ratio
= !$is_prank && !$is_zscore
&& eval {$self->{parent_tab}->index_is_ratio(%list_and_index)};
my $is_divergent
= !$is_prank && !$is_zscore && !$is_ratio
&& eval {$self->{parent_tab}->index_is_divergent(%list_and_index)};

my $abs_extreme;
if ($is_ratio) {
$abs_extreme = exp (max (abs log $analysis_min, log $analysis_max));
$analysis_min = 1 / $abs_extreme;
$analysis_max = $abs_extreme;
}
elsif ($is_divergent) { # assumes zero - needs work
$abs_extreme = max(abs $analysis_min, abs $analysis_max);
$analysis_min = 0;
$analysis_max = $abs_extreme;
}


foreach my $node_ref (@$cluster_nodes) {

Expand All @@ -1264,10 +1307,12 @@ sub recolour_cluster_lines {
: undef; # allows for missing lists

$colour_ref = defined $val
? ( $is_canape ? $map->get_colour_canape($val)
: $is_zscore ? $map->get_colour_zscore($val)
: $is_prank ? $map->get_colour_prank($val)
: $map->get_colour ($val, $analysis_min, $analysis_max)
? ( $is_canape ? $map->get_colour_canape($val) :
$is_zscore ? $map->get_colour_zscore($val) :
$is_prank ? $map->get_colour_prank($val) :
$is_ratio ? $map->get_colour_ratio ($val, $abs_extreme) :
$is_divergent ? $map->get_colour_divergent ($val, 0, $abs_extreme) :
$map->get_colour ($val, $analysis_min, $analysis_max)
)
: undef;
}
Expand Down Expand Up @@ -3111,6 +3156,7 @@ sub update_legend {
return if !$legend;

if ($self->{width_px} && $self->{height_px}) {
$legend->make_rect;
$legend->reposition($self->{width_px}, $self->{height_px});
}

Expand Down
11 changes: 11 additions & 0 deletions lib/Biodiverse/GUI/Grid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ sub update_legend {

return if !($self->{width_px} && $self->{height_px});

$self->get_legend->make_rect;
$self->get_legend->reposition($self->{width_px}, $self->{height_px});

return;
Expand Down Expand Up @@ -1134,6 +1135,7 @@ sub set_colour_for_undef {
$self->{colour_none} = $colour;
}

# need factory generation for the next few
sub get_colour {
my $self = shift;
return $self->get_legend->get_colour (@_);
Expand All @@ -1154,6 +1156,15 @@ sub get_colour_prank {
return $self->get_legend->get_colour_prank (@_);
}

sub get_colour_ratio {
my $self = shift;
return $self->get_legend->get_colour_ratio (@_);
}

sub get_colour_divergent {
my $self = shift;
return $self->get_legend->get_colour_divergent (@_);
}


##########################################################
Expand Down
Loading

0 comments on commit 3de625a

Please sign in to comment.