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.



400
401
402
403
404
405
406
407
# File 'lib/cicada/file_interaction.rb', line 400

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.



193
194
195
196
# File 'lib/cicada/file_interaction.rb', line 193

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



207
208
209
210
# File 'lib/cicada/file_interaction.rb', line 207

def self.in_situ_corr_data_filename(p)
  dir = [: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.



283
284
285
286
287
288
289
290
291
292
293
294
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 283

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



166
167
168
169
170
# File 'lib/cicada/file_interaction.rb', line 166

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.



180
181
182
183
# File 'lib/cicada/file_interaction.rb', line 180

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.



219
220
221
# File 'lib/cicada/file_interaction.rb', line 219

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



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

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



250
251
252
253
254
255
256
# File 'lib/cicada/file_interaction.rb', line 250

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.



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/cicada/file_interaction.rb', line 231

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.



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/cicada/file_interaction.rb', line 417

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.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/cicada/file_interaction.rb', line 359

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.



323
324
325
326
327
328
329
330
331
332
333
# File 'lib/cicada/file_interaction.rb', line 323

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



343
344
345
346
347
348
349
350
351
# File 'lib/cicada/file_interaction.rb', line 343

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