csa

Module contents

CSA headers are Siemens-specific private data elements that are embedded in some DICOM headers, specifically as the (0029, 1010)/”CSA Image Header Info” and (0029, 1020)/”CSA Series Header Info” tags. For more information see this NiBabel article.

Submodules

dicom_parser.utils.siemens.csa.ascii.element module

Definition of a single CSA header data element. These elements are parsed from the full string header using new line characters.

class dicom_parser.utils.siemens.csa.ascii.element.CsaAsciiElement(raw_element: str)

Bases: object

Represents a single CSA header data element as extracted from the raw header information.

CLEAN_PART_PATTERN = '[A-Z].*'
LIST_PART_PATTERN = '\\[\\d+\\]'
clean_part(part: str) str

Returns a the part’s name without any prefixed lowercase characters.

Parameters

part (str) – One of the listed key’s parts

Returns

Prefix-omitted part name

Return type

str

key_to_list(chained_key: str) list

The string data elements represents nested keys using a ‘.’ character. Returns a clean (prefix-omitted) list of the nested key structure.

Parameters

chained_key (str) – CSA header data element key

Returns

Clean and listed representation of the provided key

Return type

list

search_array_pattern(part: str) int

Checks if the pattern indicating a particular key part represents an array.

Parameters

part (str) – Some part of a listed key

Returns

The value’s index if array pattern found, else None

Return type

int

split() tuple

Splits the raw CSA element to a clean and listed key and a value.

Returns

key, value

Return type

tuple

dicom_parser.utils.siemens.csa.ascii.header module

Definition of the CsaAsciiHeader class which handles the parsing of CSA header values returned by pydicom as bytes.

class dicom_parser.utils.siemens.csa.ascii.header.CsaAsciiHeader(header: Union[str, bytes])

Bases: object

ASCCONV_ELEMENT_PATTERN = '([A-Z][^\\n]*)'

A pattern used to slice the entire header into single raw (string) values.

ASCCONV_HEADER_PATTERN = '### ASCCONV BEGIN(.*?)### ASCCONV END ###'

A pattern used to the extract the header information from the raw value.

ENCODING = 'ISO-8859-1'
create_elements(raw_elements: Optional[list] = None) list

Creates CsaAsciiElement instances that parse the key and the value.

Parameters

raw_elements (list) – Raw ASCCONV CSA header elements

Returns

CsaAsciiElement instances.

Return type

list

property elements: list

Caches the CsaAsciiElement instances representing the entire header information.

Returns

CsaAsciiElement instances

Return type

list

classmethod extract_ascii_header(raw_header: Union[str, bytes]) str

Returns the decoded and extracted header information from the full data element’s value.

Returns

Decoded clean header information

Return type

str

extract_elements() list

Splits the decoded header information into a list of raw (string) data elements, each containing a key-value pair. The first item is skipped because it is an unrequired heading.

Returns

CSA data elements in raw string format

Return type

list

property n_slices: int

Returns the number of slices (tiles) in a mosaic.

Returns

Number of slices encoded as a 2D mosaic

Return type

int

parse(elements: Optional[list] = None) dict

Parses a list of CsaAsciiElement instances (or all if left None) as a dictionary.

Parameters

elements (list, optional) – CsaAsciiElement instances, by default None

Returns

Header information as a dictionary

Return type

dict

property parsed: dict

Caches the parsed dictionary as a private attribute.

Returns

Header information as dictionary

Return type

dict

property raw_elements: list

Caches the raw (sting) CSA data elements as a private attribute.

Returns

Raw CSA header data elements

Return type

list

dicom_parser.utils.siemens.csa.ascii.parser module

Definition of parser that accepts CsaAsciiElement instances to be parsed and keeps a pointer to a dictionary that may aggregate the results.

class dicom_parser.utils.siemens.csa.ascii.parser.CsaAsciiParser(destination: Optional[dict] = None)

Bases: object

Parses CSA header data elements given as CsaAsciiElement instances into a public dictionary.

static assign_list_element(part: str, value, destination: dict)

