question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom getColorValue fct to normalize variable by MAX of ALL hexagon cells

See original GitHub issue

I am creating a custom function to get the color value and elevation value of the hexagon layer. I am facing an issue in calculating data for my custom function.

Let us take this example from documentation, with the added functionality of having a variable radius implemented through options (not shown below):

 class MyHexagonLayer {
    renderLayers() {
      return new HexagonLayer({
        id: 'hexagon-layer',
        getColorValue: myFunction(),
        data,
        radius: 500
      });
    }
 }

My data looks like this: index,lon,lat, Pharmacy, Restaurant, with 1 or 0 as pharmacy,restaurants row values.

Now myFunction() goal is to normalize the number of pharmacy & restaurant in each cell. For example:

  • Hex Cell 1 has 5 pharmacies, 10 restaurants --> total point of interest of 15
  • Hex Cell 2 has 2 pharmacies, 7 restaurants --> total point of interest of 9
  • MAX (ALL HEX CELLS) = MAX (15,9) = 15
  • myFunction() output should be a weighted sum of pharmacy and restaurants in each cell divided by the MAX number of point of interest of all cells (in this case Max =15). This is important because the hex radius is variable, therefore, the MAX of all cells is variable. An example of what the function should return weighted_sum / MAX_All_Hex_Cells:
    • Hex Cell 1: (weight_A x 5+weight_B x 10)/15
    • Hex Cell 2: (weight_A x 2+weight_B x 7)/15

My issue is how do i compute the maximum number of points of ALL HEX CELLS (15 in example above), and not IN each cell. I thought to calculate the sum of all points counts in each hex cell, then find the maximum value of that sum across all cells:

function myFunction(points) {
   //calculating weighted_sum
   let weighted_sum = arraySum(points.map(p => p['Pharmacy']))*weight_A +
              arraySum(points.map(p => p['Restaurant']))*weight_B;
  
 // calculate MAX_All_Hex_Cells
let sumHexPOI = arraySum(points.map(p => p['Pharmacy'])) + 
arraySum(points.map(p => p['Restaurant']));

 let MAX_All_Hex_Cells = Math.max(sumHexPOI); 
//this is not working, since i need to store each of the hex sumHexPOI in an array
 and compute their maximum. 

  return weighted_sum/MAX_All_Hex_Cells;

}

function arraySum(array) {
      return array.reduce((a,b) => a + b, 0);
    }

I can get the sum of points of interest in each hexagon, however how do I store that into an array and do a Math.max to get the maximum?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
Pessimistresscommented, Jun 1, 2020

All hex value should be a value from 0 to 10.

My suggestion is don’t mess those values. Internally they are only used to calculate color. The only place users will see them is in the tooltip. You can normalize the value before displaying it, by listening to onSetColorDomain.

0reactions
alitarrafcommented, Jun 25, 2020

I ended up changing my approach. Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to normalize data between -1 and 1? - Cross Validated
I have seen the min-max normalization formula but that normalizes values between 0 and ...
Read more >
How to Normalize Data Between 0 and 100 - Statology
This tutorial explains how to normalize the values in a dataset to be between the range of 0 and 100.
Read more >
How to Normalize data in R [3 easy methods] - DigitalOcean
Another efficient way of Normalizing values is through the Min-Max Scaling method. With Min-Max Scaling, we scale the data values between a ...
Read more >
Normalizing values depending on group in R - Stack Overflow
I have 49 Cells and the time reaches 664 for each one of them. In this case time is not important, as I'd...
Read more >
Normalize data - MATLAB normalize - MathWorks
This MATLAB function returns the vectorwise z-score of the data in A with center 0 and standard deviation 1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found