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

https://github.com/osmoai/publications
15 September 2023, 19:03:31 UTC
  • Code
  • Branches (1)
  • Releases (0)
  • Visits
Revision 1ed5899d22f225b2e03d53dd23d109d8c7b87dfc authored by Rick Gerkin on 25 April 2023, 01:00:55 UTC, committed by GitHub on 25 April 2023, 01:00:55 UTC
Create README.md
1 parent 1643707
  • Files
  • Changes
    • Branches
    • Releases
    • HEAD
    • refs/heads/main
    • 1ed5899d22f225b2e03d53dd23d109d8c7b87dfc
    No releases to show
  • 734cbcc
  • /
  • lee_et_al_2023
  • /
  • src
  • /
  • analysis.py
Raw File Download Save again
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

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
  • snapshot
origin badgerevision badge
swh:1:rev:1ed5899d22f225b2e03d53dd23d109d8c7b87dfc
origin badgedirectory badge
swh:1:dir:9b439c8800f1cd38f7e33461013419d71e619299
origin badgecontent badge
swh:1:cnt:0c72f1d6bbeda6cf986b91724fc49dcc92756135
origin badgesnapshot badge
swh:1:snp:c43d033ddac10413c69edd9339aaca62b46b295e

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
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 1ed5899d22f225b2e03d53dd23d109d8c7b87dfc authored by Rick Gerkin on 25 April 2023, 01:00:55 UTC
Create README.md
Tip revision: 1ed5899
analysis.py
import itertools
import random

import numpy as np
import pandas as pd

from lee_et_al_2023.src import base

def center_model(model):
    """Center a model."""
    new_model = model.copy()
    all_ratings = new_model[base.MONELL_CLASS_LIST].values
    bias = np.mean(all_ratings, axis=0)
    new_model[base.MONELL_CLASS_LIST] = all_ratings - bias
    return new_model

def fast_process(humans, models, f=None, subjects=None, center_fn=center_model,
             descriptors=base.MONELL_CLASS_LIST, subgroup_size=1, axis=0):
    # humans: Either a dataframe of all the data OR a dict of individual human data in the same shape and style as the model dataframes.
    # models: A dict of individual model predictions.
    # f: A transformation to apply before computing correlation
    if f is None:
        f = lambda x: x
    
    if axis == 0:
        corr_fn = lambda df1, df2: f(df1).T.corrwith(f(df2).T)
    elif axis == 1:
        corr_fn = lambda df1, df2: f(df1).corrwith(f(df2))
    if subjects is None:
        if isinstance(humans, pd.DataFrame):
            subjects = humans['SubjectCode'].unique().tolist()
        else:
            subjects = list(humans.keys())
    subject_groups = list(itertools.combinations(subjects, subgroup_size))
    random.shuffle(subject_groups)
    subject_groups = subject_groups[:100] # For computational reasons...
    columns = list(models.keys()) + ['Human']
    corrs = []
    for subj_group in subject_groups:
        for rep in [1, 2]:
            # Slice out one subject and a subpanel of the remaining subjects
            human = humans[humans['SubjectCode'].isin(subj_group) & (humans['Rep'] == rep)
                          ].groupby('RedJade Code')[descriptors].mean()
            panel = humans[~humans['SubjectCode'].isin(subj_group)
                          ].groupby('RedJade Code')[descriptors].mean()
            # Align indices and select the subset of molecules that this human rated 
            panel = panel.loc[human.index]
            human[:] = center_fn(human)
            panel[:] = center_fn(panel)
            _rows = pd.DataFrame({
                'SubjectCode' : ', '.join(subj_group),
                'Rep': rep,
                'Human': corr_fn(human, panel)
            })
            # Compute correlation for each model with the same subpanel
            for model_name, model in models.items():
                model_ = model.loc[human.index][descriptors]
                model_[:] = center_fn(model_)
                _rows[model_name] = corr_fn(model_, panel)
            corrs.append(_rows)
    corrs = pd.concat(corrs)
    corrs = corrs.reset_index().set_index(['RedJade Code' if axis == 0 else 'index', 'SubjectCode', 'Rep'])
    return corrs


def model_performance(corrs, model_name):
    """
    Reports the performance of `model_name` in matrix `corrs` along
    `axis`, which determines aggregation across either descriptors or
    across molecules.
    
    Params:
        corrs: The output of `fast_corrs`.
        model_name: e.g. 'GNN'
    Returns:
        A pd.Series indexed by molecules or descriptors
        and containing win rates.
    """
    n_wins = (corrs[model_name] > corrs['Human']).groupby('RedJade Code').sum()
    n_ties = (corrs[model_name] == corrs['Human']).groupby('RedJade Code').sum()
    n_losses = (corrs[model_name] < corrs['Human']).groupby('RedJade Code').sum()
    return (n_wins + 0.5*n_ties) / (n_wins + n_ties + n_losses)


def shuffle_df(df, shuffle):
    df_contents = df.to_numpy()
    if shuffle == 'descriptors':
        # Generate an independent descriptor shuffle for each molecule
        shuffle_array = np.argsort(np.random.random(df_contents.shape), axis=1)
        df_contents = df_contents[np.arange(df_contents.shape[0])[:, np.newaxis], shuffle_array]
    elif shuffle == 'molecules':
        df_contents = df_contents[np.random.permutation(df_contents.shape[0])]
    else:
        raise ValueError('Unsupported shuffle technique: {}'.format(shuffle))
    return pd.DataFrame(df_contents, index=df.index, columns=df.columns)
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