How can I know that plt.colorbar must "eat" ax.pcolor? - matplotlib

Consider the following code:
#%%
Z=np.random.rand(len(X),len(Y))
X=np.arange(0,10)
Y=np.arange(0,10)
[fig1,ax1]=plt.subplots()
pcm=ax1.pcolor(X,Y,Z,cmap='seismic',edgecolors='black')
plt.colorbar(pcm,extend='max')
#[fig1,ax1]=plt.subplots()
#ax1.pcolor(X,Y,Z,cmap='seismic',edgecolors='black')
#plt.colorbar(extend='max')
The uncommented part works while the commented one doesnt. The error returned being:
RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
Looking at the documentation, it is said:
The matplotlib.cm.ScalarMappable (i.e., Image, ContourSet, etc.)
described by this colorbar. This argument is mandatory for the
Figure.colorbar method but optional for the pyplot.colorbar function,
which sets the default to the current image.
In this case, pcm correspond to the return value of ax1.pcolor() which type is "matplotlib.collections.Collection" and doesn't appear in the (non exhaustive) list given by the documentation.
How could I know from the documentation that I would indeed have to provide to plt.colorbar ax1.pcolor ? I know this from a stackoverflow post. How could I guess this from reading at the documentation ?

Related

Checking to see if an image format supports a usage in Vulkan?

If I want to see what an image format can be used for I can do the vkGetPhysicalDeviceImageFormatProperties2() and set the usage flag for the image format. I've noticed if the format isn't supported for those usages and settings the structure I pass in is set to all zero, and I can know if the format supports those uses. So if I want to know if VK_FORMAT_R8G8B8_UINT supports sampling from a shader I set the VK_IMAGE_USAGE_SAMPLED_BIT in the usage flags and call that function.
What I wanted to know is if that's equivalent to calling another function, called vkGetPhysicalDeviceFormatProperties2(), exactly the same name but without 'image' in the name, give that function the format, and check whether the VK_IMAGE_USAGE_SAMPLED_BIT is set.
So using the first method I give the format and usages I want from it, and then check if the values returned are zero max width, max height, etc, meaning those usages aren't supported, versus the second method of passing the format, getting back the flags and then checking the flags.
Are these two methods equivalent?
TL;DR: Do your image format checking properly: ask how you can use the format, then ask what functionality is available from usable format&usage combinations.
If you call vkGetPhysicalDeviceImageFormatProperties2 with usage flags and the like that don't correspond to a supported image type, you get an error: VK_ERROR_FORMAT_NOT_SUPPORTED. It inherits this due to the fact that it is said to "behave similarly to vkGetPhysicalDeviceImageFormatProperties", which has an explicit statement about this error:
If format is not a supported image format, or if the combination of format, type, tiling, usage, and flags is not supported for images, then vkGetPhysicalDeviceImageFormatProperties returns VK_ERROR_FORMAT_NOT_SUPPORTED.
Now normally, a function which gives rise to an error will yield undefined values in any return values. But there is a weird exception:
If the combination of parameters to vkGetPhysicalDeviceImageFormatProperties2 is not supported by the implementation for use in vkCreateImage, then all members of imageFormatProperties will be filled with zero.
However, there's an explicit note saying that this was old, bad behavior and is only preserved for compatibility's sake. Being a compatibility feature means that you can rely on it, but you shouldn't. Also, it only applies to the imageFormatProperties data and not any of the extension structures you can pass.
So it's best to just ignore this and ask your questions in the right order.

TFAgents: how to take into account invalid actions

