pyamr.core package

Subpackages

Submodules

pyamr.core.acsi module

Classes:

ACSI([column_specimen, ...])

Antimicrobial Collateral Sensitivity Index.

Functions:

CRI(x[, func_mis])

Collateral resistance index

collateral_resistance_index(m)

Collateral Resistance Index

create_combinations_v1(d, groupby[, ...])

Creates the DataFrame with all combinations.

mutual_info_matrix_v3([x, y, ct])

Compute the component information score.

class pyamr.core.acsi.ACSI(column_specimen='SPECIMEN', column_microorganism='MICROORGANISM', column_antimicrobial='ANTIMICROBIAL', column_sensitivity='SENSITIVITY', column_laboratory='LAB_NUMBER')[source]

Bases: object

Antimicrobial Collateral Sensitivity Index.

Other possible names for this index…

  1. Antimicrobial Disjoint Resistance Index

  2. Antimicrobial Collateral Resistance Index

  3. Antimicrobial Collateral Sensitivity Index

  4. Antimicrobial Collateral Resistance Index

Attributes:

c_abx

c_dat

c_lab

c_org

c_sns

c_spe

Methods:

combinations(dataframe, **kwargs)

Creates the combinations.

compute(dataframe[, flag_combinations, ...])

Computes the Antimicrobial Collateral Sensitivity.

compute_from_combinations()

compute_from_contingency()

c_abx = 'ANTIMICROBIAL'
c_dat = 'DATE'
c_lab = 'LAB_NUMBER'
c_org = 'MICROORGANISM'
c_sns = 'SENSITIVITY'
c_spe = 'SPECIMEN'
combinations(dataframe, **kwargs)[source]

Creates the combinations.

Note

In theory the combinations only need to be grouped by laboratory number, however, in order to maintain the rest of the information (like date) we need to pass all of them.

compute(dataframe, flag_combinations=False, flag_contingency=False, groupby=None, func_mis=None, return_combinations=False)[source]

Computes the Antimicrobial Collateral Sensitivity.

Note

Enable to pass a combinations dataframe.

Note

The lab number should be used only to compute the combinations. It should not be used when computing the ACSI.

Parameters:
  • dataframe (pd.DataFrame) – A dataframe with the susceptibility test interpretations as columns.

  • combinations (boolean) – Indicates whether the variable DataFrame contains susceptibility test records (combinations=False) or the antimicrobial combinations and the class (combination=True)

  • func_mis (function) – The function to use to compute the contingency matrix from the mutual information score. By default it uses the function mutual_info_matrix_v3.

Return type:

pd.Series or pd.DataFrame

compute_from_combinations()[source]
compute_from_contingency()[source]
pyamr.core.acsi.CRI(x, func_mis=<function mutual_info_matrix_v3>)[source]

Collateral resistance index

Parameters:
  • x (pd.Series) – Contains the combinations classes (e.g. SS, SR, RS, RR)

  • func_mis (function) – The function to use to compute the contingency matrix from the mutual information score. By default it uses the function mutual_info_matrix_v3.

pyamr.core.acsi.collateral_resistance_index(m)[source]

Collateral Resistance Index

The collateral resistance index is based on the mutual information matrix. This implementation assumes there are only two classes resistant (R) and sensitive (S).

Warning

Only works for a 2x2 contingency matrix

Parameters:

m (np.array) – A numpy array with the mutual information matrix. Also called the contingency matrix.

pyamr.core.acsi.create_combinations_v1(d, groupby, col_spe='SPECIMEN', col_lab='LAB_NUMBER', col_org='MICROORGANISM', col_abx='ANTIMICROBIAL', col_sns='SENSITIVITY')[source]

Creates the DataFrame with all combinations.

Note

There might be an issue if there are two different outcomes for the same record. For example, a susceptibility test record for penicillin (APEN) with R and another one with S. Warn of this issue if it appears!

Note

If the data is right and the laboratory numbers are unique per isolate then the date is not necessary. However, what if we want to keep it? Groupby should at least contain: specimen, microorganism and lab_id

Note

How to add all data in addition to the columns manually.

pyamr.core.acsi.mutual_info_matrix_v3(x=None, y=None, ct=None)[source]

Compute the component information score.

