예제 #1
0
    def __init__(self, sim_name, result_file, tafn_to_safety_period, fmt=None):
        self._sim_name = sim_name
        self._result_names = ('received ratio',
                              'normal latency', 'ssd', 'captured',
                              'time after first normal', 'repeats')

        self._results = results.Results(
            sim_name,
            result_file,
            parameters=tuple(),
            results=self._result_names
        )

        self.tafn_to_safety_period = tafn_to_safety_period

        self.fmt = fmt
        if fmt is None:
            from data.table.data_formatter import TableDataFormatter
            self.fmt = TableDataFormatter()
예제 #2
0
파일: CommandLine.py 프로젝트: Chen-Gu/slp
    def _run_table(self, args):
        protectionless_results = results.Results(
            self.algorithm_module.result_file_path,
            parameters=self.algorithm_module.local_parameter_names,
            results=('sent', 'norm(norm(sent,time taken),network size)',
                     'normal latency', 'captured', 'received ratio'))

        fmt = TableDataFormatter(convert_to_stddev=args.show_stddev)

        result_table = fake_result.ResultTable(protectionless_results, fmt)

        self._create_table(self.algorithm_module.name + "-results",
                           result_table,
                           show=args.show)
예제 #3
0
    def __init__(self,
                 results,
                 fmt=None,
                 hide_parameters=None,
                 extractors=None,
                 resize_to_width=False,
                 longtable=True,
                 caption_values=None,
                 caption_prefix="",
                 combined_columns=None):
        self.results = results
        self.fmt = fmt
        self.parameter_names_to_use = tuple([
            x for x in self.results.parameter_names if x not in hide_parameters
        ]) if hide_parameters else self.results.parameter_names
        self.extractors = extractors
        self.resize_to_width = resize_to_width
        self.caption_values = caption_values
        self.longtable = longtable
        self.caption_prefix = caption_prefix

        self.combined_columns = {}

        if combined_columns:
            for params, header in combined_columns.items():
                if all(param in self.results.parameter_names
                       for param in params):
                    self.parameter_names_to_use = tuple([
                        x for x in self.parameter_names_to_use
                        if x not in params
                    ])

                    self.combined_columns[params] = header

        if fmt is None:
            from data.table.data_formatter import TableDataFormatter
            self.fmt = TableDataFormatter()
예제 #4
0
    def _run_table(self, args):
        from data.table.summary_formatter import TableDataFormatter
        fmt = TableDataFormatter()

        parameters = [
            'repeats',
            'time taken',
            'received ratio',
            'captured',
            'normal latency',
            'norm(sent,time taken)',
            'attacker distance',
        ]

        def results_filter(params):
            return params["noise model"] != "casino-lab" or params[
                "configuration"] != "SourceCorner" or params[
                    "source period"] == "0.125"

        hide_parameters = [
        ]  #'buffer size', 'max walk length', 'pr direct to sink']
        caption_values = ["network size"]

        extractors = {
            # Just get the distance of attacker 0 from node 0 (the source in SourceCorner)
            "attacker distance": lambda yvalue: yvalue[(0, 0)]
        }

        self._create_results_table(args.sim,
                                   parameters,
                                   fmt=fmt,
                                   results_filter=results_filter,
                                   hide_parameters=hide_parameters,
                                   extractors=extractors,
                                   resize_to_width=True,
                                   caption_values=caption_values,
                                   show=args.show)
예제 #5
0
    def _run_table(self, args):
        protectionless_results = results.Results(
            self.algorithm_module.result_file_path,
            parameters=self.algorithm_module.local_parameter_names,
            results=(
                'sent',
                'delivered',
                'time taken',
                #'energy impact',
                #'energy impact per node',
                'energy impact per node per second',
                'norm(norm(sent,time taken),num_nodes)',
                'normal latency',
                'ssd',
                'attacker distance',
            ))

        fmt = TableDataFormatter(convert_to_stddev=args.show_stddev)

        result_table = fake_result.ResultTable(protectionless_results, fmt)

        self._create_table(self.algorithm_module.name + "-results",
                           result_table,
                           show=args.show)
