Skip to main content

Compare decimal representations of floating-point numbers.

Project description

comparedecimal

A package to compare decimal representations of floating-point numbers, including a command-line tool to report on the similarity between data in CSV files.

Installation

The comparedecimal package can be installed from source by running pip3 install . or python3 setup.py within its directory. The command-line utility comparecsv will be installed as part of the package.

Rationale

I wrote this tool to help me when organizing and tidying up scientific data sets. It occasionally happens that I come across two files which I suspect contain the same data, but because they've been through different processing steps, the values are no longer byte-for-byte identical – for example, a CSV file may have been opened in Excel and saved again, truncating the number of decimal places in the floating-point values. In these cases, it's useful to be able to ascertain how compatible the files are – is it possible that one is a lower-precision version of the other (e.g. ‘2.0’ and ‘1.99’)? Or that the numerical values are in fact identical but the strings representing them differ (e.g. ‘1234’ and ‘1.234e3’)? comparedecimal provides a Python package and command-line tool to answer such questions.

Equality levels

For any pair of strings, comparedecimal determines one of five equality levels between them. The highest possible equality level is always given, so for instance a pair of strings which is both ‘compatible’ and ‘close’ will be classified as ‘compatible’. The equality levels are as follows:

  1. Identical: the character strings are equal.

  2. Numerically equal: the character strings, when parsed as floating-point decimals, produce numbers which are equal.

  3. Compatible: there exists a single floating-point number which, when formatted, could produce both the string representations. Under this definition, for example, "1.9" and "1.95" would be compatible, because they are both valid representations of 1.949. This equality level is particularly useful for finding duplicate files with differing levels of precision.

  4. Close: the difference between the numbers represented by the character strings is below a certain threshold (formally: denoting the represented values by a and b and the threshold by t, they are close if have the same sign and max(abs(a), abs(b)) <= (1 + t) * min(abs(a), abs(b))). This equality level is useful for finding ‘duplicate’ files generated from the same data in which truncation or rounding errors have caused values to diverge slightly.

  5. Unequal: The character strings are unequal and cannot represent the same number, and the values they represent are not close in the sense defined above.

The comparedecimal package

The comparedecimal package provides the class DecimalComparer, which is initialized with a separator string (used to divide lines for multi-field comparisons) and a threshold (used to define the Close equality level described above). The class provides the following methods:

  • compare_strings to compare individual strings
  • compare_string_lists to compare lists of strings
  • compare_line_lists to compare lists of lines, using the predefined separator to split each line into strings

DecimalComparer has an instance variable totals. totals is a dictionary with a key for each equality level (represented by the EqualityLevel enum). The associated value for each equality level is an integer representing the total number of comparisons made so far which resulted in this equality level.

The comparecsv command-line tool

comparecsv is a command line utility for finding duplicates among delimited textual files containing numerical data (e.g. CSV files), even when the string representations of the data differ.

comparecsv takes as its arguments two delimited files with the same layout (i.e. same number of rows and columns) and compares them field by field. For each pair of corresponding fields, it determines a level of equality as defined above.

When run on two files, comparecsv prints total counts for field pairs at each level of equality. For every field pair, the highest possible equality level is given: for instance, if two fields are not identical but are numerically equal, then they will (by definition) also be compatible and close; in this case, comparecsv will report the equality level ‘numerically equal’.

License

Copyright 2018, 2019 Pontus Lurcock pont@talvi.net

Released under the GNU GPL v3; see the file COPYING for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

comparedecimal-1.0.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

comparedecimal-1.0.0-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file comparedecimal-1.0.0.tar.gz.

File metadata

  • Download URL: comparedecimal-1.0.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for comparedecimal-1.0.0.tar.gz
Algorithm Hash digest
SHA256 30ae0ee0d907cf5f3d56c3e33aba98238e1b78a907b8bd6ba938691159541df3
MD5 87e2a303c972baae6c869df7d06e151d
BLAKE2b-256 6cb22d37ea6eaac3aad49aad0724d32ee745a537f15c782d2e413b5c1c9e2585

See more details on using hashes here.

File details

Details for the file comparedecimal-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: comparedecimal-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

File hashes

Hashes for comparedecimal-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2523519e5da355d6dad9a678fa5d6f766fa98c925f04323fa788f3d7d7b05ce
MD5 480c15fece1ca522c5b140d04849036c
BLAKE2b-256 98a06c4185db03615a0f42a6542a09d92c51d2a4961686545d7ca59af3d158ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page