Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/23/09 14:05:28 (15 years ago)
Author:
mkommend
Message:

refactored modeling database to enable the change of models (ticket #759)

Location:
trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/ModelData.cs

    r2229 r2382  
    6161
    6262    private byte[] data;
    63     [Column(Storage = "data", DbType = "image", CanBeNull = false)]
     63    [Column(Storage = "data", DbType = "image", CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
    6464    public byte[] Data {
    6565      get { return this.data; }
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DataObjects/Problem.cs

    r2304 r2382  
    3131namespace HeuristicLab.Modeling.Database.SQLServerCompact {
    3232  [Table(Name = "Problem")]
    33   public class Problem : IProblem {
     33  public class Problem {
    3434    public Problem() {
    3535    }
  • trunk/sources/HeuristicLab.Modeling.Database.SQLServerCompact/3.2/DatabaseService.cs

    r2371 r2382  
    3232namespace HeuristicLab.Modeling.Database.SQLServerCompact {
    3333  public class DatabaseService : IModelingDatabase {
    34 
    3534    private readonly string connection;
    3635    public DatabaseService(string connection) {
     
    7271    }
    7372
    74     public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
    75       GetOrCreateProblem(algorithm.Dataset);
    76       return Persist(algorithm.Model, algorithm.Name, algorithm.Description);
    77     }
    78 
    79     public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
    80       Dictionary<string, Variable> variables = GetAllVariables();
    81       Algorithm algo = GetOrCreateAlgorithm(algorithmName, algorithmDescription);
    82       Variable target = variables[model.TargetVariable];
    83       Model m;
    84 
    85       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    86         m = new Model(target, algo,model.Type);
    87         m.TrainingSamplesStart = model.TrainingSamplesStart;
    88         m.TrainingSamplesEnd = model.TrainingSamplesEnd;
    89         m.ValidationSamplesStart = model.ValidationSamplesStart;
    90         m.ValidationSamplesEnd = model.ValidationSamplesEnd;
    91         m.TestSamplesStart = model.TestSamplesStart;
    92         m.TestSamplesEnd = model.TestSamplesEnd;
    93 
    94         ctx.Models.InsertOnSubmit(m);
    95 
    96         ctx.SubmitChanges();
    97       }
    98 
    99       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    100         ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(model.Predictor)));
    101         ctx.SubmitChanges();
    102       }
    103 
    104       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    105         foreach (string inputVariable in model.InputVariables) {
    106           ctx.InputVariables.InsertOnSubmit(new InputVariable(m, variables[inputVariable]));
    107         }
    108         ctx.SubmitChanges();
    109       }
    110 
    111       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    112         foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
    113           Result result = GetOrCreateResult(pair.Key.ToString());
    114           ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
    115         }
    116         ctx.SubmitChanges();
    117       }
    118 
    119       // code to store meta-information for models (gkronber (8.9.09))
    120       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    121         foreach (KeyValuePair<string, double> pair in model.MetaData) {
    122           MetaData metaData = GetOrCreateMetaData(pair.Key);
    123           ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
    124         }
    125         ctx.SubmitChanges();
    126       }
    127 
    128       using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    129         foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
    130           foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
    131             Result result = GetOrCreateResult(variableResult.Key.ToString());
    132             ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
    133           }
    134         }
    135         ctx.SubmitChanges();
    136       }
    137 
    138       //if connected to database return inserted model
    139       if (this.ctx != null)
    140         return this.ctx.Models.Where(x => x.Id == m.Id).Single();
    141       return null;
    142     }
    143 
    144     #region Problem
     73    public IEnumerable<IModel> GetAllModels() {
     74      return ctx.Models.ToList().Cast<IModel>();
     75    }
     76
     77    public IEnumerable<IVariable> GetAllVariables() {
     78      return ctx.Variables.ToList().Cast<IVariable>();
     79    }
     80
     81    public IEnumerable<IResult> GetAllResults() {
     82      return ctx.Results.ToList().Cast<IResult>();
     83    }
     84
     85    public IEnumerable<IResult> GetAllResultsForInputVariables() {
     86      return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
     87    }
     88
     89    public IEnumerable<IMetaData> GetAllMetaData() {
     90      return ctx.MetaData.ToList().Cast<IMetaData>();
     91    }
     92
     93    public IEnumerable<IAlgorithm> GetAllAlgorithms() {
     94      return ctx.Algorithms.ToList().Cast<IAlgorithm>();
     95    }
     96
     97    public IModel CreateModel(ModelType modelType, IAlgorithm algorithm, IVariable targetVariable,
     98int trainingSamplesStart, int trainingSamplesEnd, int validationSamplesStart, int validationSamplesEnd, int testSamplesStart, int testSamplesEnd) {
     99      return CreateModel(null, modelType, algorithm, targetVariable, trainingSamplesStart, trainingSamplesEnd, validationSamplesStart, validationSamplesEnd, testSamplesStart, testSamplesEnd);
     100    }
     101
     102    public IModel CreateModel(string modelName, ModelType modelType, IAlgorithm algorithm, IVariable targetVariable,
     103     int trainingSamplesStart, int trainingSamplesEnd, int validationSamplesStart, int validationSamplesEnd, int testSamplesStart, int testSamplesEnd) {
     104      Variable target = (Variable)targetVariable;
     105      Algorithm algo = (Algorithm)algorithm;
     106      Model model = new Model(target, algo, modelType);
     107      model.Name = modelName;
     108      model.TrainingSamplesStart = trainingSamplesStart;
     109      model.TrainingSamplesEnd = trainingSamplesEnd;
     110      model.ValidationSamplesStart = validationSamplesStart;
     111      model.ValidationSamplesEnd = validationSamplesEnd;
     112      model.TestSamplesStart = testSamplesStart;
     113      model.TestSamplesEnd = testSamplesEnd;
     114
     115      return model;
     116    }
     117
     118    public void PersistModel(IModel model) {
     119      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     120        Model m = (Model)model;
     121        Model orginal = ctx.Models.GetOriginalEntityState(m);
     122        if (orginal == null)
     123          ctx.Models.Attach(m);
     124        ctx.Refresh(RefreshMode.KeepCurrentValues, m);
     125        ctx.SubmitChanges();
     126      }
     127    }
     128
     129    public void DeleteModel(IModel model) {
     130      Model m = (Model)model;
     131      ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
     132      ctx.ModelMetaData.DeleteAllOnSubmit(ctx.ModelMetaData.Where(x => x.Model == m));
     133      ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
     134      ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
     135      ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
     136      Model orginal = ctx.Models.GetOriginalEntityState(m);
     137      if (orginal == null)
     138        ctx.Models.Attach(m);
     139      ctx.Models.DeleteOnSubmit(m);
     140      ctx.SubmitChanges();
     141    }
    145142
    146143    public Dataset GetDataset() {
    147144      if (ctx.Problems.Count() != 1)
    148145        throw new InvalidOperationException("Could not get dataset. No or more than one problems are persisted in the database.");
    149 
    150146      Problem problem = ctx.Problems.Single();
    151147      return problem.Dataset;
    152 
    153     }
    154 
    155     public IProblem GetOrCreateProblem(Dataset dataset) {
    156       IProblem problem;
    157       if (ctx.Problems.Count() == 0)
    158         problem = PersistProblem(dataset);
    159       else
    160         problem = ctx.Problems.Single();
    161       if (problem.Dataset.ToString() != dataset.ToString())
    162         throw new InvalidOperationException("Could not persist dataset. The database already contains a different dataset.");
    163       return problem;
    164     }
    165 
    166     public IProblem PersistProblem(Dataset dataset) {
     148    }
     149
     150    public void PersistProblem(Dataset dataset) {
    167151      Problem problem;
    168152      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     
    176160        ctx.SubmitChanges();
    177161      }
    178       return problem;
    179     }
    180 
    181     #endregion
    182 
    183     #region Algorithm
    184     public Algorithm GetOrCreateAlgorithm(string name, string description) {
     162    }
     163
     164    public IVariable GetVariable(string variableName) {
     165      var variables = ctx.Variables.Where(v => v.Name == variableName);
     166      if (variables.Count() != 1)
     167        throw new ArgumentException("Zero or more than one variable with the name " + variableName + " are persisted in the database.");
     168      return variables.Single();
     169    }
     170
     171    public IPredictor GetModelPredictor(IModel model) {
     172      var data = (from md in ctx.ModelData
     173                  where md.Model == model
     174                  select md);
     175      if (data.Count() != 1)
     176        throw new ArgumentException("No predictor persisted for given model!");
     177      return (IPredictor)PersistenceManager.RestoreFromGZip(data.Single().Data);
     178    }
     179
     180    public void PersistPredictor(IModel model, IPredictor predictor) {
     181      Model m = (Model)model;
     182      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     183        ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
     184        ctx.ModelResults.DeleteAllOnSubmit(ctx.ModelResults.Where(x => x.Model == m));
     185        ctx.InputVariableResults.DeleteAllOnSubmit(ctx.InputVariableResults.Where(x => x.Model == m));
     186        ctx.InputVariables.DeleteAllOnSubmit(ctx.InputVariables.Where(x => x.Model == m));
     187
     188        ctx.ModelData.InsertOnSubmit(new ModelData(m, PersistenceManager.SaveToGZip(predictor)));
     189        foreach (string variableName in predictor.GetInputVariables())
     190          ctx.InputVariables.InsertOnSubmit(new InputVariable(m, (Variable)GetVariable(variableName)));
     191
     192        ctx.SubmitChanges();
     193      }
     194    }
     195
     196    public IAlgorithm GetOrPersistAlgorithm(string algorithmName) {
    185197      Algorithm algorithm;
    186198      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    187         var algorithms = from algo in ctx.Algorithms
    188                          where algo.Name == name && algo.Description == description
    189                          select algo;
     199        var algorithms = ctx.Algorithms.Where(algo => algo.Name == algorithmName);
    190200        if (algorithms.Count() == 0) {
    191           algorithm = new Algorithm(name, description);
     201          algorithm = new Algorithm(algorithmName, "");
    192202          ctx.Algorithms.InsertOnSubmit(algorithm);
    193203          ctx.SubmitChanges();
     
    195205          algorithm = algorithms.Single();
    196206        else
    197           throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + name + " are saved in database.");
     207          throw new ArgumentException("Could not get Algorithm. More than one algorithm with the name " + algorithmName + " are saved in database.");
    198208      }
    199209      return algorithm;
    200210    }
    201211
    202     public IEnumerable<IAlgorithm> GetAllAlgorithms() {
    203       return ctx.Algorithms.ToList().Cast<IAlgorithm>();
    204     }
    205     #endregion
    206 
    207     #region Variables
    208     public Dictionary<string, Variable> GetAllVariables() {
    209       Dictionary<string, Variable> dict = new Dictionary<string, Variable>();
    210       dict = ctx.Variables.ToDictionary<Variable, string>(delegate(Variable v) { return v.Name; });
    211       return dict;
    212     }
    213     #endregion
    214 
    215     #region Result
    216     public Result GetOrCreateResult(string name) {
     212    public IResult GetOrPersistResult(string resultName) {
    217213      Result result;
    218214      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    219         var results = from r in ctx.Results
    220                       where r.Name == name
    221                       select r;
     215        var results = ctx.Results.Where(r => r.Name == resultName);
    222216        if (results.Count() == 0) {
    223           result = new Result(name);
     217          result = new Result(resultName);
    224218          ctx.Results.InsertOnSubmit(result);
    225219          ctx.SubmitChanges();
     
    227221          result = results.Single();
    228222        else
    229           throw new ArgumentException("Could not get result. More than one result with the name " + name + " are saved in database.");
     223          throw new ArgumentException("Could not get result. More than one result with the name " + resultName + " are saved in database.");
    230224      }
    231225      return result;
    232226    }
    233227
    234     public IEnumerable<IResult> GetAllResults() {
    235       return ctx.Results.ToList().Cast<IResult>();
    236     }
    237 
    238     public IEnumerable<IResult> GetAllResultsForInputVariables() {
    239       return (from ir in ctx.InputVariableResults select ir.Result).Distinct().ToList().Cast<IResult>();
    240     }
    241 
    242     #endregion
    243 
    244     #region ModelResult
    245     public IEnumerable<IModelResult> GetModelResults(IModel model) {
    246       var results = from result in ctx.ModelResults
    247                     where result.Model == model
    248                     select result;
    249       return results.ToList().Cast<IModelResult>();
    250     }
    251     #endregion
    252 
    253     #region InputVariableResults
    254     public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
    255       var inputResults = from ir in ctx.InputVariableResults
    256                          where ir.Model == model
    257                          select ir;
    258       return inputResults.ToList().Cast<IInputVariableResult>();
    259     }
    260 
    261     #endregion
    262 
    263     #region ModelMetaData
    264     public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) {
    265       var metadata = from md in ctx.ModelMetaData
    266                      where md.Model == model
    267                      select md;
    268       return metadata.ToList().Cast<IModelMetaData>();
    269     }
    270     #endregion
    271 
    272     #region MetaData
    273     public IEnumerable<IMetaData> GetAllMetaData() {
    274       return ctx.MetaData.ToList().Cast<IMetaData>();
    275     }
    276 
    277     public MetaData GetOrCreateMetaData(string name) {
     228    public IMetaData GetOrPersistMetaData(string metaDataName) {
    278229      MetaData metadata;
    279230      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
    280         var md = from r in ctx.MetaData
    281                  where r.Name == name
    282                  select r;
     231        var md = ctx.MetaData.Where(r => r.Name == metaDataName);
    283232        if (md.Count() == 0) {
    284           metadata = new MetaData(name);
     233          metadata = new MetaData(metaDataName);
    285234          ctx.MetaData.InsertOnSubmit(metadata);
    286235          ctx.SubmitChanges();
     
    288237          metadata = md.Single();
    289238        else
    290           throw new ArgumentException("Could not get metadata. More than one metadata with the name " + name + " are saved in database.");
     239          throw new ArgumentException("Could not get metadata. More than one metadata with the name " + metaDataName + " are saved in database.");
    291240      }
    292241      return metadata;
    293242    }
    294243
    295     #endregion
    296 
    297     #region Model
    298     public IEnumerable<IModel> GetAllModels() {
    299       return ctx.Models.ToList().Cast<IModel>();
    300     }
    301 
    302     public void UpdateModel(IModel model) {
     244    public IEnumerable<IModelResult> GetModelResults(IModel model) {
     245      return ctx.ModelResults.Where(mr => mr.Model == model).Cast<IModelResult>();
     246    }
     247    public IEnumerable<IInputVariableResult> GetInputVariableResults(IModel model) {
     248      return ctx.InputVariableResults.Where(ivr => ivr.Model == model).Cast<IInputVariableResult>();
     249    }
     250    public IEnumerable<IModelMetaData> GetModelMetaData(IModel model) {
     251      return ctx.ModelMetaData.Where(md => md.Model == model).Cast<IModelMetaData>();
     252    }
     253
     254    public IModelResult CreateModelResult(IModel model, IResult result, double value) {
    303255      Model m = (Model)model;
    304       Model orginal = ctx.Models.GetOriginalEntityState(m);
    305       if (orginal == null)
    306         ctx.Models.Attach(m);
    307       ctx.Refresh(RefreshMode.KeepCurrentValues, m);
    308       ctx.SubmitChanges();
    309     }
    310 
    311     public byte[] GetModelData(IModel model) {
    312       var data = (from md in ctx.ModelData
    313                   where md.Model == model
    314                   select md);
    315       if (data.Count() == 0)
    316         return null;
    317       return data.Single().Data;
    318     }
    319 
    320     public void UpdateModelData(IModel model, byte[] modelData) {
     256      Result r = (Result)result;
     257      return new ModelResult(m, r, value);
     258    }
     259
     260    public void PersistModelResults(IModel model,IEnumerable<IModelResult> modelResults) {
     261      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     262        ctx.ModelResults.DeleteAllOnSubmit(GetModelResults(model).Cast<ModelResult>());
     263        ctx.ModelResults.InsertAllOnSubmit(modelResults.Cast<ModelResult>());
     264        ctx.SubmitChanges();
     265      }
     266    }
     267
     268    public IInputVariableResult CreateInputVariableResult(IInputVariable inputVariable, IResult result, double value) {
     269      InputVariable i = (InputVariable)inputVariable;
     270      Result r = (Result)result;
     271      return new InputVariableResult(i, r, value);
     272    }
     273
     274    public void PersistInputVariableResults(IModel model,IEnumerable<IInputVariableResult> inputVariableResults) {
     275      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     276        ctx.InputVariableResults.DeleteAllOnSubmit(GetInputVariableResults(model).Cast<InputVariableResult>());
     277        ctx.InputVariableResults.InsertAllOnSubmit(inputVariableResults.Cast<InputVariableResult>());
     278        ctx.SubmitChanges();
     279      }
     280    }
     281
     282    public IModelMetaData CreateModelMetaData(IModel model, IMetaData metadata, double value) {
    321283      Model m = (Model)model;
    322       ctx.ModelData.DeleteAllOnSubmit(ctx.ModelData.Where(x => x.Model == m));
    323       ctx.ModelData.InsertOnSubmit(new ModelData(m, modelData));
    324       ctx.SubmitChanges();
    325     }
    326 
    327     public IPredictor GetModelPredictor(IModel model) {
    328       byte[] data = GetModelData(model);
    329       return (IPredictor)PersistenceManager.RestoreFromGZip(data);
    330     }
    331     #endregion
     284      MetaData md = (MetaData)metadata;
     285      return new ModelMetaData(m, md, value);
     286    }
     287
     288    public void PersistModelMetaData(IModel model, IEnumerable<IModelMetaData> modelMetaData) {
     289      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     290        ctx.ModelMetaData.DeleteAllOnSubmit(GetModelMetaData(model).Cast<ModelMetaData>());
     291        ctx.ModelMetaData.InsertAllOnSubmit(modelMetaData.Cast<ModelMetaData>());
     292        ctx.SubmitChanges();
     293      }
     294    }
     295
     296    public IModel Persist(HeuristicLab.Modeling.IAlgorithm algorithm) {
     297      if (ctx.Problems.Count() == 0)
     298        PersistProblem(algorithm.Dataset);
     299      return Persist(algorithm.Model, algorithm.Name, algorithm.Description);
     300    }
     301
     302    public IModel Persist(HeuristicLab.Modeling.IAnalyzerModel model, string algorithmName, string algorithmDescription) {
     303      Algorithm algorithm = (Algorithm) GetOrPersistAlgorithm(algorithmName);
     304      Variable targetVariable  = (Variable)GetVariable (model.TargetVariable);
     305
     306      Model m = (Model)CreateModel(model.Type, algorithm, targetVariable, model.TrainingSamplesStart, model.TrainingSamplesEnd,
     307        model.ValidationSamplesStart, model.ValidationSamplesEnd, model.TestSamplesStart, model.TestSamplesEnd);
     308      PersistModel(m);
     309      PersistPredictor(m, model.Predictor);
     310
     311      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     312        foreach (KeyValuePair<string, double> pair in model.MetaData) {
     313          MetaData metaData = (MetaData)GetOrPersistMetaData(pair.Key);
     314          ctx.ModelMetaData.InsertOnSubmit(new ModelMetaData(m, metaData, pair.Value));
     315        }
     316        ctx.SubmitChanges();
     317      }
     318
     319      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     320        foreach (KeyValuePair<ModelingResult, double> pair in model.Results) {
     321          Result result = (Result)GetOrPersistResult(pair.Key.ToString());
     322          ctx.ModelResults.InsertOnSubmit(new ModelResult(m, result, pair.Value));
     323        }
     324        ctx.SubmitChanges();
     325      }
     326
     327      using (ModelingDataContext ctx = new ModelingDataContext(connection)) {
     328        foreach (InputVariable variable in ctx.InputVariables.Where(iv => iv.Model == m)) {
     329          foreach (KeyValuePair<ModelingResult, double> variableResult in model.GetVariableResults(variable.Variable.Name)) {
     330            Result result = (Result)GetOrPersistResult(variableResult.Key.ToString());
     331            ctx.InputVariableResults.InsertOnSubmit(new InputVariableResult(variable, result, variableResult.Value));
     332          }
     333        }
     334        ctx.SubmitChanges();
     335      }
     336
     337      //if connected to database return inserted model
     338      if (this.ctx != null)
     339        return this.ctx.Models.Where(x => x.Id == m.Id).Single();
     340      return null;
     341    }
    332342  }
    333343}
Note: See TracChangeset for help on using the changeset viewer.