Files – irtlib.files#

The files of a response matrix, of ability estimates and of a calibration (ADR-0018).

The response matrix and the ability estimates go to CSV files in the item bank’s conventions (ItemBank.to_csv), gzip-compressed when the path ends in .gz; a missing response is written NA, and a number that is not finite Inf, -Inf or NaN, as R writes them. A calibration goes to a NumPy .npz file, read without unpickling. The names of a file’s items and examinees are returned beside what is read, and given to the function that writes.

irtlib.files.read_abilities(path: str | PathLike[str]) → tuple[AbilityEstimates, tuple[str, ...] | None][source]#

Read ability estimates, with their examinees’ names, from a CSV file that write_abilities() writes.

The header names the columns, in any order: theta, standard_errors, evaluations, from_fallback and warnings, and an optional name column headed name or left empty. Each following row is one examinee. A number may be Inf, -Inf, NaN or NA; from_fallback is True or False, in any of R’s and Python’s spellings; warnings holds one warning per line, its category, a colon and its message, a line of the message after its first indented by two spaces. A warning of a category irtlib cannot name is read as a Warning. A path ending in .gz is read as a gzip-compressed file.

Parameters:
pathstr or os.PathLike

The file to read.

Returns:
tuple[irtlib.ability.AbilityEstimates, tuple of str or None]

The estimates, and the examinees’ names, None without a name column.

Raises:
ValueError

If the file is empty; its header lacks a column, names one twice or names an unknown one; or a row does not hold one value per column, or holds a value of the wrong kind.

irtlib.files.read_calibration(path: str | PathLike[str]) → Calibration[source]#

Read a calibration from a file that write_calibration() writes, as the class that wrote it.

The file is read without unpickling, so it cannot run code. Every object is built again through its constructor, which checks it as it does any other.

Parameters:
pathstr or os.PathLike

The file to read.

Returns:
irtlib.calibration.Calibration

The calibration, an MCMCCalibration when one was written.

Raises:
ValueError

If the file is not a calibration file, truncated ones included, holds an object array, is of a format number this version of irtlib does not read, or lacks an attribute of its class.

irtlib.files.read_responses(path: str | PathLike[str]) → tuple[NDArray[floating[Any]], tuple[str, ...], tuple[str, ...] | None][source]#

Read a response matrix, with its items’ and examinees’ names, from a CSV file.

The header names the items, one column each. A first column headed name or left empty, as R’s write.csv heads row names, holds the examinees’ names, so the first item cannot be named name. Each following row is one examinee, whose responses are 0, 1, or, for a missing one, an empty field, NA or NaN. Only blank lines at the end of the file are skipped, and a path ending in .gz is read as a gzip-compressed file.

Parameters:
pathstr or os.PathLike

The file to read.

Returns:
tuple[numpy.ndarray, tuple of str, tuple of str or None]

The response matrix, one row per examinee and NaN for a missing response; the items’ names; and the examinees’ names, None without a name column.

Raises:
ValueError

If the file is empty; its item names are not unique and non-empty; a row does not hold one value per column; or a response is not one of the values above.

irtlib.files.write_abilities(path: str | PathLike[str], abilities: AbilityEstimates, names: tuple[str, ...] | list[str] | None = None) → None[source]#

Write ability estimates to a CSV file that read_abilities() reads back.

The header is theta,standard_errors,evaluations,from_fallback,warnings, after a name column when the examinees have names. Each number is written with the fewest digits that read back as the same float, and as Inf, -Inf or NaN when it is not finite. Each warning is written on its own line of the warnings field, as its category, a colon and its message, a line of the message after its first indented by two spaces. A path ending in .gz is written gzip-compressed, and an existing file is overwritten.

Parameters:
pathstr or os.PathLike

The file to write.

abilitiesirtlib.ability.AbilityEstimates

The estimates.

namessequence of str, optional

One name per examinee. Default is None, which writes no name column.

Raises:
ValueError

If names does not hold one name per examinee.

irtlib.files.write_calibration(path: str | PathLike[str], calibration: Calibration) → None[source]#

Write a calibration, every attribute of it, to a NumPy .npz file that read_calibration() reads.

Each array is stored under its attribute’s name. The bank is stored as its item matrix and names, the grid as its nodes and weights, and a joint calibration’s abilities as their arrays, with each examinee’s warnings as text. The file also records the calibration’s class, irtlib’s version and the file’s format number. The file is written at path as given, without a suffix added, and an existing file is overwritten.

Parameters:
pathstr or os.PathLike

The file to write.

calibrationirtlib.calibration.Calibration

The calibration, by any method; a Markov chain Monte Carlo calibration keeps its draws.

Raises:
TypeError

If calibration is neither a Calibration nor a MCMCCalibration.

irtlib.files.write_responses(path: str | PathLike[str], responses: ArrayLike, item_names: tuple[str, ...] | list[str] | None = None, examinee_names: tuple[str, ...] | list[str] | None = None) → None[source]#

Write a response matrix to a CSV file that read_responses() reads back.

The header names the items, after a name column when the examinees have names. A response is written as 0 or 1, and a missing one as NA. A path ending in .gz is written gzip-compressed, and an existing file is overwritten.

Parameters:
pathstr or os.PathLike

The file to write.

responsesarray_like

The response matrix: one row per examinee, one column per item, holding 1, 0 or NaN.

item_namessequence of str, optional

One name per item, unique and non-empty, the first not name when the examinees have no names. Default is None, which names each item item_ followed by its index, as irtlib.ItemBank does.

examinee_namessequence of str, optional

One name per examinee. Default is None, which writes no name column.

Raises:
TypeError

If the responses are neither Booleans, integers nor floats, or an item name is not a string.

ValueError

If the responses are not a matrix of 1, 0 and NaN; the item names are not one unique, non-empty name per item, or the first is name without examinee names; or the examinee names are not one per examinee.