Skip to content

Commit

Permalink
Fix solve-snapshot
Browse files Browse the repository at this point in the history
Taken from mamba-org#61

Co-authored-by: Bas Zalmstra <bas@prefix.dev>

Signed-off-by: Julien Jerphanion <git@jjerphan.xyz>
  • Loading branch information
jjerphan committed Aug 20, 2024
1 parent e42b8b1 commit 9a5e9cd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tools/solve-snapshot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.csv
.ipynb_checkpoints
*/.ipynb_checkpoints/*
8 changes: 5 additions & 3 deletions tools/solve-snapshot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use clap::Parser;
use csv::WriterBuilder;
use resolvo::{snapshot::DependencySnapshot, Solver, UnsolvableOrCancelled};
use resolvo::{snapshot::DependencySnapshot, Problem, Solver, UnsolvableOrCancelled};

#[derive(Parser)]
#[clap(version = "0.1.0", author = "Bas Zalmstra <zalmstra.bas@gmail.com>")]
Expand All @@ -27,7 +27,7 @@ fn main() {
let opts: Opts = Opts::parse();

eprintln!("Loading snapshot ...");
let snapshot_file = BufReader::new(File::open(&opts.snapshot).unwrap());
let snapshot_file = BufReader::new(File::open(opts.snapshot).unwrap());
let snapshot: DependencySnapshot = serde_json::from_reader(snapshot_file).unwrap();

let mut writer = WriterBuilder::new()
Expand All @@ -50,7 +50,9 @@ fn main() {
let mut solver = Solver::new(provider);
let mut records = None;
let mut error = None;
match solver.solve(vec![package_requirement.into()], vec![]) {

let problem = Problem::default().requirements(vec![package_requirement.into()]);
match solver.solve(problem) {
Ok(solution) => {
eprintln!("OK");
records = Some(solution.len())
Expand Down
95 changes: 95 additions & 0 deletions tools/solve-snapshot/timing_comparison.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e2ecd20f-bd4f-4a4a-82d0-28d2d9db18ac",
"metadata": {},
"outputs": [],
"source": [
"import polars as pl\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.rcParams['figure.dpi'] = 300\n",
"plt.rcParams['savefig.dpi'] = 300\n",
"\n",
"# These are all the timings we want to see\n",
"paths = [\"main_timings.csv\", \"timings.csv\"]\n",
"\n",
"# Read the CSV\n",
"dfs = [\n",
" pl.scan_csv(path).select(pl.col(\"package\"), pl.col(\"duration\")).collect()\n",
" for path in paths\n",
"]\n",
"\n",
"# Define the histogram bins\n",
"threshold = 10\n",
"bins = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, threshold, threshold + 1]\n",
"\n",
"dfs_capped = [\n",
" df.select([\n",
" pl.col(\"duration\").map_elements(lambda x: min(x, threshold), return_dtype=pl.Float64)\n",
" ]) for df in dfs]\n",
"\n",
"# Create the histogram\n",
"fig, axs = plt.subplots(2, sharex=True)\n",
"\n",
"for path, df_capped, axs in zip(paths, dfs_capped, axs):\n",
" axs.hist(df_capped[\"duration\"], bins=bins, density=True)\n",
" axs.set_title(path)\n",
"\n",
"# Add labels to the ticks\n",
"fig.supxlabel(\"Duration in seconds\")\n",
"fig.supylabel(\"Percantage of succesful solves\")\n",
"fig.suptitle(\"Histogram of solve durations\")\n",
"\n",
"plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88f7081d-d2bf-4fb7-86ab-7fd22dce0202",
"metadata": {},
"outputs": [],
"source": [
"paths = [\"timings.csv\", \"main_timings.csv\"]\n",
"\n",
"# Load the timings\n",
"dfs = [\n",
" pl.scan_csv(path).select(pl.col(\"package\"), pl.col(\"duration\")) \n",
" for path in paths\n",
"]\n",
"\n",
"# Compute the solver diffs\n",
"df_diff = dfs[0].join(dfs[1], on=\"package\").select(pl.col(\"package\"), (pl.col(\"duration\")-pl.col(\"duration_right\"))).collect();\n",
"\n",
"# Create the histogram\n",
"plt.hist(df_diff[\"duration\"], bins=40, density=True)\n",
"\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 9a5e9cd

Please sign in to comment.