Skip to content

Commit

Permalink
Added connection plot to ecolab_model.
Browse files Browse the repository at this point in the history
  • Loading branch information
highperformancecoder committed Jul 5, 2024
1 parent 100b368 commit 72048d8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
14 changes: 12 additions & 2 deletions models/default_ecolab_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ def randomList(num, min, max):
ecolab.migration(nsp*[1e-5])

from plot import plot, penPlot
from GUI import gui, statusBar
from GUI import gui, statusBar, windows

from tkinter import Tk,ttk
from weakref import ref
from ecolab import ecolabHome
connectionPlot=Tk()
connectionPlot.eval('load '+ecolabHome()+'/lib/ecolab.so')
connectionPlot.eval('image create cairoSurface connectionCanvas -surface ecolab_model ecolab.connectionPlot')
ttk.Label(connectionPlot, image='connectionCanvas').pack()
windows.append(ref(connectionPlot))

def step():
ecolab.generate()
Expand All @@ -36,7 +45,8 @@ def step():
statusBar.configure(text=f't={ecolab.tstep()} nsp:{nsp}')
plot('No. species',ecolab.tstep(),nsp)
penPlot('Density',ecolab.tstep(),ecolab.density()._properties, ecolab.species()._properties)

ecolab.connectionPlot.requestRedraw()

gui(step)


Expand Down
63 changes: 63 additions & 0 deletions models/ecolab_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ using array_ns::pcoord;
#include "ecolab_model.h"
#include "pythonBuffer.h"
#include "plot.h"

#include "object.cd"
#include "ecolab_model.cd"

#include "ecolab_epilogue.h"
Expand Down Expand Up @@ -306,3 +308,64 @@ array<double> ecolab_model::lifetimes()
}


bool ConnectionPlot::redraw(int x0, int y0, int width, int height)
{
if (!surface) return false;
const double scale=4;
int low_colour, high_colour;
auto& row=connections.row;
auto& col=connections.col;
ecolab::array<int> enum_clusters(connections.diag.size());
enum_clusters=1;
enum_clusters=enumerate(enum_clusters);
for (unsigned i=0; i<row.size(); i++)
{
high_colour = enum_clusters[col[i]];
low_colour = enum_clusters[row[i]];

if (high_colour<low_colour) std::swap(high_colour,low_colour);
enum_clusters = merge( enum_clusters==high_colour,
low_colour, enum_clusters);
}

/* for grouping species into their ecologies */

ecolab::array<int> map(density.size()), mask;
map=-1;
for (int i=0; i<=max(enum_clusters); i++)
{
mask = enum_clusters==i;
map = merge( mask, enumerate(mask)+max(map)+1, map);
}

auto cairo=surface->cairo();
for (unsigned i=0; i<row.size(); i++)
{
cairo_rectangle(cairo, map[row[i]]*scale, map[col[i]]*scale, scale, scale);
//cairo_stroke_preserve(cairo);
if (density[row[i]]==0||density[col[i]]==0)
cairo_set_source_rgb(cairo, 0xf5/256.0, 0xde/256.0, 0xb3/256.0); // wheat
else
{
auto& colour=palette[enum_clusters[row[i]]%paletteSz];
cairo_set_source_rgb(cairo,colour.r,colour.g,colour.b);
}
cairo_fill(cairo);
}

// diagonals
for (size_t i=0; i<density.size(); i++)
{
cairo_rectangle(cairo, map[i]*scale, map[i]*scale, scale,scale);
if (density[i]==0)
cairo_set_source_rgb(cairo, 0xf5/256.0, 0xde/256.0, 0xb3/256.0); // wheat
else
{
auto& colour=palette[enum_clusters[i]%paletteSz];
cairo_set_source_rgb(cairo,colour.r,colour.g,colour.b);
}
cairo_fill(cairo);
}
return true;
}

14 changes: 13 additions & 1 deletion models/ecolab_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include <netcomplexity.h>

#include <cairoSurfaceImage.h>
using classdesc::Object;

/* ecolab cell */
Expand Down Expand Up @@ -53,6 +53,16 @@ struct model_data: public ecolab_point
sparse_mat_graph foodweb;
};

struct ConnectionPlot: public Object<ConnectionPlot, CairoSurface>
{
const array<int>& density;
const sparse_mat& connections;
ConnectionPlot(const array<int>& density, const sparse_mat& connections):
density(density), connections(connections) {}
bool redraw(int x0, int y0, int width, int height) override;
void requestRedraw() const {if (surface) surface->requestRedraw();}
};

class ecolab_model: public model_data
{
void mutate_model(array<int>);
Expand All @@ -65,6 +75,8 @@ class ecolab_model: public model_data
//mutation parameters
float sp_sep, mut_max, repro_min, repro_max, odiag_min, odiag_max;

ConnectionPlot connectionPlot{density,interaction};

ecolab_model()
{repro_min=0; repro_max=1; mut_max=0; odiag_min=0; odiag_max=1;
tstep=0; last_mut_tstep=0; last_mig_tstep=0;}
Expand Down

0 comments on commit 72048d8

Please sign in to comment.