Function Repository Resource:

TileBins

Source Notebook

Bin data into rectangular tiles

Contributed by: Anton Antonov

ResourceFunction["TileBins"][mat,s]

bins a numerical full 2D array mat into square grid tiles with size s.

ResourceFunction["TileBins"][mat,{hx,hy}]

bins a numerical full 2D array mat into rectangular grid tiles with x-y sizes {hx,hy}.

ResourceFunction["TileBins"][cv,s]

bins coordinates-to-value rules cv.

Details and Options

ResourceFunction["TileBins"] bins data into rectangular tiles.
The result is an association with keys that are polygon objects.
ResourceFunction["TileBins"] accepts the following options:
"AggregationFunction"Totalfunction to aggregate the data in each tile
"PolygonKeys"Truedetermine whether the result keys are polygons
If the option "PolygonKeys" is set to False, then the keys of the result are rectangle centers.
The option "AggregationFunction" is used to aggregate the values within each tile only when coordinates-to-value rules are supplied.

Examples

Basic Examples (5) 

Create a list of random 2D points:

In[1]:=
SeedRandom[113];
data = RandomVariate[
   MultinormalDistribution[{10, 10}, 7*IdentityMatrix[2]], 200];

Bin the points:

In[2]:=
ResourceFunction["TileBins"][data, 10]
Out[2]=

Bin the points and get the tile centers as keys:

In[3]:=
ResourceFunction["TileBins"][data, 10, "PolygonKeys" -> False]
Out[3]=

Plot points and square bins:

In[4]:=
Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3]], Keys@ResourceFunction["TileBins"][data, 10], Red, Point[data]}]
Out[4]=

Plot points and rectangle bins:

In[5]:=
Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3]], Keys@ResourceFunction["TileBins"][data, {10, 5}], Red, Point[data]}]
Out[5]=

Scope (4) 

Associate random values to 2D data:

In[6]:=
SeedRandom[113];
data = RandomVariate[
  MultinormalDistribution[{10, 10}, 7*IdentityMatrix[2]], 200]; dataRules = AssociationThread[data, RandomReal[{0, 1}, Length[data]]];

Bin the data rules:

In[7]:=
ResourceFunction["TileBins"][dataRules, 10]
Out[7]=

Bin the keys of the data rules:

In[8]:=
ResourceFunction["TileBins"][Keys@dataRules, 10]
Out[8]=

Check the equivalence:

In[9]:=
ResourceFunction["TileBins"][Keys@dataRules, 10] == ResourceFunction["TileBins"][data, 10]
Out[9]=

Options (3) 

AggregationFunction (2) 

Bin the data rules and compute average values on each tile:

In[10]:=
SeedRandom[113];
data = RandomVariate[
  MultinormalDistribution[{10, 10}, 7*IdentityMatrix[2]], 200]; dataRules = AssociationThread[data, RandomReal[{0, 1}, Length[data]]];
ResourceFunction["TileBins"][dataRules, 6, "AggregationFunction" -> Mean]
Out[10]=

Compare with the default option value, Total:

In[11]:=
ResourceFunction["TileBins"][dataRules, 6, "AggregationFunction" -> Total]
Out[11]=

PolygonKeys (1) 

Instead of polygons as keys, you can get the centers of the polygons:

In[12]:=
SeedRandom[113];
data = RandomVariate[
  MultinormalDistribution[{10, 10}, 7*IdentityMatrix[2]], 200]; dataRules = AssociationThread[data, RandomReal[{0, 1}, Length[data]]]; ResourceFunction[
 "TileBins"][dataRules, 10, "PolygonKeys" -> False]
Out[12]=

Applications (6) 

Data visualization (4) 

Generate data rules:

In[13]:=
SeedRandom[113];
data2 = RandomVariate[
  MultinormalDistribution[{10, 10}, 7*IdentityMatrix[2]], 300]; dataRules2 = AssociationThread[data2, RandomReal[{0, 100}, Length[data2]]];

Here we bin the data:

In[14]:=
res2 = ResourceFunction["TileBins"][dataRules2, 2];

Plot points and square bins by coloring each square according to its aggregated value:

