Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add loom package #3

Merged
merged 9 commits into from
Apr 12, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
result
result-*
27 changes: 19 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# 2. Add foo as a parameter to the outputs function
# 3. Add here: foo.flakeModule
./lib/devtools
inputs.flake-parts.flakeModules.easyOverlay
];
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];

Expand All @@ -34,18 +35,28 @@
};

toolkit = self.lib.basicTools pkgs;
in {
# Per-system attributes can be defined here. The self' and inputs'
# module parameters provide easy access to attributes of the same
# system.

devShells.default = pkgs.mkShell {
buildInputs = [] ++ toolkit;
};
callPackage = pkgs.newScope (pkgs // packages);
callPy3Package = pkgs.newScope (pkgs // pkgs.python3Packages // packages);

packages = {
inherit ociImgBase ociImgIqlQuery;
inherit callPackage
ociImgBase
ociImgIqlQuery
;

goftests = callPackage ./pkgs/goftests { };
parsable = callPackage ./pkgs/parsable { };
pymetis = callPackage ./pkgs/pymetis { };
distributions = callPackage ./pkgs/distributions { };
loom = callPy3Package ./pkgs/loom { };
};
in {
devShells.default = pkgs.mkShell {
buildInputs = [] ++ toolkit;
};

inherit packages;
};

# NOTE: this property is consumed by flake-parts.mkFlake to define fields
Expand Down
19 changes: 19 additions & 0 deletions pkgs/distributions/01-imread.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- a/examples/mixture/main.py
+++ b/examples/mixture/main.py
@@ -30,6 +30,7 @@
import numpy
import scipy
import scipy.misc
+import imageio
import scipy.ndimage
from distributions.dbg.random import sample_discrete, sample_discrete_log
from distributions.lp.models import nich
@@ -45,7 +46,7 @@
DATA = os.path.join(ROOT, 'data')
RESULTS = os.path.join(ROOT, 'results')
SAMPLES = os.path.join(DATA, 'samples.json.gz')
-IMAGE = scipy.misc.imread(os.path.join(ROOT, 'fox.png'))
+IMAGE = imageio.imread(os.path.join(ROOT, 'fox.png'))
SAMPLE_COUNT = 10000
PASSES = 10
EMPTY_GROUP_COUNT = 10
105 changes: 105 additions & 0 deletions pkgs/distributions/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
fetchFromGitHub,
callPackage,
fetchPypi,
eigen,
parsable,
goftests,
protobuf3_20,
python3Packages,
}:
let
version = "2.2.1";

src = fetchFromGitHub {
owner = "posterior";
repo = "distributions";
rev = "c2a9dccb09ab525927037ed59f3ceffb38e8a995"; # there is no tag
sha256 = "sha256-KP8o5w0PKdcwgmQJqRBRmEPrHesHvPQCp+g22mk5wOs=";
};

distributions-shared = callPackage ./distributions-shared.nix { inherit version src; };

imageio = python3Packages.buildPythonPackage rec {
pname = "imageio";
version = "2.6.1";

src = fetchPypi {
inherit pname version;
hash = "sha256-9E6yMbnfSFh08v/SLf0MPHEefeB2UWuTdO3qXGW8Z64=";
};

doCheck = false;

nativeBuildInputs = with python3Packages; [
pytest
];

propagatedBuildInputs = with python3Packages; [
pillow
];

buildInputs = with python3Packages; [
enum34
numpy
];
};
in
python3Packages.buildPythonPackage {
pname = "distributions";

inherit version src;

nativeBuildInputs = [
protobuf3_20
python3Packages.pyflakes
];

buildInputs = [
eigen
# TODO: we're not sure if this is even needed
distributions-shared
protobuf3_20
];

propagatedBuildInputs = with python3Packages; [
protobuf3_20
cython
numpy
parsable
scipy
simplejson
];

# TODO: be more precise. Some tests seem to be still in Python 2.
doCheck = false;
nativeCheckInputs = with python3Packages; [
imageio
nose
goftests
];

preBuild = ''
make protobuf
'';

patches = [
#./distributions-01-imread.patch
];

DISTRIBUTIONS_USE_PROTOBUF = 1;

# https://github.com/numba/numba/issues/8698#issuecomment-1584888063
NUMPY_EXPERIMENTAL_DTYPE_API = 1;

pythonImportsCheck = [
"distributions"
"distributions.io"
"distributions.io.stream"
];

passthru = {
# TODO: we're not sure if this is even needed
inherit distributions-shared;
};
}
26 changes: 26 additions & 0 deletions pkgs/distributions/distributions-shared.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
stdenv,
cmake,
eigen,
protobuf3_20,
src, version
}:
stdenv.mkDerivation {
pname = "distributions-shared";

inherit version src;

nativeBuildInputs = [ cmake ];
buildInputs = [eigen protobuf3_20 ];

DISTRIBUTIONS_USE_PROTOBUF = 1;

preConfigure = ''
make protobuf
'';

fixupPhase = ''
ln -sv $out/lib/libdistributions_shared_release.so $out/lib/libdistributions_shared.so
ln -sv $out/lib/libdistributions_shared_release.so $out/lib/libdistributions_shared_debug.so
'';
}
7 changes: 7 additions & 0 deletions pkgs/distributions/src.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ fetchFromGitHub }:
fetchFromGitHub {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this abstraction, but it looks like it's not called. What's the best way to import and invoke it? And what's more idiomatic -- src.nix or included in default.nix?

Copy link
Contributor Author

@srounce srounce Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this file was accidentally left in from an earlier iteration, it made more sense in the end to just declare it in a let and then pass it into the callPackage args of distributions-shared. More idiomatic to have it in the default.nix as a let binding and then pass it to both places it's used. I've removed this file now.

owner = "posterior";
repo = "distributions";
rev = "c2a9dccb09ab525927037ed59f3ceffb38e8a995";
sha256 = "sha256-KP8o5w0PKdcwgmQJqRBRmEPrHesHvPQCp+g22mk5wOs=";
}
25 changes: 25 additions & 0 deletions pkgs/goftests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
fetchPypi,
python3Packages,
}:
python3Packages.buildPythonPackage rec {
pname = "goftests";
version = "0.2.7";
format = "setuptools";

src = fetchPypi {
inherit pname version;
hash = "sha256-5s0NugSus2TuZIInesCNJNAtxEHnZLQIjn0pxGgwL/o=";
};

buildInputs = with python3Packages; [ numpy scipy ];

doCheck = false;

# https://github.com/numba/numba/issues/8698#issuecomment-1584888063
NUMPY_EXPERIMENTAL_DTYPE_API = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does nix handle these arbitrary keys at the top level in buildPythonPackage config?

Copy link
Contributor Author

@srounce srounce Apr 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be set as an environment variable in the builder context of the derivation's various phases. Gonna change this to env.* as it's a bit clearer.


patchPhase = ''
mkdir -p dist
'';
}
Loading