Exemple #1
0
 def __init__(self, value, rollback=False):
     lg.error('-- VALIDATION ERROR: {}'.format(value))
     self.value = value
     if rollback == 'cd':
         self._cruise_data_rollback()
     elif rollback == 'cd_update':
         self._cruise_data_update_rollback()
Exemple #2
0
    def set_oct_exe_path(self, path=None):
        ''' This method is run when
                * The shared.json file already has a path set >> path in argument
                * The octave path is set manually >> path in argument as well
        '''
        lg.info('-- SET OCT EXE PATH')
        # lg.warning('>> MANUAL PATH: {}'.format(path))

        if path is not None:
            if sys.platform == 'win32':
                if os.path.basename(path) != 'octave-cli.exe':
                    self.oct_exe_path = os.path.join(path, 'octave-cli.exe')
                else:
                    self.oct_exe_path = path
            else:
                if os.path.basename(path) != 'octave-cli':
                    self.oct_exe_path = os.path.join(path, 'octave-cli')
                else:
                    self.oct_exe_path = path

        if self.oct_exe_path is not False:
            os.environ['OCTAVE_EXECUTABLE'] = self.oct_exe_path
            try:
                oct2py_lib = importlib.import_module('oct2py')
                self.oc = oct2py_lib.octave
                self.oc.addpath(os.path.join(OCEAN_DATA_QC_PY, 'octave'))
                self.oc.addpath(os.path.join(OCEAN_DATA_QC_PY, 'octave', 'CANYON-B'))
                return {'octave_path': self.oct_exe_path }
            except Exception as e:
                lg.error('>> oct2py LIBRARY COULD NOT BE IMPORTED, OCTAVE PATH WAS NOT SET CORRECTLY')
        return {'octave_path': False }
    def update_flag_value(self,
                          flag_value=None,
                          flag_to_update=None,
                          row_indexes=[]):
        lg.info('-- UPDATE FLAG VALUE')
        self.env.bk_bridge.call_js({
            'object': 'tools',
            'function': 'show_wait_cursor',
        })
        if flag_value is None:
            lg.error(
                '>> An empty flag value got to `self.env.bk_flags.update_flag_value()`'
            )
        if flag_to_update is None:
            flag_to_update = self.env.cur_flag
        if row_indexes == []:
            lg.error('>> NO row_indexes selected')

        self.env.cruise_data.update_flag_values(
            column=flag_to_update,
            new_flag_value=flag_value,
            row_indices=row_indexes,
        )
        new_values = np.array(self.env.source.data[flag_to_update],
                              dtype=int)  # TODO: Int8 or Int64
        new_values[row_indexes] = flag_value
        self.env.source.data[flag_to_update] = new_values
        self.env.bk_sources.cds_df[flag_to_update] = new_values

        # Updating flag colors
        self.env.doc.hold('collect')
        self.env.bk_plots_handler.replot_color_circles()

        # NOTE: update datatable and prof sources is needed because the new flag could be invisible,
        #       then the profiles should be invisible as well
        # self.env.bk_table.update_dt_source()
        self.env.bk_sources._upd_prof_srcs(force_selection=True)

        self.env.doc.unhold()

        self.env.bk_bridge.call_js({
            'object': 'tools',
            'function': 'show_default_cursor',
        })
        self.env.bk_bridge.call_js({
            'object':
            'tools',
            'function':
            'show_snackbar',
            'params': [
                '{} values of {} updated with the flag value {}'.format(
                    len(row_indexes),
                    flag_to_update,
                    flag_value,
                )
            ],
        })
        def repl(match):
            """ This function is run for each found ocurrence """
            inner_word = match.group(0)
            new_var = False
            param_name = inner_word[2:-1]   # removin characters: ${PARAM} >> PARAM
            for elem in self.proj_settings_cps:
                if elem['param_name'] == param_name:
                    new_var = '({})'.format(elem.get('equation', False))

            if new_var is False:
                lg.error('The computed parameter does not exist')
            lg.info('>> INNER WORD: {} | NEW VAR: {}'.format(inner_word, new_var))
            return new_var
    def compute_equation(self, args):
        try:
            prec = int(args.get('precision', 5))
        except Exception:
            lg.error('Precision value could not be cast to integer value')
        (eq, computed_param_name, precision) = (
            args.get('eq', ''),
            args.get('computed_param_name', 'AUX'),
            prec
        )
        eq = re.sub(' ', '', eq)   # remove spaces

        if eq == '':
            lg.error('ERROR: Empty equation')

        def repl(match):
            """ This function is run for each found ocurrence """
            inner_word = match.group(0)
            new_var = False
            param_name = inner_word[2:-1]   # removin characters: ${PARAM} >> PARAM
            for elem in self.proj_settings_cps:
                if elem['param_name'] == param_name:
                    new_var = '({})'.format(elem.get('equation', False))

            if new_var is False:
                lg.error('The computed parameter does not exist')
            lg.info('>> INNER WORD: {} | NEW VAR: {}'.format(inner_word, new_var))
            return new_var

        while re.search(r'\$\{[a-zA-Z0-9_]+\}', eq) is not None:
            eq = re.sub(r'\$\{[a-zA-Z0-9_]+\}', repl, eq)

        if self.sandbox_funcs is None:
            self.sandbox_funcs = self._get_sandbox_funcs(locals())
        if self.sandbox_vars is None:
            self.sandbox_vars = self._get_sandbox_vars(globals())
        ids = self._get_eq_ids(eq)

        # check if all the identifiers are in the df
        for i in ids:
            if i not in self.cruise_data.df.columns:  # already calculated parameters also can be use as columns
                return {
                    'success': False,
                    'msg': 'Some identifiers do not exist in the current dataframe: {}'.format(i),
                }

        eq = '{} = {}'.format(computed_param_name, eq)
        # lg.info('>> EQUATION: {}'.format(eq))
        try:
            self.cruise_data.df.eval(
                expr=eq,
                engine='python',                 # NOTE: numexpr does not support custom functions
                inplace=True,
                local_dict=self.sandbox_funcs,
                global_dict=self.sandbox_vars
            )
        except Exception as e:
            # lg.warning('>> THE CP {} COULD NOT BE CALCULATED: {}'.format(computed_param_name, e))
            return {
                'success': False,
                'msg': 'The equation could not be computed: {}'.format(eq),
                'error': '{}'.format(e),
            }
        if computed_param_name == 'AUX' and 'AUX' in self.cruise_data.df.columns:
            del self.cruise_data.df['AUX']
        else:
            self.cruise_data.df = self.cruise_data.df.round({computed_param_name: precision})

        return {
            'success': True,
        }
