Commit aede078e authored by Joel Collins's avatar Joel Collins
Browse files

Moved user configs to hidden directory

parent b6d0aebc
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -4,8 +4,10 @@ import logging
import shutil

HERE = os.path.abspath(os.path.dirname(__file__))
DEFAULT_CONFIG_PATH = os.path.join(HERE, 'openflexurerc.default.yaml')
USER_CONFIG_PATH = os.path.join(os.path.expanduser("~"), "openflexurerc.yaml")
DEFAULT_CONFIG_PATH = os.path.join(HERE, 'microscoperc.default.yaml')

USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure")
USER_CONFIG_FILE = os.path.join(USER_CONFIG_DIR, "microscoperc.yaml")

TYPES = {
    'stream_resolution': tuple,
@@ -36,17 +38,20 @@ def load_config(config_path: str=None) -> dict:
    Args:
        config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH`
    """
    global DEFAULT_CONFIG_PATH, USER_CONFIG_PATH
    global DEFAULT_CONFIG_PATH, USER_CONFIG_FILE

    if not config_path:
        if os.path.exists(USER_CONFIG_PATH):  # If user config file already exists
            config_path = USER_CONFIG_PATH  # Load it
        if os.path.exists(USER_CONFIG_FILE):  # If user config file already exists
            config_path = USER_CONFIG_FILE  # Load it
        else:  # If user config file doesn't yet exist
            logging.warning("No user config found. Loading system defaults...")
            if not os.path.exists(USER_CONFIG_DIR):
                logging.info("Making user config directory...")
                os.makedirs(USER_CONFIG_DIR)
            logging.info("Copying default config to user config...")
            shutil.copyfile(DEFAULT_CONFIG_PATH, USER_CONFIG_PATH)
            shutil.copyfile(DEFAULT_CONFIG_PATH, USER_CONFIG_FILE)
            logging.info("Loading user config...")
            config_path = USER_CONFIG_PATH  # Load defaults in
            config_path = USER_CONFIG_FILE  # Load defaults in

    with open(config_path) as config_file:
        config_data = yaml.load(config_file)
@@ -63,9 +68,9 @@ def save_config(config_dict: dict, config_path: str=None, safe: bool=False):
        config_dict (dict): Dictionary of config data to save.
        config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH`
    """
    global USER_CONFIG_PATH
    global USER_CONFIG_FILE
    if not config_path:
        config_path = USER_CONFIG_PATH
        config_path = USER_CONFIG_FILE

    with open(config_path, 'w') as outfile:
        if not safe:
@@ -82,9 +87,9 @@ def merge_config(config_dict: dict, config_path: str=None, safe: bool=False, bac
        config_dict (dict): Dictionary of config data to save.
        config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH`
    """
    global USER_CONFIG_PATH
    global USER_CONFIG_FILE
    if not config_path:
        config_path = USER_CONFIG_PATH
        config_path = USER_CONFIG_FILE

    config_data = load_config(config_path=config_path)

+7 −7
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import os
import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)


def lens_shading_correction_from_rgb(rgb_array, binsize=64):
    """Calculate a correction to a lens shading table from an RGB image.

@@ -66,13 +67,15 @@ def lens_shading_correction_from_rgb(rgb_array, binsize=64):
    gains = 1.0/lens_shading  # 32 is unity gain
    return gains


def gains_to_lst(gains):
    """Given a lens shading gains table (where no gain=1.0), convert to 8-bit."""
    lst = gains / np.min(gains)*32 # minimum gain is 32 (= unity gain)
    lst[lst > 255] = 255 # clip at 255
    return lst.astype(np.uint8)

def generate_lens_shading_table_closed_loop(output_fname="shadingtable.npy", 

def generate_lens_shading_table_closed_loop(output_fname="microscopelst.npy",
                                            n_iterations=5,
                                            images_to_average=5):
    """Reset the camera's parameters, and recalibrate the lens shading to get unifrom images.
@@ -103,7 +106,6 @@ def generate_lens_shading_table_closed_loop(output_fname="shadingtable.npy",

    # Open the microscope and start with flat (i.e. no) lens shading correction.
        cam.start_preview()
        logging.info("Stopping worker thread during calibration, to avoid GPU memory issues")

        def get_rgb_image():  # shorthand for taking an RGB image
            return cam.array(use_video_port=True, resize=(max_res[0]//2, max_res[1]//2))
@@ -146,7 +148,7 @@ def generate_lens_shading_table_closed_loop(output_fname="shadingtable.npy",
        lens_shading_table = cam.camera.lens_shading_table

        # Saving shading table to disk
        output_path = os.path.join(os.path.expanduser("~"), output_fname)
        output_path = os.path.join(config.USER_CONFIG_DIR, output_fname)
        np.save(output_path, lens_shading_table)
        print("Lens shading table written to {}".format(output_path))

@@ -160,8 +162,6 @@ def generate_lens_shading_table_closed_loop(output_fname="shadingtable.npy",
        logging.debug("Merging config...")
        config.merge_config(settings, safe=True, backup=True)

        #cam.resume_stream_for_capture()


if __name__ == '__main__':
    generate_lens_shading_table_closed_loop()
 No newline at end of file