Exemplo n.º 1
0
    def parse_cl_arguments(self):
        """
        The function assumes that the command line parser has been setup using the initialize_argument_parser(..)

        This function parses all arguments that are specific to the command-line parser itself. Analysis
        arguments are added and parsed later by the add_and_parse_analysis_arguments(...) function.
        The reason for this is two-fold: i) to separate the parsing of analysis arguments and arguments of the
        command-line driver and ii) if the same HDF5 file is used as input and output target, then we need to
        open it first here in append mode before it gets opened in read mode later by the arguments.

        *Side effects:* The function sets ``self.output_target`` and ``self.profile_analysis``

        """
        # Parse the arguments and convert them to a dict using vars
        parsed_arguments = vars(self.parser.parse_known_args()[0])

        # Clean up the arguments to remove default arguments of the driver class
        # before we hand the arguments to the analysis class
        if self.analysis_class_arg_name in parsed_arguments:
            parsed_arguments.pop(self.analysis_class_arg_name)

        # Process the --save argument to determine where we should save the output
        if self.output_save_arg_name in parsed_arguments and mpi_helper.get_rank() == self.mpi_root:
            # Determine the filename and experiment group from the path
            self.output_target = parsed_arguments.pop(self.output_save_arg_name)
            if self.output_target is not None:
                output_filename, output_object_path = omsi_file_common.parse_path_string(self.output_target)
                # Create the output file
                if output_filename is None:
                    raise ValueError("ERROR: Invalid save parameter specification " + self.output_target)
                elif os.path.exists(output_filename) and not os.path.isfile(output_filename):
                    raise ValueError("ERROR: Save parameter not specify a file.")
                if not os.path.exists(output_filename):
                    out_file = omsi_file(output_filename, mode='a')
                    self.output_target = out_file.create_experiment()
                    self. __output_target_self = output_filename
                else:
                    out_file = omsi_file(output_filename, mode='r+')
                    if output_object_path is not None:
                        self.output_target = omsi_file_common.get_omsi_object(out_file[output_object_path])
                    else:
                        if out_file.get_num_experiments() > 0:
                            self.output_target = out_file.get_experiment(0)
                        else:
                            self.output_target = out_file.create_experiment()
        else:
            self.output_target = parsed_arguments.pop(self.output_save_arg_name)

        # The --loglovel argument
        if self.log_level_arg_name in parsed_arguments:
            user_log_level = parsed_arguments.pop(self.log_level_arg_name)
            if user_log_level in log_helper.log_levels.keys():
                log_helper.set_log_level(level=log_helper.log_levels[user_log_level])
            else:
                log_helper.error(module_name=__name__, message="Invalid log level specified")
Exemplo n.º 2
0
 def test_warp_function_save_and_recreate(self):
     def my_funct(a):
         return np.sum(a)
     g = analysis_generic.from_function(my_funct)
     res1 = g.execute(a=np.arange(10))
     f = omsi_file(self.test_filename, 'a')
     e = f.create_experiment()
     e.create_analysis(g)
     f.flush()
     del f
     del my_funct
     f = omsi_file(self.test_filename, 'a')
     e = f.get_experiment(0)
     a = e.get_analysis(0)
     g2 = a.restore_analysis()
     res2 = g2.execute()
     self.assertEquals(res1, res2)
Exemplo n.º 3
0
    def test_warp_function_save_and_recreate(self):
        def my_funct(a):
            return np.sum(a)

        g = analysis_generic.from_function(my_funct)
        res1 = g.execute(a=np.arange(10))
        f = omsi_file(self.test_filename, 'a')
        e = f.create_experiment()
        e.create_analysis(g)
        f.flush()
        del f
        del my_funct
        f = omsi_file(self.test_filename, 'a')
        e = f.get_experiment(0)
        a = e.get_analysis(0)
        g2 = a.restore_analysis()
        res2 = g2.execute()
        self.assertEquals(res1, res2)
Exemplo n.º 4
0
 def test_wrap_function_and_save(self):
     def f(a):
         return np.sum(a)
     g = analysis_generic.from_function(f)
     g.execute(a=np.arange(10))
     f = omsi_file(self.test_filename, 'a')
     e = f.create_experiment()
     a = e.create_analysis(g)
     f.flush()
     self.assertNotEquals(a, None)
Exemplo n.º 5
0
def main(argv=None):
    """Then main function"""
    if argv is None:
        argv = sys.argv

    # Check for correct usage
    if len(argv) < 5:
        print "Usage: python makeThumb.py <HDF5-File> <NMF-Index1> <NMF-Index2> <NMF-Index3>"
        sys.exit(0)

    # Settings
    nmf_analysis_index = 1
    experiment_index = 0
    input_filename = argv[1]
    image_index1 = int(argv[2])
    image_index2 = int(argv[3])
    image_index3 = int(argv[4])
    apply_log_scale = True
    thumbnail_filename = input_filename + "_0.png"

    # Open the file and required analysis dataset
    omsi_input_file = omsi_file(input_filename, 'r')
    input_experiment = omsi_input_file.get_experiment(experiment_index)
    input_analysis = input_experiment.get_analysis(nmf_analysis_index)
    ho_data = input_analysis['ho']

    # Load the required data
    num_pixel_x = ho_data.shape[0]
    num_pixel_y = ho_data.shape[1]
    image_data1 = ho_data[:, :, image_index1].reshape((num_pixel_x, num_pixel_y))
    image_data2 = ho_data[:, :, image_index2].reshape((num_pixel_x, num_pixel_y))
    image_data3 = ho_data[:, :, image_index3].reshape((num_pixel_x, num_pixel_y))

    # Scale the data
    if apply_log_scale:
        image_data1 = np.log(image_data1 + 1)
        image_data2 = np.log(image_data2 + 1)
        image_data3 = np.log(image_data3 + 1)

    # Normalize the data values
    image_data1 /= float(np.max(image_data1))
    image_data2 /= float(np.max(image_data2))
    image_data3 /= float(np.max(image_data3))

    # Generate the grayscale images
    image_channel_red = Image.fromarray(image_data1.astype('float') * 255).convert('L')
    image_channel_green = Image.fromarray(image_data2.astype('float') * 255).convert('L')
    image_channel_blue = Image.fromarray(image_data3.astype('float') * 255).convert('L')

    # Generate the RGB image and save the file
    thumbnail = Image.merge('RGB', (image_channel_red, image_channel_green, image_channel_blue))
    print "Save image"
    thumbnail.save(thumbnail_filename, 'PNG')
    print thumbnail_filename
Exemplo n.º 6
0
    def test_wrap_function_and_save(self):
        def f(a):
            return np.sum(a)

        g = analysis_generic.from_function(f)
        g.execute(a=np.arange(10))
        f = omsi_file(self.test_filename, 'a')
        e = f.create_experiment()
        a = e.create_analysis(g)
        f.flush()
        self.assertNotEquals(a, None)
Exemplo n.º 7
0
def run_peakcube(omsiInFile, expIndex, dataIndex, LPFIndex, NPGIndex):

	#Open the input HDF5 file
	try:
		omsiFile = omsi_file(omsiInFile,'r+')
	except:
		print "Error opening input file \"",omsiInFile,"\": ", sys.exc_info()[0]
		exit(0)

	# Get the experiment
	exp = omsiFile.get_experiment( expIndex )
	data = exp.get_msidata(dataIndex)
	peaksMZdata = data.mz[:]
	LPFanalysis = exp.get_analysis( LPFIndex )
	NPGanalysis = exp.get_analysis( NPGIndex )

	peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
	peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
	peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
	NPGPeaksLabels = NPGanalysis['npghc_peaks_labels'][:]
	NPGLabelsList = NPGanalysis['npghc_labels_list'][:]

	print "\n--- Creating Peak Cube ---"
	myPC = omsi_peakcube(name_key="omsi_peakcube_" + str(ctime()))
	#myPC.omsi_peakcube_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, NPGPeaksLabels, NPGLabelsList)
	myPC.execute( peaksBins = peaksBins,
                  peaksIntensities = peaksIntensities,
                  peaksArrayIndex = peaksArrayIndex,
                  peaksMZdata = peaksMZdata,
                  HCpeaksLabels = NPGPeaksLabels ,
                  HCLabelsList = NPGLabelsList)


	PCm = myPC['npg_peak_cube_mz']
	print "NPG Peak Cube Mzs: \n", PCm.shape, "\n", PCm
	PMz = myPC['npg_peak_mz']
	print "NPG Peak Mz: \n", PMz.shape, "\n", PMz

	print "\nSaving HDF5 analysis..."
	PCanalysis, PCanalysisindex = exp.create_analysis(myPC)
	print "done!"
	print "--- omsi_peakcube complete ---\n"
	print "PeakCube Analysis Index:", PCanalysisindex


	# flush of peakcube
	exp.experiment.file.flush()
	omsiFile.close_file()

	return PCanalysisindex
