Class: Cicada::FileInteraction

Inherits:
Object
  • Object
show all
Defined in:
lib/cicada/file_interaction.rb

Overview

A collection of methods for interacting with input and output files for cicada.

Constant Summary collapse

REQUIRED_PARAMETERS =

parameters required by the methods in this class

[:dirname_set, :basename_set, :mask_relative_dirname, :mask_extra_extension, :data_directory, :correction_date, :output_positions_to_directory]
OPTIONAL_PARAMETERS =

parmeters used but not required in this class or only required for optional functionality

[:in_situ_aberr_corr_basename_set]
POS_XML_EXTENSION =

extension on position data (image object) files.

"_position_data.xml"
POS_HUMAN_EXTENSION =

extension on human-friendly position data (image object) files.

"_position_data.csv"
CORR_XML_EXTENSION =

extension on the correction files

"_correction.xml"
DIFFS_TXT_EXTENSION =

extension on the distance measurement files

"_diffs.txt"
MULTI_NAME_SEP =

separator used in the parameter file for multiple files, directories, etc.

","

Class Method Summary collapse

Class Method Details

.correction_filename(p) ⇒ String

Gets the filename for storing/reading the correction based upon the supplied parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the correction file location.

Returns:

  • (String)

    the filename for the correction file.



322
323
324
325
326
# File 'lib/cicada/file_interaction.rb', line 322

def self.correction_filename(p)
  dir = p[:data_directory]
  fn = p[:correction_date]
  File.expand_path(fn + CORR_XML_EXTENSION, dir)
end

.human_friendly_position_data_filename(p) ⇒ String

Gets the filename to which human-friendly-formatted object positions will be written.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the positions.

Returns:

  • (String)

    the absolute path to the position file.



162
163
164
165
# File 'lib/cicada/file_interaction.rb', line 162

def self.human_friendly_position_data_filename(p)
  dir = p[:data_directory]
  File.expand_path(p[:basename_set].split(MULTI_NAME_SEP)[0] + POS_HUMAN_EXTENSION, dir)
end

.in_situ_corr_data_filename(p) ⇒ String

Gets the filename of data to use for in situ correction from a parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the in situ correction data

Returns:

  • (String)

    the absolute path to the in situ correction data file



175
176
177
178
# File 'lib/cicada/file_interaction.rb', line 175

def self.in_situ_corr_data_filename(p)
  dir = p[:data_directory]
  File.expand_path(p[:in_situ_aberr_corr_basename_set].split(MULTI_NAME_SEP)[0] + POS_XML_EXTENSION, dir)
end

.list_files(p) ⇒ Array<OpenStruct>

Lists all the files and masks to be analyzed given a parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the analysis

Returns:

  • (Array<OpenStruct>)

    an array of objects that respond to #image_fn and #mask_fn, which return each image’s filename and its paired mask’s filename respectively.



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cicada/file_interaction.rb', line 241

def self.list_files(p)
  dirnames = p[:dirname_set].split(MULTI_NAME_SEP)
  basenames = p[:basename_set].split(MULTI_NAME_SEP)
  image_sets = []

  dirnames.each do |d|
    mask_dirname = File.join(d, p[:mask_relative_dirname])
    Dir.foreach(d) do |f|
      if basenames.any? { |e| f.match(e) } then    
        im = File.expand_path(f, d)
        msk = File.expand_path(f + p[:mask_extra_extension], mask_dirname)
        current = OpenStruct.new(image_fn: im, mask_fn: msk)    
        image_sets << current
      end          
    end
  end    
  image_sets
end

.load_image(image_fn) ⇒ ReadOnlyImage

Loads an image from the specified file.

Parameters:

  • image_fn (String)

    the image’s filename

Returns:

  • (ReadOnlyImage)

    the image at the specified filename



139
140
141
# File 'lib/cicada/file_interaction.rb', line 139

def self.load_image(image_fn)
  RImageAnalysisTools.get_image(image_fn)
end

.position_data_filename(p) ⇒ String

Gets the filename to which / from which image object positions will be written / read from a parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the positions.

Returns:

  • (String)

    the absolute path to the position file.



150
151
152
153
# File 'lib/cicada/file_interaction.rb', line 150

def self.position_data_filename(p)
  dir = p[:data_directory]
  File.expand_path(p[:basename_set].split(MULTI_NAME_SEP)[0] + POS_XML_EXTENSION, dir)
end

.position_file_exists?(p) ⇒ Boolean

Checks if the position data file already exists.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the positions.

