[7128] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Drawing;
|
---|
| 4 | using System.Drawing.Drawing2D;
|
---|
| 5 | using System.IO;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using HeuristicLab.Common;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
| 11 | using HeuristicLab.Optimization;
|
---|
| 12 | using HeuristicLab.Parameters;
|
---|
| 13 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 14 | using ICSharpCode.SharpZipLib.Zip;
|
---|
| 15 | using System.Drawing.Imaging;
|
---|
| 16 |
|
---|
| 17 | namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts {
|
---|
| 18 |
|
---|
| 19 | [Item("Box Chart Creator", "Visualizes essential run information by creating an overview chart.")]
|
---|
| 20 | [StorableClass]
|
---|
| 21 | public class BoxChartCreator : ParameterizedNamedItem, IRunCollectionModifier {
|
---|
| 22 |
|
---|
| 23 | #region Parameters
|
---|
| 24 | public ValueParameter<StringValue> ChartNameParameter {
|
---|
| 25 | get { return (ValueParameter<StringValue>)Parameters["ChartName"]; }
|
---|
| 26 | }
|
---|
| 27 | public ValueParameter<CheckedItemList<IBoxChartElementGenerator>> ElementsParameter {
|
---|
| 28 | get { return (ValueParameter<CheckedItemList<IBoxChartElementGenerator>>)Parameters["Elements"]; }
|
---|
| 29 | }
|
---|
| 30 | public ValueParameter<IntValue> SizeParameter {
|
---|
| 31 | get { return (ValueParameter<IntValue>)Parameters["Size"]; }
|
---|
| 32 | }
|
---|
| 33 | public ValueParameter<IntValue> PaddingParameter {
|
---|
| 34 | get { return (ValueParameter<IntValue>) Parameters["Padding"]; }
|
---|
| 35 | }
|
---|
| 36 | public ValueParameter<StringValue> ZipFileNameParameter {
|
---|
| 37 | get { return (ValueParameter<StringValue>) Parameters["ZipFileName"]; }
|
---|
| 38 | }
|
---|
| 39 | #endregion
|
---|
| 40 |
|
---|
| 41 | #region Parameter Values
|
---|
| 42 | protected string ChartName { get { return ChartNameParameter.Value.Value; } }
|
---|
| 43 | protected string ZipFileName { get { return ZipFileNameParameter.Value.Value; } }
|
---|
| 44 | protected int Size { get { return SizeParameter.Value.Value; } }
|
---|
| 45 | protected int Padding { get { return PaddingParameter.Value.Value; } }
|
---|
| 46 | protected List<IBoxChartElementGenerator> Generators {
|
---|
| 47 | get {
|
---|
| 48 | return ElementsParameter.Value.CheckedItems.Select(i => i.Value).ToList();
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | #region Construction & Cloning
|
---|
| 54 | [StorableConstructor]
|
---|
| 55 | protected BoxChartCreator(bool deserializing) : base(deserializing) { }
|
---|
| 56 | protected BoxChartCreator(BoxChartCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 57 | public BoxChartCreator() {
|
---|
| 58 | Parameters.Add(new ValueParameter<StringValue>("ChartName", "The result name of the chart.", new StringValue("Chart")));
|
---|
| 59 | Parameters.Add(new ValueParameter<CheckedItemList<IBoxChartElementGenerator>>("Elements", "List of patient chart element generators.", new CheckedItemList<IBoxChartElementGenerator>()));
|
---|
| 60 | Parameters.Add(new ValueParameter<IntValue>("Size", "The size of one box in pixels.", new IntValue(50)));
|
---|
| 61 | Parameters.Add(new ValueParameter<IntValue>("Padding", "The padding inside the boxes", new IntValue(1)));
|
---|
| 62 | Parameters.Add(new ValueParameter<StringValue>("ZipFileName", "The name of the zip file that will contain all generated charts.", new StringValue("")));
|
---|
| 63 | }
|
---|
| 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new BoxChartCreator(this, cloner);
|
---|
| 66 | }
|
---|
| 67 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 68 | private void AfterDeserialization() {
|
---|
| 69 | if (!Parameters.ContainsKey("ZipFileName"))
|
---|
| 70 | Parameters.Add(new ValueParameter<StringValue>("ZipFileName", "The name of the zip file that will contain all generated charts.", new StringValue("")));
|
---|
| 71 | }
|
---|
| 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region IRunCollectionModifier Members
|
---|
| 75 |
|
---|
| 76 | public void Modify(List<IRun> runs) {
|
---|
| 77 | var bitmaps = new Dictionary<string, Bitmap>();
|
---|
| 78 | var generators = Generators;
|
---|
| 79 | var maxX = generators.Max(g => g.X + g.ColSpan);
|
---|
| 80 | var maxY = generators.Max(g => g.Y + g.RowSpan);
|
---|
| 81 | for (int i = 0; i<runs.Count; i++) {
|
---|
| 82 | var run = runs[i];
|
---|
| 83 | var b = new Bitmap(maxX*Size, maxY*Size);
|
---|
| 84 | using (var g = Graphics.FromImage(b)) {
|
---|
| 85 | g.FillRectangle(SystemBrushes.Control, 0, 0, b.Width, b.Height);
|
---|
| 86 | foreach (var gen in generators) {
|
---|
| 87 | g.SetClip(new Rectangle(gen.X*Size+Padding, gen.Y*Size+Padding, gen.ColSpan*Size-2*Padding, gen.RowSpan*Size-2*Padding));
|
---|
| 88 | gen.Draw(run, g);
|
---|
| 89 | g.ResetClip();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | run.Results[ChartName] = new BitmapItem(b);
|
---|
| 93 | bitmaps.Add(string.Format("{0}.png", run.Name), (Bitmap)b.Clone());
|
---|
| 94 | }
|
---|
| 95 | SaveAllBitmaps(bitmaps);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private void SaveAllBitmaps(Dictionary<string, Bitmap> bitmaps) {
|
---|
| 99 | if (ZipFileNameParameter.Value == null && string.IsNullOrEmpty(ZipFileName))
|
---|
| 100 | return;
|
---|
| 101 | var filename = ZipFileName;
|
---|
| 102 | if (!filename.ToLowerInvariant().EndsWith("zip"))
|
---|
| 103 | filename = string.Format("{0}.zip", filename);
|
---|
| 104 | using (var stream = File.Create(filename)) {
|
---|
| 105 | using (var zipStream = new ZipOutputStream(stream)) {
|
---|
| 106 | foreach (var bitmap in bitmaps) {
|
---|
| 107 | zipStream.PutNextEntry(new ZipEntry(bitmap.Key) { DateTime = DateTime.Now });
|
---|
| 108 | using (var s = new MemoryStream()) {
|
---|
| 109 | bitmap.Value.Save(s, ImageFormat.Png);
|
---|
| 110 | zipStream.Write(s.ToArray(), 0, (int)s.Length);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | #endregion
|
---|
| 118 | }
|
---|
| 119 | }
|
---|