codedx 0.1.0__tar.gz → 0.3.0__tar.gz

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 @@ wheels/
10
10
  # Virtual environments
11
11
  .venv
12
12
 
13
- # local config. while I do use claude to code this
14
- # I dont consider the agent config part of the project.
13
+ # environment file. may contain secrets.
14
+ .env
15
+
16
+ # agent config is not part of the project.
15
17
  .claude
16
18
 
17
19
  # Downloaded at build time — not committed
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## 0.3.0 — 2026-06-08
4
+
5
+ ### Added
6
+
7
+ - `chapter_to_roman(chapter)` — convert chapter number (int, `"1"`, or `"01"`) to Roman numeral (`"I"`–`"XXII"`)
8
+ - `roman_to_chapter(roman)` — convert Roman numeral chapter to int 1–22; case-insensitive
9
+ - `get_range(chapter_code)` — extract code range string from a chapter title (e.g. `"01"` → `"A00-B99"`)
10
+ - All three exported from `codedx` top-level
11
+
12
+ ## 0.2.0
13
+
14
+ - Migrate ICD-10-SE downloads to Ehälsomyndigheten
15
+
16
+ ## 0.1.0
17
+
18
+ - Initial release: ICD-10-SE, WHO ICD-10, ICD-10-CM, KSH97-P, rehab code lookups
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: codedx
3
- Version: 0.1.0
3
+ Version: 0.3.0
4
4
  Summary: Swedish medical coding used for diagnosis codes
5
5
  Author-email: Ludvig Hult <ludvig.hult@gmail.com>
6
6
  Requires-Python: >=3.12
@@ -46,6 +46,13 @@ Dots are dropped throughout, matching what is typically found in databases: `A01
46
46
  pip install codedx
47
47
  ```
48
48
 
49
- Downloads data at install time from Socialstyrelsen, WHO, and CDC. Raw files are not redistributed due to licence restrictions.
49
+ Downloads data at install time from Ehälsomyndigheten, WHO, and CDC. Raw files are not redistributed due to licence restrictions.
50
50
 
51
- > **Note:** Socialstyrelsen is migrating data to Ehälsomyndigheten — download URLs may stop working in the future.
51
+ ## Changelog
52
+
53
+ ### 0.2.0
54
+ - ICD-10-SE and history file now downloaded from Ehälsomyndigheten (replacing Socialstyrelsen URLs)
55
+ - Retired code lookup now uses the comprehensive history file (`historik-icd-10-se-1997-2026.xlsx`) covering all codes 1997–2026, instead of the partial inactivation list
56
+
57
+ ### 0.1.0
58
+ - Initial release
@@ -36,6 +36,13 @@ Dots are dropped throughout, matching what is typically found in databases: `A01
36
36
  pip install codedx
37
37
  ```
38
38
 
39
- Downloads data at install time from Socialstyrelsen, WHO, and CDC. Raw files are not redistributed due to licence restrictions.
39
+ Downloads data at install time from Ehälsomyndigheten, WHO, and CDC. Raw files are not redistributed due to licence restrictions.
40
40
 
41
- > **Note:** Socialstyrelsen is migrating data to Ehälsomyndigheten — download URLs may stop working in the future.
41
+ ## Changelog
42
+
43
+ ### 0.2.0
44
+ - ICD-10-SE and history file now downloaded from Ehälsomyndigheten (replacing Socialstyrelsen URLs)
45
+ - Retired code lookup now uses the comprehensive history file (`historik-icd-10-se-1997-2026.xlsx`) covering all codes 1997–2026, instead of the partial inactivation list
46
+
47
+ ### 0.1.0
48
+ - Initial release
@@ -40,8 +40,8 @@ _MULTI_ZIPS = [
40
40
  "icd10se_latest.zip",
41
41
  "ICD-10-SE latest release (~15 MB)",
42
42
  [
43
- ("icd-10-se.tsv", "https://www.socialstyrelsen.se/globalassets/sharepoint-dokument/dokument-webb/klassifikationer-och-koder/icd-10-se.tsv"),
44
- ("andringar-icd-10-se.xlsx", "https://www.socialstyrelsen.se/globalassets/sharepoint-dokument/dokument-webb/klassifikationer-och-koder/andringar-icd-10-se.xlsx"),
43
+ ("icd-10-se.tsv", "https://samarbetsyta.ehalsomyndigheten.se/download/attachments/451267009/icd-10-se.tsv?api=v2"),
44
+ ("historik-icd-10-se-1997-2026.xlsx", "https://samarbetsyta.ehalsomyndigheten.se/download/attachments/451267009/historik-icd-10-se-1997-2026%20%281%29.xlsx?api=v2"),
45
45
  ],
46
46
  ),
47
47
  ]
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codedx"
3
- version = "0.1.0"
3
+ version = "0.3.0"
4
4
  description = "Swedish medical coding used for diagnosis codes"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -1,6 +1,6 @@
1
1
  import pathlib
2
2
 
3
- __version__ = "0.1.0"
3
+ __version__ = "0.3.0"
4
4
  _CACHE_DIR = pathlib.Path.home() / ".cache" / "codedx" / f"v{__version__}"
5
5
 
