Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision b36b51d687c5562713267577a305d01fa87e3302 authored by Rick Gerkin on 30 March 2023, 18:35:06 UTC, committed by Rick Gerkin on 30 March 2023, 18:35:06 UTC
Import fix
1 parent c03b494
  • Files
  • Changes
  • d9d541d
  • /
  • lee_et_al_2023
  • /
  • src
  • /
  • cached_values.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:b36b51d687c5562713267577a305d01fa87e3302
directory badge
swh:1:dir:9b439c8800f1cd38f7e33461013419d71e619299
content badge
swh:1:cnt:f8a621cff58af149ecec75b01215a3152739d8bf

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
cached_values.py
import numpy as np
import pandas as pd


class CachedValues():
  """Class for mapping keys to pre-computed values.

  Keys are hashable objects in numpy arrays, for example, SMILES strings.
  """

  def __init__(self, keys: np.ndarray, values: np.ndarray):
    if len(set(keys)) != len(keys):
      raise ValueError('All keys should be distinct; duplicates found!')
    if len(keys) != len(values):
      raise ValueError('Keys and values should have the same length,'
                       ' found len(keys) of {} and len(values) of {}'.format(
                           len(keys), len(values)))
    self.keys = keys
    self.values = values
    self.cache = pd.Series(data=np.arange(len(keys)), index=keys)

  @classmethod
  def load_from_npz(cls,
                    filepath,
                    key_name='smiles',
                    value_name='prediction'):
    data = np.load(filepath, allow_pickle=True)
    return cls(data[key_name], data[value_name])

  def __call__(self, key_array: np.ndarray):
    if not isinstance(key_array, np.ndarray):
      raise ValueError(f'key_array={key_array} should be a numpy array!')
    indices = self.cache[key_array].values
    if np.any(np.isnan(indices)):
      raise ValueError(
          f'key_array={key_array[np.isnan(indices)]} not in cache index!')
    return self.values[indices]

  def save_to_npz(self, filepath, key_name='smiles', value_name='prediction'):
    np.savez_compressed(
        filepath, **{key_name: self.keys, value_name: self.values})
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API