コード例 #1
0
ファイル: plotter.py プロジェクト: SimonStreicher/FaultMap
def get_scenario_data_vectors(graphdata, example_sourcefile, example_scenario):
    """Extract value matrices from different scenarios."""

    # example_sourcefile is based on the scenario with which the graph was called
    # This graph will be plotted multiple times for all scenarios involved
    # TODO: Find a more elegant solution

    valuematrices = []

    for scenario in graphdata.scenarios:

        # Change sourcefile path on scenario level

        sourcefile = data_processing.change_dirtype(
            example_sourcefile, example_scenario, scenario
        )
        valuematrix, _ = data_processing.read_header_values_datafile(sourcefile)
        valuematrices.append(valuematrix)

    return valuematrices
コード例 #2
0
def get_scenario_data_vectors(graphdata, example_sourcefile, example_scenario):
    """Extract value matrices from different scenarios."""

    # example_sourcefile is based on the scenario with which the graph was called
    # This graph will be plotted multiple times for all scenarios involved
    # TODO: Find a more elegant solution

    valuematrices = []

    for scenario in graphdata.scenarios:

        # Change sourcefile path on scenario level

        sourcefile = data_processing.change_dirtype(example_sourcefile,
                                                    example_scenario, scenario)
        valuematrix, _ = \
            data_processing.read_header_values_datafile(sourcefile)
        valuematrices.append(valuematrix)

    return valuematrices
コード例 #3
0
ファイル: figtypes.py プロジェクト: zzuiekongning/FaultMap
def fig_maxval_variables(graphdata, graph, scenario, savedir):
    """Generates a figure that shows dependence of method values on
    different variables in a scenario.
    Draws one line for each scenario.
    """

    # Get values for x-axis
    graphdata.get_xvalues(graph)
    graphdata.get_linelabels(graph)
    graphdata.get_legendbbox(graph)

    # Get back from savedir to trends source
    # This is up to the embed type level
    trendsdir = data_processing.change_dirtype(savedir, "graphs", "trends")

    # Extract current method and sigstatus from weightdir
    dirparts = data_processing.getfolders(trendsdir)
    # The method is three folders up from the embed level
    method = dirparts[-3]
    # The sigstatus is two folders up from the embed level
    sigstatus = dirparts[-2]

    plt.figure(1, (12, 6))

    for count, scenario in enumerate(graphdata.scenario):

        sourcefile = filename_template.format(
            graphdata.case,
            scenario,
            graphdata.method[0],
            graphdata.sigstatus,
            graphdata.boxindex,
            graphdata.sourcevar,
        )

        if delays:
            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile)
            max_values = [
                max(valuematrix[:, index + 1])
                for index in range(valuematrix.shape[1] - 1)
            ]
        else:
            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile)
            max_values = valuematrix[1:]

        plt.plot(
            graphdata.xvals,
            max_values,
            "--",
            marker="o",
            markersize=4,
            label=graphdata.linelabels[count],
        )

        if drawfit:
            graphdata.fitlinelabels(graphname)
            fit_params = np.polyfit(np.log(graphdata.xvals),
                                    np.log(max_values), 1)
            fit_y = [(i * fit_params[0] + fit_params[1])
                     for i in np.log(graphdata.xvals)]
            fitted_vals = [np.exp(val) for val in fit_y]

            plt.loglog(graphdata.xvals,
                       fitted_vals,
                       "--",
                       label=graphdata.fitlinelabels[count])

    plt.ylabel(yaxislabel[graphdata.method[0]], fontsize=14)
    plt.xlabel(r"Time constant ($\tau$)", fontsize=14)
    plt.legend(bbox_to_anchor=graphdata.legendbbox)

    if graphdata.axis_limits is not False:
        plt.axis(graphdata.axis_limits)

    plt.savefig(graph_filename_template.format(graphname))
    plt.close()

    return None
