Can TogglerBar be used as multiple CheckBox in Mathematica? - checkbox

Would it be possible to have a TogglerBar instead of the 2 Check Box to show or not the different Shapes.
With Green & Red written in each Button of the TogglerBar ?
Manipulate[
Graphics[{If[thePink, {Pink, Disk[{5, 5}, 3]}],
If[theGreen, {Green, Disk[{15, 2}, 1]}]},
PlotRange -> {{0, 20}, {0, 10}}], {{thePink, True,
Style["Pink", Black, Bold, 12]}, {True, False}}, {{theGreen, True,
Style["Green", Black, Bold, 12]}, {True, False}}]
The actual Manipulate object I am trying to adjust can be found there : http://www.laeh500.com/LAEH/COG.html
The purpose being to replace the CheckBox by a nice TogglerBar.

Like this?
Manipulate[
Graphics[{
{White, Circle[{5, 5}, r]}, (* For Mma 7 compatibility*)
If[MemberQ[color, "Red"], {Pink, Disk[{5, 5}, r]}],
If[MemberQ[color, "Green"], {Green, Disk[{4, 2}, r]}]},
PlotRange -> {{0, 20}, {0, 10}}],
{{r, 1, "Radius"}, 1, 5, 1, ControlType -> Setter},
{{color, "Red", "Color"}, {"Red", "Green"}, ControlType -> TogglerBar},
LabelStyle -> Large]
Edit
Answering your comment, I think your notebook could benefit from a template like this one:
Manipulate[
Graphics[
{
{White, Circle[{5, 5}, r]},(* For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "Circle"], {Red, Circle [{5, 5}, r]}],
If[MemberQ[whatToDisplay, "Square"], {Blue, Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black, Line [Tuples[{3, 4}, 2]]}],
},
PlotRange -> {{0, 20}, {0, 10}}
],
(* Controls follow *)
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider
, ControlPlacement-> Top
},
Control#{{whatToDisplay, True, Style["What", Black, Bold, 12]},
{"Circle", "Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Vertical",
ControlPlacement -> Left
}
]

How about this?
Manipulate[
Show[Graphics[myObject],
PlotRange -> {{0, 20}, {0, 10}}], {{myObject, {},""}, {{Pink,
Disk[{5, 5}, 3]} ->
Style["Pink", Black, Bold, 12], {Green, Disk[{15, 2}, 1]} ->
Style["Green", Black, Bold, 12]}}, ControlType -> TogglerBar]

