Adapting Code Introduction to OOP Code what code

  • Slides: 18
Download presentation
Adapting Code • Introduction to OOP • Code, what code? What & where •

Adapting Code • Introduction to OOP • Code, what code? What & where • Why adapt code anyway? • How? Astro-WISE Tutorial, Leiden 18 -20 August 2008

Introduction to OOP (1) • OOP is programming with classes • Programming in the

Introduction to OOP (1) • OOP is programming with classes • Programming in the problem domain – Describe a problem in one sentence, then the nouns are the classes/objects: • Let’s reduce a bias of ccd 50 of the WFI instrument – Classes are blue-prints describing real world “things” – Can also map to abstract “things” Astro-WISE Tutorial, Leiden 18 -20 August 2008

Introduction to OOP (2) • Advantages of using OOP: – Reuse of code –

Introduction to OOP (2) • Advantages of using OOP: – Reuse of code – Readability of code – Bug tracking • Important concepts: – Inheritance – Polymorphism Astro-WISE Tutorial, Leiden 18 -20 August 2008

Structure of a class, members class Cat: age = 3. 0 weight = 5.

Structure of a class, members class Cat: age = 3. 0 weight = 5. 2 attribute def miaw(self): print ‘Miaw!’ method object def kill_other_cat(self, other): . . awe> cat = Cat() awe> cat. age 3. 0 awe> cat. age = 5. 0 awe> cat. age 5. 0 Astro-WISE Tutorial, Leiden 18 -20 August 2008

Inheritance class Mammal: weight = -1 def breathe(self): . . class Cat(Mammal): inheritance def

Inheritance class Mammal: weight = -1 def breathe(self): . . class Cat(Mammal): inheritance def miaw(self): print ‘Miaw!’ def kill_other_cat(self, other): . . Astro-WISE Tutorial, Leiden 18 -20 August 2008 cat = Cat() cat. breate()

Polymorphism class Mammal: def swim(self): . . polymorphism class Cat(Mammal): def swim(self): # head

Polymorphism class Mammal: def swim(self): . . polymorphism class Cat(Mammal): def swim(self): # head up, move paws class Dolphin(Mammal): def swim(self): # head down, move fins class Toddler(Mammal): def yell(self, txt): print txt. upper() + ‘!!’ def swim(self): self. yell(‘I can’t swim’) Astro-WISE Tutorial, Leiden 18 -20 August 2008

Getting a checkout • CVS checkout = local copy of the code – Two

Getting a checkout • CVS checkout = local copy of the code – Two versions: AWBASE and “most recent’’ • Must use “pserver” to log in anonymously – User anoncvs, password <ask representative, on request> >cvs –d cvs. astro-wise. org: /cvsroot co awe >cvs -d : pserver: anoncvs@cvs. astrowise. org: /cvsroot login ># base version >cvs -d : pserver: anoncvs@cvs. astro-wise. org: /cvsroot checkout -r AWBASE awe Astro-WISE Tutorial, Leiden 18 -20 August 2008

Using your own checkout • Set environment AWEPIPE to point to the checkout directory

Using your own checkout • Set environment AWEPIPE to point to the checkout directory (“awe”) > setenv AWEPIPE /data/users/helmich/awe (add to. cshrc) Astro-WISE Tutorial, Leiden 18 -20 August 2008

Why adapt code? • Hopefully not often necessary • Make scripts to speed up

Why adapt code? • Hopefully not often necessary • Make scripts to speed up process, and as a memory tool • Create objects with your own methods Astro-WISE Tutorial, Leiden 18 -20 August 2008

Directory structure • Top level: awe • common, astro, lofar, ai – Isolate specific

Directory structure • Top level: awe • common, astro, lofar, ai – Isolate specific code Astro-WISE Tutorial, Leiden 18 -20 August 2008

Directory structure: common config database log math net services toolbox util | Startup and

Directory structure: common config database log math net services toolbox util | Startup and environment/configuration files | Persistency mechanism and implementations of its interface | Logging and messaging | Mathematical Python routines such as statistics & least squares | General network-related python modules | Common (web-) services | Scripts for e. g. maintenance of the database, installation, etc. | Routines for compression, checksumming, datetime manipulation. Astro-WISE Tutorial, Leiden 18 -20 August 2008

Directory structure: astro config database experimental external | Startup and environment/configuration files | Astronomy

Directory structure: astro config database experimental external | Startup and environment/configuration files | Astronomy specific database modules | Experimental modules | Python wrappers for tools such as LDAC, Sextractor and Swarp filerecipes | Data reduction recipes for use without database instrument | Instrument specific modules main | Modules for the pipeline processing of images plot | Various plotting modules recipes | Data reduction recipes (to create classes in "main") services | (Web-) services for astronomy test | Unittests toolbox | Scripts for ingestion, installation, various maintenance util | Utility modules usable throughout the code Astro-WISE Tutorial, Leiden 18 -20 August 2008

Making a script (1/2) • Based around dpu instance • Based around a task

Making a script (1/2) • Based around dpu instance • Based around a task • As a sort of task • Write the script in a file with. py extension • Run with this command: > awe script. py Astro-WISE Tutorial, Leiden 18 -20 August 2008

Making a script (2/2) • Classes/models need to be imported • Dotted (directory) structure:

Making a script (2/2) • Classes/models need to be imported • Dotted (directory) structure: – from astro. recipes. Reduce import Reduce. Task Astro-WISE Tutorial, Leiden 18 -20 August 2008

Script based around dpu from astro. recipes. mods. dpu import Processor from astro. main.

Script based around dpu from astro. recipes. mods. dpu import Processor from astro. main. Raw. Frame import Raw. Bias. Frame dpu = Processor('dpu. hpc. rug. astro-wise. org') ccd 50 = [r. filename for r in Raw. Bias. Frame. select(date='2000 -04 -28', instrument='WFI', chip='ccd 50')] ccd 51 = [r. filename for r in Raw. Bias. Frame. select(date='2000 -04 -28', instrument='WFI', chip='ccd 51')] dpu. run('Bias', instrument='WFI', raw_filenames=ccd 50) dpu. run('Bias', instrument='WFI', raw_filenames=ccd 51) Astro-WISE Tutorial, Leiden 18 -20 August 2008

Script based around Task • In particular to store configuration from astro. recipes. Gal.

Script based around Task • In particular to store configuration from astro. recipes. Gal. Phot import Gal. Phot. Task from astro. util. Pars import Pars p=Pars(Gal. Phot. Task) p. Gal. Phot. Model. process_params. r 1 = 10. 0 p. Gal. Phot. Model. process_params. dangmax=0. 2 # etc. task = Gal. Phot. Task(instrument='WFI', slid=423431, sids=[16246], pars=p. get(), commit=0) task. execute() Astro-WISE Tutorial, Leiden 18 -20 August 2008

Simple eclipse script • Eclipse is used for image arithmetic import eclipse import glob

Simple eclipse script • Eclipse is used for image arithmetic import eclipse import glob filenames = glob('*. red. fits') images = [eclipse. image(filename) for filename in filenames] cube = eclipse. cube(images) median = cube. median() median. save('eclipse_med. fits') Astro-WISE Tutorial, Leiden 18 -20 August 2008

Adapting classes • Process. Target classes are located in astro. main – Lets look

Adapting classes • Process. Target classes are located in astro. main – Lets look at Bias. Frame: class Bias. Frame(Base. Frame): . . def make_image(self). . cube = eclipse. cube([raw. image for raw in self. raw_bias_frames]) # current method does an average with sigma rejection self. image = cube. median() # use a median instead Astro-WISE Tutorial, Leiden 18 -20 August 2008