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

python: Add conda env and minor #12

Open
wants to merge 3 commits into
base: julia-python-packages
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
42 changes: 35 additions & 7 deletions python/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@

The ~Python~ interface calls the ~Julia~ ~SG-t-SNE-Π~ library, through the
~PyJulia~ package. Please follow the [[https://pyjulia.readthedocs.io/en/latest/installation.html][official installation instructions]]
to get ~PyJulia~ working. You can verify that your installation is valid
by issuing the following command. More details are available in the
to get ~PyJulia~ working.
To verify that your installation is completed succesfully try to issue the following command.

#+begin_src python
from julia import Base
Base.sind(90)
#+end_src

More details on the PyJulia package are available in the
[[https://pyjulia.readthedocs.io/en/latest/usage.html][official usage tutorial]].

You need to do either one of the following for ~PyJulia~ to work:
Expand All @@ -26,13 +33,34 @@ You need to do either one of the following for ~PyJulia~ to work:
python-jl
#+end_src
which is bundled in PyJulia.

You can verify your installation by issuing
#+begin_src python
from julia import Base
Base.sind(90)

To install the python wrapper of SG-t-SNE-Π to your python virtual environment or a conda environment follow the instructions below:

To install using Conda:

#+begin_src bash
git clone https://github.com/fcdimitr/sgtsnepi.git
cd sgtsnepi
conda env create -f python/environment.yml
source activate sgtsnepi
python-jl python/test_digits.py
#+end_src

To install to a new python virtual environment:

#+begin_src bash
python3 -m venv /path/to/new/virtual/environment
source /path/to/new/virtual/environment/bin/activate
git clone https://github.com/fcdimitr/sgtsnepi.git
cd sgtsnepi
pip install -r python/requirements.txt
python-jl python/test_digits.py
#+end_src





* SG-t-SNE-Π usage

The ~SG-t-SNE-Π~ Python module is now ready to be used. Simply import the module and call ~SG-t-SNE-Π~ on a sparse, CSC matrix.
Expand Down
1 change: 1 addition & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sgtsnepi import * # noqa
8 changes: 8 additions & 0 deletions python/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: sgtsnepi
channels:
- defaults
- conda-forge
dependencies:
- pip
- pip:
- -r file:requirements.txt
14 changes: 9 additions & 5 deletions python/sgtsnepi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from julia import Main
from julia import Pkg


# load packages in Julia
Pkg.add( "SparseArrays" )
Pkg.add( Pkg.PackageSpec(name="SGtSNEpi", version="0.2.1") )
Pkg.add("SparseArrays")
Pkg.add(Pkg.PackageSpec(name="SGtSNEpi", version="0.2.1"))

from julia import SGtSNEpi


# prepare Julia wrappers
Main.eval("""
using SparseArrays, SGtSNEpi
Expand All @@ -22,7 +24,9 @@
sgtsnepi( A::PyObject ; kwargs... ) = sgtsnepi( scipyCSC_to_julia(A) ; kwargs... )
""")

# setup Python function
__all__ = ['sgtsnepi']


def sgtsnepi( A, **kwargs ):
return SGtSNEpi.sgtsnepi( A, **kwargs )
# Setup Python function
def sgtsnepi(A, **kwargs):
return SGtSNEpi.sgtsnepi(A, **kwargs)
79 changes: 41 additions & 38 deletions python/test_digits.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
## test_digits.py
# Test 3D SG-t-SNE embedding

## download the matrix and label file from Tim Davids' matrix collection
from julia.api import Julia
import requests
url = "https://suitesparse-collection-website.herokuapp.com/MM/ML_Graph/optdigits_10NN.tar.gz"
r = requests.get(url, allow_redirects=True)
open('optdigits_10NN.tar.gz', 'wb').write(r.content)
from scipy.io import mmread
from scipy.sparse import csc_matrix
import sys
import os.path
from os import path
import tarfile
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

# SG-tSNE-Π setup
# Setup julia
jl = Julia(compiled_modules=False)

## unzip to the current folder
import tarfile
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir) # Add path

tar = tarfile.open('./optdigits_10NN.tar.gz', 'r:gz')
tar.extractall()
tar.close()
from sgtsnepi import sgtsnepi

## reading out the matrix and the label
from scipy.io import mmread
from scipy.sparse import csc_matrix
if path.exists("optdigits_10NN.tar.gz"):
pass
else:
print("Downloading and extracting file")
# Download the matrix and label file from Tim Davids' matrix collection
url = "https://suitesparse-collection-website.herokuapp.com/MM/ML_Graph/optdigits_10NN.tar.gz"
r = requests.get(url, allow_redirects=True)
open('optdigits_10NN.tar.gz', 'wb').write(r.content)

# Unzip to the current folder
tar = tarfile.open('./optdigits_10NN.tar.gz', 'r:gz')
tar.extractall()
tar.close()

# Reading out the matrix and the label

A = mmread('./optdigits_10NN/optdigits_10NN.mtx')
A = csc_matrix(A)
L = mmread('./optdigits_10NN/optdigits_10NN_label.mtx')
L = csc_matrix(L).toarray()

## SG-tSNE-Π setup
# setup julia
from julia.api import Julia
jl = Julia(compiled_modules=False)

# add current path
import sys
sys.path.insert(0,"./")

# generate the embedding
from sgtsnepi import sgtsnepi
Y = sgtsnepi(A, d = 3, λ = 10, max_iter = 500)

# visualization -- require matplotlib package
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
Y = sgtsnepi(A, d = 3, λ = 10, max_iter=500)

# distinguishable color map (10 clusters)
cmap = [[0.3000, 0.3000, 1.0000],
[0.3000, 1.0000, 0.3000],
[1.0000, 0.3000, 0.3000],
[1.0000, 0.8552, 0.3000],
[0.3000, 0.3000, 0.4931],
[1.0000, 0.3000, 0.8069],
[0.3000, 0.5655, 0.3000],
[0.5897, 0.3966, 0.3000],
[0.3000, 1.0000, 0.9034],
[0.3000, 0.7586, 1.0000]];
[0.3000, 1.0000, 0.3000],
[1.0000, 0.3000, 0.3000],
[1.0000, 0.8552, 0.3000],
[0.3000, 0.3000, 0.4931],
[1.0000, 0.3000, 0.8069],
[0.3000, 0.5655, 0.3000],
[0.5897, 0.3966, 0.3000],
[0.3000, 1.0000, 0.9034],
[0.3000, 0.7586, 1.0000]]
newcmap = ListedColormap(cmap)

# draw scatter plot (3D)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')

ax.scatter(Y[:,0], Y[:,1], Y[:,2], c=L, marker='.', cmap=newcmap)
ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], c=L, marker='.', cmap=newcmap)

ax.set_xlabel('X')
ax.set_ylabel('Y')
Expand Down
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()


setuptools.setup(
name='sgtsnepi',
version='0.1',
author="Dimitris Floros",
author_email=" ",
description="A Docker and AWS utility package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/fcdimitr/sgtsnepi",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)