This report can be reproduced from the underlying RMarkdown code in multiple formats. See the wiki pages on literate programming paradigm of Donald Knuth, the Notebook interface, and Markdown as well as RStudio’s page on RMarkdown for more information.
Such R Markdown allows for coding using multiple programming
languages can in some cases can share memory resources, as is the case
between R and Python. To achieve this, the reticulate
package is loaded into memory. In addition, a function called
conversation_pretty_print is loaded from a local library
with restricted accessibility.
The first job for the python interpreter is to import the python
OpenAI object from the openai module. Also the
load_dotenv function is imported from the dotenv
module. The latter is used to extract information from the
.env file located in the root of the RStudio project.
import os
import json
import base64
from openai import OpenAI
from dotenv import load_dotenv
bin = load_dotenv()
client = OpenAI(organization = os.getenv('ORGANIZATION'))We can now explore the models available (depends on account authorizations linked to bearer token):
## [1] "babbage-002" "dall-e-2"
## [3] "dall-e-3" "davinci-002"
## [5] "gpt-3.5-turbo" "gpt-3.5-turbo-0125"
## [7] "gpt-3.5-turbo-0301" "gpt-3.5-turbo-0613"
## [9] "gpt-3.5-turbo-1106" "gpt-3.5-turbo-16k"
## [11] "gpt-3.5-turbo-16k-0613" "gpt-3.5-turbo-instruct"
## [13] "gpt-3.5-turbo-instruct-0914" "gpt-4"
## [15] "gpt-4-0125-preview" "gpt-4-0613"
## [17] "gpt-4-1106-preview" "gpt-4-1106-vision-preview"
## [19] "gpt-4-turbo" "gpt-4-turbo-2024-04-09"
## [21] "gpt-4-turbo-preview" "gpt-4-vision-preview"
## [23] "gpt-4o" "gpt-4o-2024-05-13"
## [25] "text-embedding-3-large" "text-embedding-3-small"
## [27] "text-embedding-ada-002" "tts-1"
## [29] "tts-1-1106" "tts-1-hd"
## [31] "tts-1-hd-1106" "whisper-1"
Here is the first image of the series we are going to work with:
The image is taken from the set of sample images presented by the Open Microscopy Environment Consortium (OME) and originally derived from the public MitoCheck data set and consists in the time-lapse of cell-division. The time-lapse for well A-1 of plate LT0001_02 was converted into a single OME-TIFF using Bio-Formats 5.2.4 (See source). This file is licensed under a Creative Commons Attribution 4.0 International license, which is available here. See the accompanying publication of Neumann et al. (2010) for more information (Neumann et al. 2010).
gpt-4-vision-preview modelWe will now start a conversation with an AI assistant of the
gpt-4-vision-preview model. Using the API allows us to
upload images and to parametrize the model and provide guides that set
the personality and level of experience of the AI assistant. The code
below guides you trough the conversation. As the calling of the API is
slow and costly, we cache the so-called completions (i.e. answers of the
AI assistant) individually into JSON dump files in the out sub-folder of
this project. To reset the completions, simply delete the appropriate
completion-##.json files and knit the report again. Notice
how R and Python object are being exchanged.
messages = list(
list(
role = "system",
content = "You are an experienced microscopy expert specialized " +
"in image analysis."),
list(
role = "user",
content = list(
list(
type = "text",
text = "Attached you find an fluorescence microscopy image of Hela " +
"cells with their core histone 2B proteins tagged with GFP to " +
"monitor chromosomes. Can you see the spots?"),
list(
type = "image_url",
image_url = list(
url = "https://idr.openmicroscopy.org/webclient/render_image/" +
"1483351/0/0/?c=1|14:200$808080&m=g&p=normal&ia=0&maps=" +
"[{%22inverted%22:{%22enabled%22:false}}]")))))def get_completion(file_name):
data_loc = {}
try:
with open(file_name, "r") as f:
data_loc = json.load(f)
except IOError:
pass
if data_loc == {}:
completion = client.chat.completions.create(
model = "gpt-4-vision-preview",
messages = r.messages,
max_tokens = 1000
)
data_loc = completion.choices[0].message
data_loc = {"role": data_loc.role, "content": data_loc.content}
with open(file_name, "w") as f:
json.dump(data_loc, f)
return data_loc
data = get_completion("out/completion-11.json")messages <- messages %>% append(
list(list(role = py$data$role,
content = py$data$content))) %>% append(list(
list(
role = "user",
content = list(
list(
type = "text",
text = "Some spots are fainter and blurrier than others, " +
"but how many spots do you count approximately?")))))messages <- messages %>% append(
list(list(role = py$data$role,
content = py$data$content))) %>% append(list(
list(
role = "user",
content = list(
list(
type = "text",
text = "Thank you! Would you be able to provide the XY " +
"coordinates of at least some of the spots?")))))messages <- messages %>% append(
list(list(role = py$data$role,
content = py$data$content)))
messages %>% conversation_pretty_print %>% catSo unfortunately, where in the past I was able to have this model help me analyse graphs, this does not seem possible for microscopy images. Below, I will try for the assistant to ignore the fact that the image is a microscopy image.
base64_image <- base64_encode("in/00001_01.ome_01.png")
messages = list(
list(
role = "system",
content = "You are an experienced microscopy expert specialized " +
"in image analysis."),
list(
role = "user",
content = list(
list(
type = "text",
text = "Attached you find an image that shows a number of pale " +
"blue spots. Can you see the spots?"),
list(
type = "image_url",
image_url = list(
url = "data:image/png;base64,{" + base64_image + "}")))))messages <- messages %>% append(
list(list(role = py$data$role,
content = py$data$content))) %>% append(list(
list(
role = "user",
content = list(
list(
type = "text",
text = "Do not mind about the interpretation behind the spots, " +
"but could you attempt to provide the XY-coordinates at least " +
"some of the spots?")))))messages <- messages %>% append(list(list(role = py$data$role,
content = py$data$content)))
messages %>% conversation_pretty_print %>% catAgain, no luck with actual analysis. but let us see below to what extend the assistant is still available for more general interpretation.
We will now try to have the AI assistant interpret a publiched figure instead. From this publication, we use this image:
base64_image <- base64_encode("in/es3c07195_0002.jpeg")
messages = list(
list(
role = "system",
content = "You are an experienced microscopy expert specialized " +
"in image analysis."),
list(
role = "user",
content = list(
list(
type = "text",
text = "Attached you find an image From a recent publication. " +
"The title of the presentation is: \"Human Airway Organoids and " +
"Multimodal Imaging-Based Toxicity Evaluation of " +
"1-Nitropyrene\". The caption of the figure is \"Figure 2. " +
"Architecture alteration and visual assessment of cytotoxicity " +
"effect on human airway organoids induced by 1-NP treatment. " +
"(A) Representative photomicrographs and live/dead staining " +
"images of the control group and 1, 10, and 50 μM 1-NP groups " +
"at two time points (day 3 and day 6). Green fluorescence " +
"indicates the Calcein-AM stain in the live cells and red " +
"fluorescence indicates the PI stain in the dead cells. " +
"Scale bars, 100 μm. (B,C) Relative cell viability based on " +
"fluorometric live/dead cytotoxicity test of 1-NP-treated " +
"organoids after exposure for three days (B) and six days " +
"(C). (D) Fluorescence microscopic ROS analysis utilized " +
"DCFDA (green) in 1-NP (3 days)-treated 2D airway organoids. " +
"Scale bars, 10 μm. (E) ROS fluorescence intensity is " +
"depicted as a fold change compared to the control 2D airway " +
"organoids. All the data are shown as the mean ± standard " +
"deviation of n = 3, *p < 0.05, **P < 0.01.\". The abstract of " +
"the publication is \"Despite significant advances in " +
"understanding the general health impacts of air pollution, " +
"the toxic effects of air pollution on cells in the human " +
"respiratory tract are still elusive. A robust, biologically " +
"relevant in vitro model for recapitulating the physiological " +
"response of the human airway is needed to obtain a thorough " +
"understanding of the molecular mechanisms of air pollutants. " +
"In this study, by using 1-nitropyrene (1-NP) as a proof-of-" +
"concept, we demonstrate the effectiveness and reliability " +
"of evaluating environmental pollutants in physiologically " +
"active human airway organoids. Multimodal imaging tools, " +
"including live cell imaging, fluorescence microscopy, and " +
"MALDI-mass spectrometry imaging (MSI), were implemented to " +
"evaluate the cytotoxicity of 1-NP for airway organoids. In " +
"addition, lipidomic alterations upon 1-NP treatment were " +
"quantitatively analyzed by nontargeted lipidomics. 1-NP " +
"exposure was found to be associated with the overproduction " +
"of reactive oxygen species (ROS), and dysregulation of lipid " +
"pathways, including the SM-Cer conversion, as well as " +
"cardiolipin in our organoids. Compared with that of cell " +
"lines, a higher tolerance of 1-NP toxicity was observed in the " +
"human airway organoids, which might reflect a more " +
"physiologically relevant response in the native airway " +
"epithelium. Collectively, we have established a novel system " +
"for evaluating and investigating molecular mechanisms " +
"of environmental pollutants in the human airways via the " +
"combinatory use of human airway organoids, multimodal " +
"imaging analysis, and MS-based analyses.\" Could you, " +
"based on the provided information describe the figure, please?"),
list(
type = "image_url",
image_url = list(
url = "data:image/png;base64,{" + base64_image + "}")))))messages <- messages %>% append(
list(list(role = py$data$role,
content = py$data$content))) %>% append(list(
list(
role = "user",
content = list(
list(
type = "text",
text = "I have the impression that the field-of-view of light " +
"microscopy results do not match that of the fluorescence " +
"microscopy. Would you agree with that statement?")))))messages <- messages %>% append(list(list(role = py$data$role,
content = py$data$content)))
messages %>% conversation_pretty_print %>% catOf course, the gpt-4-1106-vision-preview is still
labelled under ‘preview’, but it appears as though it will only be
useful in the interpretation of microscopy results, not in the analysis
of it. There are plenty of other options out there that do assist in the
analysis part using deep learning which I will be delving in later but
it would be good to also have a LLM attached so that we can converse in
a natural way with it.