コード例 #4
0
ファイル: figtypes.py プロジェクト: zzuiekongning/FaultMap
def fig_values_vs_boxes(graphdata, graph, scenario, savedir):
    """Plots measure values for different boxes and multiple variable pairs.

    Makes use of the trend data generated by trendextraction from arrays.

    """

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)

    # Get back from savedir to trends source
    # This is up to the embed type level
    trendsdir = data_processing.change_dirtype(savedir, "graphs", "trends")

    # Extract current method and sigstatus from weightdir
    dirparts = data_processing.getfolders(trendsdir)
    # The method is three folders up from the embed level
    method = dirparts[-3]
    # The sigstatus is two folders up from the embed level
    sigstatus = dirparts[-2]

    # Select typenames based on method and sigstatus
    if method[:16] == "transfer_entropy":

        typenames = [
            "weight_absolute_trend", "signtested_weight_directional_trend"
        ]
        delay_typenames = ["delay_absolute_trend", "delay_directional_trend"]

        if sigstatus == "sigtested":
            typenames.append("sigweight_absolute_trend")
            typenames.append("signtested_sigweight_directional_trend")

    else:
        typenames = ["weight_trend"]
        delay_typenames = ["delay_trend"]

        if sigstatus == "sigtested":
            typenames.append("sigweight_trend")

    # Y axis label lookup dictionary

    yaxislabel_lookup = {
        "weight_absolute_trend": "absolute",
        "signtested_weight_directional_trend": "directional",
        "delay_absolute_trend": "absolute",
        "delay_directional_trend": "directional",
        "sigweight_absolute_trend": "absolute",
        "signtested_sigweight_directional_trend": "directional",
    }

    for typename in typenames:
        for sourcevar in graphdata.sourcevars:

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 15:
                yaxislabelstring = yaxislabel_lookup[typename] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Box", fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(trendsdir, sourcevar,
                                      "{}.csv".format(typename))

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile)

            for destvar in graphdata.destvars:
                destvarindex = graphdata.destvars.index(destvar)
                ax.plot(
                    np.arange(len(valuematrix[:, 0])),
                    valuematrix[:, destvarindex],
                    marker="o",
                    markersize=4,
                    label=destvar,
                )

            # Shrink current axis by 20%
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            ax.legend(loc="center left", bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)

            plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir, "{}_{}_{}.pdf".format(scenario, typename,
                                                   sourcevar)))
            plt.close()

    for delay_typename in delay_typenames:
        for sourcevar in graphdata.sourcevars:

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method

            ax.set_ylabel(r"Delay ({})".format(graphdata.timeunit),
                          fontsize=14)
            ax.set_xlabel(r"Box", fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(trendsdir, sourcevar,
                                      "{}.csv".format(delay_typename))

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile)

            for destvar in graphdata.destvars:
                destvarindex = graphdata.destvars.index(destvar)
                ax.plot(
                    np.arange(len(valuematrix[:, 0])),
                    valuematrix[:, destvarindex],
                    marker="o",
                    markersize=4,
                    label=destvar,
                )

            # Shrink current axis by 20%
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            ax.legend(loc="center left", bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)

            plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir, "{}_{}_{}.pdf".format(scenario, delay_typename,
                                                   sourcevar)))
            plt.close()

    return None
コード例 #5
0
ファイル: figtypes.py プロジェクト: zzuiekongning/FaultMap
def fig_diffscen_vs_delay(graphdata, graph, scenario, savedir):
    """Plot one variable from different scenarios.
    Assumes only a single index in varindexes.
    """

    plt.close("all")

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_boxindexes(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)
    graphdata.get_sigthresholdplotting(graph)
    graphdata.get_linelabels(graph)

    # Get x-axis values
    #    graphdata.get_xvalues(graphname)

    # Get back from savedir to weightdata source
    # This is up to the embed type level
    weightdir = data_processing.change_dirtype(savedir, "graphs", "weightdata")

    # Extract current method from weightdir
    dirparts = data_processing.getfolders(weightdir)
    # The method is two folders up from the embed level
    method = dirparts[-3]

    # Select typenames based on method
    if method[:16] == "transfer_entropy":
        typenames = []
        thresh_typenames = []
        graphdata.get_typenames(graph)
        if "simple" in graphdata.typenames:
            typenames.append("weights_absolute")
            thresh_typenames.append("sigthresh_absolute")
        if "directional" in graphdata.typenames:
            typenames.append("weights_directional")
            thresh_typenames.append("sigthresh_directional")
        # typenames = [
        #     'weights_absolute',
        #     'weights_directional']
        # thresh_typenames = [
        #     'sigthresh_absolute',
        #     'sigthresh_directional']
    else:
        typenames = ["weights"]
        thresh_typenames = ["sigthresh"]

    # Get labels
    if graphdata.linelabels:
        graphdata.get_labelformat(graph)
        labels = [
            graphdata.labelformat.format(linelabel)
            for linelabel in graphdata.linelabels
        ]
    else:
        labels = [destvar for destvar in graphdata.destvars]

    for typeindex, typename in enumerate(typenames):
        for boxindex, sourcevar in itertools.product(graphdata.boxindexes,
                                                     graphdata.sourcevars):

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Delay ({})".format(graphdata.timeunit),
                          fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(
                weightdir,
                typename,
                "box{:03d}".format(boxindex),
                "{}.csv".format(sourcevar),
            )

            _, headers = data_processing.read_header_values_datafile(
                sourcefile)

            # Get valuematrices
            valuematrices = plotter.get_scenario_data_vectors(
                graphdata, sourcefile, scenario)

            bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.8)

            xaxis_intervals = []
            relevant_values = []
            for scenarioindex, valuematrix in enumerate(valuematrices):
                # # Get the maximum from each valuematrix in the entry
                # # which corresponds to the common element of interest.
                #
                # # TODO: Fix this old hardcoded remnant
                # # 3 referred to the index of tau=1 for many cases involved
                # #        values = valuematrix[:, 3]
                # values = valuematrix[:, graphdata.varindexes]
                # xaxis_intervals.append(valuematrix[:, 0])
                # relevant_values.append(values)

                for destvarindex, destvar in enumerate(graphdata.destvars):
                    destvarvalueindex = headers.index(destvar)
                    ax.plot(
                        valuematrix[:, 0],
                        valuematrix[:, destvarvalueindex],
                        marker=markers[scenarioindex],
                        markersize=8,
                        label=labels[scenarioindex],
                    )

                    label_index = list(
                        valuematrix[:, destvarvalueindex]).index(
                            max(valuematrix[:, destvarvalueindex]))

                    ax.text(
                        valuematrix[:, 0][label_index],
                        valuematrix[:, destvarvalueindex][label_index],
                        labels[scenarioindex],
                        ha="center",
                        va="center",
                        size=10,
                        bbox=bbox_props,
                    )

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)
            else:
                plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.pdf".format(scenario, typename,
                                                    boxindex, sourcevar),
                ),
                bbox_inches="tight",
                pad_inches=0,
            )

            plt.close("all")

            # Also save as SVG to allow manual editing
            # plt.savefig(
            #     os.path.join(savedir, '{}_{}_box{:03d}_{}.svg'.format(
            #         scenario, typename, boxindex, sourcevar)),
            #     bbox_inches='tight', pad_inches=0, format='svg')
            # plt.close()

    return None
