use custom functions in transform_calculate in altair - python

i have a bar chart, that i want to add approximation of each bar value, above each itself.
that value is a big number, i have a function that formats number e.g.: 10,000$ to 10k$. how can i apply that.
base = alt.Chart(target_df_hahu).mark_bar().encode(
alt.X('monthdate(date):O'),
alt.Y('value'),
color = 'variable'
)
i have try blow code .
text = base.mark_text().encode(
text = 'value:Q'
).transform_calculate(
value=custom_function(datum.value)
)
base+text

Calculate transforms are evaluated in Javascript by the Vega renderer, and thus cannot contain custom functionality defined in Python. Calculate transforms must be strings containing a well-defined subset of javascript syntax, making use of any of the functionality listed at https://vega.github.io/vega/docs/expressions/

Related

Python folium - Markercluster not iterable with GroupedLayerControl

I would like to group my 2 marker cluster layers, where one is reliant on the other by providing a separate styling. Hence the second one is set as control=False.
Nevertheless, I want to have it disappear when the first one is switched off.
Along with the new Python folium issue v.0.14 I found, that the new feature has been provided, which potentially could resolve my issue:
https://github.com/ikoojoshi/Folium-GroupedLayerControl
Allow only one layer at a time in Folium LayerControl
and I've applied the following code:
df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="OR")
or_cluster = MarkerCluster(name="Or", overlay=True, visible=True).add_to(map)
or_status = MarkerCluster(overlay=True,
control=False,
visible=False,
disableClusteringAtZoom=16,
).add_to(map)
GroupedLayerControl(
groups={'OrB': or_cluster, 'OrC': or_status},
collapsed=False,
).add_to(map)
and the console throws the following error:
TypeError: 'MarkerCluster' object is not iterable
How could I switch off 2 layer groups at once?
UPDATE:
The answer below provides the code, which seems to work but not in the way I need.
df = pd.read_csv("or_geo.csv")
fo=FeatureGroup(name="Or",overlay = True)
or_cluster = MarkerCluster(name="Or").add_to(map)
or_status = MarkerCluster(control=False,
visible=True,
disableClusteringAtZoom=16,
).add_to(map)
# definition of or_marker
# definition of or_stat_marker
or_cluster.add_child(or_marker)
or_status.add_child(or_stat_marker)
GroupedLayerControl(
groups={"Or": [or_cluster, or_status]},
collapsed=False,
exclusive_group=False,
).add_to(map)
I have a separate box instead, but what is worst I can just switch between one layer and another whereas I would like to have them reliant on the main group. The exclusive_groups option allows me to untick both of them but I am looking for something, which would allow me to switch off two of them at once (place the thick box on the major group instead). Is it possible to have something like this?
Try passing your markerclusters as a list to the GroupedLayerControl, not one by one. This is described here:
https://nbviewer.org/github/chansooligans/folium/blob/plugins-groupedlayercontrol/examples/plugin-GroupedLayerControl.ipynb
GroupedLayerControl(
groups={'OrB': [or_cluster, or_status]},
collapsed=False,
).add_to(map)
Update I
I see what you mean, that was definitely nonsense as it splits groups instead of joining them. so, back to topic
We had a similar discussion here and I am still convinced that the FeatureSubGroup should solve this issue. I use it in exact that way that I enable/disable a MarkerCluster in the legend and multiple FeatureGroupSubGroups (which are added not to the map but to the MarkerCluster) appear/disappear. Perhaps you try that again

Both latex and parameter values in python strings

I want to add a title to my plots (in matplotlib) which would display both parameters and their values, something like
f"$\alpha$ = {present value of alpha}, $\beta$ = {present value of beta}"
f-strings won't display latex but will insert the present parameter values into the placeholders; r-strings will do the former but not the latter.
Any way around this?

How to create n number of python functions based on user input?

