private static DocumentMapping CreateMapping(Expression <Func <Person, object> > expression, string name = null, params FieldMapping[] otherMappings) { var identity = new IdentityMapping(CreateMember(x => x.Id)); var fields = new[] { CreateFieldMapping(expression, name) }.Concat(otherMappings); return(new DocumentMapping(typeof(Person), identity, fields)); }
/// <summary>Read in static rating data from a TextReader</summary> /// <param name="reader">the <see cref="TextReader"/> to read from</param> /// <param name="size">the number of ratings in the file</param> /// <param name="user_mapping">mapping object for user IDs</param> /// <param name="item_mapping">mapping object for item IDs</param> /// <param name="rating_type">the data type to be used for storing the ratings</param> /// <param name="ignore_first_line">if true, ignore the first line</param> /// <returns>the rating data</returns> static public IRatings Read( TextReader reader, int size, IMapping user_mapping = null, IMapping item_mapping = null, RatingType rating_type = RatingType.FLOAT, bool ignore_first_line = false) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (ignore_first_line) { reader.ReadLine(); } IRatings ratings; if (rating_type == RatingType.BYTE) { ratings = new StaticByteRatings(size); } else if (rating_type == RatingType.FLOAT) { ratings = new StaticRatings(size); } else { throw new FormatException(string.Format("Unknown rating type: {0}", rating_type)); } string line; while ((line = reader.ReadLine()) != null) { if (line.Length == 0) { continue; } string[] tokens = line.Split(Constants.SPLIT_CHARS); if (tokens.Length < 3) { throw new FormatException("Expected at least 3 columns: " + line); } int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture); ratings.Add(user_id, item_id, rating); } ratings.InitScale(); return(ratings); }
public int InsertIdentityMapping(IdentityMapping data) { int affetcedRows = -1; string tabName = "IdentityMapping"; affetcedRows = DBSQL.InsertOperation(GRConnectionString, tabName, data); return(affetcedRows); }
/// <summary>Rate a given set of instances and write it to a TextWriter</summary> /// <param name="recommender">rating predictor</param> /// <param name="ratings">test cases</param> /// <param name="writer">the TextWriter to write the predictions to</param> /// <param name="user_mapping">an <see cref="Mapping"/> object for the user IDs</param> /// <param name="item_mapping">an <see cref="Mapping"/> object for the item IDs</param> /// <param name="line_format">a format string specifying the line format; {0} is the user ID, {1} the item ID, {2} the rating</param> /// <param name="header">if specified, write this string at the start of the output</param> public static void WritePredictions( this IRecommender recommender, IRatings ratings, TextWriter writer, IMapping user_mapping = null, IMapping item_mapping = null, string line_format = "{0}\t{1}\t{2}", string header = null) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (header != null) { writer.WriteLine(header); } if (line_format == "ranking") { foreach (int user_id in ratings.AllUsers) { if (ratings.ByUser[user_id].Count > 0) { recommender.WritePredictions( user_id, new List <int>(from index in ratings.ByUser[user_id] select ratings.Items[index]), new int[] { }, ratings.ByUser[user_id].Count, writer, user_mapping, item_mapping); } } } else { for (int index = 0; index < ratings.Count; index++) { writer.WriteLine( string.Format( CultureInfo.InvariantCulture, line_format, user_mapping.ToOriginalID(ratings.Users[index]), item_mapping.ToOriginalID(ratings.Items[index]), recommender.Predict(ratings.Users[index], ratings.Items[index]) ) ); } } }
private static DocumentMapping CreateMappingForIdentity(Expression <Func <Person, object> > expression, string name = null) { var identity = new IdentityMapping(CreateMember(expression)); if (name != null) { identity.Name = name; } return(new DocumentMapping(typeof(Person), identity, Enumerable.Empty <FieldMapping>())); }
/// <summary>Read in implicit feedback data from a TextReader</summary> /// <param name="reader">the TextReader to be read from</param> /// <param name="user_mapping">user <see cref="IMapping"/> object</param> /// <param name="item_mapping">item <see cref="IMapping"/> object</param> /// <param name="ignore_first_line">if true, ignore the first line</param> /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns> static public IPosOnlyFeedback Read(TextReader reader, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (ignore_first_line) { reader.ReadLine(); } var feedback = new PosOnlyFeedback <SparseBooleanMatrix>(); string line; while ((line = reader.ReadLine()) != null) { if (line.Trim().Length == 0) { continue; } string[] tokens = line.Split(Constants.SPLIT_CHARS); if (tokens.Length < 2) { throw new FormatException("Expected at least 2 columns: " + line); } try { int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); feedback.Add(user_id, item_id); } catch (Exception) { throw new FormatException(string.Format("Could not read line '{0}'", line)); } } return(feedback); }
public void TestCase() { string filename = "../../../../tests/example.test"; var mapping = new IdentityMapping(); var ratings = RatingData.Read(filename); var recommender = new ExternalRatingPredictor() { PredictionFile = filename, UserMapping = mapping, ItemMapping = mapping }; recommender.Train(); for (int i = 0; i < ratings.Count; i++) { Assert.AreEqual(ratings[i], recommender.Predict(ratings.Users[i], ratings.Items[i])); } }
/// <summary>Read in rating data from a TextReader</summary> /// <param name="reader">the <see cref="TextReader"/> to read from</param> /// <param name="user_mapping">mapping object for user IDs</param> /// <param name="item_mapping">mapping object for item IDs</param> /// <param name="test_rating_format">whether there is a rating column in each line or not</param> /// <returns>the rating data</returns> static public ITimedRatings Read( TextReader reader, IMapping user_mapping = null, IMapping item_mapping = null, TestRatingFileFormat test_rating_format = TestRatingFileFormat.WITH_RATINGS) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } var ratings = new TimedRatings(); string[] separators = { "::" }; string line; int seconds_pos = test_rating_format == TestRatingFileFormat.WITH_RATINGS ? 3 : 2; while ((line = reader.ReadLine()) != null) { string[] tokens = line.Split(separators, StringSplitOptions.None); if (test_rating_format == TestRatingFileFormat.WITH_RATINGS && tokens.Length < 4) { throw new FormatException("Expected at least 4 columns: " + line); } if (test_rating_format == TestRatingFileFormat.WITHOUT_RATINGS && tokens.Length < 3) { throw new FormatException("Expected at least 3 columns: " + line); } int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); float rating = test_rating_format == TestRatingFileFormat.WITH_RATINGS ? float.Parse(tokens[2], CultureInfo.InvariantCulture) : 0; long seconds = uint.Parse(tokens[seconds_pos]); var time = new DateTime(seconds * 10000000L).AddYears(1969); var offset = TimeZone.CurrentTimeZone.GetUtcOffset(time); time -= offset; ratings.Add(user_id, item_id, rating, time); } return(ratings); }
/// <summary>Read in rating data which will be interpreted as implicit feedback data from a TextReader</summary> /// <param name="reader">the TextReader to be read from</param> /// <param name="rating_threshold">the minimum rating value needed to be accepted as positive feedback</param> /// <param name="user_mapping">user <see cref="IMapping"/> object</param> /// <param name="item_mapping">item <see cref="IMapping"/> object</param> /// <param name="ignore_first_line">if true, ignore the first line</param> /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns> static public IPosOnlyFeedback Read(TextReader reader, float rating_threshold, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (ignore_first_line) { reader.ReadLine(); } var feedback = new PosOnlyFeedback <SparseBooleanMatrix>(); string line; while ((line = reader.ReadLine()) != null) { if (line.Trim().Length == 0) { continue; } string[] tokens = line.Split(Constants.SPLIT_CHARS); if (tokens.Length < 3) { throw new FormatException("Expected at least 3 columns: " + line); } int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture); if (rating >= rating_threshold) { feedback.Add(user_id, item_id); } } return(feedback); }
Read(TextReader reader, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (ignore_first_line) { reader.ReadLine(); } var ratings = new Ratings(); string line; while ((line = reader.ReadLine()) != null) { if (line.Length == 0) { continue; } string[] tokens = line.Split(Constants.SPLIT_CHARS); if (tokens.Length < 3) { throw new FormatException("Expected at least 3 columns: " + line); } int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture); ratings.Add(user_id, item_id, rating); } ratings.InitScale(); return(ratings); }
public IdentityMapping GetIdentityMappingByPK(int idGR, string idCentral) { IdentityMapping data = null; string tabName = "IdentityMapping"; Dictionary <string, DBSQL.QueryCondition> conditions = new Dictionary <string, DBSQL.QueryCondition>(); conditions.Add("idGR", new DBSQL.QueryCondition { Key = "idunico", Op = DBSQL.Op.Equal, Conj = DBSQL.Conj.And, Value = idGR }); conditions.Add("idCentral", new DBSQL.QueryCondition { Key = "idext", Op = DBSQL.Op.Equal, Conj = DBSQL.Conj.None, Value = idCentral }); DataTable data_ = DBSQL.SelectOperation(GRConnectionString, tabName, conditions); log.Info(string.Format("Query Executed! Retrieved {0} records!", data_.Rows.Count)); if (data_ != null) { if (data_.Rows.Count == 1) { data = IdentityMappingMapper.DataRowToVO(data_.Rows[0]); log.Info(string.Format("{0} Records mapped to {1}", 1, typeof(IdentityMapping).ToString())); } } return(data); }
/// <summary>Write item predictions (scores) for a given user to a TextWriter object</summary> /// <param name="recommender">the <see cref="IRecommender"/> to use for making the predictions</param> /// <param name="user_id">ID of the user to make recommendations for</param> /// <param name="candidate_items">list of candidate items</param> /// <param name="ignore_items">list of items for which no predictions should be made</param> /// <param name="num_predictions">the number of items to return per user, -1 if there should be no limit</param> /// <param name="writer">the <see cref="TextWriter"/> to write to</param> /// <param name="user_mapping">an <see cref="IMapping"/> object for the user IDs</param> /// <param name="item_mapping">an <see cref="IMapping"/> object for the item IDs</param> static public void WritePredictions( this IRecommender recommender, int user_id, ICollection <int> candidate_items, ICollection <int> ignore_items, int num_predictions, TextWriter writer, IMapping user_mapping, IMapping item_mapping) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } var ordered_items = recommender.Recommend( user_id, n: num_predictions, ignore_items: ignore_items, candidate_items: candidate_items); writer.Write("{0}\t[", user_mapping.ToOriginalID(user_id)); if (ordered_items.Count > 0) { writer.Write("{0}:{1}", item_mapping.ToOriginalID(ordered_items[0].Item1), ordered_items[0].Item2.ToString(CultureInfo.InvariantCulture)); for (int i = 1; i < ordered_items.Count; i++) { int item_id = ordered_items[i].Item1; float score = ordered_items[i].Item2; writer.Write(",{0}:{1}", item_mapping.ToOriginalID(item_id), score.ToString(CultureInfo.InvariantCulture)); } } writer.WriteLine("]"); }
/// <summary>Read in rating data from a TextReader</summary> /// <param name="reader">the <see cref="TextReader"/> to read from</param> /// <param name="user_mapping">mapping object for user IDs</param> /// <param name="item_mapping">mapping object for item IDs</param> /// <param name="ignore_first_line">if true, ignore the first line</param> /// <returns>the rating data</returns> static public ITimedRatings Read(TextReader reader, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false) { if (user_mapping == null) { user_mapping = new IdentityMapping(); } if (item_mapping == null) { item_mapping = new IdentityMapping(); } if (ignore_first_line) { reader.ReadLine(); } var ratings = new MyMediaLite.Data.TimedRatings(); var time_split_chars = new char[] { ' ', '-', ':' }; string line; while ((line = reader.ReadLine()) != null) { if (line.Length == 0) { continue; } string[] tokens = line.Split(Constants.SPLIT_CHARS); if (tokens.Length < 4) { throw new FormatException("Expected at least 4 columns: " + line); } int user_id = user_mapping.ToInternalID(tokens[0]); int item_id = item_mapping.ToInternalID(tokens[1]); float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture); string date_string = tokens[3]; if (tokens[3].StartsWith("\"") && tokens.Length > 4 && tokens[4].EndsWith("\"")) { date_string = tokens[3] + " " + tokens[4]; date_string = date_string.Substring(1, date_string.Length - 2); } uint seconds; if (date_string.Length == 19) // format "yyyy-mm-dd hh:mm:ss" { var date_time_tokens = date_string.Split(time_split_chars); ratings.Add( user_id, item_id, rating, new DateTime( int.Parse(date_time_tokens[0]), int.Parse(date_time_tokens[1]), int.Parse(date_time_tokens[2]), int.Parse(date_time_tokens[3]), int.Parse(date_time_tokens[4]), int.Parse(date_time_tokens[5]))); } else if (date_string.Length == 10 && date_string[4] == '-') // format "yyyy-mm-dd" { var date_time_tokens = date_string.Split(time_split_chars); ratings.Add( user_id, item_id, rating, new DateTime( int.Parse(date_time_tokens[0]), int.Parse(date_time_tokens[1]), int.Parse(date_time_tokens[2]))); } else if (uint.TryParse(date_string, out seconds)) // unsigned integer value, interpreted as seconds since Unix epoch { var time = new DateTime(seconds * 10000000L).AddYears(1969); var offset = TimeZone.CurrentTimeZone.GetUtcOffset(time); ratings.Add(user_id, item_id, rating, time - offset); } else { ratings.Add(user_id, item_id, rating, DateTime.Parse(date_string, CultureInfo.InvariantCulture)); } if (ratings.Count % 200000 == 199999) { Console.Error.Write("."); } if (ratings.Count % 12000000 == 11999999) { Console.Error.WriteLine(); } } ratings.InitScale(); return(ratings); }
protected override void Run(string[] args) { if (file_format == RatingFileFormat.KDDCUP_2011) { user_mapping = new IdentityMapping(); item_mapping = new IdentityMapping(); } base.Run(args); bool do_eval = false; if (test_ratio > 0 || chronological_split != null) { do_eval = true; } if (test_file != null && !test_no_ratings) { do_eval = true; } Console.Error.WriteLine( string.Format(CultureInfo.InvariantCulture, "ratings range: [{0}, {1}]", recommender.MinRating, recommender.MaxRating)); if (test_ratio > 0) { var split = new RatingsSimpleSplit(training_data, test_ratio); recommender.Ratings = training_data = split.Train[0]; test_data = split.Test[0]; Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "test ratio {0}", test_ratio)); } if (chronological_split != null) { var split = chronological_split_ratio != -1 ? new RatingsChronologicalSplit((ITimedRatings)training_data, chronological_split_ratio) : new RatingsChronologicalSplit((ITimedRatings)training_data, chronological_split_time); recommender.Ratings = training_data = split.Train[0]; test_data = split.Test[0]; if (test_ratio != -1) { Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "test ratio (chronological) {0}", chronological_split_ratio)); } else { Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "split time {0}", chronological_split_time)); } } Console.Write(training_data.Statistics(test_data, user_attributes, item_attributes)); if (find_iter != 0) { if (!(recommender is IIterativeModel)) { Abort("Only iterative recommenders (interface IIterativeModel) support --find-iter=N."); } var iterative_recommender = recommender as IIterativeModel; iterative_recommender.NumIter = num_iter; Console.WriteLine(recommender); if (cross_validation > 1) { recommender.DoIterativeCrossValidation(cross_validation, max_iter, find_iter); } else { var eval_stats = new List <double>(); if (load_model_file == null) { recommender.Train(); } if (compute_fit) { Console.WriteLine("fit {0} iteration {1}", Render(recommender.Evaluate(training_data)), iterative_recommender.NumIter); } Console.WriteLine("{0} iteration {1}", Render(Evaluate()), iterative_recommender.NumIter); for (int it = (int)iterative_recommender.NumIter + 1; it <= max_iter; it++) { TimeSpan time = Wrap.MeasureTime(delegate() { iterative_recommender.Iterate(); }); training_time_stats.Add(time.TotalSeconds); if (it % find_iter == 0) { if (compute_fit) { time = Wrap.MeasureTime(delegate() { Console.WriteLine("fit {0} iteration {1}", recommender.Evaluate(training_data), it); }); fit_time_stats.Add(time.TotalSeconds); } EvaluationResults results = null; time = Wrap.MeasureTime(delegate() { results = Evaluate(); }); eval_time_stats.Add(time.TotalSeconds); eval_stats.Add(results[eval_measures[0]]); Console.WriteLine("{0} iteration {1}", Render(results), it); Model.Save(recommender, save_model_file, it); if (prediction_file != null) { recommender.WritePredictions(test_data, prediction_file + "-it-" + it, user_mapping, item_mapping, prediction_line, prediction_header); } if (epsilon > 0.0 && results[eval_measures[0]] - eval_stats.Min() > epsilon) { Console.Error.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0} >> {1}", results[eval_measures[0]], eval_stats.Min())); Console.Error.WriteLine("Reached convergence on training/validation data after {0} iterations.", it); break; } if (results[eval_measures[0]] > cutoff) { Console.Error.WriteLine("Reached cutoff after {0} iterations.", it); break; } } } // for if (max_iter % find_iter != 0) { recommender.WritePredictions(test_data, prediction_file, user_mapping, item_mapping, prediction_line, prediction_header); } } } else { TimeSpan seconds; Console.Write(recommender + " "); if (load_model_file == null) { if (cross_validation > 1) { Console.WriteLine(); var results = DoCrossValidation(); Console.Write(Render(results)); do_eval = false; } else { if (search_hp) { double result = NelderMead.FindMinimum("RMSE", recommender); Console.Error.WriteLine("estimated quality (on split) {0}", result.ToString(CultureInfo.InvariantCulture)); } seconds = Wrap.MeasureTime(delegate() { recommender.Train(); }); Console.Write(" training_time " + seconds + " "); } } if (do_eval) { if (online_eval) { seconds = Wrap.MeasureTime(delegate() { Console.Write(Render(recommender.EvaluateOnline(test_data))); }); } else { seconds = Wrap.MeasureTime(delegate() { Console.Write(Render(Evaluate())); }); } Console.Write(" testing_time " + seconds); if (compute_fit) { Console.Write("\nfit "); seconds = Wrap.MeasureTime(delegate() { Console.Write(Render(recommender.Evaluate(training_data))); }); Console.Write(" fit_time " + seconds); } } if (prediction_file != null) { Console.WriteLine(); seconds = Wrap.MeasureTime(delegate() { recommender.WritePredictions(test_data, prediction_file, user_mapping, item_mapping, prediction_line, prediction_header); }); Console.Error.WriteLine("prediction_time " + seconds); } Console.WriteLine(); } Model.Save(recommender, save_model_file); DisplayStats(); }
static void Main(string[] args) { Stopwatch tw = new Stopwatch(); tw.Start(); string provenienza = "RISPrivati"; log.Info("Starting procedure..."); DataAccessLayer.DAL dal = new DataAccessLayer.DAL(); try { //1. Lettura Tabella dove presente lo stato della richiesta //List<RISMirth> rissM = dal.GetRISMirthAll(); //List<Guid> mirthg = rissM.Select(p=>p.) //2. Lettura Vista Gro -- select * from [eurosanita].[VW_EsamiRadiologiciRISCasilino] -- where IDRichiesta non è preso in carico //3. Verifica Anagrafica Paziente List <RISCentral> rissC = new List <RISCentral>(); // Fittizio foreach (RISCentral risC in rissC) { string name = risC.Nome; string surname = risC.Cognome; string cf = risC.CodiceFiscale; DateTime?birth = risC.DataNascita; if (name != null && surname != null && birth != null && cf != null) { Pazi paz = dal.GetPazienteByFilter(name, surname, cf, birth.Value); if (paz == null) { //4. In caso non esista, creare il pazeinte in GR Pazi nuovoPaziente = new Pazi(); nuovoPaziente.PAZINOME = risC.Nome; nuovoPaziente.PAZICOGN = risC.Cognome; nuovoPaziente.PAZICOFI = risC.CodiceFiscale; nuovoPaziente.PAZIDATA = risC.DataNascita; int written = dal.InsertPaziente(nuovoPaziente); if (written != 1) { //ERROR! FATAL continue; } //5. Creare il mappning in IdentityMapping IdentityMapping nuovoMapping = new IdentityMapping(); nuovoMapping.idext = risC.IdPaziente.ToString(); nuovoMapping.provenienza = provenienza; written = dal.InsertIdentityMapping(nuovoMapping); if (written != 1) { //ERROR! FATAL continue; } } else { // L'anagrafica esiste in GR. Bisogna controllare se esite in IdentintyMapping, se no bisogna crearla int IDPazienteCentral = risC.IdPaziente.Value; int IDPazienteGR = paz.PAZIIDID.Value; IdentityMapping mapping = dal.GetIdentityMappingByPK(IDPazienteGR, IDPazienteCentral.ToString()); if (mapping == null) { IdentityMapping nuovoMapping = new IdentityMapping(); nuovoMapping.idext = IDPazienteCentral.ToString(); nuovoMapping.provenienza = provenienza; int written = dal.InsertIdentityMapping(nuovoMapping); if (written != 1) { //ERROR! FATAL continue; } } } } } } catch (Exception) { } tw.Stop(); log.Info(string.Format("Procedure Completed! Elapsed time {0}", GeneralPurposeLib.LibString.TimeSpanToTimeHmsms(tw.Elapsed))); }