Moved user configs to hidden directory

This commit is contained in:
Joel Collins 2018-12-05 12:03:22 +00:00
parent b6d0aebc1b
commit aede078eb9
3 changed files with 23 additions and 18 deletions

View file

@ -4,8 +4,10 @@ import logging
import shutil import shutil
HERE = os.path.abspath(os.path.dirname(__file__)) HERE = os.path.abspath(os.path.dirname(__file__))
DEFAULT_CONFIG_PATH = os.path.join(HERE, 'openflexurerc.default.yaml') DEFAULT_CONFIG_PATH = os.path.join(HERE, 'microscoperc.default.yaml')
USER_CONFIG_PATH = os.path.join(os.path.expanduser("~"), "openflexurerc.yaml")
USER_CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".openflexure")
USER_CONFIG_FILE = os.path.join(USER_CONFIG_DIR, "microscoperc.yaml")
TYPES = { TYPES = {
'stream_resolution': tuple, 'stream_resolution': tuple,
@ -36,17 +38,20 @@ def load_config(config_path: str=None) -> dict:
Args: Args:
config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH` 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 not config_path:
if os.path.exists(USER_CONFIG_PATH): # If user config file already exists if os.path.exists(USER_CONFIG_FILE): # If user config file already exists
config_path = USER_CONFIG_PATH # Load it config_path = USER_CONFIG_FILE # Load it
else: # If user config file doesn't yet exist else: # If user config file doesn't yet exist
logging.warning("No user config found. Loading system defaults...") 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...") 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...") 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: with open(config_path) as config_file:
config_data = yaml.load(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_dict (dict): Dictionary of config data to save.
config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH` 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: if not config_path:
config_path = USER_CONFIG_PATH config_path = USER_CONFIG_FILE
with open(config_path, 'w') as outfile: with open(config_path, 'w') as outfile:
if not safe: 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_dict (dict): Dictionary of config data to save.
config_path (str): Path to the config YAML file. If `None`, defaults to `DEFAULT_CONFIG_PATH` 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: if not config_path:
config_path = USER_CONFIG_PATH config_path = USER_CONFIG_FILE
config_data = load_config(config_path=config_path) config_data = load_config(config_path=config_path)

View file

@ -12,6 +12,7 @@ import os
import logging, sys import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def lens_shading_correction_from_rgb(rgb_array, binsize=64): def lens_shading_correction_from_rgb(rgb_array, binsize=64):
"""Calculate a correction to a lens shading table from an RGB image. """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 gains = 1.0/lens_shading # 32 is unity gain
return gains return gains
def gains_to_lst(gains): def gains_to_lst(gains):
"""Given a lens shading gains table (where no gain=1.0), convert to 8-bit.""" """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 = gains / np.min(gains)*32 # minimum gain is 32 (= unity gain)
lst[lst > 255] = 255 # clip at 255 lst[lst > 255] = 255 # clip at 255
return lst.astype(np.uint8) 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, n_iterations=5,
images_to_average=5): images_to_average=5):
"""Reset the camera's parameters, and recalibrate the lens shading to get unifrom images. """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. # Open the microscope and start with flat (i.e. no) lens shading correction.
cam.start_preview() 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)) 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) # 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 lens_shading_table = cam.camera.lens_shading_table
# Saving shading table to disk # 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) np.save(output_path, lens_shading_table)
print("Lens shading table written to {}".format(output_path)) 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...") logging.debug("Merging config...")
config.merge_config(settings, safe=True, backup=True) config.merge_config(settings, safe=True, backup=True)
#cam.resume_stream_for_capture()
if __name__ == '__main__': if __name__ == '__main__':
generate_lens_shading_table_closed_loop() generate_lens_shading_table_closed_loop()