def watch_for_fitness(self, path):
        """
        Watches a specific folder for a fitness.json file
        Updates the algorithm with new fitnesses once found
        Appends the median fitnesses to the average

        Args:
            path (str): Folder to watch
        """

        fitness_path = os.path.join(path, fn.FITNESS_FILE)
        print("Waiting for fitness file in {}.".format(os.path.relpath(path)))

        # Wait for the fitness file to be created
        while not os.path.exists(fitness_path):
            time.sleep(0.5)

        # Get the fitnesses and update the algorithm
        fitness_data = json.read(fitness_path)
        fitnesses = fitness_data["fitness"]
        self.algorithm.set_fitness_value(fitnesses)

        # Append the average of the fitnesses to the overall average
        avg = np.median(fitnesses)
        self.average_fitnesses.append(avg)
Example #2
0
    def analysis_protocol(self, xp_list: list):
        """Analysis protocol for analysing each experiment
        Iterates through each experiment to obtain UV/IR spectra
        and cleans the vial afterwards

        Args:
            xp_list (list): List of experiments to analyse
        """

        # Iterate through each experiment
        for xp_path in xp_list:
            # Load the Info file to log start times and end times
            info_file = json.read(os.path.join(xp_path, fn.XP_LOG))

            # Log experiment
            self.manager.log(f"Analysing experiment: {xp_path}")

            # Obtain experimental spectra
            info_file["analysis_start"] = time.strftime("%H:%M:%S")
            self.manager.obtain_uv_ir_spectra(xp_path=xp_path, dilute=True)
            info_file["analysis_end"] = time.strftime("%H:%M:%S")

            # Clean the vial and tubing
            info_file["clean_start"] = time.strftime("%H:%M:%S")
            self.manager.clean_vial()
            info_file["clean_end"] = time.strftime("%H:%M:%S")

            # Write info back out
            json.write(info_file, os.path.join(xp_path, fn.XP_LOG))
Example #3
0
    def reaction_protocol(self, xp_list: list):
        """Dispensing protocol
        Loads in parameters for each experiment and dispenses reagents

        Args:
            xp_list (list): List of experimental paths to load in
        """

        # Iterate through each experiment
        for xp_path in xp_list:
            # Info file generation
            experiment_info = {}

            # Log experiment
            self.manager.log(f"Conducting experiment: {xp_path}")

            # Load in parameters
            params = json.read(os.path.join(xp_path, fn.PARAMS_FILE))

            experiment_info["raw_params"] = params

            # Convert parameters for dispensing
            normalised_params = self.manager.normalise_parameters(
                params, self.info[keys.PUMPS], 10)

            # Convert back to dict for sequential dispense
            reagent_values = {
                pump: reagent.volume
                for pump, reagent in normalised_params.items()
            }

            # Move gold to the end
            reagent_values["gold"] = reagent_values.pop("gold")

            # Log XP values
            experiment_info["reagents"] = reagent_values

            # Log XP start time
            experiment_info["dispense_start"] = time.strftime("%H:%M:%S")

            # Pump sequentially
            for pump, volume in reagent_values.items():
                if pump == "gold":
                    time.sleep(10)
                self.manager.dispense_single_reagent(pump,
                                                     volume,
                                                     ignore_turn=True)

            # Log XP end time
            experiment_info["dispense_end"] = time.strftime("%H:%M:%S")

            # Write out info
            json.write(experiment_info, os.path.join(xp_path, fn.XP_LOG))

            # Turn the wheel
            self.manager.turn_wheel(1)
def guard_the_realm():
    """Creates watchers and generation managers dependent on algorithm choice
    """

    info = json.read(os.path.join(RUN, fn.INFO_FILE))

    if info[keys.ALGORITHM] == GA:
        guard_the_genetic_realm(info)
    elif info[keys.ALGORITHM] == SINGLE:
        guard_the_single_realm(info)
Example #5
0
    def __init__(self, info_file):
        self.info = json.read(os.path.relpath(info_file))

        if self.info[keys.ALGORITHM] == "single":
            self.root_xp_folder = os.path.join(DATA, SINGLE_FOLDER,
                                               self.info[keys.TITLE])
        elif self.info[keys.ALGORITHM] == "GA":
            self.root_xp_folder = os.path.join(DATA, "genetic",
                                               self.info[keys.TITLE],
                                               str(self.info[keys.SEED]))

        self.manager = Manager()
