Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dyneusr-fire #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/dyneusr_fire/run_dyneusr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# example command-line
dyneusr-fire load_data --X trefoil.npy --y trefoil-target.npy - run_mapper - visualize
4 changes: 4 additions & 0 deletions examples/dyneusr_fire/run_dyneusr_interactive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# example command-line
dyneusr-fire init -- --interactive
Binary file added examples/dyneusr_fire/trefoil-target.npy
Binary file not shown.
Binary file added examples/dyneusr_fire/trefoil.npy
Binary file not shown.
3 changes: 3 additions & 0 deletions requirements-conda.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ scikit-learn
matplotlib
seaborn
networkx
umap-learn
hdbscan
fire
3 changes: 3 additions & 0 deletions requirements-versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ seaborn==0.9.0
networkx==2.2
nilearn==0.5.0a
kmapper==1.2.0
umap-learn
hdbscan
fire
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ seaborn
networkx
nilearn
kmapper>=1.2
umap-learn
hdbscan
fire
166 changes: 166 additions & 0 deletions scripts/dyneusr-fire
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#!/usr/bin/env python

import numpy as np
import pandas as pd
from sklearn.datasets.base import Bunch
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.cluster import KMeans, DBSCAN
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from umap.umap_ import UMAP
from hdbscan import HDBSCAN
from kmapper import KeplerMapper, Cover
from dyneusr import DyNeuGraph

def check_estimator(estimator):
"""Check estimator and process if not valid.
"""
if isinstance(estimator, str):
try:
estimator = eval(estimator)
except NameError as e:
# TOOD: show use a list of valid estimators
print('valid projections: PCA, TSNE, UMAP')
print('valid scalers: MinMaxScaler, StandardScaler')
print('valid clusterers: KMeans, DBSCAN, HDBSCAN')
raise e
return estimator


class DyNeuSR(object):

def init(self):
return self

def load_data(self, X=None, y=None):
"""Load the data.

Parameters
----------
X : str
Filename of data matrix to load.

y : str, optional
Filename of meta data to load.

"""
# Helper functions
def check_array_from_file(fp):
print("Loading data from file:", fp)
d = None
if str(fp).endswith('.npy'):
d = np.load(fp)
elif str(fp).endswith('.npz'):
d = np.loadz(fp)
d = d[list(d.keys())[0]]
elif str(fp).endswith('.tsv'):
d = pd.read_table(fp)
elif str(fp).endswith('.csv'):
d = pd.read_csv(fp)
elif str(fp).endswith('.txt'):
d = np.genfromtxt(fp)
else:
print('Data format not recognized ...')
print('Please use an accepted format:')
print('\t.npy')
print('\t.npz')
print('\t.tsv')
print('\t.csv')
print('\t.txt')
return d

# Load the data from a file.
X = check_array_from_file(X)
y = check_array_from_file(y)
dataset = Bunch(data=X, target=y)

# Store as variables
self.dataset = dataset
self.X = X
self.y = y
return self


def load_example(self, size=100):
"""Load the data.

TODO
----
- generalize to load any dataset supplied by the user

"""
# Generate synthetic dataset (for now)
from dyneusr.datasets import make_trefoil
dataset = make_trefoil(size=size)
X = dataset.data
y = dataset.target

# Store variables
self.dataset = dataset
self.X = X
self.y = y
return self


def run_mapper(self,
projection=TSNE(2),
scaler=MinMaxScaler(),
resolution=6, gain=0.2,
clusterer=KMeans(2),
verbose=1):
"""Run KeplerMapper.
"""
# Generate shape graph using KeplerMapper
mapper = KeplerMapper(verbose=verbose)

# Check estimators
self._projection = check_estimator(projection)
self._scaler = check_estimator(scaler)
self._cover = Cover(resolution, gain)
self._clusterer = check_estimator(clusterer)

# Run kmapper
lens = mapper.fit_transform(
self.X,
projection=self._projection,
scaler=self._scaler
)
graph = mapper.map(
lens, self.X,
cover=self._cover,
clusterer=self._cluster
)

# Store results
self.lens = lens
self.graph = graph
return self


def visualize(self,
save_as='dyneusr_output.html',
template=None,
static=True,
show=True,
port=None):
"""Visualize the graph using DyNeuSR
"""
# Visualize the shape graph using DyNeuSR's DyNeuGraph
dG = DyNeuGraph(G=self.graph, y=self.y)
dG.visualize(
save_as,
template=template,
static=static,
show=show,
port=port
)

# Store the results
self.dG = dG
return self



if __name__=='__main__':
import fire
fire.Fire(DyNeuSR)
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from setuptools import find_packages, setup
import re
import os

# parse dyneusr/_version.py
try:
Expand All @@ -25,6 +26,7 @@
setup(
name='dyneusr',
version=version,
scripts=[os.path.join('scripts/dyneusr-fire')],
description='Dynamical Neural Spatiotemporal Representations.',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down