Module gonioimsoft.imaging_parameters

Settting and getting imaging parameters.

Default imaging parameters, and ParameterEditor for letting the user modify them in the program.

Functions

def getModifiedParameters(libui=None, **kwargs)

Take in the DEFAULT parameters in the beginning of this code file and let the user modify them using text based ParameterEditor

def getRightType(parameter_name, string_value)

Convert user inputted string to correct parameter value based on DYNAMIC_PARAMETER_TYPES

TODO - channel checking, check that the channel is proper NI channel

def load_parameters(fn)

Loading imaging parameters, saved as a json file.

def save_parameters(fn, parameters)

Loading imaging parameters, saved as a json file.

Classes

class ParameterEditor (dynamic_parameters, libui=None)

Dictionary editor on command line with ability to load and save presets.

dynamic_parameters Dictionary of the dynamic imaging parameters.

Expand source code
class ParameterEditor:
    '''
    Dictionary editor on command line with ability to load and save presets.
    '''
    def __init__(self, dynamic_parameters, libui=None):
        '''
        dynamic_parameters      Dictionary of the dynamic imaging parameters.
        '''
        self.libui = libui

        self.dynamic_parameters = dynamic_parameters
        self.parameter_names = sorted(self.dynamic_parameters.keys())

        self.presets_savedir = os.path.join(USERDATA_DIR, 'presets')
        self.presets = self.load_presets(self.presets_savedir)

    
    def load_presets(self, directory):
        
        presets = {}
        
        if os.path.isdir(directory):
            files = [os.path.join(directory, fn) for fn in os.listdir(directory)]
        else:
            files = []

        for afile in files:
            try:
                preset = load_parameters(afile)
            except:
                print("Couldn't load preset {}".format(afile))
                continue
            
            # If older files lack some parameters, use default parameters
            for key in self.parameter_names:
                if not key in preset:
                    preset[key] = DEFAULT_DYNAMIC_PARAMETERS[key]
                    
            presets[os.path.basename(afile)] = preset

        return presets


    def print_preset(self, preset):
        '''
        Prints the current parameters and the help strings.
        '''
                 
        parameter_names = sorted(self.dynamic_parameters.keys())

        print()
        
        print('{:<20} {:<40} {}'.format('PARAMETER NAME', 'VALUE', 'DESCRIPTION'))
        for parameter in parameter_names:

            value = str(preset[parameter])

            # Special addition for frame_length parameter; Show Hz so that
            # it is harder to do mistakes
            if parameter == 'frame_length':
                hz = str(round(1/float(value)))
                if hz == 1:
                    hz = str(1/float(value))
                value += ' ({} Hz)'.format(hz)

            print('{:<20} {:<40} {}'.format(
                parameter, value, DYNAMIC_PARAMETERS_HELP[parameter]))
        print()


    def getModified(self):
        '''
        Ask user to edit the parameters until happy and then return
        the parameters.
        '''
        
        while True:
            print('MODIFYING IMAGING PARAMETERS')
            self.print_preset(self.dynamic_parameters)
            parameter = self.libui.input('Parameter name or (list/save/load/back) (Enter to continue) >> ')
            
            # If breaking free
            if parameter == '':
                break
            

            self.presets = self.load_presets(self.presets_savedir)

            
            # If saving preset
            if parameter.lower() == 'save':
                name = self.libui.input('Save current parameters under preset name (if empty == suffix) >> ')
                if name == '' and self.dynamic_parameters['suffix'] != '':
                    name = self.dynamic_parameters['suffix']
                
                if os.path.isdir(USERDATA_DIR):
                    os.makedirs(self.presets_savedir, exist_ok=True)
                    save_parameters(os.path.join(self.presets_savedir, name), self.dynamic_parameters)
                else:
                    print('Saving the preset failed, {} does not exist'.format(USERDATA_DIR))

                continue        

            if parameter.lower() == 'list':
                if self.presets is {}:
                    print('There are no existing presets!')
                else:
                    print('These are the existing presets:')
                    for preset in self.presets.keys():
                        print('  '+preset)
                    print('')
                continue

            if parameter.lower() == 'load':
                # If parameter is actually a preset
                
                while True:
                    
                    preset_names = sorted(self.presets.keys())

                    for i, name in enumerate(preset_names):
                        print('{}) {}'.format(i+1, name))
                    
                    sel = self.libui.input('>> ')

                    try:
                        to_load = preset_names[int(sel)-1]
                        break
                    except:
                        print('Invalid preset.')

                parameter = to_load

            if parameter == 'back':
                return None

            if parameter in self.presets.keys():
                self.print_preset(self.presets[parameter])
                
                confirm = self.libui.input('Load this (y/n)>> ').lower()
                if confirm and confirm[0] == 'y':
                    self.dynamic_parameters = self.presets[parameter]
                else:
                    print('Answer not yes, loading of the preset cancelled')
                continue
            
    

            try:
                self.dynamic_parameters[parameter]
            except KeyError:
                print('Invalid input, not a parameter name')
                time.sleep(1)
                continue

            while True:

                value = self.libui.input('Value for {} >> '.format(parameter))
                
                if value == '':
                    break

                try:
                    value = getRightType(parameter, value)
                except ValueError as e:
                    print(str(e))
                    print('Could not convert the value to right type. Try againg or Enter to skip.')
                    continue

                self.dynamic_parameters[parameter] = value
                break

        return self.dynamic_parameters

Methods

def getModified(self)

Ask user to edit the parameters until happy and then return the parameters.

def load_presets(self, directory)
def print_preset(self, preset)

Prints the current parameters and the help strings.