import time
import json
import numpy as np
import cv2
from base64 import b64encode

import mdml_client as mdml # pip install mdml_client #

print("****************************************************************************")
print("*** This example publish an image 10 times a second for 30 seconds.      ***")
print("*** Press Ctrl+C to stop the example.                                    ***")
print("****************************************************************************")
time.sleep(5)

# Approved experiment ID (supplied by MDML administrators - will not work otherwise)
Exp_ID = 'TEST'
# MDML message broker host
host = 'merfpoc.egs.anl.gov'
# MDML username and password
username = 'test'
password = '<PASSWORD>'

# Create MDML experiment
My_MDML_Exp = mdml.experiment(Exp_ID, username, password, host)

# Receive events about your experiment from MDML
def user_func(msg):
    msg_obj = json.loads(msg)
    if msg_obj['type'] == "NOTE":
        print(f'MDML NOTE: {msg_obj["message"]}')
    elif msg_obj['type'] == "ERROR":
        print(f'MDML ERROR: {msg_obj["message"]}')
    elif msg_obj['type'] == "RESULTS":
        print(f'MDML ANALYSIS RESULT FOR "{msg_obj["analysis_id"]}": {msg_obj["message"]}')
    else:
        print("ERROR WITHIN MDML, CONTACT ADMINS.")
My_MDML_Exp.set_debug_callback(user_func)
My_MDML_Exp.start_debugger()

# Sleep to let debugger thread set up
time.sleep(1)

# Add and validate a configuration for the experiment
My_MDML_Exp.add_config('./examples_config.json', 'mdml_examples')

# Send configuration file to the MDML
My_MDML_Exp.send_config() # this starts the experiment

# Sleep to let MDML ingest the configuration
time.sleep(2)

reset = False

try:
    i = 0
    while True:
        while i < 15000:
            if i < 1000:
                # Generating random images
                random_image = np.random.randint(255, size=(50,50,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)

            elif i < 2000:
                # Generating random images
                random_image = np.random.randint(255, size=(100,200,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.1)

            elif i < 3000:
                # Generating random images
                random_image = np.random.randint(255, size=(200,200,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.1)

            elif i < 4000:
                # Generating random images
                random_image = np.random.randint(255, size=(200,400,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.5)

            elif i < 5000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,400,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.5)

            elif i < 6000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,800,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 7000:
                # Generating random images
                random_image = np.random.randint(255, size=(800,800,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 8000:
                # Generating random images
                random_image = np.random.randint(255, size=(800,1600,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 9000:
                # Generating random images
                random_image = np.random.randint(255, size=(1600,1600,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 10000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 11000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 12000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1.5)

            elif i < 13000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1.5)
            elif i < 14000:
                # Generating random images
                random_image = np.random.randint(255, size=(50,100,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)
            elif i < 15000:
                # Generating random images
                random_image = np.random.randint(255, size=(100,100,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)

            i += 1
        if not reset:
            print("Ending MDML experiment")
            My_MDML_Exp.reset()
            reset = True
except KeyboardInterrupt:
    if not reset:
        My_MDML_Exp.reset()
    My_MDML_Exp.stop_debugger()
import time
import json
import numpy as np
import cv2
from base64 import b64encode

import mdml_client as mdml # pip install mdml_client #

print("****************************************************************************")
print("*** This example publish an image 10 times a second for 30 seconds.      ***")
print("*** Press Ctrl+C to stop the example.                                    ***")
print("****************************************************************************")
time.sleep(5)

# Approved experiment ID (supplied by MDML administrators - will not work otherwise)
Exp_ID = 'TEST'
# MDML message broker host
host = 'merfpoc.egs.anl.gov'
# MDML username and password
username = 'test'
password = '<PASSWORD>'

# Create MDML experiment
My_MDML_Exp = mdml.experiment(Exp_ID, username, password, host)

# Receive events about your experiment from MDML
def user_func(msg):
    msg_obj = json.loads(msg)
    if msg_obj['type'] == "NOTE":
        print(f'MDML NOTE: {msg_obj["message"]}')
    elif msg_obj['type'] == "ERROR":
        print(f'MDML ERROR: {msg_obj["message"]}')
    elif msg_obj['type'] == "RESULTS":
        print(f'MDML ANALYSIS RESULT FOR "{msg_obj["analysis_id"]}": {msg_obj["message"]}')
    else:
        print("ERROR WITHIN MDML, CONTACT ADMINS.")
My_MDML_Exp.set_debug_callback(user_func)
My_MDML_Exp.start_debugger()

# Sleep to let debugger thread set up
time.sleep(1)

# Add and validate a configuration for the experiment
My_MDML_Exp.add_config('./examples_config.json', 'mdml_examples')

# Send configuration file to the MDML
My_MDML_Exp.send_config() # this starts the experiment

# Sleep to let MDML ingest the configuration
time.sleep(2)

reset = False

try:
    i = 0
    while True:
        while i < 15000:
            if i < 1000:
                # Generating random images
                random_image = np.random.randint(255, size=(50,50,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)

            elif i < 2000:
                # Generating random images
                random_image = np.random.randint(255, size=(100,200,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.1)

            elif i < 3000:
                # Generating random images
                random_image = np.random.randint(255, size=(200,200,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.1)

            elif i < 4000:
                # Generating random images
                random_image = np.random.randint(255, size=(200,400,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.5)

            elif i < 5000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,400,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(.5)

            elif i < 6000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,800,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 7000:
                # Generating random images
                random_image = np.random.randint(255, size=(800,800,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 8000:
                # Generating random images
                random_image = np.random.randint(255, size=(800,1600,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 9000:
                # Generating random images
                random_image = np.random.randint(255, size=(1600,1600,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 10000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 11000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1)

            elif i < 12000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1.5)

            elif i < 13000:
                # Generating random images
                random_image = np.random.randint(255, size=(400,500,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)
                time.sleep(1.5)
            elif i < 14000:
                # Generating random images
                random_image = np.random.randint(255, size=(50,100,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)
            elif i < 15000:
                # Generating random images
                random_image = np.random.randint(255, size=(100,100,3), dtype=np.uint8)    
                _, img = cv2.imencode('.jpg', random_image)
                img_bytes = img.tobytes()
                img_b64bytes = b64encode(img_bytes)
                img_byte_string = img_b64bytes.decode('utf-8')
            
                # Publish image
                My_MDML_Exp.publish_image('IMAGE', img_byte_string, 'random_image_' + str(i) + '.JPG', 0)

                time.sleep(.1)

            i += 1
        if not reset:
            print("Ending MDML experiment")
            My_MDML_Exp.reset()
            reset = True
except KeyboardInterrupt:
    if not reset:
        My_MDML_Exp.reset()
    My_MDML_Exp.stop_debugger()
# pip install mdml_client # # Approved experiment ID (supplied by MDML administrators - will not work otherwise) # MDML message broker host # MDML username and password # Create MDML experiment # Receive events about your experiment from MDML # Sleep to let debugger thread set up # Add and validate a configuration for the experiment # Send configuration file to the MDML # this starts the experiment # Sleep to let MDML ingest the configuration # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image # Generating random images # Publish image