Parameters:
  • x (list) – List with the classes

  • y (list) – List with the classes

pyamr.core.asai module

Classes:

ASAI([column_genus, column_specie, ...])

Antimicrobial Spectrum of Activity Index.

ASAI_old([weights, threshold, column_genus, ...])

This class computes the antimicrobial spectrum of activity.

Functions:

antimicrobial_spectrum_activity_index(dataframe)

Computes the Antimicrobial Spectrum of Activity.

asai(*args, **kwargs)

Redirects to antimicrobial_spectrum_activity_index.

class pyamr.core.asai.ASAI(column_genus='GENUS', column_specie='SPECIE', column_resistance='RESISTANCE', column_threshold='THRESHOLD', column_frequency='FREQUENCY', column_wgenus='W_GENUS', column_wspecie='W_SPECIE')[source]

Bases: object

Antimicrobial Spectrum of Activity Index.

Attributes:

c_fre

c_gen

c_res

c_spe

c_thr

c_wgen

c_wspe

Methods:

compute(dataframe[, groupby, min_freq])

Computes the ASAI index (safely).

c_fre = 'FREQUENCY'
c_gen = 'GENUS'
c_res = 'RESISTANCE'
c_spe = 'SPECIE'
c_thr = 'THRESHOLD'
c_wgen = 'W_GENUS'
c_wspe = 'W_SPECIE'
compute(dataframe, groupby=None, min_freq=None, **kwargs)[source]

Computes the ASAI index (safely).

Parameters:
  • dataframe (pd.DataFrame) – The pandas dataframe with the information. The following columns are always required [RESISTANCE, GENUS and SPECIE]. In addition, [W_GENUS and W_SPECIE] are required if weights is None. Also, if weights = ‘frequency’ the column FREQUENCY must be present.

  • groupby (list, default=None) – The elements to groupby (pd.groupby)

  • min_freq (int, default=None) – The minimum number of susceptibility tests required in order to include the species to compute ASAI. Note that to work the dataframe must include a column indicating the frequencies.

  • weights (string, default=None) –

    The method to compute the weights. The methods supported are:

    • None: weights must be specified in [W_GENUS and W_SPECIE]

    • ‘uniform’: uniform weights for genus and species within genus.

    • ‘frequency: weights are proportional to the frequencies.

    The following rules must be fulfilled by the weight columns:

    • consistent weight for a given genus

    • all genus weights must add up to one.

    • all specie weights within a genus must add up to one.

  • threshold (float, default=None) – The threshold resistance value above which the antimicrobial is considered non-effective to treat the microorganism. For instance, for a resistance threshold of 0.5, if a pair <o,a> has a resistance value of 0.4, the microorganism will be considered sensitive. In order to use specific thresholds keep threshold to None and include a column ‘THRESHOLD’.ss

  • tol (float, default=1e-6) – The tolerance in order to check that all conditions (uniqueness and sums) are satisfied. Note that that float precision varies and therefore not always adds up to exactly one.

  • verbose (int, default=0) – The level of verbosity.

Returns:

The dataframe with the ASAI information and counts.

Return type:

pd.DataFrame

class pyamr.core.asai.ASAI_old(weights='uniform', threshold=0.5, column_genus='GENUS', column_specie='SPECIE', column_antibiotic='ANTIBIOTIC', column_resistance='RESISTANCE', column_threshold='THRESHOLD', column_frequency='FREQUENCY', column_wgenus='W_GENUS', column_wspecie='W_SPECIE')[source]

Bases: object

This class computes the antimicrobial spectrum of activity.

Deprecated since version 0.0.1.

Attributes:

c_abx

c_fre

c_gen

c_res

c_spe

c_thr

c_wgen

c_wspe

Methods:

compute(dataframe, by_category)

This function computes the asai index by category.

c_abx = 'ANTIBIOTIC'
c_fre = 'FREQUENCY'
c_gen = 'GENUS'
c_res = 'RESISTANCE'
c_spe = 'SPECIE'
c_thr = 'THRESHOLD'
c_wgen = 'W_GENUS'
c_wspe = 'W_SPECIE'
compute(dataframe, by_category)[source]

This function computes the asai index by category.

