Image as a Repeating Element in Mathematica - image

I have been working with FormFunction in Mathematica and the function I created takes an image as an input. I am hoping to be able to input multiple images. I found this RepeatingElement example that works for Strings but returns an error when I change to image. When I get rid of the parameters there is no error, but it still will not work.
FormFunction["field" -> RepeatingElement["String", {2, {1, 5}}]]
FormFunction["field" -> RepeatingElement["Image", {2, {1, 5}}]]

This should work.
CloudDeploy[FormFunction[{"image" -> RepeatingElement["Image"]}, #image &], Permissions -> "Public"]

Related

ListLinePlot function in mathematica

I wish to plot a vector with the help of ListLinePlot function in mathematica. I want to change the scale of the Y axis in it. I cannot find a way in the documentation. The vector that I wish to plot is given below
{{1, 0}, {1., 0.00404858}, {1.00001, 0.00809717}, {1.00002,
0.0121457}, {1.00003, 0.0161943}, {1.00005, 0.0202429}, {1.00007,
0.0242915}, {1.00009, 0.0283401}, {1.00012, 0.0323887}, {1.00015,
0.0364372}, {1.00018, 0.0404858}, {1.00022, 0.0445344}, {1.00027,
0.048583}, {1.00031, 0.0526316}, {1.00036, 0.0566802}, {1.00042,
0.0607287}, {1.00047, 0.0647773}, {1.00053, 0.0688259}, {1.0006,
0.0728745}, {1.00067, 0.0769231}, {1.00074, 0.0809717}, {1.00081,
0.0850202}, {1.00089, 0.0890688}, {1.00098, 0.0931174}, {1.00106,
0.097166}, {1.00115, 0.101215}, {1.00125, 0.105263}, {1.00134,
0.109312}, {1.00145, 0.11336}, {1.00155, 0.117409}, {1.00166,
0.121457}, {1.00177, 0.125506}, {1.00189, 0.129555}, {1.00201,
0.133603}, {1.00213, 0.137652}, {1.00226, 0.1417}, {1.00239,
0.145749}, {1.00252, 0.149798}, {1.00266, 0.153846}, {1.0028,
0.157895}, {1.00295, 0.161943}, {1.0031, 0.165992}, {1.00325,
0.17004}, {1.00341, 0.174089}, {1.00357, 0.178138}, {1.00373,
0.182186}, {1.0039, 0.186235}, {1.00407, 0.190283}, {1.00424,
0.194332}, {1.00442, 0.198381}, {1.0046, 0.202429}, {1.00479,
0.206478}, {1.00498, 0.210526}, {1.00517, 0.214575}, {1.00537,
0.218623}, {1.00557, 0.222672}, {1.00577, 0.226721}, {1.00598,
0.230769}, {1.00619, 0.234818}, {1.0064, 0.238866}, {1.00662,
0.242915}, {1.00684, 0.246964}, {1.00707, 0.251012}, {1.0073,
0.255061}, {1.00753, 0.259109}, {1.00776, 0.263158}, {1.008,
0.267206}, {1.00825, 0.271255}, {1.00849, 0.275304}, {1.00874,
0.279352}, {1.009, 0.283401}, {1.00926, 0.287449}, {1.00952,
0.291498}, {1.00978, 0.295547}, {1.01005, 0.299595}}
It is actually a list of lists. when I plot it, the scale on the Y-axis takes an increment of 0.05. I wish to make it 0.2. Also I want some more of the X axis to be shown, i.e from 0.99 to 1.01. Currently it shows only from 1.00 to 1.01.!!
Like this?
ListLinePlot[data, PlotRange -> {{.99, 1.01}, All}, Ticks -> {Automatic, {0., .2}}]

how to apply AccountingForm to TableView without affecting the row/column labels?

I am using M 8.0.4.
I am use TableView in demo Manipulate[] to display final solution data, which is a matrix of numerical data.
TableView is very nice to use, since it comes with an automatic scroll bars to move down the rows and to the right over the columns, so that one can see the whole matrix, while keeping the overall display area fixed. (I have an image at the bottom)
The problem that I am not able to find a solution for, is that I need to format the matrix data, so that it looks nice. Otherwise, if the a data element is too large to fit in a cell, it will wrap around, making the TableView misaligned. I also need to adjust decimal point an such other formatting.
I can't apply NumberForm nor AccountForm to the data, and then apply TableView on the result, because TableView does not like to see those wrappers, it needs the numbers.
But if apply the AccountingForm to the TableView, then the numbers that represent the row numbers and column numbers, which are added by TableView automatically, get formatted as well. And I do not want those formatted as well. A row number should remain an integer, not floating point. I just want the data itself formatted.
I could not figure how to format the data from inside Table view. When I use FullForm to see how the data is kept inside TableView, I could not figure how to format it without breaking TableView.
I'll show the problem, then show what I tried.
(* make up some data, and make a TableView of it *)
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}}
tbl = TableView[data, ItemSize -> {{3, {8}}}, DefaultBaseStyle -> 11,
Spacings -> {.2, .1}, FrameStyle -> Gray]
Notice the row numbers and columns number (marked) are positive integers.
Now, I wanted to format the data itself, (the exact formatting options used below is just an example)
AccountingForm[tbl, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True]
But now the Table rows and columns are also formatted:
The FullForm of TableView is:
In[156]:= FullForm[tbl]
TableView[List[List[-1234.8`,0.123`],List[0.12345678`,0.1234`]],
Rule[ItemSize,List[List[3,List[8]]]],
Rule[DefaultBaseStyle,11],Rule[Spacings,List[0.2`,0.1`]],
Rule[FrameStyle,GrayLevel[0.5`]]]
So the data in TableView can be pulled out using
In[166]:= tbl[[1]]
Out[166]= {{-1234.8,0.123},{0.12345678,0.1234}}
But when I change tbl[[1]], using ReplacePart[], with an AccountingForm version of the data, TableView no longer works:
formattedData =
AccountingForm[data, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", "0"}, SignPadding -> True];
In[245]:= tbl=ReplacePart[tbl,1->formatted]
Out[245]= TableView[{{-1234.8,+0.123},{+0.123,+0.123}},
ItemSize->{{3,{8}}},DefaultBaseStyle->11,Spacings->{0.2,0.1},
FrameStyle->GrayLevel[0.5]]
So, I broke TableView. Since it does not display any more.
Question is: How to format numeric data that goes into TableView without affecting the row/column index values?
Fyi, this is how the TableView looks inside one Manipulate demo I have. Notice the automatics scroll-bars. (In this one, I am not using NumberForm and its friends to do the formatting. But what I did is not very efficient, I'd rather use NumberForm if I can, hence my question)
thanks
update 12/22/11 1:30 AM
Here is a complete code example for Mike to follow up on his answer below
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}};
newData =
Map[AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &, data, {2}];
tbl = TableView[newData, ItemSize -> {{3, {8}}}, Spacings -> {.2, .1},
FrameStyle -> Gray]
Now how exactly do I use the Cell command for the above? (This is in Manipulate, not a notebook session, Manipulate runs all in one cell. I can't make separate cells and such in this code. But for trying it, I can in a new notebook, but the actual code that will use this solution, has to run in a Manipulate with no Cells.
Update 12/22/11 5 am
I am noticing some not good performance of TableView. Consider this code
Remove["Global`*"];
ClearSystemCache[]
m1 = MemoryInUse[$FrontEnd];
N[m1/10^6]
n = 256;
data = Table[RandomReal[], {n}, {n}];
TableView[data, ContentSize -> {300, 300}]
m2 = MemoryInUse[$FrontEnd] - m1;
N[m2/10^6]
The data itself, assuming double for the reals, is about half MB only. ByteCount says
2 MB due to other bookkeeping data struct.
In[114]:= ByteCount[data]/10^6//N
Out[114]= 2.10948
But Front end seems to use much more RAM (for the whole TableView I mean not just the data), sometimes I get 20 MB and sometimes much more (got 100 MB at one time). But if you try the above on your computer, you'll notice M is having hard time with this. I think it might be the rendering of the table part that causes M to take so much time.
I do not think 256 by 256 is such a large matrix. Even with 128 by 128, it was having hard time to render it on the screen. Once it is up, then it is fast and no problem using it.
It looks like TableView is not yet optimized well. I think I will only use it to display small part of the solution Matrix as the performance is not good to use it in a demo to display the whole solution, it will make the demo look bad.
The problem is that my solution matrix can be large, and I wanted a away to display the data in limited amount of GUI space. TableView is all what I can find that comes with scrollbars build-in. Nothing else in the Manipulate controls has something like this to use, so I have to use TableView.
Update 12/22/11 4 PM
Thanks for the hint by Mike below, I am now looking at using Pane with Scollbars, and it is working better than TableView. (still little slow with large data, but not as bad as TableView)
Here is a code example of doing that:
n = 32;
data = Table[RandomReal[], {n}, {n}];
grid = Grid[data, Spacings -> {.4, .4}, Alignment -> Left, Frame -> All];
grid = Style[NumberForm[grid, {6, 5}], LineBreakWithin -> False];
pane = Pane[grid, ImageSize -> {200}, Scrollbars -> True]
Only problem I noticed so far is that when n gets large, Pane generates this outer frame saying very large output was generated. But I am sure there is a programmatic way (option somewhere) to turn that effect off, I just started looking at this and should be able to find a way around this once I have my coffee. (fingers crossed)
good news
The "Very large output was generated" ONLY showed up when in notebook interface. When I put the code in a Manipulate and and run it, it did not show this output frame. So, I am happy now and this issue is closed. I'll use Pane from now on. (Thanks again to Mike)
btw, to remove the message in question, here is a useful link I found:
http://forums.wolfram.com/mathgroup/archive/2009/Apr/msg00935.html
but I did not need to do anything for what I am doing.
I guess this is a different enough method to post as another answer, though it clearly borrows from Mike's answer.
f = ToString #
AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &;
data = {{-1234.8, 0.123}, {0.12345678, 0.1234}};
TableView[Map[f, data, {2}], ItemSize -> {{3, {8}}},
DefaultBaseStyle -> 11, Spacings -> {.2, .1}, FrameStyle -> Gray]
Since using ToString was non-intuitive, I decided to explore it a bit more. It seems that TableView suppresses quotation marks in strings, but only bare strings. e.g.:
string = "\"\"\"test\"";
TableView[{{ string }}]
TableView[{{ {string} }}]
Other interesting behavior can be seen with 2D formatted strings:
string = "\!\(\*SuperscriptBox[SqrtBox[\"5\"], \"2\"]\)";
TableView[{{ string }}]
TableView[{{ {string} }}]
One suggestion:
Firstly make each element in your data into AccountingForm, or whatever Form you want:
f = AccountingForm[#, {6, 3}, NumberSigns -> {"-", "+"},
NumberPadding -> {"", ""}, SignPadding -> True] &
newdata = {f[#[[1]]], f[#[[2]]]} & /# data
Note that I've had a bit of a brain fade doing this. I'm sure there is an easier way than Mapping (Edit: the brain fade made me forget levelspec). This will give you output like this:
The problem of course is that you now have string characters showing and "conventional" attempts to switch this off failed ("conventional" == options inspector, Style[] etc.). But you can fix this by modifying two styles that exist in the Core.nb stylesheet. So you will need to edit the private styles in your current notebook -- and also remove DefaultBaseStyle from your code because it will cause conflicts with the stylesheet style. So Format > Edit Stylesheet and add this style:
Cell[StyleData["TableViewItemExpression"],
ShowStringCharacters->False,
TemplateBoxOptions->{DisplayFunction->(StyleBox[#, Deployed -> False]& )}]
Actually that is really all you need to do but if you subsequently wanted to do some additional styling of the numbers then you need to modify a different style. Here is an example making the numbers red:
Cell[StyleData["TableViewGrid"],
ContextMenu->FEPrivate`FrontEndResource["ContextMenus", "TableView"],
ShowAutoStyles->False,
AutoSpacing->False,
ShowStringCharacters->False,
AutoNumberFormatting->True,
LineIndent->0,
FontWeight->"Plain",
FontColor->RGBColor[1, 0, 0]]
And this is what it looks like:

How to modify OUTPUT font type?

Is it possible to change the OUTPUT font type instead of the default one? How?
This is my default stylesheet: http://filefactory.com/file/cfc2cb0/n/blueOutput.nb
Thanks!
The problem lies in StandardForm not respecting the FontFamily option, although it does seem to respect most other font options. Sjoerd's answer used TraditionalForm output and thus worked. You can see this problem if you run
SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Output"],
FontColor -> RGBColor[0, 0, .5], FontSize -> 14,
FontFamily -> "Symbol", FontWeight -> "Bold"]}]]
Then compare
{1 + 1, "abc", Sin[x]} (* This is by default in StandardForm *)
{1 + 1, "abc", Sin[x]} // StandardForm
{1 + 1, "abc", Sin[x]} // OutputForm
{1 + 1, "abc", Sin[x]} // TraditionalForm
You can also look at
Dynamic[CurrentValue/#{FontFamily, FontWeight, FontSize}]
Dynamic[CurrentValue/#{FontFamily, FontWeight, FontSize}] // TraditionalForm
which shows that the CurrentValue of FontFamily "seen" in the output depends on the output format.
Unfortunately, I don't see how to get around this issue...
Just go to the Format > Edit Stylesheet... menu. Then in the private style definitions sheet that pops-up choose 'Output' from the pull-down menu and change the looks of the resulting Output cell. This stylesheet will be stored with your open notebook.
In light of Simon's answer, you could force output printing in a certain style using $PrePrint.
$PrePrint = Style[#, FontFamily -> "Symbol"] &;
{1 + 1, "abc", Sin[x]}
You can do this by redefining the StandardForm style which is used for Output style by default (see the DefaultFormatType option in the Output style):
SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["StandardForm"],
FontFamily -> "Palatino Linotype"]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]
But Input style in this case is also affected because it is based on the StandardForm style too...
You could try wrapping your inputs using the Style[] command. For example:
test="This is a test string.";
Style[test,{Red,"Title"}]
This generates the string in my style sheet's 'title' settings in the colour red. The solution of changing your Stylesheets is obviously preferable to this, but this might be a quick and dirty temporary workaround.

Getting CellDingbat to remember its state between Mathematica sessions

I have modified my notebook's stylesheet to include a StyleData["Todo"] that inherits from StyleData["Item"]. It changes the cell dingbat to a checkbox. In the stylesheet editor:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat->DynamicModuleBox[{$CellContext`color$$},
CheckboxBox[
Dynamic[$CellContext`color$$], {RGBColor[1, 0.5, 0],RGBColor[0,Rational[2, 3], 0]},
Background -> Dynamic[$CellContext`color$$]],
DynamicModuleValues :> {}
],
]
The problem is that the state of the checkbox, when used in a notebook, is not saved between Mathematica sessions. I thought the DynamicModule[] would do the trick. How do I get the checkbox to remember its state?
EDIT
Simon's solution does save the state of the checkbox, but the checkbox is clipped when used as a CellDingbat (MacOS X). Putting Simon's code in a CellFrameLabels options does the trick, and also keeps the default "Item" CellDingbat. Here is what I've gone with:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellFrameLabels->{{
ButtonBox[
CheckboxBox[False], ButtonFunction :> (SelectionMove[
ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[
NotebookSelection[
ButtonNotebook[]], CellFrameLabels], CheckboxBox[
Pattern[$CellContext`x,
Alternatives[True, False]]] :> CheckboxBox[
Not[$CellContext`x]]]},
SetOptions[
NotebookSelection[
ButtonNotebook[]], $CellContext`new]]; SelectionMove[
ButtonNotebook[], After, CellContents]), Appearance -> None,
Method -> "Preemptive", Evaluator -> Automatic], None}, {
None, None}},
MenuSortingValue->1621]
The problem with your code (I think) is that a new DynamicModule does not get created each time you create a new "ToDo" cell. So there is nowhere that the state of each Checkbox can get saved.
The simplest solution I could think of for storing the state of the Checkbox for each "ToDo" cell is to overwrite the CellDingbat the first time that the Checkbox is activated.
(Other options I played with were using TaggingRules,
toggling between "ToDo" and "ToDone" styles, etc...)
However, even a plain Checkbox in a CellDingbat does not store its state - try running the following then cycle the output through a Show Expression cycle.
CellPrint[Cell["test", "Text", CellDingbat -> ToBoxes[Checkbox[]]]]
To get around this, I used Checkbox with the definite argument True or False wrapped up in a button that changes the state. This is stupid and inefficient, but it works!
So, my code for the cell style
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat -> ButtonBox[CheckboxBox[False],
ButtonFunction :> (SelectionMove[ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[NotebookSelection[ButtonNotebook[]], CellDingbat],
CheckboxBox[Pattern[$CellContext`x, Alternatives[True, False]]] :> CheckboxBox[Not[$CellContext`x]]]},
SetOptions[NotebookSelection[ButtonNotebook[]], $CellContext`new]];
SelectionMove[ButtonNotebook[], After, CellContents]),
Appearance -> None, Method -> "Preemptive", Evaluator -> Automatic]]
I'm not happy with this solution, but it's the best I've come up with. An improvement would be to move the button function code out of the cell so that it is not repeated for every checked ToDo cell. Also to make it run without a ReplaceAll so that the kernel is not needed and the function can be run using just the frontend.

question on dynamic, SynchronousUpdating, inside Manipulate

I am trying to see if I can make my own simulation loop inside Manipulate as I am not happy with either the Trigger control or using Tasks (both have problems and limitations for what I'd like to do).
So, I was trying to see if I can make my own control loop, this way I have better control of things, where by clicking on a 'run' button, simulation loop will start until a 'stop' button is clicked.
A basic problem is that Manipulate times out after 5 seconds even though I am using SynchronousUpdating -> False. i.e. when I click the 'run' button, I start a loop (with some Pause[] in it of course), and will then update something in the loop. This works fine, but after 5 seconds, the loop stops on its own, since Manipulate decided to time out.
I might be misunderstanding something basic here. I show below simple example:
Manipulate[
Dynamic[Refresh[Text#x,TrackedSymbols->{x}]],
{{x,0},ControlType->None},
{{running,True},ControlType->None},
Button[Text["play"],
{
running=True,
While[running,
x+=1;
FinishDynamic[];
Pause[0.1]
]
}],
Button[Text["stop"],
running=False
],
TrackedSymbols->{None},
SynchronousUpdating->False,
SynchronousInitialization->False
]
When running the above, it always stops around count 58 or so, which is about 5 seconds, the time-out value for Manipulate
Outside Manipulate, it works ok as expected:
x = 0;
Dynamic[Refresh[x, UpdateInterval -> 0.001]]
Do[
(
x += 1;
FinishDynamic[];
Print[x];
Pause[0.01]
), {i, 0, 200}
]
I can make the count above as large as I want, no problem.
So, it seems a configuration option for Manipulate, but I am now not able to find now which option it is I need to use for this to work.
Thanks
Update
Using the Method->"Queued" as given below by Simon, now the loop works. But there are problems with this method: I can not use Mathematica while the Button is running, even with large Pauses in the loop, as it blocks the whole front end. It behaves as if the button is pressed all the time. So, this idea is out of question to use. Well, it was something to try.
btw, this is a good time to mention this, I found that using cell of type 'code' instead of the default 'input' causes many crashes in the kernel. Just tried cell type 'code' and after few clicks on the button, kernel crashed:
So I no longer use cells of type 'code'.
Back to the drawing board.
Update 2: 8/29/11, 6 PM
Using Mathematica 8.0.1, on windows 7, SP1, intel pc, here is the code that crashes it when using "code" cell
Manipulate[
Dynamic[Refresh[Text#x,TrackedSymbols->{x},UpdateInterval->0.005]],
{{x,0},ControlType->None},
{{running,True},ControlType->None},
Button[Text["play"],
{
running=True,
While[running,
x+=1;
FinishDynamic[];
]
},Method->"Queued"],
Button[Text["stop"],
running=False
],
TrackedSymbols->{None},
SynchronousUpdating->False,
SynchronousInitialization->False
]
May be someone can try the above? Had to click on start/stop few times to get it to crash.
I can reproduce this.
Update 9/2/11
on new answer: It looks Simon version (second one below) seems faster on my PC, Mathematica 8.0.1. I started both at the same time, and the Simon version seems to run faster (counter runs faster).
screen shot:
I think that it's actually the Button that is timing out, not the Manipulate.
To quote the Options > Method section of the Button docs,
By default, button functions are evaluated on a preemptive link, which
times out after 5 seconds:
Set the option Method -> "Queued" for the button and everything works as expected.
You might get better results if you let Manipulate control the "looping":
Manipulate[
If[running, x++, x]
, {{x, 0}, ControlType -> None}
, {{running, True}, ControlType -> None}
, Button["play", running = True]
, Button["stop", running = False]
]
I presume that Manipulate is being used here to support further controls within the real application. If not, then DynamicModule would be sufficient.
DynamicModule[{x = 0, running = True}
, Column[
{ Button["play", running = True]
, Button["stop", running = False]
, Dynamic[If[running, x++, x]]
}
]
]
The following example animates a moving disk using this technique:
DynamicModule[{t = 0, running = True}
, Column[
{ Button["play", running = True]
, Button["stop", running = False]
, Dynamic[
If[running, t++, t] /.
t_ :> Graphics[Disk[{Cos[t/10], Sin[t/10]}]
, PlotRange -> {{-3,3},{-3,3}}
, Axes -> True
]
]
}
]
]

Resources