This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def sliding_window(x: np.ndarray, n: int) -> np.ndarray: | |
"""Returns a sliding window of n elements from x. | |
Raises a ValueError of n > len(x). | |
""" | |
if n > len(x): raise ValueError("N must be less than the array length") | |
# Courtesy of https://stackoverflow.com/questions/13728392/moving-average-or-running-mean | |
return np.convolve(x, np.ones((n,)) / n, mode='valid') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from IPython.display import display, Markdown | |
import pandas as pd | |
def head(df: pd.DataFrame, n_rows:int=1) -> None: | |
"""Pretty-print the head of a Pandas table in a Jupyter notebook and show its dimensions.""" | |
display(Markdown("**whole table (below):** {} rows × {} columns".format(len(df), len(df.columns)))) | |
display(df.head(n_rows)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Generic, TypeVar, Iterable | |
T = TypeVar('T') | |
def exists(keep_predicate: Callable[[T], bool], seq: Iterable[T]) -> bool: | |
"""Efficient existential quantifier for a filter() predicate. | |
Returns true iff keep_predicate is true for one or more elements.""" | |
for e in seq: | |
if keep_predicate(e): return True # short-circuit | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from typing import Iterator | |
def is_proper_file(path: str) -> bool: | |
name = os.path.split(path)[1] | |
return len(name) > 0 and name[0] not in {'.', '~', '_'} | |
def scantree(path: str, follow_symlinks: bool=False) -> Iterator[str]: | |
"""List the full path of every file not beginning with '.', '~', or '_' in a directory, recursively.""" | |
for entry in os.scandir(path): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip, io | |
from typing import Iterator | |
def lines(file_name: str, known_encoding='utf-8') -> Iterator[str]: | |
"""Lazily read a text file or gzipped text file, decode, and strip any newline character (\n or \r). | |
If the file name ends with '.gz' or '.gzip', assumes the file is Gzipped. | |
Arguments: | |
known_encoding: Applied only when decoding gzip | |
""" | |
if file_name.endswith('.gz') or file_name.endswith('.gzip'): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def zip_strict(*args): | |
"""Same as zip(), but raises a ValueError if the lengths don't match.""" | |
iters = [iter(axis) for axis in args] | |
n_elements = 0 | |
failures = [] | |
while len(failures) == 0: | |
n_elements += 1 | |
values = [] | |
failures = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def format_time(time: datetime.datetime) -> str: | |
"""Standard timestamp format. Ex: 2016-05-02_22_35_56.""" | |
return time.strftime("%Y-%m-%d_%H-%M-%S") | |
def timestamp() -> str: | |
"""Standard timestamp of time now. Ex: 2016-05-02_22_35_56.""" | |
return format_time(datetime.datetime.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import sys | |
from io import StringIO | |
@contextlib.contextmanager | |
def silenced(no_stdout=True, no_stderr=True): | |
""" | |
Suppresses output to stdout and/or stderr. | |
Always resets stdout and stderr, even on an exception. | |
Usage: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Apache License | |
Version 2.0, January 2004 | |
http://www.apache.org/licenses/ | |
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | |
1. Definitions. | |
"License" shall mean the terms and conditions for use, reproduction, | |
and distribution as defined by Sections 1 through 9 of this document. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Douglas Myers-Turnbull wrote this for the Kokel Lab, which has released it under the Apache Software License, Version 2.0 | |
# See the license file here: https://gist.github.com/dmyersturnbull/bfa1c3371e7449db553aaa1e7cd3cac1 | |
# The list of copyright owners is unknown | |
import re | |
import warnings | |
import time | |
from chemspipy import ChemSpider |