Free cookie consent management tool by TermsFeed Policy Generator

Changeset 273


Ignore:
Timestamp:
05/29/08 18:05:50 (16 years ago)
Author:
gkronber
Message:

fixed #160

Location:
trunk/sources
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/DatasetParser.cs

    r272 r273  
    2828namespace HeuristicLab.DataAnalysis {
    2929  public class DatasetParser {
     30    private const string PROBLEMNAME = "PROBLEMNAME";
     31    private const string VARIABLENAMES = "VARIABLENAMES";
     32    private const string TARGETVARIABLE = "TARGETVARIABLE";
     33    private const string MAXIMUMTREEHEIGHT = "MAXIMUMTREEHEIGHT";
     34    private const string MAXIMUMTREESIZE = "MAXIMUMTREESIZE";
     35    private const string TRAININGSAMPLESSTART = "TRAININGSAMPLESSTART";
     36    private const string TRAININGSAMPLESEND = "TRAININGSAMPLESEND";
    3037    private Tokenizer tokenizer;
    3138    private Dictionary<string, List<Token>> metadata;
     
    5360    public string ProblemName {
    5461      get {
    55         return metadata["PROBLEMNAME"][0].stringValue;
     62        if(metadata.ContainsKey(PROBLEMNAME)) {
     63          return metadata[PROBLEMNAME][0].stringValue;
     64        } else return "-";
    5665      }
    5766    }
     
    5968    public string[] VariableNames {
    6069      get {
    61         List<Token> nameList = metadata["VARIABLENAMES"];
    62         string[] names = new string[nameList.Count];
    63         for(int i = 0; i < names.Length; i++) {
    64           names[i] = nameList[i].stringValue;
    65         }
    66 
    67         return names;
     70        if(metadata.ContainsKey(VARIABLENAMES)) {
     71          List<Token> nameList = metadata[VARIABLENAMES];
     72          string[] names = new string[nameList.Count];
     73          for(int i = 0; i < names.Length; i++) {
     74            names[i] = nameList[i].stringValue;
     75          }
     76          return names;
     77        } else {
     78          string[] names = new string[columns];
     79          for(int i = 0; i < names.Length; i++) {
     80            names[i] = "X" + i.ToString("000");
     81          }
     82          return names;
     83        }
    6884      }
    6985    }
     
    7187    public int TargetVariable {
    7288      get {
    73         return metadata["TARGETVARIABLE"][0].intValue;
     89        if(metadata.ContainsKey(TARGETVARIABLE)) {
     90          return metadata[TARGETVARIABLE][0].intValue;
     91        } else return 0; // default is the first column
    7492      }
    7593    }
     
    7795    public int MaxTreeHeight {
    7896      get {
    79         return metadata["MAXIMUMTREEHEIGHT"][0].intValue;
     97        if(metadata.ContainsKey(MAXIMUMTREEHEIGHT)) {
     98          return metadata[MAXIMUMTREEHEIGHT][0].intValue;
     99        } else return 0;
    80100      }
    81101    }
     
    83103    public int MaxTreeSize {
    84104      get {
    85         return metadata["MAXIMUMTREESIZE"][0].intValue;
     105        if(metadata.ContainsKey(MAXIMUMTREESIZE)) {
     106          return metadata[MAXIMUMTREESIZE][0].intValue;
     107        } else return 0;
    86108      }
    87109    }
     
    89111    public int TrainingSamplesStart {
    90112      get {
    91         if(!metadata.ContainsKey("TRAININGSAMPLESSTART")) return 0;
    92         else return metadata["TRAININGSAMPLESSTART"][0].intValue;
     113        if(metadata.ContainsKey(TRAININGSAMPLESSTART)) {
     114          return metadata[TRAININGSAMPLESSTART][0].intValue;
     115        } else return 0;
    93116      }
    94117    }
     
    96119    public int TrainingSamplesEnd {
    97120      get {
    98         if(!metadata.ContainsKey("TRAININGSAMPLESEND")) return rows;
    99         else return metadata["TRAININGSAMPLESEND"][0].intValue;
     121        if(metadata.ContainsKey(TRAININGSAMPLESEND)) {
     122          return metadata[TRAININGSAMPLESEND][0].intValue;
     123        } else return rows;
    100124      }
    101125    }
     
    279303            if(samplesList.Count > 0 && samplesList[0].Count != row.Count) {
    280304              Error("The first row of the dataset has " + samplesList[0].Count + " columns." +
    281                 "\nLine " + tokenizer.CurrentLineNumber + " has " + row.Count + " columns.");
     305                "\nLine " + tokenizer.CurrentLineNumber + " has " + row.Count + " columns.", "", tokenizer.CurrentLineNumber);
    282306            }
    283307          } else if(samplesList.Count > 0) {
     
    301325          // when we are parsing non-strictly we also allow unreadable values inserting NAN instead
    302326          if(strict) {
    303             Error("Unkown value " + current + " in line " + tokenizer.CurrentLineNumber +
    304               "\n" + tokenizer.CurrentLine);
     327            Error("Unexpected token.", current.stringValue, tokenizer.CurrentLineNumber);
    305328          } else {
    306329            row.Add(double.NaN);
     
    316339        Token nameToken = tokenizer.Next();
    317340        if(nameToken.type != TokenTypeEnum.String)
    318           throw new Exception("Expected a variable name; got " + nameToken +
    319             "\nLine " + tokenizer.CurrentLineNumber + ": " + tokenizer.CurrentLine);
     341          Error("Expected a variable name.", nameToken.stringValue, tokenizer.CurrentLineNumber);
    320342
    321343        Expect(Tokenizer.AssignmentToken);
     
    335357      Token actualToken = tokenizer.Next();
    336358      if(actualToken != expectedToken) {
    337         Error("Expected: " + expectedToken + " got: " + actualToken +
    338           "\nLine " + tokenizer.CurrentLineNumber + ": " + tokenizer.CurrentLine);
    339       }
    340     }
    341 
    342     private void Error(string message) {
    343       throw new Exception("Error while parsing.\n" + message);
     359        Error("Expected: " + expectedToken, actualToken.stringValue, tokenizer.CurrentLineNumber);
     360      }
     361    }
     362
     363    private void Error(string message, string token, int lineNumber) {
     364      throw new DataFormatException("Error while parsing.\n" + message, token, lineNumber);
    344365    }
    345366    #endregion
  • trunk/sources/HeuristicLab.DataAnalysis/HeuristicLab.DataAnalysis.csproj

    r234 r273  
    5151  </ItemGroup>
    5252  <ItemGroup>
     53    <Compile Include="DataFormatException.cs" />
    5354    <Compile Include="Dataset.cs" />
    5455    <Compile Include="DatasetParser.cs" />
  • trunk/sources/HeuristicLab.StructureIdentification/StructIdProblemInjectorView.cs

    r172 r273  
    7474        bool success = false;
    7575        try {
    76           parser.Import(openFileDialog.FileName, true);
    77           success = true;
    78         } catch (Exception) {
    79           // not possible to parse strictly => try to parse non-strict
    80           parser.Import(openFileDialog.FileName, false);
    81           success = true;
     76          try {
     77            parser.Import(openFileDialog.FileName, true);
     78            success = true;
     79          } catch(DataFormatException) {
     80            // not possible to parse strictly => try to parse non-strict
     81            parser.Import(openFileDialog.FileName, false);
     82            success = true;
     83          }
     84        } catch(DataFormatException ex) {
     85          // if the non-strict parsing also failed then show the exception
     86          ShowErrorMessageBox(ex);
    8287        }
    8388        if (success) {
     
    95100      }
    96101    }
     102
     103    private void ShowErrorMessageBox(Exception ex) {
     104      MessageBox.Show(BuildErrorMessage(ex),
     105                      "Error - " + ex.GetType().Name,
     106                      MessageBoxButtons.OK,
     107                      MessageBoxIcon.Error);
     108    }
     109    private string BuildErrorMessage(Exception ex) {
     110      StringBuilder sb = new StringBuilder();
     111      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
     112
     113      while(ex.InnerException != null) {
     114        ex = ex.InnerException;
     115        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
     116      }
     117      return sb.ToString();
     118    }
    97119  }
    98120}
Note: See TracChangeset for help on using the changeset viewer.