Guest User

Untitled

a guest
Dec 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. """
  2. Concat spectrum from different sensors.
  3. """
  4. from io import StringIO
  5. import logging
  6.  
  7. import click
  8. import coloredlogs
  9. import numpy as np
  10. import pandas as pd
  11.  
  12. coloredlogs.install(
  13. level='DEBUG',
  14. fmt='%(asctime)s %(module)s[%(process)d] %(levelname)s %(message)s',
  15. datefmt='%H:%M:%S'
  16. )
  17.  
  18. logger = logging.getLogger(__name__)
  19.  
  20. def load_file(path, offset=22):
  21. with open(path, 'r') as fd:
  22. lines = fd.readlines()
  23. if offset:
  24. lines = lines[offset:]
  25.  
  26. buffer = StringIO(''.join(lines))
  27. df = pd.read_csv(
  28. buffer,
  29. sep='\t', # parse by tabs
  30. names=['wavelength', 'intensity'], index_col=False
  31. )
  32. df.set_index('wavelength', inplace=True)
  33. return df
  34.  
  35. def concat_data(x, y, mode='lstsq'):
  36. """
  37. Concat two spectrum X and Y using specified region as reference.
  38. """
  39. overlap = x.index & y.index
  40. xo, yo = x.loc[overlap], y.loc[overlap]
  41.  
  42. if mode == 'lstsq':
  43. A = np.vstack([xo['intensity'].values, np.ones(len(xo))]).T
  44. m, c = np.linalg.lstsq(A, yo['intensity'].values, rcond=None)[0]
  45. y = (y-c)/m
  46. elif mode == 'last':
  47. y /= yo.iloc[-1]/xo.iloc[-1]
  48. else:
  49. raise ValueError("unknown concatenation method")
  50.  
  51. return x.combine_first(y)
  52.  
  53. def normalize_peak(df, lpf=500.):
  54. logger.info("using data after {:.2f} nm to normalize".format(lpf))
  55. df_max = df[df.index > lpf].max()
  56. df /= df_max
  57. return df
  58.  
  59. @click.command()
  60. @click.argument('short', type=click.Path(exists=True))
  61. @click.argument('long', type=click.Path(exists=True))
  62. @click.argument('output')
  63. @click.option('--mode', type=click.Choice(['lstsq', 'last']),
  64. help='Method to determine concatenation ratio.')
  65. @click.option('--no-norm', 'norm', is_flag=True, default=False,
  66. help='Do not normalize the result to [0, 1].')
  67. @click.option('--lpf', type=np.float32, default=500.,
  68. help='Long-pass frequency used in normalization, default 500.0 nm.')
  69. def main(short, long, output, mode, norm, lpf):
  70. """
  71. This script concat spectrum from SHORT and LONG file and save the resolved
  72. result in OUTPUT. Spectrum from SHORT is favored over LONG.
  73. """
  74. sh_data = load_file(short)
  75. ln_data = load_file(long)
  76.  
  77. if mode is None:
  78. mode = 'lstsq'
  79. df = concat_data(sh_data, ln_data, mode)
  80. if not norm:
  81. df = normalize_peak(df, lpf)
  82.  
  83. df.to_csv(output, index_label='wavelength', header=True, float_format='%.6g')
  84. logger.info("result saved to \"{}\"".format(output))
  85.  
  86. if __name__ == '__main__':
  87. try:
  88. main()
  89. except Exception as e:
  90. logger.exception(str(e))
Add Comment
Please, Sign In to add comment