Create a dataframe row for a prediction.
332323
def get_dataframe_row(self, dataset_cases, predicted_data, pdb_data, record_id, additional_prediction_data_columns):
        ''''''
        record = dataset_cases[record_id]
        for m in record['PDBMutations']:
            assert('DSSPSimpleSSType' not in m)
            m['DSSPSimpleSSType'] = dssp_elision.get(m['ComplexDSSP']) or dssp_elision.get(m['MonomericDSSP'])
            m['DSSPType'] = m.get('ComplexDSSP') or m.get('MonomericDSSP')
            m['DSSPExposure'] = m.get('ComplexExposure') or m.get('MonomericExposure')

        dataframe_record = super(DBBenchmarkRun, self).get_dataframe_row(dataset_cases, predicted_data, pdb_data, record_id, additional_prediction_data_columns)
        # add columns
        return dataframe_record
1321874
def get_dataframe_row(self, dataset_cases, predicted_data, pdb_data, record_id, additional_prediction_data_columns):
        ''''''

        # Ignore derived mutations if appropriate
        record = dataset_cases[record_id]
        if self.is_this_record_a_derived_mutation(record) and not self.include_derived_mutations:
            return None

        amino_acid_details, CAA, PAA, HAA = self.amino_acid_details, self.CAA, self.PAA, self.HAA
        burial_cutoff = self.burial_cutoff

        # Initialize variables. For ambiguous cases where the set of distinct values has multiple values, we default to None
        residue_charge, residue_charges = None, set()
        exposure, exposures = None, set()
        volume_change, volume_changes = None, set()
        record_wtaa, wtaas = None, set()
        record_mutaa, mutaas = None, set()
        DSSPSimpleSSType, DSSPSimpleSSTypes = None, set()
        DSSPType, DSSPTypes = None, set()
        DSSPExposure, DSSPExposures = None, set()
        scops = set()
        mutation_string = []
        num_derivative_errors = predicted_data.get('Errors', {}).get('Derivative error count', 0)
        run_time = predicted_data.get('RunTime', None)
        max_memory = predicted_data.get('MaxMemory', None)

        mutations = self.get_record_mutations(record)
        for m in mutations:

            wtaa = m['WildTypeAA']
            mutaa = m['MutantAA']
            mutation_string.append('{0} {1}{2}{3}'.format(m['Chain'], m['WildTypeAA'], m['ResidueID'], m['MutantAA']))

            # Residue types and chain
            wtaas.add(wtaa)
            mutaas.add(mutaa)
            if m.get('SCOP class'):
                scops.add(m['SCOP class'])
            DSSPSimpleSSTypes.add(m['DSSPSimpleSSType'])
            DSSPTypes.add(m['DSSPType'])
            DSSPExposures.add(m['DSSPExposure'])

            # Burial
            if m['DSSPExposure'] != None:
                if m['DSSPExposure'] > burial_cutoff:
                    exposures.add('E')
                else:
                    exposures.add('B')
            else:
                exposures.add(None)

            # Volume
            if amino_acid_details[wtaa]['van der Waals volume'] < amino_acid_details[mutaa]['van der Waals volume']:
                volume_changes.add('SL')
            elif amino_acid_details[wtaa]['van der Waals volume'] > amino_acid_details[mutaa]['van der Waals volume']:
                volume_changes.add('LS')
            elif amino_acid_details[wtaa]['van der Waals volume'] == amino_acid_details[mutaa]['van der Waals volume']:
                volume_changes.add('XX')

            # Charge
            if ((wtaa in CAA or wtaa in PAA) and (mutaa in HAA)) or ((mutaa in CAA or mutaa in PAA) and (wtaa in HAA)):
                residue_charges.add('Change')
            elif (wtaa in CAA or wtaa in PAA) and (mutaa in CAA or mutaa in PAA):
                residue_charges.add('Polar/Charged')
            elif (wtaa in HAA) and (mutaa in HAA):
                residue_charges.add('Hydrophobic/Non-polar')
            else:
                 raise colortext.Exception('Should not reach here.')

        # Create a string representing the mutations (useful for labeling rather than analysis)
        mutation_string = '; '.join(mutation_string)

        # Taking unique values, determine the residue charges of the wildtype and mutant residues, the wildtype residue exposure, and the relative change in van der Waals volume
        if len(residue_charges) == 1: residue_charge = residue_charges.pop()
        if len(exposures) == 1: exposure = exposures.pop()
        if len(volume_changes) == 1: volume_change = volume_changes.pop()

        # Taking unique values, determine the wildtype and mutant residue types
        all_residues = wtaas.union(mutaas)
        if len(wtaas) == 1: record_wtaa = wtaas.pop()
        if len(mutaas) == 1: record_mutaa = mutaas.pop()

        # Taking unique values, determine the secondary structure and residue exposures from the DSSP data in the dataset
        if len(DSSPSimpleSSTypes) == 1: DSSPSimpleSSType = DSSPSimpleSSTypes.pop()
        if len(DSSPTypes) == 1: DSSPType = DSSPTypes.pop()
        if len(DSSPExposures) == 1: DSSPExposure = DSSPExposures.pop()

        # Determine the SCOP classification from the SCOPe data in the dataset
        full_scop_classification, scop_class, scop_fold = None, None, None
        if len(scops) > 1:
            self.log('Warning: There is more than one SCOPe class for record {0}.'.format(record_id), colortext.warning)
        elif len(scops) == 1:
            full_scop_classification = scops.pop()
            scop_tokens = full_scop_classification.split('.')
            scop_class = scop_tokens[0]
            if len(scop_tokens) > 1:
                scop_fold = '.'.join(scop_tokens[0:2])

        # Partition the data by PDB resolution with bins: N/A, <1.5, 1.5-<2.0, 2.0-<2.5, >=2.5
        pdb_record = pdb_data.get(self.get_record_pdb_file_id(record).upper())
        pdb_resolution_bin = None
        pdb_resolution = pdb_record.get('Resolution')
        if pdb_resolution != None:
            if pdb_resolution < 1.5:
                pdb_resolution_bin = '<1.5'
            elif pdb_resolution < 2.0:
                pdb_resolution_bin = '1.5-2.0'
            elif pdb_resolution < 2.5:
                pdb_resolution_bin = '2.0-2.5'
            else:
                pdb_resolution_bin = '>=2.5'
        pdb_resolution_bin = pdb_resolution_bin or 'N/A'

        # Mark mutations involving glycine or proline
        has_gp_mutation = 'G' in all_residues or 'P' in all_residues

        # Create the data matrix
        dataframe_record = dict(
            DatasetID = record_id,
            PDBFileID = self.get_record_pdb_file_id(record),
            Mutations = mutation_string,
            NumberOfMutations = len(mutations),
            Predicted = predicted_data[self.ddg_analysis_type],
            ResidueCharges = residue_charge,
            VolumeChange = volume_change,
            HasGPMutation = int(has_gp_mutation),
            WildTypeDSSPType = DSSPType,
            WildTypeDSSPSimpleSSType = DSSPSimpleSSType,
            WildTypeDSSPExposure = DSSPExposure,
            WildTypeSCOPClass = scop_class,
            WildTypeSCOPFold = scop_fold,
            WildTypeSCOPClassification = full_scop_classification,
            WildTypeExposure = exposure,
            WildTypeAA = record_wtaa,
            MutantAA = record_mutaa,
            PDBResolution = pdb_record.get('Resolution'),
            PDBResolutionBin = pdb_resolution_bin,
            NumberOfResidues = self.count_residues(record, pdb_record) or None,
            NumberOfDerivativeErrors = num_derivative_errors,
            RunTime = run_time,
            MaxMemory = max_memory,
            )
        for c in additional_prediction_data_columns:
            dataframe_record[c] = predicted_data.get(c)

        if self.contains_experimental_data:
            # These fields are particular to dataframes containing experimental values e.g. for benchmarking runs or for
            # datasets where we have associated experimental values
            self.get_experimental_ddg_values(record, dataframe_record)
            self.compute_stability_classification(predicted_data, record, dataframe_record)
            self.compute_absolute_error(predicted_data, record, dataframe_record)

        return dataframe_record
