From 1d1cb6d7992aaa54562d888fa6e975c0e77094d4 Mon Sep 17 00:00:00 2001 From: BO ZHANG Date: Tue, 23 Aug 2022 14:25:06 +0800 Subject: [PATCH] major updates --- csst_proto/__init__.py | 5 ++ csst_proto/data/data.csv | 3 ++ csst_proto/flip_image.py | 35 ++++++++++++ .../your_data.txt => some_other_modules.py} | 0 csst_proto/top_level_interface.py | 4 ++ demo/__init__.py | 0 demo/demo_processor.py | 43 --------------- examples/how_this_code_will_be_used.py | 53 +++++++++++++++++++ .../how_to_write_docstring.py | 4 +- 9 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 csst_proto/data/data.csv rename csst_proto/{data/your_data.txt => some_other_modules.py} (100%) create mode 100644 csst_proto/top_level_interface.py delete mode 100644 demo/__init__.py delete mode 100644 demo/demo_processor.py create mode 100644 examples/how_this_code_will_be_used.py rename demo/demo_docstring.py => examples/how_to_write_docstring.py (91%) diff --git a/csst_proto/__init__.py b/csst_proto/__init__.py index f102a9c..ba61ccc 100644 --- a/csst_proto/__init__.py +++ b/csst_proto/__init__.py @@ -1 +1,6 @@ +import os +from .top_level_interface import * + __version__ = "0.0.1" + +PACKAGE_PATH = os.path.dirname(__file__) diff --git a/csst_proto/data/data.csv b/csst_proto/data/data.csv new file mode 100644 index 0000000..cd92b6f --- /dev/null +++ b/csst_proto/data/data.csv @@ -0,0 +1,3 @@ +a, b, c +1,2,3 +4,5,6 \ No newline at end of file diff --git a/csst_proto/flip_image.py b/csst_proto/flip_image.py index e69de29..109e7a1 100644 --- a/csst_proto/flip_image.py +++ b/csst_proto/flip_image.py @@ -0,0 +1,35 @@ +import numpy as np +from astropy import table +from . import PACKAGE_PATH + + +# the main algorithm +def flip_image(img: np.ndarray): + """ flip an input image + + This function uses an awesome algorithm to flip an input image. + + Parameters + ---------- + img: + the input image + + Returns + ------- + the flipped image + + """ + try: + assert img.ndim == 2 + except AssertionError: + raise AssertionError("The input image is {}D not 2D!".format(img.ndim)) + return img[::-1, ::-1] + + +# a demo on how to package data +def print_data(): + """ print out data """ + fp_data = PACKAGE_PATH + "/data/data.csv" + data = table.Table.read(fp_data) + print(data) + return diff --git a/csst_proto/data/your_data.txt b/csst_proto/some_other_modules.py similarity index 100% rename from csst_proto/data/your_data.txt rename to csst_proto/some_other_modules.py diff --git a/csst_proto/top_level_interface.py b/csst_proto/top_level_interface.py new file mode 100644 index 0000000..aa96701 --- /dev/null +++ b/csst_proto/top_level_interface.py @@ -0,0 +1,4 @@ +from .flip_image import flip_image, print_data + + +__all__ = ["flip_image", "print_data"] diff --git a/demo/__init__.py b/demo/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/demo/demo_processor.py b/demo/demo_processor.py deleted file mode 100644 index be22f22..0000000 --- a/demo/demo_processor.py +++ /dev/null @@ -1,43 +0,0 @@ -import joblib - -from csst.core.processor import CsstProcessor -from csst.msc.data_manager import CsstMscDataManager - - -class CsstProcPhotonAbsorption(CsstProcessor): - """ This processor absorbs photons """ - - def __init__(self, dm: CsstMscDataManager, n_jobs: int = 1): - super(CsstProcPhotonAbsorption, self).__init__() - self.dm = dm - self.n_jobs = n_jobs - - def run(self, debug=False): - """ run this processor - - Parameters - ---------- - debug - if True, use debug mode - - Returns - ------- - - """ - joblib.Parallel(n_jobs=self.n_jobs)( - joblib.delayed(CsstProcPhotonAbsorption.run_one_chip)(this_ccd_id) - for this_ccd_id in self.dm.target_ccd_ids) - return - - def prepare(self, **kwargs): - """ prepare the environment """ - return - - def cleanup(self): - """ clean up the environment """ - return - - @staticmethod - def run_one_chip(ccd_id): - """ run for one chip """ - return diff --git a/examples/how_this_code_will_be_used.py b/examples/how_this_code_will_be_used.py new file mode 100644 index 0000000..a1071e6 --- /dev/null +++ b/examples/how_this_code_will_be_used.py @@ -0,0 +1,53 @@ +import joblib +from astropy.io import fits +from csst.core.processor import CsstProcessor +from csst.msc.data_manager import CsstMscDataManager + +from csst_proto import flip_image + + +class CsstProcFlipImage(CsstProcessor): + """ This processor absorbs photons """ + + def __init__(self, dm: CsstMscDataManager, n_jobs: int = 1): + super(CsstProcFlipImage, self).__init__() + self.dm = dm + self.n_jobs = n_jobs + + def run(self, debug: bool = False): + """ run this processor + + Parameters + ---------- + debug: + if True, use debug mode + + Returns + ------- + + """ + img_flipped_list = joblib.Parallel(n_jobs=self.n_jobs)( + joblib.delayed(CsstProcFlipImage.run_one_chip)(this_ccd_id) + for this_ccd_id in self.dm.target_ccd_ids) + return img_flipped_list + + def prepare(self, **kwargs): + """ prepare the environment """ + return + + def cleanup(self): + """ clean up the environment """ + return + + @staticmethod + def run_one_chip(dm, ccd_id): + """ run for one chip + + this function + """ + # input file path + fp_input = dm.l1_ccd(ccd_id, post="img.fits") + img_input = fits.getdata(fp_input) + # flip image + img_flipped = flip_image(img_input) + return img_flipped diff --git a/demo/demo_docstring.py b/examples/how_to_write_docstring.py similarity index 91% rename from demo/demo_docstring.py rename to examples/how_to_write_docstring.py index 34a8430..037c368 100644 --- a/demo/demo_docstring.py +++ b/examples/how_to_write_docstring.py @@ -1,7 +1,7 @@ import numpy as np -# this function need to be finished +# a function to be finished def cos_to_be_finished(x): # TODO: to be finished return @@ -28,5 +28,3 @@ def cos(x: float = 0.): """ return np.cos(x) - - -- GitLab