File size: 1,803 Bytes
8d55ddb | 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 | import os
import shutil
class StreamingDemo(object):
def __init__(self, npoints=50, nbatches=5):
self.npoints = npoints
self.nbatches = nbatches
def params(*args, **kwargs):
""" Get analysis-specific parameters """
raise NotImplementedError
def run(*args, **kwargs):
""" Run the streaming demo """
raise NotImplementedError
@staticmethod
def make(demoname, *args, **kwargs):
""" Create a streaming demo """
from mlstreaming.kmeans import StreamingKMeans
DEMOS = {
'kmeans': StreamingKMeans
}
return DEMOS[demoname](*args, **kwargs)
def setup(self, path, overwrite=False):
""" Setup paths for a streaming demo where temporary data will be read / written"""
if os.path.isdir(path):
if overwrite:
shutil.rmtree(path)
os.mkdir(path)
else:
raise Exception('Base directory %s already exists and overwrite is set to False' % path)
else:
os.mkdir(path)
datain = os.path.join(path, 'input')
dataout = os.path.join(path, 'output')
if os.path.isdir(datain):
shutil.rmtree(datain)
if os.path.isdir(dataout):
shutil.rmtree(dataout)
os.mkdir(datain)
os.mkdir(dataout)
self.datain = datain
self.dataout = dataout
return self
def writepoints(self, pts, i):
""" Write data points in a form that can be read by MLlib's vector parser """
f = file(os.path.join(self.datain, 'batch%g.txt' % i), 'w')
s = map(lambda p: ",".join(str(p).split()).replace('[,', '[').replace(',]', ']'), pts)
tmp = "\n".join(s)
f.write(tmp)
f.close()
|