Skip to content

Commit

Permalink
added plotMotif
Browse files Browse the repository at this point in the history
  • Loading branch information
Avsecz committed Mar 30, 2017
1 parent 1549b92 commit be2c13a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 15 deletions.
42 changes: 39 additions & 3 deletions concise/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from keras import initializers
from keras.layers.pooling import _GlobalPooling1D
from keras.layers import Conv1D, Input
from deeplift.visualization import viz_sequence
import matplotlib.pyplot as plt

from concise.regularizers import GAMRegularizer
from concise.splines import BSpline
import matplotlib.pyplot as plt


# TODO - improve the naming
Expand Down Expand Up @@ -80,7 +82,7 @@ def __init__(self, filters,
# override input shape
if seq_length:
kwargs["input_shape"] = (seq_length, 4)
kwargs["batch_input_shape"] = None
kwargs.pop("batch_input_shape", None)

super(ConvDNA, self).__init__(
filters=filters,
Expand Down Expand Up @@ -111,7 +113,41 @@ def get_config(self):
config["seq_length"] = self.seq_length
return config

# TODO - define the plotting function for motifs
# def plotWeights(self):
# """Plot weights as matrices
# """
# pass

def plotMotif(self, index, figsize=(10, 2)):
"""
Arguments:
index: Which motif to plot
"""

W = self.get_weights()[0]

assert isinstance(index, int)
assert index >= 0
assert index < W.shape[2]
viz_sequence.plot_weights(W[:, :, index], figsize=figsize)

def plotMotifs(self, figsize=(10, 2)):
"""
Arguments:
indices: Index list which ones to choose
"""

W = self.get_weights()[0]

for index in range(W.shape[2]):
print("filter index: {0}".format(index))
viz_sequence.plot_weights(W[:, :, index], figsize=figsize)

# TODO - improve the plotting functions for motifs - refactor the viz_sequence
# - mutliple panels with titles
# - save to file if needed

############################################
# Smoothing layers
Expand Down
23 changes: 11 additions & 12 deletions nbs/01-simulated-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,24 @@
"## Simulated data case study"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we will replicate the results from [Plositional_effect/Simulation/01_fixed_seq_len.html](https://i12g-gagneurweb.in.tum.de/project/deepcis/#Scripts_Positional_effect_Simulation_01_fixed_seq_len.html) using concise models. Please have a look at that notebook first.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"## See dataset: \n",
"#%matplotlib notebook\n",
"# Used additional packages\n",
"%matplotlib inline\n",
"\n",
"import matplotlib.pyplot as plt\n",
Expand All @@ -128,15 +136,6 @@
"import keras.optimizers as ko"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we will replicate the results from [Plositional_effect/Simulation/01_fixed_seq_len.html](https://i12g-gagneurweb.in.tum.de/project/deepcis/#Scripts_Positional_effect_Simulation_01_fixed_seq_len.html) using concise models. Please have a look at that notebook first.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"tensorflow",
"glmnet",
"keras>=2.0.1",
'deeplift>=0.4',
]

dependency_links = [
"https://github.com/kundajelab/deeplift/tarball/v0.4.0-alpha#egg=deeplift-0.4"
]

test_requirements = [
Expand All @@ -43,6 +48,7 @@
include_package_data=True,
setup_requires=['numpy'],
install_requires=requirements,
dependency_links=dependency_links,
license="MIT license",
zip_safe=False,
keywords=["computational biology", "bioinformatics", "genomics",
Expand Down
35 changes: 35 additions & 0 deletions tests/test_conv1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from keras.utils.generic_utils import deserialize_keras_object, serialize_keras_object, get_custom_objects
from keras.models import Sequential, model_from_json
import matplotlib.pyplot as plt

def test_correct_initialization():
pwm_list = [PWM(np.array([[1, 2, 3, 4], [2, 4, 4, 5]])),
Expand Down Expand Up @@ -88,3 +89,37 @@ def test_init_serialization():

# serialization was successfull
assert np.all(a.kernel_initializer.pwm_list[0].pwm == pwm_list[0].pwm)


def test_plot(tmpdir):
pwm_list = [PWM([[1, 2, 3, 4],
[2, 4, 4, 5]]),
PWM([[1, 2, 1, 4],
[2, 10, 4, 5]])]

# should work out of the box
# get_custom_objects()['PWMKernelInitializer'] = PWMKernelInitializer
# get_custom_objects()['PWMBiasInitializer'] = PWMBiasInitializer

seq_length = 100
input_shape = (None, seq_length, 4) # (batch_size, steps, input_dim)
# input_shape = (seq_length, 4) # (batch_size, steps, input_dim)

# output_shape = (None, steps, filters)
from concise.layers import ConvDNA
conv_l = ConvDNA(filters=15, kernel_size=11,
kernel_regularizer=L1L2(l1=1, l2=1), # Regularization
activation="relu",
kernel_initializer=PWMKernelInitializer(pwm_list, stddev=0.1),
bias_initializer=PWMBiasInitializer(pwm_list, kernel_size=11),
seq_length=seq_length
)

conv_l.build(input_shape)

# plt.savefig('data.png')
# conv_l.plotMotif(0)
# plt.close()

# TODO

0 comments on commit be2c13a

Please sign in to comment.