# Addition Information for Python insiders

## Step-by-step Python tutorial

For you to call and use the ProcessMRXS Python class via Python script, this is the step-by-step tutorial:

1. **Clone or download**[ **this** ](https://github.com/mapoferri/process_mrxs)**repository to your local machine/laptop.**
2. **Import the&#x20;**<mark style="color:red;">**`processMRXSData`**</mark>**&#x20;class into your Python script.**

```python
from process_mrxs_data import ProcessMRXSData
```

3. **Create an instance of the&#x20;*****ProcessMRXSData*****&#x20;class by providing paths to your MRXS files in a specific directory and inventory file, as well as the other files necessary to launch the call.**

{% code overflow="wrap" %}

```python
mrxs_directory = "path/to/your/mrxs_files"
inventory_file = "path/to/your/inventory.csv"
output_path = "path/to/your/directory/to/outputs"
output_extension="xlsx/csv" 
#(should choose one of the extension)

processor = ProcessMRXSData.process_directory(mrxs_directory, inventory_file, output_path, output_extension)
```

{% endcode %}

4. **Call the other functions for the rate calculation and relative images:**

<pre class="language-python" data-overflow="wrap"><code class="lang-python"><strong>#Remember to define the output_filename as ENV 
</strong><strong># EXPORT output_filename="whatever/name"
</strong><strong>rate = ProcessMRXSData.process_rate(output_path, output_filename)
</strong>
for file in rate:
    ProcessMRXSData.process_heatmaps(rate)
    ProcessMRXSData.process_scatterplots(rate)
</code></pre>

5. **Check the output images in the output\_path**

***

### Example of workflow\_template script:

In a new file with the extension *<mark style="color:red;">.py</mark>*, you should write:&#x20;

{% code overflow="wrap" %}

```python
import argparse
from process_mrxs_data import ProcessMRXSData

def main():
    parser = argparse.ArgumentParser(description="Run MRXS data processing workflow")
    parser.add_argument("directory_path", help="Path to the directory containing .mrxs files")
    parser.add_argument("inventory_file", help="Path to the inventory file")
    parser.add_argument("output_path", help="Path to the directory containing output files")
    parser.add_argument("output_extension", help="Extension of the final file")
    

    args = parser.parse_args()

    directory_path = args.directory_path
    inventory_file = args.inventory_file
    output_path = args.output_path
    output_ex = args.output_extension
    
    final_data_filename = "final_data" + f".{output_ex}"
    print ("Final data filename", final_data_filename)

    final_data = ProcessMRXSData.process_directory(directory_path, inventory_file, output_path, output_ex)
    final_files = ProcessMRXSData.process_rate(output_path, final_data_filename)

 
    for file in final_files:
        ProcessMRXSData.process_heatmaps(file)
        ProcessMRXSData.process_scatterplots(file)

    # Print the final DataFrame
    #print(f"Final DataFrame saved to {output_filename}")
    print(f"Final Immunopositivity DataFrame saved to {final_data_filename}")
    print(f"Processing complete.")

if __name__ == "__main__":
    main()
```

{% endcode %}

Using this standard file and calling via command line, you should haver in the same directory the resulting file and scatterplots.

#### Just in case:&#x20;

You can call your Python script *<mark style="color:red;">template.py</mark>* on the terminal with this command:&#x20;

```bash
python workflow_template.py path/to/directory/with/mrxs/files /path/to/inventory/file
/path/to/directory/for/output/files xlsx/csv (should choose one of the extension)
```

Or:

{% code overflow="wrap" %}

```bash
python3 workflow_template.py /path/to/directory/with/mrxs/files /path/to/inventory/file /path/to/directory/for/output/files xlsx/csv (should choose one of the extension)
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://disc4all-qupath.gitbook.io/qupath-project/result-analysis-docs/processing-package-tutorial/addition-information-for-python-insiders.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
