Skip to content

Commit

Permalink
Merge pull request #99 from erinwild-rubikloud/erinwild/cosmetic_cleanup
Browse files Browse the repository at this point in the history
Delete .pyc and .directory files + linting of whitespace and imports
  • Loading branch information
sevamoo authored Feb 20, 2019
2 parents 76b60eb + c0c6b28 commit 0cf04fb
Show file tree
Hide file tree
Showing 22 changed files with 309 additions and 193 deletions.
11 changes: 0 additions & 11 deletions .directory

This file was deleted.

125 changes: 124 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,124 @@

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# VS Code project settings
.vscode/

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# macOS
.DS_Store

.directory
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ python setup.py install


Many thanks to @sebastiandev, the library is now standardized in a pythonic tradition. Below you can see some basic examples, showing how to use the library.
But I recommend you to go through the codes. There are several functionalities already implemented, but not documented. I would be very happy to add your new examples here.
But I recommend you to go through the codes. There are several functionalities already implemented, but not documented. I would be very happy to add your new examples here.

[Basice Example](https://gist.github.com/sevamoo/035c56e7428318dd3065013625f12a11)
[Basic Example](https://gist.github.com/sevamoo/035c56e7428318dd3065013625f12a11)

### Citation

Expand All @@ -42,7 +42,7 @@ There is no published paper about this library. However if possible, please cite
Main Contributers:
Vahid Moosavi @sevamoo
Sebastian Packmann @sebastiandev
Iván Vallés @ivallesp
Iván Vallés @ivallesp
```


Expand All @@ -51,5 +51,5 @@ For more information, you can contact me via [email protected] or [email protected].



Thanks a lot.
Thanks a lot.
Best Vahid Moosavi
5 changes: 1 addition & 4 deletions sompy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

from logging.config import dictConfig
import matplotlib

import matplotlib
#matplotlib.use('Agg') # Use whatever backend is available

dictConfig({
Expand All @@ -25,7 +24,5 @@
}
})



from .sompy import SOMFactory
from .visualization import *
26 changes: 13 additions & 13 deletions sompy/codebook.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numpy as np
import scipy as sp

from sklearn.decomposition import PCA
#from sklearn.decomposition import RandomizedPCA# (randomizedpca is deprecated)

from .decorators import timeit


Expand All @@ -13,13 +12,14 @@ class InvalidNodeIndexError(Exception):
class InvalidMapsizeError(Exception):
pass


def generate_hex_lattice(n_rows, n_columns):
x_coord = []
y_coord = []
for i in range(n_rows):
for j in range(n_columns):
x_coord.append(i*1.5)
y_coord.append(np.sqrt(2/3)*(2*j+(1+i)%2))
x_coord.append(i * 1.5)
y_coord.append(np.sqrt(2 / 3) * (2 * j + (1 + i) % 2))
coordinates = np.column_stack([x_coord, y_coord])
return coordinates

Expand All @@ -35,13 +35,13 @@ def __init__(self, mapsize, lattice='rect'):
elif 1 == len(mapsize):
_size = [1, mapsize[0]]
print('input was considered as the numbers of nodes')
print('map size is [{dlen},{dlen}]'.format(dlen=int(mapsize[0]/2)))
print('map size is [{dlen},{dlen}]'.format(dlen=int(mapsize[0] / 2)))
else:
raise InvalidMapsizeError(
"Mapsize is expected to be a 2 element list or a single int")

self.mapsize = _size
self.nnodes = mapsize[0]*mapsize[1]
self.nnodes = mapsize[0] * mapsize[1]
self.matrix = np.asarray(self.mapsize)
self.initialized = False

Expand All @@ -59,7 +59,7 @@ def random_initialization(self, data):
"""
mn = np.tile(np.min(data, axis=0), (self.nnodes, 1))
mx = np.tile(np.max(data, axis=0), (self.nnodes, 1))
self.matrix = mn + (mx-mn)*(np.random.rand(self.nnodes, data.shape[1]))
self.matrix = mn + (mx - mn) * (np.random.rand(self.nnodes, data.shape[1]))
self.initialized = True

@timeit()
Expand Down Expand Up @@ -108,8 +108,8 @@ def pca_linear_initialization(self, data):

mx = np.max(coord, axis=0)
mn = np.min(coord, axis=0)
coord = (coord - mn)/(mx-mn)
coord = (coord - .5)*2
coord = (coord - mn) / (mx - mn)
coord = (coord - .5) * 2
me = np.mean(data, 0)
data = (data - me)
tmp_matrix = np.tile(me, (self.nnodes, 1))
Expand All @@ -122,11 +122,11 @@ def pca_linear_initialization(self, data):
eigvec = pca.components_
eigval = pca.explained_variance_
norms = np.sqrt(np.einsum('ij,ij->i', eigvec, eigvec))
eigvec = ((eigvec.T/norms)*eigval).T
eigvec = ((eigvec.T / norms) * eigval).T

for j in range(self.nnodes):
for i in range(eigvec.shape[0]):
tmp_matrix[j, :] = tmp_matrix[j, :] + coord[j, i]*eigvec[i, :]
tmp_matrix[j, :] = tmp_matrix[j, :] + coord[j, i] * eigvec[i, :]

self.matrix = np.around(tmp_matrix, decimals=6)
self.initialized = True
Expand Down Expand Up @@ -169,7 +169,7 @@ def _rect_dist(self, node_ind):
dist = None

# bmu should be an integer between 0 to no_nodes
if 0 <= node_ind <= (rows*cols):
if 0 <= node_ind <= (rows * cols):
node_col = int(node_ind % cols)
node_row = int(node_ind / cols)
else:
Expand All @@ -179,7 +179,7 @@ def _rect_dist(self, node_ind):
if rows > 0 and cols > 0:
r = np.arange(0, rows, 1)[:, np.newaxis]
c = np.arange(0, cols, 1)
dist2 = (r-node_row)**2 + (c-node_col)**2
dist2 = (r - node_row)**2 + (c - node_col)**2

dist = dist2.ravel()
else:
Expand Down
7 changes: 4 additions & 3 deletions sompy/neighborhood.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import inspect
import sys

import numpy as np

small = .000000000001


Expand All @@ -24,7 +25,7 @@ class GaussianNeighborhood(object):

@staticmethod
def calculate(distance_matrix, radius, dim):
return np.exp(-1.0*distance_matrix/(2.0*radius**2)).reshape(dim, dim)
return np.exp(-1.0 * distance_matrix / (2.0 * radius**2)).reshape(dim, dim)

def __call__(self, *args, **kwargs):
return self.calculate(*args)
Expand All @@ -38,7 +39,7 @@ class BubbleNeighborhood(object):
def calculate(distance_matrix, radius, dim):
def l(a, b):
c = np.zeros(b.shape)
c[a-b >= 0] = 1
c[a - b >= 0] = 1
return c

return l(radius,
Expand Down
9 changes: 5 additions & 4 deletions sompy/normalization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import sys
import inspect
import sys

import numpy as np


class NormalizerFactory(object):
Expand Down Expand Up @@ -37,12 +38,12 @@ def _mean_and_standard_dev(self, data):
def normalize(self, data):
me, st = self._mean_and_standard_dev(data)
st[st == 0] = 1 # prevent: when sd = 0, normalized result = NaN
return (data-me)/st
return (data - me) / st

def normalize_by(self, raw_data, data):
me, st = self._mean_and_standard_dev(raw_data)
st[st == 0] = 1 # prevent: when sd = 0, normalized result = NaN
return (data-me)/st
return (data - me) / st

def denormalize_by(self, data_by, n_vect):
me, st = self._mean_and_standard_dev(data_by)
Expand Down
Loading

0 comments on commit 0cf04fb

Please sign in to comment.