Class blog 2/13 - part 2

ECE 3340 - Han Q. Le (c)

We talked in class about creating a function. We had a discussion of what can be input of a function? is it number? string? array? is there any limit to the type of array or array dimension? 
The answer is that any computable object can be an input. Below is an example.

1. Object input of a function

Recall that we did this in Tutorial 2, and you should have a record of.

mydatarec={"your last\! name","first name", "student ID" ,"email" (* all are String *)
,(* image object: your selfie obtained earlier *)
, (* sound object: should be sound of you announcing your ID obtained above *)};

Now, we will make a function that uses the object above as input,

2. Making a function using Block or Module

First, let’s review basic steps to make a function. You should follow by typing line by line while trying to learn the structure of a Block function below.

2.1 Basic

You can use Block or Module for the basic structure below:

basicprivatefunc[x_,y_,{a_,b_,c_}]:=Block[{}
(* above the red comma: local variable definition  *)
(*  most important is the big red comma below which separate
the two parts *)
,
(*  This is the computation part, as well as the return which is 
the last part of the Block or Module  *)
]

More specific

myfunction[inputvar1_,inputvar2_,{inputvar3_,inputvar4_}]:=Block[
{ localvar1, localvar2,...
}  (* this is above the red comma  *)

,

 (* this is  below the red comma  *)
(* things to be computed, for example:  *)
localvar1=inputvar1 * inputvar2 ;
localvar2=Transpose[{inputvar3,inputvar4}] ;
(* stuffs that are output or return of the function  *)

{localvar1, ListPlot[localvar2] (* etc.  *)}
(* no semi-conlon for the output or return  *)
] ;

2.2 No need for input variables: prompt or does thing automatically

It is OK to have no input variable:

counting10:=Block[{}
,
Range[10]
]

counting10

telltime:=Block[{daterec,year,month,date, hr, min
,daynm,monthnm,hrS, minS,greeting}
,

daterec=DateList[LocalTime[]];
daynm=DayName[daterec];
monthnm=DateValue[daterec,"MonthName"];
{year,month,date,hr, min}=Take[daterec,5];
{ hrS, minS}=ToString /@ {Mod[hr,12], min};
greeting=If[hr<4,"good grief, what are you doing at this hour?"
,If[hr<6,"you are up so early"
,If[hr<12, "good morning"
,If[ hr<18,"good afternoon"
,If[hr<22,"good evening"
,"good night, you should be in bed now"]
]]]];
Speak /@ {greeting
," it's now "<>hrS<>If[min<10,"o ",""]<>minS<>If[hr<12,"a ","p "]<>"m"
,"today is ",daynm,monthnm,date};
]

telltime

2.3 Most useful functions need no input variables: it does thing automatically by looking for things, log-in etc.

checkbankstmt:=(* You can write a code that reads your bank statement,
tallies items, checks for specific things and returns what you want to know 
Also, it can do web operations, but this is iffy: Sometime it works,
sometime it doesn't: use http functions to
log in a webpage, username, password, and download info
*)
Block[{(* whatever variables you want to be local *)} 

,

bnkdownload=Import["C:\\Users\\...bankstatement.csv"] ;
relevantdata=bnkdownload[[ startrow ;;      ,   startcol ;;   ]];
(* you know your bank statement format, which row and which
column the real data are located *)
deposit=Select[relevantdata, #[[moneycol]]>0   &   ];
(*  if value in money col >0 it is deposit  *)
expenses=Sort @ Select[relevantdata, #[[moneycol]]<0   &   ];
(* sort money out from high to low *) 
(* you can write things like classifying various types
of expenses, or specific expenses using certain keywords
in the column of category, using StringMatchQ or StringContainQ *)

(* example  *)
keywords={"pizza","kroger","heb" } ;
itemcheck=Table[Select[expenses,StringContainsQ[#[[categorycol]]
,kw, IgnoreCase->True]& ]
,{kw,keywords}] ;

(* you will get a list of expenses containing each KW *)
(* make a list grid output that you want *)

]

3. A Block function that reads your record above

mydatarec={"your last\! name","first name", "student ID" ,"email" (* all are String *)
,(* image object: your selfie obtained earlier *)
, (* sound object: should be sound of you announcing your ID obtained above *)};

recordreadFunc[datarec_]:=Block[{imgavatar, voicepersonal,stuID,email
,rec0, return,stringdat,name
,ln="last name N/A",fn="firstname N/A"
,transcript
,exit}


,

If[!ListQ[datarec] || Length[datarec]==0, return="non-list or empty";
Goto[exit],];
voicepersonal=Select[datarec,AudioQ[#] &];
imgavatar=Select[datarec,ImageQ[#] &];
stringdat=Select[datarec,StringQ[#] && StringLength[#]≥ 2 &];
stuID=Select[stringdat, DigitQ[#] &];
email=Select[stringdat, StringMatchQ[#,"*@*.*"] &];
name=Select[stringdat,!MemberQ[Flatten @{stuID,email},#]&];
If[Length[name]≥2, {ln,fn}=Take[name,2]
,If[Length[name]==1,ln=name[[1]],]];

return={
If[Length[voicepersonal]>0
,{SpeechRecognize[#],Spectrogram[#]} & /@  voicepersonal
,"no audio"]

,If[Length[imgavatar]>0, imageCheck[imgavatar] ,"no picts"]

,If[Length[stuID]>0,stuID=DeleteDuplicates[stuID];
stuID,"no ID"]
,If[Length[email]>0,email=DeleteDuplicates[email];
email,"no email"]
,{ln,fn}
};
Label[exit];
return
] ;


imageCheck[imgListinp_]:=Block[{imgLoc,classobj,imgSel}
,
imgLoc=ImageAdjust[ImageResize[#,300]] & /@  imgListinp;
classobj=ImageIdentify /@  imgLoc;

If[Length[imgLoc]≥2,
imgSel=Transpose[{classobj,imgLoc}]
,imgSel={classobj,imgLoc}];
imgSel=Select[imgSel,#[[1]]== (Entity["person", Entity["Concept","Person::93r37"], "Entity[\"Concept\", \"Person::93r37\"]", "concept"])& ];
If[Length[imgSel]≥ 2
, Transpose[imgSel][[2]]
,If[Length[imgSel]==1,imgSel[[1,2]]
,"no pict"]
]
];

Now, go ahead and put in the data record you did for Tutorial 2. Below is an example.

testdatarec={, "667408"
, ,"fallingapple@gmail.com" 
,,"Newton" , 
,
, "667408" ,"Isaac"
,"fallingapple@gmail.com"
,"sirmintwarden@gov.uk"
};
testdatarec

Now, see what the function we design does to the above object:

recordreadFunc[testdatarec]

Another example

recordreadFunc[{"",,}]

mydatarec={"your last\! name","first name", "student ID" ,"email" (* all are String *)
,(* image object: your selfie obtained earlier *)
, (* sound object: should be sound of you announcing your ID obtained above *)};
recordreadFunc[mydatarec]

End

You are strongly encouraged to go through all the tutorials up to now. Send in questions if need help.