API References

Comparing low vs. high dimensions/embeddings.

KNRscore.KNRscore.check_logger(verbose: [<class 'str'>, <class 'int'>] = 'info')

Check the logger by printing messages at different levels.

Parameters:

verbose ([str, int], default is 'info') – Verbosity level to test.

Examples

>>> # Test all logger levels
>>> KNRscore.check_logger('debug')
DEBUG
INFO
WARNING
CRITICAL
KNRscore.KNRscore.compare(mapX, mapY, nn=250, n_steps=5, verbose='info')

Comparison of two embeddings.

Description

Quantification of local similarity across two maps or embeddings, such as PCA and t-SNE. To compare the embedding of samples in two different maps using a scale dependent similarity measure. For a pair of maps X and Y, we compare the sets of the, respectively, kx and ky nearest neighbours of each sample.

param mapX:

Mapping of first embedding.

type mapX:

numpy array

param data2:

Mapping of second embedding.

type data2:

numpy array

param nn:

number of neirest neighbor to compare between the two maps. This can be set based on the smalles class size or the aveage class size. The default is 250.

type nn:

integer, optional

param n_steps:

The number of evaluation steps until reaching nn, optional. If higher, the resolution becomes lower and vice versa. The default is 5.

type n_steps:

integer

param verbose:

print messages. The default is 3.

type verbose:

integer, optional

returns:
  • scores : array with the scores across various nearest neighbors (nn).

  • nn : nearest neighbors

  • n_steps : The number of evaluation steps until reaching nn.

rtype:

dict()

Examples

>>> # Load data
>>> X, y = KNRscore.import_example()
>>>
>>> # Compute embeddings
>>> embed_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
>>> embed_tsne = manifold.TSNE(n_components=2, init='pca').fit_transform(X)
>>>
>>> # Compare PCA vs. tSNE
>>> scores = KNRscore.compare(embed_pca, embed_tsne, n_steps=10)
>>>
>>> # plot PCA vs. tSNE
>>> fig, ax = knrs.scatter(embed_tsne[:, 0], embed_tsne[:, 1], labels=y, cmap='Set1', title='tSNE Scatter Plot')
>>> fig, ax = knrs.scatter(embed_pca[:, 0], embed_pca[:, 1], labels=y, cmap='Set1', title='PCA Scatter Plot')
>>>

References

KNRscore.KNRscore.convert_verbose_to_new(verbose)

Convert old verbosity to the new.

Parameters:

verbose (int or str) – Verbosity level to convert.

Returns:

New verbosity level.

Return type:

str

Examples

>>> # Convert numeric verbosity to string
>>> verbose = KNRscore.convert_verbose_to_new(3)
>>> print(verbose)  # 'info'
KNRscore.KNRscore.convert_verbose_to_old(verbose)

Convert new verbosity to the old ones.

Parameters:

verbose (int or str) – Verbosity level to convert.

Returns:

Old verbosity level.

Return type:

int

Examples

>>> # Convert string verbosity to numeric
>>> verbose = KNRscore.convert_verbose_to_old('info')
>>> print(verbose)  # 3
KNRscore.KNRscore.disable_tqdm()

Disable tqdm progress bars based on logger level.

Returns:

True if tqdm should be disabled, False otherwise.

Return type:

bool

Examples

>>> # Check if tqdm should be disabled
>>> disable = KNRscore.disable_tqdm()
>>> print(disable)  # True if logger level is warning or higher
KNRscore.KNRscore.get_logger()

Get the current logger level.

Returns:

Current logger level.

Return type:

int

Examples

>>> # Get current logger level
>>> level = KNRscore.get_logger()
>>> print(level)  # 20 (for info level)
KNRscore.KNRscore.import_example(data='digits', url=None, sep=',')

Import example dataset from github source.

Import one of the few datasets from github source or specify your own download url link.

Parameters:
  • data (str) – Name of datasets: ‘digits’

  • url (str) – url link to to dataset.

  • sep (str, optional) – Separator for CSV files. The default is ‘,’.

