Skip to content

Commit 97e3b27

Browse files
Merge pull request #107082 from likebupt/update-upload-files-in-script
update python/r script articles about uploading files using sdk
2 parents 998fb06 + b5f8215 commit 97e3b27

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

articles/machine-learning/algorithm-module-reference/execute-python-script.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.topic: reference
99

1010
author: likebupt
1111
ms.author: keli19
12-
ms.date: 10/22/2019
12+
ms.date: 03/10/2020
1313
---
1414
# Execute Python Script module
1515

@@ -70,7 +70,49 @@ import os
7070
os.system(f"pip install scikit-misc")
7171
```
7272

73-
## How to use
73+
## Upload files
74+
The **Execute Python Script** supports uploading files using [Azure Machine Learning Python SDK](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run%28class%29?view=azure-ml-py#upload-file-name--path-or-stream-).
75+
76+
The following example shows how to upload an image file in the **Execute Python Script** module:
77+
78+
```Python
79+
80+
# The script MUST contain a function named azureml_main
81+
# which is the entry point for this module.
82+
83+
# imports up here can be used to
84+
import pandas as pd
85+
86+
# The entry point function can contain up to two input arguments:
87+
# Param<dataframe1>: a pandas.DataFrame
88+
# Param<dataframe2>: a pandas.DataFrame
89+
def azureml_main(dataframe1 = None, dataframe2 = None):
90+
91+
# Execution logic goes here
92+
print(f'Input pandas.DataFrame #1: {dataframe1}')
93+
94+
from matplotlib import pyplot as plt
95+
plt.plot([1, 2, 3, 4])
96+
plt.ylabel('some numbers')
97+
img_file = "line.png"
98+
plt.savefig(img_file)
99+
100+
from azureml.core import Run
101+
run = Run.get_context(allow_offline=True)
102+
run.upload_file(f"graphics/{img_file}", img_file)
103+
104+
# Return value must be of a sequence of pandas.DataFrame
105+
# E.g.
106+
# - Single return value: return dataframe1,
107+
# - Two return values: return dataframe1, dataframe2
108+
return dataframe1,
109+
}
110+
```
111+
112+
After the pipeline is submitted successfully, you can preview the image in the right panel of the module
113+
![Uploaded-image](media/module/upload-image-in-python-script.png)
114+
115+
## How to configure Execute Python Script
74116

75117
The **Execute Python Script** module contains sample Python code that you can use as a starting point. To configure the **Execute Python Script** module, you provide a set of inputs and Python code to execute in the **Python script** text box.
76118

articles/machine-learning/algorithm-module-reference/execute-r-script.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.topic: reference
99

1010
author: likebupt
1111
ms.author: keli19
12-
ms.date: 11/19/2019
12+
ms.date: 03/10/2020
1313
---
1414

1515
# Execute R Script
@@ -63,11 +63,44 @@ azureml_main <- function(dataframe1, dataframe2){
6363
> [!NOTE]
6464
> Please check if the package already exists before install it to avoid repeat installing. Like ` if(!require(zoo)) install.packages("zoo",repos = "http://cran.us.r-project.org")` in above sample code. Repeat installing may cause web service request timeout.
6565
66+
## Upload files
67+
The **Execute R Script** supports uploading files using Azure Machine Learning R SDK.
68+
69+
The following example shows how to upload an image file in the **Execute R Script**:
70+
```R
71+
72+
# R version: 3.5.1
73+
# The script MUST contain a function named azureml_main
74+
# which is the entry point for this module.
75+
76+
# The entry point function can contain up to two input arguments:
77+
# Param<dataframe1>: a R DataFrame
78+
# Param<dataframe2>: a R DataFrame
79+
azureml_main <- function(dataframe1, dataframe2){
80+
print("R script run.")
81+
82+
# Generate a jpeg graph
83+
img_file_name <- "rect.jpg"
84+
jpeg(file=img_file_name)
85+
example(rect)
86+
dev.off()
87+
88+
upload_files_to_run(names = list(file.path("graphic", img_file_name)), paths=list(img_file_name))
89+
90+
91+
# Return datasets as a Named List
92+
return(list(dataset1=dataframe1, dataset2=dataframe2))
93+
}
94+
```
95+
96+
After the pipeline is submitted successfully, you can preview the image in the right panel of the module
97+
![Uploaded-image](media/module/upload-image-in-r-script.png)
98+
6699
## How to configure Execute R Script
67100

68101
The **Execute R Script** module contains sample code that you can use as a starting point. To configure the **Execute R Script** module, provide a set of inputs and code to execute.
69102

70-
![R-module](media/module/execute-r-script.png)
103+
![R-module](media/module/upload-image-in-r-script.png)
71104

72105
Datasets stored in the designer are automatically converted to an R data frame when loaded with this module.
73106

@@ -183,7 +216,7 @@ azureml_main <- function(dataframe1, dataframe2){
183216

184217
This sample shows how to use a dataset in a ZIP file as an input to the **Execute R Script** module.
185218

186-
1. Create the data file in CSV format, and name it mydatafile.csv.
219+
1. Create the data file in CSV format, and name it "mydatafile.csv".
187220
1. Create a ZIP file and add the CSV file to the archive.
188221
1. Upload the zipped file to your Azure Machine Learning workspace.
189222
1. Connect the resulting dataset to the **ScriptBundle** input of your **Execute R Script** module.
@@ -219,7 +252,7 @@ azureml_main <- function(dataframe1, dataframe2){
219252

220253
You can pass R objects between instances of the **Execute R Script** module by using the internal serialization mechanism. This example assumes that you want to move the R object named `A` between two **Execute R Script** modules.
221254

222-
1. Add the first **Execute R Script** module to your pipeline, and type the following code in the **R Script** text box to create a serialized object `A` as a column in the modules output Data Table:
255+
1. Add the first **Execute R Script** module to your pipeline, and type the following code in the **R Script** text box to create a serialized object `A` as a column in the module's output Data Table:
223256

224257
```R
225258
azureml_main <- function(dataframe1, dataframe2){
134 KB
Loading
381 KB
Loading

0 commit comments

Comments
 (0)