Skip to content

Commit

Permalink
Merge pull request #30 from VasudhaJha/feature/export-all-files
Browse files Browse the repository at this point in the history
Feature/export all files
  • Loading branch information
VasudhaJha authored Jul 17, 2023
2 parents 348f7b4 + 5727938 commit b2aa869
Show file tree
Hide file tree
Showing 29 changed files with 954 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
steps:
- uses: rymndhng/release-on-push-action@master
with:
bump_version_scheme: patch # can be either "major", "minor", "patch" or "norelease"
bump_version_scheme: minor # can be either "major", "minor", "patch" or "norelease"
tag_prefix: v
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ plt.figure(1) # Plot the first genomap
plt.imshow(findI, origin = 'lower', extent = [0, 10, 0, 10], aspect = 1)
plt.title('Genomap of a cell from TM dataset')
```

### Example 2 - Try genoVis for data visualization and clustering

```python
Expand Down Expand Up @@ -274,4 +275,3 @@ Islam, M.T., Xing, L. Cartography of Genomic Interactions Enables Deep Analysis




1 change: 1 addition & 0 deletions genomap/genoClassification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .genoClassification import *
65 changes: 65 additions & 0 deletions genomap/genoClassification/genoClassification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

"""
Created on Sun Jul 16 21:27:17 2023
@author: Md Tauhidul Islam, Research Scientist, Dept. of radiation Oncology, Stanford University
"""

import numpy as np
import os
import torch
from torch.utils.data import DataLoader, Dataset
import scipy

from genomap.genomap import construct_genomap
from genomap.utils.util_Sig import select_n_features
import genomap.genoNet as gNet

def genoClassification(training_data, training_labels, test_data, rowNum=32, colNum=32, epoch=100):

# training_data: numpy array in cellxgene shape
# training_labels: numpy array
# test_data: numpy array in cellxgene shape
# rowNum: row number of genomap
# colNum: column number of genomap
# epoch: epoch number for training

data = np.concatenate((training_data, test_data), axis=0)
# Construction of genomaps
nump=rowNum*colNum
if nump<data.shape[1]:
data,index=select_n_features(data,nump)

dataNorm=scipy.stats.zscore(data,axis=0,ddof=1)
genoMaps=construct_genomap(dataNorm,rowNum,colNum,epsilon=0.0,num_iter=200)

# Split the data for training and testing
dataMat_CNNtrain = genoMaps[:training_labels.shape[0]]
dataMat_CNNtest = genoMaps[training_labels.shape[0]:]

groundTruthTrain = training_labels
classNum = len(np.unique(groundTruthTrain))

# Preparation of training and testing data for PyTorch computation
XTrain = dataMat_CNNtrain.transpose([0,3,1,2])
XTest = dataMat_CNNtest.transpose([0,3,1,2])
yTrain = groundTruthTrain

# Train the network in PyTorch framework
miniBatchSize = 128
net = gNet.traingenoNet(XTrain, yTrain, maxEPOCH=epoch, batchSize=miniBatchSize, verbose=True)

# # Process the test data
yTest = np.random.rand(dataMat_CNNtest.shape[0])
testset = gNet.geneDataset(XTest, yTest)
testloader = gNet.DataLoader(testset, batch_size=miniBatchSize, shuffle=False)
device = gNet.get_device()

# Perform data classification
prob_test = gNet.predict(net, testloader, device)
pred_test = np.argmax(prob_test, axis=-1)

return pred_test