コード例 #6
0
ファイル: figtypes.py プロジェクト: zzuiekongning/FaultMap
def fig_values_vs_delays(graphdata, graph, scenario, savedir):
    """Generates a figure that shows dependence of method values on delays.

    Constrained to a single scenario, box and source variable.

    Automatically iterates through absolute and directional weights.
    Able to iterate through multiple box indexes and source variables.

    Provides the option to plot weight significance threshold values.

    """
    plt.close("all")

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_boxindexes(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)
    graphdata.get_sigthresholdplotting(graph)
    graphdata.get_linelabels(graph)

    # Get back from savedir to weightdata source
    # This is up to the embed type level
    weightdir = data_processing.change_dirtype(savedir, "graphs", "weightdata")

    # Extract current method from weightdir
    dirparts = data_processing.getfolders(weightdir)
    # The method is two folders up from the embed level
    method = dirparts[-3]

    # Select typenames based on method
    if method[:16] == "transfer_entropy":
        typenames = []
        thresh_typenames = []
        graphdata.get_typenames(graph)
        if "simple" in graphdata.typenames:
            typenames.append("weights_absolute")
            thresh_typenames.append("sigthresh_absolute")
        if "directional" in graphdata.typenames:
            typenames.append("weights_directional")
            thresh_typenames.append("sigthresh_directional")
        # typenames = [
        #     'weights_absolute',
        #     'weights_directional']
        # thresh_typenames = [
        #     'sigthresh_absolute',
        #     'sigthresh_directional']
    else:
        typenames = ["weights"]
        thresh_typenames = ["sigthresh"]

    # Get labels
    if graphdata.linelabels:
        graphdata.get_labelformat(graph)
        labels = [
            str(graphdata.labelformat).format(linelabel)
            for linelabel in graphdata.linelabels
        ]
    else:
        labels = [destvar for destvar in graphdata.destvars]

    for typeindex, typename in enumerate(typenames):
        for boxindex, sourcevar in itertools.product(graphdata.boxindexes,
                                                     graphdata.sourcevars):

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Delay ({})".format(graphdata.timeunit),
                          fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(
                weightdir,
                typename,
                "box{:03d}".format(boxindex),
                "{}.csv".format(sourcevar),
            )

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile)

            if graphdata.thresholdplotting:
                threshold_sourcefile = os.path.join(
                    weightdir,
                    thresh_typenames[typeindex],
                    "box{:03d}".format(boxindex),
                    "{}.csv".format(sourcevar),
                )

                threshmatrix, headers = data_processing.read_header_values_datafile(
                    threshold_sourcefile)

            bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.8)

            for destvarindex, destvar in enumerate(graphdata.destvars):
                destvarvalueindex = headers.index(destvar)
                ax.plot(
                    valuematrix[:, 0],
                    valuematrix[:, destvarvalueindex],
                    marker=markers[destvarindex],
                    markersize=8,
                    label=labels[destvarindex],
                )

                label_index = list(valuematrix[:, destvarvalueindex]).index(
                    max(valuematrix[:, destvarvalueindex]))

                ax.text(
                    valuematrix[:, 0][label_index],
                    valuematrix[:, destvarvalueindex][label_index],
                    labels[destvarindex],
                    ha="center",
                    va="center",
                    size=10,
                    bbox=bbox_props,
                )

                if graphdata.thresholdplotting:
                    ax.plot(
                        threshmatrix[:, 0],
                        threshmatrix[:, destvarvalueindex],
                        marker="x",
                        markersize=4,
                        linestyle=":",
                        label=destvar + " threshold",
                    )

            # Shrink current axis by 20%
            #                box = ax.get_position()
            #                ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            # ax.legend(loc='center left',
            #          bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)
            else:
                plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.pdf".format(scenario, typename,
                                                    boxindex, sourcevar),
                ),
                bbox_inches="tight",
                pad_inches=0,
            )

            # Also save as SVG to allow manual editing
            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.svg".format(scenario, typename,
                                                    boxindex, sourcevar),
                ),
                bbox_inches="tight",
                pad_inches=0,
                format="svg",
            )
            plt.close()

    return None
