jjhelmus / nmrglue

A module for working with NMR data in Python
BSD 3-Clause "New" or "Revised" License
209 stars 86 forks source link

ng.pipe.make_uc() type error #113

Closed hadwin-357 closed 4 years ago

hadwin-357 commented 4 years ago

I am using python 3.7.6 and tried both the nmrglue 0.7 and nmrglue 0.8.dev

I am running to a typeerror when I tried to use ng.pipe.make_uc().

Here is my code:

dic, data = ng.pipe.read("./test.ft") uc2=ng.pipe.make_uc(dic, data) fig = plt.figure() ax = fig.add_subplot(111)

ax.plot(uc2.ppm_scale()[::-1], data[::-1], 'k-')

This gives me the following error message:

TypeError Traceback (most recent call last) /usr/local/lib/python3.7/site-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis) 116 try: --> 117 num = operator.index(num) 118 except TypeError:

TypeError: 'float' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)

in 4 ax = fig.add_subplot(111) 5 ----> 6 ax.plot(uc2.ppm_scale()[::-1], data[::-1], 'k-') /usr/local/lib/python3.7/site-packages/nmrglue/fileio/fileiobase.py in ppm_scale(self) 274 """ 275 x0, x1 = self.ppm_limits() --> 276 return np.linspace(x0, x1, self._size) 277 278 def hz_limits(self): <__array_function__ internals> in linspace(*args, **kwargs) /usr/local/lib/python3.7/site-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis) 119 raise TypeError( 120 "object of type {} cannot be safely interpreted as an integer." --> 121 .format(type(num))) 122 123 if num < 0: TypeError: object of type cannot be safely interpreted as an integer. I also tried the following code and it works: # read in the data from a NMRPipe file dic, data = ng.pipe.read("./test.ft") ## create a unit conversion object for the axis udic = ng.pipe.guess_udic(dic, data) uc = ng.fileio.fileiobase.uc_from_udic(udic) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(uc.ppm_scale()[::-1], data[::-1], 'k-')
jjhelmus commented 4 years ago

@hadwin-357 What version of NumPy do you have installed? I believe some of the recent releases are more strict about automatic type conversion.

reneeotten commented 4 years ago

I ran into a similar issue today with NumPy version 1.18.1. The problem is that linspace does not accept floats anymore for the number of samples to generate (this is the upstream NumPy commit).

Explicitly converting that argument to a int everywhere would work, but perhaps making sure that ._size is an integer makes more sense. The issue for me can be resolved by changing self._size = size to self._size = int(size) on line 76 of fileio/fileiobase.py.

reneeotten commented 4 years ago

as a side-note there are two additional warnings about code in fileio/fileiobase.py:

/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nmrglue/fileio/fileiobase.py:388: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if unit is 'ppm':
/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nmrglue/fileio/fileiobase.py:391: SyntaxWarning: "is" with a literal. Did you mean "=="?
  elif unit is 'hz':

If you want I can make a PR to fix these at the same time.

jjhelmus commented 4 years ago

@reneeotten Thanks for looking into this issue. I agree, the size argument should be cast to an integer. I've implemented this and changed to string comparison to use an equality in #114.

In the future feel free to submit PRs for issues like these, I'd be happy to review them.

reneeotten commented 4 years ago

thanks @jjhelmus - I will submit a PR in the future. Are you planning to tag/release a new version soon-ish?

jjhelmus commented 4 years ago

Are you planning to tag/release a new version soon-ish?

It is about time for a release. I'll tag one later this week.