public HlaMsr1 GetGroundInstance(string name, ref HashSet <string> warningSet) { HlaMsr1 hla = (HlaMsr1)GetGroundOrAbstractInstance(name, ref warningSet); SpecialFunctions.CheckCondition(hla.IsGround, "Hla is not ground. " + name); return(hla); }
//!!!Perforance ideas: Could the backoff models be applied better than just one at a time until one works? //!!!Perforance ideas: Does the web use load all the models on every use? Is this a problem? private static void RemoveRedundantTerms(ref List <KeyValuePair <string, string> > termAndInParenList, ref HashSet <string> warningSet) { termAndInParenList.Sort((termAndInParen1, termAndInParen2) => termAndInParen1.Key.Length.CompareTo(termAndInParen2.Key.Length)); for (int i = termAndInParenList.Count - 1; i > 0; --i) { string keyI = termAndInParenList[i].Key; for (int j = 0; j < i; ++j) { string keyJ = termAndInParenList[j].Key; if (HlaMsr1.IsMoreGeneralOrEqualThan(keyJ, keyI)) { if (keyJ == keyI) { termAndInParenList[j] = new KeyValuePair <string, string>(keyJ, termAndInParenList[j].Value + "/" + termAndInParenList[1].Value); termAndInParenList.RemoveAt(i); warningSet.Add("Two terms combined because they are equal at the generality used by the model."); break; // really break, not continue; } throw new GeneralizingTermException(keyJ, keyI, string.Format("Term {0} generalizes term {1}", keyJ, keyI)); } } } }
public static IEnumerable <PidAndHlaSet> GetEnumerationSparse(TextReader inputTextReader) { string previousPid = null; Dictionary <string, List <HlaMsr1> > classToHlaList = null; HashSet <string> warningSet = null; foreach (var pidAndHlaRecord in SpecialFunctions.ReadDelimitedFile(inputTextReader, new { pid = "", hla = "" }, new char[] { '\t' }, true)) { string pid = pidAndHlaRecord.pid; if (previousPid != pid) { if (previousPid != null) { PidAndHlaSet pidAndHlaSet = CreatePidAndHlaSet(previousPid, classToHlaList, warningSet); yield return(pidAndHlaSet); } previousPid = pid; classToHlaList = new Dictionary <string, List <HlaMsr1> >(); warningSet = new HashSet <string>(); } HlaMsr1 hlaMsr1 = (HlaMsr1)HlaMsr1Factory444.GetGroundOrAbstractInstance(pidAndHlaRecord.hla, ref warningSet); List <HlaMsr1> hlaList = classToHlaList.GetValueOrDefault(hlaMsr1.ClassName); hlaList.Add(hlaMsr1); } if (previousPid != null) { PidAndHlaSet pidAndHlaSet = CreatePidAndHlaSet(previousPid, classToHlaList, warningSet); yield return(pidAndHlaSet); } }
private void SetGroundOnHla(List <KeyValuePair <string, string> > termList, HlaMsr1 hlaMsrMsr1) { if (termList.Count > 1) { hlaMsrMsr1._isGround = false; } else { SetGroundOnSingleTermHla(hlaMsrMsr1); } }
private void SetGroundOnSingleTermHla(HlaMsr1 hlaMsrMsr1) { int hlaLength = ClassNameToHlaLength[hlaMsrMsr1.ClassName]; switch (hlaLength) { case 4: { if (hlaMsrMsr1.Name.Length == 1) { hlaMsrMsr1._isGround = false; } else if (HlaMsr1Factory.TrimToTwoList.Contains(hlaMsrMsr1.Name)) { hlaMsrMsr1._isGround = true; } else { hlaMsrMsr1._isGround = hlaMsrMsr1.Name.Length == 5; } } break; case 2: { if (hlaMsrMsr1.Name.Length == 1) { hlaMsrMsr1._isGround = false; } else { hlaMsrMsr1._isGround = hlaMsrMsr1.Name.Length == 3; } } break; case 0: { hlaMsrMsr1._isGround = hlaMsrMsr1.Name.Length == 1; } break; default: SpecialFunctions.CheckCondition(false, "Don't have code for hlaLength " + hlaLength.ToString()); break; } }
public Hla GetGroundOrAbstractInstance(string name, ref HashSet <string> warningSet) { string soFar = name.ToUpperInvariant(); var keyAndInParenListX = CreateCanonicalTermList(soFar, name, ref warningSet); var keyAndOrigList = (from kv in keyAndInParenListX let k = kv.Key let inParen = kv.Value let ori = (inParen == "") ? k : string.Format("{0}({1})", k, inParen) select new KeyValuePair <string, string>(k, ori)).ToList(); RemoveRedundantTerms(ref keyAndOrigList, ref warningSet); keyAndOrigList.Sort(SortTermsAlpha); HlaMsr1 hlaMsrMsr1 = new HlaMsr1(keyAndOrigList); SetGroundOnHla(keyAndOrigList, hlaMsrMsr1); return(hlaMsrMsr1); }
private IEnumerable <HlaMsr1> GeneralizationList(HlaMsr1 groundHla) { return(groundHla.GeneralizationList(HlaMsr1Factory)); }
private IEnumerable <KeyValuePair <HlaMsr1, double> > ExpandHla(TableInfo tableInfo, HlaMsr1 hlaAbstractOrGround, LinkedList1 <HlaMsr1> linkedList1) { HashSet <HlaMsr1> groundSet = new HashSet <HlaMsr1>(); foreach (HlaMsr1 term in hlaAbstractOrGround.TermList(tableInfo.HlaMsr1Factory)) { List <HlaMsr1> groundHlaList; if (tableInfo.AbstractHlaToGroundHlaList.TryGetValue(term, out groundHlaList)) { if (groundHlaList.Count == 1 && groundHlaList.First().Equals(term)) { groundSet.AddNew(term); } else { foreach (HlaMsr1 ground in groundHlaList) { groundSet.AddNew(ground); } } } } if (groundSet.Count == 0) { yield break; } List <Dictionary <HlaMsr1, double> > rowsOfInterest = tableInfo.PullOutTheRowsOfInterest(linkedList1); //!!!for each list of rowsOfInterest we could cache the sum of exp's to speed things up //!!!This could be made faster by giving a serial number to each HLA and then doing the calcuations in arrays in which the serial number is the index. Dictionary <HlaMsr1, double> hlaToTotal = new Dictionary <HlaMsr1, double>(); foreach (Dictionary <HlaMsr1, double> hlaToWeight in rowsOfInterest) { foreach (KeyValuePair <HlaMsr1, double> hlaAndWeight in hlaToWeight) { hlaToTotal[hlaAndWeight.Key] = hlaToTotal.GetValueOrDefault(hlaAndWeight.Key) + hlaAndWeight.Value; } } Dictionary <HlaMsr1, double> hlaToExpTotal = new Dictionary <HlaMsr1, double>(); double totalOfExpsPlus1 = 1; foreach (KeyValuePair <HlaMsr1, double> hlaAndTotal in hlaToTotal) { double exp = Math.Exp(hlaAndTotal.Value); totalOfExpsPlus1 += Math.Exp(hlaAndTotal.Value); hlaToExpTotal.Add(hlaAndTotal.Key, exp); } foreach (HlaMsr1 hlaGround in groundSet) { double prob = hlaToExpTotal[hlaGround] / totalOfExpsPlus1; yield return(new KeyValuePair <HlaMsr1, double>(hlaGround, prob)); } }
private IEnumerable <KeyValuePair <HlaMsr1, KeyValuePair <double, bool> > > ExpandHla(HlaMsr1 hlaAbstractOrGround, LinkedList1 <HlaMsr1> linkedList1) { //Reference: http://en.wikipedia.org/wiki/Multinomial_logit#Model //Dictionary<string, Dictionary<HlaMsr1, double>> predictorNameToTargetHlaToWeight = Ethnicity.ClassNamePredictorNameOrInterceptToTargetToWeight[hlaAbstractOrGround.ClassName]; EClass eclass = Ethnicity.HlaClassNameToEClass[hlaAbstractOrGround.ClassName]; bool usedLowerResModel = false; foreach (LinkedList1 <int> hlaLengthList in eclass.HlaLengthListSorted()) { bool anyReturned = false; TableInfo tableInfo = eclass.HlaLengthListToTableInfo[hlaLengthList]; foreach (var hlaAndProb in ExpandHla(tableInfo, hlaAbstractOrGround, linkedList1)) { anyReturned = true; yield return(new KeyValuePair <HlaMsr1, KeyValuePair <double, bool> >(hlaAndProb.Key, new KeyValuePair <double, bool>(hlaAndProb.Value, usedLowerResModel))); } if (anyReturned) { yield break; // really yield break, not yield return or return; } usedLowerResModel = true; } throw new HlaNotInModelException(hlaAbstractOrGround.Name, string.Format("Can't find {0} or any prefix in any model", hlaAbstractOrGround.ToString(/*withParen*/ true))); }