コード例 #7
0
def fig_values_vs_delays(graphdata, graph, scenario, savedir):
    """Generates a figure that shows dependence of method values on delays.

    Constrained to a single scenario, box and source variable.

    Automatically iterates through absolute and directional weights.
    Able to iterate through multiple box indexes and source variables.

    Provides the option to plot weight significance threshold values.

    """

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_boxindexes(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)
    graphdata.get_sigthresholdplotting(graph)
    graphdata.get_linelabels(graph)

    # Get back from savedir to weightdata source
    # This is up to the embed type level
    weightdir = data_processing.change_dirtype(savedir, 'graphs', 'weightdata')

    # Extract current method from weightdir
    dirparts = data_processing.getfolders(weightdir)
    # The method is two folders up from the embed level
    method = dirparts[-3]

    # Select typenames based on method
    if method[:16] == 'transfer_entropy':
        typenames = ['weights_absolute', 'weights_directional']
        thresh_typenames = ['sigthresh_absolute', 'sigthresh_directional']
    else:
        typenames = ['weights']
        thresh_typenames = ['sigthresh']

    # Get labels
    if graphdata.linelabels:
        graphdata.get_labelformat(graph)
        labels = [
            graphdata.labelformat.format(linelabel)
            for linelabel in graphdata.linelabels
        ]
    else:
        labels = [destvar for destvar in graphdata.destvars]

    for typeindex, typename in enumerate(typenames):
        for boxindex in graphdata.boxindexes:
            for sourcevar in graphdata.sourcevars:

                fig = plt.figure(1, figsize=(12, 6))
                ax = fig.add_subplot(111)
                if len(typename) > 8:
                    yaxislabelstring = typename[8:] + '_' + method
                else:
                    yaxislabelstring = method
                ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
                ax.set_xlabel(r'Delay ({})'.format(graphdata.timeunit),
                              fontsize=14)

                # Open data file and plot graph
                sourcefile = os.path.join(weightdir, typename,
                                          'box{:03d}'.format(boxindex),
                                          '{}.csv'.format(sourcevar))

                valuematrix, headers = \
                    data_processing.read_header_values_datafile(sourcefile)

                if graphdata.thresholdplotting:
                    threshold_sourcefile = os.path.join(
                        weightdir, thresh_typenames[typeindex],
                        'box{:03d}'.format(boxindex),
                        '{}.csv'.format(sourcevar))

                    threshmatrix, headers = \
                        data_processing.read_header_values_datafile(
                            threshold_sourcefile)

                for destvarindex, destvar in enumerate(graphdata.destvars):
                    destvarvalueindex = headers.index(destvar)
                    ax.plot(valuematrix[:, 0],
                            valuematrix[:, destvarvalueindex],
                            marker="o",
                            markersize=4,
                            label=labels[destvarindex])

                    if graphdata.thresholdplotting:
                        ax.plot(threshmatrix[:, 0],
                                threshmatrix[:, destvarvalueindex],
                                marker="x",
                                markersize=4,
                                linestyle=':',
                                label=destvar + ' threshold')

                # Shrink current axis by 20%


#                box = ax.get_position()
#                ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

                ax.legend(loc='center left',
                          bbox_to_anchor=graphdata.legendbbox)

                if graphdata.axis_limits is not False:
                    ax.axis(graphdata.axis_limits)
                else:
                    plt.gca().set_ylim(bottom=-0.05)

                plt.savefig(
                    os.path.join(
                        savedir, '{}_{}_box{:03d}_{}.pdf'.format(
                            scenario, typename, boxindex, sourcevar)))
                plt.close()

    return None
コード例 #8
0
ファイル: figtypes.py プロジェクト: SimonStreicher/FaultMap
def fig_maxval_variables(graphdata, graph, scenario, savedir):
    """Generates a figure that shows dependence of method values on
    different variables in a scenario.
    Draws one line for each scenario.
    """

    # Get values for x-axis
    graphdata.get_xvalues(graph)
    graphdata.get_linelabels(graph)
    graphdata.get_legendbbox(graph)

    # Get back from savedir to trends source
    # This is up to the embed type level
    trendsdir = data_processing.change_dirtype(savedir, "graphs", "trends")

    # Extract current method and sigstatus from weightdir
    dirparts = data_processing.getfolders(trendsdir)
    # The method is three folders up from the embed level
    method = dirparts[-3]
    # The sigstatus is two folders up from the embed level
    sigstatus = dirparts[-2]

    plt.figure(1, (12, 6))

    for count, scenario in enumerate(graphdata.scenario):

        sourcefile = filename_template.format(
            graphdata.case,
            scenario,
            graphdata.method[0],
            graphdata.sigstatus,
            graphdata.boxindex,
            graphdata.sourcevar,
        )

        if delays:
            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile
            )
            max_values = [
                max(valuematrix[:, index + 1])
                for index in range(valuematrix.shape[1] - 1)
            ]
        else:
            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile
            )
            max_values = valuematrix[1:]

        plt.plot(
            graphdata.xvals,
            max_values,
            "--",
            marker="o",
            markersize=4,
            label=graphdata.linelabels[count],
        )

        if drawfit:
            graphdata.fitlinelabels(graphname)
            fit_params = np.polyfit(np.log(graphdata.xvals), np.log(max_values), 1)
            fit_y = [
                (i * fit_params[0] + fit_params[1]) for i in np.log(graphdata.xvals)
            ]
            fitted_vals = [np.exp(val) for val in fit_y]

            plt.loglog(
                graphdata.xvals, fitted_vals, "--", label=graphdata.fitlinelabels[count]
            )

    plt.ylabel(yaxislabel[graphdata.method[0]], fontsize=14)
    plt.xlabel(r"Time constant ($\tau$)", fontsize=14)
    plt.legend(bbox_to_anchor=graphdata.legendbbox)

    if graphdata.axis_limits is not False:
        plt.axis(graphdata.axis_limits)

    plt.savefig(graph_filename_template.format(graphname))
    plt.close()

    return None
