-
Notifications
You must be signed in to change notification settings - Fork 4
/
figure-cube.py
91 lines (84 loc) · 3.17 KB
/
figure-cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 Nicolas Rougier - INRIA - CORTEX Project
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact: CORTEX Project - INRIA
# INRIA Lorraine,
# Campus Scientifique, BP 239
# 54506 VANDOEUVRE-LES-NANCY CEDEX
# FRANCE
if __name__ == '__main__':
import os
import numpy as np
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from network import NG,SOM,DSOM
n = 32
epochs = 10000
N = 5000
# Pick N points on a cube uniformly
np.random.seed(123)
samples = np.zeros((N,3))
samples[:,0] = np.random.uniform(low=0,high=1,size=N)
samples[:,1] = np.random.uniform(low=0,high=1,size=N)
samples[:,2] = np.random.uniform(low=0,high=1,size=N)
for i in range(N):
index = int(np.random.random()*3)
value = int(np.random.random()*2)
samples[i,index] = value
x = samples[:,0]
y = samples[:,1]
z = samples[:,2]
# Learn
np.random.seed(123)
dsom = DSOM((n,n,3), elasticity=1.0, init_method='fixed')
dsom.learn(samples, epochs)
fig = plt.figure(figsize=(10,10))
axes = Axes3D(fig)
axes.scatter(x,y,z)
C = dsom.codebook
Cx,Cy,Cz = C[...,0], C[...,1], C[...,2]
for i in range(C.shape[0]):
axes.plot (Cx[i,:], Cy[i,:], Cz[i,:], 'k', alpha=0.85, lw=1.5)
for i in range(C.shape[1]):
axes.plot (Cx[:,i], Cy[:,i], Cz[:,i], 'k', alpha=0.85, lw=1.5)
axes.scatter (Cx.flatten(), Cy.flatten(), Cz.flatten(), s=50, c= 'w', edgecolors='k', zorder=10)
file = open('cube.plot', 'w')
file.write('''set parametric\n''')
file.write('''set hidden3d\n''')
file.write('''unset key\n''')
file.write('''set style line 1 lw 1 lc rgb "#000000"\n''')
file.write('''set style line 2 lw 1 lc rgb "#999999"\n''')
file.write('''set style increment user\n''')
file.write('''set xrange [0:1]\n''')
file.write('''set yrange [0:1]\n''')
file.write('''set zrange [0:1]\n''')
file.write('''set style data line\n''')
file.write('''set ticslevel 0\n''')
file.write('''set size ratio 1\n''')
file.write('''set view 45,200\n''')
file.write('''set terminal svg size 512,512\n''')
file.write('''set output 'cube.svg'\n''')
file.write('''splot '-' using 1:2:3\n\n''')
for i in range(C.shape[0]):
for j in range(C.shape[1]):
file.write('%.3f %.3f %.3f\n' % (C[i,j,0],C[i,j,1],C[i,j,2]))
file.write('''\n''')
os.system('gnuplot cube.plot')
plt.show()