Exemple #1
0
    def __init__(self):
        self.distributor = Distributor()

        self.distributor.add(GetServerTime())
        self.distributor.add(Multiply())
        self.distributor.add(GetReqCount())
        logging.info('added 3 request types')

        self.wrapper = Wrapper(distributor=self.distributor)
        self.wrapper.run(port=4657)
Exemple #2
0
 def __init__(self, amt_trials):
     print(self.SHAPE)
     self.robot = Robot.Robot.eeg_side_quickstart(self.URI)
     self.distr = Distributor()
     self.eeg = EEG.EEG(self.PATH)
     self.net = Net.Net(self.SHAPE)
     self.labels = []
     self.datalist = []
     self.eventlist = []
     self.amt_trials = amt_trials  # Amount of trials
Exemple #3
0
    def base_stations_init(cls, bs_params, slice_params):
        base_stations = []
        i = 0
        usage_patterns = {}
        for name, s in slice_params.items():
            usage_patterns[name] = Distributor(
                name, get_dist(s['usage_pattern']['distribution']),
                *s['usage_pattern']['params'])

        for bs in bs_params:
            slices = []
            ratios = bs['ratios']
            capacity = bs['capacity_bandwidth']
            for name, s in slice_params.items():
                s_cap = capacity * ratios[name]

                s = Slice(name, ratios[name], 0, s['client_weight'],
                          s['delay_tolerance'], s['qos_class'],
                          s['bandwidth_guaranteed'], s['bandwidth_max'], s_cap,
                          usage_patterns[name])
                s.capacity = Container(init=s_cap, capacity=s_cap)
                slices.append(s)
            base_station = BaseStation(
                i, Coverage((bs['x'], bs['y']), bs['coverage']), capacity,
                slices)
            base_stations.append(base_station)
            i += 1
        return base_stations
Exemple #4
0
    def clients_init(cls, n_clients, client_params):
        i = 0
        clients = []
        ufp = client_params['usage_frequency']
        usage_freq_pattern = Distributor(f'ufp',
                                         get_dist(ufp['distribution']),
                                         *ufp['params'],
                                         divide_scale=ufp['divide_scale'])

        for _ in range(n_clients):
            loc_x = client_params['location']['x']
            loc_y = client_params['location']['y']
            location_x = get_dist(loc_x['distribution'])(*loc_x['params'])
            location_y = get_dist(loc_y['distribution'])(*loc_y['params'])

            connected_slice_index = get_random_slice_index(cls.slice_weights)
            c = Client(i, location_x, location_y,
                       usage_freq_pattern.generate_scaled(),
                       connected_slice_index, None, None)
            clients.append(c)
            i += 1
        return clients
Exemple #5
0
    def setupUI(self):
        self.setWindowFlags(self.windowFlags()
                            ^ QtCore.Qt.WindowContextHelpButtonHint)
        self.setGeometry(800, 200, 800, 500)
        self.setFixedSize(400, 100)
        self.setWindowTitle("ImageDistributor Processing")
        self.progress = QLabel("진행 중")

        layout = QGridLayout()
        layout.addWidget(self.progress, 0, 0)

        self.setLayout(layout)
        self.distributor = Distributor(self.progress, self.files,
                                       self.sourcePath, self.targetDirPath,
                                       self.instagramIdList)