1045337
def add_row(self, row):

        """"""

        if self._field_names and len(row) != len(self._field_names):
            raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self._field_names)))
        if not self._field_names:
            self.field_names = [("Field %d" % (n+1)) for n in range(0,len(row))]
        self._rows.append(list(row))
946352
public float[] predict( Map<String, Double> row ) {
    return predict(map(row,new double[getNames().length]),new float[getNumResponseClasses()+1]);
  }
834497
def row_to_obj(self, row, cur):
        """"""
        obj = tornado.util.ObjectDict()
        for val, desc in zip(row, cur.description):
            obj[desc.name] = val
        return obj
1133115
def _make_tabular_predict_fn(self, labels, instance, categories):
        

        def _predict_fn(np_instance):

            df = pd.DataFrame(
                np_instance,
                columns=(self._categorical_columns + self._numeric_columns))

            # Convert categorical indices back to categories.
            for col_name, col_categories in zip(self._categorical_columns, categories):
                df[col_name] = df[col_name].apply(lambda x: col_categories[int(x)])

            # Add columns that do not exist in the perturbed data,
            # such as key, text, and image data.
            for col_name in self._headers:
                if col_name not in (self._categorical_columns + self._numeric_columns):
                    df[col_name] = instance[col_name]

            r = _local_predict.get_prediction_results(
                self._model_dir, df, self._headers, with_source=False)
            probs = _local_predict.get_probs_for_labels(labels, r)
            probs = np.asarray(probs)
            return probs

        return _predict_fn
