ResultDict

class threadcount.fit.ResultDict(data_array=None, names=None, data_dict=None, generate_pixel_coordinates=True, comment='')[source]

Bases: OrderedDict

Container for ordered dict of numpy ndarrays with save/load functionality.

Create a new ResultDict and possibly generate row/column images.

This class is intended to give you a way to easily access ‘images’ of various parameters, whether it’s a signal-to-noise image or a map of how many gaussians are in the best fit to your spaxel, or a map of fit parameters. The class also provides methods for saving and loading: savetxt() and loadtxt().

A big caveat here is that I have not implemented internal checks to make sure that all the data are the same spatial shape.

The intention is for each entry in the arrays should contain information consistently for a given spaxel.

This constructor will create an OrderedDict from data_dict, and then update that dict with key, value pairs calculated from (for i in range(len(names))) names[i] : data_array[i]. This means that the order of keys will be of data_dict.keys(), then any new entries in names (any overlapping keys will be overridden with the value from data_array).

The default behavior is (generate_pixel_coordinates is True) to determine the shape of the first value (array) in the OrderedDict, and create a coordinate ‘image’ for all the dimensions necessary, and named from DIM_NAMES, and moved to the beginning of the dict. This will give “columns” in the output file called “row”, “col” and contain the indices of each spaxel.

A comment string may be provided, and is saved as a header during savetxt, or is read from the header in loadtxt.

Parameters:
  • data_array (numpy ndarray, optional) – Array where data_array[0] corresponds to names[0], by default None

  • names (list of string, optional) – The dictionary keys that will correspond to data_array, by default None

  • data_dict (dict of numpy ndarray, optional) – Dictionary to start with, by default None

  • generate_pixel_coordinates (bool, optional) – Whether to generate “row”, “col” entries, by default True

  • comment (str, optional) – A string that will be saved/loaded as a header from the txt file, by default “”

Raises:

ValueError – When there’s a mismatch between the length of names and the number of entries in data_array

Attributes Summary

DIM_NAMES

Default dimension labels, outer -> inner names.

Methods Summary

data()

Prepare for saving with numpy.savetxt().

loadtxt(fname[, comments, delimiter])

Load a file which has been saved by savetxt().

names()

Return a list of all the keys in the dictionary.

savetxt(fname[, delimiter, header, fmt])

Save this information to a text file.

Attributes Documentation

DIM_NAMES = ('panel', 'row', 'col')

Default dimension labels, outer -> inner names.

Methods Documentation

data()[source]

Prepare for saving with numpy.savetxt().

Returns:

A flattened numpy array, transposed such that each spaxel’s information will be on one line in the output file.

Return type:

numpy ndarray

classmethod loadtxt(fname, comments='#', delimiter=None, **kwargs)[source]

Load a file which has been saved by savetxt().

This should ideally load that text file into one of these ResultDict objects. The function reads in the header comment string, and adds all but the final line to the ResultDict.comment attribute. The final line is used to create the keys for the entries.

numpy.loadtxt() is used to load the data. The keys are searched for any entry in DIM_NAMES to search for labels like row or col. If found, the data is sorted on those columns, and then each column is reshaped into a spatial image. The row and/or col indices arrays are created again and compared to the read-in values to ensure our spatial placement was accurate.

Parameters:
  • fname (str) – filename to read in

  • comments (str, optional) – The numpy.loadtxt() keyword, indicating the start of a comment, by default “#”

  • delimiter (str, optional) – The numpy.loadtxt() keyword, indicating the string used to separate values, by default None (meaning whitespace).

Returns:

A reconstructed ResultDict.

Return type:

ResultDict

Raises:

ValueError – If generated row/col indices do not match the read-in indices. This will likely be caused by missing row/col entries, as the method I use to reshape the arrays does not deal with that possibility yet.

names()[source]

Return a list of all the keys in the dictionary.

Returns:

A list of the dictionary keys.

Return type:

list of string

savetxt(fname, delimiter='\t', header='', fmt='%.8g', **kwargs)[source]

Save this information to a text file.

This function translates between this class and numpy.savetxt().

The header will consist of header + self.comment + computed label row. The delimiter, header, and fmt are updated into the kwargs dict which is passed straight to numpy.savetxt().

Parameters:
  • fname (string) – Filename path string

  • delimiter (str, optional) – string delimiter to separate columns, by default “\t”

  • header (str, optional) – Any additional header string, by default “”

  • fmt (str, optional) – See numpy.savetxt(), by default “%”+FLOAT_FMT

  • **kwargs (dict, optional) – Any further keyword arguments passed to numpy.savetxt().