コード例 #9
0
ファイル: figtypes.py プロジェクト: SimonStreicher/FaultMap
def fig_values_vs_boxes(graphdata, graph, scenario, savedir):
    """Plots measure values for different boxes and multiple variable pairs.

    Makes use of the trend data generated by trendextraction from arrays.

    """

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)

    # Get back from savedir to trends source
    # This is up to the embed type level
    trendsdir = data_processing.change_dirtype(savedir, "graphs", "trends")

    # Extract current method and sigstatus from weightdir
    dirparts = data_processing.getfolders(trendsdir)
    # The method is three folders up from the embed level
    method = dirparts[-3]
    # The sigstatus is two folders up from the embed level
    sigstatus = dirparts[-2]

    # Select typenames based on method and sigstatus
    if method[:16] == "transfer_entropy":

        typenames = ["weight_absolute_trend", "signtested_weight_directional_trend"]
        delay_typenames = ["delay_absolute_trend", "delay_directional_trend"]

        if sigstatus == "sigtested":
            typenames.append("sigweight_absolute_trend")
            typenames.append("signtested_sigweight_directional_trend")

    else:
        typenames = ["weight_trend"]
        delay_typenames = ["delay_trend"]

        if sigstatus == "sigtested":
            typenames.append("sigweight_trend")

    # Y axis label lookup dictionary

    yaxislabel_lookup = {
        "weight_absolute_trend": "absolute",
        "signtested_weight_directional_trend": "directional",
        "delay_absolute_trend": "absolute",
        "delay_directional_trend": "directional",
        "sigweight_absolute_trend": "absolute",
        "signtested_sigweight_directional_trend": "directional",
    }

    for typename in typenames:
        for sourcevar in graphdata.sourcevars:

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 15:
                yaxislabelstring = yaxislabel_lookup[typename] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Box", fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(trendsdir, sourcevar, "{}.csv".format(typename))

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile
            )

            for destvar in graphdata.destvars:
                destvarindex = graphdata.destvars.index(destvar)
                ax.plot(
                    np.arange(len(valuematrix[:, 0])),
                    valuematrix[:, destvarindex],
                    marker="o",
                    markersize=4,
                    label=destvar,
                )

            # Shrink current axis by 20%
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            ax.legend(loc="center left", bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)

            plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir, "{}_{}_{}.pdf".format(scenario, typename, sourcevar)
                )
            )
            plt.close()

    for delay_typename in delay_typenames:
        for sourcevar in graphdata.sourcevars:

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method

            ax.set_ylabel(r"Delay ({})".format(graphdata.timeunit), fontsize=14)
            ax.set_xlabel(r"Box", fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(
                trendsdir, sourcevar, "{}.csv".format(delay_typename)
            )

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile
            )

            for destvar in graphdata.destvars:
                destvarindex = graphdata.destvars.index(destvar)
                ax.plot(
                    np.arange(len(valuematrix[:, 0])),
                    valuematrix[:, destvarindex],
                    marker="o",
                    markersize=4,
                    label=destvar,
                )

            # Shrink current axis by 20%
            box = ax.get_position()
            ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            ax.legend(loc="center left", bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)

            plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir, "{}_{}_{}.pdf".format(scenario, delay_typename, sourcevar)
                )
            )
            plt.close()

    return None
