adrn / thejoker

A custom Monte Carlo sampler for the (gravitational) two-body problem
MIT License
30 stars 8 forks source link

I follow the tutorial you wrote, but when I got to this step, the following errors occurred, I would like to ask you for advice;joker = tj.TheJoker(prior, random_state=rnd) joker_samples = joker.rejection_sample(data, prior_samples, max_posterior_samples=256), #132

Closed yanhuahui1 closed 8 months ago

yanhuahui1 commented 8 months ago

RepresenterError Traceback (most recent call last) Cell In[21], line 2 1 joker = tj.TheJoker(prior, random_state=rnd) ----> 2 joker_samples = joker.rejection_sample(data, prior_samples)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/thejoker.py:248, in TheJoker.rejection_sample(self, data, prior_samples, n_prior_samples, max_posterior_samples, n_linear_samples, return_logprobs, return_all_logprobs, n_batches, randomize_prior_order, in_memory) 238 samples = rejection_sample_inmem( 239 joker_helper, 240 prior_samples, (...) 244 n_linear_samples=n_linear_samples, 245 return_all_logprobs=return_all_logprobs) 247 else: --> 248 samples = rejection_sample_helper( 249 joker_helper, 250 prior_samples, 251 pool=self.pool, 252 random_state=self.random_state, 253 n_prior_samples=n_prior_samples, 254 max_posterior_samples=max_posterior_samples, 255 n_linear_samples=n_linear_samples, 256 return_logprobs=return_logprobs, 257 n_batches=n_batches, 258 randomize_prior_order=randomize_prior_order, 259 return_all_logprobs=return_all_logprobs) 261 return samples

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/utils.py:294, in tempfile_decorator..wrapper(*args, *kwargs) 292 func_return = func(args, **kwargs) 293 except Exception as e: --> 294 raise e 295 finally: 296 os.unlink(f.name)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/utils.py:290, in tempfile_decorator..wrapper(*args, *kwargs) 286 f.close() 288 try: 289 # write samples to tempfile and recursively call this method --> 290 prior_samples.write(f.name, overwrite=True) 291 kwargs['prior_samples_file'] = f.name 292 func_return = func(args, **kwargs)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/samples.py:464, in JokerSamples.write(self, output, overwrite, append) 462 t.write(output, overwrite=overwrite) 463 else: --> 464 write_table_hdf5(self.tbl, output, path=self._hdf5_path, 465 compression=False, 466 append=append, overwrite=overwrite, 467 serialize_meta=True, metadata_conflicts='error', 468 maxshape=(None, ))

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/samples_helpers.py:124, in write_table_hdf5(table, output, path, compression, append, overwrite, serialize_meta, metadata_conflicts, create_dataset_kwargs) 122 # Recursively call the write function 123 try: --> 124 return write_table_hdf5(table, f, path=path, 125 compression=compression, append=append, 126 overwrite=overwrite, 127 serialize_meta=serialize_meta, 128 create_dataset_kwargs) 129 finally: 130 f.close()

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/thejoker/samples_helpers.py:194, in write_table_hdf5(table, output, path, compression, append, overwrite, serialize_meta, metadata_conflicts, create_dataset_kwargs) 190 dset = output_group.create_dataset(name, data=table.as_array(), 191 create_dataset_kwargs) 193 if serialize_meta: --> 194 header_yaml = meta.get_yaml_from_table(table) 196 header_encoded = [h.encode('utf-8') for h in header_yaml] 197 output_group.create_dataset(meta_path(name), 198 data=header_encoded)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:308, in get_yaml_from_table(table) 305 if table.meta: 306 header["meta"] = table.meta --> 308 return get_yaml_from_header(header)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:388, in get_yaml_from_header(header) 385 header["datatype"] = [_get_col_attributes(col) for col in header["cols"]] 386 del header["cols"] --> 388 lines = yaml.dump( 389 header, default_flow_style=None, Dumper=TableDumper, width=130 390 ).splitlines() 391 return lines

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/init.py:253, in dump(data, stream, Dumper, kwds) 248 def dump(data, stream=None, Dumper=Dumper, kwds): 249 """ 250 Serialize a Python object into a YAML stream. 251 If stream is None, return the produced string instead. 252 """ --> 253 return dump_all([data], stream, Dumper=Dumper, **kwds)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/init.py:241, in dump_all(documents, stream, Dumper, default_style, default_flow_style, canonical, indent, width, allow_unicode, line_break, encoding, explicit_start, explicit_end, version, tags, sort_keys) 239 dumper.open() 240 for data in documents: --> 241 dumper.represent(data) 242 dumper.close() 243 finally:

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:27, in BaseRepresenter.represent(self, data) 26 def represent(self, data): ---> 27 node = self.represent_data(data) 28 self.serialize(node) 29 self.represented_objects = {}

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:48, in BaseRepresenter.represent_data(self, data) 46 data_types = type(data).mro 47 if data_types[0] in self.yaml_representers: ---> 48 node = self.yaml_representers[data_types[0]](self, data) 49 else: 50 for data_type in data_types:

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:207, in SafeRepresenter.represent_dict(self, data) 206 def represent_dict(self, data): --> 207 return self.represent_mapping('tag:yaml.org,2002:map', data)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:366, in get_yaml_from_header..TableDumper.represent_mapping(self, tag, mapping, flow_style) 364 for item_key, item_value in mapping: 365 node_key = self.represent_data(item_key) --> 366 node_value = self.represent_data(item_value) 367 if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): 368 best_style = False

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:48, in BaseRepresenter.represent_data(self, data) 46 data_types = type(data).mro 47 if data_types[0] in self.yaml_representers: ---> 48 node = self.yaml_representers[data_types[0]](self, data) 49 else: 50 for data_type in data_types:

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:152, in _repr_odict(dumper, data) 139 def _repr_odict(dumper, data): 140 """ 141 Represent OrderedDict in yaml dump. 142 (...) 150 '!!omap [foo: bar, mumble: quux, baz: gorp]\n' 151 """ --> 152 return _repr_pairs(dumper, "tag:yaml.org,2002:omap", data.items())

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:127, in _repr_pairs(dump, tag, sequence, flow_style) 125 best_style = True 126 for key, val in sequence: --> 127 item = dump.represent_data({key: val}) 128 if not (isinstance(item, yaml.ScalarNode) and not item.style): 129 best_style = False

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:48, in BaseRepresenter.represent_data(self, data) 46 data_types = type(data).mro 47 if data_types[0] in self.yaml_representers: ---> 48 node = self.yaml_representers[data_types[0]](self, data) 49 else: 50 for data_type in data_types:

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:207, in SafeRepresenter.represent_dict(self, data) 206 def represent_dict(self, data): --> 207 return self.represent_mapping('tag:yaml.org,2002:map', data)

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/astropy/table/meta.py:366, in get_yaml_from_header..TableDumper.represent_mapping(self, tag, mapping, flow_style) 364 for item_key, item_value in mapping: 365 node_key = self.represent_data(item_key) --> 366 node_value = self.represent_data(item_value) 367 if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): 368 best_style = False

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:58, in BaseRepresenter.represent_data(self, data) 56 node = self.yaml_multi_representers[None](self, data) 57 elif None in self.yaml_representers: ---> 58 node = self.yaml_representers[None](self, data) 59 else: 60 node = ScalarNode(None, str(data))

File ~/anaconda3/envs/thejoker2/lib/python3.8/site-packages/yaml/representer.py:231, in SafeRepresenter.represent_undefined(self, data) 230 def represent_undefined(self, data): --> 231 raise RepresenterError("cannot represent an object", data)

RepresenterError: ('cannot represent an object', Generator(PCG64) at 0x7F152CB68200)

adrn commented 8 months ago

This looks like a Numpy error to me -- the new version of TheJoker requires Python 3.9 or higher, so could you try this instead in a more recent version of Python? For example, 3.11?

yanhuahui1 commented 8 months ago

Great! Problem solved. Thank you very much for your help.