_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q260600 | delete_locks | validation | def delete_locks(context, network_ids, addresses):
"""Deletes locks for each IP address that is no longer null-routed."""
addresses_no_longer_null_routed = _find_addresses_to_be_unlocked(
context, network_ids, addresses)
LOG.info("Deleting %s lock holders on IPAddress with ids: %s",
len(addresses_no_longer_null_routed),
[addr.id for addr in addresses_no_longer_null_routed])
for address in addresses_no_longer_null_routed:
lock_holder = None
try:
lock_holder = db_api.lock_holder_find(
context, lock_id=address.lock_id, name=LOCK_NAME,
| python | {
"resource": ""
} |
q260601 | create_locks | validation | def create_locks(context, network_ids, addresses):
"""Creates locks for each IP address that is null-routed.
The function creates the IP address if it is not present in the database.
"""
for address in addresses:
address_model = None
try:
address_model = _find_or_create_address(
context, network_ids, address)
lock_holder = None
if address_model.lock_id:
lock_holder = db_api.lock_holder_find(
| python | {
"resource": ""
} |
q260602 | IronicDriver.select_ipam_strategy | validation | def select_ipam_strategy(self, network_id, network_strategy, **kwargs):
"""Return relevant IPAM strategy name.
:param network_id: neutron network id.
:param network_strategy: default strategy for the network.
NOTE(morgabra) This feels like a hack but I can't think of a better
idea. The root problem is we can now attach ports to networks with
a different backend driver/ipam strategy than the network speficies.
We handle the the backend driver part with allowing network_plugin to
be specified for port objects. This works pretty well because nova or
whatever knows when we are hooking up an Ironic node so it can pass
along that key during port_create().
IPAM is a little trickier, especially in Ironic's case, because we
*must* use a specific IPAM for provider networks. There isn't really
much of an option other than involve the backend driver when selecting
the IPAM strategy.
"""
LOG.info("Selecting IPAM strategy for network_id:%s "
"network_strategy:%s" % (network_id, network_strategy))
net_type = "tenant"
if STRATEGY.is_provider_network(network_id):
net_type = "provider"
strategy = self._ipam_strategies.get(net_type, {})
default = strategy.get("default")
| python | {
"resource": ""
} |
q260603 | IronicDriver._get_base_network_info | validation | def _get_base_network_info(self, context, network_id, base_net_driver):
"""Return a dict of extra network information.
:param context: neutron request context.
:param network_id: neturon network id.
:param net_driver: network driver associated with network_id.
:raises IronicException: Any unexpected data fetching failures will
be logged and IronicException raised.
This driver can attach to networks managed by other drivers. We may
need some information from these drivers, or otherwise inform
downstream about the type of network we are attaching to. We can
make these decisions here.
"""
driver_name = base_net_driver.get_name()
net_info = {"network_type": driver_name}
LOG.debug('_get_base_network_info: %s %s'
% (driver_name, network_id))
# If the driver is NVP, we need to look up the lswitch id we should
# be attaching to.
if driver_name == 'NVP':
LOG.debug('looking up lswitch ids for network %s'
% (network_id))
| python | {
"resource": ""
} |
q260604 | IronicDriver.create_port | validation | def create_port(self, context, network_id, port_id, **kwargs):
"""Create a port.
:param context: neutron api request context.
:param network_id: neutron network id.
:param port_id: neutron port id.
:param kwargs:
required keys - device_id: neutron port device_id (instance_id)
instance_node_id: nova hypervisor host id
mac_address: neutron port mac address
base_net_driver: the base network driver
optional keys - addresses: list of allocated IPAddress models
security_groups: list of associated security groups
:raises IronicException: If the client is unable to create the
downstream port for any reason, the exception will be logged
and IronicException raised.
"""
LOG.info("create_port %s %s %s" % (context.tenant_id, network_id,
port_id))
# sanity check
if not kwargs.get('base_net_driver'):
raise IronicException(msg='base_net_driver required.')
base_net_driver = kwargs['base_net_driver']
if not kwargs.get('device_id'):
raise IronicException(msg='device_id required.')
device_id = kwargs['device_id']
if not kwargs.get('instance_node_id'):
raise IronicException(msg='instance_node_id | python | {
"resource": ""
} |
q260605 | IronicDriver.update_port | validation | def update_port(self, context, port_id, **kwargs):
"""Update a port.
:param context: neutron api request context.
:param port_id: neutron port id.
:param kwargs: optional kwargs.
:raises IronicException: If the client is unable to update the
downstream port for any reason, the exception will be logged
and IronicException raised.
TODO(morgabra) It does not really make sense in the context of Ironic
to allow updating ports. fixed_ips and mac_address are burned in the
configdrive on the host, and we otherwise cannot migrate a port between
instances. Eventually we will need to support security groups, but for
now it's a no-op on port data changes, and we | python | {
"resource": ""
} |
q260606 | IronicDriver.diag_port | validation | def diag_port(self, context, port_id, **kwargs):
"""Diagnose a port.
:param context: neutron api request context.
:param port_id: neutron port id.
:param kwargs: optional kwargs.
:raises IronicException: If the client is unable to fetch the
downstream | python | {
"resource": ""
} |
q260607 | Tag.set | validation | def set(self, model, value):
"""Set tag on model object."""
self.validate(value)
| python | {
"resource": ""
} |
q260608 | Tag.get | validation | def get(self, model):
"""Get a matching valid tag off the model."""
for tag in model.tags:
if self.is_tag(tag):
value = self.deserialize(tag)
try:
| python | {
"resource": ""
} |
q260609 | Tag._pop | validation | def _pop(self, model):
"""Pop all matching tags off the model and return them."""
tags = []
# collect any exsiting tags with matching prefix
for tag in model.tags:
if self.is_tag(tag):
| python | {
"resource": ""
} |
q260610 | Tag.pop | validation | def pop(self, model):
"""Pop all matching tags off the port, return a valid one."""
tags = self._pop(model)
if tags:
for tag in tags:
value = self.deserialize(tag)
| python | {
"resource": ""
} |
q260611 | Tag.has_tag | validation | def has_tag(self, model):
"""Does the given port have this tag?"""
for tag in model.tags:
| python | {
"resource": ""
} |
q260612 | VlanTag.validate | validation | def validate(self, value):
"""Validates a VLAN ID.
:param value: The VLAN ID to validate against.
:raises TagValidationError: Raised if the VLAN ID is invalid.
"""
try:
vlan_id_int = int(value)
assert vlan_id_int >= self.MIN_VLAN_ID
assert vlan_id_int <= self.MAX_VLAN_ID
except Exception:
msg = ("Invalid vlan_id. Got '%(vlan_id)s'. "
"vlan_id should be an integer between %(min)d and %(max)d "
| python | {
"resource": ""
} |
q260613 | TagRegistry.get_all | validation | def get_all(self, model):
"""Get all known tags from a model.
Returns a dict of {<tag_name>:<tag_value>}.
"""
tags = {}
for name, tag in self.tags.items():
| python | {
"resource": ""
} |
q260614 | TagRegistry.set_all | validation | def set_all(self, model, **tags):
"""Validate and set all known tags on a port."""
for name, tag in self.tags.items():
if name in tags:
value = tags.pop(name)
if value:
try:
tag.set(model, value)
| python | {
"resource": ""
} |
q260615 | SecurityGroupsClient.serialize_rules | validation | def serialize_rules(self, rules):
"""Creates a payload for the redis server."""
# TODO(mdietz): If/when we support other rule types, this comment
# will have to be revised.
# Action and direction are static, for now. The implementation may
# support 'deny' and 'egress' respectively in the future. We allow
# the direction to be set to something else, technically, but current
# plugin level call actually raises. It's supported here for unit
# test purposes at this time
serialized = []
for rule in rules:
direction = rule["direction"]
source = ''
destination = ''
if rule.get("remote_ip_prefix"):
prefix = rule["remote_ip_prefix"]
if direction == "ingress":
source = self._convert_remote_network(prefix)
else:
if (Capabilities.EGRESS not in
CONF.QUARK.environment_capabilities):
raise q_exc.EgressSecurityGroupRulesNotEnabled()
else:
destination = self._convert_remote_network(prefix)
| python | {
"resource": ""
} |
q260616 | SecurityGroupsClient.serialize_groups | validation | def serialize_groups(self, groups):
"""Creates a payload for the redis server
The rule schema is the following:
REDIS KEY - port_device_id.port_mac_address/sg
REDIS VALUE - A JSON dump of the following:
port_mac_address must be lower-cased and stripped of non-alphanumeric
characters
{"id": "<arbitrary uuid>",
"rules": [
{"ethertype": <hexademical integer>,
"protocol": <integer>,
"port start": <integer>, # optional
"port end": <integer>, # optional
"icmp type": <integer>, # optional
"icmp code": <integer>, # optional
"source network": <string>,
"destination network": <string>,
"action": <string>,
"direction": <string>},
],
"security groups ack": <boolean>
}
Example:
{"id": "004c6369-9f3d-4d33-b8f5-9416bf3567dd",
"rules": | python | {
"resource": ""
} |
q260617 | SecurityGroupsClient.apply_rules | validation | def apply_rules(self, device_id, mac_address, rules):
"""Writes a series of security group rules to a redis server."""
LOG.info("Applying security group rules for device %s with MAC %s" %
(device_id, mac_address))
rule_dict = {SECURITY_GROUP_RULE_KEY: rules}
redis_key = self.vif_key(device_id, mac_address)
# TODO(mdietz): | python | {
"resource": ""
} |
q260618 | SecurityGroupsClient.get_security_group_states | validation | def get_security_group_states(self, interfaces):
"""Gets security groups for interfaces from Redis
Returns a dictionary of xapi.VIFs with values of the current
acknowledged status in Redis.
States not explicitly handled:
* ack key, no rules - This is the same as just tagging the VIF,
the instance will be inaccessible
* rules key, no ack - Nothing will happen, the VIF will
not be tagged.
"""
LOG.debug("Getting security groups from Redis for {0}".format(
interfaces))
interfaces = tuple(interfaces)
vif_keys = [self.vif_key(vif.device_id, vif.mac_address)
for vif in interfaces]
# Retrieve all fields associated with this key, which should be
# 'security groups ack' and 'security group rules'.
sec_grp_all = self.get_fields_all(vif_keys)
ret = {}
# Associate the vif with the fields in a dictionary
for vif, group in zip(interfaces, sec_grp_all):
if group:
ret[vif] = {SECURITY_GROUP_ACK: None,
| python | {
"resource": ""
} |
q260619 | SecurityGroupsClient.update_group_states_for_vifs | validation | def update_group_states_for_vifs(self, vifs, ack):
"""Updates security groups by setting the ack field"""
vif_keys = [self.vif_key(vif.device_id, vif.mac_address)
| python | {
"resource": ""
} |
q260620 | run_migrations_offline | validation | def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script | python | {
"resource": ""
} |
q260621 | run_migrations_online | validation | def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = create_engine(
neutron_config.database.connection,
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(
connection=connection,
| python | {
"resource": ""
} |
q260622 | do_notify | validation | def do_notify(context, event_type, payload):
"""Generic Notifier.
Parameters:
- `context`: session context
- `event_type`: the event type to report, i.e. ip.usage
- `payload`: dict containing the payload | python | {
"resource": ""
} |
q260623 | notify | validation | def notify(context, event_type, ipaddress, send_usage=False, *args, **kwargs):
"""Method to send notifications.
We must send USAGE when a public IPv4 address is deallocated or a FLIP is
associated.
Parameters:
- `context`: the context for notifier
- `event_type`: the event type for IP allocate, deallocate, associate,
disassociate
- `ipaddress`: the ipaddress object to notify about
Returns:
nothing
Notes: this may live in the billing module
"""
if (event_type == IP_ADD and not CONF.QUARK.notify_ip_add) or \
(event_type == IP_DEL and not CONF.QUARK.notify_ip_delete) or \
(event_type == IP_ASSOC and not CONF.QUARK.notify_flip_associate) or \
(event_type == IP_DISASSOC and not CONF.QUARK.notify_flip_disassociate)\
or (event_type == IP_EXISTS and not CONF.QUARK.notify_ip_exists):
LOG.debug('IP_BILL: notification {} is disabled by config'.
format(event_type))
return
# Do not send notifications when we are undoing due to an error
if 'rollback' in kwargs and kwargs['rollback']:
LOG.debug('IP_BILL: not sending notification because we are in undo')
return
# ip.add needs the allocated_at time.
# All other events need the current time.
ts = ipaddress.allocated_at if event_type == IP_ADD else _now()
payload = build_payload(ipaddress, event_type, event_time=ts)
# Send the notification with the payload
do_notify(context, event_type, payload)
# When we deallocate an IP or associate a FLIP we must send
| python | {
"resource": ""
} |
q260624 | build_payload | validation | def build_payload(ipaddress,
event_type,
event_time=None,
start_time=None,
end_time=None):
"""Method builds a payload out of the passed arguments.
Parameters:
`ipaddress`: the models.IPAddress object
`event_type`: USAGE,CREATE,DELETE,SUSPEND,or UNSUSPEND
`start_time`: startTime for cloudfeeds
`end_time`: endTime for cloudfeeds
Returns a dictionary suitable to notify billing.
Message types mapping to cloud feeds for references:
ip.exists - USAGE
ip.add - CREATE
ip.delete - DELETE
ip.associate - UP
ip.disassociate - DOWN
Refer to: http://rax.io/cf-api for more details.
"""
# This is the common part of all message types
payload = {
'event_type': unicode(event_type),
'tenant_id': unicode(ipaddress.used_by_tenant_id),
'ip_address': unicode(ipaddress.address_readable),
'ip_version': int(ipaddress.version),
'ip_type': unicode(ipaddress.address_type),
'id': unicode(ipaddress.id)
}
# Depending on the message type add the appropriate fields
if event_type == IP_EXISTS:
if start_time is None or end_time is None:
raise ValueError('IP_BILL: {} start_time/end_time cannot be empty'
.format(event_type))
| python | {
"resource": ""
} |
q260625 | build_full_day_ips | validation | def build_full_day_ips(query, period_start, period_end):
"""Method to build an IP list for the case 1
when the IP was allocated before the period start
and is still allocated after the period end.
This method only looks at public IPv4 addresses.
"""
# Filter out only IPv4 that have not been deallocated
ip_list = query.\
filter(models.IPAddress.version == 4L).\
filter(models.IPAddress.network_id == PUBLIC_NETWORK_ID).\
filter(models.IPAddress.used_by_tenant_id is not None).\
| python | {
"resource": ""
} |
q260626 | calc_periods | validation | def calc_periods(hour=0, minute=0):
"""Returns a tuple of start_period and end_period.
Assumes that the period is 24-hrs.
Parameters:
- `hour`: the hour from 0 to 23 when the period ends
- `minute`: the minute from 0 to 59 when the period ends
This method will calculate the end of the period as the closest hour/minute
going backwards.
It will also calculate the start of the period as the passed hour/minute
but 24 hrs ago.
Example, if we pass 0, 0 - we will get the events from 0:00 midnight of the
day before yesterday until today's midnight.
If we pass 2,0 - we will get the start time as 2am of the previous morning
till 2am of today's morning.
By default it's midnight.
"""
# Calculate the time intervals in a usable form
period_end = datetime.datetime.utcnow().replace(hour=hour,
minute=minute,
| python | {
"resource": ""
} |
q260627 | _make_job_dict | validation | def _make_job_dict(job):
"""Creates the view for a job while calculating progress.
Since a root job does not have a transaction id (TID) it will return its
id as the TID.
"""
body = {"id": job.get('id'),
"action": job.get('action'),
"completed": job.get('completed'),
"tenant_id": job.get('tenant_id'),
"created_at": job.get('created_at'),
"transaction_id": job.get('transaction_id'),
"parent_id": job.get('parent_id', None)}
if not body['transaction_id']:
body['transaction_id'] = job.get('id')
completed = 0
for sub in job.subtransactions:
if sub.get('completed'):
| python | {
"resource": ""
} |
q260628 | get_mac_address_range | validation | def get_mac_address_range(context, id, fields=None):
"""Retrieve a mac_address_range.
: param context: neutron api request context
: param id: UUID representing the network to fetch.
: param fields: a list of strings that are valid keys in a
network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP
object in neutron/api/v2/attributes.py. Only these fields
| python | {
"resource": ""
} |
q260629 | delete_mac_address_range | validation | def delete_mac_address_range(context, id):
"""Delete a mac_address_range.
: param context: neutron api request context
: param id: UUID representing the mac_address_range to delete.
"""
LOG.info("delete_mac_address_range %s for tenant %s" %
(id, context.tenant_id))
if not context.is_admin:
raise n_exc.NotAuthorized()
| python | {
"resource": ""
} |
q260630 | delete_segment_allocation_range | validation | def delete_segment_allocation_range(context, sa_id):
"""Delete a segment_allocation_range.
: param context: neutron api request context
: param id: UUID representing the segment_allocation_range to delete.
"""
LOG.info("delete_segment_allocation_range %s for tenant %s" %
| python | {
"resource": ""
} |
q260631 | filter_factory | validation | def filter_factory(global_conf, **local_conf):
"""Returns a WSGI filter app for use with paste.deploy."""
conf = global_conf.copy()
conf.update(local_conf)
def | python | {
"resource": ""
} |
q260632 | get_used_ips | validation | def get_used_ips(session, **kwargs):
"""Returns dictionary with keys segment_id and value used IPs count.
Used IP address count is determined by:
- allocated IPs
- deallocated IPs whose `deallocated_at` is within the `reuse_after`
window compared to the present time, excluding IPs that are accounted for
in the current IP policy (because IP policy is mutable and deallocated IPs
are not checked nor deleted on IP policy creation, thus deallocated IPs
that don't fit the current IP policy can exist in the neutron database).
"""
LOG.debug("Getting used IPs...")
with session.begin():
query = session.query(
models.Subnet.segment_id,
func.count(models.IPAddress.address))
query = query.group_by(models.Subnet.segment_id)
query = _filter(query, **kwargs)
reuse_window = timeutils.utcnow() - datetime.timedelta(
seconds=cfg.CONF.QUARK.ipam_reuse_after)
# NOTE(asadoughi): This is an outer join instead of a regular join
# to include subnets with zero IP addresses in the database.
query = query.outerjoin(
models.IPAddress,
and_(models.Subnet.id == models.IPAddress.subnet_id,
or_(not_(models.IPAddress.lock_id.is_(None)),
models.IPAddress._deallocated.is_(None),
| python | {
"resource": ""
} |
q260633 | get_unused_ips | validation | def get_unused_ips(session, used_ips_counts, **kwargs):
"""Returns dictionary with key segment_id, and value unused IPs count.
Unused IP address count is determined by:
- adding subnet's cidr's size
- subtracting IP policy exclusions on subnet
- subtracting used ips per segment
"""
LOG.debug("Getting unused IPs...")
with session.begin():
query = session.query(
models.Subnet.segment_id,
models.Subnet)
query = _filter(query, **kwargs)
query = query.group_by(models.Subnet.segment_id, models.Subnet.id)
ret = defaultdict(int)
for | python | {
"resource": ""
} |
q260634 | XapiClient.get_interfaces | validation | def get_interfaces(self):
"""Returns a set of VIFs from `get_instances` return value."""
LOG.debug("Getting interfaces from Xapi")
with self.sessioned() as session:
instances = self.get_instances(session)
recs = session.xenapi.VIF.get_all_records()
interfaces = set()
for | python | {
"resource": ""
} |
q260635 | XapiClient.update_interfaces | validation | def update_interfaces(self, added_sg, updated_sg, removed_sg):
"""Handles changes to interfaces' security groups
Calls refresh_interfaces on argument VIFs. Set security groups on
added_sg's VIFs. Unsets security groups on removed_sg's VIFs.
| python | {
"resource": ""
} |
q260636 | update_network | validation | def update_network(context, id, network):
"""Update values of a network.
: param context: neutron api request context
: param id: UUID representing the network to update.
: param network: dictionary with keys indicating fields to update.
valid keys are those that have a value of True for 'allow_put'
as listed in the RESOURCE_ATTRIBUTE_MAP object in
neutron/api/v2/attributes.py.
"""
LOG.info("update_network %s for tenant %s" %
(id, context.tenant_id))
with context.session.begin():
net = db_api.network_find(context, id=id, scope=db_api.ONE)
if not net:
raise | python | {
"resource": ""
} |
q260637 | get_network | validation | def get_network(context, id, fields=None):
"""Retrieve a network.
: param context: neutron api request context
: param id: UUID representing the network to fetch.
: param fields: a list of strings that are valid keys in a
network dictionary as listed in the RESOURCE_ATTRIBUTE_MAP
object in neutron/api/v2/attributes.py. Only these fields
| python | {
"resource": ""
} |
q260638 | get_networks | validation | def get_networks(context, limit=None, sorts=['id'], marker=None,
page_reverse=False, filters=None, fields=None):
"""Retrieve a list of networks.
The contents of the list depends on the identity of the user
making the request (as indicated by the context) as well as any
filters.
: param context: neutron api request context
: param filters: a dictionary with keys that are valid keys for
a network as listed in the RESOURCE_ATTRIBUTE_MAP object
in neutron/api/v2/attributes.py. Values in this dictiontary
are an iterable containing values that will be used for an exact
match comparison for that value. Each result returned by this
function will have matched one of the values for each key in
filters.
: | python | {
"resource": ""
} |
q260639 | get_networks_count | validation | def get_networks_count(context, filters=None):
"""Return the number of networks.
The result depends on the identity of the user making the request
(as indicated by the context) as well as any filters.
: param context: neutron api request context
: param filters: a dictionary with keys that are valid keys for
a network as listed in the RESOURCE_ATTRIBUTE_MAP object
| python | {
"resource": ""
} |
q260640 | delete_network | validation | def delete_network(context, id):
"""Delete a network.
: param context: neutron api request context
: param id: UUID representing the network to delete.
"""
LOG.info("delete_network %s for tenant %s" % (id, context.tenant_id))
with context.session.begin():
net = db_api.network_find(context=context, limit=None, sorts=['id'],
marker=None, page_reverse=False, id=id,
scope=db_api.ONE)
if not net:
raise n_exc.NetworkNotFound(net_id=id)
if not context.is_admin:
| python | {
"resource": ""
} |
q260641 | make_case2 | validation | def make_case2(context):
"""This is a helper method for testing.
When run with the current context, it will create a case 2 entries
in the database. See top of file for what case 2 is.
"""
query = context.session.query(models.IPAddress)
period_start, period_end = billing.calc_periods()
ip_list = billing.build_full_day_ips(query, period_start, period_end)
| python | {
"resource": ""
} |
q260642 | main | validation | def main(notify, hour, minute):
"""Runs billing report. Optionally sends notifications to billing"""
# Read the config file and get the admin context
config_opts = ['--config-file', '/etc/neutron/neutron.conf']
config.init(config_opts)
# Have to load the billing module _after_ config is parsed so
# that we get the right network strategy
network_strategy.STRATEGY.load()
billing.PUBLIC_NETWORK_ID = network_strategy.STRATEGY.get_public_net_id()
config.setup_logging()
context = neutron_context.get_admin_context()
# A query to get all IPAddress objects from the db
query = context.session.query(models.IPAddress)
(period_start, period_end) = billing.calc_periods(hour, minute)
full_day_ips = billing.build_full_day_ips(query,
period_start,
period_end)
partial_day_ips = billing.build_partial_day_ips(query,
period_start,
period_end)
if notify:
# '==================== Full Day ============================='
for ipaddress in full_day_ips:
click.echo('start: {}, end: {}'.format(period_start, period_end))
payload = billing.build_payload(ipaddress,
billing.IP_EXISTS,
start_time=period_start,
end_time=period_end)
billing.do_notify(context,
billing.IP_EXISTS,
payload)
# '==================== Part Day ============================='
for ipaddress in partial_day_ips:
click.echo('start: {}, end: {}'.format(period_start, period_end))
| python | {
"resource": ""
} |
q260643 | QuarkAsyncPluginBase.start_rpc_listeners | validation | def start_rpc_listeners(self):
"""Configure all listeners here"""
self._setup_rpc()
if not self.endpoints:
return []
self.conn = n_rpc.create_connection()
| python | {
"resource": ""
} |
q260644 | QuarkAsyncPluginBase.context | validation | def context(self):
"""Provides an admin context for workers."""
if not self._context:
| python | {
"resource": ""
} |
q260645 | QuarkSGAsyncProcessCallback.update_sg | validation | def update_sg(self, context, sg, rule_id, action):
"""Begins the async update process."""
db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)
if not db_sg:
return None
with context.session.begin():
job_body = dict(action="%s sg rule %s" % (action, rule_id),
resource_id=rule_id,
tenant_id=db_sg['tenant_id'])
job_body = dict(job=job_body)
job = job_api.create_job(context.elevated(), job_body) | python | {
"resource": ""
} |
q260646 | QuarkSGProducerCallback.populate_subtasks | validation | def populate_subtasks(self, context, sg, parent_job_id):
"""Produces a list of ports to be updated async."""
db_sg = db_api.security_group_find(context, id=sg, scope=db_api.ONE)
if not db_sg:
return None
ports = db_api.sg_gather_associated_ports(context, db_sg)
if len(ports) == 0:
return {"ports": 0}
for port in ports:
job_body = dict(action="update port %s" % port['id'],
| python | {
"resource": ""
} |
q260647 | QuarkSGConsumerCallback.update_ports_for_sg | validation | def update_ports_for_sg(self, context, portid, jobid):
"""Updates the ports through redis."""
port = db_api.port_find(context, id=portid, scope=db_api.ONE)
if not port:
LOG.warning("Port not found")
return
net_driver = port_api._get_net_driver(port.network, port=port)
base_net_driver = port_api._get_net_driver(port.network)
sg_list = [sg for sg in port.security_groups]
success = False
error = None
retries = 3
retry_delay = 2
for retry in xrange(retries):
try:
net_driver.update_port(context, port_id=port["backend_key"],
mac_address=port["mac_address"],
device_id=port["device_id"],
base_net_driver=base_net_driver,
| python | {
"resource": ""
} |
q260648 | sg_gather_associated_ports | validation | def sg_gather_associated_ports(context, group):
"""Gather all ports associated to security group.
Returns:
* list, or None
"""
if not group:
return None
if not | python | {
"resource": ""
} |
q260649 | security_group_rule_update | validation | def security_group_rule_update(context, rule, **kwargs):
'''Updates a security group rule.
| python | {
"resource": ""
} |
q260650 | segment_allocation_find | validation | def segment_allocation_find(context, lock_mode=False, **filters):
"""Query for segment allocations."""
range_ids = filters.pop("segment_allocation_range_ids", None)
query = context.session.query(models.SegmentAllocation)
if lock_mode:
query = query.with_lockmode("update")
query = query.filter_by(**filters)
# Optionally filter by given list | python | {
"resource": ""
} |
q260651 | NikoHomeControlConnection.send | validation | def send(self, s):
"""
Sends the given command to Niko Home Control and returns the output of
the system.
| python | {
"resource": ""
} |
q260652 | if_ | validation | def if_(*args):
"""Implements the 'if' operator with support for multiple elseif-s."""
for i in range(0, len(args) - 1, 2):
if args[i]:
| python | {
"resource": ""
} |
q260653 | soft_equals | validation | def soft_equals(a, b):
"""Implements the '==' operator, which does type JS-style coertion."""
if isinstance(a, str) or isinstance(b, str):
return str(a) == str(b)
| python | {
"resource": ""
} |
q260654 | less | validation | def less(a, b, *args):
"""Implements the '<' operator with JS-style type coertion."""
types = set([type(a), type(b)])
if float in types or int in types:
try:
a, b = float(a), float(b)
| python | {
"resource": ""
} |
q260655 | less_or_equal | validation | def less_or_equal(a, b, *args):
"""Implements the '<=' operator with JS-style type coertion."""
return (
less(a, b) | python | {
"resource": ""
} |
q260656 | minus | validation | def minus(*args):
"""Also, converts either to ints or to floats."""
| python | {
"resource": ""
} |
q260657 | merge | validation | def merge(*args):
"""Implements the 'merge' operator for merging lists."""
ret = []
for arg in args:
if isinstance(arg, list) or isinstance(arg, tuple):
| python | {
"resource": ""
} |
q260658 | get_var | validation | def get_var(data, var_name, not_found=None):
"""Gets variable value from data dictionary."""
try:
for key in str(var_name).split('.'):
| python | {
"resource": ""
} |
q260659 | missing | validation | def missing(data, *args):
"""Implements the missing operator for finding missing variables."""
not_found = object()
if args and | python | {
"resource": ""
} |
q260660 | missing_some | validation | def missing_some(data, min_required, args):
"""Implements the missing_some operator for finding missing variables."""
if min_required < 1:
return []
found = 0
not_found = object()
ret = []
for arg in args:
if get_var(data, arg, not_found) | python | {
"resource": ""
} |
q260661 | jsonLogic | validation | def jsonLogic(tests, data=None):
"""Executes the json-logic with given data."""
# You've recursed to a primitive, stop!
if tests is None or not isinstance(tests, dict):
return tests
data = data or {}
operator = list(tests.keys())[0]
values = tests[operator]
# Easy syntax for unary operators, like {"var": "x"} instead of strict
# {"var": ["x"]}
if not isinstance(values, list) and not isinstance(values, tuple):
values = [values]
# Recursion!
values = [jsonLogic(val, data) for val in values]
if operator == 'var':
return get_var(data, *values)
| python | {
"resource": ""
} |
q260662 | PyIndenterMode.indent | validation | def indent(self):
"""
Performs an indentation
"""
if not self.tab_always_indent:
super(PyIndenterMode, self).indent()
else:
cursor = self.editor.textCursor()
assert isinstance(cursor, QtGui.QTextCursor)
if cursor.hasSelection():
self.indent_selection(cursor)
else:
# simply insert indentation at the cursor position
tab_len = self.editor.tab_length
| python | {
"resource": ""
} |
q260663 | PyIndenterMode.unindent | validation | def unindent(self):
"""
Performs an un-indentation
"""
if self.tab_always_indent:
cursor = self.editor.textCursor()
| python | {
"resource": ""
} |
q260664 | PyAutoIndentMode._handle_indent_between_paren | validation | def _handle_indent_between_paren(self, column, line, parent_impl, tc):
"""
Handle indent between symbols such as parenthesis, braces,...
"""
pre, post = parent_impl
next_char = self._get_next_char(tc)
prev_char = self._get_prev_char(tc)
prev_open = prev_char in ['[', '(', '{']
next_close = next_char in [']', ')', '}']
(open_line, open_symbol_col), (close_line, close_col) = \
self._get_paren_pos(tc, column)
open_line_txt = self._helper.line_text(open_line)
open_line_indent = len(open_line_txt) - len(open_line_txt.lstrip())
if prev_open:
post = (open_line_indent + self.editor.tab_length) * ' '
elif next_close and prev_char != ',':
post = open_line_indent * ' '
elif tc.block().blockNumber() == open_line:
post = open_symbol_col * ' '
# adapt indent if cursor on closing line and next line have same
# indent -> PEP8 compliance
if close_line and close_col:
txt = self._helper.line_text(close_line)
bn = tc.block().blockNumber()
flg = bn == close_line
next_indent = self._helper.line_indent(bn + 1) * ' '
if flg and txt.strip().endswith(':') and next_indent == post:
| python | {
"resource": ""
} |
q260665 | PyAutoIndentMode._at_block_start | validation | def _at_block_start(tc, line):
"""
Improve QTextCursor.atBlockStart to ignore spaces
"""
| python | {
"resource": ""
} |
q260666 | PyConsole.update_terminal_colors | validation | def update_terminal_colors(self):
"""
Update terminal color scheme based on the pygments color scheme colors
"""
self.color_scheme | python | {
"resource": ""
} |
q260667 | PyInteractiveConsole.mouseMoveEvent | validation | def mouseMoveEvent(self, e):
"""
Extends mouseMoveEvent to display a pointing hand cursor when the
mouse cursor is over a file location
"""
super(PyInteractiveConsole, self).mouseMoveEvent(e)
cursor = self.cursorForPosition(e.pos())
assert isinstance(cursor, QtGui.QTextCursor)
| python | {
"resource": ""
} |
q260668 | PyInteractiveConsole.mousePressEvent | validation | def mousePressEvent(self, e):
"""
Emits open_file_requested if the press event occured over
a file location string.
"""
super(PyInteractiveConsole, self).mousePressEvent(e)
cursor = self.cursorForPosition(e.pos())
p = cursor.positionInBlock()
usd = cursor.block().userData()
| python | {
"resource": ""
} |
q260669 | MainWindow.setup_actions | validation | def setup_actions(self):
""" Connects slots to signals """
self.actionOpen.triggered.connect(self.on_open)
self.actionNew.triggered.connect(self.on_new)
self.actionSave.triggered.connect(self.on_save)
self.actionSave_as.triggered.connect(self.on_save_as)
self.actionQuit.triggered.connect(
QtWidgets.QApplication.instance().quit)
self.tabWidget.current_changed.connect(self.on_current_tab_changed)
self.tabWidget.last_tab_closed.connect(self.on_last_tab_closed)
| python | {
"resource": ""
} |
q260670 | MainWindow.setup_editor | validation | def setup_editor(self, editor):
"""
Setup the python editor, run the server and connect a few signals.
:param editor: editor to setup.
"""
editor.cursorPositionChanged.connect(self.on_cursor_pos_changed)
| python | {
"resource": ""
} |
q260671 | MainWindow.open_file | validation | def open_file(self, path, line=None):
"""
Creates a new GenericCodeEdit, opens the requested file and adds it
to the tab widget.
:param path: Path of the file to open
:return The opened editor if open succeeded.
"""
editor = None
if path:
interpreter, pyserver, args = self._get_backend_parameters()
editor = self.tabWidget.open_document(
path, None, interpreter=interpreter, server_script=pyserver,
args=args)
if editor:
| python | {
"resource": ""
} |
q260672 | MainWindow.on_new | validation | def on_new(self):
"""
Add a new empty code editor to the tab widget
"""
interpreter, pyserver, args = self._get_backend_parameters()
self.setup_editor(self.tabWidget.create_new_document(
extension='.py', | python | {
"resource": ""
} |
q260673 | MainWindow.on_open | validation | def on_open(self):
"""
Shows an open file dialog and open the file if the dialog was
accepted.
"""
filename, filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Open')
if filename:
| python | {
"resource": ""
} |
q260674 | MainWindow.on_save_as | validation | def on_save_as(self):
"""
Save the current editor document as.
"""
path = self.tabWidget.current_widget().file.path
path = os.path.dirname(path) if path else ''
filename, filter = QtWidgets.QFileDialog.getSaveFileName(
| python | {
"resource": ""
} |
q260675 | MainWindow.setup_mnu_style | validation | def setup_mnu_style(self, editor):
""" setup the style menu for an editor tab """
menu = QtWidgets.QMenu('Styles', self.menuEdit)
group = QtWidgets.QActionGroup(self)
self.styles_group = group
current_style = editor.syntax_highlighter.color_scheme.name
group.triggered.connect(self.on_style_changed)
for s in sorted(PYGMENTS_STYLES):
a = QtWidgets.QAction(menu) | python | {
"resource": ""
} |
q260676 | MainWindow.on_current_tab_changed | validation | def on_current_tab_changed(self):
"""
Update action states when the current tab changed.
"""
self.menuEdit.clear()
self.menuModes.clear()
self.menuPanels.clear()
editor = self.tabWidget.current_widget()
self.menuEdit.setEnabled(editor is not None)
self.menuModes.setEnabled(editor is not None)
self.menuPanels.setEnabled(editor is not None)
self.actionSave.setEnabled(editor is not None)
self.actionSave_as.setEnabled(editor is not | python | {
"resource": ""
} |
q260677 | MainWindow.on_run | validation | def on_run(self):
"""
Run the current current script
"""
filename = self.tabWidget.current_widget().file.path
wd = os.path.dirname(filename)
args = Settings().get_run_config_for_file(filename)
self.interactiveConsole.start_process(
| python | {
"resource": ""
} |
q260678 | MainWindow.on_goto_out_of_doc | validation | def on_goto_out_of_doc(self, assignment):
"""
Open the a new tab when goto goes out of the current document.
:param assignment: Destination
"""
| python | {
"resource": ""
} |
q260679 | calltips | validation | def calltips(request_data):
"""
Worker that returns a list of calltips.
A calltips is a tuple made of the following parts:
- module_name: name of the module of the function invoked
- call_name: name of the function that is being called
- params: the list of parameter names.
- index: index of the current parameter
- bracket_start
:returns tuple(module_name, call_name, params)
"""
code = request_data['code']
line = request_data['line'] + 1
column = request_data['column']
path = request_data['path']
# encoding = request_data['encoding']
encoding = 'utf-8'
# use jedi to get call signatures
script = jedi.Script(code, line, column, path, encoding)
signatures = script.call_signatures()
for sig | python | {
"resource": ""
} |
q260680 | goto_assignments | validation | def goto_assignments(request_data):
"""
Go to assignements worker.
"""
code = request_data['code']
line = request_data['line'] + 1
column = request_data['column']
path = request_data['path']
# encoding = request_data['encoding']
encoding = 'utf-8'
script = jedi.Script(code, line, column, path, encoding)
try:
definitions = script.goto_assignments()
| python | {
"resource": ""
} |
q260681 | defined_names | validation | def defined_names(request_data):
"""
Returns the list of defined names for the document.
"""
global _old_definitions
ret_val = []
path = request_data['path']
toplvl_definitions = jedi.names( | python | {
"resource": ""
} |
q260682 | quick_doc | validation | def quick_doc(request_data):
"""
Worker that returns the documentation of the symbol under cursor.
"""
code = request_data['code']
line = request_data['line'] + 1
column = request_data['column']
path = request_data['path']
# encoding = 'utf-8'
encoding = 'utf-8'
script = jedi.Script(code, line, column, path, encoding)
try:
| python | {
"resource": ""
} |
q260683 | run_pep8 | validation | def run_pep8(request_data):
"""
Worker that run the pep8 tool on the current editor text.
:returns a list of tuples (msg, msg_type, line_number)
"""
import pycodestyle
from pyqode.python.backend.pep8utils import CustomChecker
WARNING = 1
code = request_data['code']
path = request_data['path']
max_line_length = request_data['max_line_length']
ignore_rules = request_data['ignore_rules']
ignore_rules += ['W291', 'W292', 'W293', 'W391']
pycodestyle.MAX_LINE_LENGTH = max_line_length
# setup our custom style guide with our custom checker which returns a list
# of strings instread of spitting the results at stdout
pep8style = pycodestyle.StyleGuide(parse_argv=False, config_file='',
| python | {
"resource": ""
} |
q260684 | icon_from_typename | validation | def icon_from_typename(name, icon_type):
"""
Returns the icon resource filename that corresponds to the given typename.
:param name: name of the completion. Use to make the distinction between
public and private completions (using the count of starting '_')
:pram typename: the typename reported by jedi
:returns: The associate icon resource filename or None.
"""
ICONS = {
'CLASS': ICON_CLASS,
'IMPORT': ICON_NAMESPACE,
'STATEMENT': ICON_VAR,
'FORFLOW': ICON_VAR,
'FORSTMT': ICON_VAR,
'WITHSTMT': ICON_VAR,
'GLOBALSTMT': ICON_VAR,
'MODULE': ICON_NAMESPACE,
'KEYWORD': ICON_KEYWORD,
'PARAM': ICON_VAR,
'ARRAY': ICON_VAR,
'INSTANCEELEMENT': ICON_VAR,
'INSTANCE': ICON_VAR,
'PARAM-PRIV': ICON_VAR,
'PARAM-PROT': ICON_VAR,
'FUNCTION': ICON_FUNC,
'DEF': ICON_FUNC,
'FUNCTION-PRIV': ICON_FUNC_PRIVATE,
'FUNCTION-PROT': ICON_FUNC_PROTECTED
}
ret_val = None
icon_type = icon_type.upper()
# jedi 0.8 introduced NamedPart class, which have a string instead of being
# one
| python | {
"resource": ""
} |
q260685 | JediCompletionProvider.complete | validation | def complete(code, line, column, path, encoding, prefix):
"""
Completes python code using `jedi`_.
:returns: a list of completion.
"""
ret_val = []
try:
script = jedi.Script(code, line + 1, column, path, encoding)
completions = script.completions()
| python | {
"resource": ""
} |
q260686 | make_python_patterns | validation | def make_python_patterns(additional_keywords=[], additional_builtins=[]):
"""Strongly inspired from idlelib.ColorDelegator.make_pat"""
kw = r"\b" + any("keyword", kwlist + additional_keywords) + r"\b"
kw_namespace = r"\b" + any("namespace", kw_namespace_list) + r"\b"
word_operators = r"\b" + any("operator_word", wordop_list) + r"\b"
builtinlist = [str(name) for name in dir(builtins)
if not name.startswith('_')] + additional_builtins
for v in ['None', 'True', 'False']:
builtinlist.remove(v)
builtin = r"([^.'\"\\#]\b|^)" + any("builtin", builtinlist) + r"\b"
builtin_fct = any("builtin_fct", [r'_{2}[a-zA-Z_]*_{2}'])
comment = any("comment", [r"#[^\n]*"])
instance = any("instance", [r"\bself\b", r"\bcls\b"])
decorator = any('decorator', [r'@\w*', r'.setter'])
number = any("number",
[r"\b[+-]?[0-9]+[lLjJ]?\b",
r"\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b",
r"\b[+-]?0[oO][0-7]+[lL]?\b",
r"\b[+-]?0[bB][01]+[lL]?\b",
| python | {
"resource": ""
} |
q260687 | GoToAssignmentsMode._check_word_cursor | validation | def _check_word_cursor(self, tc=None):
"""
Request a go to assignment.
:param tc: Text cursor which contains the text that we must look for
its assignment. Can be None to go to the text that is under
the text cursor.
:type tc: QtGui.QTextCursor
"""
if not tc:
tc = TextHelper(self.editor).word_under_cursor()
request_data = {
'code': self.editor.toPlainText(),
'line': tc.blockNumber(),
'column': tc.columnNumber(),
| python | {
"resource": ""
} |
q260688 | GoToAssignmentsMode._unique | validation | def _unique(self, seq):
"""
Not performant but works.
"""
# order preserving
checked = []
for e in seq:
| python | {
"resource": ""
} |
q260689 | read_bgen | validation | def read_bgen(filepath, metafile_filepath=None, samples_filepath=None, verbose=True):
r""" Read a given BGEN file.
Parameters
----------
filepath : str
A bgen file path.
metafile_filepath : str, optional
If ``None``, it will try to read the ``filepath + ".metadata"`` file. If this is
not possible, it will create one. It tries to create one at
``filepath + ".metadata"``. If that is also no possible, it tries to create one
at a temporary folder.
samples_filepath : str, optional
A sample file in `gen format <https://goo.gl/bCzo7m>`_.
If ``samples_filepath`` is provided, sample ids are read from this file.
Otherwise, it reads from the bgen file itself if possible. Defaults to ``None``.
verbose : bool, optional
``True`` to show progress; ``False`` otherwise. Defaults to ``True``.
Returns
-------
variants : :class:`dask.dataFrame.DataFrame`
Variant position, chromosomes, rsids, etc.
samples : :class:`pandas.Series`
Sample identifications.
genotype : list
List of genotypes.
Examples
--------
.. doctest::
>>> from bgen_reader import example_files, read_bgen
>>>
>>> with example_files("haplotypes.bgen") as filepath:
... bgen = read_bgen(filepath, verbose=False)
... variants = bgen["variants"]
... samples = bgen["samples"]
...
... v = variants.loc[0].compute()
... g = bgen["genotype"][0].compute()
... print(v)
... print(samples)
... print(g["probs"][0])
id rsid chrom pos nalleles allele_ids vaddr
0 SNP1 RS1 1 1 2 A,G 102
0 sample_0
1 | python | {
"resource": ""
} |
q260690 | create_metafile | validation | def create_metafile(bgen_filepath, metafile_filepath, verbose=True):
r"""Create variants metadata file.
Variants metadata file helps speed up subsequent reads of the associated
bgen file.
Parameters
----------
bgen_filepath : str
Bgen file path.
metafile_file : str
Metafile file path.
verbose : bool
``True`` to show progress; ``False`` otherwise.
Examples
--------
.. doctest::
>>> import os
>>> from bgen_reader import create_metafile, example_files
>>>
>>> with example_files("example.32bits.bgen") as filepath:
... folder = os.path.dirname(filepath)
... metafile_filepath = os.path.join(folder, filepath + ".metadata")
...
... try:
... create_metafile(filepath, metafile_filepath, verbose=False)
... finally:
... if os.path.exists(metafile_filepath):
... os.remove(metafile_filepath)
"""
if verbose:
verbose = 1
else:
verbose = 0
bgen_filepath = make_sure_bytes(bgen_filepath) | python | {
"resource": ""
} |
q260691 | CheckLiteral.match | validation | def match(self, subsetLines, offsetOfSubset, fileName):
"""
Search through lines for match.
Raise an Exception if fail to match
If match is succesful return the position the match was found
"""
for (offset,l) in enumerate(subsetLines):
column = l.find(self.literal)
if column != -1:
truePosition = offset + offsetOfSubset
_logger.debug('Found match on line {}, col {}'.format(str(truePosition+ 1), column))
| python | {
"resource": ""
} |
q260692 | CheckNot.match | validation | def match(self, subsetLines, offsetOfSubset, fileName):
"""
Search through lines for match.
Raise an Exception if a match
"""
for (offset,l) in enumerate(subsetLines):
for t in self.regex:
m = t.Regex.search(l)
if m != None:
truePosition = offset + offsetOfSubset
_logger.debug('Found match on line {}'.format(str(truePosition+ 1)))
| python | {
"resource": ""
} |
q260693 | isA | validation | def isA(instance, typeList):
"""
Return true if ``instance`` is an instance of any the Directive
types in ``typeList``
| python | {
"resource": ""
} |
q260694 | _touch | validation | def _touch(fname, mode=0o666, dir_fd=None, **kwargs):
""" Touch a file.
Credits to <https://stackoverflow.com/a/1160227>.
"""
flags = os.O_CREAT | os.O_APPEND
| python | {
"resource": ""
} |
q260695 | allele_frequency | validation | def allele_frequency(expec):
r""" Compute allele frequency from its expectation.
Parameters
----------
expec : array_like
Allele expectations encoded as a samples-by-alleles matrix.
Returns
-------
:class:`numpy.ndarray`
Allele frequencies encoded as a variants-by-alleles matrix.
Examples
--------
.. doctest::
>>> from bgen_reader import read_bgen, example_files
>>> from bgen_reader import allele_expectation, allele_frequency
>>>
>>> # Download an example
>>> example = example_files("example.32bits.bgen")
>>> filepath = example.filepath
>>>
>>> bgen = read_bgen(filepath, verbose=False)
>>>
>>> variants = bgen["variants"]
>>> samples = bgen["samples"]
>>> genotype = bgen["genotype"]
>>>
>>> variant = variants[variants["rsid"] == "RSID_6"].compute()
>>> variant_idx = variant.index.item()
>>>
>>> p = genotype[variant_idx].compute()["probs"]
>>> # For unphased genotypes only.
>>> e = allele_expectation(bgen, variant_idx)
>>> f = allele_frequency(e)
>>>
>>> alleles = variant["allele_ids"].item().split(",")
| python | {
"resource": ""
} |
q260696 | compute_dosage | validation | def compute_dosage(expec, alt=None):
r""" Compute dosage from allele expectation.
Parameters
----------
expec : array_like
Allele expectations encoded as a samples-by-alleles matrix.
alt : array_like, optional
Alternative allele index. If ``None``, the allele having the minor
allele frequency for the provided ``expec`` is used as the alternative.
Defaults to ``None``.
Returns
-------
:class:`numpy.ndarray`
Dosage encoded as an array of size equal to the number of samples.
Examples
--------
.. code-block:: python
:caption: First a quick-start example.
>>> from bgen_reader import allele_expectation, compute_dosage
>>> from bgen_reader import example_files, read_bgen
>>>
>>> # Download an example.
>>> example = example_files("example.32bits.bgen")
>>> filepath = example.filepath
>>>
>>> # Read the example.
>>> bgen = read_bgen(filepath, verbose=False)
>>>
>>> # Extract the allele expectations of the fourth variant.
>>> variant_idx = 3
>>> e = allele_expectation(bgen, variant_idx)
>>>
>>> # Compute the dosage when considering the first allele
>>> # as the reference/alternative one.
>>> alt_allele_idx = 1
>>> d = compute_dosage(e, alt=alt_allele_idx)
>>>
>>> # Print the dosage of the first five samples only.
>>> print(d[:5])
[1.96185308 0.00982666 0.01745552 1.00347899 1.01153563]
>>>
>>> # Clean-up the example
>>> example.close()
.. code-block:: python
:caption: Genotype probabilities, allele expectations and frequencies.
>>> from bgen_reader import (
... allele_expectation,
... allele_frequency,
... compute_dosage,
... example_files,
... read_bgen,
... )
>>> from pandas import DataFrame
>>> from xarray import DataArray
>>>
>>> # Download an example
>>> example = example_files("example.32bits.bgen")
>>> filepath = example.filepath
>>>
>>> # Open the bgen file.
>>> bgen = read_bgen(filepath, verbose=False)
>>> variants = bgen["variants"]
>>> genotype = bgen["genotype"]
>>> samples = bgen["samples"]
>>>
>>> variant_idx = 3
>>> variant = variants.loc[variant_idx].compute()
>>> # Print the metadata of the fourth variant.
>>> print(variant)
id rsid chrom pos nalleles allele_ids vaddr
3 SNPID_5 RSID_5 01 5000 2 A,G 16034
>>> geno = bgen["genotype"][variant_idx].compute()
>>> metageno = DataFrame({k: geno[k] for k in ["ploidy", "missing"]},
... index=samples)
>>> metageno.index.name = "sample"
>>> print(metageno) # doctest: +IGNORE_EXCEPTION_DETAIL, +NORMALIZE_WHITESPACE
ploidy missing
sample
sample_001 2 False
sample_002 2 False
sample_003 2 False
sample_004 2 False
... ... ...
sample_497 2 False
sample_498 2 False
sample_499 2 False
sample_500 2 False
<BLANKLINE>
[500 rows x 2 columns]
>>> p = DataArray(
... geno["probs"],
... name="probability",
... coords={"sample": samples},
... dims=["sample", "genotype"],
... )
>>> # Print the genotype probabilities.
>>> print(p.to_series().unstack(level=-1)) # doctest: +IGNORE_EXCEPTION_DETAIL, +NORMALIZE_WHITESPACE
genotype 0 1 2
sample
sample_001 0.00488 0.02838 0.96674
sample_002 0.99045 0.00928 0.00027
sample_003 0.98932 0.00391 0.00677
sample_004 0.00662 0.98328 0.01010
... | python | {
"resource": ""
} |
q260697 | allele_expectation | validation | def allele_expectation(bgen, variant_idx):
r""" Allele expectation.
Compute the expectation of each allele from the genotype probabilities.
Parameters
----------
bgen : bgen_file
Bgen file handler.
variant_idx : int
Variant index.
Returns
-------
:class:`numpy.ndarray`
Samples-by-alleles matrix of allele expectations.
Note
----
This function supports unphased genotypes only.
Examples
--------
.. doctest::
>>> from bgen_reader import allele_expectation, example_files, read_bgen
>>>
>>> from texttable import Texttable
>>>
>>> # Download an example.
>>> example = example_files("example.32bits.bgen")
>>> filepath = example.filepath
>>>
>>> # Read the example.
>>> bgen = read_bgen(filepath, verbose=False)
>>>
>>> variants = bgen["variants"]
>>> samples = bgen["samples"]
>>> genotype = bgen["genotype"]
>>>
>>> genotype = bgen["genotype"]
>>> # This `compute` call will return a pandas data frame,
>>> variant = variants[variants["rsid"] == "RSID_6"].compute()
>>> # from which we retrieve the variant index.
>>> variant_idx = variant.index.item()
>>> print(variant)
id rsid chrom pos nalleles allele_ids vaddr
4 SNPID_6 RSID_6 01 6000 2 A,G 19377
>>> genotype = bgen["genotype"]
>>> # Samples is a pandas series, and we retrieve the
>>> # sample index from the sample name.
>>> sample_idx = samples[samples == "sample_005"].index.item()
>>>
>>> genotype = bgen["genotype"]
>>> # This `compute` call will return a dictionary from which
>>> # we can get the probability matrix the corresponding
>>> # variant.
>>> p = genotype[variant_idx].compute()["probs"][sample_idx]
>>>
>>> genotype = bgen["genotype"]
>>> # Allele expectation makes sense for unphased genotypes only,
>>> # which is the case here.
>>> e = allele_expectation(bgen, variant_idx)[sample_idx]
>>>
>>> genotype = bgen["genotype"]
>>> alleles = variant["allele_ids"].item().split(",")
>>>
>>> genotype = bgen["genotype"]
>>>
>>> # Print what we have got in a nice format.
>>> table = Texttable()
>>> table = table.add_rows(
| python | {
"resource": ""
} |
q260698 | Windows.find_libname | validation | def find_libname(self, name):
"""Try to infer the correct library name."""
names = ["{}.lib", "lib{}.lib", "{}lib.lib"]
names = [n.format(name) for n in names]
dirs = self.get_library_dirs()
for d in dirs:
| python | {
"resource": ""
} |
q260699 | SimilarityDistance.fit | validation | def fit(self, X, y=None):
"""Fit distance-based AD.
Parameters
----------
X : array-like or sparse matrix, shape (n_samples, n_features)
The input samples. Use ``dtype=np.float32`` for maximum
efficiency.
Returns
-------
self : object
Returns self.
"""
# Check data
X = check_array(X)
self.tree = BallTree(X, leaf_size=self.leaf_size, metric=self.metric)
dist_train = self.tree.query(X, k=2)[0]
if self.threshold == 'auto':
self.threshold_value = 0.5 * sqrt(var(dist_train[:, 1])) + mean(dist_train[:, 1])
elif self.threshold == 'cv':
if y is None:
raise ValueError("Y must be specified to find the optimal threshold.")
y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
self.threshold_value = 0
score = 0
Y_pred, Y_true, AD = [], [], []
cv = KFold(n_splits=5, random_state=1, shuffle=True)
for train_index, test_index in cv.split(X):
x_train = safe_indexing(X, train_index)
x_test = safe_indexing(X, test_index)
y_train = safe_indexing(y, train_index)
y_test = safe_indexing(y, test_index)
data_test = safe_indexing(dist_train[:, 1], | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.