コード例 #10
0
ファイル: figtypes.py プロジェクト: SimonStreicher/FaultMap
def fig_diffscen_vs_delay(graphdata, graph, scenario, savedir):
    """Plot one variable from different scenarios.
    Assumes only a single index in varindexes.
    """

    plt.close("all")

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_boxindexes(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)
    graphdata.get_sigthresholdplotting(graph)
    graphdata.get_linelabels(graph)

    # Get x-axis values
    #    graphdata.get_xvalues(graphname)

    # Get back from savedir to weightdata source
    # This is up to the embed type level
    weightdir = data_processing.change_dirtype(savedir, "graphs", "weightdata")

    # Extract current method from weightdir
    dirparts = data_processing.getfolders(weightdir)
    # The method is two folders up from the embed level
    method = dirparts[-3]

    # Select typenames based on method
    if method[:16] == "transfer_entropy":
        typenames = []
        thresh_typenames = []
        graphdata.get_typenames(graph)
        if "simple" in graphdata.typenames:
            typenames.append("weights_absolute")
            thresh_typenames.append("sigthresh_absolute")
        if "directional" in graphdata.typenames:
            typenames.append("weights_directional")
            thresh_typenames.append("sigthresh_directional")
        # typenames = [
        #     'weights_absolute',
        #     'weights_directional']
        # thresh_typenames = [
        #     'sigthresh_absolute',
        #     'sigthresh_directional']
    else:
        typenames = ["weights"]
        thresh_typenames = ["sigthresh"]

    # Get labels
    if graphdata.linelabels:
        graphdata.get_labelformat(graph)
        labels = [
            graphdata.labelformat.format(linelabel)
            for linelabel in graphdata.linelabels
        ]
    else:
        labels = [destvar for destvar in graphdata.destvars]

    for typeindex, typename in enumerate(typenames):
        for boxindex, sourcevar in itertools.product(
            graphdata.boxindexes, graphdata.sourcevars
        ):

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Delay ({})".format(graphdata.timeunit), fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(
                weightdir,
                typename,
                "box{:03d}".format(boxindex),
                "{}.csv".format(sourcevar),
            )

            _, headers = data_processing.read_header_values_datafile(sourcefile)

            # Get valuematrices
            valuematrices = plotter.get_scenario_data_vectors(
                graphdata, sourcefile, scenario
            )

            bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.8)

            xaxis_intervals = []
            relevant_values = []
            for scenarioindex, valuematrix in enumerate(valuematrices):
                # # Get the maximum from each valuematrix in the entry
                # # which corresponds to the common element of interest.
                #
                # # TODO: Fix this old hardcoded remnant
                # # 3 referred to the index of tau=1 for many cases involved
                # #        values = valuematrix[:, 3]
                # values = valuematrix[:, graphdata.varindexes]
                # xaxis_intervals.append(valuematrix[:, 0])
                # relevant_values.append(values)

                for destvarindex, destvar in enumerate(graphdata.destvars):
                    destvarvalueindex = headers.index(destvar)
                    ax.plot(
                        valuematrix[:, 0],
                        valuematrix[:, destvarvalueindex],
                        marker=markers[scenarioindex],
                        markersize=8,
                        label=labels[scenarioindex],
                    )

                    label_index = list(valuematrix[:, destvarvalueindex]).index(
                        max(valuematrix[:, destvarvalueindex])
                    )

                    ax.text(
                        valuematrix[:, 0][label_index],
                        valuematrix[:, destvarvalueindex][label_index],
                        labels[scenarioindex],
                        ha="center",
                        va="center",
                        size=10,
                        bbox=bbox_props,
                    )

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)
            else:
                plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.pdf".format(
                        scenario, typename, boxindex, sourcevar
                    ),
                ),
                bbox_inches="tight",
                pad_inches=0,
            )

            plt.close("all")

            # Also save as SVG to allow manual editing
            # plt.savefig(
            #     os.path.join(savedir, '{}_{}_box{:03d}_{}.svg'.format(
            #         scenario, typename, boxindex, sourcevar)),
            #     bbox_inches='tight', pad_inches=0, format='svg')
            # plt.close()

    return None