Example #6
0
    def parse_file(self, path):
        """
        Parses a JSON file

        Args:
            path (str): path to file

        Returns:
            json_data (Dict): JSON data as a dictionary
        """
        while self.is_file_busy(path):
            time.sleep(0.5)
        return json.read(path)
    def get_calibrations(self):
        """MINUTE
        Attempts to read the calibrations file

        Returns:
            calibrations (Dict): The calibrations for each of the pumps
            empty (Dict): An empty dictionary if the calibrations don't exist
        """
        try:
            filename = self.name + ".json"
            calib_path = os.path.join(fp.CALIBRATIONS, filename)
            print(calib_path)
            calibrations = json.read(calib_path)
            print("Calibrations found: {}".format(calibrations))
            return calibrations
        except:
            print(
                "Cannot find calibration file: {}\nReturning empty calibrations."
                .format(os.path.relpath(calib_path)))
            return {}
Example #8
0
    def reaction_protocol(self, xp_list: list, normalise: bool = False):
        """Dispensing protocol
        Loads in parameters for each experiment and dispenses reagents

        Args:
            xp_list (list): List of experimental paths to load in
        """

        # Iterate through each experiment
        for xp_path in xp_list:
            # Log experiment
            self.manager.log(f"Conducting experiment: {xp_path}")

            # Load in parameters
            params = json.read(
                os.path.join(xp_path, fn.PARAMS_FILE)
            )

            # Convert parameters for dispensing
            if normalise:
                params = self.manager.normalise_parameters(
                    params,
                    self.info[keys.PUMPS],
                    10
                )
            else:
                params = self.manager.convert_params_to_tuple_dict(params)

            # Dispense all reagents
            self.manager.log(f"Dispensing reagents: {params}")

            # Pump sequentially
            for pump, reagent in params.items():
                self.manager.dispense_single_reagent(pump, reagent.volume, ignore_turn=True)

                # Give time for reducing
                if pump == "reductant":
                    time.sleep(25)

            # Turn the wheel
            self.manager.turn_wheel(1)
    def create_generation(self, n_gen):
        """
        Creates a generation folder, populated with experiments
        Algorithm generates parameters for each experiment
        Random parameters at first then algorithmically chosen onces thereafter

        Args:
            n_gen (int): Current generation number
        """
        # Creates a population of experiments for the generation
        population = self.create_population()
        # Creates the folder for the generation
        gen_number = filetools.generate_n_digit_name(n_gen)
        gen_folder = os.path.join(self.xp_path, gen_number)
        filetools.ensure_dir(gen_folder)

        print("Creating generation: {}".format(os.path.relpath(gen_folder)))

        # Iterates through the population, creating a folder and params file for each xp
        for pos, _ in enumerate(population):
            # Creates the xp folder
            xp_folder_name = filetools.generate_n_digit_name(pos)
            xp_folder = os.path.join(gen_folder, xp_folder_name)
            filetools.ensure_dir(xp_folder)

            # Converts the population for this XP into a dictionary
            params = self.convert_pop_to_dict(population[pos])
            params_file = os.path.join(xp_folder, fn.PARAMS_FILE)
            xp_log = os.path.join(xp_folder, fn.XP_LOG)

            # Checks if a parameter file exists and checks the values for this seed number match
            if os.path.exists(xp_log):
                params = json.read(xp_log)["raw_params"]
                json.write(params, params_file)
            else:
                json.write(params, params_file)

        self.watch_for_fitness(gen_folder)
Example #10
0
    # parser.add_argument('--quiet', '-q',
    #                     action='store_true',
    #                     help='Optional: Suppress print output')
    parser.add_argument('--version', '-v',
                        default=boss_test_utils.DEFAULT_VERSION,
                        help='Optional: Boss API version')
    parser.add_argument('--domain', '-d',
                        # default=boss_test_utils.DEFAULT_DOMAIN,
                        help='Optional: Domain name')
    parser_args = parser.parse_args()
    if not bool(parser_args.input_file):
        parser.print_usage()
        raise Exception('Missing argument: configuration file name')

    # Parse the JSON configuration file into a python dictionary
    json_config = json_utils.read(parser_args.input_file)
    start_time = time.gmtime()
    output_dir = '{0}/{1}/{2}_{3:0>2}_{4:0>2}_{5:0>2}_{6:0>2}_{7:0>2}'.format(
        # Optional command argument: output directory #
        path.dirname(path.realpath(__file__)),
        'output' if not bool(parser_args.output) else parser_args.output,
        start_time.tm_year,
        start_time.tm_mon,
        start_time.tm_mday,
        start_time.tm_hour,
        start_time.tm_min,
        start_time.tm_sec)

    # Parse the JSON configuration file and define the system tests to perform
    # ToDo: This is disorganized and/or difficult to read
    suite = unittest.TestSuite()