Parameters:
  • dataframe (pandas DataFrame) – The pandas DataFrame containing the data. In particular it needs to contain the following columns: genus, specie, antibiotic and the resistance outcome within the range [0,1].

  • by_category (string) – The name of the column that will be used to group ASAI.

Returns:

the dataframe…

Return type:

pandas dataframe

pyamr.core.asai.antimicrobial_spectrum_activity_index(dataframe, weights='uniform', threshold=0.5, tol=1e-06, verbose=0)[source]

Computes the Antimicrobial Spectrum of Activity.

Note

Since threshold and weights have a default value, the warnings below will not be displayed. However, the code is there in case the behaviour needs to be changed in the future.

Note

Another way to check that the weights are correct is just by computing ASAI with th=0 and th=1. These should result in asai=1 and asai=0 respectively. | # Compute score | score_1 = np.sum(wgn * wsp * (sari <= 0)) | score_2 = np.sum(wgn * wsp * (sari <= 1))

Warning

Should the duplicated check only for the columns GENUS and SPECIE? What if we do not group by antibiotic? It has to be unique for the antibiotic also. It is up to the user to make the right use of this?

Parameters:
  • dataframe (pd.DataFrame) – The pandas dataframe with the information. The following columns are always required [RESISTANCE, GENUS and SPECIE]. In addition, [W_GENUS and W_SPECIE] are required if weights is None. Also, if weights = ‘frequency’ the column FREQUENCY must be present.

  • weights (string, default=’uniform’) –

    The method to compute the weights. The methods supported are:

    • ‘specified’: weights must be in [W_GENUS and W_SPECIE]

    • ‘uniform’: uniform weights for genus and species within genus.

    • ‘frequency’: weights are proportional to the frequencies.

    The following rules must be fulfilled by the weight columns:

    • consistent weight for a given genus

    • all genus weights must add up to one.

    • all specie weights within a genus must add up to one.

  • threshold (float, default=0.5) – The threshold resistance value above which the antimicrobial is considered non-effective to treat the microorganism. For instance, for a resistance threshold of 0.5, if a pair <o,a> has a resistance value of 0.4, the microorganism will be considered sensitive. In order to use specific thresholds keep threshold to None and include a column ‘THRESHOLD’.ss

  • tol (float, default=1e-6) – The tolerance in order to check that all conditions (uniqueness and sums) are satisfied. Note that that float precision varies and therefore not always adds up to exactly one.

  • verbose (int, default=0) – The level of verbosity.

Returns:

The dataframe with the ASAI information and counts.

Return type:

pd.DataFrame

pyamr.core.asai.asai(*args, **kwargs)[source]

Redirects to antimicrobial_spectrum_activity_index.

pyamr.core.dri module

Classes:

DRI([column_resistance, column_usage])

Drug Resistance Index.

Functions:

dri(*args, **kwargs)

Redirects to drug_resistance_index.

drug_resistance_index(dataframe[, ...])

Computes the Drug Resistance Index.

drug_resistance_index_v2(smmry[, cu, cr, ...])

Computes the Drug Resistance Index

class pyamr.core.dri.DRI(column_resistance='RESISTANCE', column_usage='USE')[source]

Bases: object

Drug Resistance Index.

Attributes:

c_res

c_use

Methods:

compute(dataframe[, groupby, column_usage])

Computes the Drug Resistance Index.

c_res = 'RESISTANCE'
c_use = 'USE'
compute(dataframe, groupby=None, column_usage=None, **kwargs)[source]

Computes the Drug Resistance Index.

Note

Needs to include checks.

Parameters:
  • dataframe (pd.DataFrame) – The pandas dataframe with the information. The following columns are always required [RESISTANCE and USE]. The RESISTANCE column indicates the proportion of resistance isolates and the USE the amount of antimicrobial doses applied.

  • groupby (list, default=None) – The elements to groupby (pd.groupby)

  • column_usage (str) – The column with the usage values. This value overwrites the column_usage value passed during the instance creation. If the value is None, the default value passed during the instance creation will be used.

  • **kwargs

pyamr.core.dri.dri(*args, **kwargs)[source]

Redirects to drug_resistance_index.

pyamr.core.dri.drug_resistance_index(dataframe, return_complete=False, return_usage=False)[source]

Computes the Drug Resistance Index.

