summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2017-08-27 12:22:20 +0300
committerGitHub <noreply@github.com>2017-08-27 12:22:20 +0300
commit7922f6e6a55f0aa4ddbf55089131a2981be4a81b (patch)
treecbc3f48cc2669051662781324902f076eabbf806
parent(no commit message) (diff)
downloadheatmap-7922f6e6a55f0aa4ddbf55089131a2981be4a81b.tar.gz
heatmap-7922f6e6a55f0aa4ddbf55089131a2981be4a81b.zip
Diffstat (limited to '')
-rw-r--r--ascii-town-heatmap.rs66
1 files changed, 48 insertions, 18 deletions
diff --git a/ascii-town-heatmap.rs b/ascii-town-heatmap.rs
index 83795ea..23b80da 100644
--- a/ascii-town-heatmap.rs
+++ b/ascii-town-heatmap.rs
@@ -6,6 +6,7 @@ extern crate serde;
 #[macro_use]
 extern crate serde_derive;
 
+use std::collections::BTreeSet;
 use std::io;
 
 #[derive(Debug, Deserialize)]
@@ -20,43 +21,72 @@ struct Tile {
     access_time: u64
 }
 
+// Torus size.
 const W: u32 = 512;
 
+// Tile width/height in pixels.
+const TW: u32 = 4;
+const TH: u32 = 1;
+
 fn main() {
+    // Read the `torus.csv` into a 2D array of `(access, modify)`.
+    // Also track the values we see in `BTreeSet` (for ordering).
+    let mut tiles = [[(0, 0); W as usize]; W as usize];
+    let mut sa = BTreeSet::new();
+    let mut sm = BTreeSet::new();
     let mut rdr = csv::Reader::from_reader(io::stdin());
-    let mut ma = 0.0f32;
-    let mut mm = 0.0f32;
-    let mut tiles = [[(0.0f32, 0.0f32); W as usize]; W as usize];
     for tile in rdr.deserialize().map(Result::<Tile, _>::unwrap) {
-        let a = tile.access_count as f32;
-        let m = tile.modify_count as f32;
+        let a = tile.access_count;
+        let m = tile.modify_count;
+
+        // HACK(eddyb) ignore 0,0 for analysis - too busy.
         if (tile.tile_x, tile.tile_y) != (0, 0) {
-            ma = ma.max(a);
-            mm = mm.max(m);
+            sa.insert(a);
+            sm.insert(m);
         }
+
         tiles[tile.tile_y][tile.tile_x] = (a, m);
     }
 
-    let mut heatmap = image::ImageBuffer::new(W * 4, W);
-    let green = hsl::HSL::from_rgb(&[0, 255, 0]).h;
+    // Extract the maximum values.
+    // TODO(eddyb) chunk values for better representation.
+    let ma = sa.iter().next_back().cloned().unwrap_or(0) as f64;
+    let mm = sm.iter().next_back().cloned().unwrap_or(0) as f64;
+
+    // Compose the heatmap image in-memory from the 2D array.
+    let mut heatmap = image::ImageBuffer::new(W * TW, W * TH);
     let red = hsl::HSL::from_rgb(&[255, 0, 0]).h;
+    // let green = hsl::HSL::from_rgb(&[0, 255, 0]).h;
+    // let blue = hsl::HSL::from_rgb(&[0, 0, 255]).h;
     for y in 0..W {
         for x in 0..W {
             let (a, m) = tiles[y as usize][x as usize];
-            let a = (a / ma).min(1.0).powf(0.2) as f64;
-            let m = (m / mm).min(1.0).powf(0.2) as f64;
+            let a = (a as f64 / ma).min(1.0).powf(0.1);
+            let m = (m as f64 / mm).min(1.0).powf(0.1);
 
-            let l = a.max(m) * 0.5;
-            let h = green * (1.0 - m) + red * m;
-            let (r, g, b) = hsl::HSL { h, s: 1.0, l }.to_rgb();
+            // access => luminosity, modify => hue (green -> red)
+            // let h = green * (1.0 - m) + red * m;
+            // let s = 1.0;
+            // let l = a.max(m) * 0.5;
+
+            // access => luminosity, modify => saturation (grey -> red)
+            let h = red;
+            let s = m;
+            let l = a * 0.5;
+
+            let (r, g, b) = hsl::HSL { h, s, l }.to_rgb();
             let rgb = image::Rgb([r, g, b]);
 
-            let y = (y + W / 2) % W;
-            for dx in 0..4 {
-                let x = (x * 4 + dx + (W - 1) * 2) % (W * 4);
-                heatmap.put_pixel(x, y, rgb);
+            for dy in 0..TH {
+                let y = ((y * 2 + W + 1) * TH / 2 + dy) % (W * TH);
+                for dx in 0..TW {
+                    let x = ((x * 2 + W + 1) * TW / 2 + dx) % (W * TW);
+                    heatmap.put_pixel(x, y, rgb);
+                }
             }
         }
     }
+
+    // Save the heatmap image.
     heatmap.save("heatmap.png").unwrap();
 }