6
6
  from codedx._core import ( # noqa: E402
@@ -14,6 +14,7 @@ from codedx._core import ( # noqa: E402
14
14
  is_rehab_code,
15
15
  is_retired_icd10se_code,
16
16
  )
17
+ from codedx.chapters import chapter_to_roman, roman_to_chapter, get_range # noqa: E402
17
18
 
18
19
  __all__ = [
19
20
  "get_name",
@@ -25,4 +26,7 @@ __all__ = [
25
26
  "is_ksh97p_code",
26
27
  "is_rehab_code",
27
28
  "is_retired_icd10se_code",
29
+ "chapter_to_roman",
30
+ "roman_to_chapter",
31
+ "get_range",
28
32
  ]
@@ -0,0 +1,55 @@
1
+ """Chapter and block numeral/range utilities for ICD-10."""
2
+
3
+ import re
4
+
5
+ _INT_TO_ROMAN: dict[int, str] = {
6
+ 1: "I", 2: "II", 3: "III", 4: "IV", 5: "V", 6: "VI", 7: "VII",
7
+ 8: "VIII", 9: "IX", 10: "X", 11: "XI", 12: "XII", 13: "XIII",
8
+ 14: "XIV", 15: "XV", 16: "XVI", 17: "XVII", 18: "XVIII",
9
+ 19: "XIX", 20: "XX", 21: "XXI", 22: "XXII",
10
+ }
11
+
12
+ _ROMAN_TO_INT: dict[str, int] = {v: k for k, v in _INT_TO_ROMAN.items()}
13
+
14
+ _RANGE_RE = re.compile(r'\(([A-Z]\d{2}(?:\.\d)?-[A-Z]\d{2}(?:\.\d)?)\)')
15
+
16
+
17
+ def chapter_to_roman(chapter: int | str) -> str:
18
+ """Convert chapter number to Roman numeral. Accepts 1, '1', or '01'."""
19
+ n = int(chapter)
20
+ try:
21
+ return _INT_TO_ROMAN[n]
22
+ except KeyError:
23
+ raise ValueError(f"Chapter number {n!r} out of range 1–22") from None
24
+
25
+
26
+ def roman_to_chapter(roman: str) -> int:
27
+ """Convert Roman numeral chapter (e.g. 'XVIII') to integer 1–22. Case-insensitive."""
28
+ try:
29
+ return _ROMAN_TO_INT[roman.upper()]
30
+ except KeyError:
31
+ raise ValueError(f"Unknown Roman numeral {roman!r}") from None
32
+
33
+
34
+ def get_range(code: str) -> str:
35
+ """Return the code range string for a chapter.
36
+
37
+ Chapter '01' → 'A00-B99' (extracted from title)
38
+ """
39
+ import polars as pl
40
+ from codedx._core import icd10se_table
41
+
42
+ try:
43
+ row = icd10se_table.row(by_predicate=pl.col("Code") == code, named=True)
44
+ except Exception:
45
+ raise ValueError(f"Code {code!r} not found in ICD-10-SE") from None
46
+
47
+ if row["Level"] != "Chapter":
48
+ raise ValueError(f"Code {code!r} is level {row['Level']!r}, not Chapter")
49
+ m = _RANGE_RE.search(row["Titel"])
50
+ if m:
51
+ return m.group(1)
52
+ raise ValueError(f"No range found in chapter title: {row['Titel']!r}")
53
+
54
+
55
+ __all__ = ["chapter_to_roman", "roman_to_chapter", "get_range"]
@@ -52,19 +52,19 @@ def _build_retired(work_dir: pathlib.Path) -> pl.DataFrame:
52
52
  "Code": pl.Series([], dtype=pl.String),
53
53
  "Titel": pl.Series([], dtype=pl.String),
54
54
  })
55
- xlsx = work_dir / "andringar-icd-10-se.xlsx"
56
- if not xlsx.exists():
55
+ historik = work_dir / "historik-icd-10-se-1997-2026.xlsx"
56
+ if not historik.exists():
57
57
  return _empty
58
58
  try:
59
- df = pl.read_excel(xlsx, sheet_name="Inaktiverade koder")
59
+ df = pl.read_excel(historik)
60
60
  except Exception:
61
61
  return _empty
62
- if "Inaktiverad kod" not in df.columns:
62
+ if "Giltig till" not in df.columns:
63
63
  return _empty
64
64
  return (
65
- df.filter(pl.col("Inaktiverad kod").is_not_null())
65
+ df.filter(pl.col("Giltig till").is_not_null() & (pl.col("Giltig till") != ""))
66
66
  .select(
67
- pl.col("Inaktiverad kod").str.replace_all("\\.", "").alias("Code"),
67
+ pl.col("Kod").str.replace_all("\\.", "").alias("Code"),
68
68
  pl.col("Titel"),
69
69
  )
70
70
  .unique("Code")
@@ -5,13 +5,7 @@ import pathlib
5
5
  import polars as pl
6
6
 
7
7
  from codedx import _CACHE_DIR
8
-
9
- _ROMAN = {
10
- "I": 1, "II": 2, "III": 3, "IV": 4, "V": 5, "VI": 6, "VII": 7,
11
- "VIII": 8, "IX": 9, "X": 10, "XI": 11, "XII": 12, "XIII": 13,
12
- "XIV": 14, "XV": 15, "XVI": 16, "XVII": 17, "XVIII": 18,
13
- "XIX": 19, "XX": 20, "XXI": 21, "XXII": 22,
14
- }
8
+ from codedx.chapters import _ROMAN_TO_INT as _ROMAN
15
9
 
16
10
 
17
11
  def _build(work_dir: pathlib.Path) -> pl.DataFrame:
File without changes
File without changes
File without changes
File without changes