Exemplo n.º 8
0
def run_peakcube(omsiInFile, expIndex, dataIndex, LPFIndex, NPGIndex):

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile, 'r+')
    except:
        print "Error opening input file \"", omsiInFile, "\": ", sys.exc_info(
        )[0]
        exit(0)

    # Get the experiment
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    peaksMZdata = data.mz[:]
    LPFanalysis = exp.get_analysis(LPFIndex)
    NPGanalysis = exp.get_analysis(NPGIndex)

    peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
    peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
    peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
    NPGPeaksLabels = NPGanalysis['npghc_peaks_labels'][:]
    NPGLabelsList = NPGanalysis['npghc_labels_list'][:]

    print "\n--- Creating Peak Cube ---"
    myPC = omsi_peakcube(name_key="omsi_peakcube_" + str(ctime()))
    #myPC.omsi_peakcube_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, NPGPeaksLabels, NPGLabelsList)
    myPC.execute(peaksBins=peaksBins,
                 peaksIntensities=peaksIntensities,
                 peaksArrayIndex=peaksArrayIndex,
                 peaksMZdata=peaksMZdata,
                 HCpeaksLabels=NPGPeaksLabels,
                 HCLabelsList=NPGLabelsList)

    PCm = myPC['npg_peak_cube_mz']
    print "NPG Peak Cube Mzs: \n", PCm.shape, "\n", PCm
    PMz = myPC['npg_peak_mz']
    print "NPG Peak Mz: \n", PMz.shape, "\n", PMz

    print "\nSaving HDF5 analysis..."
    PCanalysis, PCanalysisindex = exp.create_analysis(myPC)
    print "done!"
    print "--- omsi_peakcube complete ---\n"
    print "PeakCube Analysis Index:", PCanalysisindex

    # flush of peakcube
    exp.experiment.file.flush()
    omsiFile.close_file()

    return PCanalysisindex
Exemplo n.º 9
0
def run_npg(omsiInFile, expIndex, dataIndex, LPFIndex, mzth, tcut):

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile, 'r+')
    except:
        print "Error opening input file \"", omsiInFile, "\": ", sys.exc_info(
        )[0]
        exit(0)

    # Get the experiment and data
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    LPFanalysis = exp.get_analysis(LPFIndex)

    peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
    peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
    peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
    peaksMZdata = data.mz[:]
    peaksMZ = peaksMZdata[peaksBins]

    # NPG --------------
    print "\n--- Executing NPG ---"
    myNPG = omsi_npg(name_key="omsi_npg_" + str(ctime()))
    #myNPG.omsi_npg_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, peaksMZ, MZ_TH = mzth, clusterCut = tcut)
    myNPG.execute(peaksBins=peaksBins,
                  npg_peaks_Intensities=peaksIntensities,
                  npg_peaks_ArrayIndex=peaksArrayIndex,
                  peaksMZdata=peaksMZdata,
                  peaksMZ=peaksMZ,
                  npg_mz_threshold=mzth,
                  npg_cluster_treecut=tcut)
    print "\n\nResults"
    NPGPL = myNPG['npghc_peaks_labels']
    print "NPG HC Peaks Labels: \n", NPGPL
    NPGLL = myNPG['npghc_labels_list']
    print "NPG HC Labels List: \n", NPGLL

    print "\nSaving HDF5 analysis..."
    NPGanalysis, NPGanalysisindex = exp.create_analysis(myNPG)
    print "done!"
    print "--- omsi_npg complete ---\n"
    print "NPG Analysis Index:", NPGanalysisindex

    # flush of npg
    exp.experiment.file.flush()
    omsiFile.close_file()

    return NPGanalysisindex
Exemplo n.º 10
0
def run_npg(omsiInFile, expIndex, dataIndex, LPFIndex, mzth, tcut):

	#Open the input HDF5 file
	try:
		omsiFile = omsi_file(omsiInFile,'r+')
	except:
		print "Error opening input file \"",omsiInFile,"\": ", sys.exc_info()[0]
		exit(0)

	# Get the experiment and data
	exp = omsiFile.get_experiment( expIndex )
	data = exp.get_msidata(dataIndex)
	LPFanalysis = exp.get_analysis( LPFIndex )

	peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
	peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
	peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
	peaksMZdata = data.mz[:]
	peaksMZ = peaksMZdata[peaksBins]

	# NPG --------------
	print "\n--- Executing NPG ---"
	myNPG = omsi_npg(name_key="omsi_npg_" + str(ctime()))
	#myNPG.omsi_npg_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, peaksMZ, MZ_TH = mzth, clusterCut = tcut)
	myNPG.execute( peaksBins=peaksBins,
                   npg_peaks_Intensities=peaksIntensities,
                   npg_peaks_ArrayIndex=peaksArrayIndex,
                   peaksMZdata=peaksMZdata,
                   peaksMZ=peaksMZ,
                   npg_mz_threshold=mzth,
                   npg_cluster_treecut=tcut   )
	print "\n\nResults"
	NPGPL = myNPG['npghc_peaks_labels']
	print "NPG HC Peaks Labels: \n", NPGPL
	NPGLL = myNPG['npghc_labels_list']
	print "NPG HC Labels List: \n", NPGLL

	print "\nSaving HDF5 analysis..."
	NPGanalysis, NPGanalysisindex = exp.create_analysis(myNPG)
	print "done!"
	print "--- omsi_npg complete ---\n"
	print "NPG Analysis Index:", NPGanalysisindex

	# flush of npg
	exp.experiment.file.flush()
	omsiFile.close_file()

	return NPGanalysisindex
Exemplo n.º 11
0
def main(argv=None):
    """Then main function"""

    import sys
    from sys import argv,exit
    
    if argv is None:
        argv = sys.argv
   
    #Check for correct usage
    if len(argv) <2 :
        print "USAGE: Call \"omsiHDF5File [expIndex dataIndex]   \" "
        print "\n"
        print "This is a simple viewer for looking at OMSI HDF5 files."
        print "The viewer takes the index of the experiment and the"
        print "dataset to be used as optional input"
        exit(0)

    #Read the input arguments 
    omsiOutFile  = argv[1]
    expIndex = 0
    dataIndex = 0 
    if len(argv)==4 :
        expIndex = int(argv[2] )
        dataIndex = int(argv[3] )

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file( omsiOutFile , 'r' ) #Open file in read only mode
    except:
        print "Unexpected error creating the output file:", sys.exc_info()[0]
        exit(0)

    #Get the experiment and dataset
    exp = omsiFile.get_experiment( expIndex )
    data = exp.get_msidata(dataIndex)
    mzdata = exp.get_instrument_info().get_instrument_mz()    

    if data is None:
        print "Could not access image data for the experiment"
        exit(0)

    #Initalize the viewer
    viewer = MyViewer(  data , mzdata )
    plt.show()     