1 change: 1 addition & 0 deletions genomap/genoDR/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .genoDimReduction import *
67 changes: 67 additions & 0 deletions genomap/genoDR/genoDimReduction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 16:11:15 2023
@author: Md Tauhidul Islam, Ph.D., Dept. of Radiation Oncology, Stanford University
"""
import numpy as np
import pandas as pd
import scanpy as sc
from sklearn.feature_selection import VarianceThreshold
from tensorflow.keras.optimizers import Adam
import umap

from genomap.genomap import construct_genomap
from genomap.utils.ConvIDEC import ConvIDEC
from genomap.utils.gTraj_utils import nearest_divisible_by_four
from genomap.utils.util_Sig import select_n_features

def genoDR(data,n_dim=32, n_clusters=None, colNum=32,rowNum=32,batch_size=64,verbose=1,
pretrain_epochs=100,maxiter=300):

# Construction of genomap
colNum=nearest_divisible_by_four(colNum)
rowNum=nearest_divisible_by_four(rowNum)
nump=rowNum*colNum
if nump<data.shape[1]:
data,index=select_n_features(data,nump)

genoMaps=construct_genomap(data,rowNum,colNum,epsilon=0.0,num_iter=200)

if n_clusters==None:

adata = convertToAnnData(data)
# Perform Louvain clustering
sc.pp.neighbors(adata)
sc.tl.louvain(adata, resolution=1.0)
# Access the cluster assignments
cluster_labels = adata.obs['louvain']
n_clusters = len(np.unique(cluster_labels))

# Deep learning-based dimensionality reduction and clustering
optimizer = Adam()
model = ConvIDEC(input_shape=genoMaps.shape[1:], filters=[32, 64, 128, n_dim], n_clusters=n_clusters)
model.compile(optimizer=optimizer, loss=['kld', 'mse'], loss_weights=[0.1, 1.0])
pretrain_optimizer ='adam'
update_interval=50
model.pretrain(genoMaps, y=None, optimizer=pretrain_optimizer, epochs=pretrain_epochs, batch_size=batch_size,
verbose=verbose)

y_pred = model.fit(genoMaps, y=None, maxiter=maxiter, batch_size=batch_size, update_interval=update_interval,
)
y_pred = model.predict_labels(genoMaps)
feat_DNN=model.extract_features(genoMaps)
#embedding2D = umap.UMAP(n_neighbors=30,min_dist=0.3,n_epochs=200).fit_transform(feat_DNN)
return feat_DNN


def convertToAnnData(data):
# Create pseudo cell names
cell_names = ['Cell_' + str(i) for i in range(1, data.shape[0]+1)]
# Create pseudo gene names
gene_names = ['Gene_' + str(i) for i in range(1, data.shape[1]+1)]
# Create a pandas DataFrame
df = pd.DataFrame(data, index=cell_names, columns=gene_names)
adataMy=sc.AnnData(df)
return adataMy

14 changes: 5 additions & 9 deletions genomap/genoMOI/genoMOI.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"""

from tensorflow.keras.optimizers import Adam
from genomap.utils import ConvIDEC
from genomap.utils.ConvIDEC import ConvIDEC
from sklearn.feature_selection import VarianceThreshold
from genomap.genomap import construct_genomap
import umap
from genomap.utils.gTraj_utils import nearest_divisible_by_four
from genomap.utils.utils_MOI import *
from genomap.utils.utils_MOI import *
from genomap.utils.util_Sig import select_n_features

def compute_genoMOI(*arrays,n_clusters=None, colNum, rowNum):
def genoMOI(*arrays,n_clusters=None, colNum, rowNum):

# arrays: number of arrays such as array1,array2
# n_clusters: number of data classes
Expand Down Expand Up @@ -50,12 +51,7 @@ def extract_genoVis_features(data,n_clusters=20, colNum=32,rowNum=32,batch_size=
rowNum=nearest_divisible_by_four(rowNum)
nump=rowNum*colNum
if nump<data.shape[1]:
# create an instance of the VarianceThreshold class
selector = VarianceThreshold( )
# fit the selector to the data and get the indices of the top n most variable features
var_threshold = selector.fit(data)
top_n_indices = var_threshold.get_support(indices=True)
data=data[:,top_n_indices[0:nump]]
data,index=select_n_features(data,nump)
genoMaps=construct_genomap(data,rowNum,colNum,epsilon=0.0,num_iter=200)

# Deep learning-based dimensionality reduction and clustering
Expand Down
1 change: 1 addition & 0 deletions genomap/genoNet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .genoNet import *
8 changes: 3 additions & 5 deletions genomap/genoNet/genoNet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
"""
Supervised genoNet
Created on Sun May 15 09:03:29 2022

@author: anonymous
"""
Created on Sun Jul 16 21:27:17 2023
@author: Md Tauhidul Islam, Research Scientist, Dept. of radiation Oncology, Stanford University
"""

import os
Expand Down
1 change: 1 addition & 0 deletions genomap/genoNetRegression/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .genoNet_regression import *
Loading

0 comments on commit b2aa869

Please sign in to comment.