예제 #6
0
class ResultTable(object):
    def __init__(self,
                 results,
                 fmt=None,
                 hide_parameters=None,
                 extractors=None,
                 resize_to_width=False,
                 longtable=True,
                 caption_values=None,
                 caption_prefix="",
                 combined_columns=None):
        self.results = results
        self.fmt = fmt
        self.parameter_names_to_use = tuple([
            x for x in self.results.parameter_names if x not in hide_parameters
        ]) if hide_parameters else self.results.parameter_names
        self.extractors = extractors
        self.resize_to_width = resize_to_width
        self.caption_values = caption_values
        self.longtable = longtable
        self.caption_prefix = caption_prefix

        self.combined_columns = {}

        if combined_columns:
            for params, header in combined_columns.items():
                if all(param in self.results.parameter_names
                       for param in params):
                    self.parameter_names_to_use = tuple([
                        x for x in self.parameter_names_to_use
                        if x not in params
                    ])

                    self.combined_columns[params] = header

        if fmt is None:
            from data.table.data_formatter import TableDataFormatter
            self.fmt = TableDataFormatter()

    def _column_layout(self):
        parameter_count = len(self.parameter_names_to_use) + len(
            self.combined_columns)
        result_count = len(self.results.result_names)

        inital_header = "|l|"  # Source Period
        parameter_header = "|".join(
            "c" * parameter_count) + "|" if parameter_count else ""
        result_header = "|" + "|".join("c" * result_count) + "|"

        return inital_header + parameter_header + result_header

    def _title_row(self, row):
        titles = [
            "{" + self.fmt.format_header(title)[row] + "}"
            for title in ("source period", ) + self.parameter_names_to_use
        ]

        for (combined, header) in self.combined_columns.items():
            titles.append(header[row])

        titles.extend([
            "{" + self.fmt.format_header(title)[row] + "}"
            for title in self.results.result_names
        ])

        return "        " + "\\rowcolor{gray!40}" + " & ".join(titles) + "\\\\"

    def _extract(self, name, value):
        if self.extractors:
            return self.extractors.get(name, lambda x: x)(value)
        else:
            return value

    def caption_formatter(self, table_key):
        caption = " and ".join([
            "{} {}".format(name.title(), latex.escape(value))
            for (name,
                 value) in zip(self.results.global_parameter_names, table_key)
            if self.caption_values is None or name in self.caption_values
        ])

        return self.caption_prefix + caption

    def write_tables(self,
                     stream,
                     param_filter=lambda *args: True,
                     font_size=None):
        def _custom_sort(name, values):
            if name == "configuration":
                return sorted(values, key=configuration_rank)
            elif name == "network size":
                return map(str, sorted(map(int, values)))
            else:
                return sorted(values)

        storted_parameters = [
            _custom_sort(name, values)
            for (name, values) in self.results.parameters()
        ]

        for table_key in itertools.product(*storted_parameters):

            try:
                source_periods = sorted(
                    set(self.results.data[table_key].keys()))
            except KeyError as ex:
                print("Unable to find source period for  {}".format(ex))
                continue

            caption = self.caption_formatter(table_key)

            if not self.longtable:
                print('\\begin{table}[H]', file=stream)
                print('    \\centering', file=stream)

            else:
                print('{\\centering', file=stream)

            if font_size:
                print('    \\{}'.format(font_size), file=stream)
            print('    \\rowcolors{2}{white}{gray!15}', file=stream)

            if self.longtable:
                if self.resize_to_width:
                    print('\\begin{{longtabu}} to \\textwidth {{{}}}'.format(
                        self._column_layout()),
                          file=stream)
                else:
                    print('\\begin{{longtabu}} {{{}}}'.format(
                        self._column_layout()),
                          file=stream)

                print('\\hiderowcolors', file=stream)
                print('\\caption{{\\normalsize {}}}\\\\'.format(caption),
                      file=stream)
                print('\\showrowcolors', file=stream)

            else:
                if self.resize_to_width:
                    print('    \\resizebox{\\textwidth}{!}{%', file=stream)
                print('    \\begin{{tabular}}{{{}}}'.format(
                    self._column_layout()),
                      file=stream)

            print('        \\hline', file=stream)
            print(self._title_row(0), file=stream)
            print(self._title_row(1), file=stream)
            print('        \\hline', file=stream)

            if self.longtable:
                print('        \\endfirsthead', file=stream)
                print(self._title_row(0), file=stream)
                print(self._title_row(1), file=stream)
                print('        \\hline', file=stream)
                print('        \\endhead', file=stream)

            for source_period in source_periods:
                items = self.results.data[table_key][source_period].items()

                items = [(k, v) for (k, v) in items if param_filter(*k)]

                for (params, results) in sorted(items,
                                                key=lambda item: item[0]):
                    to_print = [
                        self.fmt.format_value("source period", source_period)
                    ]

                    paramsd = dict(zip(self.results.parameter_names, params))

                    for (name, value) in paramsd.items():
                        if name in self.parameter_names_to_use:
                            to_print.append(self.fmt.format_value(name, value))

                    for (combined, header) in self.combined_columns.items():
                        names, loc = zip(*[(n, paramsd[n]) for n in combined])

                        try:
                            to_print.append(self.fmt.format_value(names, loc))
                        except RuntimeError:
                            to_print.append("(" + ", ".join(map(str, loc)) +
                                            ")")

                    for (name, value) in zip(self.results.result_names,
                                             results):
                        to_print.append(
                            self.fmt.format_value(name,
                                                  self._extract(name, value)))

                    print(" & ".join(to_print) + "\\\\", file=stream)
                print('        \\hline', file=stream)

            if not self.longtable:
                print('    \\end{tabular}', file=stream)

                if self.resize_to_width:
                    print('    }%', file=stream)

                print('\\caption{{{}}} '.format(caption), file=stream)

                print('\\end{table}', file=stream)

            else:
                print('\\end{longtabu}', file=stream)
                print('\\par}', file=stream)

            #print('\\vspace{-0.5cm}', file=stream)

            print('', file=stream)
