Example #1
0
    def convert_pot_df_to_pot(is_quantum, pot_df, ord_nodes, normalize=True):
        """
        Returns a Potential pot with ordered nodes ord_nodes.

        Parameters
        ----------
        is_quantum : bool
        pot_df : pandas.DataFrame
            a dataframe returned by learn_pot_df()
        ord_nodes : list[BayesNode]
            ordered nodes of Potential pot that is returned
        normalize : bool
            True if want pot to be normalized as a cond probability
            or cond amplitude, P(x|y) or A(x|y),  where x is ord_nodes[-1]

        Returns
        -------
        pandas.DataFrame

        """
        pot = DiscreteCondPot(is_quantum, ord_nodes, bias=0)
        for row in range(len(pot_df.index)):
            # print([pot_df.iloc[row, col]
            #                              for col in range(len(ord_nodes))
            #                              ])
            index = [
                ord_nodes[col].st_name_index(str(pot_df.iloc[row, col]))
                for col in range(len(ord_nodes))
            ]
            pot.pot_arr[tuple(index)] = pot_df.loc[row, 'pot_values']

        if normalize:
            pot.normalize_self()
        # print(pot)
        return pot
Example #2
0
    def convert_pot_df_to_pot(is_quantum, pot_df, ord_nodes, normalize=True):
        """
        Returns a Potential pot with ordered nodes ord_nodes.

        Parameters
        ----------
        is_quantum : bool
        pot_df : pandas.DataFrame
            a dataframe returned by learn_pot_df()
        ord_nodes : list[BayesNode]
            ordered nodes of Potential pot that is returned
        normalize : bool
            True if want pot to be normalized as a cond probability
            or cond amplitude, P(x|y) or A(x|y),  where x is ord_nodes[-1]

        Returns
        -------
        pandas.DataFrame

        """
        pot = DiscreteCondPot(is_quantum, ord_nodes, bias=0)
        for row in range(len(pot_df.index)):
            # print([pot_df.iloc[row, col]
            #                              for col in range(len(ord_nodes))
            #                              ])
            index = [
                    ord_nodes[col].st_name_index(str(pot_df.iloc[row, col]))
                    for col in range(len(ord_nodes))
                ]
            pot.pot_arr[tuple(index)] = pot_df.loc[row, 'pot_values']

        if normalize:
            pot.normalize_self()
        # print(pot)
        return pot