Example #11
0
import sys
import time
import inspect

HERE = os.path.dirname(os.path.abspath(inspect.getfile(
    inspect.currentframe())))

# adding parent directory to path, so we can access the utils easily
root_path = os.path.join(HERE, '..', "..")
base_path = os.path.join(HERE, "..")
sys.path.append(base_path)
sys.path.append(root_path)

from utils import json_utils

camera_config = json_utils.read("./camera-config.json")
control_config = camera_config["control_config"]

import cv2
import numpy as np


def record_video(save_loc: str, duration, fps=30):
    """
    Records a video to file

    Args:
        save_loc (str): path to save the video
        duration (int/float): Duration of the video
        fps (int): Frames per second of recording.
    """
 def __init__(self, config):
     self.mgr = CommandManager.from_configfile(config)
     self.config = json_utils.read(config)
     # self.devices = self.config[DEVICES].keys()
     self.devices = self.mgr.devices.keys()
 def __init__(self, config: str):
     self.mgr = CommandManager.from_configfile(config)
     self.config = json_utils.read(config)
Example #14
0
import os
import sys
import time
import inspect
HERE = os.path.dirname(os.path.abspath(inspect.getfile(
    inspect.currentframe())))

# adding parent directory to path, so we can access the utils easily
root_path = os.path.join(HERE, '..', "..")
base_path = os.path.join(HERE, "..")
sys.path.append(base_path)
sys.path.append(root_path)

from utils import json_utils

camera_config = json_utils.read(os.path.join(HERE, "camera_config.json"))
control_config = camera_config["control_config"]

import cv2
import numpy as np


def record_video(save_loc, duration, fps=30):
    cap = cv2.VideoCapture(1)
    cap.set(cv2.CAP_PROP_AUTOFOCUS, 1)
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    out = cv2.VideoWriter(save_loc, fourcc, fps, (640, 480))

    curr_time = time.time()

    while cap.isOpened():
Example #15
0
    def obtain_spectrum_image_only(self, ref_path, img_path):
        """
        Obtains a spectrum and outputs to image format only

        Args:
            ref_path (str): Path to the reference spectrum json file
            img_path (str): Path to save the image file

        Returns:
            output_data (Dict): Dictionary containing the spectrum data
        """
        output_data = {}
        spectrum_data = self.get_spectrum()
        ref_data = json.read(ref_path)

        ref_intensities = ref_data[cst.INTENSITY]
        spectrum_intensities = spectrum_data[cst.INTENSITY]

        wavelengths = np.array(spectrum_data[cst.WAVELENGTH])
        output_data[cst.WAVELENGTH] = wavelengths.tolist()

        absorbances = []
        for ref, measured in zip(ref_intensities, spectrum_intensities):
            try:
                if ref == 0 or measured == 0:
                    continue
                absorbances.append(np.log10(ref / measured))
            except:
                break

        output_data[cst.ABSORBANCE] = absorbances
        output_data[cst.INTENSITY] = spectrum_intensities

        # Clears any previous plots from graph
        plt.clf()
        plt.cla()

        absorbances = np.array(absorbances)

        plt.plot(wavelengths, absorbances, color="black", linewidth=0.5)
        plt.title("UV/Vis Spectrum")
        plt.xlabel("Wavelength")
        plt.ylabel("Absorbance")

        # Add an annotated line for maximum absorbance
        plt.gca().set_ylim(bottom=0)
        vert = wavelengths[np.argmax(absorbances)]
        max_y = max(absorbances)
        leg_label = f"Maximum: {vert:.2f}nm"
        plt.vlines(x=vert,
                   ymin=0,
                   ymax=max_y,
                   color="g",
                   zorder=3,
                   label=leg_label)
        leg = plt.legend()

        # Set linewidth of legend to 5.0
        for legobj in leg.legendHandles:
            legobj.set_linewidth(5.0)

        plt.savefig(img_path)

        return output_data