Parameters:
  • dataframe (pd.DataFrame)

  • return_complete (bool, default=False) – Returns the whole set of results.

  • return_usage (bool, default=False) – Returns only ‘use_period’ and ‘dri’.

pyamr.core.dri.drug_resistance_index_v2(smmry, cu='use', cr='sari', return_all=False, reference_time=None, **kwargs)[source]

Computes the Drug Resistance Index

An possible summary table would look like this…

DATE MICROORGANISM ANTIMICROBIAL sari use 2011 Q2 E. Coli Aminopenicillins 0.422 300 2011 Q2 E. Coli Quinolones 0.130 250 2011 Q2 E. Coli Cephalosporins 0.010 100 2011 Q3 E. Coli Aminopenicillins 0.437 250 2011 Q3 E. Coli Quinolones 0.132 300 2011 Q3 E. Coli Cephalosporins 0.014 1500

Parameters:
  • smmry (pd.DataFrame) – The summary DataFrame with the data required to compute the drug resistance index. The following information needs to be present in the DataFrame:

    1. the date (e.g. DATE)

    2. the resistance (e.g. sari)

    3. the drug use (e.g. use)

  • cu (str) – Column name with use

  • cr (str) – Column name with resistance

  • ct (str) – Column name with time

  • **kwargs – Arguments to pass to groupby

pyamr.core.freq module

Classes:

Frequency([column_antibiotic, ...])

class pyamr.core.freq.Frequency(column_antibiotic='ANTIBIOTIC', column_organism='SPECIE', column_date='DATE', column_outcome='SENSITIVITY', column_labnumber=None, dfmt='%Y-%M-%d')[source]

Bases: object

Attributes:

c_abx

c_dat

c_org

c_out

Methods:

compute(dataframe[, strategy, by_category, ...])

This function computes the frequencies.

fit()

c_abx = 'ANTIBIOTIC'
c_dat = 'DATE'
c_org = 'SPECIE'
c_out = 'SENSITIVITY'
compute(dataframe, strategy='overall', by_category='pairs', fs=None, wshift=None, wsize=None)[source]

This function computes the frequencies.

The method allows to compute the overall frequencies, the frequencies for independent time intervals (ITI) such as monthly or yearly and the frequencies for overlapping time intervals (OTI) in which the parameters wshift and wsize need to be specified.

Parameters:
  • dataframe (dataframe-like) – The microbiology dataframe with the following columns.

  • by_category (string) – The category to group the outcomes. The outcomes are grouped in pairs formed by (organism, antibiotic) by default. However, these can be also grouped by organisms or antibiotics.

  • fs (string) – The frequency sample (e.g. 1D, 1M, 7D, …)

  • wshift (integer) – The shift between consecutive windows (OTI).

  • wsize (integer) – The size of the window (OTI)

Return type:

dataframe

fit()[source]

pyamr.core.mari module

Classes:

MARI([groupby])

Multiple Antimicrobial Resistance Index

class pyamr.core.mari.MARI(groupby=['SPECIMEN', 'MICROORGANISM', 'LAB_NUMBER', 'SENSITIVITY'])[source]

Bases: object

Multiple Antimicrobial Resistance Index

Attributes:

c_dat

c_lab

c_org

c_out

c_spe

Methods:

compute(dataframe, **kwargs)

Compute the Multiple Antimicrobial Resistance Index.

compute_v1(dataframe[, shift, period, ...])

Compute MARI v1

compute_v2(dataframe[, shift, period, ...])

Compute MARI v2.

compute_v3(dataframe[, shift, period, ...])

Compute MARI v3.

compute_v4(dataframe[, shift, period, ...])

Compute MARI v4.

grouping(dataframe, period, cdate)

Compute metric with independent groups.

rolling(dataframe, period, cdate[, shift])

Compute metric using rolling approach

c_dat = 'DATE'
c_lab = 'LAB_NUMBER'
c_org = 'MICROORGANISM'
c_out = 'SENSITIVITY'
c_spe = 'SPECIMEN'
compute(dataframe, **kwargs)[source]

Compute the Multiple Antimicrobial Resistance Index.