Example #3
0
    def convert_pot_df_to_pot(is_quantum,
                              pot_df,
                              ord_nodes,
                              s_d_pair,
                              use_int_sts=None,
                              normalize=True):
        """
        Returns a DiscreteCondPot pot with ordered nodes ord_nodes.

        Parameters
        ----------
        is_quantum : bool
        pot_df : pandas.DataFrame
            a dataframe returned by learn_pot_df(). pot_df must have one
            column for each node in ord_nodes plus additioanl final column
            with pot values
        ord_nodes : list[BayesNode]
            ordered nodes of Potential pot that is returned. Nodes must be
            ordered so that pot_df[:-1].columns = [nd.name for nd in
            ord_nodes]
        s_d_pair : tuple[int|str, float]
            a pair consisting of a state and a degs. The state is taken
            directly from states_df, so it might be a digit or an actual
            state name, depending on whether use_int_sts was True or False
            when states_df was generated. The state is a state of the focus
            node (the last column of states_cols corresponds to the focus
            node). The degs is an angle in degrees. For an s_d_pair equal to
            (x0,ang), whenever pot(C=x| pa(C)=y) = 0 for all x, we will set
            pot(C=x|pa(C)=y) = exp( 1j*ang*pi/180)*delta(x, x0) in the
            quantum case and pot( C=x| pa(C)=y) = delta(x, x0) in the
            classical case.
        use_int_sts : bool
            True if states in states_df are integers, False if they are the
            actual state names. If the parameter use_int_sts is set to None,
            this function will attempt to find its bool value using the
            function NetStrucLner:int_sts_detector()
        normalize : bool
            True if want pot to be normalized as a cond probability
            or cond amplitude, P(x|y) or A(x|y), where x is ord_nodes[-1]

        Returns
        -------
        DiscreteCondPot

        """
        for k, vtx in enumerate(pot_df.columns[:-1]):
            assert ord_nodes[k].name == vtx
        focus_vtx = pot_df.columns[-2]
        focus_nd = ord_nodes[-1]
        if use_int_sts is None:
            use_int_sts = NetParamsLner.int_sts_detector(pot_df[:-1])

        def int_state(st):
            if use_int_sts:
                return int(st)
            else:
                return focus_nd.pos_of_st_name(str(st))

        pot = DiscreteCondPot(is_quantum, ord_nodes, bias=0)
        for row in range(len(pot_df.index)):
            # print([pot_df.iloc[row, col]
            #                              for col in range(len(ord_nodes))
            #                              ])
            if not use_int_sts:
                arr_index = [
                    ord_nodes[col].pos_of_st_name(str(pot_df.iloc[row, col]))
                    for col in range(len(ord_nodes))
                ]

            else:
                arr_index = [
                    int(pot_df.iloc[row, col]) for col in range(len(ord_nodes))
                ]
            pot.pot_arr[tuple(arr_index)] = \
                    pot_df.loc[row, 'last_col_with_vals']

        if normalize:
            try:
                pot.normalize_self()
            except UnNormalizablePot as xce:
                # print('caught exception')
                focus_index = int_state(s_d_pair[0])
                arr_index = tuple(list(xce.pa_indices) + [focus_index])
                # print('********************arr_index', arr_index)
                if is_quantum:
                    pot.pot_arr[arr_index] = \
                        np.exp(1j * s_d_pair[1] * np.pi / 180)
                else:
                    pot.pot_arr[arr_index] = 1.0
                print('mended pot:\n', pot)
                # try normalizing pot again now that it is mended
                pot.normalize_self()
        return pot
    def convert_pot_df_to_pot(is_quantum, pot_df, ord_nodes,
                s_d_pair, use_int_sts=None, normalize=True):
        """
        Returns a DiscreteCondPot pot with ordered nodes ord_nodes.

        Parameters
        ----------
        is_quantum : bool
        pot_df : pandas.DataFrame
            a dataframe returned by learn_pot_df(). pot_df must have one
            column for each node in ord_nodes plus additioanl final column
            with pot values
        ord_nodes : list[BayesNode]
            ordered nodes of Potential pot that is returned. Nodes must be
            ordered so that pot_df[:-1].columns = [nd.name for nd in
            ord_nodes]
        s_d_pair : tuple[int|str, float]
            a pair consisting of a state and a degs. The state is taken
            directly from states_df, so it might be a digit or an actual
            state name, depending on whether use_int_sts was True or False
            when states_df was generated. The state is a state of the focus
            node (the last column of states_cols corresponds to the focus
            node). The degs is an angle in degrees. For an s_d_pair equal to
            (x0,ang), whenever pot(C=x| pa(C)=y) = 0 for all x, we will set
            pot(C=x|pa(C)=y) = exp( 1j*ang*pi/180)*delta(x, x0) in the
            quantum case and pot( C=x| pa(C)=y) = delta(x, x0) in the
            classical case.
        use_int_sts : bool
            True if states in states_df are integers, False if they are the
            actual state names. If the parameter use_int_sts is set to None,
            this function will attempt to find its bool value using the
            function NetStrucLner:int_sts_detector()
        normalize : bool
            True if want pot to be normalized as a cond probability
            or cond amplitude, P(x|y) or A(x|y), where x is ord_nodes[-1]

        Returns
        -------
        DiscreteCondPot

        """
        for k, vtx in enumerate(pot_df.columns[:-1]):
                assert ord_nodes[k].name == vtx
        focus_vtx = pot_df.columns[-2]
        focus_nd = ord_nodes[-1]
        if use_int_sts is None:
            use_int_sts = NetStrucLner.int_sts_detector(pot_df[:-1])

        def int_state(st):
            if use_int_sts:
                return int(st)
            else:
                return focus_nd.st_name_index(str(st))

        pot = DiscreteCondPot(is_quantum, ord_nodes, bias=0)
        for row in range(len(pot_df.index)):
            # print([pot_df.iloc[row, col]
            #                              for col in range(len(ord_nodes))
            #                              ])
            if not use_int_sts:
                arr_index = [
                    ord_nodes[col].st_name_index(str(pot_df.iloc[row, col]))
                    for col in range(len(ord_nodes))]
                
            else:
                arr_index = [int(pot_df.iloc[row, col])
                            for col in range(len(ord_nodes))]
            pot.pot_arr[tuple(arr_index)] = \
                    pot_df.loc[row, 'last_col_with_vals']

        if normalize:
            try:
                pot.normalize_self()
            except UnNormalizablePot as xce:
                # print('caught exception')
                focus_index = int_state(s_d_pair[0])
                arr_index = tuple(list(xce.pa_indices) + [focus_index])
                # print('********************arr_index', arr_index)
                if is_quantum:
                    pot.pot_arr[arr_index] = \
                        np.exp(1j * s_d_pair[1] * np.pi / 180)
                else:
                    pot.pot_arr[arr_index] = 1.0
                print('mended pot:\n', pot)
                # try normalizing pot again now that it is mended
                pot.normalize_self()
        return pot