def main(): gravityNode = GravityNode() count = 0 while gravityNode.init("PythonTest") != gravity.SUCCESS and count < 5: Log.warning("failed to init, retrying...") time.sleep(1) count += 1 if count == 5: Log.critical("Could not connect to ServiceDirectory") return 1 ret = testPubSub(gravityNode) if ret != 0: return ret ret = testService(gravityNode) if ret != 0: return ret ret = testHB(gravityNode) if ret != 0: return ret ret = testServiceTimeout(gravityNode) if ret != 0: return ret Log.message("Python tests successful!") return 0
def createTempService(): tempGravityNode = GravityNode() count = 0 while tempGravityNode.init("TempNode") != gravity.SUCCESS and count < 5: Log.warning("failed to init, retrying...") time.sleep(1) count += 1 if count == 5: Log.critical("Could not connect to ServiceDirectory") return 1 testProv = TestProvider() tempGravityNode.registerService("TempService", gravity.TCP, testProv) time.sleep(2) # give the registration time to complete return 0
if pointPB.name not in self.sample_data: self.sample_data[pointPB.name] = [] self.sample_data[pointPB.name].append( (pointPB.timestamp, pointPB.value)) # See if we have enough data from every stream to do a run of the autoencoder # A real example would probably need to time-align and possibly interpolate the data min_length = reduce( lambda a, b: min(a, len(self.sample_data[b])), self.sample_data, len(self.sample_data[list(self.sample_data)[0]])) if min_length >= model.n_steps: data.put(self.sample_data) self.sample_data = {} gn = GravityNode() while gn.init("AnomalyDetector") != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) model_filename = gn.getStringParam("model_file", "model.json") model = GravityModel(model_filename) mySub = MySubscriber() channel = gn.getStringParam("subscription_name", "channel") gn.subscribe(channel, mySub) while True: d = data.get() mse = model.ComputeMSE(d) Log.message("MSE = %f" % (mse))
#** License along with this program; #** If not, see <http://www.gnu.org/licenses/>. #** import time, sys from gravity import GravityNode, GravityDataProduct, gravity, GravitySubscriber, Log from DataPoint_pb2 import DataPointPB from datetime import datetime import numpy as np name = 'Pub' anomaly_point = None if len(sys.argv) > 1: anomaly_point = int(sys.argv[1]) gn = GravityNode() while gn.init(name) != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) inc1 = gn.getFloatParam("increment1", 0.05) inc2 = gn.getFloatParam("increment2", 0.05) channel = gn.getStringParam("subscription_name", "channel") gn.registerDataProduct(channel, gravity.TCP) pointPB1 = DataPointPB() pointPB1.name = "Pub1" gdp1 = GravityDataProduct(channel) pointPB2 = DataPointPB() pointPB2.name = "Pub2"
has_enough_data = True for k,v in training_data.items(): if len(v) < training_size: has_enough_data = False break if has_enough_data: Log.message("Calling trainmodel") self.train_state = TrainingState.TRAINING TrainModel(training_data, model_file, epochs=epochs) self.train_state = TrainingState.TRAINED mySub = MySubscriber() gn = GravityNode() while gn.init("AnomalyDetector") != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) epochs = gn.getIntParam("training_epochs", epochs) training_size = gn.getIntParam("training_size", training_size) channel = gn.getStringParam("subscription_name", "channel") model_file = gn.getStringParam("model_file", "model.json") gn.subscribe(channel, mySub) while mySub.train_state != TrainingState.TRAINED: time.sleep(1) Log.message("Training Complete " + str(mySub.train_state)) gn.unsubscribe(channel, mySub) sys.exit(0)
operands = MultiplicationOperandsPB() Log.message(str(type(operands))) dataProduct.populateMessage(operands) Log.message("have operands = " + str([operands.multiplicand_a, operands.multiplicand_b])) multResponse = MultiplicationResultPB() multResponse.result = operands.multiplicand_a * operands.multiplicand_b gdp = GravityDataProduct("MultResponse") gdp.data = multResponse Log.message("returning response with result = " + str(multResponse.result)) return gdp gn = GravityNode() while gn.init("PyRequest") != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) requestorProvider = MyRequestorProvider() gn.registerService("Multiplication", gravity.TCP, requestorProvider) # Async request operands = MultiplicationOperandsPB() operands.multiplicand_a = 3 operands.multiplicand_b = 4 gdp = GravityDataProduct("MultRequest") gdp.data = operands gn.request("Multiplication", gdp, requestorProvider)
# initialize Gravity Python components to ensure that __init__ is called # on each parent. def __init__(self): GravitySubscriber.__init__(self) def subscriptionFilled(self, dataProducts): counterPB = BasicCounterDataProductPB() for gdp in dataProducts: gdp.populateMessage(counterPB) Log.message("received counter with value = " + str(counterPB.count)) mySub = MySubscriber() gn = GravityNode() while gn.init("PyPub") != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) gn.registerDataProduct("PythonGDP", gravity.TCP) gn.subscribe("PythonGDP", mySub) counterPB = BasicCounterDataProductPB() gdp = GravityDataProduct("PythonGDP") for i in range(1, 50): counterPB.count = i gdp.data = counterPB gn.publish(gdp) time.sleep(1) gn.waitForExit()