SYNTOLOGY HomeExplorerAtlasCodeMethodologyAboutDevelopersFeedPricing
Paper · 2403.09605 · 2024

Counterfactual contrastive learning: robust representations via causal image synthesis

arXiv · PDF · Open in the Atlas

Code that ran

9 of 13 samples (functions and classes) lifted from this paper's linked repositories executed on a synthesized input in Syntology's jail (0 of them constructed an object rather than computing a result; 0 honoured the contract we drafted, 2 violated it and 1 has no contract verdict; 6 failures of our instrument; 4 no recorded run), measured 2026-11-22; repository links from the Papers with Code archive attributed under CC BY-SA, no affiliation; bodies inline only under a permissive licence as GitHub detects it. Not a reproduction of the paper's results.

"Ran" means the function executed on a synthesized input and returned a value. It is not a reproduction of the paper's results. This record, dated, as JSON: /api/ran/2403.09605.json · badge: /api/ran/2403.09605.svg

Each repository's role is a label: a classifier's output recorded on the link, or, where no role is recorded, what the Papers with Code archive marked. The line under it names its basis. Neither says who wrote the code.

RepositoryRoleRan
biomedia-mira/counterfactual-contrastive canonical (classifier label)
classifier basis: census
9 ran · 4 no recorded run
FunctionStatusWhere it lives
exists Ran
ran, violated the contract we drafted
biomedia-mira/counterfactual-contrastive/causal_models/utils.py
pointer only (licence: MIT); the stored copy get_code serves, from ThomasMrY/VCT, is pointer only (licence: NONE) · get_code("aa5486a3650902d8")
How it ran

The body of this sample is withheld: this paper's link allows the body inline (MIT), but get_code serves the stored copy, first harvested from ThomasMrY/VCT, and GitHub detected no licence in that repository when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

gaussian_kl Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/hvae.py
code served (permissive licence) · get_code("227af6c3aae15736")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import numpy as np
import torch.nn as nn
import torch.distributions as dist

torch.manual_seed(0)

# Create small tensors for the KL divergence computation
# Shape: (batch=2, dim=4) for all parameters
q_loc = torch.randn(2, 4)
q_logscale = torch.randn(2, 4)
p_loc = torch.randn(2, 4)
p_logscale = torch.randn(2, 4)