Exemple #6
0
def run():
    """Beginn of main clopper-script."""

    logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s',
                        datefmt="%Y-%m-%d %H:%M:%S",
                        filemode='w',
                        level=logging.INFO,
                        filename='../clopper-log' +
                        str(time.time()).replace('.', '') + '.log')
    logging.info("Starting execution")
    data = parse_json(sys.argv[1])
    node_dict = data['ip-list']  # format: {instance-i : ip}

    # configure instances if required
    if 'True' in data['setup']:
        threads = [
            threading.Thread(target=set_up, args=(
                node,
                data,
            )) for node in node_dict.iteritems()
        ]
        logging.info("Start setting up hosts.")
        [t.start() for t in threads]
        [thread.join() for thread in threads]
        logging.info("Instances successfully configured.")

    try:
        os.mkdir(expanduser('~/tmp'))
    except OSError:
        shutil.rmtree(expanduser('~/tmp'))
        os.mkdir(expanduser('~/tmp'))
        logging.warning("Folder tmp replaced.")
    # create and distribute test suite
    distributor = Distributor(data, strategy=eval(data['distribution']))
    test_suite = distributor.get_suite()

    logging.info("Test suite generated and splitted.")
    logging.info(data['distribution'])
    logging.info(test_suite)
    for x in reversed(range(0, len(test_suite))):
        if test_suite[x] == [[None], [None]]:
            node = node_dict.keys()[x]
            del node_dict[node]
            del test_suite[x]
            logging.info("Testsuite too small for " + str(data['total']) +
                         " instances. Release " + node)

    distribute_test_suite(node_dict, test_suite, data)
    logging.info("Splits distributed among instances.")
    os.chdir(expanduser('~'))

    # start gRPC-server on instances
    start_grpc_server(node_dict, data)
    logging.info("Waiting for instances to start grpc server...")
    time.sleep(5)

    # start gRPC-client on local host
    logging.info("Starting cloud-manager-client...")
    client.run(node_dict)

    # shut down instances
    logging.info("Grabbing results...")
    get_results(data)
    logging.info("Execution finished.")
    factoryDistributorBottomQueue.PushEnvelope(CUSTOMER_INITIAL_ORDERS)

"""
-------------------------------------------------------
Now we initialize our SupplyChainObjects. Passing the correct
queues is essential.
-------------------------------------------------------
"""

theCustomer = Customer()
myRetailer = Retailer(None, wholesalerRetailerTopQueue, wholesalerRetailerBottomQueue, None, theCustomer)

myWholesaler = Wholesaler(wholesalerRetailerTopQueue, distributorWholesalerTopQueue,
                          distributorWholesalerBottomQueue, wholesalerRetailerBottomQueue)

myDistributor = Distributor(distributorWholesalerTopQueue, factoryDistributorTopQueue,
                            factoryDistributorBottomQueue, distributorWholesalerBottomQueue)

myFactory = Factory(factoryDistributorTopQueue, None, None, factoryDistributorBottomQueue, QUEUE_DELAY_WEEKS)

#Initialize Statistics object
myStats = SupplyChainStatistics()

"""
-------------------------------------------------------
Main game-play!
-------------------------------------------------------
"""

for thisWeek in range(0, WEEKS_TO_PLAY):
    
    print("--- Week {0} ---".format(thisWeek))
