def predict_to_dataframe(<EOL>self,<EOL>peptides,<EOL>alleles=None,<EOL>allele=None,<EOL>throw=True,<EOL>include_individual_model_predictions=False,<EOL>include_percentile_ranks=True,<EOL>include_confidence_intervals=True,<EOL>centrality_measure=DEFAULT_CENTRALITY_MEASURE):
if isinstance(peptides, string_types):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if isinstance(alleles, string_types):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if allele is None and alleles is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>peptides = EncodableSequences.create(peptides)<EOL>df = pandas.DataFrame({<EOL>'<STR_LIT>': peptides.sequences<EOL>}, copy=False)<EOL>if allele is not None:<EOL><INDENT>if alleles is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>df["<STR_LIT>"] = allele<EOL>normalized_allele = mhcnames.normalize_allele_name(allele)<EOL>df["<STR_LIT>"] = normalized_allele<EOL>unique_alleles = [normalized_allele]<EOL><DEDENT>else:<EOL><INDENT>df["<STR_LIT>"] = numpy.array(alleles)<EOL>df["<STR_LIT>"] = df.allele.map(<EOL>mhcnames.normalize_allele_name)<EOL>unique_alleles = df.normalized_allele.unique()<EOL><DEDENT>if len(df) == <NUM_LIT:0>:<EOL><INDENT>logging.warning("<STR_LIT>")<EOL>empty_result = pandas.DataFrame(<EOL>columns=[<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>'<EOL>])<EOL>return empty_result<EOL><DEDENT>(min_peptide_length, max_peptide_length) = (<EOL>self.supported_peptide_lengths)<EOL>if (peptides.min_length < min_peptide_length or<EOL>peptides.max_length > max_peptide_length):<EOL><INDENT>all_peptide_lengths_supported = False<EOL>sequence_length = df.peptide.str.len()<EOL>df["<STR_LIT>"] = (<EOL>(sequence_length >= min_peptide_length) &<EOL>(sequence_length <= max_peptide_length))<EOL>if (~df.supported_peptide_length).any():<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT:%s>" % (<EOL>(~df.supported_peptide_length).sum(),<EOL>min_peptide_length,<EOL>max_peptide_length,<EOL>str(df.ix[~df.supported_peptide_length].peptide.unique())))<EOL>logging.warning(msg)<EOL>if throw:<EOL><INDENT>raise ValueError(msg)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>df["<STR_LIT>"] = True<EOL>all_peptide_lengths_supported = True<EOL><DEDENT>num_pan_models = len(self.class1_pan_allele_models)<EOL>max_single_allele_models = max(<EOL>len(self.allele_to_allele_specific_models.get(allele, []))<EOL>for allele in unique_alleles<EOL>)<EOL>predictions_array = numpy.zeros(<EOL>shape=(df.shape[<NUM_LIT:0>], num_pan_models + max_single_allele_models),<EOL>dtype="<STR_LIT>")<EOL>predictions_array[:] = numpy.nan<EOL>if self.class1_pan_allele_models:<EOL><INDENT>unsupported_alleles = [<EOL>allele for allele in<EOL>df.normalized_allele.unique()<EOL>if allele not in self.allele_to_fixed_length_sequence<EOL>]<EOL>if unsupported_alleles:<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (<EOL>"<STR_LIT:U+0020>".join(unsupported_alleles),<EOL>"<STR_LIT:U+0020>".join(sorted(self.allele_to_fixed_length_sequence))))<EOL>logging.warning(msg)<EOL>if throw:<EOL><INDENT>raise ValueError(msg)<EOL><DEDENT><DEDENT>mask = df.supported_peptide_length<EOL>if mask.sum() > <NUM_LIT:0>:<EOL><INDENT>masked_allele_encoding = AlleleEncoding(<EOL>df.loc[mask].normalized_allele,<EOL>allele_to_fixed_length_sequence=self.allele_to_fixed_length_sequence)<EOL>masked_peptides = peptides.sequences[mask]<EOL>for (i, model) in enumerate(self.class1_pan_allele_models):<EOL><INDENT>predictions_array[mask, i] = model.predict(<EOL>masked_peptides,<EOL>allele_encoding=masked_allele_encoding)<EOL><DEDENT><DEDENT><DEDENT>if self.allele_to_allele_specific_models:<EOL><INDENT>unsupported_alleles = [<EOL>allele for allele in unique_alleles<EOL>if not self.allele_to_allele_specific_models.get(allele)<EOL>]<EOL>if unsupported_alleles:<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (<EOL>"<STR_LIT:U+0020>".join(unsupported_alleles),<EOL>"<STR_LIT:U+0020>".join(sorted(self.allele_to_allele_specific_models))))<EOL>logging.warning(msg)<EOL>if throw:<EOL><INDENT>raise ValueError(msg)<EOL><DEDENT><DEDENT>for allele in unique_alleles:<EOL><INDENT>models = self.allele_to_allele_specific_models.get(allele, [])<EOL>if len(unique_alleles) == <NUM_LIT:1> and all_peptide_lengths_supported:<EOL><INDENT>mask = None<EOL><DEDENT>else:<EOL><INDENT>mask = (<EOL>(df.normalized_allele == allele) &<EOL>df.supported_peptide_length).values<EOL><DEDENT>if mask is None or mask.all():<EOL><INDENT>for (i, model) in enumerate(models):<EOL><INDENT>predictions_array[:, num_pan_models + i] = (<EOL>model.predict(peptides))<EOL><DEDENT><DEDENT>elif mask.sum() > <NUM_LIT:0>:<EOL><INDENT>peptides_for_allele = EncodableSequences.create(<EOL>df.ix[mask].peptide.values)<EOL>for (i, model) in enumerate(models):<EOL><INDENT>predictions_array[<EOL>mask,<EOL>num_pan_models + i,<EOL>] = model.predict(peptides_for_allele)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if callable(centrality_measure):<EOL><INDENT>centrality_function = centrality_measure<EOL><DEDENT>else:<EOL><INDENT>centrality_function = CENTRALITY_MEASURES[centrality_measure]<EOL><DEDENT>logs = numpy.log(predictions_array)<EOL>log_centers = centrality_function(logs)<EOL>df["<STR_LIT>"] = numpy.exp(log_centers)<EOL>if include_confidence_intervals:<EOL><INDENT>df["<STR_LIT>"] = numpy.exp(numpy.nanpercentile(logs, <NUM_LIT>, axis=<NUM_LIT:1>))<EOL>df["<STR_LIT>"] = numpy.exp(numpy.nanpercentile(logs, <NUM_LIT>, axis=<NUM_LIT:1>))<EOL><DEDENT>if include_individual_model_predictions:<EOL><INDENT>for i in range(num_pan_models):<EOL><INDENT>df["<STR_LIT>" % i] = predictions_array[:, i]<EOL><DEDENT>for i in range(max_single_allele_models):<EOL><INDENT>df["<STR_LIT>" % i] = predictions_array[<EOL>:, num_pan_models + i<EOL>]<EOL><DEDENT><DEDENT>if include_percentile_ranks:<EOL><INDENT>if self.allele_to_percent_rank_transform:<EOL><INDENT>df["<STR_LIT>"] = self.percentile_ranks(<EOL>df.prediction,<EOL>alleles=df.normalized_allele.values,<EOL>throw=throw)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL><DEDENT><DEDENT>del df["<STR_LIT>"]<EOL>del df["<STR_LIT>"]<EOL>return df<EOL>
Predict nM binding affinities. Gives more detailed output than `predict`
method, including 5-95% prediction intervals.

If multiple predictors are available for an allele, the predictions are
the geometric means of the individual model predictions.

One of 'allele' or 'alleles' must be specified. If 'allele' is specified
all predictions will be for the given allele. If 'alleles' is specified
it must be the same length as 'peptides' and give the allele
corresponding to each peptide. 

Parameters
----------
peptides : `EncodableSequences` or list of string
alleles : list of string
allele : string
throw : boolean
    If True, a ValueError will be raised in the case of unsupported
    alleles or peptide lengths. If False, a warning will be logged and
    the predictions for the unsupported alleles or peptides will be NaN.
include_individual_model_predictions : boolean
    If True, the predictions of each individual model are included as
    columns in the result dataframe.
include_percentile_ranks : boolean, default True
    If True, a "prediction_percentile" column will be included giving the
    percentile ranks. If no percentile rank information is available,
    this will be ignored with a warning.
centrality_measure : string or callable
    Measure of central tendency to use to combine predictions in the
    ensemble. Options include: mean, median, robust_mean.

Returns
-------
`pandas.DataFrame` of predictions