Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17860


Ignore:
Timestamp:
03/08/21 16:20:24 (4 years ago)
Author:
dpiringe
Message:

#3105

  • added usage checks for imports and helper functions (the formatter only includes necessary stuff)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3105_PythonFormatter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionPythonFormatter.cs

    r17855 r17860  
    3636
    3737    private int VariableCounter { get; set; } = 0;
    38     private IDictionary<string, string> VariableMap = new Dictionary<string, string>();
     38    private IDictionary<string, string> VariableMap { get; } = new Dictionary<string, string>();
     39    private int MathLibCounter { get; set; } = 0;
     40    private int StatisticLibCounter { get; set; } = 0;
     41    private int EvaluateIfCounter { get; set; } = 0;
    3942
    4043    [StorableConstructor]
     
    6568
    6669    private void GenerateImports(StringBuilder strBuilder) {
    67       strBuilder.AppendLine("# imports");
    68       strBuilder.AppendLine("import math");
    69       strBuilder.AppendLine("import statistics");
     70      if(MathLibCounter > 0 || StatisticLibCounter > 0)
     71        strBuilder.AppendLine("# imports");
     72      if(MathLibCounter > 0)
     73        strBuilder.AppendLine("import math");
     74      if(StatisticLibCounter > 0)
     75        strBuilder.AppendLine("import statistics");
    7076    }
    7177
    7278    private void GenerateIfThenElseSource(StringBuilder strBuilder) {
    73       strBuilder.AppendLine("# condition helper function");
    74       strBuilder.AppendLine("def evaluate_if(condition, then_path, else_path): ");
    75       strBuilder.AppendLine("\tif condition:");
    76       strBuilder.AppendLine("\t\treturn then_path");
    77       strBuilder.AppendLine("\telse:");
    78       strBuilder.AppendLine("\t\treturn else_path");
     79      if(EvaluateIfCounter > 0) {
     80        strBuilder.AppendLine("# condition helper function");
     81        strBuilder.AppendLine("def evaluate_if(condition, then_path, else_path): ");
     82        strBuilder.AppendLine("\tif condition:");
     83        strBuilder.AppendLine("\t\treturn then_path");
     84        strBuilder.AppendLine("\telse:");
     85        strBuilder.AppendLine("\t\treturn else_path");
     86      }
    7987    }
    8088
     
    110118        FormatNode(node, strBuilder, infixSymbol: " and ");
    111119      } else if (symbol is Average) {
     120        StatisticLibCounter++;
    112121        FormatNode(node, strBuilder, prefixSymbol: "statistics.mean", openingSymbol: "([", closingSymbol: "])");
    113122      } else if (symbol is Cosine) {
     123        MathLibCounter++;
    114124        FormatNode(node, strBuilder, "math.cos");
    115125      } else if (symbol is Division) {
    116126        FormatDivision(node, strBuilder);
    117127      } else if (symbol is Exponential) {
     128        MathLibCounter++;
    118129        FormatNode(node, strBuilder, "math.exp");
    119130      } else if (symbol is GreaterThan) {
    120131        FormatNode(node, strBuilder, infixSymbol: " > ");
    121132      } else if (symbol is IfThenElse) {
     133        EvaluateIfCounter++;
    122134        FormatNode(node, strBuilder, "evaluate_if");
    123135      } else if (symbol is LessThan) {
    124136        FormatNode(node, strBuilder, infixSymbol: " < ");
    125137      } else if (symbol is Logarithm) {
     138        MathLibCounter++;
    126139        FormatNode(node, strBuilder, "math.log");
    127140      } else if (symbol is Multiplication) {
     
    134147        FormatNode(node, strBuilder, infixSymbol: " ^ ");
    135148      } else if (symbol is Sine) {
     149        MathLibCounter++;
    136150        FormatNode(node, strBuilder, "math.sin");
    137151      } else if (symbol is Subtraction) {
    138152        FormatSubtraction(node, strBuilder);
    139153      } else if (symbol is Tangent) {
     154        MathLibCounter++;
    140155        FormatNode(node, strBuilder, "math.tan");
    141156      } else if (symbol is HyperbolicTangent) {
     157        MathLibCounter++;
    142158        FormatNode(node, strBuilder, "math.tanh");
    143159      } else if (symbol is Square) {
    144160        FormatPower(node, strBuilder, "2");
    145161      } else if (symbol is SquareRoot) {
     162        MathLibCounter++;
    146163        FormatNode(node, strBuilder, "math.sqrt");
    147164      } else if (symbol is Cube) {
     
    150167        FormatNode(node, strBuilder, closingSymbol: " ** (1. / 3))");
    151168      } else if (symbol is Power) {
     169        MathLibCounter++;
    152170        FormatNode(node, strBuilder, "math.pow");
    153171      } else if (symbol is Root) {
     
    156174        FormatNode(node, strBuilder, "abs");
    157175      } else if (symbol is AnalyticQuotient) {
     176        MathLibCounter++;
    158177        strBuilder.Append("(");
    159178        FormatRecursively(node.GetSubtree(0), strBuilder);
     
    188207
    189208    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
     209      MathLibCounter++;
    190210      strBuilder.Append("math.pow(");
    191211      FormatRecursively(node.GetSubtree(0), strBuilder);
     
    194214
    195215    private void FormatRoot(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     216      MathLibCounter++;
    196217      strBuilder.Append("math.pow(");
    197218      FormatRecursively(node.GetSubtree(0), strBuilder);
Note: See TracChangeset for help on using the changeset viewer.