832535
def write_dataframe(rows, encoding=ENCODING, dialect=DIALECT, **kwargs):
    """"""
    global pandas
    if pandas is None:  # pragma: no cover
        import pandas
    with contextlib.closing(CsvBuffer()) as fd:
        write_csv(fd, rows, encoding, dialect)
        fd.seek(0)
        df = read_csv(pandas, fd, encoding, dialect, kwargs)
    return df
1334690
def dataframe(self,asql, logger = None):
        """"""
        import pandas as pd
        from ambry.mprlib.exceptions import BadSQLError

        try:
            def yielder(cursor):

                for i, row in enumerate(cursor):
                    if i == 0:
                        yield [ e[0] for e in cursor.getdescription()]

                    yield row

            cursor = self.query(asql, logger)

            yld = yielder(cursor)

            header = next(yld)

            return pd.DataFrame(yld, columns=header)
        except BadSQLError as e:
            import traceback
            self._logger.error("SQL Error: {}".format( e))
            self._logger.debug(traceback.format_exc())
1255693
def dataframe(self):
        """"""

        if self._dataframe is None:
            try:
                import pandas as pd
            except ImportError:
                raise RuntimeError('To enable dataframe support, '
                                   'run \'pip install datadotworld[pandas]\'')

            self._dataframe = pd.DataFrame.from_records(self._iter_rows(),
                                                        coerce_float=True)

        return self._dataframe
477161
def prepare_data(self, rows):
        
        self._rows = dict()
        for row in copy.copy(rows) if self.copy else rows:
            pseudo_key = self._get_pseudo_key(row)
            if pseudo_key not in self._rows:
                self._rows[pseudo_key] = list()
            self._rows[pseudo_key].append(row)

        # Convert begin and end dates to integers.
        self._date_type = None
        for pseudo_key, rows in self._rows.items():
            self._rows_date2int(rows)
497460
def dataframe(self):
        """"""
        data = self.data
        channel_names = self.get_channel_names()
        return pd.DataFrame(data, columns=channel_names)
632063
def broadcast(df):
    """"""

    sc = SparkContext._active_spark_context
    return DataFrame(sc._jvm.functions.broadcast(df._jdf), df.sql_ctx)
713410
def _build_dataframes(self):
        """"""

        for component in self.all_components:

            attrs = self.components[component]["attrs"]

            static_dtypes = attrs.loc[attrs.static, "dtype"].drop(["name"])

            df = pd.DataFrame({k: pd.Series(dtype=d) for k, d in static_dtypes.iteritems()},
                              columns=static_dtypes.index)

            df.index.name = "name"

            setattr(self,self.components[component]["list_name"],df)

            pnl = Dict({k : pd.DataFrame(index=self.snapshots,
                                         columns=[],
                                         #it's currently hard to imagine non-float series, but this could be generalised
                                         dtype=np.dtype(float))
                        for k in attrs.index[attrs.varying]})

            setattr(self,self.components[component]["list_name"]+"_t",pnl)
183933
def _render_dataframe(dataframe):
  """"""
  data = dataframe.to_dict(orient='records')
  fields = dataframe.columns.tolist()
  return IPython.core.display.HTML(
      datalab.utils.commands.HtmlBuilder.render_table(data, fields))
1109473
def make_table(self):
        """"""
        num_records = int(np.sum([1 for frame in self.timeseries]))
        dtype = [
                ("frame",float),("time",float),("proteinring",list),
                ("ligand_ring_ids",list),("distance",float),("angle",float),
                ("offset",float),("type","|U4"),("resid",int),("resname","|U4"),("segid","|U8") ]
        out = np.empty((num_records,),dtype=dtype)
        cursor=0
        for contact in self.timeseries:
            out[cursor] = (contact.frame, contact.time,contact.proteinring,contact.ligandring,contact.distance,contact.angle,contact.offset,contact.type,contact.resid,contact.resname,contact.segid)
            cursor+=1
        return out.view(np.recarray)
553784
def row_iter(self):
        """"""
        for k in utils.range_(self.side):
            yield self.row(k)
1326497
def create_md_row(self, row, header=False):
        """"""
        if not row:
            return
        cols = row.split('\t')
        if len(cols) == 1:
            self.out.append(cols[0])
        else:
            col_md = '|'
            underline_md = '|'

            if cols:
                for col in cols:
                    col_md += col + '|'
                    underline_md += '-|'

            if header:
                self.out.append(col_md + '\n' + underline_md)
            else:
                self.out.append(col_md)