Exemple #6
0
 def __init__(self, value):
     lg.error('-- User error: {}'.format(value))
     self.value = value
Exemple #7
0
    def get_different_values(self):
        """ Structure of the different_values dictionary:
            {
                'param_name': {
                    'stt': [
                        {
                            'hash': '...',
                            'flag':
                            'old_param_value':
                            'new_param_value':
                            'old_flag_value':
                            'new_flag_value':

                        },
                        {
                            ...
                        }
                    ]
                }
            }
        """
        lg.info('-- GET DIFFERENT VALUES')
        HASH = 0
        COL = 1
        self.diff_values = {}
        for pair in self.diff_val_pairs:
            stt = str(int(self.env.cd_aux.df.loc[
                pair[HASH], 'STNNBR']))  # the stt should be always integers?
            col = pair[COL]
            hash_id = pair[HASH]
            param = ''
            if FLAG_END in col:
                param = col[:-7]
                flag = col
            else:
                param = col
                flag = col + FLAG_END

            if param not in self.diff_values:
                self.diff_values[param] = {}
            if stt not in self.diff_values[param]:
                self.diff_values[param][stt] = []

            exist = len([
                val for val in self.diff_values[param][stt]
                if val["hash_id"] == hash_id
            ])
            if exist == 0:
                aux = {
                    'old_param_value': self.env.cruise_data.df.loc[hash_id,
                                                                   param],
                    'new_param_value': self.env.cd_aux.df.loc[hash_id, param],
                    'old_flag_value': self.env.cruise_data.df.loc[hash_id,
                                                                  flag],
                    'new_flag_value': self.env.cd_aux.df.loc[hash_id, flag],
                }

                # change = [flag, param, both ]
                for key in aux:
                    lg.info('>> VALUE: {} | TYPE: {}'.format(
                        aux[key], type(aux[key])))
                    if isinstance(
                            aux[key],
                        (np.int8, np.int16, np.int32, np.int64
                         )):  # they cannot be serialized with json.dumps
                        aux[key] = int(aux[key])
                    elif isinstance(aux[key], np.float64) and np.isnan(
                            aux[key]):
                        aux[key] = False
                    elif isinstance(aux[key], np.float64):
                        aux[key] = float(
                            aux[key])  # 15 decimal figures survive

                # TODO: fix these comparisons as well
                if aux['old_param_value'] != aux['new_param_value'] and aux[
                        'old_flag_value'] != aux['new_flag_value']:
                    changed = 'both'
                elif aux['old_param_value'] != aux['new_param_value']:
                    changed = 'param'
                elif aux['old_flag_value'] != aux['new_flag_value']:
                    changed = 'flag'

                aux.update({
                    'hash_id':
                    hash_id,
                    'castno':
                    str(self.env.cruise_data.df.loc[hash_id, 'CASTNO']),
                    'btlnbr':
                    str(self.env.cruise_data.df.loc[hash_id, 'BTLNBR']),
                    'latitude':
                    str(self.env.cruise_data.df.loc[hash_id, 'LATITUDE']),
                    'longitude':
                    str(self.env.cruise_data.df.loc[hash_id, 'LONGITUDE']),
                    'changed':
                    changed,
                })
                for key in list(aux.keys()):
                    lg.info('>> AUX [{} =: {} | TYPE: {}'.format(
                        key, aux[key], type(aux[key])))

                self.diff_values[param][stt].append(aux)
            elif exist > 1:
                lg.error(
                    'Repeated row for that value: PARAM: {} | HASH_ID: {}'.
                    format(param, hash_id))

        self.diff_values = json.dumps(self.diff_values, sort_keys=True)
        lg.info('>> DIFF VALUES JSON STRING: {}'.format(self.diff_values))
        return self.diff_values