I'm using TF-Agents library for reinforcement learning,
and I would like to take into account that, for a given state,
some actions are invalid.
How can this be implemented?
Should I define a "observation_and_action_constraint_splitter" function when
creating the DqnAgent?
If yes: do you know any tutorial on this?
Yes you need to define the function, pass it to the agent and also appropriately change the environment output so that the function can work with it. I am not aware on any tutorials on this, however you can look at this repo I have been working on.
Note that it is very messy and a lot of the files in there actually are not being used and the docstrings are terrible and often wrong (I forked this and didn't bother to sort everything out). However it is definetly working correctly. The parts that are relevant to your question are:
rl_env.py in the HanabiEnv.__init__ where the _observation_spec is defined as a dictionary of ArraySpecs (here). You can ignore game_obs, hand_obs and knowledge_obs which are used to run the environment verbosely, they are not fed to the agent.
rl_env.py in the HanabiEnv._reset at line 110 gives an idea of how the timestep observations are constructed and returned from the environment. legal_moves are passed through a np.logical_not since my specific environment marks legal_moves with 0 and illegal ones with -inf; whilst TF-Agents expects a 1/True for a legal move. My vector when cast to bool would therefore result in the exact opposite of what it should be for TF-agents.
These observations will then be fed to the observation_and_action_constraint_splitter in utility.py (here) where a tuple containing the observations and the action constraints is returned. Note that game_obs, hand_obs and knowledge_obs are implicitly thrown away (and not fed to the agent as previosuly mentioned.
Finally this observation_and_action_constraint_splitter is fed to the agent in utility.py in the create_agent function at line 198 for example.

Is there a way to assign an internal string or identifier or tag to a matplotlib artist?

Sometimes it is useful to assign a 'tag', which can be a simple string, to a matplotlib artist in order to later find it easily.
If we imagine a scenario where say plt.Line2D had a property called tag which can be retrieved using plt.Line2D.get_tag() it would be very easy to find it later in a complicated plot.
The only thing I can find that looks remotely similar is the group ID: for example line.set_gid() and line.get_gid(). I haven't found any good documentation on this. The only reference is this. Is this meant for such use as described above? Is it reserved for other operations in matplotlib?
This would be very useful for grouping different artists and then performing operations on them later, for example:
for line in ax.get_lines():
if line.get_tag() == 'group A'
line.set_color('red')
# or whatever other operation
Does such a thing exist?
You can use the gid for such purposes. The only side-effect is that those names will appear in a saved svg file as the gid tag.
Alternatively you can assign any attribute to a python object.
line, = plt.plot(...)
line.myid = "group A"
just make sure not to use any existing attribute in such case.

How to correctly pass initial value of transition_params in tensorflow linear chain CRF

I'm trying to use the linear chain CRF in my work. I took the help of the example usage code provided in -- https://github.com/tensorflow/tensorflow/tree/r1.0/tensorflow/contrib/crf
My question is how to supply some initial value of "transition_params" in "crf_log_likelihood()". For concreteness of the example, say, I want to initialize it with standard random normal distribution. In the api doc, I saw that "transition_params" can, in fact, be passed as an input argument. Inside the method I see that if no "transition_params" is passed, it is obtained by doing a "vs.get_variable()" with name = "transitions".
So should I do something similar to this, before creating the 'crf_log_likelihood' op? Something like -- transition_params = vs.get_variable("transitions", [num_tags, num_tags], initializer=tf.random_normal_initializer()) -- and then change the call of "crf_log_likelihood()" to "log_likelihood, transition_params = tf.contrib.crf.crf_log_likelihood(unary_scores, y_t, sequence_lengths_t, transition_params)"?
The get_variable() inside the definition of crf_log_likelihood() will create a fresh, randomly-initialized variable to represent the transition parameters, if you don't provide one yourself. You only need to provide an explicit transition_params if you don't want the default behavior.
To understand the behavior of get_variable(), see here:
https://www.tensorflow.org/api_docs/python/state_ops/sharing_variables#get_variable
Hope that helps!

Is it possible to replace an axis with a parasiteaxis?

I have code that requires a ParasiteAxis instance, or at least something with a toggle_axisline method, in order to function properly. I'd like to be able to pass it any arbitrary axis created in any way (e.g., through a simple plot call).
How can I convert any given axis instance into a ParasiteAxis or HostAxis instance?
(for a specific use-case, see issue https://github.com/aplpy/aplpy/pull/88 and specifically the workaround implemented in commit https://github.com/keflavich/aplpy/commit/02ecd5386f7d3a93ca8e60c3db2b34ff3216060a)