ARGS = {
    "q_loc": q_loc,
    "q_logscale": q_logscale,
    "p_loc": p_loc,
    "p_logscale": p_logscale,
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
numpy, torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
is_float_dtype Ran
ran, violated the contract we drafted
biomedia-mira/counterfactual-contrastive/causal_models/utils.py
pointer only (licence: MIT); the stored copy get_code serves, from sanyalsunny111/early_weight_avg, is pointer only (licence: NONE) · get_code("1ed19b773b317db5")
How it ran

The body of this sample is withheld: this paper's link allows the body inline (MIT), but get_code serves the stored copy, first harvested from sanyalsunny111/early_weight_avg, and GitHub detected no licence in that repository when it was checked. The setup block that called it is withheld with it, because it is written against that body. source_path and origin say where the code lives, to read under the repository's own terms. A run is recorded.

linear_warmup Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/utils.py
code served (permissive licence) · get_code("9893d00262df0805")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned function. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch

torch.manual_seed(0)

ARGS = {
    "warmup_iters": 1000
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type function — a function output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a function output the recorded ndim 0 and dtype family float describe that coercion, not the output, and are not stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
plot_counterfactual_viz_mimic Ran
ran on a synthesized input
biomedia-mira/counterfactual-contrastive/causal_models/plotting_utils.py
code served (permissive licence) · get_code("81e6698064c711ac")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Figure. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

torch.manual_seed(0)

class Args:
    def __init__(self):
        self.save_dir = "/tmp"
        self.iter = 0

# Create small tensors with shape (8, 1, 4, 3) for 8 images, 1 channel, 4x3 spatial dims
x = torch.randn(8, 1, 4, 3)
cf_x = torch.randn(8, 1, 4, 3)
pa = torch.randn(8, 5)
cf_pa = torch.randn(8, 5)
rec_loc = torch.randn(8, 1, 4, 3)

# Create do dict with sex and scanner keys
# sex values should be 0 or 1
# scanner values can be any tensor values
do = {
    'sex': torch.tensor([0, 1, 0, 1, 0, 1, 0, 1], dtype=torch.float32),
    'scanner': torch.tensor([0, 1, 0, 1, 0, 1, 0, 1], dtype=torch.float32)
}

ARGS = {
    'args': Args(),
    'x': x,
    'cf_x': cf_x,
    'pa': pa,
    'cf_pa': cf_pa,
    'do': do,
    'rec_loc': rec_loc,
    'save': False
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
matplotlib, numpy, torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Figure — a Figure output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a Figure output the recorded ndim 0 and dtype family float describe that coercion, not the output, and are not stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
plot_cxr_grid Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/plotting_utils.py
code served (permissive licence) · get_code("0e203533cf38cc3f")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned tuple. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import numpy as np
import matplotlib.pyplot as plt

torch.manual_seed(0)

# Create a small batch of chest X-ray images
# Shape: (batch_size, channels, height, width) = (2, 1, 64, 64)
x = torch.randn(2, 1, 64, 64)

# Create figure and axes for testing
fig, ax = plt.subplots(1, 2, figsize=(8, 8))

ARGS = {
    'x': x,
    'fig': fig,
    'ax': ax,
    'nrows': 1,
    'cmap': 'Greys_r',
    'norm': None,
    'cbar': False
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
matplotlib, numpy, torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type tuple — a tuple output has no array shape of its own; none is stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
preprocess_batch Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/trainer.py
code served (permissive licence) · get_code("ad5fda5f07b480d3")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned dict. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

# Create a minimal args object with required attributes
class Args:
    def __init__(self):
        self.device = 'cpu'
        self.input_res = (8, 8)

args = Args()

# Create a batch dict with required tensors
batch = {
    'x': torch.rand(2, 3, 16, 16),  # batch_size=2, channels=3, spatial dims
    'pa': torch.rand(2, 4),  # batch_size=2, features=4
    'dicom_id': ['id1', 'id2'],  # List, should not be reshaped
    'other_1d': torch.rand(2),  # 1D tensor, should be reshaped
    'other_2d': torch.rand(2, 5),  # already 2D, should not be reshaped
}

expand_pa = False

ARGS = {
    'args': args,
    'batch': batch,
    'expand_pa': expand_pa
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type dict — a dict output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a dict output the recorded ndim 0 and dtype family float describe that coercion, not the output, and are not stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
undo_norm Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/plotting_utils.py
code served (permissive licence) · get_code("bda5911e32c3e618")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned dict. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch

torch.manual_seed(0)

# The function takes a dict parameter 'pa' and modifies it in-place,
# specifically looking for an 'age' key and applying a denormalization formula
pa = {
    'age': torch.tensor(0.5),
    'name': 'test',
    'other_field': 42
}

ARGS = {
    'pa': pa
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type dict — a dict output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a dict output the recorded ndim 0 and dtype family float describe that coercion, not the output, and are not stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
zero_module Ran
ran; our contract draft was wrong, not the code
biomedia-mira/counterfactual-contrastive/causal_models/hvae.py
code served (permissive licence) · get_code("4719c763c53be3fe")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1) and returned SimpleModule. The image it ran in was not recorded. If your copy returns the same type on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

# Create a simple nn.Module with some parameters
class SimpleModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(4, 3)
        self.conv = nn.Conv1d(2, 5, 3)
    
    def forward(self, x):
        return x

module = SimpleModule()

ARGS = {
    "module": module
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type SimpleModule — a SimpleModule output has no array shape of its own; none is stated — the instrument coerces any output to an array; for a SimpleModule output the recorded ndim 0 and dtype family float describe that coercion, not the output, and are not stated
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
add_arguments Not yet run
unverified (harvested, no recorded run)
biomedia-mira/counterfactual-contrastive/causal_models/hps.py
code served (permissive licence) · get_code("ce5ff9ee4488ec4c")
How it ran

no invocation recorded.

sample_gaussian Not yet run
unverified (harvested, no recorded run)
biomedia-mira/counterfactual-contrastive/causal_models/hvae.py
code served (permissive licence) · get_code("f71d1270abe6581b")
How it ran

no invocation recorded.

scanner_counterfactuals Not yet run
unverified (harvested, no recorded run)
biomedia-mira/counterfactual-contrastive/causal_models/save_padchest_scanner_cf.py
code served (permissive licence) · get_code("a5c976876f53f736")
How it ran

no invocation recorded.

setup_hparams Not yet run
unverified (harvested, no recorded run)
biomedia-mira/counterfactual-contrastive/causal_models/hps.py
code served (permissive licence) · get_code("772421466c02275b")
How it ran

no invocation recorded.

Also present in other papers' code

Of 13 samples lifted from this paper's linked repositories, 13 carry a function identity and 0 do not. Of the 13 distinct functions with an identity, 4 are also present in other papers whose code we hold (4 with at least one paper dated earlier, 2 the same month, 2 later) and 9 are only here among the 52,977 papers whose code Syntology holds (as of 2026-09-26).

Only here means: among the 52,977 papers whose code Syntology holds (51,887 with lifted samples, 1,090 through their assemblies only), as of 2026-09-26, no other paper's lifted samples or current assembly bricks contain an identical function body after normalisation.

FunctionAlso present inEarliest-datedOthers named
exists 486 other papers
309 dated earlier, 19 the same month, 147 later, 9 same year, order not determinable, 2 undated · 78 only as an assembly brick
1603.08155 (2016-03)
Perceptual Losses for Real-Time Style Transfer and Super-Resolution
1611.07004 (2016-11)
Image-to-Image Translation with Conditional Adversarial Networks
1804.04637 (2018-04)
EMBER: An Open Dataset for Training Static PE Malware Machine Learning Models
and 483 more not named here
zero_module 196 other papers
105 dated earlier, 13 the same month, 77 later, 1 same year, order not determinable · 49 only as an assembly brick
2006.11239 (2020-06)
Denoising Diffusion Probabilistic Models
2010.02502 (2020-10)
Denoising Diffusion Implicit Models
2105.05233 (2021-05)
Diffusion Models Beat GANs on Image Synthesis
and 193 more not named here
is_float_dtype 2 other papers
2 dated earlier
2303.16570 (2023-03)
Point2Vec for Self-Supervised Representation Learning on Point Clouds
2306.03241 (2023-06)
Early Weight Averaging meets High Learning Rates for LLM Pre-training
linear_warmup 1 other paper
1 dated earlier
2402.01607 (2024-02)
Natural Counterfactuals With Necessary Backtracking

Presence, not direction: an identical function body in two papers' repositories does not say who wrote it, which came from which, or that either paper's method rests on it. Matching is exact after normalisation, so a renamed or edited function is not matched. Dates are when papers appeared, not when code was written. Only .py files the harvester lifted are covered; notebooks are not read. A new-style arXiv id YYMM.NNNN dates a paper to the month 20YY-MM; otherwise Paper.year dates it to the year; otherwise it is undated. Dates are when the paper appeared, not when the code was written. Read 2026-11-22.

Repositories linked to this paper

Some links come from the archived Papers with Code dataset (CC BY-SA 4.0): attribution and licence.

Abstract

Contrastive pretraining is well-known to improve downstream task performance and model generalisation, especially in limited label settings. However, it is sensitive to the choice of augmentation pipeline. Positive pairs should preserve semantic information while destroying domain-specific information. Standard augmentation pipelines emulate domain-specific changes with pre-defined photometric transformations, but what if we could simulate realistic domain changes instead? In this work, we show how to utilise recent progress in counterfactual image generation to this effect. We propose CF-SimCLR, a counterfactual contrastive learning approach which leverages approximate counterfactual inference for positive pair creation. Comprehensive evaluation across five datasets, on chest radiography and mammography, demonstrates that CF-SimCLR substantially improves robustness to acquisition shift with higher downstream performance on both in- and out-of-distribution data, particularly for domains which are under-represented during training.

For agents

The same record, over MCP at https://syntology.ai/mcp:

get_harvested_code_for_paper("2403.09605")
get_code_for_paper("2403.09605")
have("2403.09605")

The run record, dated, one paper per request, free:

curl https://syntology.ai/api/ran/2403.09605.json

A badge for a README (the split and the date, never a ratio):

[![Syntology run record](https://syntology.ai/api/ran/2403.09605.svg)](https://syntology.ai/paper/2403.09605)

Connect an agent — have() is free.