Exemplo n.º 12
0
def run_lpf(omsiInFile, expIndex, dataIndex, ph, slw, smw):

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile, 'r+')
    except:
        print "Error opening input file \"", omsiInFile, "\": ", sys.exc_info(
        )[0]
        exit(0)

    # Get the experiment and data
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    peaksMZdata = data.mz[:]

    # LPF --------------
    print "\n--- Executing LPF ---"
    myLPF = omsi_lpf(name_key="omsi_lpf_" + str(ctime()))
    myLPF.execute(msidata=data,
                  mzdata=peaksMZdata,
                  peakheight=ph,
                  slwindow=slw,
                  smoothwidth=smw)
    print "\n\nResults"
    peaksBins = myLPF['LPF_Peaks_MZ'][:]
    print "peaksBins:\n", peaksBins
    peaksIntensities = myLPF['LPF_Peaks_Vals'][:]
    print "peaksIntensities:\n", peaksIntensities
    peaksArrayIndex = myLPF['LPF_Peaks_ArrayIndex'][:]
    print "peaksArrayIndex:\n", peaksArrayIndex

    print "\nSaving HDF5 analysis..."
    LPFanalysis, LPFanalysisindex = exp.create_analysis(myLPF)
    print "done!"
    print "--- omsi_lpf complete ---\n"
    print "LPF Analysis Index:", LPFanalysisindex

    # flush of lpf
    exp.experiment.file.flush()
    omsiFile.close_file()

    return LPFanalysisindex
Exemplo n.º 13
0
def run_lpf(omsiInFile, expIndex, dataIndex, ph, slw, smw):

	#Open the input HDF5 file
	try:
		omsiFile = omsi_file(omsiInFile,'r+')
	except:
		print "Error opening input file \"",omsiInFile,"\": ", sys.exc_info()[0]
		exit(0)


	# Get the experiment and data
	exp = omsiFile.get_experiment( expIndex )
	data = exp.get_msidata(dataIndex)
	peaksMZdata = data.mz[:]

	# LPF --------------
	print "\n--- Executing LPF ---"
	myLPF = omsi_lpf(name_key="omsi_lpf_" + str(ctime()))
	myLPF.execute( msidata=data, mzdata=peaksMZdata, peakheight = ph, slwindow = slw, smoothwidth = smw)
	print "\n\nResults"
	peaksBins = myLPF['LPF_Peaks_MZ'][:]
	print "peaksBins:\n", peaksBins
	peaksIntensities = myLPF['LPF_Peaks_Vals'][:]
	print "peaksIntensities:\n", peaksIntensities
	peaksArrayIndex = myLPF['LPF_Peaks_ArrayIndex'][:]
	print "peaksArrayIndex:\n", peaksArrayIndex

	print "\nSaving HDF5 analysis..."
	LPFanalysis, LPFanalysisindex = exp.create_analysis(myLPF)
	print "done!"
	print "--- omsi_lpf complete ---\n"
	print "LPF Analysis Index:", LPFanalysisindex

	# flush of lpf
	exp.experiment.file.flush()
	omsiFile.close_file()

	return LPFanalysisindex
Exemplo n.º 14
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

        # Check for correct usage
    if len(argv) != 6:
        print "USAGE: Call \"omsi_peakcube OMSI_FILE [expIndex dataIndex LPFanalysisIndex NPGanalysisIndex]   \" "
        print "Generates PeakCube for NPG global peaks"
        exit(0)

    #Read the input arguments
    omsiInFile = argv[1]
    expIndex = int(argv[2])
    dataIndex = int(argv[3])
    LPFanalysisIndex = int(argv[4])
    NPGanalysisIndex = int(argv[5])

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile)
    except:
        print "Error opening input file:", sys.exc_info()[0]
        exit(0)

    print "Input file: ", omsiInFile
    print "LPF Analysis Index: ", LPFanalysisIndex
    print "NPG Analysis Index: ", NPGanalysisIndex

    #Get the experiment and data
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    peaksMZdata = data.mz[:]

    LPFanalysis = exp.get_analysis(LPFanalysisIndex)
    NPGanalysis = exp.get_analysis(NPGanalysisIndex)

    print "\n[loading data...]"
    peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
    peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
    peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
    NPGPL = NPGanalysis['npghc_peaks_labels'][:]
    NPGLL = NPGanalysis['npghc_labels_list'][:]

    print "[done!] lpf data shapes:"
    print "peaksBins shape: ", peaksBins.shape
    print "peaksIntensities shape: ", peaksIntensities.shape
    print "peaksArrayIndex shape: ", peaksArrayIndex.shape
    print "peaksMZdata shape: ", peaksMZdata.shape
    print "NPGPL shape: ", NPGPL.shape
    print "NPGLL shape: ", NPGLL.shape
    sys.stdout.flush()

    # pc
    myPC = omsi_peakcube(name_key="omsi_peakcube_" + str(ctime()))
    print "--- Creating Peak Cube ---"
    #myPC.omsi_peakcube_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, NPGPL, NPGLL)
    myPC.execute(peaksBins=peaksBins,
                 peaksIntensities=peaksIntensities,
                 peaksArrayIndex=peaksArrayIndex,
                 peaksMZdata=peaksMZdata,
                 HCpeaksLabels=NPGPL,
                 HCLabelsList=NPGLL)

    PCm = myPC['npg_peak_cube_mz']
    print "NPG Peak Cube Mzs: \n", PCm.shape, "\n", PCm
    PMz = myPC['npg_peak_mz']
    print "NPG Peak Mz: \n", PMz.shape, "\n", PMz

    print "\nsaving HDF5 analysis..."
    PCanalysis, PCanalysisindex = exp.create_analysis(myPC)
    print "done!"
    print "--- omsi_peakcube complete ---\n"
    print "Peak Cube analysis index:", PCanalysisindex
    print "omsi_peakcube complete for input file: ", omsiInFile, "\n"
Exemplo n.º 15
0
 def setUp(self):
     self.named_temporary_file = tempfile.NamedTemporaryFile()
     self.test_filename = self.named_temporary_file.name
     self.testfile = omsi_file(self.test_filename)
     self.exp = self.testfile.create_experiment()
Exemplo n.º 16
0
 def setUp(self):
     self.named_temporary_file = tempfile.NamedTemporaryFile()
     self.test_filename = self.named_temporary_file.name
     self.testfile = omsi_file(self.test_filename)
     self.exp = self.testfile.create_experiment()
