Skip to content

Instantly share code, notes, and snippets.

View dmyersturnbull's full-sized avatar

Douglas Myers-Turnbull dmyersturnbull

  • Stanford University
  • Stanford, CA
View GitHub Profile
@dmyersturnbull
dmyersturnbull / colored_barplot.py
Last active November 21, 2016 23:18
Colored barplot in Matplotlib that makes sense and doesn't look terrible.
# 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 matplotlib.pyplot as plt
import numpy as np
from typing import Optional, Tuple
def colored_barplot(x: np.ndarray, y: np.ndarray, colors: np.ndarray, y_ticks: Optional[np.ndarray]=None, fig_size: Tuple[float, float]=(10.0, 10.0), label_rotation: float=75):
index = np.arange(0, len(x))
@dmyersturnbull
dmyersturnbull / clustering_plot.py
Last active November 21, 2016 23:18
Calculate tSNE or MDS+PCA and plot the results in Seaborn in a way that doesn't look terrible.
# 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 pandas as pd
import seaborn as sns
from scipy.spatial.distance import squareform
from sklearn import manifold
from scipy.spatial.distance import pdist
from sklearn.decomposition import PCA
@dmyersturnbull
dmyersturnbull / marker_iterator.py
Last active November 21, 2016 23:17
Iterate over Matplotlib marker shapes.
# 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 pandas as pd
import itertools
from typing import Iterator
def marker_iterator(df: pd.DataFrame, class_column: str='class') -> Iterator[str]:
"""Returns an iterator of decent marker shapes. The order is such that similar markers aren't used unless they're needed."""
@dmyersturnbull
dmyersturnbull / plot_dose_response.py
Last active November 21, 2016 23:17
Plot a grid of nice-looking dose-response curves.
# 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 numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
import matplotlib
import pandas as pd
from typing import Callable, Tuple, Dict, Optional, Union
@dmyersturnbull
dmyersturnbull / connected.py
Last active November 21, 2016 23:16
Context with convenience functions for MySQL/MariaDB calls with pymysql.
# 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 pymysql
import contextlib
from typing import Tuple, List, Dict, Iterator
@contextlib.contextmanager
def connected(connection: pymysql.connections.Connection):
@dmyersturnbull
dmyersturnbull / SpiderRecovery.py
Last active November 21, 2016 23:16
Makes a best-effort attempt to recover SMILES strings from compound names unambiguously by searching ChemSpider.
# 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
@dmyersturnbull
dmyersturnbull / LICENSE
Created November 21, 2016 23:13
Apache License, Version 2.0 for all Gists under dmyersturnbull. The copyright owners may be different for each Gist.
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.
@dmyersturnbull
dmyersturnbull / toml_data.py
Last active February 8, 2017 00:18
A data structure for TOML data that's more convenient to use than a plain dict.
from typing import Iterator, Tuple, Dict, List
class MissingConfigEntry(Exception): pass
class TomlData:
"""A better TOML data structure than a plain dict.
Usage examples:
data = TomlData({'x': {'y': {'z': 155}}})
print(data['x.y.z']) # prints 155
@dmyersturnbull
dmyersturnbull / jupyter_audio_display.py
Created May 5, 2020 01:10
Audio container in Jupyter notebook
from typing import Union
from pathlib import PurePath
def listen(path: Union[str, PurePath, bytes]):
"""
Returns an audio container that Jupyter notebook will display.
Must be run from a Jupyter notebook.
Will raise an ImportError if IPython cannot be imported.
:param path: The local path to the audio file
:return: A jupyter notebook ipd.Audio object
@dmyersturnbull
dmyersturnbull / jupyter_audio_display.py
Created May 5, 2020 01:11
Jupyter notebook display using the full width of the page
from IPython.display import display, Markdown, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))