Message Boards Message Boards

Why a Histogram is not returned properly as image with CloudDeploy?

GROUPS:

In Notebook the below code runs properly

myDist[Latitude_, Longitude_] := 
 EstimatedDistribution[
  WeatherData[{Latitude, Longitude}, 
   "MaxWindSpeed", {{1990}, {2015}, "Year"}], 
  GumbelDistribution[\[Alpha], \[Beta]], 
  ParameterEstimator -> "MaximumLikelihood", WorkingPrecision -> 25]

URLExecute[CloudDeploy[api], {"Latitude" -> 40, "Longitude" -> 18}]

and the result is look like this

enter image description here

But I need to deploy to the cloud, and after CloudDeploy and APIFunction used the respond is sometimes only the data set instead of the PNG image with the histogram. Even if I used the same coordinates as input. (i.e 40,18)

enter image description here

POSTED BY: Balazs Kisfali
Answer
1 year ago

Short Answer:

You have to tell APIFunction or whatever you are using to make an Image by specifying what format it should be. Take a look at this code from the documentation:

CloudDeploy[
 APIFunction["country" -> "Country", 
  GeoGraphics[Polygon[#country]] &, "PNG"]]

The last argument to APIFunction is "PNG", telling it to render the output as a PNG.

POSTED BY: Sean Clarke
Answer
1 year ago

Hi Sean, Thanks for your answer. Actually I am doing on that way, so the argument is "PNG" Please see the whole code below.

myDist[Latitude_, Longitude_] := 
 EstimatedDistribution[
  WeatherData[{Latitude, Longitude}, 
   "MaxWindSpeed", {{1990}, {2015}, "Year"}], 
  GumbelDistribution[\[Alpha], \[Beta]], 
  ParameterEstimator -> "MaximumLikelihood", WorkingPrecision -> 25]

CloudDeploy[
 APIFunction[{"Latitude" -> "Integer", "Longitude" -> "Integer"}, 
  Show[Histogram[
     WeatherData[{#Latitude, #Longitude}, 
      "MaxWindSpeed", {{1990}, {2015}, "Year"}], 6, 
     "ProbabilityDensity", ChartStyle -> "Aquamarine", 
     PlotRange -> All], 
    Plot[PDF[myDist[{#Latitude, #Longitude}], 
       Quantity[x, "Kilometers"/"Hours"]] // Evaluate, {x, 0, 200}, 
     PlotRange -> {Automatic, All}, 
     PlotStyle -> {Blend[{Blue, Black}], Thick}]] &, "PNG"], 
 Permissions -> "Public"]

However, it doesn't work properly. In some calls is ok but if I call again then the above result can be seen. Can be something with the function itself? What is also strange that in Mathematica notebook has similar output sometimes, after evaluate notebook.

I copy the link here below from the above code. I made it public, so you can test it yourself as well. (at the moment it works with integers) https://www.wolframcloud.com/objects/5cc0a503-f381-4120-af24-89d4282989f9

POSTED BY: Balazs Kisfali
Answer
1 year ago

Some things you probably want to know.

Plotting functions do not actually return images. They output some code. This code is then interpreted by the front end as a graphic that you can see. That is, it returns a kind of vector graphic, like SVG which more people are familiar with.

You can see what the underlying code for a graphic looks like by wrapping it in the function "InputForm".

POSTED BY: Sean Clarke
Answer
1 year ago

I am seeing a similar problem, I deployed a FormPage and the response seems always to be code. I don't know how to apply the "PNG" trick from above to a FormPage though.

FormPage[
 {{"zips", "Zip Codes"} -> 
   Restricted[
     DelimitedSequence[
      Restricted["String", RegularExpression["\\d\\d\\d\\d?\\d?"]], 
      ","], {2, 1000}] -> 
    StringRiffle[
     Map[(#["Name"]) &, GeoEntities[$GeoLocation, "ZIPCode"]], ","], 
  {"property", "Report"} -> Restricted["String", 
    {{"AverageHouseValue", "MedianHouseholdIncome", 
      "PersonsPerHousehold", "PopulationDensity"}}]}, 
 (GeoRegionValuePlot[
    Module[{p = #property}, 
     Map[(Module[{z = ZIPCodeData[#]}, z -> z[p]]) &, #zips]],
    PlotLabel -> #property, 
    MissingStyle -> Directive[Green, EdgeForm[Dashed]],
    GeoLabels -> (Tooltip[#1, #2] &)]) &, 
 AppearanceRules -> <|"Title" -> "ZIP Code Demographics v2", 
   "Description" -> 
    "Enter two or more comma-separated Zip Codes to compare \
demographic data."|>]

The output from the page starts

Legended[GeoGraphics[Graphics[{{Annotation[{Texture[Image[RawArray["UnsignedInteger8", {{{158, 158, 158, 
POSTED BY: Simon Johnston
Answer
2 months ago

Group Abstract Group Abstract