Returns:

  • (Boolean)

    whether the position data file exists.



187
188
189
# File 'lib/cicada/file_interaction.rb', line 187

def self.position_file_exists?(p)
  File.exist?(FileInteraction.position_data_filename(p))
end

.read_in_situ_corr_data(p) ⇒ Array<ImageObject>

Reads the image objects for in situ correction associated with an analysis specified by

a parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the in situ correction data

Returns:

  • (Array<ImageObject>)

    the image objects for in situ correction associated with the analysis



228
229
230
231
# File 'lib/cicada/file_interaction.rb', line 228

def self.read_in_situ_corr_data(p)
  fn = FileInteraction.in_situ_corr_data_filename(p)
  FileInteraction.unserialize_position_data_file(fn)
end

.read_position_data(p) ⇒ Array<ImageObject>

Reads the image objects associated with an analysis specified by a parameter dictionary.

Parameters:

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the positions.

Returns:

  • (Array<ImageObject>)

    the image objects associated with the analysis



214
215
216
217
# File 'lib/cicada/file_interaction.rb', line 214

def self.read_position_data(p)
  fn = FileInteraction.position_data_filename(p)
  FileInteraction.unserialize_position_data_file(fn)
end

.unserialize_position_data_file(fn) ⇒ Array<ImageObject>

Unserializes image object position data from a specified file using the methods in the Serialization

class.

Parameters:

  • fn (String)

    the name of the data file.

Returns:

  • (Array<ImageObject>)

    the image objects contained in the file.



199
200
201
202
203
204
205
# File 'lib/cicada/file_interaction.rb', line 199

def self.unserialize_position_data_file(fn)
  data_str = nil
  File.open(fn) do |f|
    data_str = f.read
  end
  Serialization.unserialize_image_objects(data_str)
end

.write_differences(diffs, p) ⇒ void

This method returns an undefined value.

Writes an array of distance measurements to file based upon the supplied parameter dictionary.

Parameters:

  • diffs (Enumerable<#to_s>)

    an enumerable list of distance meaurements.

  • p (ParameterDictionary, hash)

    a hash-like object specifying the file location.



336
337
338
339
340
341
342
343
344
# File 'lib/cicada/file_interaction.rb', line 336

def self.write_differences(diffs, p)
  dirname = p[:output_positions_to_directory]
  fn = File.expand_path(p[:basename_set] + DIFFS_TXT_EXTENSION, dirname)
  File.open(fn, 'w') do |f|
    diffs.each do |d|
      f.puts(d.to_s)
    end
  end
end

.write_human_friendly_position_data_file(image_objects, fn) ⇒ Object

Writes the provided image objects to a human-readable file at the location specified.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cicada/file_interaction.rb', line 295

def self.write_human_friendly_position_data_file(image_objects, fn)
  CSV.open(fn, 'wb') do |csv|
    obj = image_objects[0]
    n_channels = obj.getFitParametersByChannel.size
    headers = ["object_id"]
    n_channels.times do |i|
      headers.concat(["pos#{i}_x", "pos#{i}_y", "pos#{i}_z"])
    end
    csv << headers

    image_objects.each do |im_obj|
      row = [im_obj.getLabel]
      n_channels.times do |i|
        row.concat(im_obj.getPositionForChannel(i).toArray)
      end
      csv << row
    end
  end
end

.write_position_data(image_objects, p) ⇒ void

This method returns an undefined value.

Writes the provided image objects to file to the location specified in a parameter dictionary.

Parameters:

  • image_objects (Enumerable<ImageObject>)

    the objects that will be written to file.

  • p (ParameterDictionary, Hash)

    a hash-like object specifying the filename for the data.



268
269
270
271
272
273
# File 'lib/cicada/file_interaction.rb', line 268

def self.write_position_data(image_objects, p)
  fn = position_data_filename(p)
  write_position_data_file(image_objects,fn)
  fn2 = human_friendly_position_data_filename(p)
  write_human_friendly_position_data_file(image_objects, fn2)
end

.write_position_data_file(image_objects, fn) ⇒ void

This method returns an undefined value.

Writes the provided image objects to file to the location specified.

Parameters:

  • image_objects (Enumerable<ImageObject>)

    the objects that will be written to file.

  • fn (String)

    the filename of the file to which to write the data



283
284
285
286
287
# File 'lib/cicada/file_interaction.rb', line 283

def self.write_position_data_file(image_objects, fn)
  File.open(fn, 'w') do |f|
    f.write(Serialization.serialize_image_objects(image_objects))
  end
end