Skip to content

Commit

Permalink
Fix CBCL example
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed May 22, 2024
1 parent e7efda8 commit 4c2f59e
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions examples/cbcl/cbcl.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import numpy, datetime, json, subprocess, sys, os, glob
from PIL import Image
import scipy.misc

def load(dir):
images = []
vecs = []
for f in glob.glob(os.path.join(dir,'*.pgm')):
image = numpy.array(scipy.misc.imread(f))
image = numpy.array(Image.open(f))
images.append((f,image))
vecs.append(image.ravel())
return numpy.vstack(vecs), images

def embed(feature_matrix):
input_file = 'tmp_cbcl_input'
numpy.savetxt(input_file,feature_matrix)
numpy.savetxt(input_file, feature_matrix, delimiter=',')
output_file = 'tmp_cbcl_output.dat'
run_string = './bin/tapkee_cli -i %s -o %s -m ltsa -k 20 --transpose --verbose --benchmark' % (input_file,output_file)
output = subprocess.check_output(run_string, shell=True)
embedding = numpy.loadtxt(output_file)
os.remove(output_file)
runner_string = './bin/tapkee -i %s -o %s -m ltsa -k 80 --transpose-output --verbose --benchmark' % (input_file, output_file)
process = subprocess.run(runner_string, shell=True, capture_output=True, text=True)
print(process.stderr)
if process.returncode != 0:
raise Exception('Failed to embed')
embedding = numpy.loadtxt(output_file, delimiter=',')
return embedding

def export_json(outfile,embedding,images):
def export_json(outfile, embedding, images):
json_dict = {}
N = embedding.shape[1]
print 'N', N
import scipy.misc
json_dict['data'] = [{'cx':embedding[0,i], 'cy':embedding[1,i], 'fname':images[i][0]} for i in xrange(N)]
json.dump(json_dict, open(outfile, 'w'))

Expand All @@ -34,21 +35,21 @@ def plot_embedding(embedding,images):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(embedding[0],embedding[1],alpha=0.0)
for i in xrange(embedding.shape[1]):
img = numpy.zeros((images[i][1].shape[0],images[i][1].shape[1],4))
img[:,:,0] = 255*images[i][1]
img[:,:,1] = 255*images[i][1]
img[:,:,2] = 255*images[i][1]
img[:,:,3] = 1
img[(images[i][1]==28),3] = 0
for i in range(embedding.shape[1]):
img = numpy.zeros((images[i][1].shape[0], images[i][1].shape[1], 4))
img[:,:,0] = images[i][1]/255.0
img[:,:,1] = images[i][1]/255.0
img[:,:,2] = images[i][1]/255.0
img[:,:,3] = 1.0
img[(images[i][1]==28), 3] = 0
imagebox = OffsetImage(img,cmap=plt.cm.gray,zoom=0.2)
ab = AnnotationBbox(imagebox, (embedding[0][i], embedding[1,i]),pad=0.001,frameon=False)
ab = AnnotationBbox(imagebox, (embedding[0][i], embedding[1,i]), pad=0.001, frameon=False)
ax.add_artist(ab)
plt.show()

if __name__ == "__main__":
feature_matrix, images = load('data/cbcl')
embedding = embed(feature_matrix)
if len(sys.argv)==3:
export_json(sys.argv[2],embedding, images)
export_json(sys.argv[2], embedding, images)
plot_embedding(embedding,images)

0 comments on commit 4c2f59e

Please sign in to comment.