コード例 #1
0
ファイル: test_benford.py プロジェクト: jordanmng/test
def helper_extract_leading_digits_payments(num_payments, col, num_digits):
    prefix = TEST_DATA_DIR + "/payments_" + str(num_payments) + "_"
    input_filename = prefix+"input.csv"
    amounts = read_column_from_csv(input_filename, col, True)
    amounts_copy = amounts[:]

    expected_filename = prefix + str(num_digits) + "_leading_digits.txt"
    expected = []
    for x in read_column_from_csv(expected_filename, 0, True):
        expected.append(int(x))
    leading_digits = extract_leading_digits_from_list(amounts, num_digits)
    assert leading_digits == expected
    assert amounts == amounts_copy, "Do not change the list that is passed to your function!"
コード例 #2
0
ファイル: test_benford.py プロジェクト: jordanmng/test
def helper_test_compute_benford_MAD(prefix, col, num_digits):
    input_filename = prefix + "input.csv"
    amounts = read_column_from_csv(input_filename, col, True)
    actual = compute_benford_MAD(amounts, num_digits)

    expected_filename = prefix + "computed_benford_mad_{0}_output.txt".format(num_digits)
    compare_actual_expected_from_file([actual], expected_filename)
コード例 #3
0
ファイル: test_benford.py プロジェクト: jordanmng/test
def compare_actual_expected_from_file(actual, expected_filename):
    # get expected list of values from the file
    expected = []
    for x in read_column_from_csv(expected_filename, 0, True):
        expected.append(float(x))

    compare_actual_expected(actual, expected)
コード例 #4
0
ファイル: test_benford.py プロジェクト: jordanmng/test
def helper_test_compute_benford_dist(prefix, col, num_digits):
    input_filename = prefix + "input.csv"
    amounts = read_column_from_csv(input_filename, col, True)
    amounts_copy = amounts[:]
    actual = compute_benford_dist(amounts, num_digits)

    expected_filename = prefix + "computed_benford_dist_{0}_output.txt".format(num_digits)
    compare_actual_expected_from_file(actual, expected_filename)
    assert amounts == amounts_copy, "Do not change the list that is passed to your function!"
コード例 #5
0
ファイル: plot_benford.py プロジェクト: kwitte2232/CS121
    plt.xticks(range(lb, ub, 10**(num_digits-1)))

    # compute limits for the y axis
    max_val = max(max(expected), max(actual))
    y_ub = max_val + max_val * .1
    plt.ylim(0,y_ub)

    # add labels
    plt.title("Actual (blue) and expected (red) Benford distributions")
    if num_digits ==1: 
        plt.xlabel("Leading digit")
    else:
        plt.xlabel("Leading digits")
    plt.ylabel("Proportion")

    plt.savefig(output_filename)


if __name__=="__main__":
    if len(sys.argv) != 5:
        print("usage: python benford.py <input filename> <column number>  <num digits> <output filename>")
    else:
        input_filename = sys.argv[1]
        data = util.read_column_from_csv(input_filename, int(sys.argv[2]), True)
        num_digits = int(sys.argv[3])
        output_filename = sys.argv[4]
        plot_benford_dist(data, num_digits, output_filename)
        # print only four digits after the decimal point
        print("MAD: {0:.4}".format(benford.compute_benford_MAD(data, num_digits)))