Hi Stephanie,

The reason this is failing is that you're trying to represent 80 kpc as a tuple in your field definition.  Tuples are just a shorthand way of representing unit-aware quantities in some of the function calls (e.g. ProjectionPlot(ds, 'x', width=(80, 'kpc')) ), but they don't work outside of these special functions.

To fix this, you can just make your function call:

from yt.units import kpc

def _zdisk(field,data):
    return data['index','z'] - 80*kpc

Also, it's worth mentioning that there was a discussion on slack last week about offering a series of "relative_position" fields, where after you define a "center" for your data object (in your case, all_data), "relative_position_z" would automatically return the desired position relative to that center.  These fields already exist for velocity (e.g. for calculating radial velocities from a central point once a "bulk_velocity" is defined), and for particle fields, but not for grid-based position fields.  I believe Corentin Cadiou was going to implement this, which may provide an easier alternative than you defining your own fields.  And it would be used as:

ad = all_data()
ad.set_field_parameter("center", [0.5, 0.5, 0.5])
ad[('gas', 'relative_position_z')].to('kpc')

For more info on the "relative" fields, grep for "relative" on the "Fields" page: http://yt-project.org/docs/dev/reference/field_list.html#gas-relative-velocity-x
For more on declaring units, please see Nathan's excellent primer in the docs: http://yt-project.org/docs/dev/analyzing/units/symbolic_units.html


On Sat, Jan 19, 2019 at 1:05 PM Stephanie Tonnesen <stonnes@gmail.com> wrote:
Hi all,

I am making a phaseplot where the x-axis is the distance along the z-axis and the y-axis is the velocity in the z direction.  I am really, however, plotting distance above the disk, which is at [0.5,0.5,0.5] or [80,80,80] in kpc.  Instead of my x-axis going from 70 to 120 where 80 is the disk position, I would like it to go from -10 to 40 where 0 is the disk position.  

So, I thought I would define a new variable, zdisk, and use that, but have run into some problems.  Since this plot works beautifully when I am not messing with the z-values, I am guessing that I have done something wrong defining my variable.  Any help is much appreciated!!


yt version:
Version = 3.5.dev0
Changeset = f7ac18f5bf99

Okay, so the code:

import sys
import numpy as np
import yt
from yt.units import kpc

def _zdisk(field,data):
    return data['index','z'].in_units('kpc')-(80.,'kpc')

ds = yt.load("/mnt/ceph/users/stonnesen/data1/BPr16vs30arcfo/DD"+loop[i]+"/BPr16vs30arcfo"+loop[i])

yt.add_field(("gas","zdisk"), function=_zdisk,units="kpc")
ad = ds.all_data()
diska = ad.cut_region(["obj['Metal_Density']/obj['density'] > 0.25"])
profile = yt.create_profile(diska, ['zdisk','velocity_z'], 'cell_mass',n_bins=80,weight_field=None)
histogram = profile.field_data['gas','cell_mass']
histogram[histogram < 1e37] = 0
plot2 = yt.PhasePlot.from_profile(profile)


and now the error:
yt : [INFO     ] 2019-01-19 15:55:25,744 Parameters: current_time              = 3119.9999022826
yt : [INFO     ] 2019-01-19 15:55:25,744 Parameters: domain_dimensions         = [256 256 256]
yt : [INFO     ] 2019-01-19 15:55:25,744 Parameters: domain_left_edge          = [ 0.  0.  0.]
yt : [INFO     ] 2019-01-19 15:55:25,745 Parameters: domain_right_edge         = [ 1.  1.  1.]
yt : [INFO     ] 2019-01-19 15:55:25,746 Parameters: cosmological_simulation   = 0.0
/mnt/home/stonnesen/yt-conda/yt/yt/fields/local_fields.py:46: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  warnings.warn("Because 'sampling_type' not specified, yt will "

Parsing Hierarchy :   0%|          | 0/4284 [00:00<?, ?it/s]
Parsing Hierarchy :  23%|██▎       | 968/4284 [00:00<00:00, 9672.70it/s]
Parsing Hierarchy :  40%|███▉      | 1702/4284 [00:00<00:00, 8971.55it/s]
Parsing Hierarchy :  62%|██████▏   | 2643/4284 [00:00<00:00, 9101.75it/s]
Parsing Hierarchy :  77%|███████▋  | 3298/4284 [00:00<00:00, 8334.29it/s]
Parsing Hierarchy :  99%|█████████▉| 4241/4284 [00:00<00:00, 8660.50it/s]
Parsing Hierarchy : 100%|██████████| 4284/4284 [00:00<00:00, 8466.80it/s]
yt : [INFO     ] 2019-01-19 15:55:26,407 Gathering a field list (this may take a moment.)
Traceback (most recent call last):
  File "yt_allphaseplot_newz.py", line 40, in <module>
    profile = yt.create_profile(diska, ['zdisk','velocity_z'], 'cell_mass',n_bins=80,weight_field=None)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/data_objects/profiles.py", line 1042, in create_profile
    bin_fields = data_source._determine_fields(bin_fields)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/data_objects/data_containers.py", line 1165, in _determine_fields
    finfo = self.ds._get_field_info("unknown", fname)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/data_objects/static_output.py", line 756, in _get_field_info
  File "/mnt/home/stonnesen/yt-conda/yt/yt/data_objects/static_output.py", line 514, in index
  File "/mnt/home/stonnesen/yt-conda/yt/yt/data_objects/static_output.py", line 571, in create_field_info
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/field_info_container.py", line 276, in load_all_plugins
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/field_info_container.py", line 289, in find_dependencies
    deps, unavailable = self.check_derived_fields(loaded)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/field_info_container.py", line 362, in check_derived_fields
    fd = fi.get_dependencies(ds = self.ds)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/derived_field.py", line 216, in get_dependencies
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/field_detector.py", line 108, in __missing__
    vv = finfo(self)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/fields/derived_field.py", line 256, in __call__
    dd = self._function(self, data)
  File "yt_allphaseplot_newz.py", line 21, in _zdisk
    return data['index','z'].in_units('kpc')-(80.,'kpc')
  File "/mnt/home/stonnesen/yt-conda/yt/yt/units/yt_array.py", line 1391, in __array_ufunc__
    inps, units, ufunc, ret_class)
  File "/mnt/home/stonnesen/yt-conda/yt/yt/units/yt_array.py", line 161, in handle_preserve_units
    any_nonzero = [np.any(inps[0]), np.any(inps[1])]
  File "/mnt/xfs1/home/stonnesen/yt-conda/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1970, in any
    return arr.any(axis=axis, out=out, **kwargs)
  File "/mnt/xfs1/home/stonnesen/yt-conda/lib/python3.6/site-packages/numpy/core/_methods.py", line 38, in _any
    return umr_any(a, axis, dtype, out, keepdims)
TypeError: cannot perform reduce with flexible type

Dr. Stephanie Tonnesen
Associate Research Scientist
CCA, Flatiron Institute
New York, NY

yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org

Cameron Hummels
NSF Postdoctoral Fellow
Department of Astronomy
California Institute of Technology