コード例 #11
0
ファイル: figtypes.py プロジェクト: SimonStreicher/FaultMap
def fig_values_vs_delays(graphdata, graph, scenario, savedir):
    """Generates a figure that shows dependence of method values on delays.

    Constrained to a single scenario, box and source variable.

    Automatically iterates through absolute and directional weights.
    Able to iterate through multiple box indexes and source variables.

    Provides the option to plot weight significance threshold values.

    """
    plt.close("all")

    graphdata.get_legendbbox(graph)
    graphdata.get_timeunit(graph)
    graphdata.get_boxindexes(graph)
    graphdata.get_sourcevars(graph)
    graphdata.get_destvars(graph)
    graphdata.get_sigthresholdplotting(graph)
    graphdata.get_linelabels(graph)

    # Get back from savedir to weightdata source
    # This is up to the embed type level
    weightdir = data_processing.change_dirtype(savedir, "graphs", "weightdata")

    # Extract current method from weightdir
    dirparts = data_processing.getfolders(weightdir)
    # The method is two folders up from the embed level
    method = dirparts[-3]

    # Select typenames based on method
    if method[:16] == "transfer_entropy":
        typenames = []
        thresh_typenames = []
        graphdata.get_typenames(graph)
        if "simple" in graphdata.typenames:
            typenames.append("weights_absolute")
            thresh_typenames.append("sigthresh_absolute")
        if "directional" in graphdata.typenames:
            typenames.append("weights_directional")
            thresh_typenames.append("sigthresh_directional")
        # typenames = [
        #     'weights_absolute',
        #     'weights_directional']
        # thresh_typenames = [
        #     'sigthresh_absolute',
        #     'sigthresh_directional']
    else:
        typenames = ["weights"]
        thresh_typenames = ["sigthresh"]

    # Get labels
    if graphdata.linelabels:
        graphdata.get_labelformat(graph)
        labels = [
            str(graphdata.labelformat).format(linelabel)
            for linelabel in graphdata.linelabels
        ]
    else:
        labels = [destvar for destvar in graphdata.destvars]

    for typeindex, typename in enumerate(typenames):
        for boxindex, sourcevar in itertools.product(
            graphdata.boxindexes, graphdata.sourcevars
        ):

            fig = plt.figure(1, figsize=(12, 6))
            ax = fig.add_subplot(111)
            if len(typename) > 8:
                yaxislabelstring = typename[8:] + "_" + method
            else:
                yaxislabelstring = method
            ax.set_ylabel(yaxislabel[yaxislabelstring], fontsize=14)
            ax.set_xlabel(r"Delay ({})".format(graphdata.timeunit), fontsize=14)

            # Open data file and plot graph
            sourcefile = os.path.join(
                weightdir,
                typename,
                "box{:03d}".format(boxindex),
                "{}.csv".format(sourcevar),
            )

            valuematrix, headers = data_processing.read_header_values_datafile(
                sourcefile
            )

            if graphdata.thresholdplotting:
                threshold_sourcefile = os.path.join(
                    weightdir,
                    thresh_typenames[typeindex],
                    "box{:03d}".format(boxindex),
                    "{}.csv".format(sourcevar),
                )

                threshmatrix, headers = data_processing.read_header_values_datafile(
                    threshold_sourcefile
                )

            bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.8)

            for destvarindex, destvar in enumerate(graphdata.destvars):
                destvarvalueindex = headers.index(destvar)
                ax.plot(
                    valuematrix[:, 0],
                    valuematrix[:, destvarvalueindex],
                    marker=markers[destvarindex],
                    markersize=8,
                    label=labels[destvarindex],
                )

                label_index = list(valuematrix[:, destvarvalueindex]).index(
                    max(valuematrix[:, destvarvalueindex])
                )

                ax.text(
                    valuematrix[:, 0][label_index],
                    valuematrix[:, destvarvalueindex][label_index],
                    labels[destvarindex],
                    ha="center",
                    va="center",
                    size=10,
                    bbox=bbox_props,
                )

                if graphdata.thresholdplotting:
                    ax.plot(
                        threshmatrix[:, 0],
                        threshmatrix[:, destvarvalueindex],
                        marker="x",
                        markersize=4,
                        linestyle=":",
                        label=destvar + " threshold",
                    )

            # Shrink current axis by 20%
            #                box = ax.get_position()
            #                ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

            # ax.legend(loc='center left',
            #          bbox_to_anchor=graphdata.legendbbox)

            if graphdata.axis_limits is not False:
                ax.axis(graphdata.axis_limits)
            else:
                plt.gca().set_ylim(bottom=-0.05)

            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.pdf".format(
                        scenario, typename, boxindex, sourcevar
                    ),
                ),
                bbox_inches="tight",
                pad_inches=0,
            )

            # Also save as SVG to allow manual editing
            plt.savefig(
                os.path.join(
                    savedir,
                    "{}_{}_box{:03d}_{}.svg".format(
                        scenario, typename, boxindex, sourcevar
                    ),
                ),
                bbox_inches="tight",
                pad_inches=0,
                format="svg",
            )
            plt.close()

    return None
