text_prompt stringlengths 157 13.1k | code_prompt stringlengths 7 19.8k ⌀ |
|---|---|
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _add_modifiers(self, sql, blueprint, column):
""" Add the column modifiers to the deifinition """ |
for modifier in self._modifiers:
method = '_modify_%s' % modifier
if hasattr(self, method):
sql += getattr(self, method)(blueprint, column)
return sql |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _clean_pivot_attributes(self, model):
""" Get the pivot attributes from a model. :type model: eloquent.Model """ |
values = {}
delete_keys = []
for key, value in model.get_attributes().items():
if key.find('pivot_') == 0:
values[key[6:]] = value
delete_keys.append(key)
for key in delete_keys:
delattr(model, key)
return values |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_relation_count_query_for_self_join(self, query, parent):
""" Add the constraints for a relationship count query on the same table. :type query: eloquent.... |
query.select(QueryExpression('COUNT(*)'))
table_prefix = self._query.get_query().get_connection().get_table_prefix()
hash_ = self.get_relation_count_hash()
query.from_('%s AS %s%s' % (self._table, table_prefix, hash_))
key = self.wrap(self.get_qualified_parent_key_name())
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_aliased_pivot_columns(self):
""" Get the pivot columns for the relation. :rtype: list """ |
defaults = [self._foreign_key, self._other_key]
columns = []
for column in defaults + self._pivot_columns:
value = '%s.%s AS pivot_%s' % (self._table, column, column)
if value not in columns:
columns.append('%s.%s AS pivot_%s' % (self._table, column, co... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _set_join(self, query=None):
""" Set the join clause for the relation query. :param query: The query builder :type query: eloquent.orm.Builder :return: self ... |
if not query:
query = self._query
base_table = self._related.get_table()
key = '%s.%s' % (base_table, self._related.get_key_name())
query.join(self._table, key, '=', self.get_other_key())
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def save(self, model, joining=None, touch=True):
""" Save a new model and attach it to the parent model. :type model: eloquent.Model :type joining: dict :type to... |
if joining is None:
joining = {}
model.save({'touch': False})
self.attach(model.get_key(), joining, touch)
return model |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _attach_new(self, records, current, touch=True):
""" Attach all of the IDs that aren't in the current dict. """ |
changes = {
'attached': [],
'updated': []
}
for id, attributes in records.items():
if id not in current:
self.attach(id, attributes, touch)
changes['attached'].append(id)
elif len(attributes) > 0 and self.update_e... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update_existing_pivot(self, id, attributes, touch=True):
""" Update an existing pivot record on the table. """ |
if self.updated_at() in self._pivot_columns:
attributes = self.set_timestamps_on_attach(attributes, True)
updated = self._new_picot_statement_for_id(id).update(attributes)
if touch:
self.touch_if_touching()
return updated |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def join(self, table, one=None, operator=None, two=None, type='inner', where=False):
""" Add a join clause to the query :param table: The table to join with, can... |
if isinstance(table, JoinClause):
self.joins.append(table)
else:
if one is None:
raise ArgumentError('Missing "one" argument')
join = JoinClause(table, type)
self.joins.append(join.on(
one, operator, two, 'and', where
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def join_where(self, table, one, operator, two, type='inner'):
""" Add a "join where" clause to the query :param table: The table to join with, can also be a Joi... |
return self.join(table, one, operator, two, type, True) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _where_in_sub(self, column, query, boolean, negate=False):
""" Add a where in with a sub select to the query :param column: The column :type column: str :par... |
if negate:
type = 'not_in_sub'
else:
type = 'in_sub'
self.wheres.append({
'type': type,
'column': column,
'query': query,
'boolean': boolean
})
self.merge_bindings(query)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def union(self, query, all=False):
""" Add a union statement to the query :param query: A QueryBuilder instance :type query: QueryBuilder :param all: Whether it ... |
self.unions.append({
'query': query,
'all': all
})
return self.merge_bindings(query) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def find(self, id, columns=None):
""" Execute a query for a single record by id :param id: The id of the record to retrieve :type id: mixed :param columns: The c... |
if not columns:
columns = ['*']
return self.where('id', '=', id).first(1, columns) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_fresh(self, columns=None):
""" Execute the query as a fresh "select" statement :param columns: The columns to get :type columns: list :return: The result... |
if not columns:
columns = ['*']
if not self.columns:
self.columns = columns
return self._processor.process_select(self, self._run_select()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_list_select(self, column, key=None):
""" Get the columns that should be used in a list :param column: The column to get the values for :type column: str... |
if key is None:
elements = [column]
else:
elements = [column, key]
select = []
for elem in elements:
dot = elem.find('.')
if dot >= 0:
select.append(column[dot + 1:])
else:
select.append(elem)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def delete(self, id=None):
""" Delete a record from the database :param id: The id of the row to delete :type id: mixed :return: The number of rows deleted :rtyp... |
if id is not None:
self.where('id', '=', id)
sql = self._grammar.compile_delete(self)
return self._connection.delete(sql, self.get_bindings()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def merge_wheres(self, wheres, bindings):
""" Merge a list of where clauses and bindings :param wheres: A list of where clauses :type wheres: list :param binding... |
self.wheres = self.wheres + wheres
self._bindings['where'] = self._bindings['where'] + bindings |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def remove(self, builder, model):
""" Remove the scope from a given query builder. :param builder: The query builder :type builder: eloquent.orm.builder.Builder ... |
column = model.get_qualified_deleted_at_column()
query = builder.get_query()
wheres = []
for where in query.wheres:
# If the where clause is a soft delete date constraint,
# we will remove it from the query and reset the keys
# on the wheres. This a... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def extend(self, builder):
""" Extend the query builder with the needed functions. :param builder: The query builder :type builder: eloquent.orm.builder.Builder ... |
for extension in self._extensions:
getattr(self, '_add_%s' % extension)(builder)
builder.on_delete(self._on_delete) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _only_trashed(self, builder):
""" The only-trashed extension. :param builder: The query builder :type builder: eloquent.orm.builder.Builder """ |
model = builder.get_model()
self.remove(builder, model)
builder.get_query().where_not_null(model.get_qualified_deleted_at_column()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def big_integer(self, column, auto_increment=False, unsigned=False):
""" Create a new big integer column on the table. :param column: The column :type column: st... |
return self._add_column('big_integer', column,
auto_increment=auto_increment,
unsigned=unsigned) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def medium_integer(self, column, auto_increment=False, unsigned=False):
""" Create a new medium integer column on the table. :param column: The column :type colu... |
return self._add_column('medium_integer', column,
auto_increment=auto_increment,
unsigned=unsigned) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def tiny_integer(self, column, auto_increment=False, unsigned=False):
""" Create a new tiny integer column on the table. :param column: The column :type column: ... |
return self._add_column('tiny_integer', column,
auto_increment=auto_increment,
unsigned=unsigned) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _add_column(self, type, name, **parameters):
""" Add a new column to the blueprint. :param type: The column type :type type: str :param name: The column name... |
parameters.update({
'type': type,
'name': name
})
column = Fluent(**parameters)
self._columns.append(column)
return column |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def dissociate(self):
""" Dissociate previously associated model from the given parent. :rtype: eloquent.Model """ |
self._parent.set_attribute(self._foreign_key, None)
return self._parent.set_relation(self._relation, None) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_relation_value(self, dictionary, key, type):
""" Get the value of the relationship by one or many type. :type dictionary: dict :type key: str :type type... |
value = dictionary[key]
if type == 'one':
return value[0]
return self._related.new_collection(value) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def first_or_create(self, _attributes=None, **attributes):
""" Get the first related record matching the attributes or create it. :param attributes: The attribut... |
if _attributes is not None:
attributes.update(_attributes)
instance = self.where(attributes).first()
if instance is None:
instance = self.create(**attributes)
return instance |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def eager_load_relations(self, models):
""" Eager load the relationship of the models. :param models: :type models: list :return: The models :rtype: list """ |
for name, constraints in self._eager_load.items():
if name.find('.') == -1:
models = self._load_relation(models, name, constraints)
return models |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _load_relation(self, models, name, constraints):
""" Eagerly load the relationship on a set of models. :rtype: list """ |
relation = self.get_relation(name)
relation.add_eager_constraints(models)
if callable(constraints):
constraints(relation)
else:
relation.merge_query(constraints)
models = relation.init_relation(models, name)
results = relation.get_eager()
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _has_nested(self, relations, operator='>=', count=1, boolean='and', extra=None):
""" Add nested relationship count conditions to the query. :param relations:... |
relations = relations.split('.')
def closure(q):
if len(relations) > 1:
q.where_has(relations.pop(0), closure)
else:
q.has(relations.pop(0), operator, count, boolean, extra)
return self.where_has(relations.pop(0), closure) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def doesnt_have(self, relation, boolean='and', extra=None):
""" Add a relationship count to the query. :param relation: The relation to count :type relation: str... |
return self.has(relation, '<', 1, boolean, extra) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def where_has(self, relation, extra, operator='>=', count=1):
""" Add a relationship count condition to the query with where clauses. :param relation: The relati... |
return self.has(relation, operator, count, 'and', extra) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def or_has(self, relation, operator='>=', count=1):
""" Add a relationship count condition to the query with an "or". :param relation: The relation to count :typ... |
return self.has(relation, operator, count, 'or') |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def or_where_has(self, relation, extra, operator='>=', count=1):
""" Add a relationship count condition to the query with where clauses and an "or". :param relat... |
return self.has(relation, operator, count, 'or', extra) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _merge_wheres_to_has(self, has_query, relation):
""" Merge the "wheres" from the relation query to a has query. :param has_query: The has query :type has_que... |
relation_query = relation.get_base_query()
has_query.merge_wheres(relation_query.wheres, relation_query.get_bindings())
self._query.merge_bindings(has_query.get_query()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_has_relation_query(self, relation):
""" Get the "has" relation base query :type relation: str :rtype: Builder """ |
from .relations import Relation
return Relation.no_constraints(
lambda: getattr(self.get_model(), relation)()
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _parse_nested(self, name, results):
""" Parse the nested relationship in a relation. :param name: The name of the relationship :type name: str :type results:... |
progress = []
for segment in name.split('.'):
progress.append(segment)
last = '.'.join(progress)
if last not in results:
results[last] = self.__class__(self.get_query().new_query())
return results |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def rollback(self, path, pretend=False):
""" Rollback the last migration operation. :param path: The path :type path: str :param pretend: Whether we execute the ... |
self._notes = []
migrations = self._repository.get_last()
if not migrations:
self._note('<info>Nothing to rollback.</info>')
return len(migrations)
for migration in migrations:
self._run_down(path, migration, pretend)
return len(migration... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def detect_column_renamings(self, table_differences):
""" Try to find columns that only changed their names. :type table_differences: TableDiff """ |
rename_candidates = {}
for added_column_name, added_column in table_differences.added_columns.items():
for removed_column in table_differences.removed_columns.values():
if len(self.diff_column(added_column, removed_column)) == 0:
if added_column.get_name... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def diff_column(self, column1, column2):
""" Returns the difference between column1 and column2 :type column1: eloquent.dbal.column.Column :type column2: eloquen... |
properties1 = column1.to_dict()
properties2 = column2.to_dict()
changed_properties = []
for prop in ['type', 'notnull', 'unsigned', 'autoincrement']:
if properties1[prop] != properties2[prop]:
changed_properties.append(prop)
if properties1['default... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _create_model_by_type(self, type):
""" Create a new model instance by type. :rtype: Model """ |
klass = None
for cls in eloquent.orm.model.Model.__subclasses__():
morph_class = cls.__morph_class__ or cls.__name__
if morph_class == type:
klass = cls
break
return klass() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _populate_stub(self, name, stub, table):
""" Populate the placeholders in the migration stub. :param name: The name of the migration :type name: str :param s... |
stub = stub.replace('DummyClass', self._get_class_name(name))
if table is not None:
stub = stub.replace('dummy_table', table)
return stub |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def purge(self, name=None):
""" Disconnect from the given database and remove from local cache :param name: The name of the connection :type name: str :rtype: No... |
self.disconnect(name)
if name in self._connections:
del self._connections[name] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_keys(self, models, key=None):
""" Get all the primary keys for an array of models. :type models: list :type key: str :rtype: list """ |
return list(set(map(lambda value: value.get_attribute(key) if key else value.get_key(), models))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _set_join(self, query=None):
""" Set the join clause for the query. """ |
if not query:
query = self._query
foreign_key = '%s.%s' % (self._related.get_table(), self._second_key)
query.join(self._parent.get_table(), self.get_qualified_parent_key_name(), '=', foreign_key) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def plot_best_worst_fits(assignments_df, data, modality_col='Modality', score='$\log_2 K$'):
"""Violinplots of the highest and lowest scoring of each modality""" |
ncols = 2
nrows = len(assignments_df.groupby(modality_col).groups.keys())
fig, axes = plt.subplots(nrows=nrows, ncols=ncols,
figsize=(nrows*4, ncols*6))
axes_iter = axes.flat
fits = 'Highest', 'Lowest'
for modality, df in assignments_df.groupby(modality_col):
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bar(self, counts, phenotype_to_color=None, ax=None, percentages=True):
"""Draw barplots grouped by modality of modality percentage per group Parameters Retur... |
if percentages:
counts = 100 * (counts.T / counts.T.sum()).T
# with sns.set(style='whitegrid'):
if ax is None:
ax = plt.gca()
full_width = 0.8
width = full_width / counts.shape[0]
for i, (group, series) in enumerate(counts.iterrows()):
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def event_estimation(self, event, logliks, logsumexps, renamed=''):
"""Show the values underlying bayesian modality estimations of an event Parameters Returns --... |
plotter = _ModelLoglikPlotter()
plotter.plot(event, logliks, logsumexps, self.modality_to_color,
renamed=renamed)
return plotter |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def predict(self, fitted):
"""Assign the most likely modality given the fitted data Parameters fitted : pandas.DataFrame or pandas.Series Either a (n_modalities,... |
if fitted.shape[0] != len(self.modalities):
raise ValueError("This data doesn't look like it had the distance "
"between it and the five modalities calculated")
return fitted.idxmin() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def logliks(self, x):
"""Calculate log-likelihood of a feature x for each model Converts all values that are exactly 1 or exactly 0 to 0.999 and 0.001 because th... |
x = x.copy()
# Replace exactly 0 and exactly 1 values with a very small number
# (machine epsilon, the smallest number that this computer is capable
# of storing) because 0 and 1 are not in the Beta distribution.
x[x == 0] = VERY_SMALL_NUMBER
x[x == 1] = 1 - VERY_SMALL_... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def nice_number_string(number, decimal_places=2):
"""Convert floats to either integers or a nice looking fraction""" |
if number == np.round(number):
return str(int(number))
elif number < 1 and number > 0:
inverse = 1 / number
if int(inverse) == np.round(inverse):
return r'\frac{{1}}{{{}}}'.format(int(inverse))
else:
template = '{{:.{0}}}'.format(d... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def violinplot(self, n=1000, **kwargs):
"""Plot violins of each distribution in the model family Parameters n : int Number of random variables to generate kwargs... |
kwargs.setdefault('palette', 'Purples')
dfs = []
for rv in self.rvs:
psi = rv.rvs(n)
df = pd.Series(psi, name=self.ylabel).to_frame()
alpha, beta = rv.args
alpha = self.nice_number_string(alpha, decimal_places=2)
beta = self.nice_num... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _single_feature_logliks_one_step(self, feature, models):
"""Get log-likelihood of models at each parameterization for given data Parameters feature : pandas.... |
x_non_na = feature[~feature.isnull()]
if x_non_na.empty:
return pd.DataFrame()
else:
dfs = []
for name, model in models.items():
df = model.single_feature_logliks(feature)
df['Modality'] = name
dfs.append(df)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def fit(self, data):
"""Get the modality assignments of each splicing event in the data Parameters data : pandas.DataFrame A (n_samples, n_events) dataframe of s... |
self.assert_less_than_or_equal_1(data.values.flat)
self.assert_non_negative(data.values.flat)
if isinstance(data, pd.DataFrame):
log2_bayes_factors = data.apply(self.single_feature_fit)
elif isinstance(data, pd.Series):
log2_bayes_factors = self.single_feature_f... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def predict(self, log2_bayes_factors, reset_index=False):
"""Guess the most likely modality for each event For each event that has at least one non-NA value, if ... |
if reset_index:
x = log2_bayes_factors.reset_index(level=0, drop=True)
else:
x = log2_bayes_factors
if isinstance(x, pd.DataFrame):
not_na = (x.notnull() > 0).any()
not_na_columns = not_na[not_na].index
x.ix[NULL_MODEL, not_na_columns]... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def single_feature_logliks(self, feature):
"""Calculate log-likelihoods of each modality's parameterization Used for plotting the estimates of a single feature P... |
self.assert_less_than_or_equal_1(feature.values)
self.assert_non_negative(feature.values)
logliks = self._single_feature_logliks_one_step(
feature, self.one_param_models)
logsumexps = self.logliks_to_logsumexp(logliks)
# If none of the one-parameter models passed,... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def single_feature_fit(self, feature):
"""Get the log2 bayes factor of the fit for each modality""" |
if np.isfinite(feature).sum() == 0:
series = pd.Series(index=MODALITY_ORDER)
else:
logbf_one_param = pd.Series(
{k: v.logsumexp_logliks(feature) for
k, v in self.one_param_models.items()})
# Check if none of the previous features fit... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def violinplot(self, n=1000, figsize=None, **kwargs):
r"""Visualize all modality family members with parameters Use violinplots to visualize distributions of mod... |
if figsize is None:
nrows = len(self.models)
width = max(len(m.rvs) for name, m in self.models.items())*0.625
height = nrows*2.5
figsize = width, height
fig, axes = plt.subplots(nrows=nrows, figsize=figsize)
for ax, model_name in zip(axes, MODALI... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bin_range_strings(bins, fmt=':g'):
"""Given a list of bins, make a list of strings of those bin ranges Parameters bins : list_like List of anything, usually ... |
return [('{' + fmt + '}-{' + fmt + '}').format(i, j)
for i, j in zip(bins, bins[1:])] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def binify(data, bins):
"""Makes a histogram of each column the provided binsize Parameters data : pandas.DataFrame A samples x features dataframe. Each feature ... |
if bins is None:
raise ValueError('Must specify "bins"')
if isinstance(data, pd.DataFrame):
binned = data.apply(lambda x: pd.Series(np.histogram(x, bins=bins,
range=(0, 1))[0]))
elif isinstance(data, pd.Series):
binned = p... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def kld(p, q):
"""Kullback-Leiber divergence of two probability distributions pandas dataframes, p and q Parameters p : pandas.DataFrame An nbins x features Data... |
try:
_check_prob_dist(p)
_check_prob_dist(q)
except ValueError:
return np.nan
# If one of them is zero, then the other should be considered to be 0.
# In this problem formulation, log0 = 0
p = p.replace(0, np.nan)
q = q.replace(0, np.nan)
return (np.log2(p / q) * p)... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def jsd(p, q):
"""Finds the per-column JSD between dataframes p and q Jensen-Shannon divergence of two probability distrubutions pandas dataframes, p and q. Thes... |
try:
_check_prob_dist(p)
_check_prob_dist(q)
except ValueError:
return np.nan
weight = 0.5
m = weight * (p + q)
result = weight * kld(p, m) + (1 - weight) * kld(q, m)
return result |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def entropy(binned, base=2):
"""Find the entropy of each column of a dataframe Parameters binned : pandas.DataFrame A nbins x features DataFrame of probability d... |
try:
_check_prob_dist(binned)
except ValueError:
np.nan
return -((np.log(binned) / np.log(base)) * binned).sum(axis=0) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def binify_and_jsd(df1, df2, bins, pair=None):
"""Binify and calculate jensen-shannon divergence between two dataframes Parameters df1, df2 : pandas.DataFrames D... |
binned1 = binify(df1, bins=bins).dropna(how='all', axis=1)
binned2 = binify(df2, bins=bins).dropna(how='all', axis=1)
binned1, binned2 = binned1.align(binned2, axis=1, join='inner')
series = np.sqrt(jsd(binned1, binned2))
series.name = pair
return series |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def cross_phenotype_jsd(data, groupby, bins, n_iter=100):
"""Jensen-Shannon divergence of features across phenotypes Parameters data : pandas.DataFrame A (n_samp... |
grouped = data.groupby(groupby)
jsds = []
seen = set([])
for phenotype1, df1 in grouped:
for phenotype2, df2 in grouped:
pair = tuple(sorted([phenotype1, phenotype2]))
if pair in seen:
continue
seen.add(pair)
if phenotype1 == ph... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def jsd_df_to_2d(jsd_df):
"""Transform a tall JSD dataframe to a square matrix of mean JSDs Parameters jsd_df : pandas.DataFrame A (n_features, n_phenotypes^2) d... |
jsd_2d = jsd_df.mean().reset_index()
jsd_2d = jsd_2d.rename(
columns={'level_0': 'phenotype1', 'level_1': 'phenotype2', 0: 'jsd'})
jsd_2d = jsd_2d.pivot(index='phenotype1', columns='phenotype2',
values='jsd')
return jsd_2d + np.tril(jsd_2d.T, -1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_next_value( sequence_name='default', initial_value=1, reset_value=None, *, nowait=False, using=None):
""" Return the next value for a given sequence. """ |
# Inner import because models cannot be imported before their application.
from .models import Sequence
if reset_value is not None:
assert initial_value < reset_value
if using is None:
using = router.db_for_write(Sequence)
connection = connections[using]
if (getattr(connecti... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def check(self, final_line_count):
"""Check the status of all provided data and update the suite.""" |
if self._lines_seen["version"]:
self._process_version_lines()
self._process_plan_lines(final_line_count) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _process_version_lines(self):
"""Process version line rules.""" |
if len(self._lines_seen["version"]) > 1:
self._add_error(_("Multiple version lines appeared."))
elif self._lines_seen["version"][0] != 1:
self._add_error(_("The version must be on the first line.")) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _process_plan_lines(self, final_line_count):
"""Process plan line rules.""" |
if not self._lines_seen["plan"]:
self._add_error(_("Missing a plan."))
return
if len(self._lines_seen["plan"]) > 1:
self._add_error(_("Only one plan line is permitted per file."))
return
plan, at_line = self._lines_seen["plan"][0]
if not... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _plan_on_valid_line(self, at_line, final_line_count):
"""Check if a plan is on a valid line.""" |
# Put the common cases first.
if at_line == 1 or at_line == final_line_count:
return True
# The plan may only appear on line 2 if the version is at line 1.
after_version = (
self._lines_seen["version"]
and self._lines_seen["version"][0] == 1
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def handle_bail(self, bail):
"""Handle a bail line.""" |
self._add_error(_("Bailed: {reason}").format(reason=bail.reason)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def handle_skipping_plan(self, skip_plan):
"""Handle a plan that contains a SKIP directive.""" |
skip_line = Result(True, None, skip_plan.directive.text, Directive("SKIP"))
self._suite.addTest(Adapter(self._filename, skip_line)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _add_error(self, message):
"""Add an error test to the suite.""" |
error_line = Result(False, None, message, Directive(""))
self._suite.addTest(Adapter(self._filename, error_line)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def format_exception(exception):
"""Format an exception as diagnostics output. exception is the tuple as expected from sys.exc_info. """ |
exception_lines = traceback.format_exception(*exception)
# The lines returned from format_exception do not strictly contain
# one line per element in the list (i.e. some elements have new
# line characters in the middle). Normalize that oddity.
lines = "".join(exception_lines).splitlines(True)
... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def parse(self, fh):
"""Generate tap.line.Line objects, given a file-like object `fh`. `fh` may be any object that implements both the iterator and context manag... |
with fh:
try:
first_line = next(fh)
except StopIteration:
return
first_parsed = self.parse_line(first_line.rstrip())
fh_new = itertools.chain([first_line], fh)
if first_parsed.category == "version" and first_parsed.vers... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def parse_line(self, text, fh=None):
"""Parse a line into whatever TAP category it belongs.""" |
match = self.ok.match(text)
if match:
return self._parse_result(True, match, fh)
match = self.not_ok.match(text)
if match:
return self._parse_result(False, match, fh)
if self.diagnostic.match(text):
return Diagnostic(text)
match = s... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _parse_plan(self, match):
"""Parse a matching plan line.""" |
expected_tests = int(match.group("expected"))
directive = Directive(match.group("directive"))
# Only SKIP directives are allowed in the plan.
if directive.text and not directive.skip:
return Unknown()
return Plan(expected_tests, directive) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _parse_result(self, ok, match, fh=None):
"""Parse a matching result line into a result instance.""" |
peek_match = None
try:
if fh is not None and self._try_peeking:
peek_match = self.yaml_block_start.match(fh.peek())
except StopIteration:
pass
if peek_match is None:
return Result(
ok,
number=match.group... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _extract_yaml_block(self, indent, fh):
"""Extract a raw yaml block from a file handler""" |
raw_yaml = []
indent_match = re.compile(r"^{}".format(indent))
try:
fh.next()
while indent_match.match(fh.peek()):
raw_yaml.append(fh.next().replace(indent, "", 1))
# check for the end and stop adding yaml if encountered
if... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def yaml_block(self):
"""Lazy load a yaml_block. If yaml support is not available, there is an error in parsing the yaml block, or no yaml is associated with thi... |
if LOAD_YAML and self._yaml_block is not None:
try:
yaml_dict = yaml.load(self._yaml_block)
return yaml_dict
except yaml.error.YAMLError:
print("Error parsing yaml block. Check formatting.")
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def load(self, files):
"""Load any files found into a suite. Any directories are walked and their files are added as TAP files. :returns: A ``unittest.TestSuite`... |
suite = unittest.TestSuite()
for filepath in files:
if os.path.isdir(filepath):
self._find_tests_in_directory(filepath, suite)
else:
suite.addTest(self.load_suite_from_file(filepath))
return suite |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def load_suite_from_file(self, filename):
"""Load a test suite with test lines from the provided TAP file. :returns: A ``unittest.TestSuite`` instance """ |
suite = unittest.TestSuite()
rules = Rules(filename, suite)
if not os.path.exists(filename):
rules.handle_file_does_not_exist()
return suite
line_generator = self._parser.parse_file(filename)
return self._load_lines(filename, line_generator, suite, rule... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def load_suite_from_stdin(self):
"""Load a test suite with test lines from the TAP stream on STDIN. :returns: A ``unittest.TestSuite`` instance """ |
suite = unittest.TestSuite()
rules = Rules("stream", suite)
line_generator = self._parser.parse_stdin()
return self._load_lines("stream", line_generator, suite, rules) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _load_lines(self, filename, line_generator, suite, rules):
"""Load a suite with lines produced by the line generator.""" |
line_counter = 0
for line in line_generator:
line_counter += 1
if line.category in self.ignored_lines:
continue
if line.category == "test":
suite.addTest(Adapter(filename, line))
rules.saw_test()
elif line... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _track(self, class_name):
"""Keep track of which test cases have executed.""" |
if self._test_cases.get(class_name) is None:
if self.streaming and self.header:
self._write_test_case_header(class_name, self.stream)
self._test_cases[class_name] = []
if self.combined:
self.combined_test_cases_seen.append(class_name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def set_plan(self, total):
"""Notify the tracker how many total tests there will be.""" |
self.plan = total
if self.streaming:
# This will only write the plan if we haven't written it
# already but we want to check if we already wrote a
# test out (in which case we can't just write the plan out
# right here).
if not self.combined_t... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def generate_tap_reports(self):
"""Generate TAP reports. The results are either combined into a single output file or the output file name is generated from the ... |
# We're streaming but set_plan wasn't called, so we can only
# know the plan now (at the end).
if self.streaming and not self._plan_written:
print("1..{0}".format(self.combined_line_number), file=self.stream)
self._plan_written = True
return
if self.... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _write_plan(self, stream):
"""Write the plan line to the stream. If we have a plan and have not yet written it out, write it to the given stream. """ |
if self.plan is not None:
if not self._plan_written:
print("1..{0}".format(self.plan), file=stream)
self._plan_written = True |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_tap_file_path(self, test_case):
"""Get the TAP output file path for the test case.""" |
sanitized_test_case = test_case.translate(self._sanitized_table)
tap_file = sanitized_test_case + ".tap"
if self.outdir:
return os.path.join(self.outdir, tap_file)
return tap_file |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def main(argv=sys.argv, stream=sys.stderr):
"""Entry point for ``tappy`` command.""" |
args = parse_args(argv)
suite = build_suite(args)
runner = unittest.TextTestRunner(verbosity=args.verbose, stream=stream)
result = runner.run(suite)
return get_status(result) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def build_suite(args):
"""Build a test suite by loading TAP files or a TAP stream.""" |
loader = Loader()
if len(args.files) == 0 or args.files[0] == "-":
suite = loader.load_suite_from_stdin()
else:
suite = loader.load(args.files)
return suite |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def addFailure(self, result):
"""Add a failure to the result.""" |
result.addFailure(self, (Exception, Exception(), None))
# Since TAP will not provide assertion data, clean up the assertion
# section so it is not so spaced out.
test, err = result.failures[-1]
result.failures[-1] = (test, "") |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def after_flush_postexec(self, session, context):
""" Event listener to recursively expire `left` and `right` attributes the parents of all modified instances pa... |
instances = self.instances[session]
while instances:
instance = instances.pop()
if instance not in session:
continue
parent = self.get_parent_value(instance)
while parent != NO_VALUE and parent is not None:
instances.disca... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def move_inside(self, parent_id):
""" Moving one node of tree inside another For example see: * :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_inside_func... | # noqa
session = Session.object_session(self)
self.parent_id = parent_id
self.mptt_move_inside = parent_id
session.add(self) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def move_after(self, node_id):
""" Moving one node of tree after another For example see :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_after_function` ""... | # noqa
session = Session.object_session(self)
self.parent_id = self.parent_id
self.mptt_move_after = node_id
session.add(self) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def move_before(self, node_id):
""" Moving one node of tree before another For example see: * :mod:`sqlalchemy_mptt.tests.cases.move_node.test_move_before_functi... | # noqa
session = Session.object_session(self)
table = _get_tree_table(self.__mapper__)
pk = getattr(table.c, self.get_pk_column().name)
node = session.query(table).filter(pk == node_id).one()
self.parent_id = node.parent_id
self.mptt_move_before = node_id
sessio... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def leftsibling_in_level(self):
""" Node to the left of the current node at the same level For example see :mod:`sqlalchemy_mptt.tests.cases.get_tree.test_leftsi... | # noqa
table = _get_tree_table(self.__mapper__)
session = Session.object_session(self)
current_lvl_nodes = session.query(table) \
.filter_by(level=self.level).filter_by(tree_id=self.tree_id) \
.filter(table.c.lft < self.left).order_by(table.c.lft).all()
if curre... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _node_to_dict(cls, node, json, json_fields):
""" Helper method for ``get_tree``. """ |
if json:
pk_name = node.get_pk_name()
# jqTree or jsTree format
result = {'id': getattr(node, pk_name), 'label': node.__repr__()}
if json_fields:
result.update(json_fields(node))
else:
result = {'node': node}
return res... |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_tree(cls, session=None, json=False, json_fields=None, query=None):
""" This method generate tree of current node table in dict or json format. You can ma... | # noqa
tree = []
nodes_of_level = {}
# handle custom query
nodes = cls._base_query(session)
if query:
nodes = query(nodes)
nodes = cls._base_order(nodes).all()
# search minimal level of nodes.
min_level = min([node.level for node in nodes] ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.