Exemple #8
0
class Client:

    BREAKTIME = 1.5
    URI = "tecs://192.168.1.132:9000/ps"  # URI of the TECS server
    SHAPE = (8,655,1)  # (Numpy) shape for the input for the net
    PATH = ".\Ressources\Recordings sec\\"  # Filepath and name to the keras model
    print(PATH)
    PATH_CNN = PATH + "cnn_model.h5" # PATH to the CNN
    NO_IMAGES = 12  # Number of images in the trials
    P = 0.66  # Probability of being right on an image in training
    EPOCHS = 100  # How long to train the net for
    DURATION = 8 * 60
    SFREQ = 250

    def __init__(self, amt_trials):
        print(self.SHAPE)
        self.robot = Robot.Robot.eeg_side_quickstart(self.URI)
        self.distr = Distributor()
        self.eeg = EEG.EEG(self.PATH)
        self.net = Net.Net(self.SHAPE)
        self.labels = []
        self.datalist = []
        self.eventlist = []
        self.amt_trials = amt_trials  # Amount of trials
        #Plot.GUI_thread().run()


        #try:
        #    model_file = open(self.PATH_CNN)
        #    model_file.close()
        #    self.net.load(self.PATH_CNN)
        #except IOError:
        #    self.net.createCNN()

    def train(self):
        print("Started Training")

        index = 0
        for l in range(6):
            begin = time.time()
            print(f"Starting section{l}")
            while time.time() - begin < self.DURATION:
                print("Time left of", time.time() - begin, self.DURATION)
                index += 1
                print(f'Index: {index}')
                self.robot.publish("show", show(12))
                event = self.robot.wait_for_events()[0]
                print(f"got event {event}")
                if not isinstance(event, shown):
                    print("something is wrong!")
                time.sleep(self.BREAKTIME)
                print("Started trial")

                image = random.randrange(self.NO_IMAGES)  # Get display image
                self.robot.publish("show", show(image))
                shown_event = self.robot.wait_for_events()[0]
                if not isinstance(event, shown):
                    print("something is wrong!")
                if shown_event.image_shown < 0:
                    raise Exception("Error")

                time.sleep(1)

                actual_image = random.choice(self.createDistribution(image))  # Get actually shown image
                self.eeg.toggle_recording()
                time.sleep(0.5)
                trial_begin = time.time()
                self.robot.publish("moveArm", moveArm(0 if actual_image < 6 else 1))
                self.eeg.set_event(Events.MOVEARMLEFT.value if actual_image < 6 else Events.MOVEARMRIGHT.value)
                moved_event = self.robot.wait_for_events()[0]
                if not isinstance(event, moved):
                    print("something is wrong!")
                t = 2 - (time.time() - trial_begin)
                if t>0:
                    time.sleep(t)
                self.eeg.toggle_recording()
                Plot.data = self.eeg.get_data().copy()
                #print(Filtering.Filtering.check_quality(self.eeg.get_data().copy(), self.SFREQ))
                if not moved_event.success:
                    raise Exception("Error")
                if (image < 6 and actual_image >= 6) or (image >= 6 and actual_image < 6):
                    self.eeg.set_event(Events.ERRP.value)
                else:
                    self.eeg.set_event(Events.NOERRP.value)
                self.eeg.write_to_file(str(index*10+1), str(index*10+1))
                self.eeg.clear()
                if (image < 6 and actual_image >= 6) or (image >= 6 and actual_image < 6):
                    self.labels.append("ErrP")
                    self.robot.publish("reset", reset())
                    event = self.robot.wait_for_events()[0]
                    if not isinstance(event, moved):
                        print("something is wrong!")
                    continue
                else:
                    self.labels.append("noErrP")

                self.eeg.toggle_recording()
                time.sleep(0.5)
                trial_begin = time.time()
                self.robot.publish("moveCategory", moveCategory(0 if actual_image < 3 else
                                                (1 if actual_image < 6 else
                                                 (2 if actual_image < 9 else 3))))
                self.eeg.set_event(Events.MOVECATEGROYLEFT.value if actual_image < 6 else Events.MOVECATEGROYRIGHT.value)
                moved_event = self.robot.wait_for_events()[0]
                if not isinstance(event, shown):
                    print("something is wrong!")
                t = 2 - (time.time() - trial_begin)
                if t > 0:
                    time.sleep(t)
                self.eeg.toggle_recording()
                Plot.data = self.eeg.get_data().copy()
                #print(Filtering.Filtering.check_quality(self.eeg.get_data().copy(), self.SFREQ))
                if not moved_event.success:
                    raise Exception("Error")
                if (image % 6 < 3 and actual_image % 6 >= 3) or (image % 6 >= 3 and actual_image % 6 < 3):
                    self.eeg.set_event(Events.ERRP.value)
                else:
                    self.eeg.set_event(Events.NOERRP.value)
                self.eeg.write_to_file(str(index*10+2), str(index*10+2))
                self.eeg.clear()
                if (image % 6 < 3 and actual_image % 6 >= 3) or (image % 6 >= 3 and actual_image % 6 < 3):
                    self.labels.append("ErrP")
                    self.robot.publish("reset", reset())
                    moved_event = self.robot.wait_for_events()[0]
                    if not isinstance(event, moved):
                        print("something is wrong!")
                    continue
                else:
                    self.labels.append("noErrP")

                self.eeg.toggle_recording()
                time.sleep(0.5)
                trial_begin = time.time()
                self.robot.publish("moveImg", moveImg(actual_image))
                self.eeg.set_event(
                    Events.MOVEIMAGEONE.value if actual_image % 3 == 0 else
                    (Events.MOVEIMAGETWO.value if actual_image % 3 == 1 else Events.MOVEIMAGETHREE.value))
                moved_event = self.robot.wait_for_events()[0]
                if not isinstance(event, moved):
                    print("something is wrong!")
                t = 2 - (time.time() - trial_begin)
                if t > 0:
                    time.sleep(t)
                self.eeg.toggle_recording()
                Plot.data = self.eeg.get_data().copy()
                #print(Filtering.Filtering.check_quality(self.eeg.get_data().copy(), self.SFREQ))
                if image != actual_image:
                    self.labels.append("ErrP")
                    self.eeg.set_event(Events.ERRP.value)
                else:
                    self.labels.append("noErrP")
                    self.eeg.set_event(Events.NOERRP.value)
                if not moved_event.success:
                    raise Exception("Error")
                # ELSE reset to original position and restart whole trial as success
                self.eeg.write_to_file(str(index*10+3), str(index*10+3))
                self.eeg.clear()
                self.robot.publish("reset", reset())
                moved_event = self.robot.wait_for_events()[0]
                if not isinstance(event, shown):
                    print("something is wrong!")

            print("Commence waiting for 2 minutes")
            time.sleep(60)
            print("Waited 1:00")
            time.sleep(30)
            print("Waited 1:30")
            time.sleep(30)
            print("Continuing")

        with open(self.PATH + "LABELS.json", "w+") as f:
            json.dump(f, self.labels)

    def run(self):
        classifier = Classifier.Classifier()
        begin = time.time()
        index = 0

        while time.time() - begin < self.DURATION:
            index += 1
            self.robot.publish("show", show(12))
            event = self.robot.wait_for_events()
            shown_event = event.parse(shown())
            while True:
                image_text = input("Enter the next image")
                words = ["bring", "give", "put", "conference_room", "entrance", "lab", "1", "2", "3", "Hammer", "Level", "Wrench"]
                try:
                    image = words.index(image_text)
                    break
                except Exception:
                    print("Entered unknown command. Try again")

            self.robot.publish("show", show(image))
            event = self.robot.wait_for_events()
            shown_event = event.parse(shown())
            if shown_event.image_shown < 0:
                raise Exception("Error")

            classification = classifier.classify(image)
            self.robot.publish("moveArm", moveArm(0 if classification < 6 else 1))
            self.eeg.toggle_recording()
            event = self.robot.wait_for_events()
            moved_event = event.parse(moved())
            time.sleep(1)  # To make sure the recording is long enough
            self.eeg.toggle_recording()
            if not moved_event.success:
                raise Exception("Error")
            prediction = self.net.predict(self.eeg.get_data())
            if prediction == 1:
                self.robot.publish("reset", reset())
                event = self.robot.wait_for_events()
                moved_event = event.parse(moved())
                continue
            else:
                self.eeg.clear()

            self.robot.publish("moveCategory", moveCategory(0 if classification < 3 else
                                            (1 if classification < 6 else
                                             (2 if classification < 9 else 3))))
            self.eeg.toggle_recording()
            event = self.robot.wait_for_events()
            moved_event = event.parse(moved())
            time.sleep(1)
            self.eeg.toggle_recording()
            if not moved_event.success:
                raise Exception("Error")
            if (image % 6 < 3 and classification % 6 >= 3) or (image % 6 >= 3 and classification % 6 < 3):
                self.robot.publish("reset", reset())
                event = self.robot.wait_for_events()
                moved_event = event.parse(moved())
                continue
            else:
                self.eeg.clear()

            self.robot.publish("moveImg", moveImg(classification % 3))
            self.eeg.toggle_recording()
            event = self.robot.wait_for_events()
            moved_event = event.parse(moved())
            time.sleep(1)
            self.eeg.toggle_recording()
            if not moved_event.success:
                raise Exception("Error")
            # ELSE reset to original position and restart whole trial as success
            self.robot.publish("reset", reset())
            event = self.robot.wait_for_events()
            moved_event = event.parse(moved())

        with open(self.PATH + "LABELS.json", "w+") as f:
            json.dump(f, self.labels)

    def train_net(self):
        #files = [file for file in os.listdir(self.PATH) if file.endswith(".npy")]
        self.net.createCNN()
        recordings = self.datalist
        #labels = json.load(self.PATH + "LABELS.json")
        labels = self.eventlist
        labels = [0 if label == "ErrP" else 1 for label in labels]
        labels = to_categorical(labels)
        data = numpy.empty((len(recordings),8, 655), dtype=int)
        max = 0
        for i in recordings:
            if len(i[0]) > max:
                max = len(i[0])
        print ("The maximum is: " + str( max))
        for i in recordings:
            dump = []
            for k in range (8):
                for j in range(655):
                    dump.append(i[k][j])
            dump = numpy.array(dump)
            numpy.append(data, dump)



        data = data.reshape(370, 8, 655, 1 )



        self.net.fit(data, labels, self.EPOCHS, batch_size=1,validation=0.2)
        self.net.save(self.PATH_CNN)

    def __load_recordings(self, files):
        return [numpy.load(self.PATH + file) for file in files]

    def createDistribution(self, selected):
        if selected < 3:
            sel = list(range(3))
            selalt = list(range(3,6))
            alt =  list(range(6, 9))
            altalt = list(range(9, 12))
            self.distr.blanceImages(sel,selected)
            self.distr.balanceCategories(sel, selalt, selected)
            self.distr.balanceArms(sel,selected,selalt,alt,altalt)
            sel.extend(selalt)
            sel.extend(alt)
            sel.extend(altalt)
            return sel

        elif selected < 6:
            selalt = list(range(3))
            sel = list(range(3, 6))
            alt =  list(range(6, 9))
            altalt = list(range(9, 12))
            self.distr.blanceImages(sel, selected)
            self.distr.balanceCategories(sel, selalt, selected)
            self.distr.balanceArms(sel, selected, selalt, alt, altalt)
            sel.extend(selalt)
            sel.extend(alt)
            sel.extend(altalt)
            return sel

        elif selected < 9:
            alt = list(range(3))
            altalt = list(range(3, 6))
            sel = list(range(6, 9))
            selalt = list(range(9, 12))
            self.distr.blanceImages(sel, selected)
            self.distr.balanceCategories(sel, selalt, selected)
            self.distr.balanceArms(sel, selected, selalt, alt, altalt)
            sel.extend(selalt)
            sel.extend(alt)
            sel.extend(altalt)
            return sel
        else:
            alt = list(range(3))
            altalt = list(range(3, 6))
            selalt = list(range(6, 9))
            sel = list(range(9, 12))
            self.distr.blanceImages(sel, selected)
            self.distr.balanceCategories(sel, selalt, selected)
            self.distr.balanceArms(sel, selected, selalt, alt, altalt)
            sel.extend(selalt)
            sel.extend(alt)
            sel.extend(altalt)
            return sel



    def readFiles (self):
        #129 == noErrP
        #npy stores data
        #json stores events
        outputData = []
        outputEvents = []
        errorlist = []
        errorfilelist = []
        errorCount = 0
        total = 0
        for i in range(6):
            addendum = "Recordings"+str(i)+"\\"
            curdir = os.listdir("Ressources\\Recordings sec\\"+addendum)
            lendir = len(curdir)
            j = 0
            while (j < lendir):
                total += 1
                eventpath = curdir[j]
                j += 1
                datapath = curdir[j]
                j += 1
                self.eeg.read_from_file(addendum + datapath, addendum + eventpath)
                allEvents = self.eeg.get_events()
                try:
                    if (allEvents[1][1] == 128):
                        outputEvents.append("ErrP")
                    else:
                        outputEvents.append("noErrP")
                except:
                    errorCount += 1
                    errorlist.append(allEvents)
                    errorfilelist.append(eventpath)
                    continue
                outputData.append(self.eeg.get_data())
        self.datalist = outputData
        self.eventlist = outputEvents

        print("Errors: " + str(errorCount) + "with total of: " + str(total) + "with Events" + str(errorlist)+ ", " + str(errorfilelist))