Commit 8bd942a9 authored by Médéric Boquien's avatar Médéric Boquien
Browse files

Replace the SED.info OrderedDict() with a very light weight and speficically...

Replace the SED.info OrderedDict() with a very light weight and speficically tailored reimplementation of an OrderedDict(). The reason is that an OrderedDict() takes an inordinate amount to time to copy. In the end this could amount to a very significant fraction of the total runtime. Do not use this new implementation anywhere or it will break your code. It makes very strong assumptions on how it is to be used.
parent 08f7084a
......@@ -232,7 +232,7 @@ class PdfAnalysis(AnalysisModule):
analysed_std)
save_table_best('best_models.txt', obs_table['id'], best_chi2,
best_chi2_red, best_parameters, best_fluxes, filters,
info)
info.keys())
if mock_flag is True:
......@@ -296,7 +296,7 @@ class PdfAnalysis(AnalysisModule):
analysed_std)
save_table_best('best_mock_models.txt', mock_table['id'],
best_chi2, best_chi2_red, best_parameters,
best_fluxes, filters, info)
best_fluxes, filters, info.keys())
print("Run completed!")
......
......@@ -31,8 +31,8 @@ Such SED is characterised by:
"""
import numpy as np
from collections import OrderedDict
from . import utils
from .infodict import InfoDict
from .io.vo import save_sed_to_vo
from scipy.constants import c, parsec
from ..data import Database
......@@ -69,7 +69,7 @@ class SED(object):
self.contribution_names = []
self.luminosity = None
self.luminosities = None
self.info = OrderedDict()
self.info = InfoDict()
self.mass_proportional_info = set()
@property
......
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Institute of Astronomy
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Médéric Boquien
"""
This class is the implementation of a light weight ordered-dictionary-like
object. Its main advantage is that it is very fast to copy, unlike
OrderedDict(), which can take a significant fraction of the runtime.
It makes some very strong assumptions on the data and how the object is
used. Because of this, it should not be used for anything else other than the
info member of the SED class.
The implementation is based on the emulation of a container type and therefore
gives some standard function to behave like a dictionary, making it a drop-in
replacement. The class members are self-explanatory.
"""
class InfoDict(object):
"""Light weight ordered-dictionary-like object that is fast to copy.
"""
def __init__(self, keys=[], values=[]):
self.k = keys[:]
self.v = values[:]
def __len__(self):
return len(self.k)
def __getitem__(self, key):
try:
idx = self.k.index(key)
return self.v[idx]
except ValueError:
raise KeyError
def __setitem__(self, key, value):
if key not in self.k:
self.k.append(key)
self.v.append(value)
else:
self.v[self.k.index(key)] = value
def __contains__(self, key):
if key in self.k:
return True
else:
return False
def keys(self):
return self.k[:]
def values(self):
return self.v[:]
def items(self):
return ((k, v) for k, v in zip(self.k, self.v))
def copy(self):
return InfoDict(self.k, self.v)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment