Commit 2977a902 authored by Nicolas Elie's avatar Nicolas Elie
Browse files

Update ReadFileTXTOlympus+FileResults.ipynb

parent c8f1faff
Loading
Loading
Loading
Loading
+2 −2
Changes for ReadFileTXTOlympus+FileResults.ipynb: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  8 15:42:39 2022

@author: elie
"""
```

%% Output

    '\nCreated on Tue Mar  8 15:42:39 2022\n\n@author: elie\n'

%% Cell type:code id: tags:

``` python
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import os
import javabridge
import bioformats
import xml.etree.ElementTree as ET
import pandas as pd
import numpy as np
```

%% Cell type:code id: tags:

``` python
def listdirectory(path):
    fichier=[]
    for root, dirs, files in os.walk(path):
        for i in files:
            if ".vsi" in i:
                path,ext=os.path.splitext(i)
                if (os.path.isfile(os.path.join(root, path+".vsi"))):
                    fichier.append(os.path.join(root, i))
    return sorted(fichier)



def _init_logger():
    """This is so that Javabridge doesn't spill out a lot of DEBUG messages
    during runtime.
    From CellProfiler/python-bioformats.
    """
    rootLoggerName = javabridge.get_static_field("org/slf4j/Logger",
                                         "ROOT_LOGGER_NAME",
                                         "Ljava/lang/String;")

    rootLogger = javabridge.static_call("org/slf4j/LoggerFactory",
                                "getLogger",
                                "(Ljava/lang/String;)Lorg/slf4j/Logger;",
                                rootLoggerName)

    logLevel = javabridge.get_static_field("ch/qos/logback/classic/Level",
                                   "WARN",
                                   "Lch/qos/logback/classic/Level;")

    javabridge.call(rootLogger,
            "setLevel",
            "(Lch/qos/logback/classic/Level;)V",
            logLevel)

def get_metadata(filename):
    """Read the meta data and return the metadata object.
    """
    meta = bioformats.get_omexml_metadata(filename)
    #metadata = bioformats.omexml.OMEXML(meta)
    return meta
```

%% Cell type:markdown id: tags:

## Indicate YOUR PATHs

%% Cell type:code id: tags:

``` python
# Path for vsi files and txt files (extract from CellSens)
pathchemin="/media/elie/8To/REDPOL/SaolomeNov2023/exemple"
pathchemin="/mypath"
# Path for csv file results
pathResult="/media/elie/8To/REDPOL/SaolomeNov2023/exemple"
pathResult="/mypath"
```

%% Cell type:code id: tags:

``` python

javabridge.start_vm(class_path=bioformats.JARS,run_headless=False)
logger = _init_logger()

print("Images : ",len(listdirectory(pathchemin)))
listfile=listdirectory(pathchemin)
for fileVSI in listfile: #154
    numero=fileVSI.split("/")[-1].split(".")[0].replace("Process_","")
    chemin = os.path.dirname(fileVSI)
    print("Process number :"+str(numero))
    if os.path.isfile(pathResult+"/_"+numero+"_resultat.csv"):
        if os.path.isfile(chemin+"/Process_"+numero+".txt"):

            "Read metadata to read real time"
            metadata = get_metadata(fileVSI)
            mdroot = ET.fromstring(metadata)
            df = pd.DataFrame(columns=['Chiffre', 'entete', 'temps ms'])
            for child in mdroot[3]:
                if "Acquisition Value #" in child[0][0][0].text:
                    chiffre=int(child[0][0][0].text.split("#")[1])
                    df2 = pd.DataFrame([[chiffre,child[0][0][0].text,child[0][0][1].text]], columns=['Chiffre', 'entete', 'temps ms'] )
                    #df=df.concat(df2,axis=0, join='outer')
                    df = df.append(df2)
            sorted_df = df.sort_values(by='Chiffre')

            if float(sorted_df.iat[3,2])<200:
                sorted_df.reset_index(drop = True, inplace = True)
                sorted_df=sorted_df.iloc[::2] # impair
                sorted_df.reset_index(drop = True, inplace = True)
                sorted_df = sorted_df.tail(-1)
                sorted_df = sorted_df.head(-1)
            else:
                sorted_df.reset_index(drop = True, inplace = True)
                sorted_df=sorted_df.iloc[1::2] # pair
                sorted_df.reset_index(drop = True, inplace = True)
                sorted_df = sorted_df.head(-2)
            sorted_df['Frame']=np.arange(len(sorted_df))



            df = pd.read_csv (pathResult+"/_"+numero+"_resultat.csv",sep=";")
            df3=df.join(sorted_df.set_index('Frame'), on='Sequence')
            "Read file text create by Cellsens "
            fileRead=open(chemin+"/Process_"+numero+".txt","r",encoding='UTF-16')
            data=fileRead.readlines()
            index = data.index("[Marker]\n")
            df3["Mark"]=""
            del df3['Chiffre']
            del df3['entete']
            compteur=1
            for line in range(len(data)-1,index,-1):
                timeStamp=float(data[line].replace("[","").replace("]","").replace(",",".").replace(" ms","").split("=")[0])
                #print(timeStamp)
                for chf in range(len(df3)-1):
                    if (float(df3.iat[chf,12])==timeStamp):
                        #print(df3.iat[chf,0])
                        df3.iat[chf,13]="Mark "+str(compteur)
                        compteur+=1
                    elif (float(df3.iat[chf,12])<=timeStamp) and (timeStamp<float(df3.iat[chf+1,12])):
                        #print(df3.iat[chf+1,0])
                        df3.iat[chf+1,13]="Mark "+str(compteur)
                        compteur+=1
            df3.to_csv(pathResult+"/_"+numero+"_new_resultat.csv",index=False,sep=";")
            #print("Mark number : "+str(compteur))

```

%% Output

    Images :  1
    Process number :34