def __init__(self, filename): """ Initialize. Parameters ---------- filename : str The path to the filename containing the recorded data. """ super(SqliteCaseReader, self).__init__(filename) if filename is not None: if not is_valid_sqlite3_db(filename): raise IOError('File does not contain a valid ' 'sqlite database ({0})'.format(filename)) with sqlite3.connect(self.filename) as con: cur = con.cursor() cur.execute("SELECT format_version, abs2prom, prom2abs FROM metadata") row = cur.fetchone() self.format_version = row[0] self._abs2prom = None self._prom2abs = None if PY2: self._abs2prom = pickle.loads(str(row[1])) if row[1] is not None else None self._prom2abs = pickle.loads(str(row[2])) if row[2] is not None else None if PY3: self._abs2prom = pickle.loads(row[1]) if row[1] is not None else None self._prom2abs = pickle.loads(row[2]) if row[2] is not None else None con.close() self._load()
def __init__(self, filename): """ Initialize. Parameters ---------- filename : str The path to the filename containing the recorded data. """ super(SqliteCaseReader, self).__init__(filename) if filename is not None: if not is_valid_sqlite3_db(filename): raise IOError('File does not contain a valid ' 'sqlite database ({0})'.format(filename)) self._coordinate_split_re = re.compile('\|\\d+\|*') with sqlite3.connect(self.filename) as con: cur = con.cursor() cur.execute( "SELECT format_version, abs2prom, prom2abs, abs2meta FROM metadata" ) row = cur.fetchone() self.format_version = row[0] self._abs2prom = None self._prom2abs = None self._abs2meta = None if PY2: self._abs2prom = pickle.loads(str( row[1])) if row[1] is not None else None self._prom2abs = pickle.loads(str( row[2])) if row[2] is not None else None self._abs2meta = pickle.loads(str( row[3])) if row[3] is not None else None if PY3: self._abs2prom = pickle.loads( row[1]) if row[1] is not None else None self._prom2abs = pickle.loads( row[2]) if row[2] is not None else None self._abs2meta = pickle.loads( row[3]) if row[3] is not None else None con.close() self.output2meta = PromotedToAbsoluteMap(self._abs2meta, self._prom2abs, self._abs2prom, True) self.input2meta = PromotedToAbsoluteMap(self._abs2meta, self._prom2abs, self._abs2prom, False) self._load()
def __init__(self, filename): """ Initialize. """ super(SqliteCaseReader, self).__init__(filename) if filename is not None: if not is_valid_sqlite3_db(filename): raise IOError('File does not contain a valid ' 'sqlite database ({0})'.format(filename)) with sqlite3.connect(self.filename) as con: cur = con.cursor() cur.execute("SELECT format_version FROM metadata") row = cur.fetchone() self.format_version = row[0] con.close() self._load()
def __init__(self, filename): """ Initialize. Parameters ---------- filename : str The path to the filename containing the recorded data. """ super(SqliteCaseReader, self).__init__(filename) if filename is not None: if not is_valid_sqlite3_db(filename): if not os.path.exists(filename): raise IOError('File does not exist({0})'.format(filename)) else: raise IOError('File does not contain a valid ' 'sqlite database ({0})'.format(filename)) self._coordinate_split_re = re.compile('\|\\d+\|*') with sqlite3.connect(self.filename) as con: cur = con.cursor() # need to see what columns are in the metadata table before we query it cursor = cur.execute('select * from metadata') names = [description[0] for description in cursor.description] if "var_settings" in names: cur.execute( "SELECT format_version, abs2prom, prom2abs, abs2meta, var_settings " "FROM metadata") else: cur.execute( "SELECT format_version, abs2prom, prom2abs, abs2meta FROM metadata" ) row = cur.fetchone() self.format_version = row[0] self._abs2prom = None self._prom2abs = None self._abs2meta = None self._var_settings = None if self.format_version >= 4: self._var_settings = safe_load(row[4]) if self.format_version >= 3: self._abs2prom = safe_load(row[1]) self._prom2abs = safe_load(row[2]) self._abs2meta = safe_load(row[3]) for name in self._abs2meta: if 'lower' in self._abs2meta[name]: self._abs2meta[name]['lower'] =\ convert_to_np_array(self._abs2meta[name]['lower'], name, self._abs2meta) if 'upper' in self._abs2meta[name]: self._abs2meta[name]['upper'] =\ convert_to_np_array(self._abs2meta[name]['upper'], name, self._abs2meta) elif self.format_version in (1, 2): if PY2: self._abs2prom = pickle.loads(str( row[1])) if row[1] is not None else None self._prom2abs = pickle.loads(str( row[2])) if row[2] is not None else None self._abs2meta = pickle.loads(str( row[3])) if row[3] is not None else None if PY3: try: self._abs2prom = pickle.loads( row[1]) if row[1] is not None else None self._prom2abs = pickle.loads( row[2]) if row[2] is not None else None self._abs2meta = pickle.loads( row[3]) if row[3] is not None else None except TypeError: # Reading in a python 2 pickle recorded pre-OpenMDAO 2.4. self._abs2prom = pickle.loads(row[1].encode()) if row[1] is not None \ else None self._prom2abs = pickle.loads(row[2].encode()) if row[2] is not None \ else None self._abs2meta = pickle.loads(row[3].encode()) if row[3] is not None \ else None con.close() self.output2meta = PromotedToAbsoluteMap(self._abs2meta, self._prom2abs, self._abs2prom, True) self.input2meta = PromotedToAbsoluteMap(self._abs2meta, self._prom2abs, self._abs2prom, False) self._load()
def _get_viewer_data(problem_or_rootgroup_or_filename): """Get the data needed by the N2 viewer as a dictionary.""" if isinstance(problem_or_rootgroup_or_filename, Problem): root_group = problem_or_rootgroup_or_filename.model elif isinstance(problem_or_rootgroup_or_filename, Group): if not problem_or_rootgroup_or_filename.pathname: # root group root_group = problem_or_rootgroup_or_filename else: # this function only makes sense when it is at the root return {} elif is_valid_sqlite3_db(problem_or_rootgroup_or_filename): import sqlite3 con = sqlite3.connect(problem_or_rootgroup_or_filename, detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() cur.execute("SELECT model_viewer_data FROM driver_metadata;") model_pickle = cur.fetchone() from six import PY2, PY3 if PY2: import cPickle return cPickle.loads(str(model_pickle[0])) if PY3: import pickle return pickle.loads(model_pickle[0]) else: raise TypeError( 'get_model_viewer_data only accepts Problems, Groups or filenames') data_dict = {} component_execution_idx = [0] #list so pass by ref component_execution_orders = {} data_dict['tree'] = _get_tree_dict(root_group, component_execution_orders, component_execution_idx) connections_list = [] sorted_abs_input2src = OrderedDict( sorted(root_group._conn_global_abs_in2out.items()) ) # sort to make deterministic for testing root_group._conn_global_abs_in2out = sorted_abs_input2src G = root_group.compute_sys_graph(comps_only=True) scc = nx.strongly_connected_components(G) scc_list = [s for s in scc if len(s) > 1] #list(scc) for in_abs, out_abs in iteritems(sorted_abs_input2src): if out_abs is None: continue src_subsystem = out_abs.rsplit('.', 1)[0] tgt_subsystem = in_abs.rsplit('.', 1)[0] count = 0 edges_list = [] for li in scc_list: if src_subsystem in li and tgt_subsystem in li: count = count + 1 if (count > 1): raise ValueError('Count greater than 1') exe_tgt = component_execution_orders[tgt_subsystem] exe_src = component_execution_orders[src_subsystem] exe_low = min(exe_tgt, exe_src) exe_high = max(exe_tgt, exe_src) subg = G.subgraph(li).copy() for n in list(subg.nodes()): exe_order = component_execution_orders[n] if (exe_order < exe_low or exe_order > exe_high): subg.remove_node(n) src_to_tgt_str = src_subsystem + ' ' + tgt_subsystem for tup in subg.edges(): edge_str = tup[0] + ' ' + tup[1] if edge_str != src_to_tgt_str: edges_list.append(edge_str) if (len(edges_list) > 0): edges_list.sort( ) # make deterministic so same .html file will be produced each run connections_list.append( OrderedDict([('src', out_abs), ('tgt', in_abs), ('cycle_arrows', edges_list)])) else: connections_list.append( OrderedDict([('src', out_abs), ('tgt', in_abs)])) data_dict['connections_list'] = connections_list return data_dict
def _get_viewer_data(problem_or_rootgroup_or_filename): """Get the data needed by the N2 viewer as a dictionary.""" if isinstance(problem_or_rootgroup_or_filename, Problem): root_group = problem_or_rootgroup_or_filename.model elif isinstance(problem_or_rootgroup_or_filename, Group): if not problem_or_rootgroup_or_filename.pathname: # root group root_group = problem_or_rootgroup_or_filename else: # this function only makes sense when it is at the root return {} elif is_valid_sqlite3_db(problem_or_rootgroup_or_filename): import sqlite3 con = sqlite3.connect(problem_or_rootgroup_or_filename, detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() cur.execute("SELECT model_viewer_data FROM driver_metadata;") model_pickle = cur.fetchone() from six import PY2, PY3 if PY2: import cPickle return cPickle.loads(str(model_pickle[0])) if PY3: import pickle return pickle.loads(model_pickle[0]) else: raise TypeError('get_model_viewer_data only accepts Problems, Groups or filenames') data_dict = {} component_execution_idx = [0] #list so pass by ref component_execution_orders = {} data_dict['tree'] = _get_tree_dict(root_group, component_execution_orders, component_execution_idx) connections_list = [] sorted_abs_input2src = OrderedDict(sorted(root_group._conn_global_abs_in2out.items())) # sort to make deterministic for testing root_group._conn_global_abs_in2out = sorted_abs_input2src G = root_group.compute_sys_graph(comps_only=True) scc = nx.strongly_connected_components(G) scc_list = [s for s in scc if len(s)>1] #list(scc) for in_abs, out_abs in iteritems(sorted_abs_input2src): if out_abs is None: continue src_subsystem = out_abs.rsplit('.', 1)[0] tgt_subsystem = in_abs.rsplit('.', 1)[0] count = 0 edges_list = [] for li in scc_list: if src_subsystem in li and tgt_subsystem in li: count = count+1 if(count > 1): raise ValueError('Count greater than 1') exe_tgt = component_execution_orders[tgt_subsystem] exe_src = component_execution_orders[src_subsystem] exe_low = min(exe_tgt,exe_src) exe_high = max(exe_tgt,exe_src) subg = G.subgraph(li).copy() for n in list(subg.nodes()): exe_order = component_execution_orders[n] if(exe_order < exe_low or exe_order > exe_high): subg.remove_node(n) src_to_tgt_str = src_subsystem + ' ' + tgt_subsystem for tup in subg.edges(): edge_str = tup[0] + ' ' + tup[1] if edge_str != src_to_tgt_str: edges_list.append(edge_str) if(len(edges_list) > 0): edges_list.sort() # make deterministic so same .html file will be produced each run connections_list.append(OrderedDict([('src', out_abs), ('tgt', in_abs), ('cycle_arrows', edges_list)])) else: connections_list.append(OrderedDict([('src', out_abs), ('tgt', in_abs)])) data_dict['connections_list'] = connections_list return data_dict
def _get_viewer_data(data_source): """ Get the data needed by the N2 viewer as a dictionary. Parameters ---------- data_source : <Problem> or <Group> or str A Problem or Group or case recorder file name containing the model or model data. Returns ------- dict A dictionary containing information about the model for use by the viewer. """ if isinstance(data_source, Problem): root_group = data_source.model elif isinstance(data_source, Group): if not data_source.pathname: # root group root_group = data_source else: # this function only makes sense when it is at the root return {} elif is_valid_sqlite3_db(data_source): import sqlite3 con = sqlite3.connect(data_source, detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() cur.execute("SELECT format_version FROM metadata") row = cur.fetchone() format_version = row[0] cur.execute("SELECT model_viewer_data FROM driver_metadata;") model_text = cur.fetchone() from six import PY2, PY3 if row is not None: if format_version >= 3: return json.loads(model_text[0]) elif format_version in (1, 2): if PY2: import cPickle return cPickle.loads(str(model_text[0])) if PY3: import pickle return pickle.loads(model_text[0]) else: raise TypeError( 'get_model_viewer_data only accepts Problems, Groups or filenames') data_dict = {} comp_exec_idx = [0] # list so pass by ref comp_exec_orders = {} data_dict['tree'] = _get_tree_dict(root_group, comp_exec_orders, comp_exec_idx) connections_list = [] # sort to make deterministic for testing sorted_abs_input2src = OrderedDict( sorted(root_group._conn_global_abs_in2out.items())) root_group._conn_global_abs_in2out = sorted_abs_input2src G = root_group.compute_sys_graph(comps_only=True) scc = nx.strongly_connected_components(G) scc_list = [s for s in scc if len(s) > 1] for in_abs, out_abs in iteritems(sorted_abs_input2src): if out_abs is None: continue src_subsystem = out_abs.rsplit('.', 1)[0] tgt_subsystem = in_abs.rsplit('.', 1)[0] src_to_tgt_str = src_subsystem + ' ' + tgt_subsystem count = 0 edges_list = [] for li in scc_list: if src_subsystem in li and tgt_subsystem in li: count += 1 if (count > 1): raise ValueError('Count greater than 1') exe_tgt = comp_exec_orders[tgt_subsystem] exe_src = comp_exec_orders[src_subsystem] exe_low = min(exe_tgt, exe_src) exe_high = max(exe_tgt, exe_src) subg = G.subgraph( n for n in li if exe_low <= comp_exec_orders[n] <= exe_high) for edge in subg.edges(): edge_str = ' '.join(edge) if edge_str != src_to_tgt_str: edges_list.append(edge_str) if (edges_list): edges_list.sort( ) # make deterministic so same .html file will be produced each run connections_list.append( OrderedDict([('src', out_abs), ('tgt', in_abs), ('cycle_arrows', edges_list)])) else: connections_list.append( OrderedDict([('src', out_abs), ('tgt', in_abs)])) data_dict['connections_list'] = connections_list data_dict['abs2prom'] = root_group._var_abs2prom return data_dict