Hi, everyone
I want to utilize data using ds.region function. I have used the CHANGA code-Tipsy dataset, which has 3 particle types - Gas, Stars, Darkmatter. Because there is no particle index, so I added a field this way. ``` ds=yt.load("My data~~") gas_num=np.shape(ds.all_data()['Gas','Mass'])[0] #number of gas particle star_num=np.shape(ds.all_data()['Stars','Mass'])[0] #number of star particle dm_num=np.shape(ds.all_data()['DarkMatter','Mass'])[0] #number of darkmatter particle gas_index=range(gas_num) #gas index star_index=range(gas_num,gas_num+star_num) #star index dm_index=range(gas_num+star_num,gas_num+star_num+dm_num) #darkmatter index all_index=range(gas_num+star_num+dm_num) def _gas_index(field,data): #define index field return (ds.arr(gas_index)) def _star_index(field,data): return (ds.arr(star_index)) def _dm_index(field,data): return (ds.arr(dm_index)) def _all_index(field,data): return (ds.arr(all_index)) yt.add_field( name=("Gas", "particle_index"), function=_gas_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("Gas", "particle_index"), function=_gas_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("Stars", "particle_index"), function=_star_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("Stars", "particle_index"), function=_star_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("DarkMatter", "particle_index"), function=_dm_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("DarkMatter", "particle_index"), function=_dm_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("all", "particle_index"), function=_all_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("all", "particle_index"), function=_all_index, sampling_type="particle", units="(dimensionless)", ) ```
and then, I made a sphere region to select data in this region. However, the problem is although I made the regions, particle_index returns all datasets.
``` sp=ds.sphere(center,(radius,"kpc")) len(ds.all_data()["DarkMatter","particle_mass"]) -->19584552 len(sp["DarkMatter","particle_mass"]) -->1287749 ``` this is what I expected, but
``` len(ds.all_data()["DarkMatter","particle_index"]) -->19584552 len(sp["DarkMatter","particle_index"]) -->19584552 ``` the particle_index field returns the same value. It seems that this is because I just used array when I define the field function, but I don't have an idea.
I would appreciate it if anyone can give me some favors. Thank you for reading!
Sincerely, HyeonYong
Hi HyeonYong,
Can you try two things for me?
1) Add the fields before you create the dataset. Instead of accessing the star count etc outside of it, access it like:
data.ds.whatever
so that it gets dynamically called each time. 2) Return a newly allocated array each time, instead of a copy of an external array.
There's also the possibility that there will be issues with "sub-chunking" where the subsets of data don't line up. I think I have an idea for that case, but I want to see if these will work first.
Thanks,
Matt
On Mon, Mar 28, 2022 at 1:27 PM HyeonYong Kim gusdyd398@snu.ac.kr wrote:
Hi, everyone
I want to utilize data using ds.region function. I have used the CHANGA code-Tipsy dataset, which has 3 particle types - Gas, Stars, Darkmatter. Because there is no particle index, so I added a field this way.
ds=yt.load("My data~~") gas_num=np.shape(ds.all_data()['Gas','Mass'])[0] #number of gas particle star_num=np.shape(ds.all_data()['Stars','Mass'])[0] #number of star particle dm_num=np.shape(ds.all_data()['DarkMatter','Mass'])[0] #number of darkmatter particle gas_index=range(gas_num) #gas index star_index=range(gas_num,gas_num+star_num) #star index dm_index=range(gas_num+star_num,gas_num+star_num+dm_num) #darkmatter index all_index=range(gas_num+star_num+dm_num) def _gas_index(field,data): #define index field return (ds.arr(gas_index)) def _star_index(field,data): return (ds.arr(star_index)) def _dm_index(field,data): return (ds.arr(dm_index)) def _all_index(field,data): return (ds.arr(all_index)) yt.add_field( name=("Gas", "particle_index"), function=_gas_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("Gas", "particle_index"), function=_gas_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("Stars", "particle_index"), function=_star_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("Stars", "particle_index"), function=_star_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("DarkMatter", "particle_index"), function=_dm_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("DarkMatter", "particle_index"), function=_dm_index, sampling_type="particle", units="(dimensionless)", ) yt.add_field( name=("all", "particle_index"), function=_all_index, sampling_type="particle", units="(dimensionless)", ) ds.add_field( name=("all", "particle_index"), function=_all_index, sampling_type="particle", units="(dimensionless)", )
and then, I made a sphere region to select data in this region. However, the problem is although I made the regions, particle_index returns all datasets.
sp=ds.sphere(center,(radius,"kpc")) len(ds.all_data()["DarkMatter","particle_mass"]) -->19584552 len(sp["DarkMatter","particle_mass"]) -->1287749
this is what I expected, but
len(ds.all_data()["DarkMatter","particle_index"]) -->19584552 len(sp["DarkMatter","particle_index"]) -->19584552
the particle_index field returns the same value. It seems that this is because I just used array when I define the field function, but I don't have an idea.
I would appreciate it if anyone can give me some favors. Thank you for reading!
Sincerely, HyeonYong _______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org https://mail.python.org/mailman3/lists/yt-users.python.org/ Member address: matthewturk@gmail.com
Hi Matthew!
Thanks! The problem is solved. I changed the function like below. ``` def _gas_index(field,data): gas_num=np.shape(data['Gas','Mass'])[0] gas_index=np.arange(gas_num) return (data.ds.arr(gas_index)) ```
I think the former is the solution to this problem. I tried to return the newly allocated array each time accessing the outside counting data, It didn't work.
Thank you for your help! HyeonYong