0

I am trying to run the following code in python 3.7 using spyder

import numpy as np
import freud
from util import box_2d_to_points
import matplotlib.pyplot as plt

data_path = "data/phi065"
box_data = np.load("{}/box_data.npy".format(data_path))
pos_data = np.load("{}/pos_data.npy".format(data_path))

def plot_rdf(box_arr, points_arr, prop, rmax=10, dr=0.1, label=None, ax=None):
    """Helper function for plotting RDFs."""
    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=(12, 8))
        ax.set_title(prop, fontsize=16)
    rdf = freud.density.RDF(rmax, dr)
    for box, points in zip(box_arr, points_arr):
        rdf.accumulate(box, points)
    if label is not None:
        ax.plot(rdf.R, getattr(rdf, prop), label=label)
        ax.legend()
    else:
        ax.plot(rdf.R, getattr(rdf, prop))
    return ax

First installed a package called "freud" (pip install freud) then when I run i get this error,

ImportError: cannot import name 'box_2d_to_points' from 'util' (/opt/anaconda3/lib/python3.7/site-packages/util/__init__.cpython-37m-darwin.so)

So I checked to see if package is installed by using

pip show util

I got this: WARNING: Package(s) not found: util Note: you may need to restart the kernel to use updated packages.

and yes i did restart the kernel, nothing changed.

When i try to install it

pip install util

ERROR: Could not find a version that satisfies the requirement util (from versions: none) ERROR: No matching distribution found for util Note: you may need to restart the kernel to use updated packages.

I searched online and found that it can be installed as pip install python-utils but then it creates a different directory and the code doesn't compile even if i change to from python_utils import box_2d_to_points.

But I think it is some directory issue that the package cannot be read. If you look into the screenshot attached, in library with folder 'freud' there is a folder 'util' that has the file "init.cpython-37m-darwin.so" I am wondering how to install this package. I followed the solutions in (Install a Python package into a different directory using pip?) but still didn't solve the problem.

The documentation for freud package can be found here:

and here is a screen shot of directories

Thank you in advance,

  • In the future, if you are working from example code, please make sure to state that explicitly and point users to that code. In this case, it seems you are working from the code found in [these demos from **freud-examples**](https://github.com/glotzerlab/freud-examples/tree/6f9a5f28c95a85f66085abf60860feaf58efd44e/archive/demos). Those are archived and not meant to be supported. [`util.py`](https://github.com/glotzerlab/freud-examples/blob/6f9a5f28c95a85f66085abf60860feaf58efd44e/archive/demos/util.py) is simply local code that the Jupyter notebooks load - not a module. – merv Oct 31 '19 at 16:40
  • Also, if you have Anaconda, it is best not to use Pip unless absolutely necessary and never use it in the **base** env (see "[*Using Pip in a Conda Environment*](https://www.anaconda.com/using-pip-in-a-conda-environment/)"). In this case, `conda install -c conda-forge freud` is clearly stated in the documentation, so that is the installation command to be preferred. – merv Oct 31 '19 at 16:48
  • Thanks for feedback, I should have mentioned i installed using ```conda install -c conda-forge freud``` ,,,,, it didn't load or recognize util, I couldn't find anywhere in documentation how to install util with conda , that is why I used pip install instead , which still doesn't work – Racaio Cmoto Nov 01 '19 at 01:19
  • I see. Yeah the point is, `util` isn't a really a thing, it's just a file that was used in some old examples. You can download that file and place it alongside code you want to write, but I think it would be better to stick to the newer documentation. – merv Nov 01 '19 at 01:30

3 Answers3

2

I am a maintainer of the freud package. This issue with confusion over the util file was previously reported by another user (https://github.com/glotzerlab/freud/issues/446) and has been resolved in version 2.0 of the freud library which was released yesterday (October 31, 2019).

As mentioned in other comments and answers, the util.py file is not included in the freud package. It was used in freud documentation examples for version 1.0, and contained functions that were mostly used for generating particle systems and plotting results. Because this has confused several new users, we decided to incorporate many of those functions directly into the freud package. In a few cases, we have chosen to leave the util.py file with plotting-specific code and clearly document where the file can be found (example: https://freud.readthedocs.io/en/stable/examples/module_intros/order.Hexatic.html). The reason for this choice to separate utility functions is that freud is primarily an analysis package, and visualization choices are generally left up to the user (though as of v2.0, many compute objects have helper methods for plotting their results with Matplotlib).

In freud v2.0, the documentation has been revised. The code snippet cited above has been updated for the new API in v2.0 and no longer relies on the util.py file. The new example is located here: https://freud.readthedocs.io/en/stable/examples/module_intros/density.RDF-AccumulateFluid.html

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

data_path = "data/phi065"
box_data = np.load("{}/box_data.npy".format(data_path))
pos_data = np.load("{}/pos_data.npy".format(data_path))

def plot_rdf(box_arr, points_arr, prop, r_max=10, bins=100, label=None, ax=None):
    """Helper function for plotting RDFs."""
    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=(12, 8))
        ax.set_title(prop, fontsize=16)
    rdf = freud.density.RDF(bins, r_max)
    for box, points in zip(box_arr, points_arr):
        rdf.compute(system=(box, points), reset=False)
    if label is not None:
        ax.plot(rdf.bin_centers, getattr(rdf, prop), label=label)
        ax.legend()
    else:
        ax.plot(rdf.bin_centers, getattr(rdf, prop))
    return ax

Also, I wanted to confirm that pip install freud will not get the right package. freud was originally available through conda channels but not via the Python Package Index (PyPI). We found that the name freud was already taken on PyPI when we started building wheels for easy installation with pip. The Installation page in the documentation lists the correct methods for installation, which are currently conda install -c conda-forge freud if using a conda environment, or pip install freud-analysis.

Thanks for using freud, and please feel free to reach out on the freud-users mailing list or freud's GitHub issues page if you have further questions.

  • Thanks a lot for your comments, I have gone through your new freud version and noticed that you changed the documentation. It gives me errors when trying to run Accumulation g(r) for fluid: – Racaio Cmoto Nov 11 '19 at 16:42
  • Type error ``----> 4 ax = plot_rdf(box_arr, pos_arr, 'rdf', label='One frame') in plot_rdf(box_arr, points_arr, prop, r_max, bins, label, ax) --->16 rdf.compute(system=(box, points), reset=False) TypeError: compute() takes at least 2 positional arguments (0 given) `` – Racaio Cmoto Nov 11 '19 at 16:47
  • @RacaioCmoto check out the discussion here https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/freud-users/iR1Z8PWXx_M/jyvodC9vBQAJ – Doryx Nov 11 '19 at 18:18
0

pip install freud installs a wrong package. The installation page shows the command pip install freud-analysis. Packages freud and freud-analysis are two completely different and unrelated ones.

So first uninstall freud then install freud-analysis:

pip uninstall freud
pip install freud-analysis

Unfortunately even that correct freud doesn't have box_2d_to_points. The only mention of box_2d_to_points is in the docs and nowhere else. I think you should ask the authors what they mean.

phd
  • 82,685
  • 13
  • 120
  • 165
0

Having gone through the codes and repository in GitHub and remarks that users: phd and merv gave , I noticed there is a file called util.py. I copied the it to the right directory and was able to run the code without the errors. If you encounter other problem that files are missing, I think the right way is to make sure you have right modules installed and have the necessary files in your directory.