예제 #1
0
def save_local_robustness_results_to_csv(results, samples, outdir):
    '''
    saves local robustness summary and detailed results in csv format.
    '''
    n_inputs, n_outputs = len(samples[0][0]), len(samples[0][1])

    summary_lines = ['leps,ueps\n']
    details_lines = [
        's,leps,ueps,spred,' +
        ','.join([f'cex_x{x}' for x in range(n_inputs)] +
                 [f'cex_y{y}' for y in range(n_outputs)]) + '\n'
    ]
    summary, details = results
    summary_lines.append(','.join([str(summary[0]), str(summary[1])]) + '\n')
    for s, detail in enumerate(details):
        leps, ueps, spred, cex = detail[0], detail[1], samples[s][1].index(
            max(samples[s][1])), detail[2]
        cex = cex[0] if cex else ([0 for i in range(n_inputs)],
                                  [0 for i in range(n_outputs)])
        details_lines.append(','.join(
            [str(s), str(leps), str(ueps),
             str(spred)] + [str(x)
                            for x in cex[0]] + [str(y)
                                                for y in cex[1]]) + '\n')
    create_dirpath(outdir)
    summary_file = os.path.join(outdir, 'local_summary.csv')
    details_file = os.path.join(outdir, 'local_details.csv')
    with open(summary_file, 'w') as f:
        f.writelines(summary_lines)
        logger.info(f'wrote summary to {summary_file}')
    with open(details_file, 'w') as f:
        f.writelines(details_lines)
        logger.info(f'wrote detils to {details_file}')
예제 #2
0
def save_sensitivity_results_to_csv(results, samples, outdir):
    '''
    saves sensitivity summary and detailed results in csv format.

    @param results (dict): sensitivity results dictionary ({x0: (summary_tuple, details_list), ...})
    @param outdir (string): output directory
    '''
    summary_lines = ['x,leps,ueps\n']
    details_lines = ['x,s,leps,ueps,spred\n']
    for x_id, result in results.items():
        x = int(x_id[1:])
        summary, details = result
        summary_lines.append(','.join(
            [str(x), str(summary[0]), str(summary[1])]) + '\n')
        details_lines.extend([
            ','.join([
                str(x),
                str(s),
                str(detail[0]),
                str(detail[1]),
                str(samples[s][1].index(max(samples[s][1])))
            ]) + '\n' for s, detail in enumerate(details)
        ])
    summary_file = os.path.join(outdir, 'summary.csv')
    details_file = os.path.join(outdir, 'details.csv')
    create_dirpath(outdir)
    with open(summary_file, 'w') as f:
        f.writelines(summary_lines)
        logger.info(f'wrote summary to {summary_file}')
    with open(details_file, 'w') as f:
        f.writelines(details_lines)
        logger.info(f'wrote detils to {details_file}')
예제 #3
0
    def save(lgkm, outpath='./lgkm.p', serialize=False, include_data=False):
        '''
        saves a LabelGuidedKMeans object (or list of dicts) to a pickle

        Parameters
            lgkm         : LabelGuidedKMeans object
            outpath      : the output filepath
            serialized   : bool (if true, serializes lgkm to list of dicts)
            include_data : bool (if true, saves X and Y - much larger file size)
        '''
        data = lgkm if not serialize else LabelGuidedKMeansUtils.serialize_regions(
            lgkm, include_data=include_data)
        create_dirpath(os.path.dirname(os.path.abspath(outpath)))
        pickle.dump(data, open(outpath, 'wb'))
        print(f'saved to {outpath}')
예제 #4
0
def save_verified_regions(vregions, outdir=default_outdir, n_categories=5):
    n_features, n_categories = vregions[0]['centroid'].shape[0], n_categories
    header = ','.join([f'cx{i}' for i in range(n_features)] + [
        'radius', 'epsilon', 'n', 'density', 'category', 'oradius', 'duration'
    ] + [f'cex_x{x}' for x in range(n_features)] +
                      [f'cex_y{y}' for y in range(n_categories)])
    rows = []
    for r in vregions:
        cex = r['counterexample']
        cex = cex[0] if cex else (['' for i in range(n_features)],
                                  ['' for i in range(n_categories)])
        rows.append(','.join([str(x) for x in r['centroid']] + [
            str(v) for v in (r['radius'], r['epsilon'], r['n'], r['density'],
                             r['category'], r['oradius'], r['duration'])
        ] + [str(x) for x in cex[0]] + [str(y) for y in cex[1]]))
    create_dirpath(outdir)
    outpath = os.path.join(outdir, 'vregions.csv')
    with open(outpath, 'w') as f:
        f.writelines('\n'.join([header] + rows))
        logger.info(f'wrote verified regions to {outpath}')
예제 #5
0
    def save_regions_csv(lgkm, sort=True, sortrev=True, outpath='./lgkm.csv'):
        '''
        saves a LabelGuidedKMeans object's regions to CSV

        Parameters
            lgkm    : LabelGuidedKMeans object
            sort    : bool (sorts in ascending order by radius & density)
            sortrev : bool (reverses order of sort)
            outpath : the output filepath
        '''
        regions = lgkm.get_regions(sort=sort, sortrev=sortrev)
        n_features = regions[0].centroid.shape[0]
        header = ','.join([f'cx{i}' for i in range(n_features)] +
                          ['radius', 'n', 'density', 'category'])
        rows = []
        for r in regions:
            rows.append(','.join(
                [str(x) for x in r.centroid] +
                [str(v) for v in (r.radius, r.n, r.density, r.category)]))
        create_dirpath(outpath)
        with open(outpath, 'w') as f:
            f.writelines('\n'.join([header] + rows))
            print(f'saved regions to {outpath}')