astropy / astroplan

Observation planning package for astronomers – maintainer @bmorris3
https://astroplan.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
199 stars 109 forks source link

Astroplan - set issue #585

Open dsliski opened 2 months ago

dsliski commented 2 months ago

Hi,

I am trying to use the astroplan feature to decide when to observe various fields for an all sky H-alpha Survey. I am trying to incorporate the "best_month" feature into my output table which will then be used to decide which fields to schedule when. However, some of the fields are not observable due to only being observable from the Northern Hemisphere or Southern Hemisphere. As a result, I think best months should return an empty list. However, when it tries to write the column to a text file, it throws an error

"TypeError: unhashable type: 'set'"

Happy to share my python notebook if that is helpful. Just can't upload it here.

Best,

David

pllim commented 2 months ago

Thanks for reposting, @dsliski !

Notebook available from https://groups.google.com/g/astropy-dev/c/UmQdDbRgbS0 -- FYI

bmorris3 commented 2 months ago

Hi @dsliski!

For context here on GitHub, the code that @dsliski is trying to run looks like this:

from astropy.table import Table

observability_table = Table()

observability_table['targets'] = ['a', 'b', 'c']
observability_table['ever_observable'] = [True, True, False]
observability_table['always_observable'] = [True, False, False]
observability_table['best_months'] = [set([1, 2, 3]), set([2, 3]), set()]

observability_table.write('example_table.csv', overwrite=True)

The best_months column is a list of sets. Each element of an astropy Table object is supposed to be a hashable type – it shouldn't be a list, set, array, etc. You can get the behavior you're looking for by converting each set into a string like this, for example:

from astropy.table import Table

observability_table = Table()

observability_table['targets'] = ['a', 'b', 'c']
observability_table['ever_observable'] = [True, True, False]
observability_table['always_observable'] = [True, False, False]
observability_table['best_months'] = [set([1, 2, 3]), set([2, 3]), set()]

# convert sets to strings:
observability_table['best_months'] = [str(months) for months in observability_table['best_months']]

observability_table.write('example_table.csv', overwrite=True)

The resulting csv file looks like this:

targets,ever_observable,always_observable,best_months
a,True,True,"{1, 2, 3}"
b,True,False,"{2, 3}"
c,False,False,set()

Depending on what you plan to do with this table, you may want to store your results differently.

Does that answer your question?