Parameters:
  • dataframe (pd.DataFrame) – A DataFrame with the susceptibility test interpretations as columns. The default strategies used (see below) expect the following columns [‘sensitive’, ‘intermediate’, ‘resistant’] and if they do not appear they weill be set to zeros.

  • shift (str) – Frequency value to pass to pd.Grouper.

  • period (str, int) – Window value to pass to pd.rolling.

  • cdate (string, default=None) – The column that will be used as date.

  • return_frequencies (boolean, default=True) – Whether to return the frequencies (isolates) or just the resistance index.

  • return_isolates (boolean, default=True) – Whether to return the resistance index for each individual isolate.

  • strategy (string or func, default=’hard’) – The method used to compute sari. The possible options are ‘soft’, ‘medium’ and ‘hard’. In addition, a function with the following signature func(dataframe, **kwargs) can be passed.

    1. soft as R / R+I+S

    2. medium as R / R+S

    3. hard as R+I / R+I+S

    4. other as R+0.5I / R+0.5I+S [Not yet]

  • **kwargs (arguments to pass the strategy function.)

Returns:

  • dataframe (pd.Series or pd.DataFrame) – The resistance index (pd.Series) or a pd.Dataframe with the resistance index (sari), the sums and the frequencies.

  • isolates (pd.DataFrame) – The resistance index and each of the sensitivity value counts for each individual isolate.

compute_v1(dataframe, shift=None, period=None, cdate=None, return_frequencies=True, return_isolates=True, **kwargs)[source]

Compute MARI v1

compute_v2(dataframe, shift=None, period=None, cdate=None, return_frequencies=True, return_isolates=True, **kwargs)[source]

Compute MARI v2.

compute_v3(dataframe, shift=None, period=None, cdate=None, return_frequencies=True, return_isolates=True, **kwargs)[source]

Compute MARI v3.

compute_v4(dataframe, shift=None, period=None, cdate=None, return_frequencies=True, return_isolates=True, **kwargs)[source]

Compute MARI v4.

grouping(dataframe, period, cdate)[source]

Compute metric with independent groups.

rolling(dataframe, period, cdate, shift=None)[source]

Compute metric using rolling approach

pyamr.core.sari module

Classes:

SARI([groupby])

Single Antimicrobial Resistance Index.

Functions:

sari([dataframe, strategy])

Computes the Single Antimicrobial Resistance Index.

class pyamr.core.sari.SARI(groupby=['SPECIMEN', 'MICROORGANISM', 'ANTIMICROBIAL', 'SENSITIVITY'])[source]

Bases: object

Single Antimicrobial Resistance Index.

Attributes:

c_abx

c_dat

c_org

c_out

c_spe

Methods:

compute(dataframe[, period, shift, cdate, ...])

Computes Single Antimicrobial Resistance Index.

grouping(dataframe, period, cdate)

Computes metric using independent groups.

rolling(dataframe, period, cdate[, shift])

Computed metric using a rolling approach.

c_abx = 'ANTIMICROBIAL'
c_dat = 'DATE'
c_org = 'MICROORGANISM'
c_out = 'SENSITIVITY'
c_spe = 'SPECIMEN'
compute(dataframe, period=None, shift=None, cdate=None, return_frequencies=True, **kwargs)[source]

Computes Single Antimicrobial Resistance Index.

Examples

shift

period

description

None

None

Uses all data (agnostic to time)

None

year

Value every year using whole year’s data.

None

2D

Value every 2 days using 2 days data.

2D

2D

Value every 2 days using 2 days data.

None

2

Invalid

2D

2

Invalid Value every 2 days using 2x2D=4 days data.

2D

None

Invalid - period cannot be None (in this case)

2D

year

Invalid - period cannot be a year (in this case)

year

Invalid - shift cannot be a named time.

2

Invalid - shift cannot be a number.

Note

Using shift=2D and period=2D is equivalent to shift=2D and period=1.