How about
Manipulate[
Graphics[{#} & /# x,
PlotRange -> {{0, 20}, {0, 10}}],
{{x, {}, "Colour"},
{{Pink, Disk[{5, 5}, 3]} \[Rule] "Pink",
{Green, Disk[{15, 2}, 1]} \[Rule] "Green"},
ControlType -> TogglerBar}]
it's ugly and inelegant, though! Dynamic manipulation is not my favourite use of Mathematica, so this is sort of trial and error for me too...
EDIT: Slightly less ugly now...
EDIT2: Added a label

Related

Mongodb - how to aggregate with "bucket"

that's my dataset:
[
{_id: "628d54495995f1e6589675b3", age: 52, sex: 1, cp: 0, trestbps: 125,
chol: 212, fbs: 0, restecg: "1", thalach: 168, exang: "0", oldpeak: 1},
{_id: "628d54495995f1e6589675b4", age: 53, sex: 1, cp: 1, trestbps: 140,
chol: 203, fbs: 1, restecg: "0", thalach: 155, exang: "1", oldpeak: 3.1},
]
... and so on
I want to group for 2 classes of age (under 40 (included) and over 40 (not included)) and for each class to determine which value of cp is the most frequent.
cp can have four different values: 0, 1, 2, 3
I thought bucket could be the right operator, but I can't get it to work
Thanks for helping

Identify rows of one numpy array that gave rise to another numpy array

I think I'm missing something obvious. Consider the following code:
import numpy as np
a = np.array([[ 0, 2, 0, 5, 4, 6, 2, 4],
[ 3, 4, 0, 1, 0, 7, 4, 6],
[ 2, 6, 3, 5, 2, 5, 5, 8],
[ 0, 1, 0, 8, 0, 5, 8, 10],
[ 7, 9, 2, 7, 0, 6, 7, 2],
[ 0, 1, 4, 9, 0, 7, 9, 9],
[ 0, 6, 7, 5, 6, 2, 4, 13],
[ 0, 1, 1, 4, 1, 3, 2, 3]]
# isolate columns 2,3,6,7
mask = [False,False, True, True,False,False, True, True]
b = a[:,mask]
# determine rows of b having unique elements
s = np.sort(b, axis=1)
c = b[~(s[:,:-1] == s[:,1:]).any(1)]
c looks like:
c = [[ 0, 5, 2, 4],
[ 0, 1, 4, 6],
[ 7, 5, 4, 13],
[ 1, 4, 2, 3]]
QUESTION: How do I 'recover' the rows of a that gave rise to the rows of c?
The output should be like:
d = [[ 0, 2, 0, 5, 4, 6, 2, 4],
[ 3, 4, 0, 1, 0, 7, 4, 6],
[ 0, 6, 7, 5, 6, 2, 4, 13],
[ 0, 1, 1, 4, 1, 3, 2, 3]]

Doing a loop in mathematica with sneg package

I got a problem getting a loop done in Mathematica while using the SNEG package (it's for calculus with fermionic operators).
Here's a little bit of my code:
natord = {{cjdckd, 1, 2}, {cjdckd, 1, 3}, {cjdckd, 1, 4}, {cjdck, 1,
1}, {cjdck, 1, 2}, {cjdck, 1, 3}, {cjdck, 1, 4},
{cjdckd, 2, 3}, {cjdckd, 2, 4}, {cjdck, 2, 1}, {cjdck, 2,
2}, {cjdck, 2, 3}, {cjdck, 2, 4}, {cjdckd, 3, 4}, {cjdck, 3,
1}, {cjdck, 3, 2}, {cjdck, 3, 3}, {cjdck, 3, 4}, {cjdck, 4,
1}, {cjdck, 4, 2}, {cjdck, 4, 3}, {cjdck, 4, 4}, {cjck, 1,
2}, {cjck, 1, 3}, {cjck, 1, 4}, {cjck, 2, 3}, {cjck, 2, 4}, {cjck,
3, 4}};
Do[
j = natord[[ind, 2]] // ToString;
k = natord[[ind, 3]] // ToString;
cjd = c[0, j];
cj = c[1, j];
ckd = c[0, k];
ck = c[1, k];
cjck = nc[cj, ck];
cjdck = nc[cjd, ck];
cjdckd = nc[cjd, ckd];
korr = natord[[ind, 1]];
After this the following code generates a list of coefficients and one single value.
The list should be appended to a List M, so I have matrix in the end and the single value is going to be in a list so I get a vector.
When I'm not doing a Do-Loop and just saying
j="1";
k="2";
and
korr=cjck;
it somehow this will do it.
As you can see in the natord list, I got 28 cases which I don't want to put in each explicitly but creating a Loop doing this for me.
Hope you can help me. If you need more code tell me.
Cheers
Your code sample doesn't reveal any problem you may be encountering with Append or AppendTo.
m = {};
Do[AppendTo[m, natord[[i]]], {i, Length[natord]}]
m == natord
True

create new dictionary based on two existing dictionaries

I have two dictionaries as:
d={'doc_1': {'hope': 1, 'court': 2}, 'doc_2': {'hope': 1, 'court': 1}, 'doc_3': {'hope': 1, 'mention': 1}}
and
count={'doc_1': 6, 'doc_2': 5, 'doc_3': 12}
All I want to divide the values of nested dictionary of dictionary d with values of dictionary count based on same keys of both dictionaries.
Expected output:-
new={{'doc_1': {'hope': 0.16666666, 'court': 0.3333333}, 'doc_2': {'hope': 0.2, 'court': 0.2}, 'doc_3': {'hope': 0.0833333, 'mention': 0.0833333}}.
What I did so far:
new={}
for k,v in d.items():
for p,q in count.items():
for w,r in v.items():
if k==p:
ratio=r/q
new[k][w]=ratio
That gave me an error!!!
You can use dict comprehension:
from pprint import pprint
d={'doc_1': {'hope': 1, 'court': 2}, 'doc_2': {'hope': 1, 'court': 1}, 'doc_3': {'hope': 1, 'mention': 1}}
count={'doc_1': 6, 'doc_2': 5, 'doc_3': 12}
new_d = {k:{kk:vv/count[k] for kk, vv in v.items()} for k, v in d.items()}
pprint(new_d)
Prints:
{'doc_1': {'court': 0.3333333333333333, 'hope': 0.16666666666666666},
'doc_2': {'court': 0.2, 'hope': 0.2},
'doc_3': {'hope': 0.08333333333333333, 'mention': 0.08333333333333333}}
Regarding your code, the error is generated because you are trying to set a new[k][w] while new[k] doesn't exist. To correct this, you should initialize new[k] as an empty dictionary and then fill it :
new={}
for k,v in d.items():
new[k] = {}
for p,q in count.items():
for w,r in v.items():
if k==p:
ratio=r/q
new[k][w]=ratio
Output
{'doc_1': {'hope': 0.16666666666666666, 'court': 0.3333333333333333},
'doc_2': {'hope': 0.2, 'court': 0.2},
'doc_3': {'hope': 0.08333333333333333, 'mention': 0.08333333333333333}}

Create Numpy Grid in correct order

I have several arrays with points along my x-, y-,...-axes and want to create a grid with all points in it like this:
x = [1, 2, 3]
y = [20, 40, 60, 80]
result = []
for xi in x:
for yi in y:
result.append([xi, yi])
np.array(result)
which gives me
array([[ 1, 20],
[ 1, 40],
[ 1, 60],
[ 1, 80],
[ 2, 20],
[ 2, 40],
[ 2, 60],
[ 2, 80],
[ 3, 20],
[ 3, 40],
[ 3, 60],
[ 3, 80]])
To do this with numpy I found the following code in this question:
np.vstack(np.meshgrid(x, y)).reshape(2, -1).T
But this gives the result in the wrong order:
array([[ 1, 20],
[ 2, 20],
[ 3, 20],
[ 1, 40],
[ 2, 40],
[ 3, 40],
[ 1, 60],
[ 2, 60],
[ 3, 60],
[ 1, 80],
[ 2, 80],
[ 3, 80]])
It goes through the x-values first, then y-values.
I can get around this by using
np.vstack(reversed(np.meshgrid(y, x))).reshape(2, -1).T
but this does not work any more in 3D, where
np.vstack(np.meshgrid(x, y, z)).reshape(3, -1).T
goes through z-values first, then x-values, then y-values.
How can I get the correct order in all dimensions with numpy?
You can specify the matrix indexing ij in np.meshgrid as the indexing parameter to get the reversed order, by default it's the cartesian indexing order xy:
x = [1, 2, 3]
y = [20, 40, 60, 80]
np.stack(np.meshgrid(x, y, indexing='ij'), axis=-1).reshape(-1, 2)
#array([[ 1, 20],
# [ 1, 40],
# [ 1, 60],
# [ 1, 80],
# [ 2, 20],
# [ 2, 40],
# [ 2, 60],
# [ 2, 80],
# [ 3, 20],
# [ 3, 40],
# [ 3, 60],
# [ 3, 80]])
In 3d this could be:
np.stack(np.meshgrid(x, y, z, indexing='ij'), axis=-1).reshape(-1, 3)

Resources