Skip to content

Commit

Permalink
Merge branch 'main' into deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
tnaccarato committed Aug 30, 2024
2 parents ce8f81c + 8b3c063 commit 2a613a5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
10 changes: 5 additions & 5 deletions vis_phewas/som/som_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ def initialise_som(x_normalised, som_x=None, som_y=None, sigma=1.0, learning_rat
"""
# Use rule of 10 sqrt(n) for the number of neurons if not specified
if som_x is None:
som_x = int(np.sqrt(5 * np.sqrt(x_normalised.shape[0])))
som_x = int(np.sqrt(10 * np.sqrt(x_normalised.shape[0])))
if som_y is None:
som_y = int(np.sqrt(5 * np.sqrt(x_normalised.shape[0])))
som_y = int(np.sqrt(10 * np.sqrt(x_normalised.shape[0])))

print(
f"Training SOM with {som_x}x{som_y} grid, sigma={sigma}, learning_rate={learning_rate}, num_iterations={num_iterations}")
print(f"Training SOM with {som_x}x{som_y} grid, sigma={sigma}, learning_rate={learning_rate}, "
f"num_iterations={num_iterations}")

# Initialise the SOM
input_len = x_normalised.shape[1]
Expand All @@ -144,7 +144,6 @@ def clean_filters(filters, som_type):
:param som_type: Type of the SOM (SNP or disease)
:return: Cleaned and formatted filters string
"""
print(f"Filters: {filters}")
# If no filters are provided, return "All Genes" or "All Categories" as appropriate
if not filters:
return "All Genes" if som_type == 'snp' else "All Categories"
Expand Down Expand Up @@ -265,6 +264,7 @@ def plot_metrics_on_som(positions, som_type):
silhouette_scores = []
range_n_clusters = range(2, 11) # Trying 2 to 10 clusters

# Perform KMeans clustering for each number of clusters
for n_clusters in range_n_clusters:
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
cluster_labels = kmeans.fit_predict(positions) # Use the SOM positions for clustering
Expand Down
3 changes: 3 additions & 0 deletions vis_phewas/som/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def process_and_visualise_som(self, data_id, num_clusters, filters, som_type, te
filter_list = cleaned_filters.lower()
filter_list = filter_list.replace('<br>', ',').split(',')
filter_list = [f.strip() for f in filter_list]
# Uppercase is type is SNP
if som_type == 'snp':
filter_list = [f.upper() for f in filter_list]

# Render the visualisation
graph_div = pio.to_html(fig, full_html=False)
Expand Down
1 change: 0 additions & 1 deletion vis_phewas/static/som/js/som.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* @param {number} [num_clusters] - The number of clusters to generate. Defaults to 5 for 'disease' and 7 for 'allele'.
*/
function generateSOM(filters, type, num_clusters) {
console.log("Generating SOM with filters: " + filters + ", type: " + type + ", num_clusters: " + num_clusters);
// Validate the type parameter
if (type !== "snp" && type !== "disease") {
alert("Invalid SOM type specified. Must be 'snp' or 'disease'.");
Expand Down
4 changes: 2 additions & 2 deletions vis_phewas/templates/mainapp/tools.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<div class="panel tools_panel">
<div id="som_options" style="align-items: center; display: flex; flex-direction: column">
<h4>Generate Self Organising Map</h4>
<button class="button btn btn-info dataAction" onclick="generateSOM('', 'disease', 5)" style="color: white; margin-bottom: 1%">Generate Disease SOM</button>
<button class="button btn btn-info dataAction" onclick="generateSOM('', 'snp', 7)" style="color: white;">Generate Allele SOM</button>
<button class="button btn btn-info dataAction" onclick="generateSOM('', 'disease')" style="color: white; margin-bottom: 1%">Generate Disease SOM</button>
<button class="button btn btn-info dataAction" onclick="generateSOM('', 'snp')" style="color: white;">Generate Allele SOM</button>
</div>
</div>

15 changes: 11 additions & 4 deletions vis_phewas/templates/som/som_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h2 style="align-self: center">Filter by Gene Name</h2>
<div class="checkbox-item">
<!-- Check the checkbox if it is in the selected filters (or if all genes are selected) -->
<input type="checkbox" id="{{ category }}" name="filters" value="{{ category }}"
{% if "all genes" in cleaned_filters %}
{% if "ALL GENES" in cleaned_filters %}
checked
{% else %}
{% if category in cleaned_filters %} checked {% endif %}
Expand Down Expand Up @@ -70,7 +70,7 @@ <h2 style="align-self: center">Filter by Gene Name</h2>

<!-- Add a button to submit the form -->
<button type="submit" class="button btn btn-success" style="align-self: normal; border-radius: 20px">
Generate SOM with Selection
Generate New SOM with Selection
</button>
<!-- Add a button to download the cluster results CSV -->
<button class="btn btn-info dataAction" type="button">
Expand Down Expand Up @@ -118,8 +118,15 @@ <h2 style="align-self: center">Filter by Gene Name</h2>
const filters = getSelectedFilters();
const num_clusters = document.getElementById('clusters').value;
const type = "{{ type }}";
console.log("Number of clusters: ", num_clusters);
console.log("Type: ", type);
// Handle edge case where only DPA1 is selected
if(filters === encodeURIComponent('gene_name:==:DPA1')){
alert("Insufficient data for this filter alone. Please select another filter.");
return;
}
if (filters === '') {
alert("Please select at least one filter.");
return;
}
// Call the generateSOM function with the filters, type and number of clusters
generateSOM(filters, type, num_clusters);
}
Expand Down

0 comments on commit 2a613a5

Please sign in to comment.