mimo-openenv-software / tasks /format-code-task-001926.json
akseljoonas's picture
Publish validated twelve-task MiMo curriculum and immutable image request
2a3480c verified
Raw History Blame Contribute Delete
8.15 kB
{"cwd": "/workspace/repo", "dataset_type": "opensource-code", "docker_image": "format-code-task-001926:latest", "instance_id": "format-code-task-001926", "problem_statement": "I want `mrinufft.density.cell_count(traj, shape, osf=1.0) -> numpy.ndarray` to estimate density compensation weights by counting how many non-Cartesian k-space samples fall in each Cartesian grid cell. `traj` should accept 2D or 3D sample coordinates, including arrays with leading dimensions that can be flattened into samples, and `shape` defines the reconstruction grid dimensions; `osf` controls the number of bins as `int(osf * size)` along each axis over unit k-space from `-0.5` to `0.5`.\n\nFor `traj = [[0.0, 0.0], [0.25, 0.25]]`, `shape = (2, 2)`, and `osf = 1.0`, both points are in the same cell, so the returned weights should be `[0.5, 0.5]`. For `traj = [[-0.25, -0.25], [0.25, 0.25], [0.25, 0.25]]`, `shape = (2, 2)`, and `osf = 1.0`, the isolated point should get twice the weight of each duplicated-cell point, returning `[0.5, 0.25, 0.25]`. The weights should be inverse cell-count weights normalized so the returned vector sums to 1, and repeated calls with the same inputs should return equal arrays without mutating `traj`.\n\nI also need `get_density(\"cell_count\")` to resolve to this estimator, and `get_density(\"cell_count\", traj, shape, osf=2.0)` to call it directly with the supplied arguments. If the number of entries in `shape` does not match the coordinate dimension of `traj`, the call should raise `ValueError` instead of returning a weight vector.", "test_command": "bash /workspace/repo/mimo_test_command.sh", "test_patch": "diff --git a/usercase-test-coderl/test_cell_count_density.py b/usercase-test-coderl/test_cell_count_density.py\nnew file mode 100644\n--- /dev/null\n+++ b/usercase-test-coderl/test_cell_count_density.py\n@@ -0,0 +1,230 @@\n+\n+\n+\n+import json\n+import subprocess\n+import sys\n+\n+import numpy as np\n+import pytest\n+\n+from mrinufft.density import cell_count, get_density\n+\n+\n+def assert_weights(actual, expected):\n+\n+ assert isinstance(actual, np.ndarray)\n+ assert actual.shape == (len(expected),)\n+\n+ np.testing.assert_allclose(actual, expected, rtol=1e-12, atol=1e-12)\n+\n+\n+def test_two_2d_points_in_same_grid_cell_get_equal_half_weights():\n+\n+\n+\n+\n+ traj = np.array([[0.0, 0.0], [0.25, 0.25]])\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(actual, [0.5, 0.5])\n+\n+\n+def test_isolated_2d_point_gets_twice_duplicate_cell_weight():\n+\n+\n+\n+\n+ traj = np.array([[-0.25, -0.25], [0.25, 0.25], [0.25, 0.25]])\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(actual, [0.5, 0.25, 0.25])\n+\n+\n+def test_each_2d_point_in_distinct_cell_gets_uniform_quarter_weight():\n+\n+\n+\n+\n+ traj = np.array(\n+ [[-0.25, -0.25], [-0.25, 0.25], [0.25, -0.25], [0.25, 0.25]]\n+ )\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(actual, [0.25, 0.25, 0.25, 0.25])\n+\n+\n+def test_fractional_oversampling_uses_int_scaled_bin_count():\n+\n+\n+\n+\n+ traj = np.array([[-0.25, -0.25], [-0.1, -0.1], [0.1, 0.1], [0.25, 0.25]])\n+\n+ actual = cell_count(traj, (2, 2), osf=1.5)\n+\n+ assert_weights(\n+ actual,\n+ [0.3333333333333333, 0.16666666666666666, 0.16666666666666666, 0.3333333333333333],\n+ )\n+\n+\n+def test_leading_dimensions_are_flattened_before_cell_counting():\n+\n+\n+\n+\n+ traj = np.array(\n+ [\n+ [[-0.25, -0.25], [0.25, 0.25]],\n+ [[0.25, 0.25], [-0.25, 0.25]],\n+ ]\n+ )\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(\n+ actual,\n+ [0.3333333333333333, 0.16666666666666666, 0.16666666666666666, 0.3333333333333333],\n+ )\n+\n+\n+def test_3d_coordinates_are_counted_against_3d_shape():\n+\n+\n+\n+\n+ traj = np.array(\n+ [\n+ [-0.25, -0.25, -0.25],\n+ [0.25, 0.25, 0.25],\n+ [0.25, 0.25, 0.25],\n+ [-0.25, 0.25, 0.25],\n+ ]\n+ )\n+\n+ actual = cell_count(traj, (2, 2, 2), osf=1.0)\n+\n+ assert_weights(\n+ actual,\n+ [0.3333333333333333, 0.16666666666666666, 0.16666666666666666, 0.3333333333333333],\n+ )\n+\n+\n+def test_lower_unit_kspace_boundary_is_included_in_counting():\n+\n+\n+\n+\n+ traj = np.array([[-0.5, -0.5], [-0.25, -0.25], [0.25, 0.25]])\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(actual, [0.25, 0.25, 0.5])\n+\n+\n+def test_get_density_resolves_cell_count_estimator():\n+\n+\n+\n+\n+ actual = get_density(\"cell_count\")\n+\n+\n+ assert callable(actual)\n+ traj = np.array([[0.0, 0.0], [0.25, 0.25]])\n+ assert_weights(actual(traj, (2, 2), osf=1.0), cell_count(traj, (2, 2), osf=1.0))\n+\n+\n+def test_get_density_direct_call_forwards_arguments_to_cell_count():\n+\n+\n+\n+\n+ traj = np.array([[0.0, 0.0], [0.25, 0.25]])\n+\n+ actual = get_density(\"cell_count\", traj, (2, 2), osf=2.0)\n+\n+ assert_weights(actual, [0.5, 0.5])\n+\n+\n+def test_shape_dimension_mismatch_raises_value_error():\n+\n+\n+\n+\n+ traj = np.array([[0.0, 0.0], [0.25, 0.25]])\n+\n+\n+ with pytest.raises(ValueError):\n+ cell_count(traj, (2, 2, 2), osf=1.0)\n+\n+\n+def test_referential_transparency_same_input_returns_equal_array():\n+ traj = np.array([[-0.25, -0.25], [0.25, 0.25], [0.25, 0.25]])\n+\n+ first = cell_count(traj, (2, 2), osf=1.0)\n+ second = cell_count(traj, (2, 2), osf=1.0)\n+\n+\n+ np.testing.assert_allclose(first, second, rtol=1e-12, atol=1e-12)\n+ assert_weights(first, [0.5, 0.25, 0.25])\n+\n+\n+def test_cell_count_does_not_mutate_traj():\n+ traj = np.array(\n+ [\n+ [[-0.25, -0.25], [0.25, 0.25]],\n+ [[0.25, 0.25], [-0.25, 0.25]],\n+ ]\n+ )\n+ original = traj.copy()\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(\n+ actual,\n+ [0.3333333333333333, 0.16666666666666666, 0.16666666666666666, 0.3333333333333333],\n+ )\n+ np.testing.assert_array_equal(traj, original)\n+\n+\n+def test_cell_count_has_no_same_key_global_cache_state_across_processes():\n+ traj = np.array([[-0.25, -0.25], [-0.1, -0.1], [0.1, 0.1], [0.25, 0.25]])\n+ in_process = cell_count(traj, (2, 2), osf=1.5).tolist()\n+ script = \"\"\"\n+import json\n+import numpy as np\n+from mrinufft.density import cell_count\n+traj = np.array([[-0.25, -0.25], [-0.1, -0.1], [0.1, 0.1], [0.25, 0.25]])\n+print(json.dumps(cell_count(traj, (2, 2), osf=1.5).tolist()))\n+\"\"\"\n+\n+ completed = subprocess.run(\n+ [sys.executable, \"-c\", script],\n+ check=True,\n+ text=True,\n+ stdout=subprocess.PIPE,\n+ stderr=subprocess.PIPE,\n+ )\n+ fresh_process = json.loads(completed.stdout)\n+\n+\n+ assert_weights(\n+ np.asarray(in_process),\n+ [0.3333333333333333, 0.16666666666666666, 0.16666666666666666, 0.3333333333333333],\n+ )\n+ np.testing.assert_allclose(fresh_process, in_process, rtol=1e-12, atol=1e-12)\n+\n+\n+def test_cell_count_is_independent_of_current_working_directory(monkeypatch, tmp_path):\n+\n+ monkeypatch.chdir(tmp_path)\n+ traj = np.array([[-0.25, -0.25], [0.25, 0.25], [0.25, 0.25]])\n+\n+ actual = cell_count(traj, (2, 2), osf=1.0)\n+\n+ assert_weights(actual, [0.5, 0.25, 0.25])\ndiff --git a/mimo_test_command.sh b/mimo_test_command.sh\nnew file mode 100755\n--- /dev/null\n+++ b/mimo_test_command.sh\n@@ -0,0 +1,17 @@\n+#!/usr/bin/env bash\n+\n+\n+\n+\n+export NODE_OPTIONS=\"${NODE_OPTIONS:-} --max-old-space-size=4096\"\n+export MAVEN_OPTS=\"${MAVEN_OPTS:-} -Xmx3g -XX:+UseG1GC\"\n+export GRADLE_OPTS=\"${GRADLE_OPTS:-} -Xmx3g -XX:+UseG1GC\"\n+export TEST_JVM_OPTS=\"${TEST_JVM_OPTS:-} -Xmx3g\"\n+set -e\n+cd /workspace/repo\n+set -euo pipefail\n+\n+cd /workspace/repo\n+export PYTHONPATH=\"/workspace/repo/src${PYTHONPATH:+:${PYTHONPATH}}\"\n+export PYTEST_DISABLE_PLUGIN_AUTOLOAD=1\n+exec python -m pytest /workspace/repo/usercase-test-coderl/test_cell_count_density.py -v\n", "verifier_timeout_sec": 1800}