Returns:

(X, y) where X is the feature matrix and y is the target vector.

Return type:

tuple

Examples

>>> # Load the digits dataset
>>> X, y = KNRscore.import_example(data='digits')
>>> print(X.shape)  # (1797, 64)
>>> print(y.shape)  # (1797,)
>>>
>>> # Load custom dataset from URL
>>> url = 'https://example.com/data.csv'
>>> X, y = KNRscore.import_example(url=url)
KNRscore.KNRscore.plot(out, cmap='jet', xlabel=None, ylabel=None, reverse_cmap=False)

Make plot.

Parameters:
  • out (dict) – output of the compare() function.

  • cmap (string, optional) – colormap. The default is ‘jet’.

  • xlabel (str, optional) – Label for x-axis. The default is None.

  • ylabel (str, optional) – Label for y-axis. The default is None.

  • reverse_cmap (bool, optional) – Reverse the colormap. The default is False.

Returns:

Figure and axes objects.

Return type:

fig, ax

Examples

>>> # Load data
>>> X, y = KNRscore.import_example()
>>>
>>> # Compute embeddings
>>> embed_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
>>> embed_tsne = manifold.TSNE(n_components=2, init='pca').fit_transform(X)
>>>
>>> # Create comparison scores
>>> scores = KNRscore.compare(embed_pca, embed_tsne)
>>>
>>> # Create plot with custom labels
>>> fig, ax = KNRscore.plot(scores, cmap='viridis', xlabel='PCA', ylabel='tSNE')
>>>
>>> # Create plot with reversed colormap
>>> fig, ax = KNRscore.plot(scores, cmap='viridis', reverse_cmap=True)
>>>
KNRscore.KNRscore.scatter(Xcoord, Ycoord, **args)

Scatterplot.

Parameters:
  • Xcoord (numpy array) – 1D Coordinates.

  • Ycoord (numpy array) – 1D Coordinates.

  • **args (TYPE) – See scatterd for all possible arguments.

Returns:

Figure and axes objects.

Return type:

fig, ax

Examples

>>> # Load data
>>> X, y = KNRscore.import_example()
>>>
>>> # Compute embeddings
>>> embed_pca = decomposition.TruncatedSVD(n_components=50).fit_transform(X)
>>> embed_tsne = manifold.TSNE(n_components=2, init='pca').fit_transform(X)
>>>
>>> # Create comparison scores
>>> scores = KNRscore.compare(embed_pca, embed_tsne)
>>>
>>> # Create scatter plot with labels
>>> fig, ax = KNRscore.scatter(embed_tsne[:, 0], embed_tsne[:, 1],
...                           labels=y,
...                           cmap='Set1',
...                           title='Scatter Plot')
>>>
>>> # Create scatter plot with custom markers
>>> fig, ax = KNRscore.scatter(embed_tsne[:, 0], embed_tsne[:, 1],
...                           marker='o',
...                           s=50,
...                           alpha=0.5)
KNRscore.KNRscore.set_logger(verbose: [<class 'str'>, <class 'int'>] = 'info')

Set the logger for verbosity messages.

Parameters:

verbose ([str, int], default is 'info' or 20) – Set the verbose messages using string or integer values. * [0, 60, None, ‘silent’, ‘off’, ‘no’]: No message. * [10, ‘debug’]: Messages from debug level and higher. * [20, ‘info’]: Messages from info level and higher. * [30, ‘warning’]: Messages from warning level and higher. * [50, ‘critical’]: Messages from critical level and higher.

Examples

>>> # Set logger to warning level
>>> KNRscore.set_logger('warning')
>>>
>>> # Set logger to debug level
>>> KNRscore.set_logger(10)
KNRscore.KNRscore.wget(url, writepath)

Download files from a URL.

Parameters:
  • url (str) – URL to download from.

  • writepath (str) – Path to save the downloaded file.

Examples

>>> # Download a file
>>> url = 'https://example.com/data.csv'
>>> writepath = 'data.csv'
>>> KNRscore.wget(url, writepath)