예제 #7
0
class TableGenerator:
    def __init__(self, sim_name, result_file, tafn_to_safety_period, fmt=None):
        self._sim_name = sim_name
        self._result_names = ('received ratio',
                              'normal latency', 'ssd', 'captured',
                              'time after first normal', 'repeats')

        self._results = results.Results(
            sim_name,
            result_file,
            parameters=tuple(),
            results=self._result_names
        )

        self.tafn_to_safety_period = tafn_to_safety_period

        self.fmt = fmt
        if fmt is None:
            from data.table.data_formatter import TableDataFormatter
            self.fmt = TableDataFormatter()

    def _get_name_and_value(self, result, name):
        return name, result[self._result_names.index(name)]

    def _get_just_value(self, result, name):
        return result[self._result_names.index(name)]['mean']

    def write_tables(self, *args, **kwargs):
        if self._sim_name == "real":
            self._write_testbed_tables(*args, **kwargs)
        else:
            self._write_tables(*args, **kwargs)

    def _write_tables(self, stream, param_filter=None):

        global_parameter_names = self._results.global_parameter_names

        global_values = [
            tuple(sorted(getattr(self._results, self._results.name_to_attr(name))))
            for name
            in global_parameter_names
        ]

        product_all = list(itertools.product(*global_values))

        all_keys = set(self._results.data.keys())

        network_size_index = global_parameter_names.index('network size')

        # Get rid of network size
        filtered_product = [x[:network_size_index] + x[network_size_index+1:] for x in product_all if x in all_keys]
        global_parameter_names = global_parameter_names[:network_size_index] + global_parameter_names[network_size_index+1:]

        network_sizes = {x[network_size_index] for x in product_all}

        if not any(table_key in self._results.data for table_key in product_all):
            raise RuntimeError("Could not find any parameter combination in the results")

        # Convert to set to remove duplicates
        for product_key in sorted(set(filtered_product)):

            product_key_dict = dict(zip(global_parameter_names, product_key))

            print(product_key_dict)

            if param_filter is not None and not param_filter(product_key_dict):
                print("Skipping {}".format(product_key))
                continue

            caption_string = " and ".join(
                f"\\textbf{{{latex.escape(product_key[i])}}} {name}"
                for (i, name) in enumerate(global_parameter_names)
            )

            print('\\begin{table}[H]', file=stream)
            print('\\vspace{-0.35cm}', file=stream)
            print('\\caption{{Safety Periods for the {}}}'.format(caption_string), file=stream)
            print('\\centering', file=stream)
            print('\\begin{tabular}{ | c | c || c | c | c | c | c || c || c | }', file=stream)
            print('\\hline', file=stream)
            print('Size & Period & Received & Source-Sink   & Latency   & Repeats & Time After First  & Safety Period & Captured \\tabularnewline', file=stream)
            print('~    & (sec)  & (\\%)    & Distance (hop)& (msec)    & ~       & Normal (seconds)  & (seconds)     & (\\%)    \\tabularnewline', file=stream)
            print('\\hline', file=stream)
            print('', file=stream)

            for size in sorted(network_sizes, key=int):

                data_key = product_key[:network_size_index] + (size,) + product_key[network_size_index:]

                if data_key not in self._results.data:
                    print("Skipping {} as it could not be found in the results".format(data_key))
                    continue

                for src_period in sorted(self._results.data[data_key]):

                    result = self._results.data[data_key][src_period][tuple()]

                    get_name_and_value = partial(self._get_name_and_value, result)
                    get_just_value = partial(self._get_just_value, result)

                    safety_period = self.tafn_to_safety_period(
                        get_just_value('time after first normal'))

                    rcvd_ratio = self.fmt.format_value(*get_name_and_value('received ratio'))
                    ssd = self.fmt.format_value(*get_name_and_value('ssd'))
                    lat = self.fmt.format_value(*get_name_and_value('normal latency'))
                    tafn = self.fmt.format_value(*get_name_and_value('time after first normal'))
                    cap = self.fmt.format_value(*get_name_and_value('captured'))
                    repeats = self.fmt.format_value(*get_name_and_value('repeats'))
                
                    print(f'{size} & {src_period} & {rcvd_ratio} & {ssd} & {lat} & {repeats} & {tafn} & {safety_period:0.2f} & {cap} \\tabularnewline', file=stream)
                    
                print('\\hline', file=stream)
                print('', file=stream)

            print('\\end{tabular}', file=stream)
            print('\\end{table}', file=stream)
            print('', file=stream)

    def _write_testbed_tables(self, stream, param_filter=lambda *args: True):

        global_parameter_names = self._results.global_parameter_names

        global_values = [
            tuple(sorted(getattr(self._results, self._results.name_to_attr(name))))
            for name
            in global_parameter_names
        ]

        product_all = list(itertools.product(*global_values))

        all_keys = set(self._results.data.keys())

        if not any(table_key in self._results.data for table_key in product_all):
            raise RuntimeError("Could not find any parameter combination in the results")

        for product_key in sorted(product_all):
            product_key_dict = dict(zip(global_parameter_names, product_key))

            print(product_key_dict)

            if param_filter is not None and not param_filter(product_key_dict):
                print("Skipping {}".format(product_key))
                continue

            caption_string = " and ".join(
                f"\\textbf{{{latex.escape(product_key[i])}}} {name}"
                for (i, name) in enumerate(global_parameter_names)
            )

            print('\\begin{table}[H]', file=stream)
            print('\\vspace{-0.35cm}', file=stream)
            print('\\caption{{Safety Periods for the {}}}'.format(caption_string), file=stream)
            print('\\centering', file=stream)
            print('\\begin{tabular}{ | c || c | c | c | c | c || c || c | }', file=stream)
            print('\\hline', file=stream)
            print('Period & Received & Source-Sink   & Latency   & Repeats & Time After First  & Safety Period & Captured \\tabularnewline', file=stream)
            print('(sec)  & (\\%)    & Distance (hop)& (msec)    & ~       & Normal (seconds)  & (seconds)     & (\\%)    \\tabularnewline', file=stream)
            print('\\hline', file=stream)
            print('', file=stream)

            if product_key not in self._results.data:
                print("Skipping {} as it could not be found in the results".format(data_key))
                continue

            for src_period in sorted(self._results.data[product_key]):

                result = self._results.data[product_key][src_period][tuple()]

                get_name_and_value = partial(self._get_name_and_value, result)
                get_just_value = partial(self._get_just_value, result)

                safety_period = self.tafn_to_safety_period(
                    get_just_value('time after first normal'))

                rcvd_ratio = self.fmt.format_value(*get_name_and_value('received ratio'))
                ssd = self.fmt.format_value(*get_name_and_value('ssd'))
                lat = self.fmt.format_value(*get_name_and_value('normal latency'))
                tafn = self.fmt.format_value(*get_name_and_value('time after first normal'))
                cap = self.fmt.format_value(*get_name_and_value('captured'))
                repeats = self.fmt.format_value(*get_name_and_value('repeats'))
            
                print(f'{src_period} & {rcvd_ratio} & {ssd} & {lat} & {repeats} & {tafn} & {safety_period:0.2f} & {cap} \\tabularnewline', file=stream)

            print('\\hline', file=stream)
            print('\\end{tabular}', file=stream)
            print('\\end{table}', file=stream)
            print('', file=stream)

    def _get_result_mapping(self, result_names, accessor):
        # (size, configuration, attacker model, noise model, communication model, fault model, distance) -> source period -> individual result
        result = {}

        try:
            indexes = [self._result_names.index(result_name) for result_name in result_names]
        except ValueError as ex:
            raise RuntimeError(f"The results do not contain any of '{result_names}'. The available names are: '{self._result_names}'", ex)

        for (table_key, other_items) in self._results.data.items():
            for (source_period, items) in other_items.items():

                individual_results = [items[tuple()][index] for index in indexes]

                result.setdefault(table_key, {})[source_period] = accessor(*individual_results)

        return result

    def safety_periods(self):
        # (size, configuration, attacker model, noise model, communication model, distance) -> source period -> safety period
        return self._get_result_mapping(('time after first normal',),
                                        lambda tafn: self.tafn_to_safety_period(tafn['mean']))

    def time_taken(self):
        # (size, configuration, attacker model, noise model, communication model, distance) -> source period -> time taken
        return self._get_result_mapping(('time taken',),
                                        lambda tt: tt['mean'])