In[15]:=
Graphics[{KeyValueMap[{FaceForm[
      Opacity[Rescale[#2, MinMax[Values[res2]], {0, 1}], Blue]], #1} &, res2], Red, Point[Keys[dataRules2]]}, Frame -> True]
Out[15]=

Repeat the visualization above with rectangular tiles instead of square ones:

In[16]:=
res3 = ResourceFunction["TileBins"][
  dataRules2, {4, 2}]; Graphics[{KeyValueMap[{FaceForm[
      Opacity[Rescale[#2, MinMax[Values[res3]], {0, 1}], Blue]], #1} &, res3], Red, Point[Keys[dataRules2]]}, Frame -> True]
Out[16]=

Make a rectangular grid (2) 

You can make rectangular grids:

In[17]:=
tiles = Keys@
   ResourceFunction["TileBins"][
    Flatten[Table[{x, y}, {x, 0, 8}, {y, 0, 5}], 1], {2, 1}];
Graphics[{EdgeForm[Blue], FaceForm[None], tiles}]
Out[17]=

The coordinates of the polygons can be retrieved and transformed further:

In[18]:=
Mean@*PolygonCoordinates /@ tiles
Out[18]=

Properties and Relations (3) 

The repository function HextileBins has similar design and implementation as TileBins. Here are comparison examples:

In[19]:=
GraphicsGrid[{{Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3]], Keys@ResourceFunction["HextileBins"][data, 5], Red, Point[data]},
     PlotLabel -> "HextileBins"], Graphics[{EdgeForm[Black], FaceForm[Opacity[0.3]], Keys@ResourceFunction["TileBins"][data, 5], Red, Point[data]}, PlotLabel -> "TileBins"]}}]
Out[19]=

The function GeoHistogram produces similar plots (in Version 12.1):

In[20]:=
geoData = EntityValue[CountryData["Japan", "LargestCities"], "Position"];
In[21]:=
gr1 = GeoHistogram[geoData, GeoProjection -> "Equirectangular", Frame -> True, PlotLabel -> "GeoHistogram", ImageSize -> 250];
In[22]:=
geoRes = ResourceFunction["TileBins"][Reverse[#[[1]]] & /@ geoData, 0.8];
gr2 = Graphics[{KeyValueMap[{FaceForm[
        Blend[{Lighter[Lighter[Yellow]], Brown}, Rescale[#2, MinMax[Values[geoRes]], {0, 1}]]], #1} &, geoRes]}, Frame -> True, Prolog -> {LightBlue, Rectangle[Scaled[{0, 0}], Scaled[{1, 1}]]},
   PlotLabel -> "TileBins", ImageSize -> 250];
In[23]:=
Row[{gr1, Spacer[10], gr2}]
Out[23]=

The function GridGraph can be also used to make rectangular grids:

In[24]:=
grGrid = GridGraph[{4, 5}]
Out[24]=

Here are the corresponding coordinates:

In[25]:=
GraphEmbedding[grGrid]
Out[25]=

Here are the corresponding polygons:

In[26]:=
grGridPolygons = Map[Polygon@GraphEmbedding[grGrid][[(List @@@ #)[[All, 1]]]] &, FindCycle[grGrid, {4, 4}, All]];
Graphics[{EdgeForm[Blue], FaceForm[Opacity[0.2]], grGridPolygons}]
Out[26]=

Make a similar grid with TileBins:

In[27]:=
tiles2 = Keys[
   ResourceFunction["TileBins"][
    Flatten[Table[{x, y}, {x, 0, 6}, {y, 0, 4}], 1], 2]];
Graphics[{EdgeForm[Blue], FaceForm[Opacity[0.2]], tiles2}]
Out[27]=

Here are the corresponding coordinates:

In[28]:=
Union[Join @@ Map[PolygonCoordinates, tiles2]]
Out[28]=

Neat Examples (1) 

Bin data and summarize the values associated to bin centers:

In[29]:=
SeedRandom[113];
data3 = RandomVariate[
  MultinormalDistribution[{10, 10}, {1, 1.7}*IdentityMatrix[2]], 300]; dataRules3 = AssociationThread[data3, RandomReal[{0, 100}, Length[data3]]];
In[30]:=
res = ResourceFunction["TileBins"][dataRules3, 10, "PolygonKeys" -> False, "AggregationFunction" -> ResourceFunction["RecordsSummary"]]
Out[30]=

Publisher

Anton Antonov

Version History

  • 1.0.0 – 05 May 2020

Related Resources

License Information