Skip to content
Permalink
Browse files

scatter json

  • Loading branch information...
basvandenberg committed Nov 28, 2014
1 parent 1dbb880 commit d59c7f02fb90b112f5ba4e00bec7a830bb1d9f84
Showing with 102 additions and 16 deletions.
  1. +5 −5 bin/classification
  2. +5 −5 spice/classification.py
  3. +89 −3 spice/featmat.py
  4. +3 −3 spice/plotpy/heatmap.py
@@ -11,11 +11,11 @@ import traceback
import numpy

# HACK TODO remove if sklearn is updated to 0.14 on compute servers...
import sklearn
if not(sklearn.__version__ == '0.14.1'):
sys.path.insert(1, os.environ['SKL'])
reload(sklearn)
assert(sklearn.__version__ == '0.14.1')
#import sklearn
#if not(sklearn.__version__ == '0.14.1'):
# sys.path.insert(1, os.environ['SKL'])
# reload(sklearn)
#assert(sklearn.__version__ == '0.14.1')

from sklearn.externals import joblib

@@ -5,11 +5,11 @@
import numpy

# HACK TODO remove if sklearn is updated to 0.14 on compute servers...
import sklearn
if not(sklearn.__version__ == '0.14.1'):
sys.path.insert(1, os.environ['SKL'])
reload(sklearn)
assert(sklearn.__version__ == '0.14.1')
#import sklearn
#if not(sklearn.__version__ == '0.14.1'):
# sys.path.insert(1, os.environ['SKL'])
# reload(sklearn)
#assert(sklearn.__version__ == '0.14.1')

from sklearn import svm
from sklearn import neighbors
@@ -728,7 +728,7 @@ def histogram_data(self, feat_id, labeling_name, class_ids=None,
if(min_val < 0.0):
while(start > min_val):
start -= step
elif(min_val > 0.0):
while(start < min_val):
start += step
@@ -750,6 +750,7 @@ def histogram_data(self, feat_id, labeling_name, class_ids=None,

# generate the bin edges
bin_edges = list(numpy.arange(start, end, step))
bin_edges.append(end)

max_count = 0
hists = {}
@@ -763,8 +764,8 @@ def histogram_data(self, feat_id, labeling_name, class_ids=None,
y_grid = []

if(max_count < 10):
y_grid = range(10)
if(max_count < 100):
y_grid = range(max_count + 1)
elif(max_count < 100):
t = (max_count / 10) + 1
y_grid = range(0, t * 10 + 1, t)
elif(max_count < 1000):
@@ -866,6 +867,91 @@ def save_histogram(self, feat_id, labeling_name, class_ids=None,

return out_f

def scatter_json(self, feat_id0, feat_id1, labeling_name=None,
class_ids=None, standardized=False, feat0_pre=None,
feat1_pre=None,):

try:
labeling = self.labeling_dict[labeling_name]
except KeyError:
raise ValueError('Labeling does not exist: %s.' % (labeling_name))

if not(labeling_name):
labeling_name = self.labeling_dict[sorted(
self.labeling_dict.keys())[0]].name

if not(class_ids):
class_ids = self.labeling_dict[labeling_name].class_names

try:
feature_index0 = self.feature_ids.index(feat_id0)
feature_index1 = self.feature_ids.index(feat_id1)
except ValueError:
raise ValueError('Feature %s or %s does not exist.' %
(feat_id0, feat_id1))

feat_name0 = self.feature_names[feat_id0]
feat_name1 = self.feature_names[feat_id1]

if(feat0_pre):
feat_name0 = ' - '.join([feat0_pre, feat_name0])
if(feat1_pre):
feat_name1 = ' - '.join([feat1_pre, feat_name1])

if(standardized):
# standardize data NOTE that fm is standardized before the objects
# are sliced out!!!
# not sure if this is the desired situation...
fm = self.standardized()
else:
fm = self.feature_matrix

legend = []
scatters = {}

xmin = 10000
xmax = -10000
ymin = 10000
ymax = -10000

# for each class id, add object ids that have that class label
for index, class_id in enumerate(class_ids):

object_is = labeling.object_indices_per_class[class_id]
x = fm[object_is, feature_index0]
y = fm[object_is, feature_index1]
#test x = list(numpy.arange(0.0, 1.0, 0.005))
#test y = list(numpy.arange(0.0, 1.0, 0.005))

xmin = min(xmin, min(x))
xmax = max(xmax, max(x))
ymin = min(ymin, min(y))
ymax = max(ymax, max(y))

legend.append(class_id)
scatters[class_id] = zip(x, y)

grid_size = 20.0

step = (xmax - xmin) / grid_size
xgrid = list(numpy.arange(xmin, xmax, step))
xgrid.append(xmax)

step = (ymax - ymin) / grid_size
ygrid = list(numpy.arange(ymin, ymax, step))
ygrid.append(ymax)

scatter_data = {}
scatter_data['legend'] = legend
for item in legend:
scatter_data[item] = scatters[item]
scatter_data['x-label'] = feat_name0
scatter_data['y-label'] = feat_name1
scatter_data['x-grid'] = xgrid
scatter_data['y-grid'] = ygrid

return json.dumps(scatter_data)

def save_scatter(self, feat_id0, feat_id1, labeling_name=None,
class_ids=None, colors=None, img_format='png',
root_dir='.', feat0_pre=None, feat1_pre=None,
@@ -12,15 +12,15 @@

def heatmap_fig(data, xlab, ylab, file_name, vmin=-3.0, vmax=3.0):

fig = pyplot.figure(figsize=(8,8))
fig = pyplot.figure(figsize=(30,30))
gs = gridspec.GridSpec(1, 2, width_ratios=[20, 1])

ax0 = pyplot.subplot(gs[0, 0])
_heatmap_axes(ax0, data, xlab, ylab, vmin, vmax)

ax1 = pyplot.subplot(gs[0, 1])
#colorbar.ColorbarBase(ax1, cmap=my_cmap())
colorbar.ColorbarBase(ax1, cmap=cm.get_cmap('RdBu'))
#colorbar.ColorbarBase(ax1, cmap=cm.get_cmap('RdBu'))
step = (vmax - vmin) / 10.0
ticklab = list(numpy.arange(vmin, vmax, step))
ticklab.append(vmax)
@@ -102,7 +102,7 @@ def heatmap_labeled_fig(data, xlab, ylab, label_lists, class_names, file_path,
'''

fig.savefig(file_path + '.png', bbox_inches='tight')
fig.savefig(file_path + '.svg', bbox_inches='tight')
#fig.savefig(file_path + '.svg', bbox_inches='tight')
#fig.savefig(file_path + '.png')
#fig.savefig(file_path + '.svg')

0 comments on commit d59c7f0

Please sign in to comment.
You can’t perform that action at this time.