public void Read(string query) { TraceListeners.Log(TraceEventType.Information, 0, "ReadMySQL::Read...", false, true); TraceListeners.Log(TraceEventType.Information, 0, "...Query: " + query, false, true); try { _conn.Open(); MySqlCommand cmd = new MySqlCommand(query, _conn); _reader = cmd.ExecuteReader(); } catch (Exception e) { try { if (_conn != null) { _conn.Close(); } } catch (Exception ei) { TraceListeners.Log(TraceEventType.Error, 0, ei.ToString(), true, true); } // MySqlException TraceListeners.Log(TraceEventType.Error, 0, e.ToString(), true, true); } }
public void Create(string modellingSetting) { TraceListeners.Log(TraceEventType.Information, 0, "Model::Create " + modellingSetting + "...", false, true); string[] labels = ConstantsModellings.DIC[ConstantsModellings.Keys.BPM][ConstantsModellings.KeysModels.labels]; double sparsityApproxThresh = System.Convert.ToDouble(ConstantsModellings.DIC[ConstantsModellings.Keys.BPM][ConstantsModellings.KeysModels.SparsityApproxThresh][0]); switch (modellingSetting) { case "BPM": _model = new BPM( labels: labels, sparsityApproxThresh: sparsityApproxThresh); break; case "BPMMapped": _model = new BPMMapped( labels: labels); break; default: TraceListeners.Log(TraceEventType.Error, 0, "Invalid Model Selected: " + modellingSetting, true, true); _model = null; break; } }
/// <summary> /// Initialise the logger and constants /// </summary> /// <param name="args">The command line arguments</param> public void InitializeApp(string[] args) { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::InitializeApp...", false, true); _modellingProfile = args[0]; // Loggers if (args.Length == 2 && args[1].ToLower() == "verbose") { _verbose = true; TraceListeners.Initialise( _verbose, Constants.GENERAL_LOG_PATH, Constants.DIAGNOSTIC_LOG_PATH); } else { TraceListeners.Initialise(_verbose, Constants.GENERAL_LOG_PATH, Constants.DIAGNOSTIC_LOG_PATH); } // Constants Constants.Initialise(); ConstantsProfiles.Initialise(); ConstantsModellings.Initialise(); }
public static void Initialise() { if (_MODELLING_SETTINGS != null) { foreach (ConfigurationSection section in _MODELLING_SETTINGS.Sections) { string sectionName = section.SectionInformation.SectionName.Split('/')[1]; NameValueCollection sectionCollection = (NameValueCollection)ConfigurationManager.GetSection( section.SectionInformation.SectionName); TraceListeners.Log(TraceEventType.Information, 0, "...Reading Model Configuration: " + sectionName, false, true); DIC[(Keys)Enum.Parse(typeof(Keys), sectionName)] = new Dictionary <KeysModels, string[]>(); switch (sectionName) { case ("BPM"): SetModelBPM(sectionCollection); break; case ("HMM"): SetModelHMM(sectionCollection); break; default: TraceListeners.Log(TraceEventType.Error, 0, "Invalid model in the modelling settings: " + sectionName, true, true); break; } } } }
public override void Train( string outputModelFileName, int iterationCount, bool computeModelEvidence, int batchCount, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, double noise) { TraceListeners.Log(TraceEventType.Warning, 0, "Advanced setting will not be used: " + "distributionName, inferenceEngineAlgorithm & noise.", false, true); // Validate _validate.Train( outputModelFileName: outputModelFileName, iterationCount: iterationCount, batchCount: batchCount); Train( outputModelFileName, iterationCount, computeModelEvidence, batchCount); }
public static void Initialise() { if (_MODELLING_PROFILES != null) { foreach (ConfigurationSection section in _MODELLING_PROFILES.Sections) { string sectionName = section.SectionInformation.SectionName.Split('/')[1]; NameValueCollection sectionCollection = (NameValueCollection)ConfigurationManager.GetSection( section.SectionInformation.SectionName); TraceListeners.Log(TraceEventType.Information, 0, "...Reading Profile Configuration: " + sectionName, false, true); DIC[sectionName] = new Dictionary <Keys, string[]>(); DIC[sectionName][Keys.ModellingSetting] = sectionCollection["ModellingSetting"].ToString().Split(';'); DIC[sectionName][Keys.ModelName] = sectionCollection["ModelName"].ToString().Split(';'); DIC[sectionName][Keys.ModelProcedure] = sectionCollection["ModelProcedure"].ToString().Split(';'); DIC[sectionName][Keys.SamplingsCombName] = sectionCollection["SamplingsCombName"].ToString().Split(';'); DIC[sectionName][Keys.ModelingsGroup] = sectionCollection["ModelingsGroup"].ToString().Split(';'); DIC[sectionName][Keys.Submodels] = sectionCollection["Submodels"].ToString().Split(';'); DIC[sectionName][Keys.SubmodelsNumCond] = sectionCollection["SubmodelsNumCond"].ToString().Split(';'); } } }
private Variable <Vector> InitialiseWeights( DistributionType distributionType, DistributionName distributionName, int dimension, string[] hyperParameters) { switch (distributionName) { case DistributionName.GaussianDefault: return(Variable.Random(new VectorGaussian( Vector.Zero(dimension), PositiveDefiniteMatrix.Identity(dimension))).Named( "w." + distributionType.ToString())); case DistributionName.GaussianInit: return(Variable <Vector> .Random( Variable.New <VectorGaussian>().Named( "w." + distributionType.ToString()))); default: TraceListeners.Log(TraceEventType.Error, 0, "Invalid distribution name: " + distributionName.ToString(), true, true); return(null); } }
/// <summary> /// Manage the getting data, model training, model testing, model validation and reporting. /// </summary> public void AnalysisProcessManager( string modelName, string modelProcedure, string modellingGroup, string submodel, string submodelCondValue, Dictionary <DatasetName, string> samplingsCombDatabase, Dictionary <DatasetName, string> samplingsCombSample) { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::AnalysisProcessManager...", false, true); // Get data, and train ReadInputData(modelName, modelProcedure, modellingGroup, submodel, submodelCondValue, DatasetName.Train, samplingsCombDatabase, samplingsCombSample); Train(true); // Clear features from the model instance (saveing memory) RemoveInputData(); // Get data, and test ReadInputData(modelName, modelProcedure, modellingGroup, submodel, submodelCondValue, DatasetName.Test, samplingsCombDatabase, samplingsCombSample); Predict(true); // Evaluations and validations Evaluate(); DiagnoseTRain(); CrossValidate(); // Clear features from the model instance (saveing memory) RemoveInputData(); }
public void Dataset( string[] labels, DatasetName datasetName, object[,] x, object[] y) { if (x == null || y == null || x.Rank != 2 || x.GetLength(0) == 0 || y.Length == 0) { TraceListeners.Log(TraceEventType.Error, 0, "Invalid number of records in the " + datasetName.ToString() + " dataset!", true, true); } if (x.GetLength(1) != y.Count()) { TraceListeners.Log(TraceEventType.Error, 0, "Number of observations in features and target do not match!", true, true); } if (y.All(v => (string)v != labels[0]) || y.All(v => (string)v != labels[1])) { TraceListeners.Log(TraceEventType.Error, 0, "All the labels must be present in the " + datasetName.ToString() + " dataset!", true, true); } if (x.GetLength(1) < x.GetLength(0)) { TraceListeners.Log(TraceEventType.Error, 0, "Number of observations is less than number of features!", true, true); } }
public override void Train( string outputModelFileName, int iterationCount, bool computeModelEvidence, int batchCount, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, double noise) { TraceListeners.Log(TraceEventType.Warning, 0, "Advanced setting will not be used: " + "batchCount.", false, true); // Validate _validate.Train( outputModelFileName: outputModelFileName, iterationCount: iterationCount, batchCount: batchCount); // The inference engine _engine[DistributionType.Prior] = SetInferenceEngine( inferenceEngineAlgorithm, iterationCount); // Initialise prior weights _w[DistributionType.Prior] = InitialiseWeights( distributionType: DistributionType.Prior, distributionName: distributionName, dimension: _numFeatures, hyperParameters: null); // BPM BayesPointMachine(_x, _y, _w[DistributionType.Prior], noise); }
/// <summary> /// Predict /// </summary> public void Predict(bool removeInputData) { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::Predict...", false, true); // Set noise double noise = System.Convert.ToDouble( ConstantsModellings.DIC[ConstantsModellings.Keys.BPM][ConstantsModellings.KeysModels.NoiseTest][0]); // Clear the features from the Feature instance (saveing memory) if (removeInputData) { _features.RemoveFeature(DatasetName.Test); } // Initialise features _model.InitialiseFeatures( DatasetName.Test, _features.GetFeaturesConverted(DatasetName.Test), _features.GetTargetConverted(DatasetName.Test)); // Clear the features converted from the Feature instance (saveing memory) _features.RemoveFeatureConverted(DatasetName.Test); // Predict _model.Predict( inputModelFileName: Constants.OUTPUT_PATH + "model_bpm.mdl", distributionName: DistributionName.GaussianDefault, inferenceEngineAlgorithm: InferenceAlgorithm.EP, noise: noise); }
/// <summary> /// The Main function for execution /// </summary> /// <param name="args">The command line arguments</param> static void Main(string[] args) { // Check input arguments if (args.Length != 1 & args.Length != 2) { TraceListeners.Log(TraceEventType.Information, 0, "Please enter the input arguments!" + "\n Compulsory Input Arguments:\n " + "[1] Model Name \n" + "\n Optional Input Arguments:\n " + "[2] Verbose Log (\"verbose\") \n", false, true); Console.ReadKey(); return; } // Main TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain...", false, true); TraceListeners.Log(TraceEventType.Information, 0, "...Arguments: " + string.Join(", ", args), false, true); ClassifierMainBPM classifierMainBPM = new ClassifierMainBPM(); classifierMainBPM.InitializeApp(args); classifierMainBPM.AnalysisProcessManagerGroup( classifierMainBPM._modellingProfile); TraceListeners.Log(TraceEventType.Information, 0, "Fin!", false, true); Console.ReadKey(); }
public virtual void InitialiseFeatures( DatasetName datasetName, object[,] features, object[] target) { TraceListeners.Log(TraceEventType.Information, 0, "InitialiseVariables...", false, true); _model.InitialiseFeatures(datasetName, features, target); }
public void ValidatePredict( Vector[] xTrain, Vector[] xTest) { if (xTrain[0].Count() != xTest[0].Count()) { TraceListeners.Log(TraceEventType.Error, 0, "Number of features in train and test do not match!", true, true); } }
public Dictionary <string, List <string> > GetColumns(String[] varNames) { TraceListeners.Log(TraceEventType.Information, 0, "ReadMySQL::GetColumns...", false, true); Dictionary <string, List <string> > table = new Dictionary <string, List <String> >(); foreach (string name in varNames) { table[name] = new List <string>(); } try { using (_conn) { while (_reader.Read()) { foreach (string varName in varNames) { //TraceListeners.Log(TraceEventType.Information, 0, // "Get column: " + varName, false); if (_reader[varName] == DBNull.Value) { table[varName].Add(_mysqlNullDefault); } else { table[varName].Add(_reader.GetString(varName)); } } } } } catch (Exception e) { // MySqlException, System.Data.SqlTypes.SqlNullValueException CloseConnection(); TraceListeners.Log(TraceEventType.Error, 0, e.ToString(), true, true); } finally { try { if (_reader != null) { _reader.Close(); _reader = null; } } catch (Exception e) { TraceListeners.Log(TraceEventType.Error, 0, e.ToString(), true, true); } } return(table); }
private bool FileExist(string fileName) { if (!string.IsNullOrEmpty(fileName) && !File.Exists(@fileName)) { TraceListeners.Log(TraceEventType.Error, 0, "The following file can not be reached:\n" + fileName, true, true); return(false); } return(true); }
public object[] GetTargetConverted(DatasetName datasetName) { // Evaluate if (!_targetConverted.ContainsKey(datasetName)) { TraceListeners.Log(TraceEventType.Error, 0, "Dataset does not exist: " + datasetName.ToString(), true, true); } // Get return(_targetConverted[datasetName]); }
public string GetVariableType(string varName) { // Evaluate if (!_featuresTypes.ContainsKey(varName)) { TraceListeners.Log(TraceEventType.Error, 0, "Feature does not exist: " + varName, true, true); } // Get return(_featuresTypes[varName]); }
public string[,] GetDataset(DatasetName datasetName) { // Evaluate if (!_datasets.ContainsKey(datasetName)) { TraceListeners.Log(TraceEventType.Error, 0, "Dataset does not exist: " + datasetName.ToString(), true, true); } // Get return(_datasets[datasetName]); }
/// <summary> /// Cross validate /// </summary> public void CrossValidate() { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::CrossValidate...", false, true); _model.CrossValidate( outputModelFileName: Constants.OUTPUT_PATH + "report_crossValidate.txt", crossValidationFoldCount: 3, iterationCount: 30, computeModelEvidence: true, batchCount: 1); }
/// <summary> /// Diagnose train /// </summary> public void DiagnoseTRain() { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::DiagnoseTRain...", false, true); _model.DiagnoseTrain( outputModelFileName: null, reportFileName: Constants.OUTPUT_PATH + "report_diagnoseTrain.txt", iterationCount: 30, computeModelEvidence: true, batchCount: 1); }
private bool FilePathExist(string fileName) { string pathName = Path.GetDirectoryName(fileName); if (!string.IsNullOrEmpty(pathName) && !Directory.Exists(@pathName)) { TraceListeners.Log(TraceEventType.Error, 0, "The following directory path can not be reached:\n" + pathName, true, true); return(false); } return(true); }
private bool AvailableDataset( DatasetName availableDatasetName, DatasetName datasetName) { if (availableDatasetName != datasetName) { TraceListeners.Log(TraceEventType.Error, 0, "The requested database is not valid or is not loaed into the model!", true, true); return(false); } return(true); }
public virtual void Predict( string inputModelFileName, DistributionName distributionName = DistributionName.Null, InferenceAlgorithm inferenceEngineAlgorithm = InferenceAlgorithm.Null, double noise = 0) { TraceListeners.Log(TraceEventType.Information, 0, "Predict...", false, true); _model.Predict( inputModelFileName, distributionName, inferenceEngineAlgorithm, noise); }
/// <summary> /// Get train and test datsets. /// </summary> public void ReadInputData( string modelName, string modelProcedure, string modellingGroup, string submodel, string submodelCondValue, DatasetName datasetName, Dictionary <DatasetName, string> samplingsCombDatabase, Dictionary <DatasetName, string> samplingsCombSample) { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::ReadInputData...", false, true); string query; string[] featureNames; // Read query = "CALL " + Constants.MYSQL_DATABASES_MYSQL[samplingsCombDatabase[datasetName]] + "." + modelName + "(\"" + modelProcedure + "\", \"" + modellingGroup + "\", \"" + submodel + "\"," + submodelCondValue + ",\"" + samplingsCombSample[datasetName] + "\");"; ReadMySQL reader = new ReadMySQL( samplingsCombDatabase[datasetName], Constants.MYSQL_SERVER, Constants.MYSQL_USERNAME, Constants.MYSQL_PASSWORD, Constants.MYSQL_DATABASES_MYSQL[samplingsCombDatabase[datasetName]], Constants.MYSQL_CMD_TIMEOUT, Constants.MYSQL_CONN_TIMEOUT, Constants.MYSQL_NULL_DEFAULT.ToString()); reader.Read(query); // Set featureNames = reader.GetColumnsNames(); _features.Set( dataset: reader.GetColumns(featureNames), datasetName: datasetName, featuresByOrder: featureNames); // Convert & Transform _features.ConvertFeatures( datasetName: datasetName, standardConversion: true); // Close reader.CloseConnection(); }
public virtual void DiagnoseTrain( string outputModelFileName, string reportFileName, int iterationCount, bool computeModelEvidence = false, int batchCount = 1) { TraceListeners.Log(TraceEventType.Information, 0, "DiagnoseTrain...", false, true); _model.DiagnoseTrain( outputModelFileName, reportFileName, iterationCount, computeModelEvidence, batchCount); }
/// <summary> /// Evalaute /// </summary> public void Evaluate() { TraceListeners.Log(TraceEventType.Information, 0, "ClassifierMain::Evaluate...", false, true); _model.Evaluate( inputModelFileName: Constants.OUTPUT_PATH + "model_bpm.mdl", reportFileName: Constants.OUTPUT_PATH + "report_evaluation.txt", positiveClassLabel: "1", groundTruthFileName: Constants.OUTPUT_PATH + "report_GroundTruth.txt", predictionsFileName: Constants.OUTPUT_PATH + "report_Predictions.txt", weightsFileName: Constants.OUTPUT_PATH + "report_Weights.txt", calibrationCurveFileName: Constants.OUTPUT_PATH + "report_Clibration_Curve.txt", precisionRecallCurveFileName: Constants.OUTPUT_PATH + "report_PrecisionRecall_Curve.txt", rocCurveFileName: Constants.OUTPUT_PATH + "report_ROC_Curve.txt"); }
public virtual IDictionary <string, double> PredictInstance( string inputModelFileName, DistributionName distributionName, InferenceAlgorithm inferenceEngineAlgorithm, int instance, double noise = 0) { TraceListeners.Log(TraceEventType.Information, 0, "PredictInstance...", false, true); return(_model.PredictInstance( inputModelFileName, distributionName, inferenceEngineAlgorithm, instance, noise)); }
public virtual void CrossValidate( string outputModelFileName, int crossValidationFoldCount, int iterationCount, bool computeModelEvidence = false, int batchCount = 1) { TraceListeners.Log(TraceEventType.Information, 0, "CrossValidate...", false, true); _model.CrossValidate( outputModelFileName, crossValidationFoldCount, iterationCount, computeModelEvidence, batchCount); }
private Variable <Vector> InferWeights( DistributionType distributionType, DistributionName distributionName, string[] hyperParameters) { switch (distributionName) { case DistributionName.GaussianDefault: VectorGaussian wObserved = _engine[distributionType].Infer <VectorGaussian>(_w[DistributionType.Prior]); return(Variable.Random(wObserved).Named("w." + distributionType.ToString())); default: TraceListeners.Log(TraceEventType.Error, 0, "Invalid distribution name: " + distributionName.ToString(), true, true); return(null); } }