repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1
value | code stringlengths 75 19.8k | code_tokens listlengths 20 707 | docstring stringlengths 3 17.3k | docstring_tokens listlengths 3 222 | sha stringlengths 40 40 | url stringlengths 87 242 | partition stringclasses 1
value | idx int64 0 252k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
openvax/pyensembl | pyensembl/download_cache.py | DownloadCache.delete_cached_files | def delete_cached_files(self, prefixes=[], suffixes=[]):
"""
Deletes any cached files matching the prefixes or suffixes given
"""
for filename in listdir(self.cache_directory_path):
delete = (
any([filename.endswith(ext) for ext in suffixes]) or
any([filename.startswith(pre) for pre in prefixes]))
if delete:
path = join(self.cache_directory_path, filename)
logger.info("Deleting %s", path)
remove(path) | python | def delete_cached_files(self, prefixes=[], suffixes=[]):
"""
Deletes any cached files matching the prefixes or suffixes given
"""
for filename in listdir(self.cache_directory_path):
delete = (
any([filename.endswith(ext) for ext in suffixes]) or
any([filename.startswith(pre) for pre in prefixes]))
if delete:
path = join(self.cache_directory_path, filename)
logger.info("Deleting %s", path)
remove(path) | [
"def",
"delete_cached_files",
"(",
"self",
",",
"prefixes",
"=",
"[",
"]",
",",
"suffixes",
"=",
"[",
"]",
")",
":",
"for",
"filename",
"in",
"listdir",
"(",
"self",
".",
"cache_directory_path",
")",
":",
"delete",
"=",
"(",
"any",
"(",
"[",
"filename"... | Deletes any cached files matching the prefixes or suffixes given | [
"Deletes",
"any",
"cached",
"files",
"matching",
"the",
"prefixes",
"or",
"suffixes",
"given"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/download_cache.py#L303-L314 | train | 199,400 |
openvax/pyensembl | pyensembl/genome.py | Genome.to_dict | def to_dict(self):
"""
Returns a dictionary of the essential fields of this Genome.
"""
return dict(
reference_name=self.reference_name,
annotation_name=self.annotation_name,
annotation_version=self.annotation_version,
gtf_path_or_url=self._gtf_path_or_url,
transcript_fasta_paths_or_urls=self._transcript_fasta_paths_or_urls,
protein_fasta_paths_or_urls=self._protein_fasta_paths_or_urls,
decompress_on_download=self.decompress_on_download,
copy_local_files_to_cache=self.copy_local_files_to_cache,
cache_directory_path=self.cache_directory_path) | python | def to_dict(self):
"""
Returns a dictionary of the essential fields of this Genome.
"""
return dict(
reference_name=self.reference_name,
annotation_name=self.annotation_name,
annotation_version=self.annotation_version,
gtf_path_or_url=self._gtf_path_or_url,
transcript_fasta_paths_or_urls=self._transcript_fasta_paths_or_urls,
protein_fasta_paths_or_urls=self._protein_fasta_paths_or_urls,
decompress_on_download=self.decompress_on_download,
copy_local_files_to_cache=self.copy_local_files_to_cache,
cache_directory_path=self.cache_directory_path) | [
"def",
"to_dict",
"(",
"self",
")",
":",
"return",
"dict",
"(",
"reference_name",
"=",
"self",
".",
"reference_name",
",",
"annotation_name",
"=",
"self",
".",
"annotation_name",
",",
"annotation_version",
"=",
"self",
".",
"annotation_version",
",",
"gtf_path_o... | Returns a dictionary of the essential fields of this Genome. | [
"Returns",
"a",
"dictionary",
"of",
"the",
"essential",
"fields",
"of",
"this",
"Genome",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L137-L150 | train | 199,401 |
openvax/pyensembl | pyensembl/genome.py | Genome._init_lazy_fields | def _init_lazy_fields(self):
"""
Member data that gets loaded or constructed on demand
"""
self.gtf_path = None
self._protein_sequences = None
self._transcript_sequences = None
self._db = None
self.protein_fasta_paths = None
self.transcript_fasta_paths = None
# only memoizing the Gene, Transcript, and Exon objects
self._genes = {}
self._transcripts = {}
self._exons = {} | python | def _init_lazy_fields(self):
"""
Member data that gets loaded or constructed on demand
"""
self.gtf_path = None
self._protein_sequences = None
self._transcript_sequences = None
self._db = None
self.protein_fasta_paths = None
self.transcript_fasta_paths = None
# only memoizing the Gene, Transcript, and Exon objects
self._genes = {}
self._transcripts = {}
self._exons = {} | [
"def",
"_init_lazy_fields",
"(",
"self",
")",
":",
"self",
".",
"gtf_path",
"=",
"None",
"self",
".",
"_protein_sequences",
"=",
"None",
"self",
".",
"_transcript_sequences",
"=",
"None",
"self",
".",
"_db",
"=",
"None",
"self",
".",
"protein_fasta_paths",
"... | Member data that gets loaded or constructed on demand | [
"Member",
"data",
"that",
"gets",
"loaded",
"or",
"constructed",
"on",
"demand"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L152-L166 | train | 199,402 |
openvax/pyensembl | pyensembl/genome.py | Genome._get_cached_path | def _get_cached_path(
self,
field_name,
path_or_url,
download_if_missing=False,
overwrite=False):
"""
Get the local path for a possibly remote file, invoking either
a download or install error message if it's missing.
"""
if len(field_name) == 0:
raise ValueError("Expected non-empty field name")
if len(path_or_url) == 0:
raise ValueError("Expected non-empty path_or_url")
return self.download_cache.local_path_or_install_error(
field_name=field_name,
path_or_url=path_or_url,
download_if_missing=download_if_missing,
overwrite=overwrite) | python | def _get_cached_path(
self,
field_name,
path_or_url,
download_if_missing=False,
overwrite=False):
"""
Get the local path for a possibly remote file, invoking either
a download or install error message if it's missing.
"""
if len(field_name) == 0:
raise ValueError("Expected non-empty field name")
if len(path_or_url) == 0:
raise ValueError("Expected non-empty path_or_url")
return self.download_cache.local_path_or_install_error(
field_name=field_name,
path_or_url=path_or_url,
download_if_missing=download_if_missing,
overwrite=overwrite) | [
"def",
"_get_cached_path",
"(",
"self",
",",
"field_name",
",",
"path_or_url",
",",
"download_if_missing",
"=",
"False",
",",
"overwrite",
"=",
"False",
")",
":",
"if",
"len",
"(",
"field_name",
")",
"==",
"0",
":",
"raise",
"ValueError",
"(",
"\"Expected no... | Get the local path for a possibly remote file, invoking either
a download or install error message if it's missing. | [
"Get",
"the",
"local",
"path",
"for",
"a",
"possibly",
"remote",
"file",
"invoking",
"either",
"a",
"download",
"or",
"install",
"error",
"message",
"if",
"it",
"s",
"missing",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L168-L186 | train | 199,403 |
openvax/pyensembl | pyensembl/genome.py | Genome.download | def download(self, overwrite=False):
"""
Download data files needed by this Genome instance.
Parameters
----------
overwrite : bool, optional
Download files regardless whether local copy already exists.
"""
self._set_local_paths(download_if_missing=True, overwrite=overwrite) | python | def download(self, overwrite=False):
"""
Download data files needed by this Genome instance.
Parameters
----------
overwrite : bool, optional
Download files regardless whether local copy already exists.
"""
self._set_local_paths(download_if_missing=True, overwrite=overwrite) | [
"def",
"download",
"(",
"self",
",",
"overwrite",
"=",
"False",
")",
":",
"self",
".",
"_set_local_paths",
"(",
"download_if_missing",
"=",
"True",
",",
"overwrite",
"=",
"overwrite",
")"
] | Download data files needed by this Genome instance.
Parameters
----------
overwrite : bool, optional
Download files regardless whether local copy already exists. | [
"Download",
"data",
"files",
"needed",
"by",
"this",
"Genome",
"instance",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L262-L271 | train | 199,404 |
openvax/pyensembl | pyensembl/genome.py | Genome.index | def index(self, overwrite=False):
"""
Assuming that all necessary data for this Genome has been downloaded,
generate the GTF database and save efficient representation of
FASTA sequence files.
"""
if self.requires_gtf:
self.db.connect_or_create(overwrite=overwrite)
if self.requires_transcript_fasta:
self.transcript_sequences.index(overwrite=overwrite)
if self.requires_protein_fasta:
self.protein_sequences.index(overwrite=overwrite) | python | def index(self, overwrite=False):
"""
Assuming that all necessary data for this Genome has been downloaded,
generate the GTF database and save efficient representation of
FASTA sequence files.
"""
if self.requires_gtf:
self.db.connect_or_create(overwrite=overwrite)
if self.requires_transcript_fasta:
self.transcript_sequences.index(overwrite=overwrite)
if self.requires_protein_fasta:
self.protein_sequences.index(overwrite=overwrite) | [
"def",
"index",
"(",
"self",
",",
"overwrite",
"=",
"False",
")",
":",
"if",
"self",
".",
"requires_gtf",
":",
"self",
".",
"db",
".",
"connect_or_create",
"(",
"overwrite",
"=",
"overwrite",
")",
"if",
"self",
".",
"requires_transcript_fasta",
":",
"self"... | Assuming that all necessary data for this Genome has been downloaded,
generate the GTF database and save efficient representation of
FASTA sequence files. | [
"Assuming",
"that",
"all",
"necessary",
"data",
"for",
"this",
"Genome",
"has",
"been",
"downloaded",
"generate",
"the",
"GTF",
"database",
"and",
"save",
"efficient",
"representation",
"of",
"FASTA",
"sequence",
"files",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L273-L284 | train | 199,405 |
openvax/pyensembl | pyensembl/genome.py | Genome.install_string | def install_string(self):
"""
Add every missing file to the install string shown to the user
in an error message.
"""
args = [
"--reference-name", self.reference_name,
"--annotation-name", self.annotation_name]
if self.annotation_version:
args.extend(["--annotation-version", str(self.annotation_version)])
if self.requires_gtf:
args.append("--gtf")
args.append("\"%s\"" % self._gtf_path_or_url)
if self.requires_protein_fasta:
args += [
"--protein-fasta \"%s\"" %
path for path in self._protein_fasta_paths_or_urls]
if self.requires_transcript_fasta:
args += [
"--transcript-fasta \"%s\"" %
path for path in self._transcript_fasta_paths_or_urls]
return "pyensembl install %s" % " ".join(args) | python | def install_string(self):
"""
Add every missing file to the install string shown to the user
in an error message.
"""
args = [
"--reference-name", self.reference_name,
"--annotation-name", self.annotation_name]
if self.annotation_version:
args.extend(["--annotation-version", str(self.annotation_version)])
if self.requires_gtf:
args.append("--gtf")
args.append("\"%s\"" % self._gtf_path_or_url)
if self.requires_protein_fasta:
args += [
"--protein-fasta \"%s\"" %
path for path in self._protein_fasta_paths_or_urls]
if self.requires_transcript_fasta:
args += [
"--transcript-fasta \"%s\"" %
path for path in self._transcript_fasta_paths_or_urls]
return "pyensembl install %s" % " ".join(args) | [
"def",
"install_string",
"(",
"self",
")",
":",
"args",
"=",
"[",
"\"--reference-name\"",
",",
"self",
".",
"reference_name",
",",
"\"--annotation-name\"",
",",
"self",
".",
"annotation_name",
"]",
"if",
"self",
".",
"annotation_version",
":",
"args",
".",
"ex... | Add every missing file to the install string shown to the user
in an error message. | [
"Add",
"every",
"missing",
"file",
"to",
"the",
"install",
"string",
"shown",
"to",
"the",
"user",
"in",
"an",
"error",
"message",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L366-L387 | train | 199,406 |
openvax/pyensembl | pyensembl/genome.py | Genome.clear_cache | def clear_cache(self):
"""
Clear any in-memory cached values and short-lived on-disk
materializations from MemoryCache
"""
for maybe_fn in self.__dict__.values():
# clear cache associated with all memoization decorators,
# GTF and SequenceData objects
if hasattr(maybe_fn, "clear_cache"):
maybe_fn.clear_cache() | python | def clear_cache(self):
"""
Clear any in-memory cached values and short-lived on-disk
materializations from MemoryCache
"""
for maybe_fn in self.__dict__.values():
# clear cache associated with all memoization decorators,
# GTF and SequenceData objects
if hasattr(maybe_fn, "clear_cache"):
maybe_fn.clear_cache() | [
"def",
"clear_cache",
"(",
"self",
")",
":",
"for",
"maybe_fn",
"in",
"self",
".",
"__dict__",
".",
"values",
"(",
")",
":",
"# clear cache associated with all memoization decorators,",
"# GTF and SequenceData objects",
"if",
"hasattr",
"(",
"maybe_fn",
",",
"\"clear_... | Clear any in-memory cached values and short-lived on-disk
materializations from MemoryCache | [
"Clear",
"any",
"in",
"-",
"memory",
"cached",
"values",
"and",
"short",
"-",
"lived",
"on",
"-",
"disk",
"materializations",
"from",
"MemoryCache"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L434-L443 | train | 199,407 |
openvax/pyensembl | pyensembl/genome.py | Genome.delete_index_files | def delete_index_files(self):
"""
Delete all data aside from source GTF and FASTA files
"""
self.clear_cache()
db_path = self.db.local_db_path()
if exists(db_path):
remove(db_path) | python | def delete_index_files(self):
"""
Delete all data aside from source GTF and FASTA files
"""
self.clear_cache()
db_path = self.db.local_db_path()
if exists(db_path):
remove(db_path) | [
"def",
"delete_index_files",
"(",
"self",
")",
":",
"self",
".",
"clear_cache",
"(",
")",
"db_path",
"=",
"self",
".",
"db",
".",
"local_db_path",
"(",
")",
"if",
"exists",
"(",
"db_path",
")",
":",
"remove",
"(",
"db_path",
")"
] | Delete all data aside from source GTF and FASTA files | [
"Delete",
"all",
"data",
"aside",
"from",
"source",
"GTF",
"and",
"FASTA",
"files"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L445-L452 | train | 199,408 |
openvax/pyensembl | pyensembl/genome.py | Genome._all_feature_values | def _all_feature_values(
self,
column,
feature,
distinct=True,
contig=None,
strand=None):
"""
Cached lookup of all values for a particular feature property from
the database, caches repeated queries in memory and
stores them as a CSV.
Parameters
----------
column : str
Name of property (e.g. exon_id)
feature : str
Type of entry (e.g. exon)
distinct : bool, optional
Keep only unique values
contig : str, optional
Restrict query to particular contig
strand : str, optional
Restrict results to "+" or "-" strands
Returns a list constructed from query results.
"""
return self.db.query_feature_values(
column=column,
feature=feature,
distinct=distinct,
contig=contig,
strand=strand) | python | def _all_feature_values(
self,
column,
feature,
distinct=True,
contig=None,
strand=None):
"""
Cached lookup of all values for a particular feature property from
the database, caches repeated queries in memory and
stores them as a CSV.
Parameters
----------
column : str
Name of property (e.g. exon_id)
feature : str
Type of entry (e.g. exon)
distinct : bool, optional
Keep only unique values
contig : str, optional
Restrict query to particular contig
strand : str, optional
Restrict results to "+" or "-" strands
Returns a list constructed from query results.
"""
return self.db.query_feature_values(
column=column,
feature=feature,
distinct=distinct,
contig=contig,
strand=strand) | [
"def",
"_all_feature_values",
"(",
"self",
",",
"column",
",",
"feature",
",",
"distinct",
"=",
"True",
",",
"contig",
"=",
"None",
",",
"strand",
"=",
"None",
")",
":",
"return",
"self",
".",
"db",
".",
"query_feature_values",
"(",
"column",
"=",
"colum... | Cached lookup of all values for a particular feature property from
the database, caches repeated queries in memory and
stores them as a CSV.
Parameters
----------
column : str
Name of property (e.g. exon_id)
feature : str
Type of entry (e.g. exon)
distinct : bool, optional
Keep only unique values
contig : str, optional
Restrict query to particular contig
strand : str, optional
Restrict results to "+" or "-" strands
Returns a list constructed from query results. | [
"Cached",
"lookup",
"of",
"all",
"values",
"for",
"a",
"particular",
"feature",
"property",
"from",
"the",
"database",
"caches",
"repeated",
"queries",
"in",
"memory",
"and",
"stores",
"them",
"as",
"a",
"CSV",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L454-L491 | train | 199,409 |
openvax/pyensembl | pyensembl/genome.py | Genome.gene_by_id | def gene_by_id(self, gene_id):
"""
Construct a Gene object for the given gene ID.
"""
if gene_id not in self._genes:
field_names = [
"seqname",
"start",
"end",
"strand",
]
optional_field_names = [
"gene_name",
"gene_biotype",
]
# Do not look for gene_name and gene_biotype if they are
# not in the database.
field_names.extend([
name for name in optional_field_names
if self.db.column_exists("gene", name)
])
result = self.db.query_one(
field_names,
filter_column="gene_id",
filter_value=gene_id,
feature="gene")
if not result:
raise ValueError("Gene not found: %s" % (gene_id,))
gene_name, gene_biotype = None, None
assert len(result) >= 4 and len(result) <= 6, \
"Result is not the expected length: %d" % len(result)
contig, start, end, strand = result[:4]
if len(result) == 5:
if "gene_name" in field_names:
gene_name = result[4]
else:
gene_biotype = result[4]
elif len(result) == 6:
gene_name, gene_biotype = result[4:]
self._genes[gene_id] = Gene(
gene_id=gene_id,
gene_name=gene_name,
contig=contig,
start=start,
end=end,
strand=strand,
biotype=gene_biotype,
genome=self)
return self._genes[gene_id] | python | def gene_by_id(self, gene_id):
"""
Construct a Gene object for the given gene ID.
"""
if gene_id not in self._genes:
field_names = [
"seqname",
"start",
"end",
"strand",
]
optional_field_names = [
"gene_name",
"gene_biotype",
]
# Do not look for gene_name and gene_biotype if they are
# not in the database.
field_names.extend([
name for name in optional_field_names
if self.db.column_exists("gene", name)
])
result = self.db.query_one(
field_names,
filter_column="gene_id",
filter_value=gene_id,
feature="gene")
if not result:
raise ValueError("Gene not found: %s" % (gene_id,))
gene_name, gene_biotype = None, None
assert len(result) >= 4 and len(result) <= 6, \
"Result is not the expected length: %d" % len(result)
contig, start, end, strand = result[:4]
if len(result) == 5:
if "gene_name" in field_names:
gene_name = result[4]
else:
gene_biotype = result[4]
elif len(result) == 6:
gene_name, gene_biotype = result[4:]
self._genes[gene_id] = Gene(
gene_id=gene_id,
gene_name=gene_name,
contig=contig,
start=start,
end=end,
strand=strand,
biotype=gene_biotype,
genome=self)
return self._genes[gene_id] | [
"def",
"gene_by_id",
"(",
"self",
",",
"gene_id",
")",
":",
"if",
"gene_id",
"not",
"in",
"self",
".",
"_genes",
":",
"field_names",
"=",
"[",
"\"seqname\"",
",",
"\"start\"",
",",
"\"end\"",
",",
"\"strand\"",
",",
"]",
"optional_field_names",
"=",
"[",
... | Construct a Gene object for the given gene ID. | [
"Construct",
"a",
"Gene",
"object",
"for",
"the",
"given",
"gene",
"ID",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L660-L711 | train | 199,410 |
openvax/pyensembl | pyensembl/genome.py | Genome.gene_by_protein_id | def gene_by_protein_id(self, protein_id):
"""
Get the gene ID associated with the given protein ID,
return its Gene object
"""
gene_id = self.gene_id_of_protein_id(protein_id)
return self.gene_by_id(gene_id) | python | def gene_by_protein_id(self, protein_id):
"""
Get the gene ID associated with the given protein ID,
return its Gene object
"""
gene_id = self.gene_id_of_protein_id(protein_id)
return self.gene_by_id(gene_id) | [
"def",
"gene_by_protein_id",
"(",
"self",
",",
"protein_id",
")",
":",
"gene_id",
"=",
"self",
".",
"gene_id_of_protein_id",
"(",
"protein_id",
")",
"return",
"self",
".",
"gene_by_id",
"(",
"gene_id",
")"
] | Get the gene ID associated with the given protein ID,
return its Gene object | [
"Get",
"the",
"gene",
"ID",
"associated",
"with",
"the",
"given",
"protein",
"ID",
"return",
"its",
"Gene",
"object"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L722-L728 | train | 199,411 |
openvax/pyensembl | pyensembl/genome.py | Genome.gene_id_of_protein_id | def gene_id_of_protein_id(self, protein_id):
"""
What is the gene ID associated with a given protein ID?
"""
results = self._query_gene_ids(
"protein_id",
protein_id,
feature="CDS")
if len(results) == 0:
raise ValueError("Protein ID not found: %s" % protein_id)
assert len(results) == 1, \
("Should have only one gene ID for a given protein ID, "
"but found %d: %s" % (len(results), results))
return results[0] | python | def gene_id_of_protein_id(self, protein_id):
"""
What is the gene ID associated with a given protein ID?
"""
results = self._query_gene_ids(
"protein_id",
protein_id,
feature="CDS")
if len(results) == 0:
raise ValueError("Protein ID not found: %s" % protein_id)
assert len(results) == 1, \
("Should have only one gene ID for a given protein ID, "
"but found %d: %s" % (len(results), results))
return results[0] | [
"def",
"gene_id_of_protein_id",
"(",
"self",
",",
"protein_id",
")",
":",
"results",
"=",
"self",
".",
"_query_gene_ids",
"(",
"\"protein_id\"",
",",
"protein_id",
",",
"feature",
"=",
"\"CDS\"",
")",
"if",
"len",
"(",
"results",
")",
"==",
"0",
":",
"rais... | What is the gene ID associated with a given protein ID? | [
"What",
"is",
"the",
"gene",
"ID",
"associated",
"with",
"a",
"given",
"protein",
"ID?"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L808-L821 | train | 199,412 |
openvax/pyensembl | pyensembl/genome.py | Genome.transcripts | def transcripts(self, contig=None, strand=None):
"""
Construct Transcript object for every transcript entry in
the database. Optionally restrict to a particular
chromosome using the `contig` argument.
"""
transcript_ids = self.transcript_ids(contig=contig, strand=strand)
return [
self.transcript_by_id(transcript_id)
for transcript_id in transcript_ids
] | python | def transcripts(self, contig=None, strand=None):
"""
Construct Transcript object for every transcript entry in
the database. Optionally restrict to a particular
chromosome using the `contig` argument.
"""
transcript_ids = self.transcript_ids(contig=contig, strand=strand)
return [
self.transcript_by_id(transcript_id)
for transcript_id in transcript_ids
] | [
"def",
"transcripts",
"(",
"self",
",",
"contig",
"=",
"None",
",",
"strand",
"=",
"None",
")",
":",
"transcript_ids",
"=",
"self",
".",
"transcript_ids",
"(",
"contig",
"=",
"contig",
",",
"strand",
"=",
"strand",
")",
"return",
"[",
"self",
".",
"tra... | Construct Transcript object for every transcript entry in
the database. Optionally restrict to a particular
chromosome using the `contig` argument. | [
"Construct",
"Transcript",
"object",
"for",
"every",
"transcript",
"entry",
"in",
"the",
"database",
".",
"Optionally",
"restrict",
"to",
"a",
"particular",
"chromosome",
"using",
"the",
"contig",
"argument",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L829-L839 | train | 199,413 |
openvax/pyensembl | pyensembl/genome.py | Genome.transcript_by_id | def transcript_by_id(self, transcript_id):
"""Construct Transcript object with given transcript ID"""
if transcript_id not in self._transcripts:
optional_field_names = [
"transcript_name",
"transcript_biotype",
"transcript_support_level",
]
field_names = [
"seqname",
"start",
"end",
"strand",
"gene_id",
]
# Do not look for the optional fields if they are not in the database.
field_names.extend([
name for name in optional_field_names
if self.db.column_exists("transcript", name)
])
result = self.db.query_one(
select_column_names=field_names,
filter_column="transcript_id",
filter_value=transcript_id,
feature="transcript",
distinct=True)
if not result:
raise ValueError("Transcript not found: %s" % (transcript_id,))
transcript_name, transcript_biotype, tsl = None, None, None
assert 5 <= len(result) <= 5 + len(optional_field_names), \
"Result is not the expected length: %d" % len(result)
contig, start, end, strand, gene_id = result[:5]
if len(result) > 5:
extra_field_names = [f for f in optional_field_names if f in field_names]
extra_data = dict(zip(extra_field_names, result[5:]))
transcript_name = extra_data.get("transcript_name")
transcript_biotype = extra_data.get("transcript_biotype")
tsl = extra_data.get("transcript_support_level")
if not tsl or tsl == 'NA':
tsl = None
else:
tsl = int(tsl)
self._transcripts[transcript_id] = Transcript(
transcript_id=transcript_id,
transcript_name=transcript_name,
contig=contig,
start=start,
end=end,
strand=strand,
biotype=transcript_biotype,
gene_id=gene_id,
genome=self,
support_level=tsl)
return self._transcripts[transcript_id] | python | def transcript_by_id(self, transcript_id):
"""Construct Transcript object with given transcript ID"""
if transcript_id not in self._transcripts:
optional_field_names = [
"transcript_name",
"transcript_biotype",
"transcript_support_level",
]
field_names = [
"seqname",
"start",
"end",
"strand",
"gene_id",
]
# Do not look for the optional fields if they are not in the database.
field_names.extend([
name for name in optional_field_names
if self.db.column_exists("transcript", name)
])
result = self.db.query_one(
select_column_names=field_names,
filter_column="transcript_id",
filter_value=transcript_id,
feature="transcript",
distinct=True)
if not result:
raise ValueError("Transcript not found: %s" % (transcript_id,))
transcript_name, transcript_biotype, tsl = None, None, None
assert 5 <= len(result) <= 5 + len(optional_field_names), \
"Result is not the expected length: %d" % len(result)
contig, start, end, strand, gene_id = result[:5]
if len(result) > 5:
extra_field_names = [f for f in optional_field_names if f in field_names]
extra_data = dict(zip(extra_field_names, result[5:]))
transcript_name = extra_data.get("transcript_name")
transcript_biotype = extra_data.get("transcript_biotype")
tsl = extra_data.get("transcript_support_level")
if not tsl or tsl == 'NA':
tsl = None
else:
tsl = int(tsl)
self._transcripts[transcript_id] = Transcript(
transcript_id=transcript_id,
transcript_name=transcript_name,
contig=contig,
start=start,
end=end,
strand=strand,
biotype=transcript_biotype,
gene_id=gene_id,
genome=self,
support_level=tsl)
return self._transcripts[transcript_id] | [
"def",
"transcript_by_id",
"(",
"self",
",",
"transcript_id",
")",
":",
"if",
"transcript_id",
"not",
"in",
"self",
".",
"_transcripts",
":",
"optional_field_names",
"=",
"[",
"\"transcript_name\"",
",",
"\"transcript_biotype\"",
",",
"\"transcript_support_level\"",
"... | Construct Transcript object with given transcript ID | [
"Construct",
"Transcript",
"object",
"with",
"given",
"transcript",
"ID"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L841-L897 | train | 199,414 |
openvax/pyensembl | pyensembl/genome.py | Genome.transcript_id_of_protein_id | def transcript_id_of_protein_id(self, protein_id):
"""
What is the transcript ID associated with a given protein ID?
"""
results = self._query_transcript_ids(
"protein_id",
protein_id,
feature="CDS")
if len(results) == 0:
raise ValueError("Protein ID not found: %s" % protein_id)
assert len(results) == 1, \
("Should have only one transcript ID for a given protein ID, "
"but found %d: %s" % (len(results), results))
return results[0] | python | def transcript_id_of_protein_id(self, protein_id):
"""
What is the transcript ID associated with a given protein ID?
"""
results = self._query_transcript_ids(
"protein_id",
protein_id,
feature="CDS")
if len(results) == 0:
raise ValueError("Protein ID not found: %s" % protein_id)
assert len(results) == 1, \
("Should have only one transcript ID for a given protein ID, "
"but found %d: %s" % (len(results), results))
return results[0] | [
"def",
"transcript_id_of_protein_id",
"(",
"self",
",",
"protein_id",
")",
":",
"results",
"=",
"self",
".",
"_query_transcript_ids",
"(",
"\"protein_id\"",
",",
"protein_id",
",",
"feature",
"=",
"\"CDS\"",
")",
"if",
"len",
"(",
"results",
")",
"==",
"0",
... | What is the transcript ID associated with a given protein ID? | [
"What",
"is",
"the",
"transcript",
"ID",
"associated",
"with",
"a",
"given",
"protein",
"ID?"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L989-L1002 | train | 199,415 |
openvax/pyensembl | pyensembl/genome.py | Genome.exons | def exons(self, contig=None, strand=None):
"""
Create exon object for all exons in the database, optionally
restrict to a particular chromosome using the `contig` argument.
"""
# DataFrame with single column called "exon_id"
exon_ids = self.exon_ids(contig=contig, strand=strand)
return [
self.exon_by_id(exon_id)
for exon_id in exon_ids
] | python | def exons(self, contig=None, strand=None):
"""
Create exon object for all exons in the database, optionally
restrict to a particular chromosome using the `contig` argument.
"""
# DataFrame with single column called "exon_id"
exon_ids = self.exon_ids(contig=contig, strand=strand)
return [
self.exon_by_id(exon_id)
for exon_id in exon_ids
] | [
"def",
"exons",
"(",
"self",
",",
"contig",
"=",
"None",
",",
"strand",
"=",
"None",
")",
":",
"# DataFrame with single column called \"exon_id\"",
"exon_ids",
"=",
"self",
".",
"exon_ids",
"(",
"contig",
"=",
"contig",
",",
"strand",
"=",
"strand",
")",
"re... | Create exon object for all exons in the database, optionally
restrict to a particular chromosome using the `contig` argument. | [
"Create",
"exon",
"object",
"for",
"all",
"exons",
"in",
"the",
"database",
"optionally",
"restrict",
"to",
"a",
"particular",
"chromosome",
"using",
"the",
"contig",
"argument",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L1010-L1020 | train | 199,416 |
openvax/pyensembl | pyensembl/genome.py | Genome.exon_by_id | def exon_by_id(self, exon_id):
"""Construct an Exon object from its ID by looking up the exon"s
properties in the given Database.
"""
if exon_id not in self._exons:
field_names = [
"seqname",
"start",
"end",
"strand",
"gene_name",
"gene_id",
]
contig, start, end, strand, gene_name, gene_id = self.db.query_one(
select_column_names=field_names,
filter_column="exon_id",
filter_value=exon_id,
feature="exon",
distinct=True)
self._exons[exon_id] = Exon(
exon_id=exon_id,
contig=contig,
start=start,
end=end,
strand=strand,
gene_name=gene_name,
gene_id=gene_id)
return self._exons[exon_id] | python | def exon_by_id(self, exon_id):
"""Construct an Exon object from its ID by looking up the exon"s
properties in the given Database.
"""
if exon_id not in self._exons:
field_names = [
"seqname",
"start",
"end",
"strand",
"gene_name",
"gene_id",
]
contig, start, end, strand, gene_name, gene_id = self.db.query_one(
select_column_names=field_names,
filter_column="exon_id",
filter_value=exon_id,
feature="exon",
distinct=True)
self._exons[exon_id] = Exon(
exon_id=exon_id,
contig=contig,
start=start,
end=end,
strand=strand,
gene_name=gene_name,
gene_id=gene_id)
return self._exons[exon_id] | [
"def",
"exon_by_id",
"(",
"self",
",",
"exon_id",
")",
":",
"if",
"exon_id",
"not",
"in",
"self",
".",
"_exons",
":",
"field_names",
"=",
"[",
"\"seqname\"",
",",
"\"start\"",
",",
"\"end\"",
",",
"\"strand\"",
",",
"\"gene_name\"",
",",
"\"gene_id\"",
","... | Construct an Exon object from its ID by looking up the exon"s
properties in the given Database. | [
"Construct",
"an",
"Exon",
"object",
"from",
"its",
"ID",
"by",
"looking",
"up",
"the",
"exon",
"s",
"properties",
"in",
"the",
"given",
"Database",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/genome.py#L1022-L1052 | train | 199,417 |
openvax/pyensembl | pyensembl/ensembl_release_versions.py | check_release_number | def check_release_number(release):
"""
Check to make sure a release is in the valid range of
Ensembl releases.
"""
try:
release = int(release)
except:
raise ValueError("Invalid Ensembl release: %s" % release)
if release < MIN_ENSEMBL_RELEASE or release > MAX_ENSEMBL_RELEASE:
raise ValueError(
"Invalid Ensembl releases %d, must be between %d and %d" % (
release, MIN_ENSEMBL_RELEASE, MAX_ENSEMBL_RELEASE))
return release | python | def check_release_number(release):
"""
Check to make sure a release is in the valid range of
Ensembl releases.
"""
try:
release = int(release)
except:
raise ValueError("Invalid Ensembl release: %s" % release)
if release < MIN_ENSEMBL_RELEASE or release > MAX_ENSEMBL_RELEASE:
raise ValueError(
"Invalid Ensembl releases %d, must be between %d and %d" % (
release, MIN_ENSEMBL_RELEASE, MAX_ENSEMBL_RELEASE))
return release | [
"def",
"check_release_number",
"(",
"release",
")",
":",
"try",
":",
"release",
"=",
"int",
"(",
"release",
")",
"except",
":",
"raise",
"ValueError",
"(",
"\"Invalid Ensembl release: %s\"",
"%",
"release",
")",
"if",
"release",
"<",
"MIN_ENSEMBL_RELEASE",
"or",... | Check to make sure a release is in the valid range of
Ensembl releases. | [
"Check",
"to",
"make",
"sure",
"a",
"release",
"is",
"in",
"the",
"valid",
"range",
"of",
"Ensembl",
"releases",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/ensembl_release_versions.py#L20-L34 | train | 199,418 |
openvax/pyensembl | pyensembl/ensembl_url_templates.py | _species_subdir | def _species_subdir(
ensembl_release,
species="homo_sapiens",
filetype="gtf",
server=ENSEMBL_FTP_SERVER):
"""
Assume ensembl_release has already been normalize by calling function
but species might be either a common name or latin name.
"""
return SPECIES_SUBDIR_TEMPLATE % {
"release": ensembl_release,
"filetype": filetype,
"species": species,
} | python | def _species_subdir(
ensembl_release,
species="homo_sapiens",
filetype="gtf",
server=ENSEMBL_FTP_SERVER):
"""
Assume ensembl_release has already been normalize by calling function
but species might be either a common name or latin name.
"""
return SPECIES_SUBDIR_TEMPLATE % {
"release": ensembl_release,
"filetype": filetype,
"species": species,
} | [
"def",
"_species_subdir",
"(",
"ensembl_release",
",",
"species",
"=",
"\"homo_sapiens\"",
",",
"filetype",
"=",
"\"gtf\"",
",",
"server",
"=",
"ENSEMBL_FTP_SERVER",
")",
":",
"return",
"SPECIES_SUBDIR_TEMPLATE",
"%",
"{",
"\"release\"",
":",
"ensembl_release",
",",... | Assume ensembl_release has already been normalize by calling function
but species might be either a common name or latin name. | [
"Assume",
"ensembl_release",
"has",
"already",
"been",
"normalize",
"by",
"calling",
"function",
"but",
"species",
"might",
"be",
"either",
"a",
"common",
"name",
"or",
"latin",
"name",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/ensembl_url_templates.py#L41-L54 | train | 199,419 |
openvax/pyensembl | pyensembl/ensembl_url_templates.py | normalize_release_properties | def normalize_release_properties(ensembl_release, species):
"""
Make sure a given release is valid, normalize it to be an integer,
normalize the species name, and get its associated reference.
"""
ensembl_release = check_release_number(ensembl_release)
if not isinstance(species, Species):
species = find_species_by_name(species)
reference_name = species.which_reference(ensembl_release)
return ensembl_release, species.latin_name, reference_name | python | def normalize_release_properties(ensembl_release, species):
"""
Make sure a given release is valid, normalize it to be an integer,
normalize the species name, and get its associated reference.
"""
ensembl_release = check_release_number(ensembl_release)
if not isinstance(species, Species):
species = find_species_by_name(species)
reference_name = species.which_reference(ensembl_release)
return ensembl_release, species.latin_name, reference_name | [
"def",
"normalize_release_properties",
"(",
"ensembl_release",
",",
"species",
")",
":",
"ensembl_release",
"=",
"check_release_number",
"(",
"ensembl_release",
")",
"if",
"not",
"isinstance",
"(",
"species",
",",
"Species",
")",
":",
"species",
"=",
"find_species_b... | Make sure a given release is valid, normalize it to be an integer,
normalize the species name, and get its associated reference. | [
"Make",
"sure",
"a",
"given",
"release",
"is",
"valid",
"normalize",
"it",
"to",
"be",
"an",
"integer",
"normalize",
"the",
"species",
"name",
"and",
"get",
"its",
"associated",
"reference",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/ensembl_url_templates.py#L57-L66 | train | 199,420 |
openvax/pyensembl | pyensembl/ensembl_url_templates.py | make_gtf_url | def make_gtf_url(ensembl_release, species, server=ENSEMBL_FTP_SERVER):
"""
Returns a URL and a filename, which can be joined together.
"""
ensembl_release, species, _ = \
normalize_release_properties(ensembl_release, species)
subdir = _species_subdir(
ensembl_release,
species=species,
filetype="gtf",
server=server)
url_subdir = urllib_parse.urljoin(server, subdir)
filename = make_gtf_filename(
ensembl_release=ensembl_release,
species=species)
return join(url_subdir, filename) | python | def make_gtf_url(ensembl_release, species, server=ENSEMBL_FTP_SERVER):
"""
Returns a URL and a filename, which can be joined together.
"""
ensembl_release, species, _ = \
normalize_release_properties(ensembl_release, species)
subdir = _species_subdir(
ensembl_release,
species=species,
filetype="gtf",
server=server)
url_subdir = urllib_parse.urljoin(server, subdir)
filename = make_gtf_filename(
ensembl_release=ensembl_release,
species=species)
return join(url_subdir, filename) | [
"def",
"make_gtf_url",
"(",
"ensembl_release",
",",
"species",
",",
"server",
"=",
"ENSEMBL_FTP_SERVER",
")",
":",
"ensembl_release",
",",
"species",
",",
"_",
"=",
"normalize_release_properties",
"(",
"ensembl_release",
",",
"species",
")",
"subdir",
"=",
"_speci... | Returns a URL and a filename, which can be joined together. | [
"Returns",
"a",
"URL",
"and",
"a",
"filename",
"which",
"can",
"be",
"joined",
"together",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/ensembl_url_templates.py#L85-L100 | train | 199,421 |
openvax/pyensembl | pyensembl/ensembl_url_templates.py | make_fasta_url | def make_fasta_url(
ensembl_release,
species,
sequence_type,
server=ENSEMBL_FTP_SERVER):
"""Construct URL to FASTA file with cDNA transcript or protein sequences
Parameter examples:
ensembl_release = 75
species = "Homo_sapiens"
sequence_type = "cdna" (other option: "pep")
"""
ensembl_release, species, reference_name = normalize_release_properties(
ensembl_release, species)
subdir = _species_subdir(
ensembl_release,
species=species,
filetype="fasta",
server=server)
server_subdir = urllib_parse.urljoin(server, subdir)
server_sequence_subdir = join(server_subdir, sequence_type)
filename = make_fasta_filename(
ensembl_release=ensembl_release,
species=species,
sequence_type=sequence_type)
return join(server_sequence_subdir, filename) | python | def make_fasta_url(
ensembl_release,
species,
sequence_type,
server=ENSEMBL_FTP_SERVER):
"""Construct URL to FASTA file with cDNA transcript or protein sequences
Parameter examples:
ensembl_release = 75
species = "Homo_sapiens"
sequence_type = "cdna" (other option: "pep")
"""
ensembl_release, species, reference_name = normalize_release_properties(
ensembl_release, species)
subdir = _species_subdir(
ensembl_release,
species=species,
filetype="fasta",
server=server)
server_subdir = urllib_parse.urljoin(server, subdir)
server_sequence_subdir = join(server_subdir, sequence_type)
filename = make_fasta_filename(
ensembl_release=ensembl_release,
species=species,
sequence_type=sequence_type)
return join(server_sequence_subdir, filename) | [
"def",
"make_fasta_url",
"(",
"ensembl_release",
",",
"species",
",",
"sequence_type",
",",
"server",
"=",
"ENSEMBL_FTP_SERVER",
")",
":",
"ensembl_release",
",",
"species",
",",
"reference_name",
"=",
"normalize_release_properties",
"(",
"ensembl_release",
",",
"spec... | Construct URL to FASTA file with cDNA transcript or protein sequences
Parameter examples:
ensembl_release = 75
species = "Homo_sapiens"
sequence_type = "cdna" (other option: "pep") | [
"Construct",
"URL",
"to",
"FASTA",
"file",
"with",
"cDNA",
"transcript",
"or",
"protein",
"sequences"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/ensembl_url_templates.py#L195-L220 | train | 199,422 |
openvax/pyensembl | pyensembl/transcript.py | Transcript._transcript_feature_positions | def _transcript_feature_positions(self, feature):
"""
Get unique positions for feature, raise an error if feature is absent.
"""
ranges = self._transcript_feature_position_ranges(
feature, required=True)
results = []
# a feature (such as a stop codon), maybe be split over multiple
# contiguous ranges. Collect all the nucleotide positions into a
# single list.
for (start, end) in ranges:
# since ranges are [inclusive, inclusive] and
# Python ranges are [inclusive, exclusive) we have to increment
# the end position
for position in range(start, end + 1):
assert position not in results, \
"Repeated position %d for %s" % (position, feature)
results.append(position)
return results | python | def _transcript_feature_positions(self, feature):
"""
Get unique positions for feature, raise an error if feature is absent.
"""
ranges = self._transcript_feature_position_ranges(
feature, required=True)
results = []
# a feature (such as a stop codon), maybe be split over multiple
# contiguous ranges. Collect all the nucleotide positions into a
# single list.
for (start, end) in ranges:
# since ranges are [inclusive, inclusive] and
# Python ranges are [inclusive, exclusive) we have to increment
# the end position
for position in range(start, end + 1):
assert position not in results, \
"Repeated position %d for %s" % (position, feature)
results.append(position)
return results | [
"def",
"_transcript_feature_positions",
"(",
"self",
",",
"feature",
")",
":",
"ranges",
"=",
"self",
".",
"_transcript_feature_position_ranges",
"(",
"feature",
",",
"required",
"=",
"True",
")",
"results",
"=",
"[",
"]",
"# a feature (such as a stop codon), maybe be... | Get unique positions for feature, raise an error if feature is absent. | [
"Get",
"unique",
"positions",
"for",
"feature",
"raise",
"an",
"error",
"if",
"feature",
"is",
"absent",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L182-L200 | train | 199,423 |
openvax/pyensembl | pyensembl/transcript.py | Transcript.spliced_offset | def spliced_offset(self, position):
"""
Convert from an absolute chromosomal position to the offset into
this transcript"s spliced mRNA.
Position must be inside some exon (otherwise raise exception).
"""
# this code is performance sensitive, so switching from
# typechecks.require_integer to a simpler assertion
assert type(position) == int, \
"Position argument must be an integer, got %s : %s" % (
position, type(position))
if position < self.start or position > self.end:
raise ValueError(
"Invalid position: %d (must be between %d and %d)" % (
position,
self.start,
self.end))
# offset from beginning of unspliced transcript (including introns)
unspliced_offset = self.offset(position)
total_spliced_offset = 0
# traverse exons in order of their appearance on the strand
# Since absolute positions may decrease if on the negative strand,
# we instead use unspliced offsets to get always increasing indices.
#
# Example:
#
# Exon Name: exon 1 exon 2
# Spliced Offset: 123456 789...
# Intron vs. Exon: ...iiiiiieeeeeeiiiiiiiiiiiiiiiieeeeeeiiiiiiiiiii...
for exon in self.exons:
exon_unspliced_start, exon_unspliced_end = self.offset_range(
exon.start, exon.end)
# If the relative position is not within this exon, keep a running
# total of the total exonic length-so-far.
#
# Otherwise, if the relative position is within an exon, get its
# offset into that exon by subtracting the exon"s relative start
# position from the relative position. Add that to the total exonic
# length-so-far.
if exon_unspliced_start <= unspliced_offset <= exon_unspliced_end:
# all offsets are base 0, can be used as indices into
# sequence string
exon_offset = unspliced_offset - exon_unspliced_start
return total_spliced_offset + exon_offset
else:
exon_length = len(exon) # exon_end_position - exon_start_position + 1
total_spliced_offset += exon_length
raise ValueError(
"Couldn't find position %d on any exon of %s" % (
position, self.id)) | python | def spliced_offset(self, position):
"""
Convert from an absolute chromosomal position to the offset into
this transcript"s spliced mRNA.
Position must be inside some exon (otherwise raise exception).
"""
# this code is performance sensitive, so switching from
# typechecks.require_integer to a simpler assertion
assert type(position) == int, \
"Position argument must be an integer, got %s : %s" % (
position, type(position))
if position < self.start or position > self.end:
raise ValueError(
"Invalid position: %d (must be between %d and %d)" % (
position,
self.start,
self.end))
# offset from beginning of unspliced transcript (including introns)
unspliced_offset = self.offset(position)
total_spliced_offset = 0
# traverse exons in order of their appearance on the strand
# Since absolute positions may decrease if on the negative strand,
# we instead use unspliced offsets to get always increasing indices.
#
# Example:
#
# Exon Name: exon 1 exon 2
# Spliced Offset: 123456 789...
# Intron vs. Exon: ...iiiiiieeeeeeiiiiiiiiiiiiiiiieeeeeeiiiiiiiiiii...
for exon in self.exons:
exon_unspliced_start, exon_unspliced_end = self.offset_range(
exon.start, exon.end)
# If the relative position is not within this exon, keep a running
# total of the total exonic length-so-far.
#
# Otherwise, if the relative position is within an exon, get its
# offset into that exon by subtracting the exon"s relative start
# position from the relative position. Add that to the total exonic
# length-so-far.
if exon_unspliced_start <= unspliced_offset <= exon_unspliced_end:
# all offsets are base 0, can be used as indices into
# sequence string
exon_offset = unspliced_offset - exon_unspliced_start
return total_spliced_offset + exon_offset
else:
exon_length = len(exon) # exon_end_position - exon_start_position + 1
total_spliced_offset += exon_length
raise ValueError(
"Couldn't find position %d on any exon of %s" % (
position, self.id)) | [
"def",
"spliced_offset",
"(",
"self",
",",
"position",
")",
":",
"# this code is performance sensitive, so switching from",
"# typechecks.require_integer to a simpler assertion",
"assert",
"type",
"(",
"position",
")",
"==",
"int",
",",
"\"Position argument must be an integer, go... | Convert from an absolute chromosomal position to the offset into
this transcript"s spliced mRNA.
Position must be inside some exon (otherwise raise exception). | [
"Convert",
"from",
"an",
"absolute",
"chromosomal",
"position",
"to",
"the",
"offset",
"into",
"this",
"transcript",
"s",
"spliced",
"mRNA",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L269-L322 | train | 199,424 |
openvax/pyensembl | pyensembl/transcript.py | Transcript._contiguous_offsets | def _contiguous_offsets(self, offsets):
"""
Sorts the input list of integer offsets,
ensures that values are contiguous.
"""
offsets.sort()
for i in range(len(offsets) - 1):
assert offsets[i] + 1 == offsets[i + 1], \
"Offsets not contiguous: %s" % (offsets,)
return offsets | python | def _contiguous_offsets(self, offsets):
"""
Sorts the input list of integer offsets,
ensures that values are contiguous.
"""
offsets.sort()
for i in range(len(offsets) - 1):
assert offsets[i] + 1 == offsets[i + 1], \
"Offsets not contiguous: %s" % (offsets,)
return offsets | [
"def",
"_contiguous_offsets",
"(",
"self",
",",
"offsets",
")",
":",
"offsets",
".",
"sort",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"offsets",
")",
"-",
"1",
")",
":",
"assert",
"offsets",
"[",
"i",
"]",
"+",
"1",
"==",
"offsets",
... | Sorts the input list of integer offsets,
ensures that values are contiguous. | [
"Sorts",
"the",
"input",
"list",
"of",
"integer",
"offsets",
"ensures",
"that",
"values",
"are",
"contiguous",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L348-L357 | train | 199,425 |
openvax/pyensembl | pyensembl/transcript.py | Transcript.start_codon_spliced_offsets | def start_codon_spliced_offsets(self):
"""
Offsets from start of spliced mRNA transcript
of nucleotides in start codon.
"""
offsets = [
self.spliced_offset(position)
for position
in self.start_codon_positions
]
return self._contiguous_offsets(offsets) | python | def start_codon_spliced_offsets(self):
"""
Offsets from start of spliced mRNA transcript
of nucleotides in start codon.
"""
offsets = [
self.spliced_offset(position)
for position
in self.start_codon_positions
]
return self._contiguous_offsets(offsets) | [
"def",
"start_codon_spliced_offsets",
"(",
"self",
")",
":",
"offsets",
"=",
"[",
"self",
".",
"spliced_offset",
"(",
"position",
")",
"for",
"position",
"in",
"self",
".",
"start_codon_positions",
"]",
"return",
"self",
".",
"_contiguous_offsets",
"(",
"offsets... | Offsets from start of spliced mRNA transcript
of nucleotides in start codon. | [
"Offsets",
"from",
"start",
"of",
"spliced",
"mRNA",
"transcript",
"of",
"nucleotides",
"in",
"start",
"codon",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L360-L370 | train | 199,426 |
openvax/pyensembl | pyensembl/transcript.py | Transcript.stop_codon_spliced_offsets | def stop_codon_spliced_offsets(self):
"""
Offsets from start of spliced mRNA transcript
of nucleotides in stop codon.
"""
offsets = [
self.spliced_offset(position)
for position
in self.stop_codon_positions
]
return self._contiguous_offsets(offsets) | python | def stop_codon_spliced_offsets(self):
"""
Offsets from start of spliced mRNA transcript
of nucleotides in stop codon.
"""
offsets = [
self.spliced_offset(position)
for position
in self.stop_codon_positions
]
return self._contiguous_offsets(offsets) | [
"def",
"stop_codon_spliced_offsets",
"(",
"self",
")",
":",
"offsets",
"=",
"[",
"self",
".",
"spliced_offset",
"(",
"position",
")",
"for",
"position",
"in",
"self",
".",
"stop_codon_positions",
"]",
"return",
"self",
".",
"_contiguous_offsets",
"(",
"offsets",... | Offsets from start of spliced mRNA transcript
of nucleotides in stop codon. | [
"Offsets",
"from",
"start",
"of",
"spliced",
"mRNA",
"transcript",
"of",
"nucleotides",
"in",
"stop",
"codon",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L373-L383 | train | 199,427 |
openvax/pyensembl | pyensembl/transcript.py | Transcript.complete | def complete(self):
"""
Consider a transcript complete if it has start and stop codons and
a coding sequence whose length is divisible by 3
"""
return (
self.contains_start_codon and
self.contains_stop_codon and
self.coding_sequence is not None and
len(self.coding_sequence) % 3 == 0
) | python | def complete(self):
"""
Consider a transcript complete if it has start and stop codons and
a coding sequence whose length is divisible by 3
"""
return (
self.contains_start_codon and
self.contains_stop_codon and
self.coding_sequence is not None and
len(self.coding_sequence) % 3 == 0
) | [
"def",
"complete",
"(",
"self",
")",
":",
"return",
"(",
"self",
".",
"contains_start_codon",
"and",
"self",
".",
"contains_stop_codon",
"and",
"self",
".",
"coding_sequence",
"is",
"not",
"None",
"and",
"len",
"(",
"self",
".",
"coding_sequence",
")",
"%",
... | Consider a transcript complete if it has start and stop codons and
a coding sequence whose length is divisible by 3 | [
"Consider",
"a",
"transcript",
"complete",
"if",
"it",
"has",
"start",
"and",
"stop",
"codons",
"and",
"a",
"coding",
"sequence",
"whose",
"length",
"is",
"divisible",
"by",
"3"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/transcript.py#L394-L404 | train | 199,428 |
openvax/pyensembl | pyensembl/database.py | Database._all_possible_indices | def _all_possible_indices(self, column_names):
"""
Create list of tuples containing all possible index groups
we might want to create over tables in this database.
If a set of genome annotations is missing some column we want
to index on, we have to drop any indices which use that column.
A specific table may later drop some of these indices if they're
missing values for that feature or are the same as the table's primary key.
"""
candidate_column_groups = [
['seqname', 'start', 'end'],
['gene_name'],
['gene_id'],
['transcript_id'],
['transcript_name'],
['exon_id'],
['protein_id'],
['ccds_id'],
]
indices = []
column_set = set(column_names)
# Since queries are often restricted by feature type
# we should include that column in combination with all
# other indices we anticipate might improve performance
for column_group in candidate_column_groups:
skip = False
for column_name in column_group:
# some columns, such as 'exon_id',
# are not available in all releases of Ensembl (or
# other GTFs)
if column_name not in column_set:
logger.info(
"Skipping database index for {%s}",
", ".join(column_group))
skip = True
if skip:
continue
indices.append(column_group)
return indices | python | def _all_possible_indices(self, column_names):
"""
Create list of tuples containing all possible index groups
we might want to create over tables in this database.
If a set of genome annotations is missing some column we want
to index on, we have to drop any indices which use that column.
A specific table may later drop some of these indices if they're
missing values for that feature or are the same as the table's primary key.
"""
candidate_column_groups = [
['seqname', 'start', 'end'],
['gene_name'],
['gene_id'],
['transcript_id'],
['transcript_name'],
['exon_id'],
['protein_id'],
['ccds_id'],
]
indices = []
column_set = set(column_names)
# Since queries are often restricted by feature type
# we should include that column in combination with all
# other indices we anticipate might improve performance
for column_group in candidate_column_groups:
skip = False
for column_name in column_group:
# some columns, such as 'exon_id',
# are not available in all releases of Ensembl (or
# other GTFs)
if column_name not in column_set:
logger.info(
"Skipping database index for {%s}",
", ".join(column_group))
skip = True
if skip:
continue
indices.append(column_group)
return indices | [
"def",
"_all_possible_indices",
"(",
"self",
",",
"column_names",
")",
":",
"candidate_column_groups",
"=",
"[",
"[",
"'seqname'",
",",
"'start'",
",",
"'end'",
"]",
",",
"[",
"'gene_name'",
"]",
",",
"[",
"'gene_id'",
"]",
",",
"[",
"'transcript_id'",
"]",
... | Create list of tuples containing all possible index groups
we might want to create over tables in this database.
If a set of genome annotations is missing some column we want
to index on, we have to drop any indices which use that column.
A specific table may later drop some of these indices if they're
missing values for that feature or are the same as the table's primary key. | [
"Create",
"list",
"of",
"tuples",
"containing",
"all",
"possible",
"index",
"groups",
"we",
"might",
"want",
"to",
"create",
"over",
"tables",
"in",
"this",
"database",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L111-L151 | train | 199,429 |
openvax/pyensembl | pyensembl/database.py | Database.connection | def connection(self):
"""
Get a connection to the database or raise an exception
"""
connection = self._get_connection()
if connection:
return connection
else:
message = "GTF database needs to be created"
if self.install_string:
message += ", run: %s" % self.install_string
raise ValueError(message) | python | def connection(self):
"""
Get a connection to the database or raise an exception
"""
connection = self._get_connection()
if connection:
return connection
else:
message = "GTF database needs to be created"
if self.install_string:
message += ", run: %s" % self.install_string
raise ValueError(message) | [
"def",
"connection",
"(",
"self",
")",
":",
"connection",
"=",
"self",
".",
"_get_connection",
"(",
")",
"if",
"connection",
":",
"return",
"connection",
"else",
":",
"message",
"=",
"\"GTF database needs to be created\"",
"if",
"self",
".",
"install_string",
":... | Get a connection to the database or raise an exception | [
"Get",
"a",
"connection",
"to",
"the",
"database",
"or",
"raise",
"an",
"exception"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L271-L282 | train | 199,430 |
openvax/pyensembl | pyensembl/database.py | Database.connect_or_create | def connect_or_create(self, overwrite=False):
"""
Return a connection to the database if it exists, otherwise create it.
Overwrite the existing database if `overwrite` is True.
"""
connection = self._get_connection()
if connection:
return connection
else:
return self.create(overwrite=overwrite) | python | def connect_or_create(self, overwrite=False):
"""
Return a connection to the database if it exists, otherwise create it.
Overwrite the existing database if `overwrite` is True.
"""
connection = self._get_connection()
if connection:
return connection
else:
return self.create(overwrite=overwrite) | [
"def",
"connect_or_create",
"(",
"self",
",",
"overwrite",
"=",
"False",
")",
":",
"connection",
"=",
"self",
".",
"_get_connection",
"(",
")",
"if",
"connection",
":",
"return",
"connection",
"else",
":",
"return",
"self",
".",
"create",
"(",
"overwrite",
... | Return a connection to the database if it exists, otherwise create it.
Overwrite the existing database if `overwrite` is True. | [
"Return",
"a",
"connection",
"to",
"the",
"database",
"if",
"it",
"exists",
"otherwise",
"create",
"it",
".",
"Overwrite",
"the",
"existing",
"database",
"if",
"overwrite",
"is",
"True",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L284-L293 | train | 199,431 |
openvax/pyensembl | pyensembl/database.py | Database.run_sql_query | def run_sql_query(self, sql, required=False, query_params=[]):
"""
Given an arbitrary SQL query, run it against the database
and return the results.
Parameters
----------
sql : str
SQL query
required : bool
Raise an error if no results found in the database
query_params : list
For each '?' in the query there must be a corresponding value in
this list.
"""
try:
cursor = self.connection.execute(sql, query_params)
except sqlite3.OperationalError as e:
error_message = e.message if hasattr(e, 'message') else str(e)
logger.warn(
"Encountered error \"%s\" from query \"%s\" with parameters %s",
error_message,
sql,
query_params)
raise
results = cursor.fetchall()
if required and not results:
raise ValueError(
"No results found for query:\n%s\nwith parameters: %s" % (
sql, query_params))
return results | python | def run_sql_query(self, sql, required=False, query_params=[]):
"""
Given an arbitrary SQL query, run it against the database
and return the results.
Parameters
----------
sql : str
SQL query
required : bool
Raise an error if no results found in the database
query_params : list
For each '?' in the query there must be a corresponding value in
this list.
"""
try:
cursor = self.connection.execute(sql, query_params)
except sqlite3.OperationalError as e:
error_message = e.message if hasattr(e, 'message') else str(e)
logger.warn(
"Encountered error \"%s\" from query \"%s\" with parameters %s",
error_message,
sql,
query_params)
raise
results = cursor.fetchall()
if required and not results:
raise ValueError(
"No results found for query:\n%s\nwith parameters: %s" % (
sql, query_params))
return results | [
"def",
"run_sql_query",
"(",
"self",
",",
"sql",
",",
"required",
"=",
"False",
",",
"query_params",
"=",
"[",
"]",
")",
":",
"try",
":",
"cursor",
"=",
"self",
".",
"connection",
".",
"execute",
"(",
"sql",
",",
"query_params",
")",
"except",
"sqlite3... | Given an arbitrary SQL query, run it against the database
and return the results.
Parameters
----------
sql : str
SQL query
required : bool
Raise an error if no results found in the database
query_params : list
For each '?' in the query there must be a corresponding value in
this list. | [
"Given",
"an",
"arbitrary",
"SQL",
"query",
"run",
"it",
"against",
"the",
"database",
"and",
"return",
"the",
"results",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L412-L445 | train | 199,432 |
openvax/pyensembl | pyensembl/database.py | Database.query_loci | def query_loci(self, filter_column, filter_value, feature):
"""
Query for loci satisfying a given filter and feature type.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns list of Locus objects
"""
# list of values containing (contig, start, stop, strand)
result_tuples = self.query(
select_column_names=["seqname", "start", "end", "strand"],
filter_column=filter_column,
filter_value=filter_value,
feature=feature,
distinct=True,
required=True)
return [
Locus(contig, start, end, strand)
for (contig, start, end, strand)
in result_tuples
] | python | def query_loci(self, filter_column, filter_value, feature):
"""
Query for loci satisfying a given filter and feature type.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns list of Locus objects
"""
# list of values containing (contig, start, stop, strand)
result_tuples = self.query(
select_column_names=["seqname", "start", "end", "strand"],
filter_column=filter_column,
filter_value=filter_value,
feature=feature,
distinct=True,
required=True)
return [
Locus(contig, start, end, strand)
for (contig, start, end, strand)
in result_tuples
] | [
"def",
"query_loci",
"(",
"self",
",",
"filter_column",
",",
"filter_value",
",",
"feature",
")",
":",
"# list of values containing (contig, start, stop, strand)",
"result_tuples",
"=",
"self",
".",
"query",
"(",
"select_column_names",
"=",
"[",
"\"seqname\"",
",",
"\... | Query for loci satisfying a given filter and feature type.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns list of Locus objects | [
"Query",
"for",
"loci",
"satisfying",
"a",
"given",
"filter",
"and",
"feature",
"type",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L539-L569 | train | 199,433 |
openvax/pyensembl | pyensembl/database.py | Database.query_locus | def query_locus(self, filter_column, filter_value, feature):
"""
Query for unique locus, raises error if missing or more than
one locus in the database.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns single Locus object.
"""
loci = self.query_loci(
filter_column=filter_column,
filter_value=filter_value,
feature=feature)
if len(loci) == 0:
raise ValueError("Couldn't find locus for %s with %s = %s" % (
feature, filter_column, filter_value))
elif len(loci) > 1:
raise ValueError("Too many loci for %s with %s = %s: %s" % (
feature, filter_column, filter_value, loci))
return loci[0] | python | def query_locus(self, filter_column, filter_value, feature):
"""
Query for unique locus, raises error if missing or more than
one locus in the database.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns single Locus object.
"""
loci = self.query_loci(
filter_column=filter_column,
filter_value=filter_value,
feature=feature)
if len(loci) == 0:
raise ValueError("Couldn't find locus for %s with %s = %s" % (
feature, filter_column, filter_value))
elif len(loci) > 1:
raise ValueError("Too many loci for %s with %s = %s: %s" % (
feature, filter_column, filter_value, loci))
return loci[0] | [
"def",
"query_locus",
"(",
"self",
",",
"filter_column",
",",
"filter_value",
",",
"feature",
")",
":",
"loci",
"=",
"self",
".",
"query_loci",
"(",
"filter_column",
"=",
"filter_column",
",",
"filter_value",
"=",
"filter_value",
",",
"feature",
"=",
"feature"... | Query for unique locus, raises error if missing or more than
one locus in the database.
Parameters
----------
filter_column : str
Name of column to filter results by.
filter_value : str
Only return loci which have this value in the their filter_column.
feature : str
Feature names such as 'transcript', 'gene', and 'exon'
Returns single Locus object. | [
"Query",
"for",
"unique",
"locus",
"raises",
"error",
"if",
"missing",
"or",
"more",
"than",
"one",
"locus",
"in",
"the",
"database",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L571-L600 | train | 199,434 |
openvax/pyensembl | pyensembl/database.py | Database._load_gtf_as_dataframe | def _load_gtf_as_dataframe(self, usecols=None, features=None):
"""
Parse this genome source's GTF file and load it as a Pandas DataFrame
"""
logger.info("Reading GTF from %s", self.gtf_path)
df = read_gtf(
self.gtf_path,
column_converters={
"seqname": normalize_chromosome,
"strand": normalize_strand,
},
infer_biotype_column=True,
usecols=usecols,
features=features)
column_names = set(df.keys())
expect_gene_feature = features is None or "gene" in features
expect_transcript_feature = features is None or "transcript" in features
observed_features = set(df["feature"])
# older Ensembl releases don't have "gene" or "transcript"
# features, so fill in those rows if they're missing
if expect_gene_feature and "gene" not in observed_features:
# if we have to reconstruct gene feature rows then
# fill in values for 'gene_name' and 'gene_biotype'
# but only if they're actually present in the GTF
logger.info("Creating missing gene features...")
df = create_missing_features(
dataframe=df,
unique_keys={"gene": "gene_id"},
extra_columns={
"gene": {
"gene_name",
"gene_biotype"
}.intersection(column_names),
},
missing_value="")
logger.info("Done.")
if expect_transcript_feature and "transcript" not in observed_features:
logger.info("Creating missing transcript features...")
df = create_missing_features(
dataframe=df,
unique_keys={"transcript": "transcript_id"},
extra_columns={
"transcript": {
"gene_id",
"gene_name",
"gene_biotype",
"transcript_name",
"transcript_biotype",
"protein_id",
}.intersection(column_names)
},
missing_value="")
logger.info("Done.")
return df | python | def _load_gtf_as_dataframe(self, usecols=None, features=None):
"""
Parse this genome source's GTF file and load it as a Pandas DataFrame
"""
logger.info("Reading GTF from %s", self.gtf_path)
df = read_gtf(
self.gtf_path,
column_converters={
"seqname": normalize_chromosome,
"strand": normalize_strand,
},
infer_biotype_column=True,
usecols=usecols,
features=features)
column_names = set(df.keys())
expect_gene_feature = features is None or "gene" in features
expect_transcript_feature = features is None or "transcript" in features
observed_features = set(df["feature"])
# older Ensembl releases don't have "gene" or "transcript"
# features, so fill in those rows if they're missing
if expect_gene_feature and "gene" not in observed_features:
# if we have to reconstruct gene feature rows then
# fill in values for 'gene_name' and 'gene_biotype'
# but only if they're actually present in the GTF
logger.info("Creating missing gene features...")
df = create_missing_features(
dataframe=df,
unique_keys={"gene": "gene_id"},
extra_columns={
"gene": {
"gene_name",
"gene_biotype"
}.intersection(column_names),
},
missing_value="")
logger.info("Done.")
if expect_transcript_feature and "transcript" not in observed_features:
logger.info("Creating missing transcript features...")
df = create_missing_features(
dataframe=df,
unique_keys={"transcript": "transcript_id"},
extra_columns={
"transcript": {
"gene_id",
"gene_name",
"gene_biotype",
"transcript_name",
"transcript_biotype",
"protein_id",
}.intersection(column_names)
},
missing_value="")
logger.info("Done.")
return df | [
"def",
"_load_gtf_as_dataframe",
"(",
"self",
",",
"usecols",
"=",
"None",
",",
"features",
"=",
"None",
")",
":",
"logger",
".",
"info",
"(",
"\"Reading GTF from %s\"",
",",
"self",
".",
"gtf_path",
")",
"df",
"=",
"read_gtf",
"(",
"self",
".",
"gtf_path"... | Parse this genome source's GTF file and load it as a Pandas DataFrame | [
"Parse",
"this",
"genome",
"source",
"s",
"GTF",
"file",
"and",
"load",
"it",
"as",
"a",
"Pandas",
"DataFrame"
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/database.py#L602-L659 | train | 199,435 |
openvax/pyensembl | pyensembl/gene.py | Gene.transcripts | def transcripts(self):
"""
Property which dynamically construct transcript objects for all
transcript IDs associated with this gene.
"""
transcript_id_results = self.db.query(
select_column_names=['transcript_id'],
filter_column='gene_id',
filter_value=self.id,
feature='transcript',
distinct=False,
required=False)
# We're doing a SQL query for each transcript ID to fetch
# its particular information, might be more efficient if we
# just get all the columns here, but how do we keep that modular?
return [
self.genome.transcript_by_id(result[0])
for result in transcript_id_results
] | python | def transcripts(self):
"""
Property which dynamically construct transcript objects for all
transcript IDs associated with this gene.
"""
transcript_id_results = self.db.query(
select_column_names=['transcript_id'],
filter_column='gene_id',
filter_value=self.id,
feature='transcript',
distinct=False,
required=False)
# We're doing a SQL query for each transcript ID to fetch
# its particular information, might be more efficient if we
# just get all the columns here, but how do we keep that modular?
return [
self.genome.transcript_by_id(result[0])
for result in transcript_id_results
] | [
"def",
"transcripts",
"(",
"self",
")",
":",
"transcript_id_results",
"=",
"self",
".",
"db",
".",
"query",
"(",
"select_column_names",
"=",
"[",
"'transcript_id'",
"]",
",",
"filter_column",
"=",
"'gene_id'",
",",
"filter_value",
"=",
"self",
".",
"id",
","... | Property which dynamically construct transcript objects for all
transcript IDs associated with this gene. | [
"Property",
"which",
"dynamically",
"construct",
"transcript",
"objects",
"for",
"all",
"transcript",
"IDs",
"associated",
"with",
"this",
"gene",
"."
] | 4b995fb72e848206d6fbf11950cf30964cd9b3aa | https://github.com/openvax/pyensembl/blob/4b995fb72e848206d6fbf11950cf30964cd9b3aa/pyensembl/gene.py#L92-L111 | train | 199,436 |
7sDream/zhihu-py3 | zhihu/common.py | clone_bs4_elem | def clone_bs4_elem(el):
"""Clone a bs4 tag before modifying it.
Code from `http://stackoverflow.com/questions/23057631/clone-element-with
-beautifulsoup`
"""
if isinstance(el, NavigableString):
return type(el)(el)
copy = Tag(None, el.builder, el.name, el.namespace, el.nsprefix)
# work around bug where there is no builder set
# https://bugs.launchpad.net/beautifulsoup/+bug/1307471
copy.attrs = dict(el.attrs)
for attr in ('can_be_empty_element', 'hidden'):
setattr(copy, attr, getattr(el, attr))
for child in el.contents:
copy.append(clone_bs4_elem(child))
return copy | python | def clone_bs4_elem(el):
"""Clone a bs4 tag before modifying it.
Code from `http://stackoverflow.com/questions/23057631/clone-element-with
-beautifulsoup`
"""
if isinstance(el, NavigableString):
return type(el)(el)
copy = Tag(None, el.builder, el.name, el.namespace, el.nsprefix)
# work around bug where there is no builder set
# https://bugs.launchpad.net/beautifulsoup/+bug/1307471
copy.attrs = dict(el.attrs)
for attr in ('can_be_empty_element', 'hidden'):
setattr(copy, attr, getattr(el, attr))
for child in el.contents:
copy.append(clone_bs4_elem(child))
return copy | [
"def",
"clone_bs4_elem",
"(",
"el",
")",
":",
"if",
"isinstance",
"(",
"el",
",",
"NavigableString",
")",
":",
"return",
"type",
"(",
"el",
")",
"(",
"el",
")",
"copy",
"=",
"Tag",
"(",
"None",
",",
"el",
".",
"builder",
",",
"el",
".",
"name",
"... | Clone a bs4 tag before modifying it.
Code from `http://stackoverflow.com/questions/23057631/clone-element-with
-beautifulsoup` | [
"Clone",
"a",
"bs4",
"tag",
"before",
"modifying",
"it",
"."
] | bcb4aa8325f8b54d3b44bd0bdc959edd9761fcfc | https://github.com/7sDream/zhihu-py3/blob/bcb4aa8325f8b54d3b44bd0bdc959edd9761fcfc/zhihu/common.py#L248-L265 | train | 199,437 |
pmorissette/ffn | ffn/utils.py | clean_ticker | def clean_ticker(ticker):
"""
Cleans a ticker for easier use throughout MoneyTree
Splits by space and only keeps first bit. Also removes
any characters that are not letters. Returns as lowercase.
>>> clean_ticker('^VIX')
'vix'
>>> clean_ticker('SPX Index')
'spx'
"""
pattern = re.compile('[\W_]+')
res = pattern.sub('', ticker.split(' ')[0])
return res.lower() | python | def clean_ticker(ticker):
"""
Cleans a ticker for easier use throughout MoneyTree
Splits by space and only keeps first bit. Also removes
any characters that are not letters. Returns as lowercase.
>>> clean_ticker('^VIX')
'vix'
>>> clean_ticker('SPX Index')
'spx'
"""
pattern = re.compile('[\W_]+')
res = pattern.sub('', ticker.split(' ')[0])
return res.lower() | [
"def",
"clean_ticker",
"(",
"ticker",
")",
":",
"pattern",
"=",
"re",
".",
"compile",
"(",
"'[\\W_]+'",
")",
"res",
"=",
"pattern",
".",
"sub",
"(",
"''",
",",
"ticker",
".",
"split",
"(",
"' '",
")",
"[",
"0",
"]",
")",
"return",
"res",
".",
"lo... | Cleans a ticker for easier use throughout MoneyTree
Splits by space and only keeps first bit. Also removes
any characters that are not letters. Returns as lowercase.
>>> clean_ticker('^VIX')
'vix'
>>> clean_ticker('SPX Index')
'spx' | [
"Cleans",
"a",
"ticker",
"for",
"easier",
"use",
"throughout",
"MoneyTree"
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/utils.py#L67-L81 | train | 199,438 |
pmorissette/ffn | ffn/utils.py | scale | def scale(val, src, dst):
"""
Scale value from src range to dst range.
If value outside bounds, it is clipped and set to
the low or high bound of dst.
Ex:
scale(0, (0.0, 99.0), (-1.0, 1.0)) == -1.0
scale(-5, (0.0, 99.0), (-1.0, 1.0)) == -1.0
"""
if val < src[0]:
return dst[0]
if val > src[1]:
return dst[1]
return ((val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0] | python | def scale(val, src, dst):
"""
Scale value from src range to dst range.
If value outside bounds, it is clipped and set to
the low or high bound of dst.
Ex:
scale(0, (0.0, 99.0), (-1.0, 1.0)) == -1.0
scale(-5, (0.0, 99.0), (-1.0, 1.0)) == -1.0
"""
if val < src[0]:
return dst[0]
if val > src[1]:
return dst[1]
return ((val - src[0]) / (src[1] - src[0])) * (dst[1] - dst[0]) + dst[0] | [
"def",
"scale",
"(",
"val",
",",
"src",
",",
"dst",
")",
":",
"if",
"val",
"<",
"src",
"[",
"0",
"]",
":",
"return",
"dst",
"[",
"0",
"]",
"if",
"val",
">",
"src",
"[",
"1",
"]",
":",
"return",
"dst",
"[",
"1",
"]",
"return",
"(",
"(",
"v... | Scale value from src range to dst range.
If value outside bounds, it is clipped and set to
the low or high bound of dst.
Ex:
scale(0, (0.0, 99.0), (-1.0, 1.0)) == -1.0
scale(-5, (0.0, 99.0), (-1.0, 1.0)) == -1.0 | [
"Scale",
"value",
"from",
"src",
"range",
"to",
"dst",
"range",
".",
"If",
"value",
"outside",
"bounds",
"it",
"is",
"clipped",
"and",
"set",
"to",
"the",
"low",
"or",
"high",
"bound",
"of",
"dst",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/utils.py#L152-L168 | train | 199,439 |
pmorissette/ffn | ffn/utils.py | as_format | def as_format(item, format_str='.2f'):
"""
Map a format string over a pandas object.
"""
if isinstance(item, pd.Series):
return item.map(lambda x: format(x, format_str))
elif isinstance(item, pd.DataFrame):
return item.applymap(lambda x: format(x, format_str)) | python | def as_format(item, format_str='.2f'):
"""
Map a format string over a pandas object.
"""
if isinstance(item, pd.Series):
return item.map(lambda x: format(x, format_str))
elif isinstance(item, pd.DataFrame):
return item.applymap(lambda x: format(x, format_str)) | [
"def",
"as_format",
"(",
"item",
",",
"format_str",
"=",
"'.2f'",
")",
":",
"if",
"isinstance",
"(",
"item",
",",
"pd",
".",
"Series",
")",
":",
"return",
"item",
".",
"map",
"(",
"lambda",
"x",
":",
"format",
"(",
"x",
",",
"format_str",
")",
")",... | Map a format string over a pandas object. | [
"Map",
"a",
"format",
"string",
"over",
"a",
"pandas",
"object",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/utils.py#L175-L182 | train | 199,440 |
pmorissette/ffn | ffn/core.py | to_price_index | def to_price_index(returns, start=100):
"""
Returns a price index given a series of returns.
Args:
* returns: Expects a return series
* start (number): Starting level
Assumes arithmetic returns.
Formula is: cumprod (1+r)
"""
return (returns.replace(to_replace=np.nan, value=0) + 1).cumprod() * start | python | def to_price_index(returns, start=100):
"""
Returns a price index given a series of returns.
Args:
* returns: Expects a return series
* start (number): Starting level
Assumes arithmetic returns.
Formula is: cumprod (1+r)
"""
return (returns.replace(to_replace=np.nan, value=0) + 1).cumprod() * start | [
"def",
"to_price_index",
"(",
"returns",
",",
"start",
"=",
"100",
")",
":",
"return",
"(",
"returns",
".",
"replace",
"(",
"to_replace",
"=",
"np",
".",
"nan",
",",
"value",
"=",
"0",
")",
"+",
"1",
")",
".",
"cumprod",
"(",
")",
"*",
"start"
] | Returns a price index given a series of returns.
Args:
* returns: Expects a return series
* start (number): Starting level
Assumes arithmetic returns.
Formula is: cumprod (1+r) | [
"Returns",
"a",
"price",
"index",
"given",
"a",
"series",
"of",
"returns",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1058-L1070 | train | 199,441 |
pmorissette/ffn | ffn/core.py | calc_stats | def calc_stats(prices):
"""
Calculates performance stats of a given object.
If object is Series, a PerformanceStats object is
returned. If object is DataFrame, a GroupStats object
is returned.
Args:
* prices (Series, DataFrame): Set of prices
"""
if isinstance(prices, pd.Series):
return PerformanceStats(prices)
elif isinstance(prices, pd.DataFrame):
return GroupStats(*[prices[x] for x in prices.columns])
else:
raise NotImplementedError('Unsupported type') | python | def calc_stats(prices):
"""
Calculates performance stats of a given object.
If object is Series, a PerformanceStats object is
returned. If object is DataFrame, a GroupStats object
is returned.
Args:
* prices (Series, DataFrame): Set of prices
"""
if isinstance(prices, pd.Series):
return PerformanceStats(prices)
elif isinstance(prices, pd.DataFrame):
return GroupStats(*[prices[x] for x in prices.columns])
else:
raise NotImplementedError('Unsupported type') | [
"def",
"calc_stats",
"(",
"prices",
")",
":",
"if",
"isinstance",
"(",
"prices",
",",
"pd",
".",
"Series",
")",
":",
"return",
"PerformanceStats",
"(",
"prices",
")",
"elif",
"isinstance",
"(",
"prices",
",",
"pd",
".",
"DataFrame",
")",
":",
"return",
... | Calculates performance stats of a given object.
If object is Series, a PerformanceStats object is
returned. If object is DataFrame, a GroupStats object
is returned.
Args:
* prices (Series, DataFrame): Set of prices | [
"Calculates",
"performance",
"stats",
"of",
"a",
"given",
"object",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1102-L1118 | train | 199,442 |
pmorissette/ffn | ffn/core.py | asfreq_actual | def asfreq_actual(series, freq, method='ffill', how='end', normalize=False):
"""
Similar to pandas' asfreq but keeps the actual dates.
For example, if last data point in Jan is on the 29th,
that date will be used instead of the 31st.
"""
orig = series
is_series = False
if isinstance(series, pd.Series):
is_series = True
name = series.name if series.name else 'data'
orig = pd.DataFrame({name: series})
# add date column
t = pd.concat([orig, pd.DataFrame({'dt': orig.index.values},
index=orig.index.values)], axis=1)
# fetch dates
dts = t.asfreq(freq=freq, method=method, how=how,
normalize=normalize)['dt']
res = orig.loc[dts.values]
if is_series:
return res[name]
else:
return res | python | def asfreq_actual(series, freq, method='ffill', how='end', normalize=False):
"""
Similar to pandas' asfreq but keeps the actual dates.
For example, if last data point in Jan is on the 29th,
that date will be used instead of the 31st.
"""
orig = series
is_series = False
if isinstance(series, pd.Series):
is_series = True
name = series.name if series.name else 'data'
orig = pd.DataFrame({name: series})
# add date column
t = pd.concat([orig, pd.DataFrame({'dt': orig.index.values},
index=orig.index.values)], axis=1)
# fetch dates
dts = t.asfreq(freq=freq, method=method, how=how,
normalize=normalize)['dt']
res = orig.loc[dts.values]
if is_series:
return res[name]
else:
return res | [
"def",
"asfreq_actual",
"(",
"series",
",",
"freq",
",",
"method",
"=",
"'ffill'",
",",
"how",
"=",
"'end'",
",",
"normalize",
"=",
"False",
")",
":",
"orig",
"=",
"series",
"is_series",
"=",
"False",
"if",
"isinstance",
"(",
"series",
",",
"pd",
".",
... | Similar to pandas' asfreq but keeps the actual dates.
For example, if last data point in Jan is on the 29th,
that date will be used instead of the 31st. | [
"Similar",
"to",
"pandas",
"asfreq",
"but",
"keeps",
"the",
"actual",
"dates",
".",
"For",
"example",
"if",
"last",
"data",
"point",
"in",
"Jan",
"is",
"on",
"the",
"29th",
"that",
"date",
"will",
"be",
"used",
"instead",
"of",
"the",
"31st",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1380-L1405 | train | 199,443 |
pmorissette/ffn | ffn/core.py | calc_inv_vol_weights | def calc_inv_vol_weights(returns):
"""
Calculates weights proportional to inverse volatility of each column.
Returns weights that are inversely proportional to the column's
volatility resulting in a set of portfolio weights where each position
has the same level of volatility.
Note, that assets with returns all equal to NaN or 0 are excluded from
the portfolio (their weight is set to NaN).
Returns:
Series {col_name: weight}
"""
# calc vols
vol = np.divide(1., np.std(returns, ddof=1))
vol[np.isinf(vol)] = np.NaN
volsum = vol.sum()
return np.divide(vol, volsum) | python | def calc_inv_vol_weights(returns):
"""
Calculates weights proportional to inverse volatility of each column.
Returns weights that are inversely proportional to the column's
volatility resulting in a set of portfolio weights where each position
has the same level of volatility.
Note, that assets with returns all equal to NaN or 0 are excluded from
the portfolio (their weight is set to NaN).
Returns:
Series {col_name: weight}
"""
# calc vols
vol = np.divide(1., np.std(returns, ddof=1))
vol[np.isinf(vol)] = np.NaN
volsum = vol.sum()
return np.divide(vol, volsum) | [
"def",
"calc_inv_vol_weights",
"(",
"returns",
")",
":",
"# calc vols",
"vol",
"=",
"np",
".",
"divide",
"(",
"1.",
",",
"np",
".",
"std",
"(",
"returns",
",",
"ddof",
"=",
"1",
")",
")",
"vol",
"[",
"np",
".",
"isinf",
"(",
"vol",
")",
"]",
"=",... | Calculates weights proportional to inverse volatility of each column.
Returns weights that are inversely proportional to the column's
volatility resulting in a set of portfolio weights where each position
has the same level of volatility.
Note, that assets with returns all equal to NaN or 0 are excluded from
the portfolio (their weight is set to NaN).
Returns:
Series {col_name: weight} | [
"Calculates",
"weights",
"proportional",
"to",
"inverse",
"volatility",
"of",
"each",
"column",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1408-L1426 | train | 199,444 |
pmorissette/ffn | ffn/core.py | calc_mean_var_weights | def calc_mean_var_weights(returns, weight_bounds=(0., 1.),
rf=0.,
covar_method='ledoit-wolf',
options=None):
"""
Calculates the mean-variance weights given a DataFrame of returns.
Args:
* returns (DataFrame): Returns for multiple securities.
* weight_bounds ((low, high)): Weigh limits for optimization.
* rf (float): `Risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ used in utility calculation
* covar_method (str): Covariance matrix estimation method.
Currently supported:
- `ledoit-wolf <http://www.ledoit.net/honey.pdf>`_
- standard
* options (dict): options for minimizing, e.g. {'maxiter': 10000 }
Returns:
Series {col_name: weight}
"""
def fitness(weights, exp_rets, covar, rf):
# portfolio mean
mean = sum(exp_rets * weights)
# portfolio var
var = np.dot(np.dot(weights, covar), weights)
# utility - i.e. sharpe ratio
util = (mean - rf) / np.sqrt(var)
# negative because we want to maximize and optimizer
# minimizes metric
return -util
n = len(returns.columns)
# expected return defaults to mean return by default
exp_rets = returns.mean()
# calc covariance matrix
if covar_method == 'ledoit-wolf':
covar = sklearn.covariance.ledoit_wolf(returns)[0]
elif covar_method == 'standard':
covar = returns.cov()
else:
raise NotImplementedError('covar_method not implemented')
weights = np.ones([n]) / n
bounds = [weight_bounds for i in range(n)]
# sum of weights must be equal to 1
constraints = ({'type': 'eq', 'fun': lambda W: sum(W) - 1.})
optimized = minimize(fitness, weights, (exp_rets, covar, rf),
method='SLSQP', constraints=constraints,
bounds=bounds, options=options)
# check if success
if not optimized.success:
raise Exception(optimized.message)
# return weight vector
return pd.Series({returns.columns[i]: optimized.x[i] for i in range(n)}) | python | def calc_mean_var_weights(returns, weight_bounds=(0., 1.),
rf=0.,
covar_method='ledoit-wolf',
options=None):
"""
Calculates the mean-variance weights given a DataFrame of returns.
Args:
* returns (DataFrame): Returns for multiple securities.
* weight_bounds ((low, high)): Weigh limits for optimization.
* rf (float): `Risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ used in utility calculation
* covar_method (str): Covariance matrix estimation method.
Currently supported:
- `ledoit-wolf <http://www.ledoit.net/honey.pdf>`_
- standard
* options (dict): options for minimizing, e.g. {'maxiter': 10000 }
Returns:
Series {col_name: weight}
"""
def fitness(weights, exp_rets, covar, rf):
# portfolio mean
mean = sum(exp_rets * weights)
# portfolio var
var = np.dot(np.dot(weights, covar), weights)
# utility - i.e. sharpe ratio
util = (mean - rf) / np.sqrt(var)
# negative because we want to maximize and optimizer
# minimizes metric
return -util
n = len(returns.columns)
# expected return defaults to mean return by default
exp_rets = returns.mean()
# calc covariance matrix
if covar_method == 'ledoit-wolf':
covar = sklearn.covariance.ledoit_wolf(returns)[0]
elif covar_method == 'standard':
covar = returns.cov()
else:
raise NotImplementedError('covar_method not implemented')
weights = np.ones([n]) / n
bounds = [weight_bounds for i in range(n)]
# sum of weights must be equal to 1
constraints = ({'type': 'eq', 'fun': lambda W: sum(W) - 1.})
optimized = minimize(fitness, weights, (exp_rets, covar, rf),
method='SLSQP', constraints=constraints,
bounds=bounds, options=options)
# check if success
if not optimized.success:
raise Exception(optimized.message)
# return weight vector
return pd.Series({returns.columns[i]: optimized.x[i] for i in range(n)}) | [
"def",
"calc_mean_var_weights",
"(",
"returns",
",",
"weight_bounds",
"=",
"(",
"0.",
",",
"1.",
")",
",",
"rf",
"=",
"0.",
",",
"covar_method",
"=",
"'ledoit-wolf'",
",",
"options",
"=",
"None",
")",
":",
"def",
"fitness",
"(",
"weights",
",",
"exp_rets... | Calculates the mean-variance weights given a DataFrame of returns.
Args:
* returns (DataFrame): Returns for multiple securities.
* weight_bounds ((low, high)): Weigh limits for optimization.
* rf (float): `Risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ used in utility calculation
* covar_method (str): Covariance matrix estimation method.
Currently supported:
- `ledoit-wolf <http://www.ledoit.net/honey.pdf>`_
- standard
* options (dict): options for minimizing, e.g. {'maxiter': 10000 }
Returns:
Series {col_name: weight} | [
"Calculates",
"the",
"mean",
"-",
"variance",
"weights",
"given",
"a",
"DataFrame",
"of",
"returns",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1429-L1486 | train | 199,445 |
pmorissette/ffn | ffn/core.py | get_num_days_required | def get_num_days_required(offset, period='d', perc_required=0.90):
"""
Estimates the number of days required to assume that data is OK.
Helper function used to determine if there are enough "good" data
days over a given period.
Args:
* offset (DateOffset): Offset (lookback) period.
* period (str): Period string.
* perc_required (float): percentage of number of days
expected required.
"""
x = pd.to_datetime('2010-01-01')
delta = x - (x - offset)
# convert to 'trading days' - rough guestimate
days = delta.days * 0.69
if period == 'd':
req = days * perc_required
elif period == 'm':
req = (days / 20) * perc_required
elif period == 'y':
req = (days / 252) * perc_required
else:
raise NotImplementedError(
'period not supported. Supported periods are d, m, y')
return req | python | def get_num_days_required(offset, period='d', perc_required=0.90):
"""
Estimates the number of days required to assume that data is OK.
Helper function used to determine if there are enough "good" data
days over a given period.
Args:
* offset (DateOffset): Offset (lookback) period.
* period (str): Period string.
* perc_required (float): percentage of number of days
expected required.
"""
x = pd.to_datetime('2010-01-01')
delta = x - (x - offset)
# convert to 'trading days' - rough guestimate
days = delta.days * 0.69
if period == 'd':
req = days * perc_required
elif period == 'm':
req = (days / 20) * perc_required
elif period == 'y':
req = (days / 252) * perc_required
else:
raise NotImplementedError(
'period not supported. Supported periods are d, m, y')
return req | [
"def",
"get_num_days_required",
"(",
"offset",
",",
"period",
"=",
"'d'",
",",
"perc_required",
"=",
"0.90",
")",
":",
"x",
"=",
"pd",
".",
"to_datetime",
"(",
"'2010-01-01'",
")",
"delta",
"=",
"x",
"-",
"(",
"x",
"-",
"offset",
")",
"# convert to 'trad... | Estimates the number of days required to assume that data is OK.
Helper function used to determine if there are enough "good" data
days over a given period.
Args:
* offset (DateOffset): Offset (lookback) period.
* period (str): Period string.
* perc_required (float): percentage of number of days
expected required. | [
"Estimates",
"the",
"number",
"of",
"days",
"required",
"to",
"assume",
"that",
"data",
"is",
"OK",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1698-L1727 | train | 199,446 |
pmorissette/ffn | ffn/core.py | calc_clusters | def calc_clusters(returns, n=None, plot=False):
"""
Calculates the clusters based on k-means
clustering.
Args:
* returns (pd.DataFrame): DataFrame of returns
* n (int): Specify # of clusters. If None, this
will be automatically determined
* plot (bool): Show plot?
Returns:
* dict with structure: {cluster# : [col names]}
"""
# calculate correlation
corr = returns.corr()
# calculate dissimilarity matrix
diss = 1 - corr
# scale down to 2 dimensions using MDS
# (multi-dimensional scaling) using the
# dissimilarity matrix
mds = sklearn.manifold.MDS(dissimilarity='precomputed')
xy = mds.fit_transform(diss)
def routine(k):
# fit KMeans
km = sklearn.cluster.KMeans(n_clusters=k)
km_fit = km.fit(xy)
labels = km_fit.labels_
centers = km_fit.cluster_centers_
# get {ticker: label} mappings
mappings = dict(zip(returns.columns, labels))
# print % of var explained
totss = 0
withinss = 0
# column average fot totss
avg = np.array([np.mean(xy[:, 0]), np.mean(xy[:, 1])])
for idx, lbl in enumerate(labels):
withinss += sum((xy[idx] - centers[lbl]) ** 2)
totss += sum((xy[idx] - avg) ** 2)
pvar_expl = 1.0 - withinss / totss
return mappings, pvar_expl, labels
if n:
result = routine(n)
else:
n = len(returns.columns)
n1 = int(np.ceil(n * 0.6666666666))
for i in range(2, n1 + 1):
result = routine(i)
if result[1] > 0.9:
break
if plot:
fig, ax = plt.subplots()
ax.scatter(xy[:, 0], xy[:, 1], c=result[2], s=90)
for i, txt in enumerate(returns.columns):
ax.annotate(txt, (xy[i, 0], xy[i, 1]), size=14)
# sanitize return value
tmp = result[0]
# map as such {cluster: [list of tickers], cluster2: [...]}
inv_map = {}
for k, v in iteritems(tmp):
inv_map[v] = inv_map.get(v, [])
inv_map[v].append(k)
return inv_map | python | def calc_clusters(returns, n=None, plot=False):
"""
Calculates the clusters based on k-means
clustering.
Args:
* returns (pd.DataFrame): DataFrame of returns
* n (int): Specify # of clusters. If None, this
will be automatically determined
* plot (bool): Show plot?
Returns:
* dict with structure: {cluster# : [col names]}
"""
# calculate correlation
corr = returns.corr()
# calculate dissimilarity matrix
diss = 1 - corr
# scale down to 2 dimensions using MDS
# (multi-dimensional scaling) using the
# dissimilarity matrix
mds = sklearn.manifold.MDS(dissimilarity='precomputed')
xy = mds.fit_transform(diss)
def routine(k):
# fit KMeans
km = sklearn.cluster.KMeans(n_clusters=k)
km_fit = km.fit(xy)
labels = km_fit.labels_
centers = km_fit.cluster_centers_
# get {ticker: label} mappings
mappings = dict(zip(returns.columns, labels))
# print % of var explained
totss = 0
withinss = 0
# column average fot totss
avg = np.array([np.mean(xy[:, 0]), np.mean(xy[:, 1])])
for idx, lbl in enumerate(labels):
withinss += sum((xy[idx] - centers[lbl]) ** 2)
totss += sum((xy[idx] - avg) ** 2)
pvar_expl = 1.0 - withinss / totss
return mappings, pvar_expl, labels
if n:
result = routine(n)
else:
n = len(returns.columns)
n1 = int(np.ceil(n * 0.6666666666))
for i in range(2, n1 + 1):
result = routine(i)
if result[1] > 0.9:
break
if plot:
fig, ax = plt.subplots()
ax.scatter(xy[:, 0], xy[:, 1], c=result[2], s=90)
for i, txt in enumerate(returns.columns):
ax.annotate(txt, (xy[i, 0], xy[i, 1]), size=14)
# sanitize return value
tmp = result[0]
# map as such {cluster: [list of tickers], cluster2: [...]}
inv_map = {}
for k, v in iteritems(tmp):
inv_map[v] = inv_map.get(v, [])
inv_map[v].append(k)
return inv_map | [
"def",
"calc_clusters",
"(",
"returns",
",",
"n",
"=",
"None",
",",
"plot",
"=",
"False",
")",
":",
"# calculate correlation",
"corr",
"=",
"returns",
".",
"corr",
"(",
")",
"# calculate dissimilarity matrix",
"diss",
"=",
"1",
"-",
"corr",
"# scale down to 2 ... | Calculates the clusters based on k-means
clustering.
Args:
* returns (pd.DataFrame): DataFrame of returns
* n (int): Specify # of clusters. If None, this
will be automatically determined
* plot (bool): Show plot?
Returns:
* dict with structure: {cluster# : [col names]} | [
"Calculates",
"the",
"clusters",
"based",
"on",
"k",
"-",
"means",
"clustering",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1730-L1802 | train | 199,447 |
pmorissette/ffn | ffn/core.py | limit_weights | def limit_weights(weights, limit=0.1):
"""
Limits weights and redistributes excedent amount
proportionally.
ex:
- weights are {a: 0.7, b: 0.2, c: 0.1}
- call with limit=0.5
- excess 0.2 in a is ditributed to b and c
proportionally.
- result is {a: 0.5, b: 0.33, c: 0.167}
Args:
* weights (Series): A series describing the weights
* limit (float): Maximum weight allowed
"""
if 1.0 / limit > len(weights):
raise ValueError('invalid limit -> 1 / limit must be <= len(weights)')
if isinstance(weights, dict):
weights = pd.Series(weights)
if np.round(weights.sum(), 1) != 1.0:
raise ValueError('Expecting weights (that sum to 1) - sum is %s'
% weights.sum())
res = np.round(weights.copy(), 4)
to_rebalance = (res[res > limit] - limit).sum()
ok = res[res < limit]
ok += (ok / ok.sum()) * to_rebalance
res[res > limit] = limit
res[res < limit] = ok
if any(x > limit for x in res):
return limit_weights(res, limit=limit)
return res | python | def limit_weights(weights, limit=0.1):
"""
Limits weights and redistributes excedent amount
proportionally.
ex:
- weights are {a: 0.7, b: 0.2, c: 0.1}
- call with limit=0.5
- excess 0.2 in a is ditributed to b and c
proportionally.
- result is {a: 0.5, b: 0.33, c: 0.167}
Args:
* weights (Series): A series describing the weights
* limit (float): Maximum weight allowed
"""
if 1.0 / limit > len(weights):
raise ValueError('invalid limit -> 1 / limit must be <= len(weights)')
if isinstance(weights, dict):
weights = pd.Series(weights)
if np.round(weights.sum(), 1) != 1.0:
raise ValueError('Expecting weights (that sum to 1) - sum is %s'
% weights.sum())
res = np.round(weights.copy(), 4)
to_rebalance = (res[res > limit] - limit).sum()
ok = res[res < limit]
ok += (ok / ok.sum()) * to_rebalance
res[res > limit] = limit
res[res < limit] = ok
if any(x > limit for x in res):
return limit_weights(res, limit=limit)
return res | [
"def",
"limit_weights",
"(",
"weights",
",",
"limit",
"=",
"0.1",
")",
":",
"if",
"1.0",
"/",
"limit",
">",
"len",
"(",
"weights",
")",
":",
"raise",
"ValueError",
"(",
"'invalid limit -> 1 / limit must be <= len(weights)'",
")",
"if",
"isinstance",
"(",
"weig... | Limits weights and redistributes excedent amount
proportionally.
ex:
- weights are {a: 0.7, b: 0.2, c: 0.1}
- call with limit=0.5
- excess 0.2 in a is ditributed to b and c
proportionally.
- result is {a: 0.5, b: 0.33, c: 0.167}
Args:
* weights (Series): A series describing the weights
* limit (float): Maximum weight allowed | [
"Limits",
"weights",
"and",
"redistributes",
"excedent",
"amount",
"proportionally",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1899-L1937 | train | 199,448 |
pmorissette/ffn | ffn/core.py | random_weights | def random_weights(n, bounds=(0., 1.), total=1.0):
"""
Generate pseudo-random weights.
Returns a list of random weights that is of length
n, where each weight is in the range bounds, and
where the weights sum up to total.
Useful for creating random portfolios when benchmarking.
Args:
* n (int): number of random weights
* bounds ((low, high)): bounds for each weight
* total (float): total sum of the weights
"""
low = bounds[0]
high = bounds[1]
if high < low:
raise ValueError('Higher bound must be greater or '
'equal to lower bound')
if n * high < total or n * low > total:
raise ValueError('solution not possible with given n and bounds')
w = [0] * n
tgt = -float(total)
for i in range(n):
rn = n - i - 1
rhigh = rn * high
rlow = rn * low
lowb = max(-rhigh - tgt, low)
highb = min(-rlow - tgt, high)
rw = random.uniform(lowb, highb)
w[i] = rw
tgt += rw
random.shuffle(w)
return w | python | def random_weights(n, bounds=(0., 1.), total=1.0):
"""
Generate pseudo-random weights.
Returns a list of random weights that is of length
n, where each weight is in the range bounds, and
where the weights sum up to total.
Useful for creating random portfolios when benchmarking.
Args:
* n (int): number of random weights
* bounds ((low, high)): bounds for each weight
* total (float): total sum of the weights
"""
low = bounds[0]
high = bounds[1]
if high < low:
raise ValueError('Higher bound must be greater or '
'equal to lower bound')
if n * high < total or n * low > total:
raise ValueError('solution not possible with given n and bounds')
w = [0] * n
tgt = -float(total)
for i in range(n):
rn = n - i - 1
rhigh = rn * high
rlow = rn * low
lowb = max(-rhigh - tgt, low)
highb = min(-rlow - tgt, high)
rw = random.uniform(lowb, highb)
w[i] = rw
tgt += rw
random.shuffle(w)
return w | [
"def",
"random_weights",
"(",
"n",
",",
"bounds",
"=",
"(",
"0.",
",",
"1.",
")",
",",
"total",
"=",
"1.0",
")",
":",
"low",
"=",
"bounds",
"[",
"0",
"]",
"high",
"=",
"bounds",
"[",
"1",
"]",
"if",
"high",
"<",
"low",
":",
"raise",
"ValueError... | Generate pseudo-random weights.
Returns a list of random weights that is of length
n, where each weight is in the range bounds, and
where the weights sum up to total.
Useful for creating random portfolios when benchmarking.
Args:
* n (int): number of random weights
* bounds ((low, high)): bounds for each weight
* total (float): total sum of the weights | [
"Generate",
"pseudo",
"-",
"random",
"weights",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1940-L1983 | train | 199,449 |
pmorissette/ffn | ffn/core.py | plot_heatmap | def plot_heatmap(data, title='Heatmap', show_legend=True,
show_labels=True, label_fmt='.2f',
vmin=None, vmax=None,
figsize=None, label_color='w',
cmap='RdBu', **kwargs):
"""
Plot a heatmap using matplotlib's pcolor.
Args:
* data (DataFrame): DataFrame to plot. Usually small matrix (ex.
correlation matrix).
* title (string): Plot title
* show_legend (bool): Show color legend
* show_labels (bool): Show value labels
* label_fmt (str): Label format string
* vmin (float): Min value for scale
* vmax (float): Max value for scale
* cmap (string): Color map
* kwargs: Passed to matplotlib's pcolor
"""
fig, ax = plt.subplots(figsize=figsize)
heatmap = ax.pcolor(data, vmin=vmin, vmax=vmax, cmap=cmap)
# for some reason heatmap has the y values backwards....
ax.invert_yaxis()
if title is not None:
plt.title(title)
if show_legend:
fig.colorbar(heatmap)
if show_labels:
vals = data.values
for x in range(data.shape[0]):
for y in range(data.shape[1]):
plt.text(x + 0.5, y + 0.5, format(vals[y, x], label_fmt),
horizontalalignment='center',
verticalalignment='center',
color=label_color)
plt.yticks(np.arange(0.5, len(data.index), 1), data.index)
plt.xticks(np.arange(0.5, len(data.columns), 1), data.columns)
return plt | python | def plot_heatmap(data, title='Heatmap', show_legend=True,
show_labels=True, label_fmt='.2f',
vmin=None, vmax=None,
figsize=None, label_color='w',
cmap='RdBu', **kwargs):
"""
Plot a heatmap using matplotlib's pcolor.
Args:
* data (DataFrame): DataFrame to plot. Usually small matrix (ex.
correlation matrix).
* title (string): Plot title
* show_legend (bool): Show color legend
* show_labels (bool): Show value labels
* label_fmt (str): Label format string
* vmin (float): Min value for scale
* vmax (float): Max value for scale
* cmap (string): Color map
* kwargs: Passed to matplotlib's pcolor
"""
fig, ax = plt.subplots(figsize=figsize)
heatmap = ax.pcolor(data, vmin=vmin, vmax=vmax, cmap=cmap)
# for some reason heatmap has the y values backwards....
ax.invert_yaxis()
if title is not None:
plt.title(title)
if show_legend:
fig.colorbar(heatmap)
if show_labels:
vals = data.values
for x in range(data.shape[0]):
for y in range(data.shape[1]):
plt.text(x + 0.5, y + 0.5, format(vals[y, x], label_fmt),
horizontalalignment='center',
verticalalignment='center',
color=label_color)
plt.yticks(np.arange(0.5, len(data.index), 1), data.index)
plt.xticks(np.arange(0.5, len(data.columns), 1), data.columns)
return plt | [
"def",
"plot_heatmap",
"(",
"data",
",",
"title",
"=",
"'Heatmap'",
",",
"show_legend",
"=",
"True",
",",
"show_labels",
"=",
"True",
",",
"label_fmt",
"=",
"'.2f'",
",",
"vmin",
"=",
"None",
",",
"vmax",
"=",
"None",
",",
"figsize",
"=",
"None",
",",
... | Plot a heatmap using matplotlib's pcolor.
Args:
* data (DataFrame): DataFrame to plot. Usually small matrix (ex.
correlation matrix).
* title (string): Plot title
* show_legend (bool): Show color legend
* show_labels (bool): Show value labels
* label_fmt (str): Label format string
* vmin (float): Min value for scale
* vmax (float): Max value for scale
* cmap (string): Color map
* kwargs: Passed to matplotlib's pcolor | [
"Plot",
"a",
"heatmap",
"using",
"matplotlib",
"s",
"pcolor",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L1986-L2031 | train | 199,450 |
pmorissette/ffn | ffn/core.py | rollapply | def rollapply(data, window, fn):
"""
Apply a function fn over a rolling window of size window.
Args:
* data (Series or DataFrame): Series or DataFrame
* window (int): Window size
* fn (function): Function to apply over the rolling window.
For a series, the return value is expected to be a single
number. For a DataFrame, it shuold return a new row.
Returns:
* Object of same dimensions as data
"""
res = data.copy()
res[:] = np.nan
n = len(data)
if window > n:
return res
for i in range(window - 1, n):
res.iloc[i] = fn(data.iloc[i - window + 1:i + 1])
return res | python | def rollapply(data, window, fn):
"""
Apply a function fn over a rolling window of size window.
Args:
* data (Series or DataFrame): Series or DataFrame
* window (int): Window size
* fn (function): Function to apply over the rolling window.
For a series, the return value is expected to be a single
number. For a DataFrame, it shuold return a new row.
Returns:
* Object of same dimensions as data
"""
res = data.copy()
res[:] = np.nan
n = len(data)
if window > n:
return res
for i in range(window - 1, n):
res.iloc[i] = fn(data.iloc[i - window + 1:i + 1])
return res | [
"def",
"rollapply",
"(",
"data",
",",
"window",
",",
"fn",
")",
":",
"res",
"=",
"data",
".",
"copy",
"(",
")",
"res",
"[",
":",
"]",
"=",
"np",
".",
"nan",
"n",
"=",
"len",
"(",
"data",
")",
"if",
"window",
">",
"n",
":",
"return",
"res",
... | Apply a function fn over a rolling window of size window.
Args:
* data (Series or DataFrame): Series or DataFrame
* window (int): Window size
* fn (function): Function to apply over the rolling window.
For a series, the return value is expected to be a single
number. For a DataFrame, it shuold return a new row.
Returns:
* Object of same dimensions as data | [
"Apply",
"a",
"function",
"fn",
"over",
"a",
"rolling",
"window",
"of",
"size",
"window",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L2041-L2065 | train | 199,451 |
pmorissette/ffn | ffn/core.py | _winsorize_wrapper | def _winsorize_wrapper(x, limits):
"""
Wraps scipy winsorize function to drop na's
"""
if isinstance(x, pd.Series):
if x.count() == 0:
return x
notnanx = ~np.isnan(x)
x[notnanx] = scipy.stats.mstats.winsorize(x[notnanx],
limits=limits)
return x
else:
return scipy.stats.mstats.winsorize(x, limits=limits) | python | def _winsorize_wrapper(x, limits):
"""
Wraps scipy winsorize function to drop na's
"""
if isinstance(x, pd.Series):
if x.count() == 0:
return x
notnanx = ~np.isnan(x)
x[notnanx] = scipy.stats.mstats.winsorize(x[notnanx],
limits=limits)
return x
else:
return scipy.stats.mstats.winsorize(x, limits=limits) | [
"def",
"_winsorize_wrapper",
"(",
"x",
",",
"limits",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"pd",
".",
"Series",
")",
":",
"if",
"x",
".",
"count",
"(",
")",
"==",
"0",
":",
"return",
"x",
"notnanx",
"=",
"~",
"np",
".",
"isnan",
"(",
"x... | Wraps scipy winsorize function to drop na's | [
"Wraps",
"scipy",
"winsorize",
"function",
"to",
"drop",
"na",
"s"
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L2068-L2081 | train | 199,452 |
pmorissette/ffn | ffn/core.py | to_excess_returns | def to_excess_returns(returns, rf, nperiods=None):
"""
Given a series of returns, it will return the excess returns over rf.
Args:
* returns (Series, DataFrame): Returns
* rf (float, Series): `Risk-Free rate(s) <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ expressed in annualized term or return series
* nperiods (int): Optional. If provided, will convert rf to different
frequency using deannualize only if rf is a float
Returns:
* excess_returns (Series, DataFrame): Returns - rf
"""
if type(rf) is float and nperiods is not None:
_rf = deannualize(rf, nperiods)
else:
_rf = rf
return returns - _rf | python | def to_excess_returns(returns, rf, nperiods=None):
"""
Given a series of returns, it will return the excess returns over rf.
Args:
* returns (Series, DataFrame): Returns
* rf (float, Series): `Risk-Free rate(s) <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ expressed in annualized term or return series
* nperiods (int): Optional. If provided, will convert rf to different
frequency using deannualize only if rf is a float
Returns:
* excess_returns (Series, DataFrame): Returns - rf
"""
if type(rf) is float and nperiods is not None:
_rf = deannualize(rf, nperiods)
else:
_rf = rf
return returns - _rf | [
"def",
"to_excess_returns",
"(",
"returns",
",",
"rf",
",",
"nperiods",
"=",
"None",
")",
":",
"if",
"type",
"(",
"rf",
")",
"is",
"float",
"and",
"nperiods",
"is",
"not",
"None",
":",
"_rf",
"=",
"deannualize",
"(",
"rf",
",",
"nperiods",
")",
"else... | Given a series of returns, it will return the excess returns over rf.
Args:
* returns (Series, DataFrame): Returns
* rf (float, Series): `Risk-Free rate(s) <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ expressed in annualized term or return series
* nperiods (int): Optional. If provided, will convert rf to different
frequency using deannualize only if rf is a float
Returns:
* excess_returns (Series, DataFrame): Returns - rf | [
"Given",
"a",
"series",
"of",
"returns",
"it",
"will",
"return",
"the",
"excess",
"returns",
"over",
"rf",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L2163-L2181 | train | 199,453 |
pmorissette/ffn | ffn/core.py | resample_returns | def resample_returns(
returns,
func,
seed=0,
num_trials=100
):
"""
Resample the returns and calculate any statistic on every new sample.
https://en.wikipedia.org/wiki/Resampling_(statistics)
:param returns (Series, DataFrame): Returns
:param func: Given the resampled returns calculate a statistic
:param seed: Seed for random number generator
:param num_trials: Number of times to resample and run the experiment
:return: Series of resampled statistics
"""
# stats = []
if type(returns) is pd.Series:
stats = pd.Series(index=range(num_trials))
elif type(returns) is pd.DataFrame:
stats = pd.DataFrame(
index=range(num_trials),
columns=returns.columns
)
else:
raise(TypeError("returns needs to be a Series or DataFrame!"))
n = returns.shape[0]
for i in range(num_trials):
random_indices = resample(returns.index, n_samples=n, random_state=seed + i)
stats.loc[i] = func(returns.loc[random_indices])
return stats | python | def resample_returns(
returns,
func,
seed=0,
num_trials=100
):
"""
Resample the returns and calculate any statistic on every new sample.
https://en.wikipedia.org/wiki/Resampling_(statistics)
:param returns (Series, DataFrame): Returns
:param func: Given the resampled returns calculate a statistic
:param seed: Seed for random number generator
:param num_trials: Number of times to resample and run the experiment
:return: Series of resampled statistics
"""
# stats = []
if type(returns) is pd.Series:
stats = pd.Series(index=range(num_trials))
elif type(returns) is pd.DataFrame:
stats = pd.DataFrame(
index=range(num_trials),
columns=returns.columns
)
else:
raise(TypeError("returns needs to be a Series or DataFrame!"))
n = returns.shape[0]
for i in range(num_trials):
random_indices = resample(returns.index, n_samples=n, random_state=seed + i)
stats.loc[i] = func(returns.loc[random_indices])
return stats | [
"def",
"resample_returns",
"(",
"returns",
",",
"func",
",",
"seed",
"=",
"0",
",",
"num_trials",
"=",
"100",
")",
":",
"# stats = []",
"if",
"type",
"(",
"returns",
")",
"is",
"pd",
".",
"Series",
":",
"stats",
"=",
"pd",
".",
"Series",
"(",
"index"... | Resample the returns and calculate any statistic on every new sample.
https://en.wikipedia.org/wiki/Resampling_(statistics)
:param returns (Series, DataFrame): Returns
:param func: Given the resampled returns calculate a statistic
:param seed: Seed for random number generator
:param num_trials: Number of times to resample and run the experiment
:return: Series of resampled statistics | [
"Resample",
"the",
"returns",
"and",
"calculate",
"any",
"statistic",
"on",
"every",
"new",
"sample",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L2230-L2264 | train | 199,454 |
pmorissette/ffn | ffn/core.py | PerformanceStats.set_riskfree_rate | def set_riskfree_rate(self, rf):
"""
Set annual risk-free rate property and calculate properly annualized
monthly and daily rates. Then performance stats are recalculated.
Affects only this instance of the PerformanceStats.
Args:
* rf (float): Annual `risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_
"""
self.rf = rf
# Note, that we recalculate everything.
self._update(self.prices) | python | def set_riskfree_rate(self, rf):
"""
Set annual risk-free rate property and calculate properly annualized
monthly and daily rates. Then performance stats are recalculated.
Affects only this instance of the PerformanceStats.
Args:
* rf (float): Annual `risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_
"""
self.rf = rf
# Note, that we recalculate everything.
self._update(self.prices) | [
"def",
"set_riskfree_rate",
"(",
"self",
",",
"rf",
")",
":",
"self",
".",
"rf",
"=",
"rf",
"# Note, that we recalculate everything.",
"self",
".",
"_update",
"(",
"self",
".",
"prices",
")"
] | Set annual risk-free rate property and calculate properly annualized
monthly and daily rates. Then performance stats are recalculated.
Affects only this instance of the PerformanceStats.
Args:
* rf (float): Annual `risk-free rate <https://www.investopedia.com/terms/r/risk-freerate.asp>`_ | [
"Set",
"annual",
"risk",
"-",
"free",
"rate",
"property",
"and",
"calculate",
"properly",
"annualized",
"monthly",
"and",
"daily",
"rates",
".",
"Then",
"performance",
"stats",
"are",
"recalculated",
".",
"Affects",
"only",
"this",
"instance",
"of",
"the",
"Pe... | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L67-L80 | train | 199,455 |
pmorissette/ffn | ffn/core.py | PerformanceStats.display_monthly_returns | def display_monthly_returns(self):
"""
Display a table containing monthly returns and ytd returns
for every year in range.
"""
data = [['Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'YTD']]
for k in self.return_table.index:
r = self.return_table.loc[k].values
data.append([k] + [fmtpn(x) for x in r])
print(tabulate(data, headers='firstrow')) | python | def display_monthly_returns(self):
"""
Display a table containing monthly returns and ytd returns
for every year in range.
"""
data = [['Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'YTD']]
for k in self.return_table.index:
r = self.return_table.loc[k].values
data.append([k] + [fmtpn(x) for x in r])
print(tabulate(data, headers='firstrow')) | [
"def",
"display_monthly_returns",
"(",
"self",
")",
":",
"data",
"=",
"[",
"[",
"'Year'",
",",
"'Jan'",
",",
"'Feb'",
",",
"'Mar'",
",",
"'Apr'",
",",
"'May'",
",",
"'Jun'",
",",
"'Jul'",
",",
"'Aug'",
",",
"'Sep'",
",",
"'Oct'",
",",
"'Nov'",
",",
... | Display a table containing monthly returns and ytd returns
for every year in range. | [
"Display",
"a",
"table",
"containing",
"monthly",
"returns",
"and",
"ytd",
"returns",
"for",
"every",
"year",
"in",
"range",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L513-L523 | train | 199,456 |
pmorissette/ffn | ffn/core.py | PerformanceStats.plot_histogram | def plot_histogram(self, freq=None, figsize=(15, 5), title=None,
bins=20, **kwargs):
"""
Plots a histogram of returns given a return frequency.
Args:
* freq (str): Data frequency used for display purposes.
This will dictate the type of returns
(daily returns, monthly, ...)
Refer to pandas docs for valid period strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* bins (int): number of bins for the histogram
* kwargs: passed to pandas' hist method
"""
if title is None:
title = self._get_default_plot_title(
self.name, freq, 'Return Histogram')
ser = self._get_series(freq).to_returns().dropna()
plt.figure(figsize=figsize)
ax = ser.hist(bins=bins, figsize=figsize, normed=True, **kwargs)
ax.set_title(title)
plt.axvline(0, linewidth=4)
return ser.plot(kind='kde') | python | def plot_histogram(self, freq=None, figsize=(15, 5), title=None,
bins=20, **kwargs):
"""
Plots a histogram of returns given a return frequency.
Args:
* freq (str): Data frequency used for display purposes.
This will dictate the type of returns
(daily returns, monthly, ...)
Refer to pandas docs for valid period strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* bins (int): number of bins for the histogram
* kwargs: passed to pandas' hist method
"""
if title is None:
title = self._get_default_plot_title(
self.name, freq, 'Return Histogram')
ser = self._get_series(freq).to_returns().dropna()
plt.figure(figsize=figsize)
ax = ser.hist(bins=bins, figsize=figsize, normed=True, **kwargs)
ax.set_title(title)
plt.axvline(0, linewidth=4)
return ser.plot(kind='kde') | [
"def",
"plot_histogram",
"(",
"self",
",",
"freq",
"=",
"None",
",",
"figsize",
"=",
"(",
"15",
",",
"5",
")",
",",
"title",
"=",
"None",
",",
"bins",
"=",
"20",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"title",
"is",
"None",
":",
"title",
"=",... | Plots a histogram of returns given a return frequency.
Args:
* freq (str): Data frequency used for display purposes.
This will dictate the type of returns
(daily returns, monthly, ...)
Refer to pandas docs for valid period strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* bins (int): number of bins for the histogram
* kwargs: passed to pandas' hist method | [
"Plots",
"a",
"histogram",
"of",
"returns",
"given",
"a",
"return",
"frequency",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L557-L582 | train | 199,457 |
pmorissette/ffn | ffn/core.py | GroupStats.set_date_range | def set_date_range(self, start=None, end=None):
"""
Update date range of stats, charts, etc. If None then
the original date range is used. So to reset to the original
range, just call with no args.
Args:
* start (date): start date
* end (end): end date
"""
start = self._start if start is None else pd.to_datetime(start)
end = self._end if end is None else pd.to_datetime(end)
self._update(self._prices.loc[start:end]) | python | def set_date_range(self, start=None, end=None):
"""
Update date range of stats, charts, etc. If None then
the original date range is used. So to reset to the original
range, just call with no args.
Args:
* start (date): start date
* end (end): end date
"""
start = self._start if start is None else pd.to_datetime(start)
end = self._end if end is None else pd.to_datetime(end)
self._update(self._prices.loc[start:end]) | [
"def",
"set_date_range",
"(",
"self",
",",
"start",
"=",
"None",
",",
"end",
"=",
"None",
")",
":",
"start",
"=",
"self",
".",
"_start",
"if",
"start",
"is",
"None",
"else",
"pd",
".",
"to_datetime",
"(",
"start",
")",
"end",
"=",
"self",
".",
"_en... | Update date range of stats, charts, etc. If None then
the original date range is used. So to reset to the original
range, just call with no args.
Args:
* start (date): start date
* end (end): end date | [
"Update",
"date",
"range",
"of",
"stats",
"charts",
"etc",
".",
"If",
"None",
"then",
"the",
"original",
"date",
"range",
"is",
"used",
".",
"So",
"to",
"reset",
"to",
"the",
"original",
"range",
"just",
"call",
"with",
"no",
"args",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L826-L838 | train | 199,458 |
pmorissette/ffn | ffn/core.py | GroupStats.display | def display(self):
"""
Display summary stats table.
"""
data = []
first_row = ['Stat']
first_row.extend(self._names)
data.append(first_row)
stats = self._stats()
for stat in stats:
k, n, f = stat
# blank row
if k is None:
row = [''] * len(data[0])
data.append(row)
continue
row = [n]
for key in self._names:
raw = getattr(self[key], k)
# if rf is a series print nan
if k == 'rf' and not type(raw) == float:
row.append(np.nan)
elif f is None:
row.append(raw)
elif f == 'p':
row.append(fmtp(raw))
elif f == 'n':
row.append(fmtn(raw))
elif f == 'dt':
row.append(raw.strftime('%Y-%m-%d'))
else:
raise NotImplementedError('unsupported format %s' % f)
data.append(row)
print(tabulate(data, headers='firstrow')) | python | def display(self):
"""
Display summary stats table.
"""
data = []
first_row = ['Stat']
first_row.extend(self._names)
data.append(first_row)
stats = self._stats()
for stat in stats:
k, n, f = stat
# blank row
if k is None:
row = [''] * len(data[0])
data.append(row)
continue
row = [n]
for key in self._names:
raw = getattr(self[key], k)
# if rf is a series print nan
if k == 'rf' and not type(raw) == float:
row.append(np.nan)
elif f is None:
row.append(raw)
elif f == 'p':
row.append(fmtp(raw))
elif f == 'n':
row.append(fmtn(raw))
elif f == 'dt':
row.append(raw.strftime('%Y-%m-%d'))
else:
raise NotImplementedError('unsupported format %s' % f)
data.append(row)
print(tabulate(data, headers='firstrow')) | [
"def",
"display",
"(",
"self",
")",
":",
"data",
"=",
"[",
"]",
"first_row",
"=",
"[",
"'Stat'",
"]",
"first_row",
".",
"extend",
"(",
"self",
".",
"_names",
")",
"data",
".",
"append",
"(",
"first_row",
")",
"stats",
"=",
"self",
".",
"_stats",
"(... | Display summary stats table. | [
"Display",
"summary",
"stats",
"table",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L840-L878 | train | 199,459 |
pmorissette/ffn | ffn/core.py | GroupStats.display_lookback_returns | def display_lookback_returns(self):
"""
Displays the current lookback returns for each series.
"""
return self.lookback_returns.apply(
lambda x: x.map('{:,.2%}'.format), axis=1) | python | def display_lookback_returns(self):
"""
Displays the current lookback returns for each series.
"""
return self.lookback_returns.apply(
lambda x: x.map('{:,.2%}'.format), axis=1) | [
"def",
"display_lookback_returns",
"(",
"self",
")",
":",
"return",
"self",
".",
"lookback_returns",
".",
"apply",
"(",
"lambda",
"x",
":",
"x",
".",
"map",
"(",
"'{:,.2%}'",
".",
"format",
")",
",",
"axis",
"=",
"1",
")"
] | Displays the current lookback returns for each series. | [
"Displays",
"the",
"current",
"lookback",
"returns",
"for",
"each",
"series",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L880-L885 | train | 199,460 |
pmorissette/ffn | ffn/core.py | GroupStats.plot | def plot(self, freq=None, figsize=(15, 5), title=None,
logy=False, **kwargs):
"""
Helper function for plotting the series.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* logy (bool): log-scale for y axis
* kwargs: passed to pandas' plot method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Equity Progression')
ser = self._get_series(freq).rebase()
return ser.plot(figsize=figsize, logy=logy,
title=title, **kwargs) | python | def plot(self, freq=None, figsize=(15, 5), title=None,
logy=False, **kwargs):
"""
Helper function for plotting the series.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* logy (bool): log-scale for y axis
* kwargs: passed to pandas' plot method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Equity Progression')
ser = self._get_series(freq).rebase()
return ser.plot(figsize=figsize, logy=logy,
title=title, **kwargs) | [
"def",
"plot",
"(",
"self",
",",
"freq",
"=",
"None",
",",
"figsize",
"=",
"(",
"15",
",",
"5",
")",
",",
"title",
"=",
"None",
",",
"logy",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"title",
"is",
"None",
":",
"title",
"=",
"sel... | Helper function for plotting the series.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* logy (bool): log-scale for y axis
* kwargs: passed to pandas' plot method | [
"Helper",
"function",
"for",
"plotting",
"the",
"series",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L887-L908 | train | 199,461 |
pmorissette/ffn | ffn/core.py | GroupStats.plot_scatter_matrix | def plot_scatter_matrix(self, freq=None, title=None,
figsize=(10, 10), **kwargs):
"""
Wrapper around pandas' scatter_matrix.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' scatter_matrix method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Scatter Matrix')
plt.figure()
ser = self._get_series(freq).to_returns().dropna()
pd.scatter_matrix(ser, figsize=figsize, **kwargs)
return plt.suptitle(title) | python | def plot_scatter_matrix(self, freq=None, title=None,
figsize=(10, 10), **kwargs):
"""
Wrapper around pandas' scatter_matrix.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' scatter_matrix method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Scatter Matrix')
plt.figure()
ser = self._get_series(freq).to_returns().dropna()
pd.scatter_matrix(ser, figsize=figsize, **kwargs)
return plt.suptitle(title) | [
"def",
"plot_scatter_matrix",
"(",
"self",
",",
"freq",
"=",
"None",
",",
"title",
"=",
"None",
",",
"figsize",
"=",
"(",
"10",
",",
"10",
")",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"title",
"is",
"None",
":",
"title",
"=",
"self",
".",
"_get_... | Wrapper around pandas' scatter_matrix.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' scatter_matrix method | [
"Wrapper",
"around",
"pandas",
"scatter_matrix",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L910-L930 | train | 199,462 |
pmorissette/ffn | ffn/core.py | GroupStats.plot_histograms | def plot_histograms(self, freq=None, title=None,
figsize=(10, 10), **kwargs):
"""
Wrapper around pandas' hist.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' hist method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Histogram Matrix')
plt.figure()
ser = self._get_series(freq).to_returns().dropna()
ser.hist(figsize=figsize, **kwargs)
return plt.suptitle(title) | python | def plot_histograms(self, freq=None, title=None,
figsize=(10, 10), **kwargs):
"""
Wrapper around pandas' hist.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' hist method
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Histogram Matrix')
plt.figure()
ser = self._get_series(freq).to_returns().dropna()
ser.hist(figsize=figsize, **kwargs)
return plt.suptitle(title) | [
"def",
"plot_histograms",
"(",
"self",
",",
"freq",
"=",
"None",
",",
"title",
"=",
"None",
",",
"figsize",
"=",
"(",
"10",
",",
"10",
")",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"title",
"is",
"None",
":",
"title",
"=",
"self",
".",
"_get_defa... | Wrapper around pandas' hist.
Args:
* freq (str): Data frequency used for display purposes.
Refer to pandas docs for valid freq strings.
* figsize ((x,y)): figure size
* title (str): Title if default not appropriate
* kwargs: passed to pandas' hist method | [
"Wrapper",
"around",
"pandas",
"hist",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L932-L952 | train | 199,463 |
pmorissette/ffn | ffn/core.py | GroupStats.plot_correlation | def plot_correlation(self, freq=None, title=None,
figsize=(12, 6), **kwargs):
"""
Utility function to plot correlations.
Args:
* freq (str): Pandas data frequency alias string
* title (str): Plot title
* figsize (tuple (x,y)): figure size
* kwargs: passed to Pandas' plot_corr_heatmap function
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Correlation Matrix')
rets = self._get_series(freq).to_returns().dropna()
return rets.plot_corr_heatmap(title=title, figsize=figsize, **kwargs) | python | def plot_correlation(self, freq=None, title=None,
figsize=(12, 6), **kwargs):
"""
Utility function to plot correlations.
Args:
* freq (str): Pandas data frequency alias string
* title (str): Plot title
* figsize (tuple (x,y)): figure size
* kwargs: passed to Pandas' plot_corr_heatmap function
"""
if title is None:
title = self._get_default_plot_title(
freq, 'Return Correlation Matrix')
rets = self._get_series(freq).to_returns().dropna()
return rets.plot_corr_heatmap(title=title, figsize=figsize, **kwargs) | [
"def",
"plot_correlation",
"(",
"self",
",",
"freq",
"=",
"None",
",",
"title",
"=",
"None",
",",
"figsize",
"=",
"(",
"12",
",",
"6",
")",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"title",
"is",
"None",
":",
"title",
"=",
"self",
".",
"_get_defa... | Utility function to plot correlations.
Args:
* freq (str): Pandas data frequency alias string
* title (str): Plot title
* figsize (tuple (x,y)): figure size
* kwargs: passed to Pandas' plot_corr_heatmap function | [
"Utility",
"function",
"to",
"plot",
"correlations",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/core.py#L954-L971 | train | 199,464 |
pmorissette/ffn | ffn/data.py | get | def get(tickers, provider=None, common_dates=True, forward_fill=False,
clean_tickers=True, column_names=None, ticker_field_sep=':',
mrefresh=False, existing=None, **kwargs):
"""
Helper function for retrieving data as a DataFrame.
Args:
* tickers (list, string, csv string): Tickers to download.
* provider (function): Provider to use for downloading data.
By default it will be ffn.DEFAULT_PROVIDER if not provided.
* common_dates (bool): Keep common dates only? Drop na's.
* forward_fill (bool): forward fill values if missing. Only works
if common_dates is False, since common_dates will remove
all nan's, so no filling forward necessary.
* clean_tickers (bool): Should the tickers be 'cleaned' using
ffn.utils.clean_tickers? Basically remove non-standard
characters (^VIX -> vix) and standardize to lower case.
* column_names (list): List of column names if clean_tickers
is not satisfactory.
* ticker_field_sep (char): separator used to determine the
ticker and field. This is in case we want to specify
particular, non-default fields. For example, we might
want: AAPL:Low,AAPL:High,AAPL:Close. ':' is the separator.
* mrefresh (bool): Ignore memoization.
* existing (DataFrame): Existing DataFrame to append returns
to - used when we download from multiple sources
* kwargs: passed to provider
"""
if provider is None:
provider = DEFAULT_PROVIDER
tickers = utils.parse_arg(tickers)
data = {}
for ticker in tickers:
t = ticker
f = None
# check for field
bits = ticker.split(ticker_field_sep, 1)
if len(bits) == 2:
t = bits[0]
f = bits[1]
# call provider - check if supports memoization
if hasattr(provider, 'mcache'):
data[ticker] = provider(ticker=t, field=f,
mrefresh=mrefresh, **kwargs)
else:
data[ticker] = provider(ticker=t, field=f, **kwargs)
df = pd.DataFrame(data)
# ensure same order as provided
df = df[tickers]
if existing is not None:
df = ffn.merge(existing, df)
if common_dates:
df = df.dropna()
if forward_fill:
df = df.fillna(method='ffill')
if column_names:
cnames = utils.parse_arg(column_names)
if len(cnames) != len(df.columns):
raise ValueError(
'column_names must be of same length as tickers')
df.columns = cnames
elif clean_tickers:
df.columns = map(utils.clean_ticker, df.columns)
return df | python | def get(tickers, provider=None, common_dates=True, forward_fill=False,
clean_tickers=True, column_names=None, ticker_field_sep=':',
mrefresh=False, existing=None, **kwargs):
"""
Helper function for retrieving data as a DataFrame.
Args:
* tickers (list, string, csv string): Tickers to download.
* provider (function): Provider to use for downloading data.
By default it will be ffn.DEFAULT_PROVIDER if not provided.
* common_dates (bool): Keep common dates only? Drop na's.
* forward_fill (bool): forward fill values if missing. Only works
if common_dates is False, since common_dates will remove
all nan's, so no filling forward necessary.
* clean_tickers (bool): Should the tickers be 'cleaned' using
ffn.utils.clean_tickers? Basically remove non-standard
characters (^VIX -> vix) and standardize to lower case.
* column_names (list): List of column names if clean_tickers
is not satisfactory.
* ticker_field_sep (char): separator used to determine the
ticker and field. This is in case we want to specify
particular, non-default fields. For example, we might
want: AAPL:Low,AAPL:High,AAPL:Close. ':' is the separator.
* mrefresh (bool): Ignore memoization.
* existing (DataFrame): Existing DataFrame to append returns
to - used when we download from multiple sources
* kwargs: passed to provider
"""
if provider is None:
provider = DEFAULT_PROVIDER
tickers = utils.parse_arg(tickers)
data = {}
for ticker in tickers:
t = ticker
f = None
# check for field
bits = ticker.split(ticker_field_sep, 1)
if len(bits) == 2:
t = bits[0]
f = bits[1]
# call provider - check if supports memoization
if hasattr(provider, 'mcache'):
data[ticker] = provider(ticker=t, field=f,
mrefresh=mrefresh, **kwargs)
else:
data[ticker] = provider(ticker=t, field=f, **kwargs)
df = pd.DataFrame(data)
# ensure same order as provided
df = df[tickers]
if existing is not None:
df = ffn.merge(existing, df)
if common_dates:
df = df.dropna()
if forward_fill:
df = df.fillna(method='ffill')
if column_names:
cnames = utils.parse_arg(column_names)
if len(cnames) != len(df.columns):
raise ValueError(
'column_names must be of same length as tickers')
df.columns = cnames
elif clean_tickers:
df.columns = map(utils.clean_ticker, df.columns)
return df | [
"def",
"get",
"(",
"tickers",
",",
"provider",
"=",
"None",
",",
"common_dates",
"=",
"True",
",",
"forward_fill",
"=",
"False",
",",
"clean_tickers",
"=",
"True",
",",
"column_names",
"=",
"None",
",",
"ticker_field_sep",
"=",
"':'",
",",
"mrefresh",
"=",... | Helper function for retrieving data as a DataFrame.
Args:
* tickers (list, string, csv string): Tickers to download.
* provider (function): Provider to use for downloading data.
By default it will be ffn.DEFAULT_PROVIDER if not provided.
* common_dates (bool): Keep common dates only? Drop na's.
* forward_fill (bool): forward fill values if missing. Only works
if common_dates is False, since common_dates will remove
all nan's, so no filling forward necessary.
* clean_tickers (bool): Should the tickers be 'cleaned' using
ffn.utils.clean_tickers? Basically remove non-standard
characters (^VIX -> vix) and standardize to lower case.
* column_names (list): List of column names if clean_tickers
is not satisfactory.
* ticker_field_sep (char): separator used to determine the
ticker and field. This is in case we want to specify
particular, non-default fields. For example, we might
want: AAPL:Low,AAPL:High,AAPL:Close. ':' is the separator.
* mrefresh (bool): Ignore memoization.
* existing (DataFrame): Existing DataFrame to append returns
to - used when we download from multiple sources
* kwargs: passed to provider | [
"Helper",
"function",
"for",
"retrieving",
"data",
"as",
"a",
"DataFrame",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/data.py#L18-L93 | train | 199,465 |
pmorissette/ffn | ffn/data.py | web | def web(ticker, field=None, start=None, end=None,
mrefresh=False, source='yahoo'):
"""
Data provider wrapper around pandas.io.data provider. Provides
memoization.
"""
if source == 'yahoo' and field is None:
field = 'Adj Close'
tmp = _download_web(ticker, data_source=source,
start=start, end=end)
if tmp is None:
raise ValueError('failed to retrieve data for %s:%s' % (ticker, field))
if field:
return tmp[field]
else:
return tmp | python | def web(ticker, field=None, start=None, end=None,
mrefresh=False, source='yahoo'):
"""
Data provider wrapper around pandas.io.data provider. Provides
memoization.
"""
if source == 'yahoo' and field is None:
field = 'Adj Close'
tmp = _download_web(ticker, data_source=source,
start=start, end=end)
if tmp is None:
raise ValueError('failed to retrieve data for %s:%s' % (ticker, field))
if field:
return tmp[field]
else:
return tmp | [
"def",
"web",
"(",
"ticker",
",",
"field",
"=",
"None",
",",
"start",
"=",
"None",
",",
"end",
"=",
"None",
",",
"mrefresh",
"=",
"False",
",",
"source",
"=",
"'yahoo'",
")",
":",
"if",
"source",
"==",
"'yahoo'",
"and",
"field",
"is",
"None",
":",
... | Data provider wrapper around pandas.io.data provider. Provides
memoization. | [
"Data",
"provider",
"wrapper",
"around",
"pandas",
".",
"io",
".",
"data",
"provider",
".",
"Provides",
"memoization",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/data.py#L97-L115 | train | 199,466 |
pmorissette/ffn | ffn/data.py | csv | def csv(ticker, path='data.csv', field='', mrefresh=False, **kwargs):
"""
Data provider wrapper around pandas' read_csv. Provides memoization.
"""
# set defaults if not specified
if 'index_col' not in kwargs:
kwargs['index_col'] = 0
if 'parse_dates' not in kwargs:
kwargs['parse_dates'] = True
# read in dataframe from csv file
df = pd.read_csv(path, **kwargs)
tf = ticker
if field is not '' and field is not None:
tf = '%s:%s' % (tf, field)
# check that required column exists
if tf not in df:
raise ValueError('Ticker(field) not present in csv file!')
return df[tf] | python | def csv(ticker, path='data.csv', field='', mrefresh=False, **kwargs):
"""
Data provider wrapper around pandas' read_csv. Provides memoization.
"""
# set defaults if not specified
if 'index_col' not in kwargs:
kwargs['index_col'] = 0
if 'parse_dates' not in kwargs:
kwargs['parse_dates'] = True
# read in dataframe from csv file
df = pd.read_csv(path, **kwargs)
tf = ticker
if field is not '' and field is not None:
tf = '%s:%s' % (tf, field)
# check that required column exists
if tf not in df:
raise ValueError('Ticker(field) not present in csv file!')
return df[tf] | [
"def",
"csv",
"(",
"ticker",
",",
"path",
"=",
"'data.csv'",
",",
"field",
"=",
"''",
",",
"mrefresh",
"=",
"False",
",",
"*",
"*",
"kwargs",
")",
":",
"# set defaults if not specified",
"if",
"'index_col'",
"not",
"in",
"kwargs",
":",
"kwargs",
"[",
"'i... | Data provider wrapper around pandas' read_csv. Provides memoization. | [
"Data",
"provider",
"wrapper",
"around",
"pandas",
"read_csv",
".",
"Provides",
"memoization",
"."
] | ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a | https://github.com/pmorissette/ffn/blob/ef09f28b858b7ffcd2627ce6a4dc618183a6bc8a/ffn/data.py#L143-L164 | train | 199,467 |
petl-developers/petl | petl/util/vis.py | display | def display(table, limit=0, vrepr=None, index_header=None, caption=None,
tr_style=None, td_styles=None, encoding=None, truncate=None,
epilogue=None):
"""
Display a table inline within an IPython notebook.
"""
from IPython.core.display import display_html
html = _display_html(table, limit=limit, vrepr=vrepr,
index_header=index_header, caption=caption,
tr_style=tr_style, td_styles=td_styles,
encoding=encoding, truncate=truncate,
epilogue=epilogue)
display_html(html, raw=True) | python | def display(table, limit=0, vrepr=None, index_header=None, caption=None,
tr_style=None, td_styles=None, encoding=None, truncate=None,
epilogue=None):
"""
Display a table inline within an IPython notebook.
"""
from IPython.core.display import display_html
html = _display_html(table, limit=limit, vrepr=vrepr,
index_header=index_header, caption=caption,
tr_style=tr_style, td_styles=td_styles,
encoding=encoding, truncate=truncate,
epilogue=epilogue)
display_html(html, raw=True) | [
"def",
"display",
"(",
"table",
",",
"limit",
"=",
"0",
",",
"vrepr",
"=",
"None",
",",
"index_header",
"=",
"None",
",",
"caption",
"=",
"None",
",",
"tr_style",
"=",
"None",
",",
"td_styles",
"=",
"None",
",",
"encoding",
"=",
"None",
",",
"truncat... | Display a table inline within an IPython notebook. | [
"Display",
"a",
"table",
"inline",
"within",
"an",
"IPython",
"notebook",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/util/vis.py#L567-L581 | train | 199,468 |
petl-developers/petl | petl/io/xlsx.py | fromxlsx | def fromxlsx(filename, sheet=None, range_string=None, row_offset=0,
column_offset=0, **kwargs):
"""
Extract a table from a sheet in an Excel .xlsx file.
N.B., the sheet name is case sensitive.
The `sheet` argument can be omitted, in which case the first sheet in
the workbook is used by default.
The `range_string` argument can be used to provide a range string
specifying a range of cells to extract.
The `row_offset` and `column_offset` arguments can be used to
specify offsets.
Any other keyword arguments are passed through to
:func:`openpyxl.load_workbook()`.
"""
return XLSXView(filename, sheet=sheet, range_string=range_string,
row_offset=row_offset, column_offset=column_offset,
**kwargs) | python | def fromxlsx(filename, sheet=None, range_string=None, row_offset=0,
column_offset=0, **kwargs):
"""
Extract a table from a sheet in an Excel .xlsx file.
N.B., the sheet name is case sensitive.
The `sheet` argument can be omitted, in which case the first sheet in
the workbook is used by default.
The `range_string` argument can be used to provide a range string
specifying a range of cells to extract.
The `row_offset` and `column_offset` arguments can be used to
specify offsets.
Any other keyword arguments are passed through to
:func:`openpyxl.load_workbook()`.
"""
return XLSXView(filename, sheet=sheet, range_string=range_string,
row_offset=row_offset, column_offset=column_offset,
**kwargs) | [
"def",
"fromxlsx",
"(",
"filename",
",",
"sheet",
"=",
"None",
",",
"range_string",
"=",
"None",
",",
"row_offset",
"=",
"0",
",",
"column_offset",
"=",
"0",
",",
"*",
"*",
"kwargs",
")",
":",
"return",
"XLSXView",
"(",
"filename",
",",
"sheet",
"=",
... | Extract a table from a sheet in an Excel .xlsx file.
N.B., the sheet name is case sensitive.
The `sheet` argument can be omitted, in which case the first sheet in
the workbook is used by default.
The `range_string` argument can be used to provide a range string
specifying a range of cells to extract.
The `row_offset` and `column_offset` arguments can be used to
specify offsets.
Any other keyword arguments are passed through to
:func:`openpyxl.load_workbook()`. | [
"Extract",
"a",
"table",
"from",
"a",
"sheet",
"in",
"an",
"Excel",
".",
"xlsx",
"file",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/xlsx.py#L11-L34 | train | 199,469 |
petl-developers/petl | petl/io/xlsx.py | toxlsx | def toxlsx(tbl, filename, sheet=None, encoding=None):
"""
Write a table to a new Excel .xlsx file.
"""
import openpyxl
if encoding is None:
encoding = locale.getpreferredencoding()
wb = openpyxl.Workbook(write_only=True)
ws = wb.create_sheet(title=sheet)
for row in tbl:
ws.append(row)
wb.save(filename) | python | def toxlsx(tbl, filename, sheet=None, encoding=None):
"""
Write a table to a new Excel .xlsx file.
"""
import openpyxl
if encoding is None:
encoding = locale.getpreferredencoding()
wb = openpyxl.Workbook(write_only=True)
ws = wb.create_sheet(title=sheet)
for row in tbl:
ws.append(row)
wb.save(filename) | [
"def",
"toxlsx",
"(",
"tbl",
",",
"filename",
",",
"sheet",
"=",
"None",
",",
"encoding",
"=",
"None",
")",
":",
"import",
"openpyxl",
"if",
"encoding",
"is",
"None",
":",
"encoding",
"=",
"locale",
".",
"getpreferredencoding",
"(",
")",
"wb",
"=",
"op... | Write a table to a new Excel .xlsx file. | [
"Write",
"a",
"table",
"to",
"a",
"new",
"Excel",
".",
"xlsx",
"file",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/xlsx.py#L75-L88 | train | 199,470 |
petl-developers/petl | petl/io/pickle.py | teepickle | def teepickle(table, source=None, protocol=-1, write_header=True):
"""
Return a table that writes rows to a pickle file as they are iterated
over.
"""
return TeePickleView(table, source=source, protocol=protocol,
write_header=write_header) | python | def teepickle(table, source=None, protocol=-1, write_header=True):
"""
Return a table that writes rows to a pickle file as they are iterated
over.
"""
return TeePickleView(table, source=source, protocol=protocol,
write_header=write_header) | [
"def",
"teepickle",
"(",
"table",
",",
"source",
"=",
"None",
",",
"protocol",
"=",
"-",
"1",
",",
"write_header",
"=",
"True",
")",
":",
"return",
"TeePickleView",
"(",
"table",
",",
"source",
"=",
"source",
",",
"protocol",
"=",
"protocol",
",",
"wri... | Return a table that writes rows to a pickle file as they are iterated
over. | [
"Return",
"a",
"table",
"that",
"writes",
"rows",
"to",
"a",
"pickle",
"file",
"as",
"they",
"are",
"iterated",
"over",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/pickle.py#L129-L137 | train | 199,471 |
petl-developers/petl | petl/transform/conversions.py | format | def format(table, field, fmt, **kwargs):
"""
Convenience function to format all values in the given `field` using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt.format(v)
return convert(table, field, conv, **kwargs) | python | def format(table, field, fmt, **kwargs):
"""
Convenience function to format all values in the given `field` using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt.format(v)
return convert(table, field, conv, **kwargs) | [
"def",
"format",
"(",
"table",
",",
"field",
",",
"fmt",
",",
"*",
"*",
"kwargs",
")",
":",
"conv",
"=",
"lambda",
"v",
":",
"fmt",
".",
"format",
"(",
"v",
")",
"return",
"convert",
"(",
"table",
",",
"field",
",",
"conv",
",",
"*",
"*",
"kwar... | Convenience function to format all values in the given `field` using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False. | [
"Convenience",
"function",
"to",
"format",
"all",
"values",
"in",
"the",
"given",
"field",
"using",
"the",
"fmt",
"format",
"string",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/conversions.py#L427-L439 | train | 199,472 |
petl-developers/petl | petl/transform/conversions.py | formatall | def formatall(table, fmt, **kwargs):
"""
Convenience function to format all values in all fields using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt.format(v)
return convertall(table, conv, **kwargs) | python | def formatall(table, fmt, **kwargs):
"""
Convenience function to format all values in all fields using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt.format(v)
return convertall(table, conv, **kwargs) | [
"def",
"formatall",
"(",
"table",
",",
"fmt",
",",
"*",
"*",
"kwargs",
")",
":",
"conv",
"=",
"lambda",
"v",
":",
"fmt",
".",
"format",
"(",
"v",
")",
"return",
"convertall",
"(",
"table",
",",
"conv",
",",
"*",
"*",
"kwargs",
")"
] | Convenience function to format all values in all fields using the
`fmt` format string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False. | [
"Convenience",
"function",
"to",
"format",
"all",
"values",
"in",
"all",
"fields",
"using",
"the",
"fmt",
"format",
"string",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/conversions.py#L445-L457 | train | 199,473 |
petl-developers/petl | petl/transform/conversions.py | interpolate | def interpolate(table, field, fmt, **kwargs):
"""
Convenience function to interpolate all values in the given `field` using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt % v
return convert(table, field, conv, **kwargs) | python | def interpolate(table, field, fmt, **kwargs):
"""
Convenience function to interpolate all values in the given `field` using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt % v
return convert(table, field, conv, **kwargs) | [
"def",
"interpolate",
"(",
"table",
",",
"field",
",",
"fmt",
",",
"*",
"*",
"kwargs",
")",
":",
"conv",
"=",
"lambda",
"v",
":",
"fmt",
"%",
"v",
"return",
"convert",
"(",
"table",
",",
"field",
",",
"conv",
",",
"*",
"*",
"kwargs",
")"
] | Convenience function to interpolate all values in the given `field` using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False. | [
"Convenience",
"function",
"to",
"interpolate",
"all",
"values",
"in",
"the",
"given",
"field",
"using",
"the",
"fmt",
"string",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/conversions.py#L463-L475 | train | 199,474 |
petl-developers/petl | petl/transform/conversions.py | interpolateall | def interpolateall(table, fmt, **kwargs):
"""
Convenience function to interpolate all values in all fields using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt % v
return convertall(table, conv, **kwargs) | python | def interpolateall(table, fmt, **kwargs):
"""
Convenience function to interpolate all values in all fields using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False.
"""
conv = lambda v: fmt % v
return convertall(table, conv, **kwargs) | [
"def",
"interpolateall",
"(",
"table",
",",
"fmt",
",",
"*",
"*",
"kwargs",
")",
":",
"conv",
"=",
"lambda",
"v",
":",
"fmt",
"%",
"v",
"return",
"convertall",
"(",
"table",
",",
"conv",
",",
"*",
"*",
"kwargs",
")"
] | Convenience function to interpolate all values in all fields using
the `fmt` string.
The ``where`` keyword argument can be given with a callable or expression
which is evaluated on each row and which should return True if the
conversion should be applied on that row, else False. | [
"Convenience",
"function",
"to",
"interpolate",
"all",
"values",
"in",
"all",
"fields",
"using",
"the",
"fmt",
"string",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/conversions.py#L481-L493 | train | 199,475 |
petl-developers/petl | petl/util/lookups.py | recordlookup | def recordlookup(table, key, dictionary=None):
"""
Load a dictionary with data from the given table, mapping to record objects.
"""
if dictionary is None:
dictionary = dict()
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'no key selected'
getkey = operator.itemgetter(*keyindices)
for row in it:
k = getkey(row)
rec = Record(row, flds)
if k in dictionary:
# work properly with shelve
l = dictionary[k]
l.append(rec)
dictionary[k] = l
else:
dictionary[k] = [rec]
return dictionary | python | def recordlookup(table, key, dictionary=None):
"""
Load a dictionary with data from the given table, mapping to record objects.
"""
if dictionary is None:
dictionary = dict()
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'no key selected'
getkey = operator.itemgetter(*keyindices)
for row in it:
k = getkey(row)
rec = Record(row, flds)
if k in dictionary:
# work properly with shelve
l = dictionary[k]
l.append(rec)
dictionary[k] = l
else:
dictionary[k] = [rec]
return dictionary | [
"def",
"recordlookup",
"(",
"table",
",",
"key",
",",
"dictionary",
"=",
"None",
")",
":",
"if",
"dictionary",
"is",
"None",
":",
"dictionary",
"=",
"dict",
"(",
")",
"it",
"=",
"iter",
"(",
"table",
")",
"hdr",
"=",
"next",
"(",
"it",
")",
"flds",... | Load a dictionary with data from the given table, mapping to record objects. | [
"Load",
"a",
"dictionary",
"with",
"data",
"from",
"the",
"given",
"table",
"mapping",
"to",
"record",
"objects",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/util/lookups.py#L324-L349 | train | 199,476 |
petl-developers/petl | petl/io/bcolz.py | appendbcolz | def appendbcolz(table, obj, check_names=True):
"""Append data into a bcolz ctable. The `obj` argument can be either an
existing ctable or the name of a directory were an on-disk ctable is
stored.
.. versionadded:: 1.1.0
"""
import bcolz
import numpy as np
if isinstance(obj, string_types):
ctbl = bcolz.open(obj, mode='a')
else:
assert hasattr(obj, 'append') and hasattr(obj, 'names'), \
'expected rootdir or ctable, found %r' % obj
ctbl = obj
# setup
dtype = ctbl.dtype
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
# check names match
if check_names:
assert tuple(flds) == tuple(ctbl.names), 'column names do not match'
# fill chunk-wise
chunklen = sum(ctbl.cols[name].chunklen
for name in ctbl.names) // len(ctbl.names)
while True:
data = list(itertools.islice(it, chunklen))
data = np.array(data, dtype=dtype)
ctbl.append(data)
if len(data) < chunklen:
break
ctbl.flush()
return ctbl | python | def appendbcolz(table, obj, check_names=True):
"""Append data into a bcolz ctable. The `obj` argument can be either an
existing ctable or the name of a directory were an on-disk ctable is
stored.
.. versionadded:: 1.1.0
"""
import bcolz
import numpy as np
if isinstance(obj, string_types):
ctbl = bcolz.open(obj, mode='a')
else:
assert hasattr(obj, 'append') and hasattr(obj, 'names'), \
'expected rootdir or ctable, found %r' % obj
ctbl = obj
# setup
dtype = ctbl.dtype
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
# check names match
if check_names:
assert tuple(flds) == tuple(ctbl.names), 'column names do not match'
# fill chunk-wise
chunklen = sum(ctbl.cols[name].chunklen
for name in ctbl.names) // len(ctbl.names)
while True:
data = list(itertools.islice(it, chunklen))
data = np.array(data, dtype=dtype)
ctbl.append(data)
if len(data) < chunklen:
break
ctbl.flush()
return ctbl | [
"def",
"appendbcolz",
"(",
"table",
",",
"obj",
",",
"check_names",
"=",
"True",
")",
":",
"import",
"bcolz",
"import",
"numpy",
"as",
"np",
"if",
"isinstance",
"(",
"obj",
",",
"string_types",
")",
":",
"ctbl",
"=",
"bcolz",
".",
"open",
"(",
"obj",
... | Append data into a bcolz ctable. The `obj` argument can be either an
existing ctable or the name of a directory were an on-disk ctable is
stored.
.. versionadded:: 1.1.0 | [
"Append",
"data",
"into",
"a",
"bcolz",
"ctable",
".",
"The",
"obj",
"argument",
"can",
"be",
"either",
"an",
"existing",
"ctable",
"or",
"the",
"name",
"of",
"a",
"directory",
"were",
"an",
"on",
"-",
"disk",
"ctable",
"is",
"stored",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/bcolz.py#L155-L195 | train | 199,477 |
petl-developers/petl | petl/io/text.py | teetext | def teetext(table, source=None, encoding=None, errors='strict', template=None,
prologue=None, epilogue=None):
"""
Return a table that writes rows to a text file as they are iterated over.
"""
assert template is not None, 'template is required'
return TeeTextView(table, source=source, encoding=encoding, errors=errors,
template=template, prologue=prologue, epilogue=epilogue) | python | def teetext(table, source=None, encoding=None, errors='strict', template=None,
prologue=None, epilogue=None):
"""
Return a table that writes rows to a text file as they are iterated over.
"""
assert template is not None, 'template is required'
return TeeTextView(table, source=source, encoding=encoding, errors=errors,
template=template, prologue=prologue, epilogue=epilogue) | [
"def",
"teetext",
"(",
"table",
",",
"source",
"=",
"None",
",",
"encoding",
"=",
"None",
",",
"errors",
"=",
"'strict'",
",",
"template",
"=",
"None",
",",
"prologue",
"=",
"None",
",",
"epilogue",
"=",
"None",
")",
":",
"assert",
"template",
"is",
... | Return a table that writes rows to a text file as they are iterated over. | [
"Return",
"a",
"table",
"that",
"writes",
"rows",
"to",
"a",
"text",
"file",
"as",
"they",
"are",
"iterated",
"over",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/text.py#L212-L221 | train | 199,478 |
petl-developers/petl | petl/transform/reductions.py | groupcountdistinctvalues | def groupcountdistinctvalues(table, key, value):
"""Group by the `key` field then count the number of distinct values in the
`value` field."""
s1 = cut(table, key, value)
s2 = distinct(s1)
s3 = aggregate(s2, key, len)
return s3 | python | def groupcountdistinctvalues(table, key, value):
"""Group by the `key` field then count the number of distinct values in the
`value` field."""
s1 = cut(table, key, value)
s2 = distinct(s1)
s3 = aggregate(s2, key, len)
return s3 | [
"def",
"groupcountdistinctvalues",
"(",
"table",
",",
"key",
",",
"value",
")",
":",
"s1",
"=",
"cut",
"(",
"table",
",",
"key",
",",
"value",
")",
"s2",
"=",
"distinct",
"(",
"s1",
")",
"s3",
"=",
"aggregate",
"(",
"s2",
",",
"key",
",",
"len",
... | Group by the `key` field then count the number of distinct values in the
`value` field. | [
"Group",
"by",
"the",
"key",
"field",
"then",
"count",
"the",
"number",
"of",
"distinct",
"values",
"in",
"the",
"value",
"field",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/reductions.py#L335-L342 | train | 199,479 |
petl-developers/petl | petl/io/whoosh.py | appendtextindex | def appendtextindex(table, index_or_dirname, indexname=None, merge=True,
optimize=False):
"""
Load all rows from `table` into a Whoosh index, adding them to any existing
data in the index.
Keyword arguments:
table
A table container with the data to be loaded.
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
merge
Merge small segments during commit?
optimize
Merge all segments together?
"""
import whoosh.index
# deal with polymorphic argument
if isinstance(index_or_dirname, string_types):
dirname = index_or_dirname
index = whoosh.index.open_dir(dirname, indexname=indexname,
readonly=False)
needs_closing = True
elif isinstance(index_or_dirname, whoosh.index.Index):
index = index_or_dirname
needs_closing = False
else:
raise ArgumentError('expected string or index, found %r'
% index_or_dirname)
writer = index.writer()
try:
for d in dicts(table):
writer.add_document(**d)
writer.commit(merge=merge, optimize=optimize)
except Exception:
writer.cancel()
raise
finally:
if needs_closing:
index.close() | python | def appendtextindex(table, index_or_dirname, indexname=None, merge=True,
optimize=False):
"""
Load all rows from `table` into a Whoosh index, adding them to any existing
data in the index.
Keyword arguments:
table
A table container with the data to be loaded.
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
merge
Merge small segments during commit?
optimize
Merge all segments together?
"""
import whoosh.index
# deal with polymorphic argument
if isinstance(index_or_dirname, string_types):
dirname = index_or_dirname
index = whoosh.index.open_dir(dirname, indexname=indexname,
readonly=False)
needs_closing = True
elif isinstance(index_or_dirname, whoosh.index.Index):
index = index_or_dirname
needs_closing = False
else:
raise ArgumentError('expected string or index, found %r'
% index_or_dirname)
writer = index.writer()
try:
for d in dicts(table):
writer.add_document(**d)
writer.commit(merge=merge, optimize=optimize)
except Exception:
writer.cancel()
raise
finally:
if needs_closing:
index.close() | [
"def",
"appendtextindex",
"(",
"table",
",",
"index_or_dirname",
",",
"indexname",
"=",
"None",
",",
"merge",
"=",
"True",
",",
"optimize",
"=",
"False",
")",
":",
"import",
"whoosh",
".",
"index",
"# deal with polymorphic argument",
"if",
"isinstance",
"(",
"... | Load all rows from `table` into a Whoosh index, adding them to any existing
data in the index.
Keyword arguments:
table
A table container with the data to be loaded.
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
merge
Merge small segments during commit?
optimize
Merge all segments together? | [
"Load",
"all",
"rows",
"from",
"table",
"into",
"a",
"Whoosh",
"index",
"adding",
"them",
"to",
"any",
"existing",
"data",
"in",
"the",
"index",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/whoosh.py#L204-L254 | train | 199,480 |
petl-developers/petl | petl/io/whoosh.py | searchtextindexpage | def searchtextindexpage(index_or_dirname, query, pagenum, pagelen=10,
indexname=None, docnum_field=None, score_field=None,
fieldboosts=None, search_kwargs=None):
"""
Search an index using a query, returning a result page.
Keyword arguments:
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
query
Either a string or an instance of `whoosh.query.Query`. If a string,
it will be parsed as a multi-field query, i.e., any terms not bound
to a specific field will match **any** field.
pagenum
Number of the page to return (e.g., 1 = first page).
pagelen
Number of results per page.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
docnum_field
If not None, an extra field will be added to the output table containing
the internal document number stored in the index. The name of the field
will be the value of this argument.
score_field
If not None, an extra field will be added to the output table containing
the score of the result. The name of the field will be the value of this
argument.
fieldboosts
An optional dictionary mapping field names to boosts.
search_kwargs
Any extra keyword arguments to be passed through to the Whoosh
`search()` method.
"""
return SearchTextIndexView(index_or_dirname, query, pagenum=pagenum,
pagelen=pagelen, indexname=indexname,
docnum_field=docnum_field,
score_field=score_field, fieldboosts=fieldboosts,
search_kwargs=search_kwargs) | python | def searchtextindexpage(index_or_dirname, query, pagenum, pagelen=10,
indexname=None, docnum_field=None, score_field=None,
fieldboosts=None, search_kwargs=None):
"""
Search an index using a query, returning a result page.
Keyword arguments:
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
query
Either a string or an instance of `whoosh.query.Query`. If a string,
it will be parsed as a multi-field query, i.e., any terms not bound
to a specific field will match **any** field.
pagenum
Number of the page to return (e.g., 1 = first page).
pagelen
Number of results per page.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
docnum_field
If not None, an extra field will be added to the output table containing
the internal document number stored in the index. The name of the field
will be the value of this argument.
score_field
If not None, an extra field will be added to the output table containing
the score of the result. The name of the field will be the value of this
argument.
fieldboosts
An optional dictionary mapping field names to boosts.
search_kwargs
Any extra keyword arguments to be passed through to the Whoosh
`search()` method.
"""
return SearchTextIndexView(index_or_dirname, query, pagenum=pagenum,
pagelen=pagelen, indexname=indexname,
docnum_field=docnum_field,
score_field=score_field, fieldboosts=fieldboosts,
search_kwargs=search_kwargs) | [
"def",
"searchtextindexpage",
"(",
"index_or_dirname",
",",
"query",
",",
"pagenum",
",",
"pagelen",
"=",
"10",
",",
"indexname",
"=",
"None",
",",
"docnum_field",
"=",
"None",
",",
"score_field",
"=",
"None",
",",
"fieldboosts",
"=",
"None",
",",
"search_kw... | Search an index using a query, returning a result page.
Keyword arguments:
index_or_dirname
Either an instance of `whoosh.index.Index` or a string containing the
directory path where the index is to be stored.
query
Either a string or an instance of `whoosh.query.Query`. If a string,
it will be parsed as a multi-field query, i.e., any terms not bound
to a specific field will match **any** field.
pagenum
Number of the page to return (e.g., 1 = first page).
pagelen
Number of results per page.
indexname
String containing the name of the index, if multiple indexes are stored
in the same directory.
docnum_field
If not None, an extra field will be added to the output table containing
the internal document number stored in the index. The name of the field
will be the value of this argument.
score_field
If not None, an extra field will be added to the output table containing
the score of the result. The name of the field will be the value of this
argument.
fieldboosts
An optional dictionary mapping field names to boosts.
search_kwargs
Any extra keyword arguments to be passed through to the Whoosh
`search()` method. | [
"Search",
"an",
"index",
"using",
"a",
"query",
"returning",
"a",
"result",
"page",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/whoosh.py#L337-L379 | train | 199,481 |
petl-developers/petl | petl/io/xls.py | fromxls | def fromxls(filename, sheet=None, use_view=True):
"""
Extract a table from a sheet in an Excel .xls file.
Sheet is identified by its name or index number.
N.B., the sheet name is case sensitive.
"""
return XLSView(filename, sheet=sheet, use_view=use_view) | python | def fromxls(filename, sheet=None, use_view=True):
"""
Extract a table from a sheet in an Excel .xls file.
Sheet is identified by its name or index number.
N.B., the sheet name is case sensitive.
"""
return XLSView(filename, sheet=sheet, use_view=use_view) | [
"def",
"fromxls",
"(",
"filename",
",",
"sheet",
"=",
"None",
",",
"use_view",
"=",
"True",
")",
":",
"return",
"XLSView",
"(",
"filename",
",",
"sheet",
"=",
"sheet",
",",
"use_view",
"=",
"use_view",
")"
] | Extract a table from a sheet in an Excel .xls file.
Sheet is identified by its name or index number.
N.B., the sheet name is case sensitive. | [
"Extract",
"a",
"table",
"from",
"a",
"sheet",
"in",
"an",
"Excel",
".",
"xls",
"file",
".",
"Sheet",
"is",
"identified",
"by",
"its",
"name",
"or",
"index",
"number",
".",
"N",
".",
"B",
".",
"the",
"sheet",
"name",
"is",
"case",
"sensitive",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/xls.py#L12-L22 | train | 199,482 |
petl-developers/petl | petl/io/xls.py | toxls | def toxls(tbl, filename, sheet, encoding=None, style_compression=0,
styles=None):
"""
Write a table to a new Excel .xls file.
"""
import xlwt
if encoding is None:
encoding = locale.getpreferredencoding()
wb = xlwt.Workbook(encoding=encoding, style_compression=style_compression)
ws = wb.add_sheet(sheet)
if styles is None:
# simple version, don't worry about styles
for r, row in enumerate(tbl):
for c, v in enumerate(row):
ws.write(r, c, label=v)
else:
# handle styles
it = iter(tbl)
hdr = next(it)
flds = list(map(str, hdr))
for c, f in enumerate(flds):
ws.write(0, c, label=f)
if f not in styles or styles[f] is None:
styles[f] = xlwt.Style.default_style
# convert to list for easy zipping
styles = [styles[f] for f in flds]
for r, row in enumerate(it):
for c, (v, style) in enumerate(izip_longest(row, styles,
fillvalue=None)):
ws.write(r+1, c, label=v, style=style)
wb.save(filename) | python | def toxls(tbl, filename, sheet, encoding=None, style_compression=0,
styles=None):
"""
Write a table to a new Excel .xls file.
"""
import xlwt
if encoding is None:
encoding = locale.getpreferredencoding()
wb = xlwt.Workbook(encoding=encoding, style_compression=style_compression)
ws = wb.add_sheet(sheet)
if styles is None:
# simple version, don't worry about styles
for r, row in enumerate(tbl):
for c, v in enumerate(row):
ws.write(r, c, label=v)
else:
# handle styles
it = iter(tbl)
hdr = next(it)
flds = list(map(str, hdr))
for c, f in enumerate(flds):
ws.write(0, c, label=f)
if f not in styles or styles[f] is None:
styles[f] = xlwt.Style.default_style
# convert to list for easy zipping
styles = [styles[f] for f in flds]
for r, row in enumerate(it):
for c, (v, style) in enumerate(izip_longest(row, styles,
fillvalue=None)):
ws.write(r+1, c, label=v, style=style)
wb.save(filename) | [
"def",
"toxls",
"(",
"tbl",
",",
"filename",
",",
"sheet",
",",
"encoding",
"=",
"None",
",",
"style_compression",
"=",
"0",
",",
"styles",
"=",
"None",
")",
":",
"import",
"xlwt",
"if",
"encoding",
"is",
"None",
":",
"encoding",
"=",
"locale",
".",
... | Write a table to a new Excel .xls file. | [
"Write",
"a",
"table",
"to",
"a",
"new",
"Excel",
".",
"xls",
"file",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/xls.py#L59-L93 | train | 199,483 |
petl-developers/petl | petl/util/base.py | asindices | def asindices(hdr, spec):
"""Convert the given field `spec` into a list of field indices."""
flds = list(map(text_type, hdr))
indices = list()
if not isinstance(spec, (list, tuple)):
spec = (spec,)
for s in spec:
# spec could be a field index (takes priority)
if isinstance(s, int) and s < len(hdr):
indices.append(s) # index fields from 0
# spec could be a field
elif s in flds:
indices.append(flds.index(s))
else:
raise FieldSelectionError(s)
return indices | python | def asindices(hdr, spec):
"""Convert the given field `spec` into a list of field indices."""
flds = list(map(text_type, hdr))
indices = list()
if not isinstance(spec, (list, tuple)):
spec = (spec,)
for s in spec:
# spec could be a field index (takes priority)
if isinstance(s, int) and s < len(hdr):
indices.append(s) # index fields from 0
# spec could be a field
elif s in flds:
indices.append(flds.index(s))
else:
raise FieldSelectionError(s)
return indices | [
"def",
"asindices",
"(",
"hdr",
",",
"spec",
")",
":",
"flds",
"=",
"list",
"(",
"map",
"(",
"text_type",
",",
"hdr",
")",
")",
"indices",
"=",
"list",
"(",
")",
"if",
"not",
"isinstance",
"(",
"spec",
",",
"(",
"list",
",",
"tuple",
")",
")",
... | Convert the given field `spec` into a list of field indices. | [
"Convert",
"the",
"given",
"field",
"spec",
"into",
"a",
"list",
"of",
"field",
"indices",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/util/base.py#L282-L298 | train | 199,484 |
petl-developers/petl | petl/util/base.py | expr | def expr(s):
"""
Construct a function operating on a table record.
The expression string is converted into a lambda function by prepending
the string with ``'lambda rec: '``, then replacing anything enclosed in
curly braces (e.g., ``"{foo}"``) with a lookup on the record (e.g.,
``"rec['foo']"``), then finally calling :func:`eval`.
So, e.g., the expression string ``"{foo} * {bar}"`` is converted to the
function ``lambda rec: rec['foo'] * rec['bar']``
"""
prog = re.compile('\{([^}]+)\}')
def repl(matchobj):
return "rec['%s']" % matchobj.group(1)
return eval("lambda rec: " + prog.sub(repl, s)) | python | def expr(s):
"""
Construct a function operating on a table record.
The expression string is converted into a lambda function by prepending
the string with ``'lambda rec: '``, then replacing anything enclosed in
curly braces (e.g., ``"{foo}"``) with a lookup on the record (e.g.,
``"rec['foo']"``), then finally calling :func:`eval`.
So, e.g., the expression string ``"{foo} * {bar}"`` is converted to the
function ``lambda rec: rec['foo'] * rec['bar']``
"""
prog = re.compile('\{([^}]+)\}')
def repl(matchobj):
return "rec['%s']" % matchobj.group(1)
return eval("lambda rec: " + prog.sub(repl, s)) | [
"def",
"expr",
"(",
"s",
")",
":",
"prog",
"=",
"re",
".",
"compile",
"(",
"'\\{([^}]+)\\}'",
")",
"def",
"repl",
"(",
"matchobj",
")",
":",
"return",
"\"rec['%s']\"",
"%",
"matchobj",
".",
"group",
"(",
"1",
")",
"return",
"eval",
"(",
"\"lambda rec: ... | Construct a function operating on a table record.
The expression string is converted into a lambda function by prepending
the string with ``'lambda rec: '``, then replacing anything enclosed in
curly braces (e.g., ``"{foo}"``) with a lookup on the record (e.g.,
``"rec['foo']"``), then finally calling :func:`eval`.
So, e.g., the expression string ``"{foo} * {bar}"`` is converted to the
function ``lambda rec: rec['foo'] * rec['bar']`` | [
"Construct",
"a",
"function",
"operating",
"on",
"a",
"table",
"record",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/util/base.py#L648-L667 | train | 199,485 |
petl-developers/petl | petl/io/csv.py | teecsv | def teecsv(table, source=None, encoding=None, errors='strict', write_header=True,
**csvargs):
"""
Returns a table that writes rows to a CSV file as they are iterated over.
"""
source = write_source_from_arg(source)
csvargs.setdefault('dialect', 'excel')
return teecsv_impl(table, source=source, encoding=encoding,
errors=errors, write_header=write_header,
**csvargs) | python | def teecsv(table, source=None, encoding=None, errors='strict', write_header=True,
**csvargs):
"""
Returns a table that writes rows to a CSV file as they are iterated over.
"""
source = write_source_from_arg(source)
csvargs.setdefault('dialect', 'excel')
return teecsv_impl(table, source=source, encoding=encoding,
errors=errors, write_header=write_header,
**csvargs) | [
"def",
"teecsv",
"(",
"table",
",",
"source",
"=",
"None",
",",
"encoding",
"=",
"None",
",",
"errors",
"=",
"'strict'",
",",
"write_header",
"=",
"True",
",",
"*",
"*",
"csvargs",
")",
":",
"source",
"=",
"write_source_from_arg",
"(",
"source",
")",
"... | Returns a table that writes rows to a CSV file as they are iterated over. | [
"Returns",
"a",
"table",
"that",
"writes",
"rows",
"to",
"a",
"CSV",
"file",
"as",
"they",
"are",
"iterated",
"over",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/csv.py#L169-L180 | train | 199,486 |
petl-developers/petl | petl/transform/dedup.py | distinct | def distinct(table, key=None, count=None, presorted=False, buffersize=None,
tempdir=None, cache=True):
"""
Return only distinct rows in the table.
If the `count` argument is not None, it will be used as the name for an
additional field, and the values of the field will be the number of
duplicate rows.
If the `key` keyword argument is passed, the comparison is done on the
given key instead of the full row.
See also :func:`petl.transform.dedup.duplicates`,
:func:`petl.transform.dedup.unique`,
:func:`petl.transform.reductions.groupselectfirst`,
:func:`petl.transform.reductions.groupselectlast`.
"""
return DistinctView(table, key=key, count=count, presorted=presorted,
buffersize=buffersize, tempdir=tempdir, cache=cache) | python | def distinct(table, key=None, count=None, presorted=False, buffersize=None,
tempdir=None, cache=True):
"""
Return only distinct rows in the table.
If the `count` argument is not None, it will be used as the name for an
additional field, and the values of the field will be the number of
duplicate rows.
If the `key` keyword argument is passed, the comparison is done on the
given key instead of the full row.
See also :func:`petl.transform.dedup.duplicates`,
:func:`petl.transform.dedup.unique`,
:func:`petl.transform.reductions.groupselectfirst`,
:func:`petl.transform.reductions.groupselectlast`.
"""
return DistinctView(table, key=key, count=count, presorted=presorted,
buffersize=buffersize, tempdir=tempdir, cache=cache) | [
"def",
"distinct",
"(",
"table",
",",
"key",
"=",
"None",
",",
"count",
"=",
"None",
",",
"presorted",
"=",
"False",
",",
"buffersize",
"=",
"None",
",",
"tempdir",
"=",
"None",
",",
"cache",
"=",
"True",
")",
":",
"return",
"DistinctView",
"(",
"tab... | Return only distinct rows in the table.
If the `count` argument is not None, it will be used as the name for an
additional field, and the values of the field will be the number of
duplicate rows.
If the `key` keyword argument is passed, the comparison is done on the
given key instead of the full row.
See also :func:`petl.transform.dedup.duplicates`,
:func:`petl.transform.dedup.unique`,
:func:`petl.transform.reductions.groupselectfirst`,
:func:`petl.transform.reductions.groupselectlast`. | [
"Return",
"only",
"distinct",
"rows",
"in",
"the",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/dedup.py#L371-L391 | train | 199,487 |
petl-developers/petl | petl/io/db_create.py | make_sqlalchemy_column | def make_sqlalchemy_column(col, colname, constraints=True):
"""
Infer an appropriate SQLAlchemy column type based on a sequence of values.
Keyword arguments:
col : sequence
A sequence of values to use to infer type, length etc.
colname : string
Name of column
constraints : bool
If True use length and nullable constraints
"""
import sqlalchemy
col_not_none = [v for v in col if v is not None]
sql_column_kwargs = {}
sql_type_kwargs = {}
if len(col_not_none) == 0:
sql_column_type = sqlalchemy.String
if constraints:
sql_type_kwargs['length'] = NULL_COLUMN_MAX_LENGTH
elif all(isinstance(v, bool) for v in col_not_none):
sql_column_type = sqlalchemy.Boolean
elif all(isinstance(v, int) for v in col_not_none):
if max(col_not_none) > SQL_INTEGER_MAX \
or min(col_not_none) < SQL_INTEGER_MIN:
sql_column_type = sqlalchemy.BigInteger
else:
sql_column_type = sqlalchemy.Integer
elif all(isinstance(v, long) for v in col_not_none):
sql_column_type = sqlalchemy.BigInteger
elif all(isinstance(v, (int, long)) for v in col_not_none):
sql_column_type = sqlalchemy.BigInteger
elif all(isinstance(v, (int, long, float)) for v in col_not_none):
sql_column_type = sqlalchemy.Float
elif all(isinstance(v, datetime.datetime) for v in col_not_none):
sql_column_type = sqlalchemy.DateTime
elif all(isinstance(v, datetime.date) for v in col_not_none):
sql_column_type = sqlalchemy.Date
elif all(isinstance(v, datetime.time) for v in col_not_none):
sql_column_type = sqlalchemy.Time
else:
sql_column_type = sqlalchemy.String
if constraints:
sql_type_kwargs['length'] = max([len(text_type(v)) for v in col])
if constraints:
sql_column_kwargs['nullable'] = len(col_not_none) < len(col)
return sqlalchemy.Column(colname, sql_column_type(**sql_type_kwargs),
**sql_column_kwargs) | python | def make_sqlalchemy_column(col, colname, constraints=True):
"""
Infer an appropriate SQLAlchemy column type based on a sequence of values.
Keyword arguments:
col : sequence
A sequence of values to use to infer type, length etc.
colname : string
Name of column
constraints : bool
If True use length and nullable constraints
"""
import sqlalchemy
col_not_none = [v for v in col if v is not None]
sql_column_kwargs = {}
sql_type_kwargs = {}
if len(col_not_none) == 0:
sql_column_type = sqlalchemy.String
if constraints:
sql_type_kwargs['length'] = NULL_COLUMN_MAX_LENGTH
elif all(isinstance(v, bool) for v in col_not_none):
sql_column_type = sqlalchemy.Boolean
elif all(isinstance(v, int) for v in col_not_none):
if max(col_not_none) > SQL_INTEGER_MAX \
or min(col_not_none) < SQL_INTEGER_MIN:
sql_column_type = sqlalchemy.BigInteger
else:
sql_column_type = sqlalchemy.Integer
elif all(isinstance(v, long) for v in col_not_none):
sql_column_type = sqlalchemy.BigInteger
elif all(isinstance(v, (int, long)) for v in col_not_none):
sql_column_type = sqlalchemy.BigInteger
elif all(isinstance(v, (int, long, float)) for v in col_not_none):
sql_column_type = sqlalchemy.Float
elif all(isinstance(v, datetime.datetime) for v in col_not_none):
sql_column_type = sqlalchemy.DateTime
elif all(isinstance(v, datetime.date) for v in col_not_none):
sql_column_type = sqlalchemy.Date
elif all(isinstance(v, datetime.time) for v in col_not_none):
sql_column_type = sqlalchemy.Time
else:
sql_column_type = sqlalchemy.String
if constraints:
sql_type_kwargs['length'] = max([len(text_type(v)) for v in col])
if constraints:
sql_column_kwargs['nullable'] = len(col_not_none) < len(col)
return sqlalchemy.Column(colname, sql_column_type(**sql_type_kwargs),
**sql_column_kwargs) | [
"def",
"make_sqlalchemy_column",
"(",
"col",
",",
"colname",
",",
"constraints",
"=",
"True",
")",
":",
"import",
"sqlalchemy",
"col_not_none",
"=",
"[",
"v",
"for",
"v",
"in",
"col",
"if",
"v",
"is",
"not",
"None",
"]",
"sql_column_kwargs",
"=",
"{",
"}... | Infer an appropriate SQLAlchemy column type based on a sequence of values.
Keyword arguments:
col : sequence
A sequence of values to use to infer type, length etc.
colname : string
Name of column
constraints : bool
If True use length and nullable constraints | [
"Infer",
"an",
"appropriate",
"SQLAlchemy",
"column",
"type",
"based",
"on",
"a",
"sequence",
"of",
"values",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/db_create.py#L49-L112 | train | 199,488 |
petl-developers/petl | petl/io/db_create.py | make_sqlalchemy_table | def make_sqlalchemy_table(table, tablename, schema=None, constraints=True,
metadata=None):
"""
Create an SQLAlchemy table definition based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
"""
import sqlalchemy
if not metadata:
metadata = sqlalchemy.MetaData()
sql_table = sqlalchemy.Table(tablename, metadata, schema=schema)
cols = columns(table)
flds = list(cols.keys())
for f in flds:
sql_column = make_sqlalchemy_column(cols[f], f,
constraints=constraints)
sql_table.append_column(sql_column)
return sql_table | python | def make_sqlalchemy_table(table, tablename, schema=None, constraints=True,
metadata=None):
"""
Create an SQLAlchemy table definition based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
"""
import sqlalchemy
if not metadata:
metadata = sqlalchemy.MetaData()
sql_table = sqlalchemy.Table(tablename, metadata, schema=schema)
cols = columns(table)
flds = list(cols.keys())
for f in flds:
sql_column = make_sqlalchemy_column(cols[f], f,
constraints=constraints)
sql_table.append_column(sql_column)
return sql_table | [
"def",
"make_sqlalchemy_table",
"(",
"table",
",",
"tablename",
",",
"schema",
"=",
"None",
",",
"constraints",
"=",
"True",
",",
"metadata",
"=",
"None",
")",
":",
"import",
"sqlalchemy",
"if",
"not",
"metadata",
":",
"metadata",
"=",
"sqlalchemy",
".",
"... | Create an SQLAlchemy table definition based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata | [
"Create",
"an",
"SQLAlchemy",
"table",
"definition",
"based",
"on",
"data",
"in",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/db_create.py#L115-L148 | train | 199,489 |
petl-developers/petl | petl/io/db_create.py | make_create_table_statement | def make_create_table_statement(table, tablename, schema=None,
constraints=True, metadata=None, dialect=None):
"""
Generate a CREATE TABLE statement based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'}
"""
import sqlalchemy
sql_table = make_sqlalchemy_table(table, tablename, schema=schema,
constraints=constraints,
metadata=metadata)
if dialect:
module = __import__('sqlalchemy.dialects.%s' % DIALECTS[dialect],
fromlist=['dialect'])
sql_dialect = module.dialect()
else:
sql_dialect = None
return text_type(sqlalchemy.schema.CreateTable(sql_table)
.compile(dialect=sql_dialect)).strip() | python | def make_create_table_statement(table, tablename, schema=None,
constraints=True, metadata=None, dialect=None):
"""
Generate a CREATE TABLE statement based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'}
"""
import sqlalchemy
sql_table = make_sqlalchemy_table(table, tablename, schema=schema,
constraints=constraints,
metadata=metadata)
if dialect:
module = __import__('sqlalchemy.dialects.%s' % DIALECTS[dialect],
fromlist=['dialect'])
sql_dialect = module.dialect()
else:
sql_dialect = None
return text_type(sqlalchemy.schema.CreateTable(sql_table)
.compile(dialect=sql_dialect)).strip() | [
"def",
"make_create_table_statement",
"(",
"table",
",",
"tablename",
",",
"schema",
"=",
"None",
",",
"constraints",
"=",
"True",
",",
"metadata",
"=",
"None",
",",
"dialect",
"=",
"None",
")",
":",
"import",
"sqlalchemy",
"sql_table",
"=",
"make_sqlalchemy_t... | Generate a CREATE TABLE statement based on data in `table`.
Keyword arguments:
table : table container
Table data to use to infer types etc.
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'} | [
"Generate",
"a",
"CREATE",
"TABLE",
"statement",
"based",
"on",
"data",
"in",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/db_create.py#L151-L187 | train | 199,490 |
petl-developers/petl | petl/io/db_create.py | create_table | def create_table(table, dbo, tablename, schema=None, commit=True,
constraints=True, metadata=None, dialect=None, sample=1000):
"""
Create a database table based on a sample of data in the given `table`.
Keyword arguments:
table : table container
Table data to load
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
commit : bool
If True commit the changes
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'}
sample : int
Number of rows to sample when inferring types etc., set to 0 to use
the whole table
"""
if sample > 0:
table = head(table, sample)
sql = make_create_table_statement(table, tablename, schema=schema,
constraints=constraints,
metadata=metadata, dialect=dialect)
_execute(sql, dbo, commit=commit) | python | def create_table(table, dbo, tablename, schema=None, commit=True,
constraints=True, metadata=None, dialect=None, sample=1000):
"""
Create a database table based on a sample of data in the given `table`.
Keyword arguments:
table : table container
Table data to load
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
commit : bool
If True commit the changes
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'}
sample : int
Number of rows to sample when inferring types etc., set to 0 to use
the whole table
"""
if sample > 0:
table = head(table, sample)
sql = make_create_table_statement(table, tablename, schema=schema,
constraints=constraints,
metadata=metadata, dialect=dialect)
_execute(sql, dbo, commit=commit) | [
"def",
"create_table",
"(",
"table",
",",
"dbo",
",",
"tablename",
",",
"schema",
"=",
"None",
",",
"commit",
"=",
"True",
",",
"constraints",
"=",
"True",
",",
"metadata",
"=",
"None",
",",
"dialect",
"=",
"None",
",",
"sample",
"=",
"1000",
")",
":... | Create a database table based on a sample of data in the given `table`.
Keyword arguments:
table : table container
Table data to load
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema to create the table in
commit : bool
If True commit the changes
constraints : bool
If True use length and nullable constraints
metadata : sqlalchemy.MetaData
Custom table metadata
dialect : text
One of {'access', 'sybase', 'sqlite', 'informix', 'firebird', 'mysql',
'oracle', 'maxdb', 'postgresql', 'mssql'}
sample : int
Number of rows to sample when inferring types etc., set to 0 to use
the whole table | [
"Create",
"a",
"database",
"table",
"based",
"on",
"a",
"sample",
"of",
"data",
"in",
"the",
"given",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/db_create.py#L190-L226 | train | 199,491 |
petl-developers/petl | petl/io/db_create.py | drop_table | def drop_table(dbo, tablename, schema=None, commit=True):
"""
Drop a database table.
Keyword arguments:
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema the table is in
commit : bool
If True commit the changes
"""
# sanitise table name
tablename = _quote(tablename)
if schema is not None:
tablename = _quote(schema) + '.' + tablename
sql = u'DROP TABLE %s' % tablename
_execute(sql, dbo, commit) | python | def drop_table(dbo, tablename, schema=None, commit=True):
"""
Drop a database table.
Keyword arguments:
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema the table is in
commit : bool
If True commit the changes
"""
# sanitise table name
tablename = _quote(tablename)
if schema is not None:
tablename = _quote(schema) + '.' + tablename
sql = u'DROP TABLE %s' % tablename
_execute(sql, dbo, commit) | [
"def",
"drop_table",
"(",
"dbo",
",",
"tablename",
",",
"schema",
"=",
"None",
",",
"commit",
"=",
"True",
")",
":",
"# sanitise table name",
"tablename",
"=",
"_quote",
"(",
"tablename",
")",
"if",
"schema",
"is",
"not",
"None",
":",
"tablename",
"=",
"... | Drop a database table.
Keyword arguments:
dbo : database object
DB-API 2.0 connection, callable returning a DB-API 2.0 cursor, or
SQLAlchemy connection, engine or session
tablename : text
Name of the table
schema : text
Name of the database schema the table is in
commit : bool
If True commit the changes | [
"Drop",
"a",
"database",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/db_create.py#L229-L253 | train | 199,492 |
petl-developers/petl | petl/util/counting.py | typecounter | def typecounter(table, field):
"""
Count the number of values found for each Python type.
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 1, 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', u'xyz', 9.0],
... ['E', 42]]
>>> etl.typecounter(table, 'foo')
Counter({'str': 5})
>>> etl.typecounter(table, 'bar')
Counter({'str': 3, 'int': 2})
>>> etl.typecounter(table, 'baz')
Counter({'str': 2, 'int': 1, 'float': 1, 'NoneType': 1})
The `field` argument can be a field name or index (starting from zero).
"""
counter = Counter()
for v in values(table, field):
try:
counter[v.__class__.__name__] += 1
except IndexError:
pass # ignore short rows
return counter | python | def typecounter(table, field):
"""
Count the number of values found for each Python type.
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 1, 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', u'xyz', 9.0],
... ['E', 42]]
>>> etl.typecounter(table, 'foo')
Counter({'str': 5})
>>> etl.typecounter(table, 'bar')
Counter({'str': 3, 'int': 2})
>>> etl.typecounter(table, 'baz')
Counter({'str': 2, 'int': 1, 'float': 1, 'NoneType': 1})
The `field` argument can be a field name or index (starting from zero).
"""
counter = Counter()
for v in values(table, field):
try:
counter[v.__class__.__name__] += 1
except IndexError:
pass # ignore short rows
return counter | [
"def",
"typecounter",
"(",
"table",
",",
"field",
")",
":",
"counter",
"=",
"Counter",
"(",
")",
"for",
"v",
"in",
"values",
"(",
"table",
",",
"field",
")",
":",
"try",
":",
"counter",
"[",
"v",
".",
"__class__",
".",
"__name__",
"]",
"+=",
"1",
... | Count the number of values found for each Python type.
>>> import petl as etl
>>> table = [['foo', 'bar', 'baz'],
... ['A', 1, 2],
... ['B', u'2', '3.4'],
... [u'B', u'3', u'7.8', True],
... ['D', u'xyz', 9.0],
... ['E', 42]]
>>> etl.typecounter(table, 'foo')
Counter({'str': 5})
>>> etl.typecounter(table, 'bar')
Counter({'str': 3, 'int': 2})
>>> etl.typecounter(table, 'baz')
Counter({'str': 2, 'int': 1, 'float': 1, 'NoneType': 1})
The `field` argument can be a field name or index (starting from zero). | [
"Count",
"the",
"number",
"of",
"values",
"found",
"for",
"each",
"Python",
"type",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/util/counting.py#L260-L288 | train | 199,493 |
petl-developers/petl | petl/io/html.py | teehtml | def teehtml(table, source=None, encoding=None, errors='strict', caption=None,
vrepr=text_type, lineterminator='\n', index_header=False,
tr_style=None, td_styles=None, truncate=None):
"""
Return a table that writes rows to a Unicode HTML file as they are
iterated over.
"""
source = write_source_from_arg(source)
return TeeHTMLView(table, source=source, encoding=encoding, errors=errors,
caption=caption, vrepr=vrepr,
lineterminator=lineterminator, index_header=index_header,
tr_style=tr_style, td_styles=td_styles,
truncate=truncate) | python | def teehtml(table, source=None, encoding=None, errors='strict', caption=None,
vrepr=text_type, lineterminator='\n', index_header=False,
tr_style=None, td_styles=None, truncate=None):
"""
Return a table that writes rows to a Unicode HTML file as they are
iterated over.
"""
source = write_source_from_arg(source)
return TeeHTMLView(table, source=source, encoding=encoding, errors=errors,
caption=caption, vrepr=vrepr,
lineterminator=lineterminator, index_header=index_header,
tr_style=tr_style, td_styles=td_styles,
truncate=truncate) | [
"def",
"teehtml",
"(",
"table",
",",
"source",
"=",
"None",
",",
"encoding",
"=",
"None",
",",
"errors",
"=",
"'strict'",
",",
"caption",
"=",
"None",
",",
"vrepr",
"=",
"text_type",
",",
"lineterminator",
"=",
"'\\n'",
",",
"index_header",
"=",
"False",... | Return a table that writes rows to a Unicode HTML file as they are
iterated over. | [
"Return",
"a",
"table",
"that",
"writes",
"rows",
"to",
"a",
"Unicode",
"HTML",
"file",
"as",
"they",
"are",
"iterated",
"over",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/io/html.py#L102-L116 | train | 199,494 |
petl-developers/petl | petl/transform/intervals.py | tupletree | def tupletree(table, start='start', stop='stop', value=None):
"""
Construct an interval tree for the given table, where each node in the tree
is a row of the table.
"""
import intervaltree
tree = intervaltree.IntervalTree()
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
assert start in flds, 'start field not recognised'
assert stop in flds, 'stop field not recognised'
getstart = itemgetter(flds.index(start))
getstop = itemgetter(flds.index(stop))
if value is None:
getvalue = tuple
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'invalid value field specification'
getvalue = itemgetter(*valueindices)
for row in it:
tree.addi(getstart(row), getstop(row), getvalue(row))
return tree | python | def tupletree(table, start='start', stop='stop', value=None):
"""
Construct an interval tree for the given table, where each node in the tree
is a row of the table.
"""
import intervaltree
tree = intervaltree.IntervalTree()
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
assert start in flds, 'start field not recognised'
assert stop in flds, 'stop field not recognised'
getstart = itemgetter(flds.index(start))
getstop = itemgetter(flds.index(stop))
if value is None:
getvalue = tuple
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'invalid value field specification'
getvalue = itemgetter(*valueindices)
for row in it:
tree.addi(getstart(row), getstop(row), getvalue(row))
return tree | [
"def",
"tupletree",
"(",
"table",
",",
"start",
"=",
"'start'",
",",
"stop",
"=",
"'stop'",
",",
"value",
"=",
"None",
")",
":",
"import",
"intervaltree",
"tree",
"=",
"intervaltree",
".",
"IntervalTree",
"(",
")",
"it",
"=",
"iter",
"(",
"table",
")",... | Construct an interval tree for the given table, where each node in the tree
is a row of the table. | [
"Construct",
"an",
"interval",
"tree",
"for",
"the",
"given",
"table",
"where",
"each",
"node",
"in",
"the",
"tree",
"is",
"a",
"row",
"of",
"the",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/intervals.py#L14-L38 | train | 199,495 |
petl-developers/petl | petl/transform/intervals.py | facettupletrees | def facettupletrees(table, key, start='start', stop='stop', value=None):
"""
Construct faceted interval trees for the given table, where each node in
the tree is a row of the table.
"""
import intervaltree
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
assert start in flds, 'start field not recognised'
assert stop in flds, 'stop field not recognised'
getstart = itemgetter(flds.index(start))
getstop = itemgetter(flds.index(stop))
if value is None:
getvalue = tuple
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'invalid value field specification'
getvalue = itemgetter(*valueindices)
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'invalid key'
getkey = itemgetter(*keyindices)
trees = dict()
for row in it:
k = getkey(row)
if k not in trees:
trees[k] = intervaltree.IntervalTree()
trees[k].addi(getstart(row), getstop(row), getvalue(row))
return trees | python | def facettupletrees(table, key, start='start', stop='stop', value=None):
"""
Construct faceted interval trees for the given table, where each node in
the tree is a row of the table.
"""
import intervaltree
it = iter(table)
hdr = next(it)
flds = list(map(text_type, hdr))
assert start in flds, 'start field not recognised'
assert stop in flds, 'stop field not recognised'
getstart = itemgetter(flds.index(start))
getstop = itemgetter(flds.index(stop))
if value is None:
getvalue = tuple
else:
valueindices = asindices(hdr, value)
assert len(valueindices) > 0, 'invalid value field specification'
getvalue = itemgetter(*valueindices)
keyindices = asindices(hdr, key)
assert len(keyindices) > 0, 'invalid key'
getkey = itemgetter(*keyindices)
trees = dict()
for row in it:
k = getkey(row)
if k not in trees:
trees[k] = intervaltree.IntervalTree()
trees[k].addi(getstart(row), getstop(row), getvalue(row))
return trees | [
"def",
"facettupletrees",
"(",
"table",
",",
"key",
",",
"start",
"=",
"'start'",
",",
"stop",
"=",
"'stop'",
",",
"value",
"=",
"None",
")",
":",
"import",
"intervaltree",
"it",
"=",
"iter",
"(",
"table",
")",
"hdr",
"=",
"next",
"(",
"it",
")",
"... | Construct faceted interval trees for the given table, where each node in
the tree is a row of the table. | [
"Construct",
"faceted",
"interval",
"trees",
"for",
"the",
"given",
"table",
"where",
"each",
"node",
"in",
"the",
"tree",
"is",
"a",
"row",
"of",
"the",
"table",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/intervals.py#L41-L72 | train | 199,496 |
petl-developers/petl | petl/transform/intervals.py | recordtree | def recordtree(table, start='start', stop='stop'):
"""
Construct an interval tree for the given table, where each node in the
tree is a row of the table represented as a record object.
"""
import intervaltree
getstart = attrgetter(start)
getstop = attrgetter(stop)
tree = intervaltree.IntervalTree()
for rec in records(table):
tree.addi(getstart(rec), getstop(rec), rec)
return tree | python | def recordtree(table, start='start', stop='stop'):
"""
Construct an interval tree for the given table, where each node in the
tree is a row of the table represented as a record object.
"""
import intervaltree
getstart = attrgetter(start)
getstop = attrgetter(stop)
tree = intervaltree.IntervalTree()
for rec in records(table):
tree.addi(getstart(rec), getstop(rec), rec)
return tree | [
"def",
"recordtree",
"(",
"table",
",",
"start",
"=",
"'start'",
",",
"stop",
"=",
"'stop'",
")",
":",
"import",
"intervaltree",
"getstart",
"=",
"attrgetter",
"(",
"start",
")",
"getstop",
"=",
"attrgetter",
"(",
"stop",
")",
"tree",
"=",
"intervaltree",
... | Construct an interval tree for the given table, where each node in the
tree is a row of the table represented as a record object. | [
"Construct",
"an",
"interval",
"tree",
"for",
"the",
"given",
"table",
"where",
"each",
"node",
"in",
"the",
"tree",
"is",
"a",
"row",
"of",
"the",
"table",
"represented",
"as",
"a",
"record",
"object",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/intervals.py#L75-L88 | train | 199,497 |
petl-developers/petl | petl/transform/intervals.py | facetrecordtrees | def facetrecordtrees(table, key, start='start', stop='stop'):
"""
Construct faceted interval trees for the given table, where each node in
the tree is a record.
"""
import intervaltree
getstart = attrgetter(start)
getstop = attrgetter(stop)
getkey = attrgetter(key)
trees = dict()
for rec in records(table):
k = getkey(rec)
if k not in trees:
trees[k] = intervaltree.IntervalTree()
trees[k].addi(getstart(rec), getstop(rec), rec)
return trees | python | def facetrecordtrees(table, key, start='start', stop='stop'):
"""
Construct faceted interval trees for the given table, where each node in
the tree is a record.
"""
import intervaltree
getstart = attrgetter(start)
getstop = attrgetter(stop)
getkey = attrgetter(key)
trees = dict()
for rec in records(table):
k = getkey(rec)
if k not in trees:
trees[k] = intervaltree.IntervalTree()
trees[k].addi(getstart(rec), getstop(rec), rec)
return trees | [
"def",
"facetrecordtrees",
"(",
"table",
",",
"key",
",",
"start",
"=",
"'start'",
",",
"stop",
"=",
"'stop'",
")",
":",
"import",
"intervaltree",
"getstart",
"=",
"attrgetter",
"(",
"start",
")",
"getstop",
"=",
"attrgetter",
"(",
"stop",
")",
"getkey",
... | Construct faceted interval trees for the given table, where each node in
the tree is a record. | [
"Construct",
"faceted",
"interval",
"trees",
"for",
"the",
"given",
"table",
"where",
"each",
"node",
"in",
"the",
"tree",
"is",
"a",
"record",
"."
] | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/intervals.py#L91-L108 | train | 199,498 |
petl-developers/petl | petl/transform/intervals.py | facetintervallookupone | def facetintervallookupone(table, key, start='start', stop='stop',
value=None, include_stop=False, strict=True):
"""
Construct a faceted interval lookup for the given table, returning at most
one result for each query.
If ``strict=True``, queries returning more than one result will
raise a `DuplicateKeyError`. If ``strict=False`` and there is
more than one result, the first result is returned.
"""
trees = facettupletrees(table, key, start=start, stop=stop, value=value)
out = dict()
for k in trees:
out[k] = IntervalTreeLookupOne(trees[k], include_stop=include_stop,
strict=strict)
return out | python | def facetintervallookupone(table, key, start='start', stop='stop',
value=None, include_stop=False, strict=True):
"""
Construct a faceted interval lookup for the given table, returning at most
one result for each query.
If ``strict=True``, queries returning more than one result will
raise a `DuplicateKeyError`. If ``strict=False`` and there is
more than one result, the first result is returned.
"""
trees = facettupletrees(table, key, start=start, stop=stop, value=value)
out = dict()
for k in trees:
out[k] = IntervalTreeLookupOne(trees[k], include_stop=include_stop,
strict=strict)
return out | [
"def",
"facetintervallookupone",
"(",
"table",
",",
"key",
",",
"start",
"=",
"'start'",
",",
"stop",
"=",
"'stop'",
",",
"value",
"=",
"None",
",",
"include_stop",
"=",
"False",
",",
"strict",
"=",
"True",
")",
":",
"trees",
"=",
"facettupletrees",
"(",... | Construct a faceted interval lookup for the given table, returning at most
one result for each query.
If ``strict=True``, queries returning more than one result will
raise a `DuplicateKeyError`. If ``strict=False`` and there is
more than one result, the first result is returned. | [
"Construct",
"a",
"faceted",
"interval",
"lookup",
"for",
"the",
"given",
"table",
"returning",
"at",
"most",
"one",
"result",
"for",
"each",
"query",
".",
"If",
"strict",
"=",
"True",
"queries",
"returning",
"more",
"than",
"one",
"result",
"will",
"raise",... | 1d33ca055f7e04e0d28a772041c9fd30c8d415d6 | https://github.com/petl-developers/petl/blob/1d33ca055f7e04e0d28a772041c9fd30c8d415d6/petl/transform/intervals.py#L363-L380 | train | 199,499 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.