Essentially, I'm looking to build a web app where the user can input n number of labels for a dataset put it into a dictionary with keywords for each label. I'd like the same function to be created for n number of labels, something like:
# labeling function for label 1
#labeling_function()
def lf_label_1(x):
if x.label_1 in ["bag", "surfboard", "skis"]:
return CARRY
return ABSTAIN
So, I'd get a new function for each new label added by the user. Each function then feeds into a list and ends up being input for a function. For example:
# list of labeling functions
lfs = [
lf_ride_object,
lf_carry_object,
lf_carry_subject,
lf_not_person,
lf_ydist,
lf_dist,
lf_area,
]
# applying label functions to create dataset
applier = PandasLFApplier(lfs)
L_train = applier.apply(df_train)
L_valid = applier.apply(df_valid)
For more details (and includes a second approach):
I'm looking for advice on how to use snorkel in a particular way. I'd like to create a labeling function with spaCy's PhraseMatcher. Basically, I want a user to input all of the words (with the corresponding label) in a web app and send it to the PhraseMatcher. Then, match a paragraph of text inside the labeling function.
How would I go about creating a labeling function(s?) for n number of labels on the backend? Typically, we would write code >n number of labeling functions for all the labels, but I'm trying to use snorkel in a use-case where we don't know how many labels there are until the user creates them.
Is there a way around this? Basically, the Matcher would go over the input text and check how many labels are in the text and then return all the labels found.Kind of like I'm trying to get the users to use snorkel without creating the functions themselves and only input the (label, word(s)) combinations.
Is there a way to use the Matcher in such a way that there is only one labeling function and it uses the Matcher for all labels?
For example, there could be a single labeling function that looks like this (peudo-code):
# labeling function for all labels
#nlp_labeling_function() # labeling function for using spaCy
def lf_labeler(x, label_keyword_dict):
labels = []
doc = nlp(x) # tokenizing the input text
matches = PhraseMatcher(doc) # finding all the token words that match a specific label
for match_id in matches:
labels.append(nlp.vocab.strings[match_id] # adds every label where there was a match
if labels: # runs if labels is not empty
return labels # this would return all the labels that were matches in the text document. Not sure if it's possible to return the list of labels like this.
else:
return ABSTAIN # abstains from using this text example in the dataset creation because there was no match
So, it would basically take in a piece of text (from a dataframe), check to see if any of the users' keywords are in the text and add all of the appropriate labels as a result. Return all the matched labels or simply return "ABSTAIN" to say that there were no matches.
While typing up this question I came up with some ideas that I wrote out here, so I'll be testing them in the meantime and have a look a the snorkel source code to see if I can come up with anything else.
Thanks!

openpyxl conditional formatting with named style

I have a named style, defined with:
style = openpyxl.styles.NamedStyle('style1')
style.fill = openpyxl.styles.PatternFill(patternType='solid', fill_type='solid', fgColor='BB0000')
style.border = openpyxl.styles.Border(left=openpyxl.styles.borders.Side(style='thin'),
right=openpyxl.styles.borders.Side(style='thin'),
bottom=openpyxl.styles.borders.Side(style='thin'),
top=openpyxl.styles.borders.Side(style='thin') )
I want to have a conditional format, based on cell value, that uses this style.
I can do something like:
rule = openpyxl.formatting.rules.CellIsRule(operator='lessThan', formula=['0']', ...)
But this doesn't seem to take a named style. It takes either a differential style, or a fill and a border as separate things...
You can't use named styles with conditional formats. There are subtle differences in the way they're applied but they are also implemented and stored differently. Just create a DifferentialStyle in the same way as a named style and set this as the dxf for the rule. You can probably do this very easily:
dxf = DifferentialStyle(**dict(style))

Using two different text sizes in one legend in Python

I am using matplotlib
I have a legend on a graph in Python which is:
plt.text(sigma1+5,5,str("$\sigma$("+"%.2f"%(sigma1) + ",0)"),color='red')
I'd like the symbol sigma to be larger than the rest of the text. Is this possible? Or do I have to create two separate legends?
Unfortunately, a matplotlib.text.Text instance uses a single style (font, size, etc.) for the whole string. So yes, I'm pretty sure you're going to need to create two of them.
If you don't know how to set the font size, see the docs for matplotlib.pyplot.text: you can either pass an optional fontdict argument that specifies font properties, or you can pass extra keyword arguments like size or fontproperties that get passed on to the Text constructor.

Categories