コード例 #12
0
ファイル: noderank.py プロジェクト: zzuiekongning/FaultMap
def dorankcalc(
    noderankdata, scenario, datadir, typename, rank_method, writeoutput, preprocessing
):

    if noderankdata.datatype == "file":
        noderankdata.get_boxes(scenario, datadir, typename)
        gainmatrices = get_gainmatrices(noderankdata, datadir, typename)
        delaymatrices = get_delaymatrices(noderankdata, datadir, typename)

    elif noderankdata.datatype == "function":
        gainmatrices = [noderankdata.gainmatrix]
        delaymatrices = [np.zeros_like(noderankdata.gainmatrix)]
        noderankdata.boxes = [0]

    if len(noderankdata.boxes) > 1:
        generate_diffs = True
    else:
        generate_diffs = False

    rankinglist_name = "rankinglist_{}.csv"
    modgainmatrix_name = "modgainmatrix.csv"
    originalgainmatrix_name = "originalgainmatrix.csv"
    graphfile_name = "graph_{}.gml"
    transientdict_name = "transientdict_{}.json"
    basevaldict_name = "basevaldict_{}.json"
    boxrankdict_name = "boxrankdict_{}.json"
    rel_boxrankdict_name = "rel_boxrankdict_{}.json"

    if generate_diffs:
        dif_rankinglist_name = "dif_rankinglist_{}.csv"
        dif_transientdict_name = "dif_transientdict_{}.json"
        dif_basevaldict_name = "dif_basevaldict_{}.json"
        dif_boxrankdict_name = "dif_boxrankdict_{}.json"
        dif_rel_boxrankdict_name = "dif_rel_boxrankdict_{}.json"
        dif_typename = "dif_" + typename
        dif_gainmatrices = get_gainmatrices(noderankdata, datadir, dif_typename)

    # Create lists to store the backward ranking list
    # for each box and associated gainmatrix ranking result

    # backward_rankinglists = []
    backward_rankingdicts = []
    dif_backward_rankingdicts = []

    for index, gainmatrix in enumerate(gainmatrices):
        if preprocessing:
            # modgainmatrix, _ = \
            #     gainmatrix_preprocessing(gainmatrix)
            modgainmatrix = gainmatrix_tobinary(gainmatrix)
        else:
            modgainmatrix = gainmatrix

        if generate_diffs:
            dif_gainmatrix = dif_gainmatrices[index]
            # Take only positive values for now due to convergence issues
            # TODO: Investigate proper handling of negative edge changes
            mod_dif_gainmatrix = dif_gainmatrix_preprocessing(
                dif_gainmatrix, method="floor"
            )

        delays = delaymatrices[index]

        #       _, dummyweight = gainmatrix_preprocessing(gainmatrix)
        # Set dummyweight to 10
        dummyweight = 10

        # This is where the actual ranking calculation happens
        rankingdict, rankinglist, connections, variables, gains = calc_gainrank(
            modgainmatrix, noderankdata, rank_method, dummyweight
        )

        if generate_diffs:
            # TODO: Review effect of positive differences only
            # Take only positive for now due to convergence issues, but investigate proper handling
            # of negative edge changes
            dif_rankingdict, dif_rankinglist, _, _, _ = calc_gainrank(
                mod_dif_gainmatrix, noderankdata, rank_method, dummyweight
            )
            # dif_backward_rankinglists.append(dif_rankinglist)
            dif_backward_rankingdicts.append(dif_rankingdict)

        # The rest of the function deals with storing the
        # results in the desired formats

        # backward_rankinglists.append(rankinglist)
        backward_rankingdicts.append(rankingdict)

        if writeoutput:
            # Make sure the correct directory exists
            # Take datadir and swop out 'weightdata' for 'noderank'
            savedir = data_processing.change_dirtype(datadir, "weightdata", "noderank")

            config_setup.ensure_existence(os.path.join(savedir, typename[:-7]))

            if preprocessing:

                # Save the modified gainmatrix
                writecsv_looprank(
                    os.path.join(savedir, typename[:-7], modgainmatrix_name),
                    modgainmatrix,
                )

                # Save the original gainmatrix
                writecsv_looprank(
                    os.path.join(savedir, typename[:-7], originalgainmatrix_name),
                    gainmatrix,
                )

        # Export graph files with dummy variables included
        # in backward rankings if available
        #        directions = ['backward']

        if noderankdata.dummies:
            dummystatus = "withdummies"
        else:
            dummystatus = "nodummies"

        # Save the ranking list for each box
        savepath = config_setup.ensure_existence(
            os.path.join(
                savedir,
                typename[:-7],
                "box{:03d}".format(noderankdata.boxes[index] + 1),
                dummystatus,
            )
        )

        writecsv_looprank(
            os.path.join(savepath, rankinglist_name.format(rank_method)), rankinglist
        )

        if generate_diffs:
            writecsv_looprank(
                os.path.join(savepath, dif_rankinglist_name.format(rank_method)),
                dif_rankinglist,
            )

        # Save the graphs to file
        graph, _ = create_importance_graph(
            noderankdata,
            variables,
            connections,
            connections,
            gains,
            delays,
            rankingdict,
        )
        graph_filename = os.path.join(savepath, graphfile_name.format(rank_method))

        # Decided to keep connections natural, will rotate hierarchical layout
        # in post-processing
        nx.readwrite.write_gml(graph, graph_filename)

        # Get ranking dictionaries
        transientdict, basevaldict, boxrankdict, rel_boxrankdict = calc_transient_importancediffs(
            backward_rankingdicts, noderankdata.variablelist
        )

        if generate_diffs:
            # Get ranking dictionaries for difference gain arrays
            dif_transientdict, dif_basevaldict, dif_boxrankdict, dif_rel_boxrankdict = calc_transient_importancediffs(
                dif_backward_rankingdicts, noderankdata.variablelist
            )

        # Store dictonaries using JSON

        # Normal dictionaries
        data_processing.write_dictionary(
            os.path.join(savepath, boxrankdict_name.format(rank_method)), boxrankdict
        )

        data_processing.write_dictionary(
            os.path.join(savepath, rel_boxrankdict_name.format(rank_method)),
            rel_boxrankdict,
        )

        data_processing.write_dictionary(
            os.path.join(savepath, transientdict_name.format(rank_method)),
            transientdict,
        )

        data_processing.write_dictionary(
            os.path.join(savepath, basevaldict_name.format(rank_method)), basevaldict
        )

        if generate_diffs:
            # Difference dictionaries
            data_processing.write_dictionary(
                os.path.join(savepath, dif_boxrankdict_name.format(rank_method)),
                dif_boxrankdict,
            )

            data_processing.write_dictionary(
                os.path.join(savepath, dif_rel_boxrankdict_name.format(rank_method)),
                dif_rel_boxrankdict,
            )

            data_processing.write_dictionary(
                os.path.join(savepath, dif_transientdict_name.format(rank_method)),
                dif_transientdict,
            )

            data_processing.write_dictionary(
                os.path.join(savepath, dif_basevaldict_name.format(rank_method)),
                dif_basevaldict,
            )

    return None