// call this externally if data changes public void UpdateData(double?[,] intensities) { Width = intensities.GetLength(1); Height = intensities.GetLength(0); double?[] intensitiesFlattened = intensities.Cast <double?>().ToArray(); Min = double.PositiveInfinity; Max = double.NegativeInfinity; foreach (double?curr in intensitiesFlattened) { if (curr.HasValue && double.IsNaN(curr.Value)) { throw new ArgumentException("Heatmaps do not support intensities of double.NaN"); } if (curr.HasValue && curr.Value < Min) { Min = curr.Value; } if (curr.HasValue && curr.Value > Max) { Max = curr.Value; } } double normalizeMin = (ScaleMin.HasValue && ScaleMin.Value < Min) ? ScaleMin.Value : Min; double normalizeMax = (ScaleMax.HasValue && ScaleMax.Value > Max) ? ScaleMax.Value : Max; if (TransparencyThreshold.HasValue) { TransparencyThreshold = Normalize(TransparencyThreshold.Value, Min, Max, ScaleMin, ScaleMax); } NormalizedIntensities = Normalize(intensitiesFlattened, null, null, ScaleMin, ScaleMax); int[] flatARGB = Colormap.GetRGBAs(NormalizedIntensities, Colormap, minimumIntensity: TransparencyThreshold ?? 0); double?[] normalizedValues = Normalize(Enumerable.Range(0, 256).Select(i => (double?)i).Reverse().ToArray(), null, null, ScaleMin, ScaleMax); int[] scaleRGBA = Colormap.GetRGBAs(normalizedValues, Colormap); BmpHeatmap?.Dispose(); BmpScale?.Dispose(); BmpHeatmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); BmpScale = new Bitmap(1, 256, PixelFormat.Format32bppArgb); Rectangle rect = new Rectangle(0, 0, BmpHeatmap.Width, BmpHeatmap.Height); Rectangle rectScale = new Rectangle(0, 0, BmpScale.Width, BmpScale.Height); BitmapData bmpData = BmpHeatmap.LockBits(rect, ImageLockMode.ReadWrite, BmpHeatmap.PixelFormat); BitmapData scaleBmpData = BmpScale.LockBits(rectScale, ImageLockMode.ReadWrite, BmpScale.PixelFormat); Marshal.Copy(flatARGB, 0, bmpData.Scan0, flatARGB.Length); Marshal.Copy(scaleRGBA, 0, scaleBmpData.Scan0, scaleRGBA.Length); BmpHeatmap.UnlockBits(bmpData); BmpScale.UnlockBits(scaleBmpData); }
public PlottableHeatmap(double[,] intensities, Colormap colormap, string label, double[] axisOffsets, double[] axisMultipliers, double?scaleMin, double?scaleMax, double?transparencyThreshold, Bitmap backgroundImage, bool displayImageAbove, bool drawAxisLabels) { this.width = intensities.GetLength(1); this.height = intensities.GetLength(0); double[] intensitiesFlattened = Flatten(intensities); this.min = intensitiesFlattened.Min(); this.max = intensitiesFlattened.Max(); this.brush = new SolidBrush(Color.Black); this.pen = new Pen(brush); this.axisOffsets = axisOffsets; this.axisMultipliers = axisMultipliers; this.colorMap = colormap; this.label = label; this.scaleMin = scaleMin; this.scaleMax = scaleMax; this.backgroundImage = backgroundImage; this.displayImageAbove = displayImageAbove; this.drawAxisLabels = drawAxisLabels; double normalizeMin = min; double normalizeMax = max; if (scaleMin.HasValue && scaleMin.Value < min) { normalizeMin = scaleMin.Value; } if (scaleMax.HasValue && scaleMax.Value > max) { normalizeMin = scaleMax.Value; } if (transparencyThreshold.HasValue) { this.transparencyThreshold = Normalize(new double[] { transparencyThreshold.Value }, min, max, scaleMin, scaleMax)[0]; } intensitiesNormalized = Normalize(intensitiesFlattened, null, null, scaleMin, scaleMax); int[] flatARGB = Colormap.GetRGBAs(intensitiesNormalized, colormap, minimumIntensity: transparencyThreshold ?? 0); bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); scale = new Bitmap(1, 256, PixelFormat.Format32bppArgb); double[] normalizedValues = Normalize(Enumerable.Range(0, scale.Height).Select(i => (double)i).Reverse().ToArray(), null, null, scaleMin, scaleMax); int[] scaleRGBA = Colormap.GetRGBAs(normalizedValues, colormap); Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Rectangle rectScale = new Rectangle(0, 0, scale.Width, scale.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); BitmapData scaleBmpData = scale.LockBits(rectScale, ImageLockMode.ReadWrite, scale.PixelFormat); Marshal.Copy(flatARGB, 0, bmpData.Scan0, flatARGB.Length); Marshal.Copy(scaleRGBA, 0, scaleBmpData.Scan0, scaleRGBA.Length); bmp.UnlockBits(bmpData); scale.UnlockBits(scaleBmpData); }
// call this externally if data changes public void UpdateData(double[,] intensities) { width = intensities.GetLength(1); height = intensities.GetLength(0); double[] intensitiesFlattened = intensities.Cast <double>().ToArray(); min = intensitiesFlattened.Min(); max = intensitiesFlattened.Max(); double normalizeMin = (ScaleMin.HasValue && ScaleMin.Value < min) ? ScaleMin.Value : min; double normalizeMax = (ScaleMax.HasValue && ScaleMax.Value > max) ? ScaleMax.Value : max; if (TransparencyThreshold.HasValue) { TransparencyThreshold = Normalize(TransparencyThreshold.Value, min, max, ScaleMin, ScaleMax); } NormalizedIntensities = Normalize(intensitiesFlattened, null, null, ScaleMin, ScaleMax); int[] flatARGB = Colormap.GetRGBAs(NormalizedIntensities, Colormap, minimumIntensity: TransparencyThreshold ?? 0); double[] normalizedValues = Normalize(Enumerable.Range(0, 256).Select(i => (double)i).Reverse().ToArray(), null, null, ScaleMin, ScaleMax); int[] scaleRGBA = Colormap.GetRGBAs(normalizedValues, Colormap); BmpHeatmap?.Dispose(); BmpScale?.Dispose(); BmpHeatmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); BmpScale = new Bitmap(1, 256, PixelFormat.Format32bppArgb); Rectangle rect = new Rectangle(0, 0, BmpHeatmap.Width, BmpHeatmap.Height); Rectangle rectScale = new Rectangle(0, 0, BmpScale.Width, BmpScale.Height); BitmapData bmpData = BmpHeatmap.LockBits(rect, ImageLockMode.ReadWrite, BmpHeatmap.PixelFormat); BitmapData scaleBmpData = BmpScale.LockBits(rectScale, ImageLockMode.ReadWrite, BmpScale.PixelFormat); Marshal.Copy(flatARGB, 0, bmpData.Scan0, flatARGB.Length); Marshal.Copy(scaleRGBA, 0, scaleBmpData.Scan0, scaleRGBA.Length); BmpHeatmap.UnlockBits(bmpData); BmpScale.UnlockBits(scaleBmpData); }
public void Update(double?[,] intensities, Colormap colormap = null, double?min = null, double?max = null) { /* This method analyzes the intensities and colormap to create a bitmap * with a single pixel for every intensity value. The bitmap is stored * and displayed (without anti-alias interpolation) when Render() is called. */ Width = intensities.GetLength(1); Height = intensities.GetLength(0); Colormap = colormap ?? Colormap; ScaleMin = min; ScaleMax = max; double?[] intensitiesFlattened = intensities.Cast <double?>().ToArray(); Min = double.PositiveInfinity; Max = double.NegativeInfinity; foreach (double?curr in intensitiesFlattened) { if (curr.HasValue && double.IsNaN(curr.Value)) { throw new ArgumentException("Heatmaps do not support intensities of double.NaN"); } if (curr.HasValue && curr.Value < Min) { Min = curr.Value; } if (curr.HasValue && curr.Value > Max) { Max = curr.Value; } } // labels for colorbar ticks ColorbarMin = (ScaleMin.HasValue && ScaleMin > Min) ? $"≤ {ScaleMin:f3}" : $"{Min:f3}"; ColorbarMax = (ScaleMax.HasValue && ScaleMax < Max) ? $"≥ {ScaleMax:f3}" : $"{Max:f3}"; double normalizeMin = (ScaleMin.HasValue && ScaleMin.Value < Min) ? ScaleMin.Value : Min; double normalizeMax = (ScaleMax.HasValue && ScaleMax.Value > Max) ? ScaleMax.Value : Max; if (TransparencyThreshold.HasValue) { TransparencyThreshold = Normalize(TransparencyThreshold.Value, Min, Max, ScaleMin, ScaleMax); } double?[] NormalizedIntensities = Normalize(intensitiesFlattened, null, null, ScaleMin, ScaleMax); int[] flatARGB = Colormap.GetRGBAs(NormalizedIntensities, Colormap, minimumIntensity: TransparencyThreshold ?? 0); double?[] normalizedValues = Normalize(Enumerable.Range(0, 256).Select(i => (double?)i).Reverse().ToArray(), null, null, ScaleMin, ScaleMax); int[] scaleRGBA = Colormap.GetRGBAs(normalizedValues, Colormap); BmpHeatmap?.Dispose(); BmpHeatmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); Rectangle rect = new Rectangle(0, 0, BmpHeatmap.Width, BmpHeatmap.Height); BitmapData bmpData = BmpHeatmap.LockBits(rect, ImageLockMode.ReadWrite, BmpHeatmap.PixelFormat); Marshal.Copy(flatARGB, 0, bmpData.Scan0, flatARGB.Length); BmpHeatmap.UnlockBits(bmpData); }