Appends to an existing list value or creates a new list instance for it.

Parameters
  • part (str) – Last part’s name

  • value (Any) – The CsaAsciiElement’s value

  • destination (dict) – A pointer to the appropriate destination with the parsed dictionary

assign_listed_element(csa_data_element: dicom_parser.utils.siemens.csa.ascii.element.CsaAsciiElement, destination: dict)

Once the destination for the CsaAsciiElement’s value has been retrieved or created, this method assigns the value at that destination.

Parameters
  • csa_data_element (CsaAsciiElement) – The instance from which to assign the value

  • destination (dict) – The appropriate destination within the parsed values dictionary

static create_new_element_list(part_name: str, destination: dict) dict

If an array part (part containing the [<index>] pattern) exists as any part of the listed key except for the last, it indicates a list of dictionary instances. This method creates a new list with a single dict instances and returns a pointer to it.

Parameters
  • part_name (str) – The key for the list in the current dict level of the constructed scaffolding.

  • destination (dict) – The current level of scaffolding within the parsed dictionary

Returns

The next level of the key’s scaffolding within the parsed dictionary

Return type

dict

static fix_value(value)

Covert a CSA header element’s value to float or int if possible. Also cleans up redundant quotation marks and decodes hexadecimal values.

Parameters

value (Any) – Some CSA header element value

Returns

Fixed (converted) value

Return type

str, int, or float

parse(csa_data_element: dicom_parser.utils.siemens.csa.ascii.element.CsaAsciiElement)

Parses a raw CSA header element into the provided dictionary or creates a new one.

Parameters

csa_data_element (CsaAsciiElement) – CSA header element to be parsed

static scaffold_dict_part(part: str, destination: dict) dict

Returns the destination of a given key’s dict part within the parsed dictionary.

Parameters
  • part (str) – List part’s name

  • destination (dict) – The current level of scaffolding within the parsed dictionary

Returns

The next level of the key’s scaffolding within the parsed dictionary

Return type

dict

scaffold_list_part(part: str, index: int, destination: dict) dict

Returns the destination of a given key’s list part within the parsed dictionary.

Parameters
  • part (str) – List part’s name

  • index (int) – The index within the list for the next part in the nested key

  • destination (dict) – The current level of scaffolding within the parsed dictionary

Returns

The next level of the key’s scaffolding within the parsed dictionary

Return type

dict

scaffold_listed_key(csa_data_element: dicom_parser.utils.siemens.csa.ascii.element.CsaAsciiElement, destination: Optional[dict] = None) dict

Creates a scaffolding within the parsed values dictionary. This means that it runs over the data elements nested key structure (LevelA.ListLevelB[5].LevelC = ‘value’) and returns a pointer to the appropriate destination for the value within the parsed values dictionary.

Parameters
  • csa_data_element (CsaAsciiElement) – Instance to scaffold a destination for

  • destination (dict, optional) – An existing destination dictionary, by default None

Returns

A pointer to the appropriate destination for the CsaAsciiElement’s value

Return type

dict

scaffold_part(csa_data_element: dicom_parser.utils.siemens.csa.ascii.element.CsaAsciiElement, part: str, destination: dict) dict

Returns the destination of a given key’s part within the parsed dictionary.

Parameters
  • csa_data_element (CsaAsciiElement) – The source instance

  • part (str) – List part’s name

  • destination (dict) – The current level of scaffolding within the parsed dictionary

Returns

The next level of the key’s scaffolding within the parsed dictionary

Return type

dict

static update_existing_element_list(part_name: str, index: int, destination: dict) dict

If an array part (part containing the [<index>] pattern) exists as any part of the listed key except for the last, it indicates a list of dictionary instances. This method check whether a dictionary exists already at the given list’s index. If it does, returns that dict instance, otherwise, creates a new one and returns it.

Parameters
  • part_name (str) – The list’s key within the destination dictionary

  • index (int) – The index of the nested part

  • destination (dict) – The current level of scaffolding within the parsed dictionary

Returns

The next level of the key’s scaffolding within the parsed dictionary

Return type

dict