Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18050


Ignore:
Timestamp:
09/08/21 15:53:37 (3 years ago)
Author:
dpiringe
Message:

#3026

  • added a new protected virtual property in CheckedItemListView to be able to change the text of the groupBox
  • removed event handling in ResultCollectionPostProcessorControl and overwrote the new property GroupBoxText
  • added a new section in ExportJsonDialog for result collection processors
  • renamed IResultCollectionPostProcessor to IResultCollectionProcessor + all connected classes
  • added/changed the workflow of ResultCollectionProcessors in JsonTemplateGenerator and JsonTemplateInstantiator
    • also changed Constants.cs -> added a new section in template ResultCollectionProcessorItems
Location:
branches/3026_IntegrationIntoSymSpace
Files:
1 added
1 deleted
9 edited
1 copied
2 moved

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs

    r17180 r18050  
    4343      set { base.Content = value; }
    4444    }
     45
     46    protected virtual string GroupBoxText => "Items";
    4547
    4648    public CheckedItemListView()
     
    179181        Invoke((Action)SetNumberOfCheckItems);
    180182      } else {
    181         this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
     183        this.itemsGroupBox.Text = String.Format("{0} (Checked: {1}/{2})", GroupBoxText, Content.CheckedItems.Count(), Content.Count);
    182184      }
    183185    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs

    r18046 r18050  
    2929    }
    3030
    31     private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
     31    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem, IEnumerable<IResultCollectionProcessor> postProcessors) {
    3232      if (optimizer.Runs.Count > 0)
    3333        File.WriteAllText(file, FetchResults(optimizer, configuredResultItem, postProcessors));
     
    4141      ResultFormatter?.Where(x => x.GetType().FullName == fullName).Last();
    4242
    43     private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
     43    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems, IEnumerable<IResultCollectionProcessor> postProcessors) {
    4444      JArray arr = new JArray();
    4545      IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
     
    6767        }
    6868
    69         IDictionary<string, string> resultDict = new Dictionary<string, string>();
    70         foreach (var processor in postProcessors) {
    71           processor.Apply(run.Results, resultDict);
    72         }
    73         foreach(var kvp in resultDict) {
    74           obj.Add(kvp.Key, kvp.Value);
    75         }
     69        foreach (var processor in postProcessors)
     70          processor.Apply(run.Results);
     71
     72        foreach(var result in run.Results)
     73          obj.Add(result.Key, result.Value.ToString());
    7674      }
    7775      return SingleLineArrayJsonWriter.Serialize(arr);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.Designer.cs

    r18047 r18050  
    177177      // postProcessorListView
    178178      this.postProcessorListControl = new ResultCollectionPostProcessorControl();
    179       this.postProcessorListControl.Content = new CheckedItemList<IResultCollectionPostProcessor>();
    180       this.postProcessorListControl.Content.Add(new SymRegPythonPostProcessor());
     179      this.postProcessorListControl.Content = new CheckedItemList<IResultCollectionProcessor>();
    181180      this.postProcessorListControl.Location = new System.Drawing.Point(0, 6);
    182181      this.postProcessorListControl.Dock = System.Windows.Forms.DockStyle.Fill;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r18047 r18050  
    2222    private IOptimizer Optimizer { get; set; }
    2323    private IList<IJsonItemVM> VMs { get; set; }
    24     private ICheckedItemList<IResultCollectionPostProcessor> PostProcessors { get; set; }
     24    private ICheckedItemList<IResultCollectionProcessor> PostProcessors { get; set; }
    2525    #endregion
    2626
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ResultCollectionPostProcessorControl.cs

    r18047 r18050  
    1111
    1212namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    13   public partial class ResultCollectionPostProcessorControl : Core.Views.CheckedItemListView<IResultCollectionPostProcessor> {
     13  public partial class ResultCollectionPostProcessorControl : Core.Views.CheckedItemListView<IResultCollectionProcessor> {
    1414    public ResultCollectionPostProcessorControl() {
    1515      InitializeComponent();
    1616    }
    1717
    18     #region Content Events
    19     protected override void OnContentChanged() {
    20       base.OnContentChanged();
    21       if (Content != null) {
    22         SetNumberOfCheckItems();
    23       }
    24     }
     18    protected override string GroupBoxText => "Result Collection Processors";
    2519
    26     protected override void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    27       base.Content_CheckedItemsChanged(sender, e);
    28       if (InvokeRequired)
    29         Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IResultCollectionPostProcessor>>(Content_CheckedItemsChanged), sender, e);
    30       else {
    31         SetNumberOfCheckItems();
    32       }
    33     }
    34     protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    35       base.Content_CollectionReset(sender, e);
    36       SetNumberOfCheckItems();
    37     }
    38     protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    39       base.Content_ItemsAdded(sender, e);
    40       SetNumberOfCheckItems();
    41     }
    42     protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    43       base.Content_ItemsMoved(sender, e);
    44       SetNumberOfCheckItems();
    45     }
    46     protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    47       base.Content_ItemsRemoved(sender, e);
    48       SetNumberOfCheckItems();
    49     }
    50     protected override void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IResultCollectionPostProcessor>> e) {
    51       base.Content_ItemsReplaced(sender, e);
    52       SetNumberOfCheckItems();
    53     }
    54     #endregion
    55 
    56     private void SetNumberOfCheckItems() { // TODO: eigene override für "Post Processors" -> Item Naming
    57       if (InvokeRequired) {
    58         Invoke((Action)SetNumberOfCheckItems);
    59       } else {
    60         this.itemsGroupBox.Text = String.Format("Post Processors (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
    61       }
    62     }
    6320  }
    6421}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Constants.cs

    r17519 r18050  
    1010    internal const string Parameters = "Parameters";
    1111    internal const string Results = "Results";
     12    internal const string ResultCollectionProcessorItems = "ResultCollectionProcessors";
    1213
    1314    internal const string Template = @"{
     
    1718      },
    1819      '" + Parameters + @"': [],
    19       '" + Results + @"': []
     20      '" + Results + @"': [],
     21      '" + ResultCollectionProcessorItems + @"': []
    2022    }";
    2123  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj

    r18046 r18050  
    6666    <Compile Include="Converters\BatchRunConverter.cs" />
    6767    <Compile Include="Converters\ExperimentConverter.cs" />
     68    <Compile Include="Converters\ObervableListConverter.cs" />
    6869    <Compile Include="Converters\RegressionProblemDataConverter.cs" />
    6970    <Compile Include="Converters\ResultConverter.cs" />
     
    8384    <Compile Include="Interfaces\IValueJsonItem.cs" />
    8485    <Compile Include="Interfaces\IValueLookupJsonItem.cs" />
    85     <Compile Include="Interfaces\IResultCollectionPostProcessor.cs" />
     86    <Compile Include="Interfaces\IResultCollectionProcessor.cs" />
    8687    <Compile Include="JsonItems\ArrayJsonItem.cs" />
    8788    <Compile Include="JsonItems\BoolJsonItems.cs" />
     
    105106    <Compile Include="JsonItems\ValueJsonItem.cs" />
    106107    <Compile Include="JsonItems\ValueLookupJsonItem.cs" />
    107     <Compile Include="ResultCollectionPostProcessors\SymRegPythonPostProcessor.cs" />
     108    <Compile Include="ResultCollectionProcessors\FilterResultCollectionProcessor.cs" />
     109    <Compile Include="ResultCollectionProcessors\SymRegPythonProcessor.cs" />
    108110    <Compile Include="ResultFormatters\MatlabResultFormatter.cs" />
    109111    <Compile Include="ResultFormatters\PythonResultFormatter.cs" />
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IResultCollectionProcessor.cs

    r18049 r18050  
    99
    1010namespace HeuristicLab.JsonInterface {
    11   public interface IResultCollectionPostProcessor : IItem {
    12    
    13     void Apply(IObservableDictionary<string, IItem> /*ResultCollection*/ results, IDictionary<string, string> output); //evtl. doch JToken als Dict?
     11  public interface IResultCollectionProcessor : IParameterizedNamedItem {
     12    void Apply(IObservableDictionary<string, IItem> results);
    1413  }
    1514}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateGenerator.cs

    r18046 r18050  
    1919    /// <param name="optimizer">the optimizer object to serialize</param>
    2020    /// <param name="rootItem">Root JsonItem for serialization, considers only active JsonItems for serialization</param>
    21     public static void GenerateTemplate(string templatePath, IOptimizer optimizer, IJsonItem rootItem, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
     21    public static void GenerateTemplate(string templatePath, IOptimizer optimizer, IJsonItem rootItem, IEnumerable<IResultCollectionProcessor> resultCollectionProcessors) {
    2222      // clear all runs
     23      if (optimizer.ExecutionState == ExecutionState.Paused)
     24        optimizer.Stop();
    2325      optimizer.Runs.Clear();
    2426
     
    3234      JArray parameterItems = new JArray();
    3335      JArray resultItems = new JArray();
    34       JArray postProcessorItems = new JArray();
    35       // postProcessors.Select(x => new JObject().Add("Name", JToken.Parse(x.GetType().Name))
    36       foreach (var proc in postProcessors) {
    37         var tmp = new JObject();
    38         tmp.Add("Name", proc.GetType().Name);
    39         postProcessorItems.Add(tmp);
    40       }
    41 
    42       IList<IJsonItem> jsonItems = new List<IJsonItem>();
    43      
     36      JArray resultCollectionProcessorItems = new JArray();
    4437      string templateName = Path.GetFileName(templatePath);
    4538      string templateDirectory = Path.GetDirectoryName(templatePath);
    46 
    4739      #endregion
    4840
    49       if(optimizer.ExecutionState == ExecutionState.Paused)
    50         optimizer.Stop();
    51 
    52       // recursively filter items with values/ranges/actualNames
    53       PopulateJsonItems(rootItem, jsonItems);
     41      // filter items with values/ranges/actualNames
     42      var jsonItems = rootItem.Where(x => x.Active && !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem));
    5443
    5544      #region Serialize HL File
     
    7160      #endregion
    7261
     62      #region ResultCollectionProcessor Serialization
     63      foreach (var proc in resultCollectionProcessors) {
     64        JArray rcpParameterItems = new JArray();
     65        var guid = StorableTypeAttribute.GetStorableTypeAttribute(proc.GetType()).Guid.ToString();
     66        var item = JsonItemConverter.Extract(proc);
     67
     68        var rcpItems = item
     69          .Where(x => !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem))
     70          .Select(x => x.GenerateJObject());
     71
     72        foreach (var i in rcpItems)
     73          rcpParameterItems.Add(i);
     74
     75        JObject processorObj = new JObject();
     76        processorObj.Add(nameof(IJsonItem.Name), item.Name);
     77        processorObj.Add("GUID", guid);
     78        processorObj.Add(Constants.Parameters, rcpParameterItems);
     79        resultCollectionProcessorItems.Add(processorObj);
     80      }
     81      #endregion
     82
    7383      #region Set Template Data
    7484      template[Constants.Metadata][Constants.TemplateName] = templateName;
     
    7686      template[Constants.Parameters] = parameterItems;
    7787      template[Constants.Results] = resultItems;
    78       template["PostProcessors"] = postProcessorItems;
     88      template[Constants.ResultCollectionProcessorItems] = resultCollectionProcessorItems;
    7989      #endregion
    8090
     
    8393      #endregion
    8494    }
    85 
    86     #region Helper   
    87     private static void PopulateJsonItems(IJsonItem item, IList<IJsonItem> jsonItems) {
    88       foreach(var x in item) { // TODO: dieses konstrukt notwendig?
    89         if (x.Active && !(x is EmptyJsonItem) && !(x is UnsupportedJsonItem)) {
    90           jsonItems.Add(x);
    91         }
    92       }
    93     }
    94     #endregion
    9595  }
    9696}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r18046 r18050  
    1414      Optimizer = optimizer;
    1515      ConfiguredResultItems = configuredResultItems;
    16       PostProcessors = Enumerable.Empty<IResultCollectionPostProcessor>();
     16      PostProcessors = Enumerable.Empty<IResultCollectionProcessor>();
    1717    }
    1818
    19     public InstantiatorResult(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
     19    public InstantiatorResult(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems, IEnumerable<IResultCollectionProcessor> postProcessors) {
    2020      Optimizer = optimizer;
    2121      ConfiguredResultItems = configuredResultItems;
     
    2525    public IOptimizer Optimizer { get; }
    2626    public IEnumerable<IResultJsonItem> ConfiguredResultItems { get; }
    27     public IEnumerable<IResultCollectionPostProcessor> PostProcessors { get; }
     27    public IEnumerable<IResultCollectionProcessor> PostProcessors { get; }
    2828  }
    2929
     
    6565      #endregion
    6666
     67      #region Deserialize HL File
    6768      // extract metadata information
    6869      string relativePath = Template[Constants.Metadata][Constants.HLFileLocation].ToString().Trim(); // get relative path
     
    7273
    7374      string hLFileLocation = Path.Combine(Path.GetDirectoryName(templateFileFullPath), relativePath);
    74       #region Deserialize HL File
    7575      ProtoBufSerializer serializer = new ProtoBufSerializer();
    7676      IOptimizer optimizer = (IOptimizer)serializer.Deserialize(hLFileLocation);
     
    9494      JsonItemConverter.Inject(optimizer, rootItem);
    9595
    96       IList<IResultCollectionPostProcessor> postProcessorList = new List<IResultCollectionPostProcessor>();
    97       var postProcessors = ApplicationManager.Manager.GetInstances<IResultCollectionPostProcessor>();
    98       foreach (JObject obj in Template["PostProcessors"]) {
    99         //string name = obj.Property("Name").Value.ToString();
    100         foreach(var proc in postProcessors) {
    101           if (proc.GetType().Name == obj["Name"].ToString())
    102             postProcessorList.Add(proc);
     96      return new InstantiatorResult(optimizer, CollectResults(), CollectResultCollectionProcessors());
     97    }
     98
     99    /// <summary>
     100    /// Instantiates all defined (in template) ResultCollectionProcessors and injects the configured parameters.
     101    /// </summary>
     102    private IEnumerable<IResultCollectionProcessor> CollectResultCollectionProcessors() {
     103      IList<IResultCollectionProcessor> postProcessorList = new List<IResultCollectionProcessor>();
     104      foreach (JObject obj in Template[Constants.ResultCollectionProcessorItems]) {
     105        var guid = obj["GUID"].ToString();
     106        var parameters = obj[Constants.Parameters];
     107        var type = Mapper.StaticCache.GetType(new Guid(guid));
     108        var rcp = (IResultCollectionProcessor)Activator.CreateInstance(type);
     109        var rcpItem = JsonItemConverter.Extract(rcp);
     110
     111        foreach (JObject param in parameters) {
     112          var path = param[nameof(IJsonItem.Path)].ToString();
     113          foreach (var item in rcpItem)
     114            if (item.Path == path)
     115              item.SetJObject(param);
    103116        }
     117
     118        JsonItemConverter.Inject(rcp, rcpItem);
     119        postProcessorList.Add(rcp);
    104120      }
    105 
    106       return new InstantiatorResult(optimizer, CollectResults(), postProcessorList);
     121      return postProcessorList;
    107122    }
    108123
     
    110125      IList<IResultJsonItem> res = new List<IResultJsonItem>();
    111126      foreach(JObject obj in Template[Constants.Results]) {
    112         //string name = obj.Property("Name").Value.ToString();
    113127        var resultItem = new ResultJsonItem();
    114128        resultItem.SetJObject(obj);
    115129        res.Add(resultItem);
    116         //res.Add(new ResultJsonItem() { Name = name });
    117130      }
    118131      return res;
     
    124137
    125138      foreach (JObject obj in Template[Constants.Parameters]) {
    126         string path = obj.Property("Path").Value.ToString();
     139        string path = obj.Property(nameof(IJsonItem.Path)).Value.ToString();
    127140        foreach(var tmp in root) {
    128141          if(tmp.Path == path) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/ResultCollectionProcessors/SymRegPythonProcessor.cs

    r18047 r18050  
    1111using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    1212using HEAL.Attic;
     13using HeuristicLab.Data;
    1314
    1415namespace HeuristicLab.JsonInterface {
    1516  [StorableType("844F2887-B7A0-4BD4-89B8-F9155C65D214")]
    16   public class SymRegPythonPostProcessor : Item, IResultCollectionPostProcessor {
     17  public class SymRegPythonProcessor : ParameterizedNamedItem, IResultCollectionProcessor {
    1718
     19    #region Constructors & Cloning
    1820    [StorableConstructor]
    19     protected SymRegPythonPostProcessor(StorableConstructorFlag _) : base(_) {
     21    protected SymRegPythonProcessor(StorableConstructorFlag _) : base(_) { }
     22    public SymRegPythonProcessor() { }
     23    public SymRegPythonProcessor(SymRegPythonProcessor original, Cloner cloner) : base(original, cloner) { }
     24
     25    public override IDeepCloneable Clone(Cloner cloner) {
     26      return new SymRegPythonProcessor(this, cloner);
    2027    }
     28    #endregion
    2129
    22     public SymRegPythonPostProcessor() { }
    23     public SymRegPythonPostProcessor(SymRegPythonPostProcessor old, Cloner cloner) { }
    24 
    25     public void Apply(IObservableDictionary<string, IItem> results, IDictionary<string, string> output) {
     30    public void Apply(IObservableDictionary<string, IItem> results) {
    2631      var formatter = new SymbolicDataAnalysisExpressionPythonFormatter();
    27       foreach (var kvp in results) {
     32      var resultCopy = new ObservableDictionary<string, IItem>(results);
     33      foreach (var kvp in resultCopy) {
    2834        if (kvp.Value is ISymbolicRegressionSolution sol) {
    29           output.Add(kvp.Key, formatter.Format(sol.Model.SymbolicExpressionTree));
     35          results.Add($"{kvp.Key} - Python" , new StringValue(formatter.Format(sol.Model.SymbolicExpressionTree)));
    3036        }
    3137      }
    3238    }
    33 
    34     public override IDeepCloneable Clone(Cloner cloner) {
    35       return new SymRegPythonPostProcessor(this, cloner);
    36     }
    3739  }
    3840}
Note: See TracChangeset for help on using the changeset viewer.