Exemplo n.º 17
0
    def get_omsi_object(cls, h5py_object, resolve_dependencies=False):
        """
        This static method is convenience function used to retrieve the corresponding interface class for a
        given h5py group object.

        :param h5py_object: h5py object for which the corresponding omsi_file API object should be generated.
                        This may also be a string describing the requested object based on a combination of the
                        path to the file and a path ot the object <filename.h5>:<object_path>
        :param resolve_dependencies: Set to True if omsi_file_dependencydata objects should be resolved to retrieve
                        the dependent object the dependency is pointing to. Dependencies are resolved recursively,
                        i.e., if a dependency points to another dependency then that one will be resolved as well.
                        Default value is False, i.e., the omis_file_dependency object itself is returned.

        :returns: None in case no corresponding object was found. Otherwise an instance of:

            * omsi_file : If the given object is a h5py.File object
            * omsi_file_experiment : If the given object is an experiment groupt
            * omsi_file_methods : If the given object is a method group
            * omsi_file_instrument : If the given object is an instrument group
            * omsi_file_analysis : If the given object is an analysis group
            * omsi_file_msidata : If the given object is a MSI data group
            * omsi_file_dependencydata : If the fiven object is a dependency group
            * The input h5py_object: If the given object is a h5py.Dataset or h5py.Group
            * None: In case that an unknown type is given.
        """
        from omsi.dataformat.omsi_file.experiment import omsi_file_experiment
        from omsi.dataformat.omsi_file.main_file import omsi_file
        from omsi.dataformat.omsi_file.dependencies import omsi_file_dependencies, omsi_file_dependencydata
        from omsi.dataformat.omsi_file.instrument import omsi_file_instrument
        from omsi.dataformat.omsi_file.methods import omsi_file_methods
        from omsi.dataformat.omsi_file.analysis import omsi_file_analysis
        from omsi.dataformat.omsi_file.msidata import omsi_file_msidata
        from omsi.dataformat.omsi_file.metadata_collection import omsi_file_metadata_collection

        # If the input object is already an omsi API object then return it as is
        if isinstance(h5py_object, omsi_file) or \
                isinstance(h5py_object, omsi_file_experiment) or \
                isinstance(h5py_object, omsi_file_methods) or\
                isinstance(h5py_object, omsi_file_instrument) or \
                isinstance(h5py_object, omsi_file_analysis) or \
                isinstance(h5py_object, omsi_file_msidata) or \
                isinstance(h5py_object, omsi_file_dependencies) or \
                isinstance(h5py_object, omsi_file_dependencydata) or \
                isinstance(h5py_object, omsi_file_metadata_collection):
            return h5py_object
        # IF we have an h5py.File then create and omsi_file
        if isinstance(h5py_object, h5py.File):
            return omsi_file(h5py_object)
        # If we have an h5py.Group, then try to create a corresponding omsi API object
        elif isinstance(h5py_object, h5py.Group):
            # Check if the group has an explicit type attribute
            try:
                # Try to determine the type of the group based on the
                # attributes
                type_attribute = h5py_object.attrs[omsi_format_common.type_attribute]
                if type_attribute == "omsi_file_experiment":
                    return omsi_file_experiment(h5py_object)
                elif type_attribute == "omsi_file_methods" or type_attribute == "omsi_file_methods":
                    return omsi_file_methods(h5py_object)
                elif type_attribute == "omsi_file_instrument":
                    return omsi_file_instrument(h5py_object)
                elif type_attribute == "omsi_file_analysis":
                    return omsi_file_analysis(h5py_object)
                elif type_attribute == "omsi_file_msidata":
                    return omsi_file_msidata(h5py_object)
                elif type_attribute == "omsi_file":
                    return omsi_file(h5py_object)
                elif type_attribute == "omsi_file_dependencydata":
                    omsiobject = omsi_file_dependencydata(h5py_object)
                    if resolve_dependencies:
                        return omsi_file_common.get_omsi_object(
                            omsi_file_common.get_h5py_object(omsiobject.get_dependency_omsiobject(),
                                                             resolve_dependencies))
                    else:
                        return omsiobject
                elif type_attribute == "omsi_file_dependencies":
                    return omsi_file_dependencies(h5py_object)
                elif type_attribute == 'omsi_file_metadata_collection':
                    return omsi_file_metadata_collection(h5py_object)
                else:
                    return h5py_object
            except:
                # If the attribute is missing, then try to determine the type
                # based on he name of group
                groupname = h5py_object.name.split("/")[-1]
                parentgroupname = h5py_object.parent.name.split("/")[-1]
                if groupname.startswith(omsi_format_experiment.exp_groupname):
                    return omsi_file_experiment(h5py_object)
                elif groupname.startswith(omsi_format_methods.methods_groupname) or \
                        groupname.startswith(omsi_format_methods.methods_old_groupname):
                    return omsi_file_methods(h5py_object)
                elif groupname.startswith(omsi_format_instrument.instrument_groupname):
                    return omsi_file_instrument(h5py_object)
                elif groupname.startswith(omsi_format_analysis.analysis_groupname):
                    return omsi_file_analysis(h5py_object)
                elif groupname.startswith(omsi_format_data.data_groupname):
                    return omsi_file_msidata(h5py_object)
                elif groupname.startswith(omsi_format_dependencies.dependencies_groupname):
                    return omsi_file_dependencies(h5py_object)
                elif parentgroupname.startswith(omsi_format_dependencies.dependencies_groupname):
                    omsiobject = omsi_file_dependencydata(h5py_object)
                    if resolve_dependencies:
                        return omsi_file_common.get_omsi_object(
                            omsi_file_common.get_h5py_object(omsiobject.get_dependency_omsiobject(),
                                                             resolve_dependencies))
                    else:
                        return omsiobject
                elif parentgroupname.startswith(omsi_format_metadata_collection.metadata_collection_groupname_default):
                    return omsi_file_metadata_collection(h5py_object)
                elif groupname == "":  # We are at the root group
                    return omsi_file(h5py_object.file)
                else:
                    return h5py_object
        # If we have an hpy.Dataset then we don't have a corresponding API object. Return as is
        elif isinstance(h5py_object, h5py.Dataset):
            return h5py_object
        elif isinstance(h5py_object, basestring):
            import os
            filename, object_path = cls.parse_path_string(h5py_object)
            if filename is not None and os.path.exists(filename) and os.path.isfile(filename):
                try:
                    curr_omsi_file = omsi_file(filename, 'r')
                except:
                    return None
            else:
                return None

            if object_path is not None:
                try:
                    file_object = curr_omsi_file.managed_group[object_path]
                except KeyError:
                    return None
                return omsi_file_common.get_omsi_object(file_object, resolve_dependencies=True)
            else:
                if isinstance(curr_omsi_file, omsi_file):
                    return curr_omsi_file
                else:
                    raise ValueError('omsi_file_common.Invalid path or file')
        else:
            return None
Exemplo n.º 18
0
def main(argv=None):
    """Then main function"""

    if argv is None:
        argv = sys.argv

    # Check for correct usage
    if len(argv) < 3:
        print_help()
        sys.exit(0)

    infile = argv[1]
    experiment_index = int(argv[2])
    analysis_index = int(argv[3])
    output_filename = argv[4]

    # Open the file and get to the data
    # Note the analysis and experiment index may vary for different datasets
    omsi_input_file = omsi_file(infile, 'r')
    input_experiment = omsi_input_file.get_experiment(experiment_index)
    input_analysis = input_experiment.get_analysis(analysis_index)
    # Get the peak finding data abd load it all
    input_dataset = input_analysis['peak_cube']
    input_mz_dataset = input_analysis['peak_mz']
    print input_analysis.items()

    # Load all the peak finding data
    peak_cube = input_dataset[:]
    peak_mz = input_mz_dataset[:]
    x_dim = peak_cube.shape[0]
    y_dim = peak_cube.shape[1]
    num_slices = peak_cube.shape[2]

    # Generate the images
    print "Generating images"
    names = []
    for row_index in range(0, num_slices):
        sys.stdout.write("[" +
                         str(int(100. * float(row_index) /
                                 float(num_slices))) + "%]" + "\r")
        sys.stdout.flush()
        image_data = peak_cube[:, :, row_index]
        image_data = image_data.reshape((x_dim, y_dim))
        image_data = image_data / np.max(image_data)
        output_images = Image.fromarray(image_data.astype('float') * 255)
        name = output_filename + str(peak_mz[row_index])
        # Replace the "." in the string to make sure pdflatex does not complain about the filename
        name = name.replace(".", "_")
        name += ".png"
        names.append(name)
        output_images.convert('L').save(name, 'PNG')

    # Generate the latex file
    print "Generating LaTeX file"

    tex_filename = output_filename + "summary.tex"
    latex_file = open(tex_filename, 'w')
    latex_file.write("\documentclass[a4paper,10pt]{article}\n")
    latex_file.write("\usepackage[utf8x]{inputenc}\n")
    latex_file.write("\usepackage{graphicx}\n")
    latex_file.write("\\title{Peak Images}\n")
    latex_file.write("\\author{Oliver Ruebel}\n")
    latex_file.write("\date{" + str(datetime.date.today()) + "}\n")
    latex_file.write("\\begin{document}\n")
    latex_file.write("\maketitle\n ")

    # Write the latex table with the image files
    number_of_columns = 3
    res = float(x_dim) / float(y_dim)
    number_of_rows = int(1.29 / (0.3 * (float(x_dim) / float(y_dim))))
    if number_of_rows < 1:
        number_of_rows = 1
        print "WARNING: Images may not fit on a single page. Reduce the width of image plots"
    total_number_of_rows = int(len(names) / float(number_of_columns) + 0.5)
    for row_index in range(0, total_number_of_rows):
        sys.stdout.write(
            "[" +
            str(int(100. * float(row_index) / float(total_number_of_rows))) +
            "%]" + "\r")
        sys.stdout.flush()
        if row_index % number_of_rows == 0:
            latex_file.write("\\begin{center}\n")
            latex_file.write("\\begin{tabular}{lll}\n")

        for column_index in xrange(0, number_of_columns):
            index = row_index * number_of_columns + column_index
            if index < peak_mz.size:
                latex_file.write(str(peak_mz[index]))
            else:
                latex_file.write(" ")
            if column_index < (number_of_columns - 1):
                latex_file.write(" & ")
            else:
                latex_file.write(" \\\\ \n")

        for column_index in xrange(0, number_of_columns):
            index = row_index * number_of_columns + column_index
            if index < len(names):
                latex_file.write("\includegraphics[width=0.3\\textwidth]{" +
                                 os.path.basename(names[index]) + "}")
            else:
                latex_file.write(" ")
            if column_index < (number_of_columns - 1):
                latex_file.write(" & ")
            else:
                latex_file.write(" \\\\ \n")

        if row_index % number_of_rows == (number_of_rows - 1) or row_index == (
                total_number_of_rows - 1):
            latex_file.write("\\end{tabular}\n")
            latex_file.write("\\end{center}\n")

    latex_file.write("\\end{document}\n")
    latex_file.close()

    print "Building the PDF file"
    abs_path = os.path.abspath(tex_filename)
    latex_dir = os.path.dirname(abs_path)
    latex_filename = os.path.basename(abs_path)
    subprocess.call(["pdflatex", latex_filename], cwd=latex_dir)