Parameters:
  • dataframe (pd.DataFrame) – A dataframe with the susceptibility test interpretations as columns. The default strategies used (see below) expect the following columns [‘sensitive’, ‘intermediate’, ‘resistant’] and if they do not appear they weill be set to zeros.

  • shift (str) – Frequency (datetime) value to group by when applying a rolling window.

  • period (str, int) – If used alone (shift=None) is the value used to create groups (e.g. year). The whole data within the groups will be used to compute the metrics. On the contrary, when used in combinations with shift, it indicates the interval used to compute the metrics (e.g. 2D, 2 times the shift value)

  • cdate (string, default=None) – The column that will be used as date.

  • return_frequencies (boolean, default=True) – Whether to return the frequencies or just the resistance index.

  • strategy (string or func, default=’hard’) – The method used to compute sari. The possible options are ‘soft’, ‘medium’ and ‘hard’. In addition, a function with the following signature func(DataFrame, **kwargs) can be passed.

    1. basic as R / R+S

    2. soft as R / R+I+S

    3. medium as R+0.5I / R+0.5I+S

    4. hard as R+I / R+I+S

  • **kwargs (arguments to pass the strategy function.)

Returns:

The resistance index (pd.Series) or a pd.DataFrame with the resistance index (sari) and the frequencies.

Return type:

pd.Series or pd.DataFrame

grouping(dataframe, period, cdate)[source]

Computes metric using independent groups.

rolling(dataframe, period, cdate, shift=None)[source]

Computed metric using a rolling approach.

pyamr.core.sari.sari(dataframe=None, strategy='hard', **kwargs)[source]

Computes the Single Antimicrobial Resistance Index.

Parameters:
  • dataframe (pd.DataFrame) – A dataframe with the susceptibility test interpretations as columns. The default strategies used (see below) expect the following columns [‘sensitive’, ‘intermediate’, ‘resistant’] and if they do not appear they weill be set to zeros.

  • strategy (string or func, default=’hard’) – The method used to compute sari. The possible options are ‘soft’, ‘medium’ and ‘hard’. In addition, a function with the following signature func(dataframe, **kwargs) can be passed.

    1. basic as R / R+S

    2. soft as R / R+I+S

    3. medium as R+0.5I / R+0.5I+S

    4. hard as R+I / R+I+S

  • **kwargs (arguments to pass the strategy function.)

Returns:

The resistance index

Return type:

pd.Series

pyamr.core.sart module

Classes:

SART([column_specimen, ...])

Single Antimicrobial Resistance Trend.

class pyamr.core.sart.SART(column_specimen='SPECIMEN', column_microorganism='MICROORGANISM', column_antimicrobial='ANTIMICROBIAL', column_date='DATE', column_outcome='SENSITIVITY', column_resistance='RESISTANCE')[source]

Bases: object

Single Antimicrobial Resistance Trend.

Attributes:

c_abx

c_dat

c_org

c_out

c_res

c_spe

Methods:

compute(dataframe[, period, shift, cdate, ...])

Computes Single Antimicrobial Resistance Index.

c_abx = 'ANTIMICROBIAL'
c_dat = 'DATE'
c_org = 'MICROORGANISM'
c_out = 'SENSITIVITY'
c_res = 'RESISTANCE'
c_spe = 'SPECIMEN'
compute(dataframe, period='180D', shift='30D', cdate=None, return_objects=True, **kwargs)[source]

Computes Single Antimicrobial Resistance Index.

Todo

  • Add parameters to rolling!

  • Place value at the left, center, right of window?

  • Ensure that works when time gaps present!

  • Carefull with various indexes!

Parameters:
  • dataframe (pd.DataFrame) – It might receive two different types of DataFrames. The first option is a DataFrame with the raw susceptibility test records where the interpretation (‘sensitive’, ‘intermediate’, ‘resistant’) that will be used to compute the sari. The second option is a DataFrame with the sari values already computed.

  • shift (str) – Frequency value to pass to pd.Grouper.

  • period (str, int) – Window value to pass to pd.rolling.

  • cdate (string, default=None) – The column that will be used as date.

  • return_frequencies (boolean, default=True) – Whether to return the frequencies or just the resistance index.

  • strategy (string or func, default=’hard’) – The method used to compute sari. The possible options are ‘soft’, ‘medium’ and ‘hard’. In addition, a function with the following signature func(DataFrame, **kwargs) can be passed.

    1. soft as R / R+I+S

    2. medium as R / R+S

    3. hard as R+I / R+I+S

    4. other as R+0.5I / R+0.5I+S [Not yet]

  • **kwargs (arguments to pass the strategy function.)

Returns:

The resistance index (pd.Series) or a pd.DataFrame with the resistance index (sari) and the frequencies.

Return type:

pd.Series or pd.DataFrame

Module contents