format-multiple-errors 0.0.2__py3-none-any.whl → 0.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,10 @@ from sys import exit, stderr
10
10
 
11
11
  try:
12
12
  import pandas as pd
13
+
14
+ have_pandas = True
13
15
  except ImportError:
14
- pd = None
16
+ have_pandas = False
15
17
 
16
18
  from .formatter import format_multiple_errors
17
19
  from .pandas import format_dataframe_errors, ColumnSpec
@@ -34,7 +36,7 @@ def _format_numbers(args: Namespace) -> None:
34
36
 
35
37
  def _check_pandas() -> None:
36
38
  """Check if Pandas is available; complain and exit if not."""
37
- if pd is None:
39
+ if not have_pandas:
38
40
  print(
39
41
  "Pandas is not installed, but is required to process a table.",
40
42
  file=stderr,
@@ -76,7 +78,7 @@ def _float_or_pair(arg: str) -> float | tuple[float, float]:
76
78
  if len(split_arg) == 1:
77
79
  return float(arg)
78
80
  elif len(split_arg) == 2:
79
- return tuple(map(float, split_arg))
81
+ return (float(split_arg[0]), float(split_arg[1]))
80
82
  else:
81
83
  message = f"Can't parse {arg} as a number or pair of numbers."
82
84
  raise ValueError(message)
@@ -94,13 +96,19 @@ def _parse_columnspec(arg: str) -> str | ColumnSpec:
94
96
  if not isinstance(split_arg[0], str):
95
97
  raise ValueError("First column has to be a single central value.")
96
98
 
97
- error_columns = []
99
+ error_columns: list[str | tuple[str, str]] = []
98
100
  for error_spec in split_arg[1:]:
99
101
  split_error = error_spec.split("-")
100
102
  if len(split_error) == 1:
101
103
  error_columns.append(error_spec)
104
+ elif len(split_error) == 2:
105
+ error_columns.append((split_error[0], split_error[1]))
102
106
  else:
103
- error_columns.append(tuple(split_error))
107
+ message = (
108
+ f"Expected one or two components in error_spec {error_spec}, "
109
+ f"found {len(split_error)}."
110
+ )
111
+ raise ValueError(message)
104
112
 
105
113
  return ColumnSpec(split_arg[0], *error_columns)
106
114
 
@@ -43,6 +43,10 @@ def format_multiple_errors(
43
43
  with `significant_figures` significant figures.
44
44
  "central": The central `value` is printed
45
45
  with `significant_figures` significant figures.
46
+ "largest": The largest uncertainty is printed
47
+ with `significant_figures` significant figures.
48
+ "decimal_places": Values are truncated to `significant_figures`
49
+ decimal places.
46
50
 
47
51
  significant_figures (default: 2):
48
52
  The number of significant figures to format.
@@ -68,14 +72,23 @@ def format_multiple_errors(
68
72
  normalised_value, normalised_errors
69
73
  )
70
74
 
71
- length_value = _get_length_value(
72
- normalised_value, normalised_errors, length_control
73
- )
74
- first_digit_index, decimal_places = _get_rounding_indices(
75
- length_value, significant_figures
76
- )
75
+ if length_control == "decimal_places":
76
+ if significant_figures < 0:
77
+ raise ValueError("Number of decimal places must be non-negative")
78
+ decimal_places = significant_figures
79
+ decimals_required = decimal_places > 0
80
+ else:
81
+ length_value = _get_length_value(
82
+ normalised_value, normalised_errors, length_control
83
+ )
84
+ first_digit_index, decimal_places = _get_rounding_indices(
85
+ length_value, significant_figures
86
+ )
87
+ decimals_required = _decimals_required(
88
+ first_digit_index, significant_figures, exponential
89
+ )
77
90
 
78
- if _decimals_required(first_digit_index, significant_figures, exponential):
91
+ if decimals_required:
79
92
  formatted_numbers = [f"{normalised_value:.0{decimal_places}f}"] + list(
80
93
  _format_errors_only(normalised_errors, decimal_places, abbreviate)
81
94
  )
@@ -202,10 +215,16 @@ def _get_length_value(value: float, errors: Errors, length_control: str) -> floa
202
215
  return value
203
216
 
204
217
  return length
218
+ if length_control == "largest":
219
+ length = _get_largest(errors)
220
+ if length is None:
221
+ return value
222
+
223
+ return length
205
224
 
206
225
  raise ValueError(
207
- f"{length_control} is not a value option for length_control."
208
- '(Available options are "smallest", "central".)'
226
+ f"{length_control} is not a value option for length_control. "
227
+ '(Available options are "smallest", "central", "largest".)'
209
228
  )
210
229
 
211
230
 
@@ -219,6 +238,16 @@ def _get_smallest(errors: Errors) -> float | None:
219
238
  return min(flat_errors)
220
239
 
221
240
 
241
+ def _get_largest(errors: Errors) -> float | None:
242
+ """Given a list of errors (number or tuples of two numbers),
243
+ find the largest finite number."""
244
+ flat_errors = _flatten_errors(errors, exclude=[math.inf])
245
+ if not flat_errors:
246
+ return None
247
+
248
+ return max(flat_errors)
249
+
250
+
222
251
  def _flatten_errors(errors: Errors, exclude: Set | Sequence = frozenset()) -> list:
223
252
  """Given a list of errors (number or tuples of two numbers),
224
253
  flatten it to a list of numbers."""
@@ -57,7 +57,7 @@ def _format_column(value: pd.Series, *errors: pd.Series, **fme_kwargs) -> pd.Ser
57
57
  return pd.Series(data=formatted_errors, index=index)
58
58
 
59
59
 
60
- def _tuplify(columns: list[pd.Series]) -> pd.Series:
60
+ def _tuplify(columns: list[pd.Series] | tuple[pd.Series]) -> pd.Series:
61
61
  """
62
62
  Turn columns into one column of tuples.
63
63
  Given a list of Pandas Series, returns a single Series
@@ -142,7 +142,13 @@ class ColumnSpec:
142
142
  Specification of columns to include in calls to `format_dataframe_errors()`
143
143
  """
144
144
 
145
- def __init__(self, value: str, *errors: str, name: str | None = None, **fme_kwargs):
145
+ def __init__(
146
+ self,
147
+ value: str,
148
+ *errors: str | tuple[str, str],
149
+ name: str | None = None,
150
+ **fme_kwargs,
151
+ ):
146
152
  """
147
153
  Specify a set of columns to turn into a single column containing formatted
148
154
  values and uncertainties.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: format_multiple_errors
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: A small widget to be able to format multiple, asymmetric errors easily.
5
5
  Author-email: Ed Bennett <e.j.bennett@swansea.ac.uk>
6
6
  Project-URL: Homepage, https://github.com/edbennett/format_multiple_errors
@@ -11,11 +11,15 @@ Classifier: Operating System :: OS Independent
11
11
  Requires-Python: >=3.9
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
+ Provides-Extra: tables
15
+ Requires-Dist: pandas; extra == "tables"
16
+ Requires-Dist: jinja2; extra == "tables"
17
+ Dynamic: license-file
14
18
 
15
19
  # format_multiple_errors
16
20
 
17
21
  [![Run tests](https://github.com/edbennett/format_multiple_errors/actions/workflows/pytest.yaml/badge.svg)](https://github.com/edbennett/format_multiple_errors/actions/workflows/pytest.yaml)
18
- [![Code quality](https://github.com/edbennett/format_multiple_errors/actions/workflows/codequality.yaml/badge.svg)](https://github.com/edbennett/format_multiple_errors/actions/workflows/codequality.yaml)
22
+ [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/edbennett/format_multiple_errors/main.svg)](https://results.pre-commit.ci/latest/github/edbennett/format_multiple_errors/main)
19
23
 
20
24
  A small library intended to make it easy to format numbers like
21
25
 
@@ -28,9 +32,16 @@ $$(6.829 \pm 0.013 {}^{+0.104}_{-0.096})\times10^{5}$$
28
32
 
29
33
  ## Installation
30
34
 
31
- To install, open a terminal and run:
35
+ To install the bare library, open a terminal and run:
32
36
 
33
- pip install https://github.com/edbennett/format_multiple_errors
37
+ pip install format-multiple-errors
38
+
39
+ If you will be formatting tables,
40
+ additional dependencies are required.
41
+ To install these,
42
+ instead run
43
+
44
+ pip install format-multiple-errors[tables]
34
45
 
35
46
 
36
47
  ## Usage as a library
@@ -93,6 +104,12 @@ These options may be combined:
93
104
  >>> format_multiple_errors(123.45, 3.14, (2.82, 12.91), length_control="central", significant_figures=5, latex=True, abbreviate=True, exponential=True)
94
105
  '1.2345(314)({}^{282}_{1291}) \\times 10^{2}'
95
106
 
107
+ Other options for `length_control` are:
108
+
109
+ - `"largest"`: controls the significant digits of the largest uncertainty
110
+ - `"decimal_places"`: instead sets the absolute number of decimal places,
111
+ which must be non-negative.
112
+
96
113
 
97
114
  ### Formatting DataFrames
98
115
 
@@ -0,0 +1,11 @@
1
+ format_multiple_errors/__init__.py,sha256=ZLtmZ1Pe3zyxnic-b4nhtuajNj8llWykspVPpFaN7Rc,460
2
+ format_multiple_errors/__main__.py,sha256=Jiy0F5VlGLfzkPfvAFpuZluNevVITxXrxvTf6ztH5pI,6157
3
+ format_multiple_errors/formatter.py,sha256=ypxkaUhmf8TgICk7ysUm2li_4XgZJ-02avvu5gnZOc8,11798
4
+ format_multiple_errors/pandas.py,sha256=S_-ThiTlbItx_lUngFYY547Abp5fJaNGoQGXwwHK0i4,7689
5
+ format_multiple_errors/typing.py,sha256=k0p3GDaHGXHd3zmBtNg0-cvcMzEupzN4RHaLdNvxX6U,342
6
+ format_multiple_errors-0.0.4.dist-info/licenses/LICENSE,sha256=G3SZheaYJW8JV0Njnnyfi7QwUJWh9evcPRQ-nAXi8Ro,1086
7
+ format_multiple_errors-0.0.4.dist-info/METADATA,sha256=hj6mtoIYOCPXVDCSd5Wijj0xqtooLyg9mDAf4VoCsNM,6811
8
+ format_multiple_errors-0.0.4.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
9
+ format_multiple_errors-0.0.4.dist-info/entry_points.txt,sha256=PeHYlzbvQciSJX3DWuM-Y5Xwi9Is0jO-LlUgdSkUTp4,79
10
+ format_multiple_errors-0.0.4.dist-info/top_level.txt,sha256=I_xD5oYbrw0qjNhbzAlj_sqNhLrkoJsTE_ozCqnlaZc,23
11
+ format_multiple_errors-0.0.4.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.1.0)
2
+ Generator: setuptools (82.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,11 +0,0 @@
1
- format_multiple_errors/__init__.py,sha256=ZLtmZ1Pe3zyxnic-b4nhtuajNj8llWykspVPpFaN7Rc,460
2
- format_multiple_errors/__main__.py,sha256=tV8VPGPwq2d3CWJp9hFSDDMyHHL3uY2jvipxeoH_dGY,5825
3
- format_multiple_errors/formatter.py,sha256=GnfJjP4HbYXxTq-Yx1E8DhbviPqRj0t3_icPTYgQ2P0,10744
4
- format_multiple_errors/pandas.py,sha256=qoK_UvXIErJJdWKfJRyOvtMNeR6JHaUv_QUlTZjnCHg,7605
5
- format_multiple_errors/typing.py,sha256=k0p3GDaHGXHd3zmBtNg0-cvcMzEupzN4RHaLdNvxX6U,342
6
- format_multiple_errors-0.0.2.dist-info/LICENSE,sha256=G3SZheaYJW8JV0Njnnyfi7QwUJWh9evcPRQ-nAXi8Ro,1086
7
- format_multiple_errors-0.0.2.dist-info/METADATA,sha256=1vJY4LlcZhCuxYANbRIGpUxs8coFFgecUKof8zuk6t4,6334
8
- format_multiple_errors-0.0.2.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
9
- format_multiple_errors-0.0.2.dist-info/entry_points.txt,sha256=PeHYlzbvQciSJX3DWuM-Y5Xwi9Is0jO-LlUgdSkUTp4,79
10
- format_multiple_errors-0.0.2.dist-info/top_level.txt,sha256=I_xD5oYbrw0qjNhbzAlj_sqNhLrkoJsTE_ozCqnlaZc,23
11
- format_multiple_errors-0.0.2.dist-info/RECORD,,