def save_arrayed_image_to_omsi_file(arrayedImage, filename=None, spotSpectra=None):
    """
    Save the data from an ArrayedImage and additional spot spectru to and OpenMSI-compliant  HDF5 file

    :param filename: Name of the output file. If filename is None, then we will ask for a filename via input
    :param arrayedImage: Instance of OMAAT ArrayedImage to be saved
    :param spotSpectra: data frame or numpy array of spot spectra generated by OMAAT

    Requires: This function requires the following packages: BASTet, h5y, numpy, pandas, pickle, datetime

    :return: Instance of omsi.dataformat.omsi_file.main_file.omsi_file of the generated output OpenMSI file
    """
    from omsi.analysis.generic import analysis_generic
    from omsi.dataformat.omsi_file.main_file import omsi_file
    import h5py
    import pickle
    import datetime
    import numpy as np
    import pandas as pd

    if filename is None:
        filename = input("Output filename: ")

    dt = datetime.datetime.now()
    curr_ana = analysis_generic(name_key="openmsi_arrayed_analysis_results_{:d}-{:d}-{:d}_{:d}h{:d}.csv".format(dt.year,dt.month,dt.day,dt.hour,dt.minute))
    dtypes = curr_ana.get_default_dtypes()
    groups = curr_ana.get_default_parameter_groups()

    curr_ana.real_analysis_type = 'omaat_lib.ArrayedImage'
    curr_ana.data_names = ['spotLocations', 'xCenters', 'yCenters', 'baseImage', 'imStack', 'spectraDF', 'arrayedImageP', 'spotSpectraP', 'spotSpectra']
    curr_ana['spotLocations'] = arrayedImage.spotLocations
    if isinstance(arrayedImage.spotList, list):
        for spot_index, spot in enumerate(arrayedImage.spotList):
            spot_name = 'spot_'+str(spot_index)
            curr_ana.data_names.append(spot_name)
            curr_ana[spot_name] = spot
    curr_ana['xCenters'] = arrayedImage.xCenters
    curr_ana['yCenters'] = arrayedImage.yCenters
    curr_ana['baseImage'] = arrayedImage.baseImage
    curr_ana['imStack'] = arrayedImage.imStack
    try:
        curr_ana['spectraDF'] = arrayedImage.spectra_df
    except AttributeError:
        curr_ana['spectraDF'] = None
    curr_ana['arrayedImageP'] = pickle.dumps(arrayedImage)
    if curr_ana['spectraDF'] is None:
        curr_ana['spectraDF'] = arrayedImage.resultsDataFrame()
    if spotSpectra is not None:
        if isinstance(spotSpectra, pd.DataFrame):
            curr_ana['spotSpectra'] = spotSpectra.as_matrix()
            curr_ana['spotSpectraP'] = pickle.dumps(spotSpectra)
        else:
            curr_ana['spotSpectra'] = np.asarray(spotSpectra)
            if curr_ana['spotSpectra'].dtype == np.dtype('O'):
                curr_ana.pop('spotSpectra')
                raise ValueError("Unsupported format for spotSpectra. Conversion to numpy resulted in type 'O'")

    curr_ana.add_parameter(name='Nrows',
                           help='Number of rows',
                           dtype=dtypes['int'],
                           required=True,
                           default=12,
                           choices=None,
                           group=groups['settings'],
                           data=arrayedImage.Nrows)
    curr_ana.add_parameter(name='Ncolumns',
                           help='Number of columns',
                           dtype=dtypes['int'],
                           required=True,
                           default=12,
                           choices=None,
                           group=groups['settings'],
                           data=arrayedImage.Ncolumns)
    curr_ana.add_parameter(name='mz',
                           help='The m/z axis of the input data',
                           dtype=dtypes['ndarray'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.mz)
    curr_ana.add_parameter(name='ions',
                           help='List of ions used',
                           dtype=dtypes['ndarray'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.ions)
    curr_ana.add_parameter(name='originalSize',
                           help='Original size',
                           dtype=dtypes['ndarray'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.originalSize)
    curr_ana.add_parameter(name='filename',
                           help='The name of the input file',
                           dtype=dtypes['unicode'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.filename)
    curr_ana.add_parameter(name='expIndex',
                           help='The index of the experiment in the input file',
                           dtype=dtypes['int'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.expIndex)
    curr_ana.add_parameter(name='dataIndex',
                           help='The index of the dataset in the input file',
                           dtype=dtypes['int'],
                           required=True,
                           group=groups['input'],
                           data=arrayedImage.dataIndex)

    outfile = omsi_file(h5py.File(filename))
    exp = outfile.create_experiment(exp_identifier='OMAAT store')
    exp.create_analysis(curr_ana)
    outfile.flush()
    return outfile
Exemplo n.º 20
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

        # Check for correct usage
    if len(argv) != 6:
        print "USAGE: Call \"omsi_peakcube OMSI_FILE [expIndex dataIndex LPFanalysisIndex NPGanalysisIndex]   \" "
        print "Generates PeakCube for NPG global peaks"
        exit(0)

    #Read the input arguments
    omsiInFile = argv[1]
    expIndex = int(argv[2])
    dataIndex = int(argv[3])
    LPFanalysisIndex = int(argv[4])
    NPGanalysisIndex = int(argv[5])

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile)
    except:
        print "Error opening input file:", sys.exc_info()[0]
        exit(0)

    print "Input file: ", omsiInFile
    print "LPF Analysis Index: ", LPFanalysisIndex
    print "NPG Analysis Index: ", NPGanalysisIndex

    #Get the experiment and data
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    peaksMZdata = data.mz[:]

    LPFanalysis = exp.get_analysis(LPFanalysisIndex)
    NPGanalysis = exp.get_analysis(NPGanalysisIndex)

    print "\n[loading data...]"
    peaksBins = LPFanalysis['LPF_Peaks_MZ'][:]
    peaksIntensities = LPFanalysis['LPF_Peaks_Vals'][:]
    peaksArrayIndex = LPFanalysis['LPF_Peaks_ArrayIndex'][:]
    NPGPL = NPGanalysis['npghc_peaks_labels'][:]
    NPGLL = NPGanalysis['npghc_labels_list'][:]

    print "[done!] lpf data shapes:"
    print "peaksBins shape: ", peaksBins.shape
    print "peaksIntensities shape: ", peaksIntensities.shape
    print "peaksArrayIndex shape: ", peaksArrayIndex.shape
    print "peaksMZdata shape: ", peaksMZdata.shape
    print "NPGPL shape: ", NPGPL.shape
    print "NPGLL shape: ", NPGLL.shape
    sys.stdout.flush()

    # pc
    myPC = omsi_peakcube(name_key="omsi_peakcube_" + str(ctime()))
    print "--- Creating Peak Cube ---"
    #myPC.omsi_peakcube_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, NPGPL, NPGLL)
    myPC.execute(peaksBins=peaksBins,
                 peaksIntensities=peaksIntensities,
                 peaksArrayIndex=peaksArrayIndex,
                 peaksMZdata=peaksMZdata,
                 HCpeaksLabels=NPGPL,
                 HCLabelsList=NPGLL)

    PCm = myPC['npg_peak_cube_mz']
    print "NPG Peak Cube Mzs: \n", PCm.shape, "\n", PCm
    PMz = myPC['npg_peak_mz']
    print "NPG Peak Mz: \n", PMz.shape, "\n", PMz

    print "\nsaving HDF5 analysis..."
    PCanalysis, PCanalysisindex = exp.create_analysis(myPC)
    print "done!"
    print "--- omsi_peakcube complete ---\n"
    print "Peak Cube analysis index:", PCanalysisindex
    print "omsi_peakcube complete for input file: ", omsiInFile, "\n"
Exemplo n.º 21
0
 def setUp(self):
     self.named_temporary_file = tempfile.NamedTemporaryFile()
     self.test_filename = self.named_temporary_file.name
     self.testfile = omsi_file(self.test_filename)
Exemplo n.º 22
0
def main(argv=None):
    """Then main function"""

    import sys
    from sys import argv,exit

    if argv is None:
        argv = sys.argv

    #Check for correct usage
    if len(argv) <3 :
        printHelp()
        exit(0)

    infile = argv[1]
    expIndex = int(argv[2])
    anaIndex = int(argv[3])
    outfileName = argv[4]

    #Open the file and get to the data
    #Note the analysis and experiment index may vary for different datasets
    f = omsi_file( infile , 'r' )
    e = f.get_experiment(expIndex)
    ana = e.get_analysis(anaIndex)
    #Get the peak finding data abd load it all
    pc = ana[ 'peak_cube' ]
    pm = ana[ 'peak_mz' ]

    #Load all the peak finding data
    peakCube = pc[:]
    peakMZ   = pm[:]
    xdim = peakCube.shape[0]
    ydim = peakCube.shape[1]
    numSlices = peakCube.shape[2]

    #Generate the images
    print "Generating images"
    names = []
    for i in range(0,numSlices) :
        sys.stdout.write("[" +str( int( 100.* float(i)/float(numSlices) )) +"%]"+ "\r")
        sys.stdout.flush()

        imData = peakCube[:,:,i]
        imData = imData.reshape((xdim,ydim))
        imData = imData / np.max(imData)
        a = Image.fromarray( imData.astype('float') * 255 )
        name = outfileName + str(peakMZ[i])
        name = name.replace( "." , "_" ) #Replace the "." in the string to make sure pdflatex does not complain about the filename
        name = name +".png"
        names.append( name )
        a.convert('L').save( name , 'PNG' )

    #Generate the latex file
    print "Generating LaTeX file"

    texFileName = outfileName+"summary.tex"
    latexFile = open( texFileName  , 'w' )
    latexFile.write("\documentclass[a4paper,10pt]{article}\n")
    latexFile.write("\usepackage[utf8x]{inputenc}\n")
    latexFile.write("\usepackage{graphicx}\n")
    latexFile.write("\\title{Peak Images}\n")
    latexFile.write("\\author{Oliver Ruebel}\n")
    latexFile.write("\date{"+str(datetime.date.today())+"}\n")
    latexFile.write("\\begin{document}\n")
    latexFile.write("\maketitle\n ")

    #Write the latex table with the image files
    numRows = 6
    numCols = 3
    totalNumRows = int( len(names) / float(numCols) +0.5 )
    for i in range( 0 , totalNumRows ):
        sys.stdout.write("[" +str( int( 100.* float(i)/float(totalNumRows) )) +"%]"+ "\r")
        sys.stdout.flush()

        if i % numRows == 0 :
            latexFile.write("\\begin{center}\n")
            latexFile.write("\\begin{tabular}{lll}\n")

        for ci in xrange(0,numCols) :
            index = i*numCols+ci
            if index < peakMZ.size :
                latexFile.write(str(peakMZ[index]))
            else :
                latexFile.write(" ")
            if ci < (numCols-1) :
                latexFile.write(" & ")
            else :
                latexFile.write( " \\\\ \n")

        for ci in xrange(0,numCols) :
            index = i*numCols+ci
            if index < len(names) :
                latexFile.write("\includegraphics[width=0.3\\textwidth]{"+names[index]+"}")
            else :
                latexFile.write(" ")
            if ci < (numCols-1) :
                latexFile.write(" & ")
            else :
                latexFile.write( " \\\\ \n")

        if i%numRows == (numRows-1) or i == (totalNumRows-1) :
            latexFile.write("\\end{tabular}\n")
            latexFile.write("\\end{center}\n")

    latexFile.write("\\end{document}\n")
Exemplo n.º 23
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

        # Check for correct usage
    if len(argv) != 5:
        print "USAGE: Call \"omsi_npg OMSI_FILE [expIndex dataIndex LPFanalysisIndex]   \" "
        print "Nearby-Peaks Global peakfinding"
        exit(0)

    #Read the input arguments
    omsiInFile = argv[1]
    expIndex = int(argv[2])
    dataIndex = int(argv[3])
    analysisIndex = int(argv[4])

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile)
    except:
        print "Error opening input file:", sys.exc_info()[0]
        exit(0)

    print "Input file: ", omsiInFile
    print "LPF Analysis Index: ", analysisIndex

    #Get the experiment and data
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    analysis = exp.get_analysis(analysisIndex)

    print "\n[loading data...]"
    peaksBins = analysis['LPF_Peaks_MZ'][:]
    peaksIntensities = analysis['LPF_Peaks_Vals'][:]
    peaksArrayIndex = analysis['LPF_Peaks_ArrayIndex'][:]
    peaksMZdata = data.mz[:]
    peaksMZ = peaksMZdata[peaksBins]

    print "[done!] lpf data shapes:"
    print "peaksBins shape: ", peaksBins.shape
    print "peaksIntensities shape: ", peaksIntensities.shape
    print "peaksArrayIndex shape: ", peaksArrayIndex.shape
    print "peaksMZdata shape: ", peaksMZdata.shape
    print "peaksMZ shape: ", peaksMZ.shape
    sys.stdout.flush()

    # npg
    myNPG = omsi_npg(name_key="omsi_npg_" + str(ctime()))
    print "--- Executing NPG ---"
    #myNPG.omsi_npg_exec(peaksBins, peaksIntensities, peaksArrayIndex, peaksMZdata, peaksMZ)
    myNPG.execute(peaksBins=peaksBins,
                  npg_peaks_Intensities=peaksIntensities,
                  npg_peaks_ArrayIndex=peaksArrayIndex,
                  peaksMZdata=peaksMZdata,
                  peaksMZ=peaksMZ)
    print "\nResults:"
    NPGPL = myNPG['npghc_peaks_labels']
    print "NPG HC Peaks Labels: \n", NPGPL
    NPGLL = myNPG['npghc_labels_list']
    print "NPG HC Labels List: \n", NPGLL

    print "\nsaving HDF5 analysis..."
    NPGanalysis, analysisindex = exp.create_analysis(myNPG)
    print "done!"

    print "npg analysis index:", analysisindex
    print "omsi_npg complete for input file: ", omsiInFile, "\n"
Exemplo n.º 24
0
def main(argv=None):
    """Then main function"""

    if argv is None:
        argv = sys.argv

        # Check for correct usage
    if len(argv) < 2:
        print "USAGE: Call \"omsi_lpf OMSI_FILE [expIndex dataIndex peakHeight]	\" "
        print "Local peakfinding using opencl"
        exit(0)

    #Read the input arguments
    omsiInFile = argv[1]

    sys.stdout.flush()
    expIndex = 0
    dataIndex = 0
    mypeakheight = 10

    if len(argv) == 4:
        expIndex = int(argv[2])
        dataIndex = int(argv[3])

    if len(argv) == 5:
        expIndex = int(argv[2])
        dataIndex = int(argv[3])
        mypeakheight = int(argv[4])

    #Open the input HDF5 file
    try:
        omsiFile = omsi_file(omsiInFile, 'a')
    except:
        print "Unexpected openeing the input file:", sys.exc_info()[0]
        exit(0)

    print "Input file: ", omsiInFile

    #Get the experiment and dataset
    exp = omsiFile.get_experiment(expIndex)
    data = exp.get_msidata(dataIndex)
    mzdata = data.mz[:]

    #Execute the peak finding
    myLPF = omsi_lpf(name_key="omsi_lpf_" + str(ctime()))
    print "--- Executing LPF ---"
    myLPF.execute(data, mzdata, peakheight=mypeakheight)
    print "\n\nGetting peak finding analysis results"
    pmz = myLPF['LPF_Peaks_MZ']
    print "pmz:\n", pmz
    pv = myLPF['LPF_Peaks_Vals']
    print "pv:\n", pv
    pai = myLPF['LPF_Peaks_ArrayIndex']
    print "pai:\n", pai

    print "\nsaving HDF5 analysis..."
    analysis, analysisindex = exp.create_analysis(myLPF)
    print "done!"

    print "lpf analysis index:", analysisindex
    print "omsi_lpf complete for input file: ", omsiInFile, "\n"
Exemplo n.º 25
0
    def parse_cl_arguments(self):
        """
        The function assumes that the command line parser has been setup using the initialize_argument_parser(..)

        This function parses all arguments that are specific to the command-line parser itself. Analysis workflow
        arguments are added and parsed later by the add_and_parse_workflow_arguments(...) function.
        The reason for this is two-fold: i) to separate the parsing of analysis arguments and arguments of the
        command-line driver and ii) if the same HDF5 file is used as input and output target, then we need to
        open it first here in append mode before it gets opened in read mode later by the arguments.

        *Side effects:* The function sets:

            - ``self.output_target``
            - ``self.profile_analyses``

        """
        # Parse the arguments and convert them to a dict using vars
        parsed_arguments = vars(self.parser.parse_known_args()[0])

        # Process the --save argument to determine where we should save the output
        if self.output_save_arg_name in parsed_arguments and mpi_helper.get_rank(
        ) == self.mpi_root:
            # Determine the filename and experiment group from the path
            self.output_target = parsed_arguments.pop(
                self.output_save_arg_name)
            if self.output_target is not None:
                output_filename, output_object_path = omsi_file_common.parse_path_string(
                    self.output_target)
                # Create the output file
                if output_filename is None:
                    raise ValueError(
                        "ERROR: Invalid save parameter specification " +
                        self.output_target)
                elif os.path.exists(output_filename
                                    ) and not os.path.isfile(output_filename):
                    raise ValueError(
                        "ERROR: Save parameter not specify a file.")
                if not os.path.exists(output_filename):
                    out_file = omsi_file(output_filename, mode='a')
                    self.output_target = out_file.create_experiment()
                    self.__output_target_self = output_filename
                else:
                    out_file = omsi_file(output_filename, mode='r+')
                    if output_object_path is not None:
                        self.output_target = omsi_file_common.get_omsi_object(
                            out_file[output_object_path])
                    else:
                        if out_file.get_num_experiments() > 0:
                            self.output_target = out_file.get_experiment(0)
                        else:
                            self.output_target = out_file.create_experiment()
        else:
            self.output_target = parsed_arguments.pop(
                self.output_save_arg_name)

        # Process the --profile profiling argument
        if self.profile_arg_name in parsed_arguments:
            self.profile_analyses = parsed_arguments.pop(self.profile_arg_name)

        # Process the --memprofile argument
        if self.profile_mem_arg_name in parsed_arguments:
            self.profile_analyses_mem = parsed_arguments.pop(
                self.profile_mem_arg_name)

        # The --loglevel argument
        if self.log_level_arg_name in parsed_arguments:
            self.user_log_level = parsed_arguments.pop(self.log_level_arg_name)
            if self.user_log_level in log_helper.log_levels.keys():
                log_helper.set_log_level(
                    level=log_helper.log_levels[self.user_log_level])
            else:
                self.user_log_level = None
                log_helper.error(module_name=__name__,
                                 message="Invalid log level specified")

        # The --script arguments
        if self.script_arg_name in parsed_arguments:
            self.script_files = parsed_arguments.pop(self.script_arg_name)
            if self.workflow_executor is None:
                self.create_workflow_executor_object()
            else:
                self.workflow_executor.add_analysis_from_scripts(
                    script_files=self.script_files)
Exemplo n.º 26
0
 def setUp(self):
     self.named_temporary_file = tempfile.NamedTemporaryFile()
     self.test_filename = self.named_temporary_file.name
     self.testfile = omsi_file(self.test_filename)
Exemplo n.º 27
0
    def get_omsi_object(cls, h5py_object, resolve_dependencies=False):
        """
        This static method is convenience function used to retrieve the corresponding interface class for a
        given h5py group object.

        :param h5py_object: h5py object for which the corresponding omsi_file API object should be generated.
                        This may also be a string describing the requested object based on a combination of the
                        path to the file and a path ot the object <filename.h5>:<object_path>
        :param resolve_dependencies: Set to True if omsi_file_dependencydata objects should be resolved to retrieve
                        the dependent object the dependency is pointing to. Dependencies are resolved recursively,
                        i.e., if a dependency points to another dependency then that one will be resolved as well.
                        Default value is False, i.e., the omis_file_dependency object itself is returned.

        :returns: None in case no corresponding object was found. Otherwise an instance of:

            * omsi_file : If the given object is a h5py.File object
            * omsi_file_experiment : If the given object is an experiment groupt
            * omsi_file_methods : If the given object is a method group
            * omsi_file_instrument : If the given object is an instrument group
            * omsi_file_analysis : If the given object is an analysis group
            * omsi_file_msidata : If the given object is a MSI data group
            * omsi_file_dependencydata : If the fiven object is a dependency group
            * The input h5py_object: If the given object is a h5py.Dataset or h5py.Group
            * None: In case that an unknown type is given.
        """
        from omsi.dataformat.omsi_file.experiment import omsi_file_experiment
        from omsi.dataformat.omsi_file.main_file import omsi_file
        from omsi.dataformat.omsi_file.dependencies import omsi_file_dependencies, omsi_file_dependencydata
        from omsi.dataformat.omsi_file.instrument import omsi_file_instrument
        from omsi.dataformat.omsi_file.methods import omsi_file_methods
        from omsi.dataformat.omsi_file.analysis import omsi_file_analysis
        from omsi.dataformat.omsi_file.msidata import omsi_file_msidata
        from omsi.dataformat.omsi_file.metadata_collection import omsi_file_metadata_collection

        # If the input object is already an omsi API object then return it as is
        if isinstance(h5py_object, omsi_file) or \
                isinstance(h5py_object, omsi_file_experiment) or \
                isinstance(h5py_object, omsi_file_methods) or\
                isinstance(h5py_object, omsi_file_instrument) or \
                isinstance(h5py_object, omsi_file_analysis) or \
                isinstance(h5py_object, omsi_file_msidata) or \
                isinstance(h5py_object, omsi_file_dependencies) or \
                isinstance(h5py_object, omsi_file_dependencydata) or \
                isinstance(h5py_object, omsi_file_metadata_collection):
            return h5py_object
        # IF we have an h5py.File then create and omsi_file
        if isinstance(h5py_object, h5py.File):
            return omsi_file(h5py_object)
        # If we have an h5py.Group, then try to create a corresponding omsi API object
        elif isinstance(h5py_object, h5py.Group):
            # Check if the group has an explicit type attribute
            try:
                # Try to determine the type of the group based on the
                # attributes
                type_attribute = h5py_object.attrs[
                    omsi_format_common.type_attribute]
                if type_attribute == "omsi_file_experiment":
                    return omsi_file_experiment(h5py_object)
                elif type_attribute == "omsi_file_methods" or type_attribute == "omsi_file_methods":
                    return omsi_file_methods(h5py_object)
                elif type_attribute == "omsi_file_instrument":
                    return omsi_file_instrument(h5py_object)
                elif type_attribute == "omsi_file_analysis":
                    return omsi_file_analysis(h5py_object)
                elif type_attribute == "omsi_file_msidata":
                    return omsi_file_msidata(h5py_object)
                elif type_attribute == "omsi_file":
                    return omsi_file(h5py_object)
                elif type_attribute == "omsi_file_dependencydata":
                    omsiobject = omsi_file_dependencydata(h5py_object)
                    if resolve_dependencies:
                        return omsi_file_common.get_omsi_object(
                            omsi_file_common.get_h5py_object(
                                omsiobject.get_dependency_omsiobject(),
                                resolve_dependencies))
                    else:
                        return omsiobject
                elif type_attribute == "omsi_file_dependencies":
                    return omsi_file_dependencies(h5py_object)
                elif type_attribute == 'omsi_file_metadata_collection':
                    return omsi_file_metadata_collection(h5py_object)
                else:
                    return h5py_object
            except:
                # If the attribute is missing, then try to determine the type
                # based on he name of group
                groupname = h5py_object.name.split("/")[-1]
                parentgroupname = h5py_object.parent.name.split("/")[-1]
                if groupname.startswith(omsi_format_experiment.exp_groupname):
                    return omsi_file_experiment(h5py_object)
                elif groupname.startswith(omsi_format_methods.methods_groupname) or \
                        groupname.startswith(omsi_format_methods.methods_old_groupname):
                    return omsi_file_methods(h5py_object)
                elif groupname.startswith(
                        omsi_format_instrument.instrument_groupname):
                    return omsi_file_instrument(h5py_object)
                elif groupname.startswith(
                        omsi_format_analysis.analysis_groupname):
                    return omsi_file_analysis(h5py_object)
                elif groupname.startswith(omsi_format_data.data_groupname):
                    return omsi_file_msidata(h5py_object)
                elif groupname.startswith(
                        omsi_format_dependencies.dependencies_groupname):
                    return omsi_file_dependencies(h5py_object)
                elif parentgroupname.startswith(
                        omsi_format_dependencies.dependencies_groupname):
                    omsiobject = omsi_file_dependencydata(h5py_object)
                    if resolve_dependencies:
                        return omsi_file_common.get_omsi_object(
                            omsi_file_common.get_h5py_object(
                                omsiobject.get_dependency_omsiobject(),
                                resolve_dependencies))
                    else:
                        return omsiobject
                elif parentgroupname.startswith(
                        omsi_format_metadata_collection.
                        metadata_collection_groupname_default):
                    return omsi_file_metadata_collection(h5py_object)
                elif groupname == "":  # We are at the root group
                    return omsi_file(h5py_object.file)
                else:
                    return h5py_object
        # If we have an hpy.Dataset then we don't have a corresponding API object. Return as is
        elif isinstance(h5py_object, h5py.Dataset):
            return h5py_object
        elif isinstance(h5py_object, basestring):
            import os
            filename, object_path = cls.parse_path_string(h5py_object)
            if filename is not None and os.path.exists(
                    filename) and os.path.isfile(filename):
                try:
                    curr_omsi_file = omsi_file(filename, 'r')
                except:
                    return None
            else:
                return None

            if object_path is not None:
                try:
                    file_object = curr_omsi_file.managed_group[object_path]
                except KeyError:
                    return None
                return omsi_file_common.get_omsi_object(
                    file_object, resolve_dependencies=True)
            else:
                if isinstance(curr_omsi_file, omsi_file):
                    return curr_omsi_file
                else:
                    raise ValueError('omsi_file_common.Invalid path or file')
        else:
            return None
Exemplo n.º 28
0
def main(argv=None):
    """Then main function"""

    if argv is None:
        argv = sys.argv

    # Check for correct usage
    if len(argv) < 3:
        print_help()
        sys.exit(0)

    infile = argv[1]
    experiment_index = int(argv[2])
    analysis_index = int(argv[3])
    output_filename = argv[4]

    # Open the file and get to the data
    # Note the analysis and experiment index may vary for different datasets
    omsi_input_file = omsi_file(infile, 'r')
    input_experiment = omsi_input_file.get_experiment(experiment_index)
    input_analysis = input_experiment.get_analysis(analysis_index)
    # Get the peak finding data abd load it all
    input_dataset = input_analysis['peak_cube']
    input_mz_dataset = input_analysis['peak_mz']
    print input_analysis.items()

    # Load all the peak finding data
    peak_cube = input_dataset[:]
    peak_mz = input_mz_dataset[:]
    x_dim = peak_cube.shape[0]
    y_dim = peak_cube.shape[1]
    num_slices = peak_cube.shape[2]

    # Generate the images
    print "Generating images"
    names = []
    for row_index in range(0, num_slices):
        sys.stdout.write("[" + str(int(100. * float(row_index)/float(num_slices))) + "%]" + "\r")
        sys.stdout.flush()
        image_data = peak_cube[:, :, row_index]
        image_data = image_data.reshape((x_dim, y_dim))
        image_data = image_data / np.max(image_data)
        output_images = Image.fromarray(image_data.astype('float') * 255)
        name = output_filename + str(peak_mz[row_index])
        # Replace the "." in the string to make sure pdflatex does not complain about the filename
        name = name.replace(".", "_")
        name += ".png"
        names.append(name)
        output_images.convert('L').save(name, 'PNG')

    # Generate the latex file
    print "Generating LaTeX file"

    tex_filename = output_filename+"summary.tex"
    latex_file = open(tex_filename, 'w')
    latex_file.write("\documentclass[a4paper,10pt]{article}\n")
    latex_file.write("\usepackage[utf8x]{inputenc}\n")
    latex_file.write("\usepackage{graphicx}\n")
    latex_file.write("\\title{Peak Images}\n")
    latex_file.write("\\author{Oliver Ruebel}\n")
    latex_file.write("\date{"+str(datetime.date.today())+"}\n")
    latex_file.write("\\begin{document}\n")
    latex_file.write("\maketitle\n ")

    # Write the latex table with the image files
    number_of_columns = 3
    res = float(x_dim) / float(y_dim)
    number_of_rows = int(1.29 / (0.3*(float(x_dim)/float(y_dim))))
    if number_of_rows < 1:
        number_of_rows = 1
        print "WARNING: Images may not fit on a single page. Reduce the width of image plots"
    total_number_of_rows = int(len(names) / float(number_of_columns) + 0.5)
    for row_index in range(0, total_number_of_rows):
        sys.stdout.write("[" + str(int(100. * float(row_index)/float(total_number_of_rows))) + "%]" + "\r")
        sys.stdout.flush()
        if row_index % number_of_rows == 0:
            latex_file.write("\\begin{center}\n")
            latex_file.write("\\begin{tabular}{lll}\n")

        for column_index in xrange(0, number_of_columns):
            index = row_index*number_of_columns+column_index
            if index < peak_mz.size:
                latex_file.write(str(peak_mz[index]))
            else:
                latex_file.write(" ")
            if column_index < (number_of_columns-1):
                latex_file.write(" & ")
            else:
                latex_file.write(" \\\\ \n")

        for column_index in xrange(0, number_of_columns):
            index = row_index*number_of_columns+column_index
            if index < len(names):
                latex_file.write("\includegraphics[width=0.3\\textwidth]{"+os.path.basename(names[index])+"}")
            else:
                latex_file.write(" ")
            if column_index < (number_of_columns-1):
                latex_file.write(" & ")
            else:
                latex_file.write(" \\\\ \n")

        if row_index % number_of_rows == (number_of_rows-1) or row_index == (total_number_of_rows-1):
            latex_file.write("\\end{tabular}\n")
            latex_file.write("\\end{center}\n")

    latex_file.write("\\end{document}\n")
    latex_file.close()

    print "Building the PDF file"
    abs_path = os.path.abspath(tex_filename)
    latex_dir = os.path.dirname(abs_path)
    latex_filename = os.path.basename(abs_path)
    subprocess.call(["pdflatex", latex_filename], cwd=latex_dir)