Data analysis#
Many LLMs are excellent data scientists. Just a reminder: it is important to specify clearly what code should do.
Give my Python code for iterating over all files in a folder.
If the file is a .tif image, open it and show it.
To iterate over all files in a folder, open each .tif
image, and display it, you can use the following Python code. This code uses the os
module to navigate the directory and the PIL
(Pillow) library to open and display images. Make sure you have the Pillow library installed (pip install Pillow
) before running this code:
import os
from PIL import Image
def show_tif_images_in_folder(folder_path):
# List all files in the given folder
for filename in os.listdir(folder_path):
# Get the full file path
file_path = os.path.join(folder_path, filename)
# Check if the current file is a .tif file
if filename.lower().endswith('.tif'):
try:
# Open and display the .tif file
with Image.open(file_path) as img:
img.show()
except IOError as e:
print(f"Cannot open {file_path}: {e}")
# Replace 'your_folder_path_here' with the path to your folder containing .tif images
folder_path = 'your_folder_path_here'
show_tif_images_in_folder(folder_path)
Notes:
Folder Path: Replace
'your_folder_path_here'
with the actual path to the folder containing your.tif
images.Error Handling: The code includes error handling to skip files that cannot be opened as images. This prevents the program from crashing if it encounters a corrupted file or a non-readable .tif.
Case Insensitivity: The check for the
.tif
extension is case insensitive (.tif
vs..TIF
).
Make sure the folder path is correctly set and validated according to your filesystem.
Chat-interaction for modifying code#
LLM platforms commonly come as chat-bots, and thus, we can refer back to formerly generated code and ask to modify the code according to our needs.
Could you also write code that does not use a function?
Certainly! Below is a Python script that iterates over all files in a specified folder, checks if each file is a .tif
image, and opens it for display. This script does not encapsulate the logic within a function:
import os
from PIL import Image
import matplotlib.pyplot as plt
# Specify the directory containing the files
directory = '/path/to/your/directory'
# Iterate over all files in the specified directory
for filename in os.listdir(directory):
# Construct the full file path
file_path = os.path.join(directory, filename)
# Check if the file is a .tif image
if os.path.isfile(file_path) and filename.lower().endswith('.tif'):
# Open the .tif image
image = Image.open(file_path)
# Display the image using matplotlib
plt.imshow(image)
plt.title(f"Displaying: {filename}")
plt.axis('off') # Hide axes
plt.show()
# Alternatively, you can display using PIL's show method:
# image.show()
Instructions:
Replace
'/path/to/your/directory'
with the actual path of the folder you want to iterate over.Make sure that both the
PIL
(Pillow) library andmatplotlib
are installed in your Python environment. You can install them usingpip install Pillow matplotlib
if needed.The
plt.show()
line will open each.tif
image one at a time. If you have a large number of images to display, you may want to include a mechanism to close the plot window or process in batches.
This code goes through each file in the specified directory, checks if the file extension is .tif
(case-insensitive), and displays the image accordingly.
Asking to explain code#
LLMs open the door for non-programmers to write code. It is extremely important to make sure that code does what it is supposed to do. Hence, if we find code which we don’t understand, we can ask the LLM to explain it to us.
What does the try: ... except: ... do in the first code example?
In the initial code example, the try: ... except: ...
block is used to handle any potential errors that may occur when attempting to open and display an image file. Here’s a more detailed explanation:
try:
block: This block contains the code that might raise an exception. In the context of your task, this involves opening an image file using theImage.open()
function and attempting to display it with theshow()
method. If everything goes well, the code in this block executes without any issues.except:
block: If an exception occurs in thetry:
block, control is immediately transferred to theexcept:
block. Theexcept:
block defines what should be done if an error occurs. In the context of opening and showing an image, common exceptions might include file corruption, issues with file permissions, or the file not being a valid image. By catching these exceptions, your code can handle the error gracefully (e.g., by printing an error message) instead of crashing.
Using a try: ... except: ...
block is a way to make the code more robust and user-friendly, ensuring that errors are handled appropriately without stopping the entire script execution.
Exercise#
Ask an LLM / Chatbot to generate Python code for:
loading a .csv file of your choice (or this one) as DataFrame
compute descriptive statistics of columns, e.g. mean and standard deviation.
plot two numeric columns against each other using seaborn.
Optional:
Ask the LLM what a DataFrame is.
Ask the LLM what seaborn is.
Ask the LLM modify the plot, e.g. change the colours of data points.