diff --git a/openflexure_microscope/config.py b/openflexure_microscope/config.py index be2c7beb..621360df 100644 --- a/openflexure_microscope/config.py +++ b/openflexure_microscope/config.py @@ -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) diff --git a/openflexure_microscope/openflexurerc.default.yaml b/openflexure_microscope/microscoperc.default.yaml similarity index 100% rename from openflexure_microscope/openflexurerc.default.yaml rename to openflexure_microscope/microscoperc.default.yaml diff --git a/utilities/recalibrate.py b/utilities/recalibrate.py index a3b777ce..5a1893ea 100644 --- a/utilities/recalibrate.py +++ b/utilities/recalibrate.py @@ -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,9 +106,8 @@ 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 + 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)) # Adjust the shutter speed until the brightest pixels are giving a set value (say 220) @@ -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